Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
SamplesNDStream.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 <vector>
12
13namespace ph
14{
15
16class SamplesNDStream final
17{
18public:
20 const real* buffer,
21 std::size_t numDims,
22 std::size_t numSamples);
23
27 const real* readSample();
28
32 template<std::size_t N>
33 std::array<real, N> readSample();
34
39
40 std::size_t numDims() const;
41 std::size_t numSamples() const;
42
43 template<std::size_t N>
44 std::array<real, N> get(std::size_t index) const;
45
49 const real* operator [] (std::size_t index) const;
50
51private:
52 const real* m_buffer;
53 std::size_t m_numDims;
54 std::size_t m_numSamples;
55 std::size_t m_readIndex;
56 std::vector<real> m_overreadBuffer;
57};
58
59// In-header Implementations:
60
62 const real* const buffer,
63 const std::size_t numDims,
64 const std::size_t numSamples) :
65
66 m_buffer (buffer),
67 m_numDims (numDims),
68 m_numSamples (numSamples),
69 m_readIndex (0),
70 m_overreadBuffer(numDims)// TODO: possibly make it record all samples for further usage (let SamplesND provide buffer?)
71{
72 PH_ASSERT(buffer);
73 PH_ASSERT_GT(numDims, 0);
74 PH_ASSERT_GT(numSamples, 0);
75}
76
77inline const real* SamplesNDStream::readSample()
78{
79 PH_ASSERT_EQ(m_overreadBuffer.size(), m_numDims);
80
81 if(m_readIndex < m_numSamples)
82 {
83 return (*this)[m_readIndex++];
84 }
85 else
86 {
87 for(auto& element : m_overreadBuffer)
88 {
89 element = math::Random::sample();
90 }
91 return m_overreadBuffer.data();
92 }
93}
94
95template<std::size_t N>
96inline std::array<real, N> SamplesNDStream::readSample()
97{
98 PH_ASSERT_EQ(N, m_numDims);
99
100 const real* const samplePtr = readSample();
101
102 std::array<real, N> sample;
103 for(std::size_t di = 0; di < N; ++di)
104 {
105 sample[di] = samplePtr[di];
106 }
107 return sample;
108}
109
111{
112 return SampleFlow(readSample(), m_numDims);
113}
114
115inline std::size_t SamplesNDStream::numDims() const
116{
117 return m_numDims;
118}
119
120inline std::size_t SamplesNDStream::numSamples() const
121{
122 return m_numSamples;
123}
124
125template<std::size_t N>
126inline std::array<real, N> SamplesNDStream::get(const std::size_t index) const
127{
128 PH_ASSERT_EQ(N, m_numDims);
129
130 std::array<real, N> sample;
131 for(std::size_t di = 0; di < N; ++di)
132 {
133 sample[di] = (*this)[index][di];
134 }
135 return sample;
136}
137
138inline const real* SamplesNDStream::operator [] (const std::size_t index) const
139{
140 PH_ASSERT_LT(index, numSamples());
141
142 return &(m_buffer[index * m_numDims]);
143}
144
145}// end namespace ph
A sample with arbitrary dimensions with fine-grained sampling control.
Definition SampleFlow.h:19
Definition SamplesNDStream.h:17
const real * operator[](std::size_t index) const
Get the i-th N-dimensional sample.
Definition SamplesNDStream.h:138
std::size_t numSamples() const
Definition SamplesNDStream.h:120
const real * readSample()
Read the next N-dimensional sample.
Definition SamplesNDStream.h:77
std::array< real, N > get(std::size_t index) const
Definition SamplesNDStream.h:126
SampleFlow readSampleAsFlow()
Read the next N-dimensional sample as flow, which has more flexible controls for sampling.
Definition SamplesNDStream.h:110
std::size_t numDims() const
Definition SamplesNDStream.h:115
SamplesNDStream(const real *buffer, std::size_t numDims, std::size_t numSamples)
Definition SamplesNDStream.h:61
static real sample()
Get a uniform random value in [0, 1].
Definition Random.cpp:44
The root for all renderer implementations.
Definition EEngineProject.h:6