Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TBvhBuilder.h
Go to the documentation of this file.
1#pragma once
2
5#include "Math/constant.h"
7#include "Math/TVector3.h"
8#include "Utility/TSpan.h"
9
10#include <cstddef>
11#include <vector>
12#include <type_traits>
13#include <concepts>
14#include <utility>
15#include <array>
16
17namespace ph::math
18{
19
20template<std::size_t N, typename Item>
21class TBvhInfoNode;
22
23template<std::size_t N, typename Item, typename ItemToAABB>
24class TBvhBuilder final
25{
26 static_assert(N > 1);
27 static_assert(std::is_invocable_r_v<AABB3D, ItemToAABB, Item>);
28
29 inline static constexpr std::size_t MAX_SAH_BUCKETS = 64;
30
31public:
34
35 static std::size_t calcTotalNodes(const InfoNodeType* node);
36 static std::size_t calcTotalItems(const InfoNodeType* node);
37 static std::size_t calcMaxDepth(const InfoNodeType* node);
38
39public:
40 explicit TBvhBuilder(
41 BvhParams params = BvhParams{})
42 requires std::default_initializable<ItemToAABB>
43 : TBvhBuilder(ItemToAABB{}, params)
44 {}
45
46 explicit TBvhBuilder(
47 ItemToAABB itemToAABB,
48 BvhParams params = BvhParams{});
49
62 -> const InfoNodeType*;
63
64 void clearBuildData();
65
69 auto totalInfoNodes() const
70 -> std::size_t;
71
75 auto totalItems() const
76 -> std::size_t;
77
78private:
79 struct SahBucket
80 {
81 public:
83 std::size_t numItems = 0;
84
85 bool isEmpty() const
86 {
87 return numItems == 0;
88 }
89 };
90
94 template<EBvhNodeSplitMethod SPLIT_METHOD>
95 auto buildBvhInfoNodeRecursive(
96 TSpan<ItemInfoType> itemInfos)
97 -> const InfoNodeType*;
98
99 bool binarySplitWithEqualItems(
100 TSpan<ItemInfoType> itemInfos,
101 std::size_t splitDimension,
102 TSpan<ItemInfoType>* out_negativePart,
103 TSpan<ItemInfoType>* out_positivePart);
104
105 bool binarySplitWithSahBuckets(
106 TSpan<ItemInfoType> itemInfos,
107 std::size_t splitDimension,
108 const AABB3D& itemsAABB,
109 const AABB3D& itemsCentroidAABB,
110 TSpan<ItemInfoType>* out_negativePart,
111 TSpan<ItemInfoType>* out_positivePart);
112
113 bool splitWithEqualItems(
114 TSpan<ItemInfoType> itemInfos,
115 std::size_t splitDimension,
116 std::array<TSpan<ItemInfoType>, N>* out_parts);
117
118 bool splitWithSahBuckets(
119 TSpan<ItemInfoType> itemInfos,
120 std::size_t splitDimension,
121 const AABB3D& itemsAABB,
122 const AABB3D& itemsCentroidAABB,
123 std::array<TSpan<ItemInfoType>, N>* out_parts);
124
125 void splitWithSahBucketsBacktracking(
126 std::size_t splitDimension,
127 const AABB3D& itemsAABB,
128 TSpanView<SahBucket> buckets,
129 std::size_t numSplits,
130 std::size_t splitBegin,
131 const std::array<std::size_t, N>& splitEnds,
132 real cost,
133 real* out_bestCost,
134 std::array<std::size_t, N>* out_bestSplitEnds) const;
135
136 std::vector<ItemInfoType> m_infoBuffer;
137 std::vector<InfoNodeType> m_infoNodes;
138 BvhParams m_params;
139 ItemToAABB m_itemToAABB;
140};
141
142}// end namespace ph::math
143
Definition BvhParams.h:20
static TAABB3D makeEmpty()
Definition TAABB3D.ipp:15
Definition TBvhBuilder.h:25
auto buildInformativeBvh(TSpanView< Item > items) -> const InfoNodeType *
Build a BVH that contains additional information useful for many purposes. The built BVH has the foll...
Definition TBvhBuilder.ipp:36
void clearBuildData()
Definition TBvhBuilder.ipp:90
auto totalInfoNodes() const -> std::size_t
Definition TBvhBuilder.ipp:98
auto totalItems() const -> std::size_t
Definition TBvhBuilder.ipp:106
TBvhBuilder(BvhParams params=BvhParams{})
Definition TBvhBuilder.h:40
static std::size_t calcTotalItems(const InfoNodeType *node)
Definition TBvhBuilder.ipp:314
TBvhInfoNode< N, Item > InfoNodeType
Definition TBvhBuilder.h:32
static std::size_t calcTotalNodes(const InfoNodeType *node)
Definition TBvhBuilder.ipp:297
static std::size_t calcMaxDepth(const InfoNodeType *node)
Definition TBvhBuilder.ipp:331
General BVH node packed with additional information. This node type is typically used for building ot...
Definition TLinearDepthFirstWideBvh.h:21
Definition TBvhItemInfo.h:11
Math functions and utilities.
Definition TransformInfo.h:10
TAABB3D< real > AABB3D
Definition TAABB3D.h:21
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
std::span< T, EXTENT > TSpan
A contiguous sequence of objects of type T. Effectively the same as std::span.
Definition TSpan.h:12
Definition TAABB2D.h:96