14template <
class TriangleCodec,
class NodeCodec>
48 size_t node_count = 1;
49 size_t to_process_max_size = 1;
50 size_t to_process_triangles_max_size = 0;
57 while (!to_estimate.
empty())
73 node_count += child_nodes.
size();
76 for (
int idx =
int(child_nodes.
size()) - 1; idx >= 0; --idx)
83 to_process_max_size = max(to_estimate.
size(), to_process_max_size);
88 to_process_triangles_max_size = max(to_estimate_triangles.
size(), to_process_triangles_max_size);
100 if (to_estimate_triangles.
empty())
103 to_estimate.
swap(to_estimate_triangles);
111 if (
size_t(total_size) != total_size)
113 outError =
"AABBTreeToBuffer: Out of memory!";
116 mTree.
reserve(
size_t(total_size));
127 size_t mNodeStart = size_t(-1);
128 size_t mTriangleStart = size_t(-1);
129 size_t mChildNodeStart[NumChildrenPerNode];
130 size_t mChildTrianglesStart[NumChildrenPerNode];
131 size_t * mParentChildNodeStart =
nullptr;
132 size_t * mParentTrianglesStart =
nullptr;
133 uint mNumChildren = 0;
137 to_process.
reserve(to_process_max_size);
139 to_process_triangles.
reserve(to_process_triangles_max_size);
152 while (!to_process.
empty())
155 NodeData *node_data = to_process.
back();
159 JPH_ASSERT(
AABox(node_data->mNodeBoundsMin, node_data->mNodeBoundsMax).
Contains(node_data->mNode->mBounds),
"AABBTreeToBuffer: Bounding box became smaller!");
164 node_data->mNumChildren = (
uint)child_nodes.
size();
169 if (i < child_nodes.
size())
171 child_bounds_min[i] = child_nodes[i]->mBounds.mMin;
172 child_bounds_max[i] = child_nodes[i]->mBounds.mMax;
181 node_data->mNodeStart = node_ctx.
NodeAllocate(node_data->mNode, node_data->mNodeBoundsMin, node_data->mNodeBoundsMax, child_nodes, child_bounds_min, child_bounds_max, mTree, outError);
182 if (node_data->mNodeStart ==
size_t(-1))
185 if (node_data->mNode->HasChildren())
188 for (
int idx =
int(child_nodes.
size()) - 1; idx >= 0; --idx)
193 JPH_ASSERT(
AABox(child_bounds_min[idx], child_bounds_max[idx]).Contains(child_node->
mBounds),
"AABBTreeToBuffer: Bounding box became smaller!");
197 child.mNode = child_node;
198 child.mNodeBoundsMin = child_bounds_min[idx];
199 child.mNodeBoundsMax = child_bounds_max[idx];
200 child.mParentChildNodeStart = &node_data->mChildNodeStart[idx];
201 child.mParentTrianglesStart = &node_data->mChildTrianglesStart[idx];
214 node_data->mTriangleStart = tri_ctx.
Pack(&inTriangles[node_data->mNode->mTrianglesBegin], node_data->mNode->mNumTriangles, inStoreUserData, mTree, outError);
215 if (node_data->mTriangleStart ==
size_t(-1))
220 if (node_data->mParentChildNodeStart !=
nullptr)
222 *node_data->mParentChildNodeStart = node_data->mNodeStart;
223 *node_data->mParentTrianglesStart = node_data->mTriangleStart;
228 if (to_process_triangles.
empty())
231 to_process.
swap(to_process_triangles);
236 JPH_ASSERT(to_process_max_size == to_process_triangles.
capacity() || to_process_triangles_max_size == to_process_triangles.
capacity());
239 for (NodeData &n : node_list)
240 if (!node_ctx.
NodeFinalize(n.mNode, n.mNodeStart, n.mNumChildren, n.mChildNodeStart, n.mChildTrianglesStart, mTree, outError))
244 tri_ctx.
Finalize(inVertices, triangle_header, mTree);
247 if (node_count != node_list.
size())
249 outError =
"Internal Error: Node memory estimate was incorrect, memory corruption!";
252 if (total_size != mTree.
size())
254 outError =
"Internal Error: Tree memory estimate was incorrect, memory corruption!";
259 return node_ctx.
Finalize(header, inRoot, node_list[0].mNodeStart, node_list[0].mTriangleStart, outError);
std::uint64_t uint64
Definition Core.h:485
unsigned int uint
Definition Core.h:481
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
A node in the tree, contains the AABox for the tree and any child nodes or triangles.
Definition AABBTreeBuilder.h:40
uint mTrianglesBegin
Triangles (if no child nodes)
Definition AABBTreeBuilder.h:81
AABox mBounds
Bounding box.
Definition AABBTreeBuilder.h:78
uint mNumTriangles
Definition AABBTreeBuilder.h:82
void GetNChildren(const Array< Node > &inNodes, uint inN, Array< const Node * > &outChildren) const
Recursively get children (breadth first) to get in total inN children (or less if there are no more)
Definition AABBTreeBuilder.cpp:76
bool HasChildren() const
Check if this node has any children.
Definition AABBTreeBuilder.h:51
Conversion algorithm that converts an AABB tree to an optimized binary buffer.
Definition AABBTreeToBuffer.h:16
static const int TriangleHeaderSize
Size in bytes of the header for the triangles.
Definition AABBTreeToBuffer.h:31
typename TriangleCodec::TriangleHeader TriangleHeader
Header for the triangles.
Definition AABBTreeToBuffer.h:28
const ByteBuffer & GetBuffer() const
Get resulting data.
Definition AABBTreeToBuffer.h:263
typename NodeCodec::Header NodeHeader
Header for the tree.
Definition AABBTreeToBuffer.h:19
static const int HeaderSize
Size in bytes of the header of the tree.
Definition AABBTreeToBuffer.h:22
static const int NumChildrenPerNode
Maximum number of children per node in the tree.
Definition AABBTreeToBuffer.h:25
bool Convert(const Array< IndexedTriangle > &inTriangles, const Array< AABBTreeBuilder::Node > &inNodes, const VertexList &inVertices, const AABBTreeBuilder::Node *inRoot, bool inStoreUserData, const char *&outError)
Convert AABB tree. Returns false if failed.
Definition AABBTreeToBuffer.h:34
const TriangleHeader * GetTriangleHeader() const
Get header for triangles.
Definition AABBTreeToBuffer.h:281
const NodeHeader * GetNodeHeader() const
Get header for tree.
Definition AABBTreeToBuffer.h:275
ByteBuffer & GetBuffer()
Get resulting data.
Definition AABBTreeToBuffer.h:269
const void * GetRoot() const
Get root of resulting tree.
Definition AABBTreeToBuffer.h:287
Axis aligned box.
Definition AABox.h:16
Vec3 mMin
Bounding box min and max.
Definition AABox.h:309
bool Contains(const AABox &inOther) const
Check if this box contains another box.
Definition AABox.h:146
Vec3 mMax
Definition AABox.h:310
void pop_back()
Remove element from the back of the array.
Definition Array.h:307
bool empty() const
Returns true if there are no elements in the array.
Definition Array.h:314
size_type capacity() const
Returns maximum amount of elements the array can hold.
Definition Array.h:326
const T & back() const
Last element in the array.
Definition Array.h:494
void swap(Array< T, Allocator > &inRHS) noexcept
Swap the contents of two arrays.
Definition Array.h:344
size_type size() const
Returns amount of elements in the array.
Definition Array.h:320
void clear()
Destruct all elements and set length to zero.
Definition Array.h:145
void push_back(const T &inValue)
Add element to the back of the array.
Definition Array.h:277
void reserve(size_type inNewSize)
Reserve array space.
Definition Array.h:113
Simple byte buffer, aligned to a cache line.
Definition ByteBuffer.h:16
Type * Allocate(size_t inSize=1)
Allocate block of data of inSize elements and return the pointer.
Definition ByteBuffer.h:33
const Type * Get(size_t inPosition) const
Get object at inPosition (an offset in bytes)
Definition ByteBuffer.h:61
This class encodes and compresses quad tree nodes.
Definition NodeCodecQuadTreeHalfFloat.h:63
bool NodeFinalize(const AABBTreeBuilder::Node *inNode, size_t inNodeStart, uint inNumChildren, const size_t *inChildrenNodeStart, const size_t *inChildrenTrianglesStart, ByteBuffer &ioBuffer, const char *&outError)
Once all nodes have been added, this call finalizes all nodes by patching in the offsets of the child...
Definition NodeCodecQuadTreeHalfFloat.h:141
size_t NodeAllocate(const AABBTreeBuilder::Node *inNode, Vec3Arg inNodeBoundsMin, Vec3Arg inNodeBoundsMax, Array< const AABBTreeBuilder::Node * > &ioChildren, Vec3 outChildBoundsMin[NumChildrenPerNode], Vec3 outChildBoundsMax[NumChildrenPerNode], ByteBuffer &ioBuffer, const char *&outError) const
Definition NodeCodecQuadTreeHalfFloat.h:81
bool Finalize(Header *outHeader, const AABBTreeBuilder::Node *inRoot, size_t inRootNodeStart, size_t inRootTrianglesStart, const char *&outError) const
Once all nodes have been finalized, this will finalize the header of the nodes.
Definition NodeCodecQuadTreeHalfFloat.h:183
void PrepareNodeAllocate(const AABBTreeBuilder::Node *inNode, uint64 &ioBufferSize) const
Mimics the size a call to NodeAllocate() would add to the buffer.
Definition NodeCodecQuadTreeHalfFloat.h:66
static constexpr int HeaderSize
Size of the header (an empty struct is always > 0 bytes so this needs a separate variable)
Definition NodeCodecQuadTreeHalfFloat.h:30
static constexpr int NumChildrenPerNode
Number of child nodes of this node.
Definition NodeCodecQuadTreeHalfFloat.h:17
This class is used to encode and compress triangle data into a byte buffer.
Definition TriangleCodecIndexed8BitPackSOA4Flags.h:135
void FinalizePreparePack(uint64 &ioBufferSize)
Mimics the size the Finalize() call would add to ioBufferSize.
Definition TriangleCodecIndexed8BitPackSOA4Flags.h:186
void Finalize(const VertexList &inVertices, TriangleHeader *ioHeader, ByteBuffer &ioBuffer) const
After all triangles have been packed, this finalizes the header and triangle buffer.
Definition TriangleCodecIndexed8BitPackSOA4Flags.h:294
size_t Pack(const IndexedTriangle *inTriangles, uint inNumTriangles, bool inStoreUserData, ByteBuffer &ioBuffer, const char *&outError)
Definition TriangleCodecIndexed8BitPackSOA4Flags.h:205
void PreparePack(const IndexedTriangle *inTriangles, uint inNumTriangles, bool inStoreUserData, uint64 &ioBufferSize)
Mimics the size a call to Pack() would add to the buffer.
Definition TriangleCodecIndexed8BitPackSOA4Flags.h:147
static constexpr int TriangleHeaderSize
Size of the header (an empty struct is always > 0 bytes so this needs a separate variable)
Definition TriangleCodecIndexed8BitPackSOA4Flags.h:35
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:103