Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
SamplesND.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <Common/assertion.h>
8
9#include <cstddef>
10#include <array>
11
12namespace ph
13{
14
15class SamplesND final
16{
17public:
18 SamplesND();
19
21 real* buffer,
22 std::size_t numDims,
23 std::size_t numSamples);
24
26 real* buffer,
27 std::size_t numDims,
28 std::size_t numSamples,
29 std::size_t strideSize,
30 std::size_t offsetInStride);
31
32 void shuffle();
33 void shuffleDimension(std::size_t dimIndex);
34 void shuffleDimensions(std::size_t dimIndexBegin, std::size_t dimIndexEnd);
36
37 void setSample(std::size_t index, const real* sample);
38
39 template<std::size_t N>
40 void setSample(std::size_t index, const std::array<real, N>& sample);
41
42 real* buffer();
43
44 std::size_t numDims() const;
45 std::size_t numSamples() const;
46 std::size_t numElements() const;
47 bool isValid() const;
48
49 real* operator [] (std::size_t index);
50
51private:
52 real* m_buffer;
53 std::size_t m_numDims;
54 std::size_t m_numSamples;
55 std::size_t m_strideSize;
56 std::size_t m_offsetInStride;
57};
58
59// In-header Implementations:
60
62 m_buffer (nullptr),
63 m_numDims (0),
64 m_numSamples (0),
65 m_strideSize (0),
66 m_offsetInStride(0)
67{}
68
70 real* const buffer,
71 const std::size_t numDims,
72 const std::size_t numSamples) :
73
75 buffer,
76 numDims,
77 numSamples,
78 numDims,
79 0)
80{}
81
83 real* const buffer,
84 const std::size_t numDims,
85 const std::size_t numSamples,
86 const std::size_t strideSize,
87 const std::size_t offsetInStride) :
88
89 m_buffer (buffer),
90 m_numDims (numDims),
91 m_numSamples (numSamples),
92 m_strideSize (strideSize),
93 m_offsetInStride(offsetInStride)
94{
95 PH_ASSERT(buffer);
96 PH_ASSERT_LT(offsetInStride, strideSize);
97 PH_ASSERT_IN_RANGE_INCLUSIVE(numDims, 1, strideSize - offsetInStride);
98 PH_ASSERT_GT(numSamples, 0);
99}
100
102{
104 0, m_numSamples,
105 [this](const std::size_t indexA, const std::size_t indexB)
106 {
107 for(std::size_t di = 0; di < m_numDims; ++di)
108 {
109 std::swap((*this)[indexA][di],
110 (*this)[indexB][di]);
111 }
112 });
113}
114
115inline void SamplesND::shuffleDimension(const std::size_t dimIndex)
116{
117 PH_ASSERT_MSG(dimIndex < m_numDims,
118 "dimension index must be smaller than number of dimensions");
119
121 0, m_numSamples,
122 [this, dimIndex](const std::size_t indexA, const std::size_t indexB)
123 {
124 std::swap((*this)[indexA][dimIndex],
125 (*this)[indexB][dimIndex]);
126 });
127}
128
129inline void SamplesND::shuffleDimensions(const std::size_t dimIndexBegin, const std::size_t dimIndexEnd)
130{
131 PH_ASSERT_MSG(dimIndexBegin < dimIndexEnd,
132 "invalid dimension index range");
133
134 for(std::size_t dimIndex = dimIndexBegin; dimIndex < dimIndexEnd; ++dimIndex)
135 {
136 shuffleDimension(dimIndex);
137 }
138}
139
141{
142 shuffleDimensions(0, m_numDims);
143}
144
145inline void SamplesND::setSample(const std::size_t index, const real* const sample)
146{
147 PH_ASSERT(sample);
148
149 for(std::size_t di = 0; di < m_numDims; ++di)
150 {
151 (*this)[index][di] = sample[di];
152 }
153}
154
155template<std::size_t N>
156inline void SamplesND::setSample(const std::size_t index, const std::array<real, N>& sample)
157{
158 PH_ASSERT_EQ(N, m_numDims);
159
160 setSample(index, sample.data());
161}
162
163inline real* SamplesND::buffer()
164{
165 return m_buffer;
166}
167
168inline std::size_t SamplesND::numDims() const
169{
170 return m_numDims;
171}
172
173inline std::size_t SamplesND::numSamples() const
174{
175 return m_numSamples;
176}
177
178inline std::size_t SamplesND::numElements() const
179{
180 return m_numSamples * m_numDims;
181}
182
183inline bool SamplesND::isValid() const
184{
185 return m_buffer != nullptr;
186}
187
188inline real* SamplesND::operator [] (const std::size_t index)
189{
190 PH_ASSERT_LT(index, numSamples());
191
192 return &(m_buffer[index * m_strideSize + m_offsetInStride]);
193}
194
195}// end namespace ph
Definition SamplesND.h:16
void setSample(std::size_t index, const real *sample)
Definition SamplesND.h:145
bool isValid() const
Definition SamplesND.h:183
std::size_t numDims() const
Definition SamplesND.h:168
real * operator[](std::size_t index)
Definition SamplesND.h:188
std::size_t numElements() const
Definition SamplesND.h:178
void shuffleDimensions(std::size_t dimIndexBegin, std::size_t dimIndexEnd)
Definition SamplesND.h:129
real * buffer()
Definition SamplesND.h:163
void shuffleDimension(std::size_t dimIndex)
Definition SamplesND.h:115
void shuffleAllDimensions()
Definition SamplesND.h:140
std::size_t numSamples() const
Definition SamplesND.h:173
void shuffle()
Definition SamplesND.h:101
SamplesND()
Definition SamplesND.h:61
void shuffle_durstenfeld_index_pairs(const std::size_t beginIndex, const std::size_t endIndex, IndexPairConsumer consumer)
Definition shuffle.h:33
The root for all renderer implementations.
Definition EEngineProject.h:6