src/kdtree.cc
branchpyrit
changeset 11 4d192e13ee84
parent 10 f9fad94cd0cc
child 12 f4fcabf05785
equal deleted inserted replaced
10:f9fad94cd0cc 11:4d192e13ee84
    17 		if (shapebb.R.x > bbox.R.x)  bbox.R.x = shapebb.R.x;
    17 		if (shapebb.R.x > bbox.R.x)  bbox.R.x = shapebb.R.x;
    18 		if (shapebb.R.y > bbox.R.y)  bbox.R.y = shapebb.R.y;
    18 		if (shapebb.R.y > bbox.R.y)  bbox.R.y = shapebb.R.y;
    19 		if (shapebb.R.z > bbox.R.z)  bbox.R.z = shapebb.R.z;
    19 		if (shapebb.R.z > bbox.R.z)  bbox.R.z = shapebb.R.z;
    20 	}
    20 	}
    21 };
    21 };
       
    22 
       
    23 Shape *Container::nearest_intersection(const Shape *origin_shape, const Ray &ray,
       
    24 	float &nearest_distance)
       
    25 {
       
    26 	Shape *nearest_shape = NULL;
       
    27 	ShapeList::iterator shape;
       
    28 	for (shape = shapes.begin(); shape != shapes.end(); shape++)
       
    29 		if (*shape != origin_shape && (*shape)->intersect(ray, nearest_distance))
       
    30 			nearest_shape = *shape;
       
    31 	return nearest_shape;
       
    32 }
    22 
    33 
    23 void KdNode::subdivide(BBox bbox, int depth)
    34 void KdNode::subdivide(BBox bbox, int depth)
    24 {
    35 {
    25 	if (depth >= 10 || shapes.size() <= 2)
    36 	if (depth >= 10 || shapes.size() <= 2)
    26 	{
    37 	{
   217 
   228 
   218 	children[0].subdivide(lbb, depth+1);
   229 	children[0].subdivide(lbb, depth+1);
   219 	children[1].subdivide(rbb, depth+1);
   230 	children[1].subdivide(rbb, depth+1);
   220 }
   231 }
   221 
   232 
   222 void KdTree::optimize()
   233 void KdTree::build()
   223 {
   234 {
   224 	root = new KdNode();
   235 	root = new KdNode();
   225 	root->shapes = shapes;
   236 	root->shapes = shapes;
   226 	root->subdivide(bbox, 0);
   237 	root->subdivide(bbox, 0);
   227 	built = true;
   238 	built = true;
   228 }
   239 }
   229 
   240 
       
   241 // this should save whole kd-tree with triangles distributed into leaves
   230 void KdTree::save(ostream &str, KdNode *node)
   242 void KdTree::save(ostream &str, KdNode *node)
   231 {
   243 {
   232 	if (!built)
   244 	if (!built)
   233 		return;
   245 		return;
   234 	if (node == NULL)
   246 	if (node == NULL)
   242 		str << "; R=";
   254 		str << "; R=";
   243 		save(str, node->getRightChild());
   255 		save(str, node->getRightChild());
   244 		str << ";)";
   256 		str << ";)";
   245 	}
   257 	}
   246 }
   258 }
       
   259 
       
   260 // load kd-tree from file/stream
       
   261 void KdTree::load(istream &str, KdNode *node)
       
   262 {
       
   263 	
       
   264 }