Nori  24
bvh.h
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by Wenzel Jakob, Romain Prévost
5 
6  Nori is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Nori is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #if !defined(__NORI_BVH_H)
20 #define __NORI_BVH_H
21 
22 #include <nori/shape.h>
23 
24 NORI_NAMESPACE_BEGIN
25 
43 class BVH {
44  friend class BVHBuildTask;
45 public:
47  BVH() { m_shapeOffset.push_back(0u); }
48 
50  virtual ~BVH() { clear(); };
51 
53  void clear();
54 
60  void addShape(Shape *shape);
61 
63  void build();
64 
80  bool rayIntersect(const Ray3f &ray, Intersection &its,
81  bool shadowRay = false) const;
82 
84  uint32_t getShapeCount() const { return (uint32_t) m_shapes.size(); }
85 
87  uint32_t getPrimitiveCount() const { return m_shapeOffset.back(); }
88 
90  Shape *getShape(uint32_t idx) { return m_shapes[idx]; }
91 
93  const Shape *getShape(uint32_t idx) const { return m_shapes[idx]; }
94 
96  const BoundingBox3f &getBoundingBox() const {
97  return m_bbox;
98  }
99 
100 protected:
105  uint32_t findShape(uint32_t &idx) const {
106  auto it = std::lower_bound(m_shapeOffset.begin(), m_shapeOffset.end(), idx+1) - 1;
107  idx -= *it;
108  return (uint32_t) (it - m_shapeOffset.begin());
109  }
110 
112  BoundingBox3f getBoundingBox(uint32_t index) const {
113  uint32_t shapeIdx = findShape(index);
114  return m_shapes[shapeIdx]->getBoundingBox(index);
115  }
116 
118  Point3f getCentroid(uint32_t index) const {
119  uint32_t shapeIdx = findShape(index);
120  return m_shapes[shapeIdx]->getCentroid(index);
121  }
122 
124  std::pair<float, uint32_t> statistics(uint32_t index = 0) const;
125 
126  /* BVH node in 32 bytes */
127  struct BVHNode {
128  union {
129  struct {
130  unsigned flag : 1;
131  uint32_t size : 31;
132  uint32_t start;
133  } leaf;
134 
135  struct {
136  unsigned flag : 1;
137  uint32_t axis : 31;
138  uint32_t rightChild;
139  } inner;
140 
141  uint64_t data;
142  };
143  BoundingBox3f bbox;
144 
145  bool isLeaf() const {
146  return leaf.flag == 1;
147  }
148 
149  bool isInner() const {
150  return leaf.flag == 0;
151  }
152 
153  bool isUnused() const {
154  return data == 0;
155  }
156 
157  uint32_t start() const {
158  return leaf.start;
159  }
160 
161  uint32_t end() const {
162  return leaf.start + leaf.size;
163  }
164  };
165 private:
166  std::vector<Shape *> m_shapes;
167  std::vector<uint32_t> m_shapeOffset;
168  std::vector<BVHNode> m_nodes;
169  std::vector<uint32_t> m_indices;
170  BoundingBox3f m_bbox;
171 };
172 
173 NORI_NAMESPACE_END
174 
175 #endif /* __NORI_BVH_H */
Build task for parallel BVH construction.
Definition: bvh.cpp:54
Bounding Volume Hierarchy for fast ray intersection queries.
Definition: bvh.h:43
uint32_t findShape(uint32_t &idx) const
Compute the shape and primitive indices corresponding to a primitive index used by the underlying gen...
Definition: bvh.h:105
bool rayIntersect(const Ray3f &ray, Intersection &its, bool shadowRay=false) const
Intersect a ray against all shapes registered with the BVH.
Definition: bvh.cpp:404
Shape * getShape(uint32_t idx)
Return one of the registered shapes.
Definition: bvh.h:90
std::pair< float, uint32_t > statistics(uint32_t index=0) const
Compute internal tree statistics.
Definition: bvh.cpp:384
uint32_t getPrimitiveCount() const
Return the total number of internally represented primitives.
Definition: bvh.h:87
virtual ~BVH()
Release all resources.
Definition: bvh.h:50
void build()
Build the BVH.
Definition: bvh.cpp:329
const Shape * getShape(uint32_t idx) const
Return one of the registered shapes (const version)
Definition: bvh.h:93
void clear()
Release all resources.
Definition: bvh.cpp:314
uint32_t getShapeCount() const
Return the total number of shapes registered with the BVH.
Definition: bvh.h:84
BVH()
Create a new and empty BVH.
Definition: bvh.h:47
void addShape(Shape *shape)
Register a shape for inclusion in the BVH.
Definition: bvh.cpp:308
Superclass of all shapes.
Definition: shape.h:97
Intersection data structure.
Definition: shape.h:37