Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TWideBvhIntersector.ipp
Go to the documentation of this file.
1#pragma once
2
4#include "Core/HitProbe.h"
5#include "Core/Ray.h"
7#include "Math/math.h"
8
9#include <Common/assertion.h>
10#include <Common/logging.h>
11#include <Common/math_basics.h>
12
13#include <optional>
14
15namespace ph
16{
17
18template<std::size_t N, typename Index>
19inline void TWideBvhIntersector<N, Index>
20::update(TSpanView<const Intersectable*> intersectables)
21{
22 rebuildWithIntersectables(intersectables);
23}
24
25template<std::size_t N, typename Index>
27::isIntersecting(const Ray& ray, HitProbe& probe) const
28{
29 return m_bvh.nearestTraversal(
30 ray.getSegment(),
31 [ray, &probe, originalProbe = probe](
32 const Intersectable* const intersectable,
33 const math::TLineSegment<real>& segment)
34 -> std::optional<real>
35 {
36 PH_ASSERT(intersectable);
37
38 const Ray raySegment(segment, ray.getTime());
39
40 HitProbe trialProbe = originalProbe;
41 if(intersectable->isIntersecting(raySegment, trialProbe))
42 {
43 probe = trialProbe;
44 return trialProbe.getHitRayT();
45 }
46 else
47 {
48 return std::nullopt;
49 }
50 });
51}
52
53template<std::size_t N, typename Index>
54inline auto TWideBvhIntersector<N, Index>
55::calcAABB() const
56-> math::AABB3D
57{
58 return m_rootAABB;
59}
60
61template<std::size_t N, typename Index>
64{
65 constexpr auto itemToAABB =
66 [](const Intersectable* item)
67 {
68 return item->calcAABB();
69 };
70
71 math::BvhParams params;
72 //params.splitMethod = math::EBvhNodeSplitMethod::EqualItems;
73 params.splitMethod = math::EBvhNodeSplitMethod::SAH_Buckets_OneAxis;
74 params.numSahBuckets = 32;
75 //params.numSahBuckets = 16;
76
77 //math::TBvhBuilder<N, const Intersectable*, decltype(itemToAABB)> builder{params};
78 math::TBvhBuilder<2, const Intersectable*, decltype(itemToAABB)> builder{params};
79
80 auto const rootInfoNode = builder.buildInformativeBvh(intersectables);
81 m_bvh.build(rootInfoNode, builder.totalInfoNodes(), builder.totalItems());
82
83 // Wide BVH uses fat nodes, we need to calculate root AABB by ourselves
84 m_rootAABB = math::AABB3D::makeEmpty();
85 if(!m_bvh.isEmpty())
86 {
87 for(std::size_t ci = 0; ci < N; ++ci)
88 {
89 m_rootAABB.unionWith(m_bvh.getRoot().getAABB(ci));
90 }
91 }
92
93 /*if constexpr(math::is_power_of_2(N))
94 {
95 PH_DEFAULT_LOG(Note, "{}", m_bvh.balancedPow2OrderTableToString());
96 }*/
97
98 // Check the constructed BVH and print some information
99#if PH_DEBUG
100 const std::size_t treeDepth = builder.calcMaxDepth(rootInfoNode);
101
102 PH_DEFAULT_LOG(Note,
103 "intersector: BVH{} ({}-byte index), total intersectables: {}, total nodes: {}, "
104 "max tree depth: {}, memory usage: {} GiB", N, sizeof(Index), m_bvh.numItems(),
105 m_bvh.numNodes(), treeDepth, math::bytes_to_GiB<double>(m_bvh.memoryUsage()));
106
107 if(treeDepth > m_bvh.TRAVERSAL_STACK_SIZE)
108 {
109 PH_DEFAULT_LOG(Error,
110 "BVH{} depth ({}) exceeds traversal stack size ({})",
111 N, treeDepth, m_bvh.TRAVERSAL_STACK_SIZE);
112 }
113#endif
114}
115
116}// end namespace ph
Lightweight ray intersection testing and reporting object. If an intersection is found,...
Definition HitProbe.h:27
An object in the scene that a ray can intersect with.
Definition Intersectable.h:31
Represents a ray in space.
Definition Ray.h:21
const math::TLineSegment< real > & getSegment() const
Definition Ray.h:229
Wide BVH acceleration structure supporting arbitrary branch factor.
Definition TWideBvhIntersector.h:20
math::AABB3D calcAABB() const override
Calculates Axis-Aligned Bounding Box (AABB) of itself.
Definition TWideBvhIntersector.ipp:55
Definition BvhParams.h:20
uint32 numSahBuckets
Definition BvhParams.h:26
EBvhNodeSplitMethod splitMethod
Definition BvhParams.h:35
Definition TBvhBuilder.h:25
Represents a line segment in space.
Definition TLineSegment.h:25
Miscellaneous math utilities.
The root for all renderer implementations.
Definition EEngineProject.h:6
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