Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TIndexedKdtree.h
Go to the documentation of this file.
1#pragma once
2
6#include "Core/Ray.h"
7#include "Core/HitProbe.h"
10#include "Utility/utility.h"
11
12#include <Common/assertion.h>
13
14#include <cstddef>
15#include <vector>
16#include <utility>
17#include <memory>
18#include <type_traits>
19
20namespace ph::math
21{
22
23template<
24 typename IndexToItem,
25 typename ItemToAABB,
26 typename Index = std::size_t>
27class TIndexedKdtree final
28{
29public:
30 static_assert(std::is_invocable_v<IndexToItem, Index>);
31 using Item = decltype(std::declval<IndexToItem>()(std::declval<Index>()));
32
33private:
36
37 static_assert(std::is_invocable_r_v<AABB3D, ItemToAABB, Item>);
38
39public:
41 std::size_t numItems,
42 IndexToItem indexToItem,
43 ItemToAABB itemToAABB,
45
46 template<typename TesterFunc>
47 bool nearestTraversal(const TLineSegment<real>& segment, TesterFunc&& intersectionTester) const;
48
49 AABB3D getAABB() const;
50 bool isEmpty() const;
51 Item getItem(std::size_t idx) const;
52
53private:
54 void build(ItemToAABB itemToAABB, IndexedKdtreeParams params);
55
56 void buildNodeRecursive(
57 std::size_t nodeIndex,
58 const AABB3D& nodeAABB,
59 const Index* nodeItemIndices,
60 std::size_t numNodeItems,
61 std::size_t currentNodeDepth,
62 std::size_t currentBadRefines,
63 const std::vector<AABB3D>& itemAABBs,
64 std::size_t maxNodeDepth,
66 Index* negativeItemIndicesCache,
67 Index* positiveItemIndicesCache,
68 std::array<std::unique_ptr<ItemEndpoint[]>, 3>& endpointsCache);
69
70private:
71 std::size_t m_numItems;
72 IndexToItem m_indexToItem;
73 AABB3D m_rootAABB;
74 std::vector<Node> m_nodeBuffer;// TODO: compact this after build or use array
75 std::size_t m_numNodes;// TODO: remove this
76 std::vector<Index> m_itemIndices;// TODO: compact this after build or use array
77};
78
79}// end namespace ph::math
80
Definition IndexedKdtreeParams.h:9
Definition TIndexedKdtree.h:28
Item getItem(std::size_t idx) const
Definition TIndexedKdtree.ipp:229
bool isEmpty() const
Definition TIndexedKdtree.ipp:218
AABB3D getAABB() const
Definition TIndexedKdtree.ipp:205
bool nearestTraversal(const TLineSegment< real > &segment, TesterFunc &&intersectionTester) const
TIndexedKdtree(std::size_t numItems, IndexToItem indexToItem, ItemToAABB itemToAABB, IndexedKdtreeParams params=IndexedKdtreeParams())
Definition TIndexedKdtree.ipp:20
decltype(std::declval< IndexToItem >()(std::declval< Index >())) Item
Definition TIndexedKdtree.h:31
An indexed kD-tree node with compacted memory layout.
Definition TIndexedKdtreeNode.h:23
Represents a line segment in space.
Definition TLineSegment.h:25
Math functions and utilities.
Definition TransformInfo.h:10
Definition TIndexedItemEndpoint.h:16