Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
HitProbe.h
Go to the documentation of this file.
1#pragma once
2
4#include "Math/TVector3.h"
5
6#include <Common/assertion.h>
7#include <Common/config.h>
8#include <Common/primitive_type.h>
9
10#include <limits>
11#include <array>
12#include <type_traits>
13#include <cstddef>
14#include <cstring>
15
16namespace ph
17{
18
19class Intersectable;
20class HitDetail;
21class Ray;
22
26class HitProbe final
27{
28public:
33 HitProbe();
34
38 void calcHitDetail(const Ray& ray, HitDetail* out_detail) const;
39
43 void calcFullHitDetail(const Ray& ray, HitDetail* out_detail) const;
44
52 bool reintersect(const Ray& ray, HitProbe& probe, const Ray& srcRay) const;
53
54 bool isOnDefaultChannel() const;
55
58 void pushIntermediateHit(const Intersectable* hitTarget);
59
64 void pushBaseHit(const Intersectable* hitTarget, real hitRayT);
65
68 void popHit();
69
70 void replaceTopHit(const Intersectable* newTopHit);
71 void replaceBaseHitRayT(real newHitRayT);
72
73 void setChannel(uint8 channel);
74 uint8 getChannel() const;
75
79 const Intersectable* getTopHit() const;
80
81 real getHitRayT() const;
82
83 template<typename T>
84 void pushCache(const T& data);
85
86 template<typename T>
87 T popCache();
88
89private:
91
92 Stack m_hitStack;
93 real m_hitRayT;
94 std::byte m_cache[PH_HIT_PROBE_CACHE_BYTES];
95 uint8 m_cacheHead;
96 uint8 m_hitDetailChannel;
97#if PH_DEBUG
98 bool m_hasBaseHitSet{false};
99#endif
100};
101
102// In-header Implementations:
103
105 : m_hitStack ()
106 , m_hitRayT (std::numeric_limits<real>::max())
107 , m_cache ()
108 , m_cacheHead (0)
109 , m_hitDetailChannel(0)
110{}
111
112inline void HitProbe::pushIntermediateHit(const Intersectable* const hitTarget)
113{
114 m_hitStack.push(hitTarget);
115}
116
117inline void HitProbe::pushBaseHit(const Intersectable* const hitTarget, const real hitRayT)
118{
119 m_hitStack.push(hitTarget);
120 m_hitRayT = hitRayT;
121
122#if PH_DEBUG
123 m_hasBaseHitSet = true;
124#endif
125}
126
127inline void HitProbe::popHit()
128{
129 m_hitStack.pop();
130}
131
132inline void HitProbe::replaceTopHit(const Intersectable* const newTopHit)
133{
134 m_hitStack.pop();
135 m_hitStack.push(newTopHit);
136}
137
138inline void HitProbe::replaceBaseHitRayT(const real newHitRayT)
139{
140 PH_ASSERT(m_hasBaseHitSet);
141 m_hitRayT = newHitRayT;
142}
143
144inline void HitProbe::setChannel(const uint8 channel)
145{
146 m_hitDetailChannel = channel;
147}
148
149inline uint8 HitProbe::getChannel() const
150{
151 return m_hitDetailChannel;
152}
153
155{
156 return m_hitStack.top();
157}
158
159inline real HitProbe::getHitRayT() const
160{
161 PH_ASSERT(m_hasBaseHitSet);
162 return m_hitRayT;
163}
164
165template<typename T>
166inline void HitProbe::pushCache(const T& data)
167{
168 static_assert(std::is_trivially_copyable_v<T>,
169 "target type is not cacheable");
170 static_assert(sizeof(T) <= sizeof(m_cache),
171 "not enough cache to store target type, consider increasing config.PH_HIT_PROBE_CACHE_BYTES");
172
173 PH_ASSERT_MSG(m_cacheHead + sizeof(T) <= sizeof(m_cache),
174 "ran out of cache, consider increasing config.PH_HIT_PROBE_CACHE_BYTES \n"
175 "m_cacheHead = " + std::to_string(m_cacheHead) + "\n" +
176 "sizeof(T) = " + std::to_string(sizeof(T)) + "\n" +
177 "sizeof(m_cache) = " + std::to_string(sizeof(m_cache)));
178
179 std::memcpy(m_cache + m_cacheHead, &data, sizeof(T));
180 m_cacheHead += sizeof(T);
181}
182
183template<typename T>
185{
186 static_assert(std::is_trivially_copyable_v<T>);
187 static_assert(sizeof(T) <= sizeof(m_cache));
188
189 PH_ASSERT_IN_RANGE_INCLUSIVE(m_cacheHead, sizeof(T), sizeof(m_cache));
190 m_cacheHead -= sizeof(T);
191
192 T data;
193 std::memcpy(&data, m_cache + m_cacheHead, sizeof(T));
194 return data;
195}
196
197}// end namespace ph
Detailed information regarding a ray-primitive intersection.
Definition HitDetail.h:26
Lightweight ray intersection testing and reporting object. If an intersection is found,...
Definition HitProbe.h:27
bool reintersect(const Ray &ray, HitProbe &probe, const Ray &srcRay) const
Intersect the intersected object again with a different ray. The operation is done using a copy of th...
Definition HitProbe.cpp:34
const Intersectable * getTopHit() const
Definition HitProbe.h:154
void replaceTopHit(const Intersectable *newTopHit)
Definition HitProbe.h:132
uint8 getChannel() const
Definition HitProbe.h:149
void pushCache(const T &data)
Definition HitProbe.h:166
void popHit()
Removes the most recent hit target from the stack.
Definition HitProbe.h:127
void calcFullHitDetail(const Ray &ray, HitDetail *out_detail) const
Calculates full hit information using this probe. The information is calculated using a copy of the c...
Definition HitProbe.cpp:26
HitProbe()
Definition HitProbe.h:104
T popCache()
Definition HitProbe.h:184
void setChannel(uint8 channel)
Definition HitProbe.h:144
void pushIntermediateHit(const Intersectable *hitTarget)
Adds a hit target that will participate in hit detail's calculation to the stack.
Definition HitProbe.h:112
void replaceBaseHitRayT(real newHitRayT)
Definition HitProbe.h:138
void calcHitDetail(const Ray &ray, HitDetail *out_detail) const
Calculates basic hit information using this probe. The information is calculated using a copy of the ...
Definition HitProbe.cpp:16
void pushBaseHit(const Intersectable *hitTarget, real hitRayT)
Adds the first hit target to the stack. Similar to pushIntermediateHit(), except the parametric hit d...
Definition HitProbe.h:117
bool isOnDefaultChannel() const
Definition HitProbe.cpp:43
real getHitRayT() const
Definition HitProbe.h:159
An object in the scene that a ray can intersect with.
Definition Intersectable.h:31
Represents a ray in space.
Definition Ray.h:21
void push(U &&item)
Adds an item to the stack. The item originally at the target index will be overwritten.
Definition TArrayStack.ipp:19
T & top()
Definition TArrayStack.ipp:35
void pop()
Removes the top item from the stack. The item originally at the target index is still alive after thi...
Definition TArrayStack.ipp:27
The root for all renderer implementations.
Definition EEngineProject.h:6
Definition TAABB2D.h:96