src/kdtree.cc
branchpyrit
changeset 23 7e258561a690
parent 22 76b7bd51d64a
child 24 d0d76e8a5203
equal deleted inserted replaced
22:76b7bd51d64a 23:7e258561a690
   184 
   184 
   185 	// choose best split pos
   185 	// choose best split pos
   186 	const Float K = 1.4; // constant, K = cost of traversal / cost of ray-triangle intersection
   186 	const Float K = 1.4; // constant, K = cost of traversal / cost of ray-triangle intersection
   187 	Float SAV = 2*(bbox.w()*bbox.h() + bbox.w()*bbox.d() + bbox.h()*bbox.d()); // surface area of node
   187 	Float SAV = 2*(bbox.w()*bbox.h() + bbox.w()*bbox.d() + bbox.h()*bbox.d()); // surface area of node
   188 	Float cost = SAV * (K + shapes->size()); // initial cost = non-split cost
   188 	Float cost = SAV * (K + shapes->size()); // initial cost = non-split cost
   189 	SplitPos *splitpos = NULL;
       
   190 	bool leaf = true;
   189 	bool leaf = true;
   191 	BBox lbb = bbox;
   190 	BBox lbb = bbox;
   192 	BBox rbb = bbox;
   191 	BBox rbb = bbox;
   193 	for (spl = splitlist.begin(); spl != splitlist.end(); spl++)
   192 	for (spl = splitlist.begin(); spl != splitlist.end(); spl++)
   194 	{
   193 	{
   201 
   200 
   202 		if (splitcost < cost)
   201 		if (splitcost < cost)
   203 		{
   202 		{
   204 			leaf = false;
   203 			leaf = false;
   205 			cost = splitcost;
   204 			cost = splitcost;
   206 			splitpos = &*spl;
   205 			split = spl->pos;
   207 		}
   206 		}
   208 	}
   207 	}
   209 
   208 
   210 	if (leaf)
   209 	if (leaf)
   211 	{
   210 	{
   234 	F << "v " << v.x << " " << v.y << " " << v.z << endl;
   233 	F << "v " << v.x << " " << v.y << " " << v.z << endl;
   235 	F << "f " << f+1 << " " << f+2 << " " << f+3 << " " << f+4 << endl;
   234 	F << "f " << f+1 << " " << f+2 << " " << f+3 << " " << f+4 << endl;
   236 	f += 4;
   235 	f += 4;
   237 #endif
   236 #endif
   238 
   237 
   239 	split = splitpos->pos;
       
   240 	Float lnum = splitpos->lnum;
       
   241 	Float rnum = splitpos->rnum;
       
   242 
       
   243 	// split this node
   238 	// split this node
   244 	delete shapes;
   239 	delete shapes;
   245 	children = new KdNode[2];
   240 	children = new KdNode[2];
   246 	int state = 0;
   241 	int state = 0;
   247 	for (sh = sslist.begin(); sh != sslist.end(); sh++)
   242 	for (sh = sslist.begin(); sh != sslist.end(); sh++)
   277 				} else
   272 				} else
   278 				{ // contained
   273 				{ // contained
   279 					if (split - bbox.L.cell[axis] < bbox.H.cell[axis] - split)
   274 					if (split - bbox.L.cell[axis] < bbox.H.cell[axis] - split)
   280 					{
   275 					{
   281 						// left subcell is smaller -> if not empty, put shape here
   276 						// left subcell is smaller -> if not empty, put shape here
   282 						if (lnum)
   277 						if (children[0].shapes->size())
   283 							children[0].addShape(sh->shape);
   278 							children[0].addShape(sh->shape);
   284 						else
   279 						else
   285 							children[1].addShape(sh->shape);
   280 							children[1].addShape(sh->shape);
   286 					} else {
   281 					} else {
   287 						// right subcell is smaller
   282 						// right subcell is smaller
   288 						if (rnum)
   283 						if (children[1].shapes->size())
   289 							children[1].addShape(sh->shape);
   284 							children[1].addShape(sh->shape);
   290 						else
   285 						else
   291 							children[0].addShape(sh->shape);
   286 							children[0].addShape(sh->shape);
   292 					}
   287 					}
   293 				}
   288 				}