6#include <Common/assertion.h>
7#include <Common/primitive_type.h>
8#include <Common/utility.h>
21template<
typename Index,
bool USE_SINGLE_ITEM_OPT = true>
25 static_assert(std::is_unsigned_v<Index>);
27 inline static constexpr std::size_t NUM_FLAG_BITS = 2;
28 inline static constexpr std::size_t NUM_NUMBER_BITS = sizeof_in_bits<Index>() - NUM_FLAG_BITS;
29 inline static constexpr std::size_t MAX_NUMBER = (std::size_t(1) << NUM_NUMBER_BITS) - 1;
31 inline static constexpr Index FLAG_BITS_MASK = 0b11;
32 inline static constexpr Index X_AXIS_FLAG = 0b00;
33 inline static constexpr Index Y_AXIS_FLAG = 0b01;
34 inline static constexpr Index Z_AXIS_FLAG = 0b10;
35 inline static constexpr Index LEAF_FLAG = 0b11;
51 std::size_t splitAxisIndex,
52 std::size_t positiveChildIndex);
55 Index indexBufferOffset,
60 std::vector<Index>& indexBuffer);
98 Index m_numberAndFlags;
103template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
107 const std::size_t splitAxisIndex,
108 const std::size_t positiveChildIndex)
115 PH_ASSERT(!std::isnan(splitPos) && !std::isinf(splitPos));
116 PH_ASSERT_IN_RANGE_INCLUSIVE(splitAxisIndex, 0, 2);
117 PH_ASSERT_LE(positiveChildIndex, MAX_NODE_INDEX);
121 node.m_numberAndFlags =
static_cast<Index
>((positiveChildIndex << NUM_FLAG_BITS) | splitAxisIndex);
126template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
129 const Index indexBufferOffset,
130 const std::size_t numItems)
133 PH_ASSERT_LE(numItems, MAX_NODE_ITEMS);
136 node.m_numberAndFlags =
static_cast<Index
>((numItems << NUM_FLAG_BITS) | LEAF_FLAG);
142template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
146 std::vector<Index>& indexBuffer)
149 PH_ASSERT(itemIndices.data());
152 if(!(USE_SINGLE_ITEM_OPT && itemIndices.size() == 1))
156 PH_ASSERT_LE(indexBuffer.size(), MAX_BUFFER_OFFSET);
157 const Index indexBufferOffset =
static_cast<Index
>(indexBuffer.size());
159 indexBuffer.insert(indexBuffer.end(), itemIndices.begin(), itemIndices.end());
161 return makeLeaf(indexBufferOffset, itemIndices.size());
167 PH_ASSERT_LE(itemIndices[0], MAX_BUFFER_OFFSET);
169 constexpr Index oneItemAndLeafFlag =
static_cast<Index
>((1 << NUM_FLAG_BITS) | LEAF_FLAG);
172 node.m_numberAndFlags = oneItemAndLeafFlag;
179template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
184 return (m_numberAndFlags & FLAG_BITS_MASK) == LEAF_FLAG;
187template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
192 PH_ASSERT(!isLeaf());
194 return static_cast<std::size_t
>(m_numberAndFlags >> NUM_FLAG_BITS);
197template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
204 return static_cast<std::size_t
>(m_numberAndFlags >> NUM_FLAG_BITS);
207template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
212 PH_ASSERT(!isLeaf());
217template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
222 PH_ASSERT(!isLeaf());
224 return static_cast<std::size_t
>(m_numberAndFlags & FLAG_BITS_MASK);
227template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
233 if constexpr(USE_SINGLE_ITEM_OPT)
235 PH_ASSERT(USE_SINGLE_ITEM_OPT && numItems() == 1);
237 return u0_itemBufferOffset;
241 PH_ASSERT_UNREACHABLE_SECTION();
242 return static_cast<std::size_t
>(-1);
246template<
typename Index,
bool USE_SINGLE_ITEM_OPT>
252 PH_ASSERT(!(USE_SINGLE_ITEM_OPT && numItems() == 1));
254 return u0_indexBufferOffset;
An indexed kD-tree node with compacted memory layout.
Definition TIndexedKdtreeNode.h:23
static constexpr std::size_t MAX_NODE_ITEMS
Definition TIndexedKdtreeNode.h:39
static constexpr std::size_t MAX_BUFFER_OFFSET
Definition TIndexedKdtreeNode.h:47
bool isLeaf() const
Definition TIndexedKdtreeNode.h:181
std::size_t getSplitAxis() const
Definition TIndexedKdtreeNode.h:219
std::size_t getSingleItemDirectIndex() const
Definition TIndexedKdtreeNode.h:229
static TIndexedKdtreeNode makeInner(real splitPos, std::size_t splitAxisIndex, std::size_t positiveChildIndex)
Definition TIndexedKdtreeNode.h:105
std::size_t numItems() const
Definition TIndexedKdtreeNode.h:199
Index u0_itemBufferOffset
Definition TIndexedKdtreeNode.h:83
static TIndexedKdtreeNode makeLeaf(Index indexBufferOffset, std::size_t numItems)
Definition TIndexedKdtreeNode.h:128
Index u0_indexBufferOffset
Definition TIndexedKdtreeNode.h:82
std::size_t getIndexBufferOffset() const
Definition TIndexedKdtreeNode.h:248
std::size_t getPositiveChildIndex() const
Definition TIndexedKdtreeNode.h:189
real u0_splitPos
Definition TIndexedKdtreeNode.h:81
real getSplitPos() const
Definition TIndexedKdtreeNode.h:209
static constexpr std::size_t MAX_NODE_INDEX
Definition TIndexedKdtreeNode.h:42
constexpr std::size_t Y_AXIS
Definition constant.h:91
constexpr std::size_t Z_AXIS
Definition constant.h:92
constexpr std::size_t X_AXIS
Definition constant.h:90
Math functions and utilities.
Definition TransformInfo.h:10
std::span< const T, EXTENT > TSpanView
Same as TSpan, except that the objects are const-qualified. Note that for pointer types,...
Definition TSpan.h:19