Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
index.h
Go to the documentation of this file.
1#ifndef BVH_V2_INDEX_H
2#define BVH_V2_INDEX_H
3
4#include "bvh/v2/utils.h"
5
6#include <cassert>
7#include <cstddef>
8
9namespace bvh::v2 {
10
11/// Packed index data structure. This index can either refer to a range of primitives for a BVH
12/// leaf, or to the children of a BVH node. In either case, the index corresponds to a contiguous
13/// range, which means that:
14///
15/// - For leaves, primitives in a BVH node should be accessed via:
16///
17/// size_t begin = index.first_id();
18/// size_t end = begin + index.prim_count();
19/// for (size_t i = begin; i < end; ++i) {
20/// size_t prim_id = bvh.prim_ids[i];
21/// // ...
22/// }
23///
24/// Note that for efficiency, reordering the original data to avoid the indirection via
25/// `bvh.prim_ids` is preferable.
26///
27/// - For inner nodes, children should be accessed via:
28///
29/// auto& left_child = bvh.nodes[index.first_id()];
30/// auto& right_child = bvh.nodes[index.first_id() + 1];
31///
32template <size_t Bits, size_t PrimCountBits>
33struct Index {
35
36 static constexpr size_t bits = Bits;
37 static constexpr size_t prim_count_bits = PrimCountBits;
38 static constexpr Type max_prim_count = make_bitmask<Type>(prim_count_bits);
39 static constexpr Type max_first_id = make_bitmask<Type>(bits - prim_count_bits);
40
41 static_assert(PrimCountBits < Bits);
42
44
45 Index() = default;
46 explicit Index(Type value) : value(value) {}
47
48 //bool operator == (const Index&) const = default;
49 //bool operator != (const Index&) const = default;
50 bool operator == (const Index& other) const {
51 return other.value == value;
52 }
53 bool operator != (const Index& other) const {
54 return other.value != value;
55 }
56
59 BVH_ALWAYS_INLINE bool is_leaf() const { return prim_count() != 0; }
60 BVH_ALWAYS_INLINE bool is_inner() const { return !is_leaf(); }
61
63 *this = Index { first_id, prim_count() };
64 }
65
67 *this = Index { first_id(), prim_count };
68 }
69
70 static BVH_ALWAYS_INLINE Index make_leaf(size_t first_prim, size_t prim_count) {
71 assert(prim_count != 0);
72 return Index { first_prim, prim_count };
73 }
74
75 static BVH_ALWAYS_INLINE Index make_inner(size_t first_child) {
76 return Index { first_child, 0 };
77 }
78
79private:
80 explicit Index(size_t first_id, size_t prim_count)
81 : Index(
82 (static_cast<Type>(first_id) << prim_count_bits) |
83 (static_cast<Type>(prim_count) & max_prim_count))
84 {
85 assert(first_id <= static_cast<size_t>(max_first_id));
86 assert(prim_count <= static_cast<size_t>(max_prim_count));
87 }
88};
89
90} // namespace bvh::v2
91
92#endif
Definition bbox.h:9
typename UnsignedInt< Bits >::Type UnsignedIntType
Definition utils.h:25
#define BVH_ALWAYS_INLINE
Definition platform.h:19
Packed index data structure.
Definition index.h:33
BVH_ALWAYS_INLINE bool is_inner() const
Definition index.h:60
BVH_ALWAYS_INLINE Type first_id() const
Definition index.h:57
static constexpr size_t bits
Definition index.h:36
static BVH_ALWAYS_INLINE Index make_inner(size_t first_child)
Definition index.h:75
UnsignedIntType< Bits > Type
Definition index.h:34
bool operator==(const Index &other) const
Definition index.h:50
BVH_ALWAYS_INLINE void set_prim_count(size_t prim_count)
Definition index.h:66
Index(Type value)
Definition index.h:46
Index(size_t first_id, size_t prim_count)
Definition index.h:80
static constexpr Type max_prim_count
Definition index.h:38
bool operator!=(const Index &other) const
Definition index.h:53
BVH_ALWAYS_INLINE void set_first_id(size_t first_id)
Definition index.h:62
static constexpr size_t prim_count_bits
Definition index.h:37
static BVH_ALWAYS_INLINE Index make_leaf(size_t first_prim, size_t prim_count)
Definition index.h:70
Index()=default
BVH_ALWAYS_INLINE Type prim_count() const
Definition index.h:58
static constexpr Type max_first_id
Definition index.h:39
Type value
Definition index.h:43
BVH_ALWAYS_INLINE bool is_leaf() const
Definition index.h:59