20 #if !defined(__MITSUBA_RENDER_SAHKDTREE2_H_)
21 #define __MITSUBA_RENDER_SAHKDTREE2_H_
44 m_extents = aabb.getExtents();
45 m_normalization = 1.0f / (m_extents.x + m_extents.y);
55 return std::pair<Float, Float>(
56 (m_extents[1-axis] + leftWidth) * m_normalization,
57 (m_extents[1-axis] + rightWidth) * m_normalization);
65 return aabb.getSurfaceArea();
68 Float m_normalization;
92 template <
typename Derived>
100 using Parent::m_nodes;
101 using Parent::m_aabb;
102 using Parent::m_indices;
106 SizeType primCount = cast()->getPrimitiveCount();
107 KDLog(
EInfo,
"Constructing a SAH kd-tree (%i primitives) ..", primCount);
113 return static_cast<Derived *
>(
this);
117 inline const Derived *
cast()
const {
118 return static_cast<const Derived *
>(
this);
146 Float &t,
void *temp)
const {
151 stack[enPt].
t = mint;
152 stack[enPt].
p = ray(mint);
156 stack[exPt].
t = maxt;
157 stack[exPt].
p = ray(maxt);
158 stack[exPt].
node = NULL;
160 bool foundIntersection =
false;
161 const KDNode * __restrict currNode = m_nodes;
162 while (currNode != NULL) {
163 while (EXPECT_TAKEN(!currNode->isLeaf())) {
164 const Float splitVal = (
Float) currNode->getSplit();
165 const int axis = currNode->getAxis();
166 const KDNode * __restrict farChild;
168 if (stack[enPt].p[axis] <= splitVal) {
169 if (stack[exPt].p[axis] <= splitVal) {
171 currNode = currNode->getLeft();
178 if (stack[enPt].p[axis] == splitVal) {
180 currNode = currNode->getRight();
185 currNode = currNode->getLeft();
186 farChild = currNode + 1;
188 if (splitVal < stack[exPt].p[axis]) {
190 currNode = currNode->getRight();
194 farChild = currNode->getLeft();
195 currNode = farChild + 1;
199 Float distToSplit = (splitVal - ray.o[axis]) * ray.dRcp[axis];
207 stack[exPt].
prev = tmp;
208 stack[exPt].
t = distToSplit;
209 stack[exPt].
node = farChild;
213 stack[exPt].
p = ray(distToSplit);
214 stack[exPt].
p[axis] = splitVal;
218 for (
IndexType entry=currNode->getPrimStart(),
219 last = currNode->getPrimEnd(); entry != last; entry++) {
220 const IndexType primIdx = m_indices[entry];
222 bool result = cast()->intersect(ray, primIdx, mint, maxt, t, temp);
226 foundIntersection =
true;
230 if (stack[exPt].t > maxt)
235 currNode = stack[exPt].
node;
236 exPt = stack[enPt].
prev;
239 return foundIntersection;
#define KDLog(level, fmt,...)
Definition: gkdtree.h:642
SurfaceAreaHeuristic2(const AABB2 &aabb)
Initialize the surface area heuristic with the bounds of a parent node.
Definition: sahkdtree2.h:43
Derived * cast()
Cast to the derived class.
Definition: sahkdtree2.h:112
KDTreeBase< AABB2 >::SizeType SizeType
Definition: sahkdtree2.h:96
Point2 p
Definition: sahkdtree2.h:136
Float t
Definition: sahkdtree2.h:132
std::pair< Float, Float > operator()(int axis, Float leftWidth, Float rightWidth) const
Definition: sahkdtree2.h:54
#define KDAssert(expr)
Definition: gkdtree.h:56
Base class of all kd-trees.
Definition: gkdtree.h:441
uint32_t prev
Definition: sahkdtree2.h:134
#define MTS_KD_MAXDEPTH
Activate lots of extra checks.
Definition: gkdtree.h:37
static Float getQuantity(const AABB2 &aabb)
Definition: sahkdtree2.h:64
More relevant debug / information message.
Definition: formatter.h:31
Implements the 2D surface area heuristic for use by the GenericKDTree construction algorithm...
Definition: sahkdtree2.h:34
const KDNode *__restrict node
Definition: sahkdtree2.h:130
KDTreeBase< AABB2 >::IndexType IndexType
Definition: sahkdtree2.h:97
Optimized KD-tree acceleration data structure for n-dimensional (n<=4) shapes and various queries on ...
Definition: gkdtree.h:706
Ray traversal stack entry for Wald-style incoherent ray tracing.
Definition: sahkdtree2.h:122
KDTreeBase< AABB2 >::KDNode KDNode
Definition: sahkdtree2.h:98
Float mint
Definition: sahkdtree2.h:124
GenericKDTree< AABB2, SurfaceAreaHeuristic2, Derived > Parent
Definition: sahkdtree2.h:95
const KDNode *__restrict node
Definition: sahkdtree2.h:123
Specializes GenericKDTree to a two-dimensional tree to be used for flatland ray tracing.
Definition: sahkdtree2.h:93
Ray traversal stack entry for Havran-style incoherent ray tracing.
Definition: sahkdtree2.h:128
const Derived * cast() const
Cast to the derived class (const version)
Definition: sahkdtree2.h:117
FINLINE bool rayIntersectHavran(const Ray2 &ray, Float mint, Float maxt, Float &t, void *temp) const
Ray tracing kd-tree traversal loop (Havran variant)
Definition: sahkdtree2.h:145
void buildInternal()
Definition: sahkdtree2.h:105