src/kdtree.cc
branchpyrit
changeset 95 ca7d4c665531
parent 93 96d65f841791
child 103 3b3257a410fe
equal deleted inserted replaced
94:4c8abb8977dc 95:ca7d4c665531
    33 #include "serialize.h"
    33 #include "serialize.h"
    34 
    34 
    35 class ShapeBound
    35 class ShapeBound
    36 {
    36 {
    37 public:
    37 public:
    38 	Shape *shape;
    38 	const Shape *shape;
    39 	Float pos;
    39 	Float pos;
    40 	bool end;
    40 	bool end;
    41 	ShapeBound(Shape *ashape, const Float apos, const bool aend):
    41 	ShapeBound(const Shape *ashape, const Float apos, const bool aend):
    42 		shape(ashape), pos(apos), end(aend) {};
    42 		shape(ashape), pos(apos), end(aend) {};
    43 	friend bool operator<(const ShapeBound& a, const ShapeBound& b)
    43 	friend bool operator<(const ShapeBound& a, const ShapeBound& b)
    44 	{
    44 	{
    45 		if (a.pos == b.pos)
    45 		if (a.pos == b.pos)
    46 			return a.shape < b.shape;
    46 			return a.shape < b.shape;
   202 	recursive_build(root, bbox, max_depth);
   202 	recursive_build(root, bbox, max_depth);
   203 	built = true;
   203 	built = true;
   204 }
   204 }
   205 
   205 
   206 /* algorithm by Vlastimil Havran, Heuristic Ray Shooting Algorithms, appendix C */
   206 /* algorithm by Vlastimil Havran, Heuristic Ray Shooting Algorithms, appendix C */
   207 Shape *KdTree::nearest_intersection(const Shape *origin_shape, const Ray &ray,
   207 const Shape *KdTree::nearest_intersection(const Shape *origin_shape, const Ray &ray,
   208 	Float &nearest_distance)
   208 	Float &nearest_distance)
   209 {
   209 {
   210 	Float a, b; /* entry/exit signed distance */
   210 	Float a, b; /* entry/exit signed distance */
   211 	Float t;    /* signed distance to the splitting plane */
   211 	Float t;    /* signed distance to the splitting plane */
   212 
   212 
   307 			stack[exit].pb[mod3[axis+2]] = ray.o[mod3[axis+2]]
   307 			stack[exit].pb[mod3[axis+2]] = ray.o[mod3[axis+2]]
   308 				+ t * ray.dir[mod3[axis+2]];
   308 				+ t * ray.dir[mod3[axis+2]];
   309 		}
   309 		}
   310 
   310 
   311 		/* current node is the leaf . . . empty or full */
   311 		/* current node is the leaf . . . empty or full */
   312 		Shape *nearest_shape = NULL;
   312 		const Shape *nearest_shape = NULL;
   313 		Float dist = stack[exit].t;
   313 		Float dist = stack[exit].t;
   314 		ShapeList::iterator shape;
   314 		ShapeList::iterator shape;
   315 		for (shape = node->getShapes()->begin(); shape != node->getShapes()->end(); shape++)
   315 		for (shape = node->getShapes()->begin(); shape != node->getShapes()->end(); shape++)
   316 			if (*shape != origin_shape && (*shape)->intersect(ray, dist)
   316 			if (*shape != origin_shape && (*shape)->intersect(ray, dist)
   317 			&& dist >= stack[entry].t - Eps)
   317 			&& dist >= stack[entry].t - Eps)
   344 	VectorPacket pb; /* the coordinates of entry/exit point */
   344 	VectorPacket pb; /* the coordinates of entry/exit point */
   345 	int prev;
   345 	int prev;
   346 };
   346 };
   347 
   347 
   348 void KdTree::packet_intersection(const Shape* const* origin_shapes, const RayPacket &rays,
   348 void KdTree::packet_intersection(const Shape* const* origin_shapes, const RayPacket &rays,
   349 		Float *nearest_distances, Shape **nearest_shapes)
   349 		Float *nearest_distances, const Shape **nearest_shapes)
   350 {
   350 {
   351 	mfloat4 a, b; /* entry/exit signed distance */
   351 	mfloat4 a, b; /* entry/exit signed distance */
   352 	mfloat4 t;    /* signed distance to the splitting plane */
   352 	mfloat4 t;    /* signed distance to the splitting plane */
   353 	mfloat4 mask = mZero;
   353 	mfloat4 mask = mZero;
   354 
   354 
   543 		st << *node.getRightChild() << ")";
   543 		st << *node.getRightChild() << ")";
   544 	}
   544 	}
   545 	return st;
   545 	return st;
   546 }
   546 }
   547 
   547 
   548 ostream & KdTree::dump(ostream &st)
   548 ostream & KdTree::dump(ostream &st) const
   549 {
   549 {
   550 	if (!built)
   550 	if (!built)
   551 		return Container::dump(st);
   551 		return Container::dump(st);
   552 
   552 
   553 	st << "(kdtree," << shapes.size();
   553 	st << "(kdtree," << shapes.size();
   554 	ShapeList::iterator shape;
   554 	ShapeList::const_iterator shape;
   555 	for (shape = shapes.begin(); shape != shapes.end(); shape++)
   555 	for (shape = shapes.begin(); shape != shapes.end(); shape++)
   556 	{
   556 	{
   557 		int idx;
   557 		int idx;
   558 		if (!shape_index.get(*shape, idx))
   558 		if (!shape_index.get(*shape, idx))
   559 			st << "," << endl << **shape;
   559 			st << "," << endl << **shape;