src/octree.cc
branchpyrit
changeset 92 9af5c039b678
parent 91 9d66d323c354
child 93 96d65f841791
equal deleted inserted replaced
91:9d66d323c354 92:9af5c039b678
    35 	}
    35 	}
    36 	else
    36 	else
    37 		delete[] children;
    37 		delete[] children;
    38 }
    38 }
    39 
    39 
    40 void OctreeNode::subdivide(BBox bbox, int maxdepth)
    40 void OctreeNode::subdivide(const BBox &bbox, int maxdepth)
    41 {
    41 {
    42 	ShapeList *l_shapes = getShapes();
    42 	ShapeList *l_shapes = getShapes();
    43 
    43 
    44 	// prepare children (this also sets this node as non-leaf)
    44 	// prepare children (this also sets this node as non-leaf)
    45 	makeChildren();
    45 	makeChildren();
    46 
    46 
    47 	// evaluate centres for axes
    47 	// evaluate centres for axes
    48 	const Float xsplit = (bbox.L.x + bbox.H.x)*0.5;
    48 	const Float xsplit = (bbox.L.x + bbox.H.x)*0.5f;
    49 	const Float ysplit = (bbox.L.y + bbox.H.y)*0.5;
    49 	const Float ysplit = (bbox.L.y + bbox.H.y)*0.5f;
    50 	const Float zsplit = (bbox.L.z + bbox.H.z)*0.5;
    50 	const Float zsplit = (bbox.L.z + bbox.H.z)*0.5f;
    51 
    51 
    52 	// set bounding boxes for children
    52 	// set bounding boxes for children
    53 	BBox childbb[8] = {bbox, bbox, bbox, bbox, bbox, bbox, bbox, bbox};
    53 	BBox childbb[8] = {bbox, bbox, bbox, bbox, bbox, bbox, bbox, bbox};
    54 	for (int i = 0; i < 4; i++)
    54 	for (int i = 0; i < 4; i++)
    55 	{
    55 	{
   153 {
   153 {
   154 	/* if we have no tree, fall back to naive test */
   154 	/* if we have no tree, fall back to naive test */
   155 	if (!built)
   155 	if (!built)
   156 		return Container::nearest_intersection(origin_shape, ray, nearest_distance);
   156 		return Container::nearest_intersection(origin_shape, ray, nearest_distance);
   157 
   157 
       
   158 #ifdef MSVC
       
   159 	// MSVC wants constant expression here... hope it won't overflow :)
       
   160 	OctreeTravState st[20];
       
   161 #else
   158 	OctreeTravState st[max_depth+1];
   162 	OctreeTravState st[max_depth+1];
       
   163 #endif
   159 	register OctreeTravState *st_cur = st;
   164 	register OctreeTravState *st_cur = st;
   160 
   165 
   161 #	define node	st_cur->node
   166 #	define node	st_cur->node
   162 #	define tx0	st_cur->tx0
   167 #	define tx0	st_cur->tx0
   163 #	define ty0	st_cur->ty0
   168 #	define ty0	st_cur->ty0
   190 		ro.z = (bbox.L.z+bbox.H.z) - ro.z;
   195 		ro.z = (bbox.L.z+bbox.H.z) - ro.z;
   191 		rdir.z = -rdir.z;
   196 		rdir.z = -rdir.z;
   192 		a |= 1;
   197 		a |= 1;
   193 	}
   198 	}
   194 
   199 
   195 	if (rdir.x == 0.0) rdir.x = Eps;
   200 	if (rdir.x == 0.0f) rdir.x = Eps;
   196 	if (rdir.y == 0.0) rdir.y = Eps;
   201 	if (rdir.y == 0.0f) rdir.y = Eps;
   197 	if (rdir.z == 0.0) rdir.z = Eps;
   202 	if (rdir.z == 0.0f) rdir.z = Eps;
   198 	rdir.x = 1.0/rdir.x;
   203 	rdir.x = 1.0f/rdir.x;
   199 	rdir.y = 1.0/rdir.y;
   204 	rdir.y = 1.0f/rdir.y;
   200 	rdir.z = 1.0/rdir.z;
   205 	rdir.z = 1.0f/rdir.z;
   201 
   206 
   202 	tx0 = (bbox.L.x - ro.x) * rdir.x;
   207 	tx0 = (bbox.L.x - ro.x) * rdir.x;
   203 	tx1 = (bbox.H.x - ro.x) * rdir.x;
   208 	tx1 = (bbox.H.x - ro.x) * rdir.x;
   204 	ty0 = (bbox.L.y - ro.y) * rdir.y;
   209 	ty0 = (bbox.L.y - ro.y) * rdir.y;
   205 	ty1 = (bbox.H.y - ro.y) * rdir.y;
   210 	ty1 = (bbox.H.y - ro.y) * rdir.y;
   236 					if (nearest_shape != NULL)
   241 					if (nearest_shape != NULL)
   237 						return nearest_shape;
   242 						return nearest_shape;
   238 				}
   243 				}
   239 				else
   244 				else
   240 				{
   245 				{
   241 					txm = 0.5 * (tx0+tx1);
   246 					txm = 0.5f * (tx0+tx1);
   242 					tym = 0.5 * (ty0+ty1);
   247 					tym = 0.5f * (ty0+ty1);
   243 					tzm = 0.5 * (tz0+tz1);
   248 					tzm = 0.5f * (tz0+tz1);
   244 
   249 
   245 					// first node
   250 					// first node
   246 					st_cur->next = 0;
   251 					st_cur->next = 0;
   247 					if (tx0 > ty0)
   252 					if (tx0 > ty0)
   248 					{
   253 					{