Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
SampleFlow.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <Common/assertion.h>
7#include <Common/primitive_type.h>
8
9#include <cstddef>
10#include <array>
11#include <optional>
12
13namespace ph
14{
15
18class SampleFlow final
19{
20public:
23 SampleFlow();
24
27 SampleFlow(const real* savedDims, std::size_t numSavedDims);
28
32 real flow1D();
33
37 std::array<real, 2> flow2D();
38
42 std::array<real, 3> flow3D();
43
48 template<std::size_t N>
49 std::array<real, N> flowND();
50
57 bool pick(real pickProbability);
58
67 bool unflowedPick(real pickProbability);
68
75 bool unflowedRandomPick(real pickProbability);
76
77private:
78 const real* m_savedDims;
79 std::size_t m_numSavedDims;
80 std::size_t m_numReadDims;
81 std::optional<real> m_partiallyUsedDim;
82
83 bool hasMoreToRead() const;
84 real load1D();
85};
86
87// In-header Implementations:
88
90 m_savedDims (nullptr),
91 m_numSavedDims (0),
92 m_numReadDims (0),
93 m_partiallyUsedDim()
94{}
95
96inline SampleFlow::SampleFlow(const real* const savedDims, const std::size_t numSavedDims) :
97 m_savedDims (savedDims),
98 m_numSavedDims (numSavedDims),
99 m_numReadDims (0),
100 m_partiallyUsedDim()
101{
102 PH_ASSERT(savedDims);
103}
104
106{
107 return flowND<1>()[0];
108}
109
110inline std::array<real, 2> SampleFlow::flow2D()
111{
112 return flowND<2>();
113}
114
115inline std::array<real, 3> SampleFlow::flow3D()
116{
117 return flowND<3>();
118}
119
120template<std::size_t N>
121inline std::array<real, N> SampleFlow::flowND()
122{
123 std::array<real, N> sample;
124 for(std::size_t i = 0; i < N; ++i)
125 {
126 sample[i] = load1D();
127 }
128 return sample;
129}
130
131inline bool SampleFlow::pick(const real pickProbability)
132{
133 return math::pick(pickProbability, load1D());
134}
135
136inline bool SampleFlow::unflowedPick(const real pickProbability)
137{
138 real dimValue = load1D();
139 const bool isPicked = math::reused_pick(pickProbability, dimValue);
140
141 PH_ASSERT(!m_partiallyUsedDim.has_value());
142 m_partiallyUsedDim = dimValue;
143
144 return isPicked;
145}
146
147inline bool SampleFlow::unflowedRandomPick(const real pickProbability)
148{
149 return math::pick(pickProbability, math::Random::sample());
150}
151
152inline bool SampleFlow::hasMoreToRead() const
153{
154 return m_numReadDims < m_numSavedDims;
155}
156
157inline real SampleFlow::load1D()
158{
159 if(m_partiallyUsedDim.has_value())
160 {
161 const real dimValue = m_partiallyUsedDim.value();
162 m_partiallyUsedDim.reset();
163 return dimValue;
164 }
165
166 return m_savedDims && hasMoreToRead()
167 ? m_savedDims[m_numReadDims++]
169}
170
171}// end namespace ph
Basic sampling routines.
A sample with arbitrary dimensions with fine-grained sampling control.
Definition SampleFlow.h:19
bool pick(real pickProbability)
Consumes the next dimension and use it to perform a random pick. Effectively using flow1D() to perfor...
Definition SampleFlow.h:131
std::array< real, N > flowND()
Makes a N-D sample by consuming the next N dimension.
Definition SampleFlow.h:121
bool unflowedRandomPick(real pickProbability)
Performs a random pick independent to this flow. No dimension is being consumed. This method is inten...
Definition SampleFlow.h:147
std::array< real, 3 > flow3D()
Makes a 3-D sample by consuming the next three dimensions.
Definition SampleFlow.h:115
real flow1D()
Makes a 1-D sample by consuming the next dimension.
Definition SampleFlow.h:105
SampleFlow()
Creates a flow of totally random values.
Definition SampleFlow.h:89
std::array< real, 2 > flow2D()
Makes a 2-D sample by consuming the next two dimensions.
Definition SampleFlow.h:110
bool unflowedPick(real pickProbability)
Uses the next dimension to perform a random pick without consuming it. This method does not cause the...
Definition SampleFlow.h:136
static real sample()
Get a uniform random value in [0, 1].
Definition Random.cpp:44
bool pick(const T pickProbability, const T sample)
Randomly pick a branch with some probability.
Definition sample.h:41
bool reused_pick(const T pickProbability, T &sample)
Definition sample.h:50
The root for all renderer implementations.
Definition EEngineProject.h:6