60 |
65 |
61 void setSplit(Float aSplit) { split = aSplit; }; |
66 void setSplit(Float aSplit) { split = aSplit; }; |
62 const Float& getSplit() const { return split; }; |
67 const Float& getSplit() const { return split; }; |
63 |
68 |
64 void setChildren(KdNode *node) { children = node; assert((flags & 3) == 0); }; |
69 void setChildren(KdNode *node) { children = node; assert((flags & 3) == 0); }; |
65 KdNode* getLeftChild() const { return (KdNode*)((off_t)children & ~3); }; |
70 KdNode* getLeftChild() const { return (KdNode*)((size_t)children & ~3); }; |
66 KdNode* getRightChild() const { return (KdNode*)((off_t)children & ~3) + 1; }; |
71 KdNode* getRightChild() const { return (KdNode*)(((size_t)children & ~3) + 16); }; |
67 |
72 |
68 ShapeList* getShapes() const { return (ShapeList*)((off_t)shapes & ~3); }; |
73 ShapeList* getShapes() const { return (ShapeList*)((size_t)shapes & ~3); }; |
69 void addShape(Shape* aShape) { getShapes()->push_back(aShape); }; |
74 void addShape(Shape* aShape) { getShapes()->push_back(aShape); }; |
70 }; |
75 }; |
71 |
76 |
72 /** |
77 /** |
73 * kd-tree |
78 * kd-tree |
74 */ |
79 */ |
75 class KdTree: public Container |
80 class KdTree: public Container |
76 { |
81 { |
77 KdNode *root; |
82 KdNode *root; |
78 bool built; |
83 bool built; |
79 int max_depth; |
84 const int max_depth; |
|
85 MemoryPool<KdNode> mempool; |
80 |
86 |
81 void recursive_build(KdNode *node, BBox bbox, int maxdepth); |
87 void recursive_build(KdNode *node, const BBox &bbox, int maxdepth); |
82 void recursive_load(istream &st, KdNode *node); |
88 void recursive_load(istream &st, KdNode *node); |
83 public: |
89 public: |
84 KdTree() : Container(), root(NULL), built(false), max_depth(32) {}; |
90 KdTree(): Container(), root(NULL), built(false), max_depth(32), mempool(64) {}; |
|
91 KdTree(int maxdepth): Container(), root(NULL), built(false), max_depth(maxdepth), mempool(64) {}; |
85 ~KdTree() { if (root) delete root; }; |
92 ~KdTree() { if (root) delete root; }; |
86 void addShape(Shape* aShape) { Container::addShape(aShape); built = false; }; |
93 void addShape(Shape* aShape) { Container::addShape(aShape); built = false; }; |
87 Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray, |
94 Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray, |
88 Float &nearest_distance); |
95 Float &nearest_distance); |
89 #ifndef NO_SSE |
96 #ifndef NO_SIMD |
90 void packet_intersection(const Shape **origin_shapes, const RayPacket &rays, |
97 void packet_intersection(const Shape* const* origin_shapes, const RayPacket &rays, |
91 Float *nearest_distances, Shape **nearest_shapes); |
98 Float *nearest_distances, Shape **nearest_shapes); |
92 #endif |
99 #endif |
93 void optimize() { build(); }; |
100 void optimize() { build(); }; |
94 void build(); |
101 void build(); |
95 bool isBuilt() const { return built; }; |
102 bool isBuilt() const { return built; }; |
96 KdNode *getRootNode() const { return root; }; |
103 KdNode *getRootNode() const { return root; }; |
97 void setMaxDepth(int md) { max_depth = md; }; |
|
98 |
104 |
99 ostream & dump(ostream &st); |
105 ostream & dump(ostream &st); |
100 istream & load(istream &st, Material *mat); |
106 istream & load(istream &st, Material *mat); |
101 }; |
107 }; |
102 |
108 |