Fermat
multijitter_inline.h
1 /*
2  * Copyright (c) 2010-2018, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 namespace cugar {
29 
30 // get a set of 2d stratified samples
31 template <typename T>
33  const uint32 samples_x,
34  const uint32 samples_y,
35  Vector<T,2>* samples,
36  Ordering ordering)
37 {
38  m_sample_xy.resize( samples_x * samples_y );
39  for (uint32 y = 0; y < samples_y; y++)
40  {
41  for (uint32 x = 0; x < samples_x; x++)
42  {
43  const size_t index = y * samples_x + x;
44  m_sample_xy[index].x = y;
45  m_sample_xy[index].y = x;
46  }
47  }
48  for (uint32 y = 0; y < samples_y; y++)
49  {
50  for (uint32 x = 0; x < samples_x-1; x++)
51  {
52  const float r = m_random.next();
53  const uint32 xx = std::min( uint32(float(r) * (samples_x - x)) + x, samples_x-1u );
54 
55  const size_t index1 = y * samples_x + x;
56  const size_t index2 = y * samples_x + xx;
57 
58  std::swap( m_sample_xy[index1].y, m_sample_xy[index2].y );
59  }
60  }
61  for (uint32 x = 0; x < samples_x; x++)
62  {
63  for (uint32 y = 0; y < samples_y-1; y++)
64  {
65  const float r = m_random.next();
66  const uint32 yy = std::min( uint32(float(r) * (samples_y - y)) + y, samples_y-1u );
67 
68  const size_t index1 = y * samples_x + x;
69  const size_t index2 = yy * samples_x + x;
70 
71  std::swap( m_sample_xy[index1].x, m_sample_xy[index2].x );
72  }
73  }
74 
75  const uint32 num_samples = samples_x * samples_y;
76  const T inv = T(1.0) / T(num_samples);
77  for (uint32 y = 0; y < samples_y; y++)
78  {
79  for (uint32 x = 0; x < samples_x; x++)
80  {
81  const size_t index = y * samples_x + x;
82  samples[index][0] = min( (T(m_sample_xy[index].x + x * samples_y) + m_random.next()) * inv, T(1) );
83  samples[index][1] = min( (T(m_sample_xy[index].y + y * samples_x) + m_random.next()) * inv, T(1) );
84  }
85  }
86  if (ordering == kRandom)
87  {
88  for (uint32 i = 0; i < num_samples; i++)
89  {
90  const float r = m_random.next();
91  const uint32 j = std::min( uint32(float(r) * (num_samples - i)) + i, num_samples-1u );
92  std::swap( samples[i], samples[j] );
93  }
94  }
95 }
96 // get a set of 3d stratified samples
97 // the first 2 dimensions are multi-jittered, the third one
98 // is selected with latin hypercube sampling wrt the first 2.
99 template <typename T>
101  const uint32 samples_x,
102  const uint32 samples_y,
103  Vector<T,3>* samples)
104 {
105  m_sample_xy.resize( samples_x * samples_y );
106  for (uint32 y = 0; y < samples_y; y++)
107  {
108  for (uint32 x = 0; x < samples_x; x++)
109  {
110  const size_t index = y * samples_x + x;
111  m_sample_xy[index].x = y;
112  m_sample_xy[index].y = x;
113  }
114  }
115  for (uint32 y = 0; y < samples_y; y++)
116  {
117  for (uint32 x = 0; x < samples_x-1; x++)
118  {
119  const float r = m_random.next();
120  const uint32 xx = std::min( uint32(float(r) * (samples_x - x)) + x, samples_x-1u );
121 
122  const size_t index1 = y * samples_x + x;
123  const size_t index2 = y * samples_x + xx;
124 
125  std::swap( m_sample_xy[index1].y, m_sample_xy[index2].y );
126  }
127  }
128  for (uint32 x = 0; x < samples_x; x++)
129  {
130  for (uint32 y = 0; y < samples_y-1; y++)
131  {
132  const float r = m_random.next();
133  const uint32 yy = std::min( uint32(float(r) * (samples_y - y)) + y, samples_y-1u );
134 
135  const size_t index1 = y * samples_x + x;
136  const size_t index2 = yy * samples_x + x;
137 
138  std::swap( m_sample_xy[index1].x, m_sample_xy[index2].x );
139  }
140  }
141 
142  const uint32 num_samples = samples_x * samples_y;
143  const T inv = T(1.0) / T(num_samples);
144  for (uint32 y = 0; y < samples_y; y++)
145  {
146  for (uint32 x = 0; x < samples_x; x++)
147  {
148  const size_t index = y * samples_x + x;
149  samples[index][0] = (T(m_sample_xy[index].x + x * samples_y) + m_random.next()) * inv;
150  samples[index][1] = (T(m_sample_xy[index].y + y * samples_x) + m_random.next()) * inv;
151  samples[index][2] = (T(index) + m_random.next()) * inv;
152  }
153  }
154  for (uint32 i = 0; i < num_samples; i++)
155  {
156  const float r = m_random.next();
157  const uint32 j = std::min( uint32(float(r) * (num_samples - i)) + i, num_samples-1u );
158  std::swap( samples[i], samples[j] );
159  }
160 }
161 // get a set of 4d stratified samples
162 template <typename T>
164  const uint32 samples_x,
165  const uint32 samples_y,
166  Vector<T,4>* samples,
167  Ordering ordering)
168 {
169  for (uint32 offset = 0; offset <= 2; offset += 2)
170  {
171  m_sample_xy.resize( samples_x * samples_y );
172  for (uint32 y = 0; y < samples_y; y++)
173  {
174  for (uint32 x = 0; x < samples_x; x++)
175  {
176  const size_t index = y * samples_x + x;
177  m_sample_xy[index].x = y;
178  m_sample_xy[index].y = x;
179  }
180  }
181  for (uint32 y = 0; y < samples_y; y++)
182  {
183  for (uint32 x = 0; x < samples_x-1; x++)
184  {
185  const float r = m_random.next();
186  const uint32 xx = std::min( uint32(float(r) * (samples_x - x)) + x, samples_x-1u );
187 
188  const size_t index1 = y * samples_x + x;
189  const size_t index2 = y * samples_x + xx;
190 
191  std::swap( m_sample_xy[index1].y, m_sample_xy[index2].y );
192  }
193  }
194  for (uint32 x = 0; x < samples_x; x++)
195  {
196  for (uint32 y = 0; y < samples_y-1; y++)
197  {
198  const float r = m_random.next();
199  const uint32 yy = std::min( uint32(float(r) * (samples_y - y)) + y, samples_y-1u );
200 
201  const size_t index1 = y * samples_x + x;
202  const size_t index2 = yy * samples_x + x;
203 
204  std::swap( m_sample_xy[index1].x, m_sample_xy[index2].x );
205  }
206  }
207 
208  const uint32 num_samples = samples_x * samples_y;
209  const T inv = T(1.0) / T(num_samples);
210  for (uint32 y = 0; y < samples_y; y++)
211  {
212  for (uint32 x = 0; x < samples_x; x++)
213  {
214  const size_t index = y * samples_x + x;
215  samples[index][offset+0] = (T(m_sample_xy[index].x + x * samples_y) + m_random.next()) * inv;
216  samples[index][offset+1] = (T(m_sample_xy[index].y + y * samples_x) + m_random.next()) * inv;
217  }
218  }
219  if (ordering == kRandom)
220  {
221  const uint32 num_samples = samples_x * samples_y;
222  for (uint32 i = 0; i < num_samples; i++)
223  {
224  const float r = m_random.next();
225  const uint32 j = std::min( uint32(float(r) * (num_samples - i)) + i, num_samples-1u );
226  std::swap( samples[i][offset+0], samples[j][offset+0] );
227  std::swap( samples[i][offset+1], samples[j][offset+1] );
228  }
229  }
230  }
231 }
232 
233 } // namespace cugar
Definition: vector.h:130
void sample(const uint32 samples_x, const uint32 samples_y, Vector< T, 2 > *samples, Ordering ordering=kRandom)
Definition: multijitter_inline.h:32
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
Definition: vector.h:264
Definition: vector.h:187