Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
node.h
Go to the documentation of this file.
1#ifndef BVH_V2_NODE_H
2#define BVH_V2_NODE_H
3
4#include "bvh/v2/index.h"
5#include "bvh/v2/vec.h"
6#include "bvh/v2/bbox.h"
7#include "bvh/v2/ray.h"
8#include "bvh/v2/stream.h"
9
10#include <cassert>
11#include <array>
12#include <limits>
13
14namespace bvh::v2 {
15
16/// Binary BVH node, containing its bounds and an index into its children or the primitives it
17/// contains. By definition, inner BVH nodes do not contain primitives; only leaves do.
18template <
19 typename T,
20 size_t Dim,
21 size_t IndexBits = sizeof(T) * CHAR_BIT,
22 size_t PrimCountBits = 4>
23struct Node {
24 using Scalar = T;
26
27 static constexpr size_t dimension = Dim;
28 static constexpr size_t prim_count_bits = PrimCountBits;
29 static constexpr size_t index_bits = IndexBits;
30
31 /// Bounds of the node, laid out in memory as `[min_x, max_x, min_y, max_y, ...]`. Users should
32 /// not really depend on a specific order and instead use `get_bbox()` and extract the `min`
33 /// and/or `max` components accordingly.
34 std::array<T, Dim * 2> bounds;
35
36 /// Index to the children of an inner node, or to the primitives for a leaf node.
38
39 Node() = default;
40
41 // bool operator == (const Node&) const = default;
42 // bool operator != (const Node&) const = default;
43 bool operator != (const Node& other) const {
44 return other.bounds == bounds;
45 }
46 bool operator == (const Node& other) const {
47 return other.bounds != bounds;
48 }
49
50 BVH_ALWAYS_INLINE bool is_leaf() const { return index.is_leaf(); }
51
53 return BBox<T, Dim>(
54 Vec<T, Dim>::generate([&] (size_t i) { return bounds[i * 2]; }),
55 Vec<T, Dim>::generate([&] (size_t i) { return bounds[i * 2 + 1]; }));
56 }
57
59 static_for<0, Dim>([&] (size_t i) {
60 bounds[i * 2 + 0] = bbox.min[i];
61 bounds[i * 2 + 1] = bbox.max[i];
62 });
63 }
64
66 return Vec<T, Dim>::generate([&] (size_t i) { return bounds[2 * static_cast<uint32_t>(i) + octant[i]]; });
67 }
68
70 return Vec<T, Dim>::generate([&] (size_t i) { return bounds[2 * static_cast<uint32_t>(i) + 1 - octant[i]]; });
71 }
72
73 /// Robust ray-node intersection routine. See "Robust BVH Ray Traversal", by T. Ize.
75 const Ray<T, Dim>& ray,
76 const Vec<T, Dim>& inv_dir,
77 const Vec<T, Dim>& inv_dir_pad,
78 const Octant& octant) const
79 {
80 auto tmin = (get_min_bounds(octant) - ray.org) * inv_dir;
81 auto tmax = (get_max_bounds(octant) - ray.org) * inv_dir_pad;
82 return make_intersection_result(ray, tmin, tmax);
83 }
84
86 const Ray<T, Dim>& ray,
87 const Vec<T, Dim>& inv_dir,
88 const Vec<T, Dim>& inv_org,
89 const Octant& octant) const
90 {
91 auto tmin = fast_mul_add(get_min_bounds(octant), inv_dir, inv_org);
92 auto tmax = fast_mul_add(get_max_bounds(octant), inv_dir, inv_org);
93 return make_intersection_result(ray, tmin, tmax);
94 }
95
97 for (auto&& bound : bounds)
98 stream.write(bound);
99 stream.write(index.value);
100 }
101
103 Node node;
104 for (auto& bound : node.bounds)
105 bound = stream.read<T>();
106 node.index = Index(stream.read<typename Index::Type>());
107 return node;
108 }
109
110private:
112 const Ray<T, Dim>& ray,
113 const Vec<T, Dim>& tmin,
114 const Vec<T, Dim>& tmax)
115 {
116 auto t0 = ray.tmin;
117 auto t1 = ray.tmax;
118 static_for<0, Dim>([&] (size_t i) {
119 t0 = robust_max(tmin[i], t0);
120 t1 = robust_min(tmax[i], t1);
121 });
122 return std::pair<T, T> { t0, t1 };
123 }
124};
125
126} // namespace bvh::v2
127
128#endif
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
Definition TDatime.h:104
Bool_t operator==(const TDatime &d1, const TDatime &d2)
Definition TDatime.h:102
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Stream of data that can be used to deserialize data structures.
Definition stream.h:10
T read(T &&default_val={})
Definition stream.h:13
Stream of data that can be used to serialize data structures.
Definition stream.h:25
bool write(const T &data)
Definition stream.h:28
double T(double x)
Definition bbox.h:9
BVH_ALWAYS_INLINE T robust_max(T a, T b)
Definition utils.h:43
BVH_ALWAYS_INLINE T fast_mul_add(T a, T b, T c)
Fast multiply-add operation.
Definition utils.h:74
BVH_ALWAYS_INLINE T robust_min(T a, T b)
Definition utils.h:41
#define BVH_ALWAYS_INLINE
Definition platform.h:19
Vec< T, N > max
Definition bbox.h:13
Vec< T, N > min
Definition bbox.h:13
Packed index data structure.
Definition index.h:33
UnsignedIntType< Bits > Type
Definition index.h:34
Binary BVH node, containing its bounds and an index into its children or the primitives it contains.
Definition node.h:23
BVH_ALWAYS_INLINE BBox< T, Dim > get_bbox() const
Definition node.h:52
static BVH_ALWAYS_INLINE Node deserialize(InputStream &stream)
Definition node.h:102
BVH_ALWAYS_INLINE std::pair< T, T > intersect_robust(const Ray< T, Dim > &ray, const Vec< T, Dim > &inv_dir, const Vec< T, Dim > &inv_dir_pad, const Octant &octant) const
Robust ray-node intersection routine. See "Robust BVH Ray Traversal", by T. Ize.
Definition node.h:74
BVH_ALWAYS_INLINE Vec< T, Dim > get_max_bounds(const Octant &octant) const
Definition node.h:69
BVH_ALWAYS_INLINE void set_bbox(const BBox< T, Dim > &bbox)
Definition node.h:58
BVH_ALWAYS_INLINE Vec< T, Dim > get_min_bounds(const Octant &octant) const
Definition node.h:65
static BVH_ALWAYS_INLINE std::pair< T, T > make_intersection_result(const Ray< T, Dim > &ray, const Vec< T, Dim > &tmin, const Vec< T, Dim > &tmax)
Definition node.h:111
Index index
Index to the children of an inner node, or to the primitives for a leaf node.
Definition node.h:37
Node()=default
std::array< T, Dim *2 > bounds
Bounds of the node, laid out in memory as [min_x, max_x, min_y, max_y, ...].
Definition node.h:34
BVH_ALWAYS_INLINE void serialize(OutputStream &stream) const
Definition node.h:96
BVH_ALWAYS_INLINE std::pair< T, T > intersect_fast(const Ray< T, Dim > &ray, const Vec< T, Dim > &inv_dir, const Vec< T, Dim > &inv_org, const Octant &octant) const
Definition node.h:85
BVH_ALWAYS_INLINE bool is_leaf() const
Definition node.h:50
Vec< T, N > org
Definition ray.h:17
static BVH_ALWAYS_INLINE Vec< T, N > generate(F &&f)
Definition vec.h:40
auto * t1
Definition textangle.C:20