Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TBinaryBvhIntersector.ipp
Go to the documentation of this file.
1#pragma once
2
4#include "Core/HitProbe.h"
5#include "Core/Ray.h"
8#include "Math/math.h"
9
10#include <Common/assertion.h>
11#include <Common/logging.h>
12
13#include <optional>
14
15namespace ph
16{
17
18template<typename Index>
19inline void TBinaryBvhIntersector<Index>
20::update(TSpanView<const Intersectable*> intersectables)
21{
22 rebuildWithIntersectables(intersectables);
23}
24
25template<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<typename Index>
54inline auto TBinaryBvhIntersector<Index>
55::calcAABB() const
56-> math::AABB3D
57{
58 if(m_bvh.isEmpty())
59 {
60 return math::AABB3D::makeEmpty();
61 }
62
63 return m_bvh.getRoot().getAABB();
64}
65
66template<typename Index>
69{
70 constexpr auto itemToAABB =
71 [](const Intersectable* item)
72 {
73 return item->calcAABB();
74 };
75
76 math::TBvhBuilder<2, const Intersectable*, decltype(itemToAABB)> builder{};
77
78 auto const rootInfoNode = builder.buildInformativeBvh(intersectables);
79 m_bvh.build(rootInfoNode, builder.totalInfoNodes(), builder.totalItems());
80
81 // Check the constructed linear BVH and print some information
82#if PH_DEBUG
83 const std::size_t treeDepth = builder.calcMaxDepth(rootInfoNode);
84
85 PH_DEFAULT_LOG(Note,
86 "intersector: Binary BVH ({}-byte index), total intersectables: {}, total nodes: {}, "
87 "max tree depth: {}, memory usage: {} GiB", sizeof(Index), m_bvh.numItems(),
88 m_bvh.numNodes(), treeDepth, math::bytes_to_GiB<double>(m_bvh.memoryUsage()));
89
90 if(treeDepth > m_bvh.TRAVERSAL_STACK_SIZE)
91 {
92 PH_DEFAULT_LOG(Error,
93 "BVH depth ({}) exceeds traversal stack size ({})",
94 treeDepth, m_bvh.TRAVERSAL_STACK_SIZE);
95 }
96#endif
97}
98
99}// 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
Classic binary BVH acceleration structure.
Definition TBinaryBvhIntersector.h:16
math::AABB3D calcAABB() const override
Calculates Axis-Aligned Bounding Box (AABB) of itself.
Definition TBinaryBvhIntersector.ipp:55
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