1 
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9 // Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 //
29 
30 #pragma once
31 
32 // appleseed.foundation headers.
33 #include "foundation/core/concepts/noncopyable.h"
34 #include "foundation/math/vector.h"
35 #include "foundation/platform/compiler.h"
36 
37 // Standard headers.
38 #include <cstddef>
39 #include <vector>
40 
41 namespace renderer
42 {
43 
44 //
45 // A strictly deterministic pixel sampler.
46 //
47 // Reference:
48 //
49 //   Strictly Deterministic Sampling Methods in Computer Graphics
50 //   http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.88.7937
51 //
52 
53 class PixelSampler
54   : public foundation::NonCopyable
55 {
56   public:
57     // Initialize the pixel sampler for a given subpixel grid size.
58     void initialize(const size_t subpixel_grid_size);
59 
60     // Compute the position of a pixel sample and optionally the initial instance
61     // number of the corresponding sampling context, given the integer coordinates
62     // of the subpixel grid cell containing the sample. The coordinates of the
63     // pixel sample are expressed in continous image space
64     // (https://github.com/appleseedhq/appleseed/wiki/Terminology).
65     void sample(
66         const int               sx,
67         const int               sy,
68         foundation::Vector2d&   sample_position) const;
69     void sample(
70         const int               sx,
71         const int               sy,
72         foundation::Vector2d&   sample_position,
73         size_t&                 initial_instance) const;
74 
75   private:
76     size_t                      m_subpixel_grid_size;
77     double                      m_rcp_subpixel_grid_size;   // 1.0 / subpixel_grid_size
78     double                      m_rcp_period;               // 1.0 / m_period
79     size_t                      m_log_period;               // log2(m_period)
80     size_t                      m_period;                   // m_sigma.size()
81     size_t                      m_period_mask;              // m_period - 1
82     std::vector<size_t>         m_sigma;                    // 2^N * radical_inverse_base2(0..N-1)
83 };
84 
85 
86 //
87 // PixelSampler class implementation.
88 //
89 
sample(const int sx,const int sy,foundation::Vector2d & sample_position)90 APPLESEED_FORCE_INLINE void PixelSampler::sample(
91     const int                   sx,
92     const int                   sy,
93     foundation::Vector2d&       sample_position) const
94 {
95     // Compute the sample coordinates in image space.
96     if (m_subpixel_grid_size == 1)
97     {
98         sample_position[0] = sx + 0.5;
99         sample_position[1] = sy + 0.5;
100     }
101     else
102     {
103         const size_t j = sx & m_period_mask;
104         const size_t k = sy & m_period_mask;
105         const size_t sigma_j = m_sigma[j];
106         const size_t sigma_k = m_sigma[k];
107 
108         sample_position[0] = (sx + sigma_k * m_rcp_period) * m_rcp_subpixel_grid_size;
109         sample_position[1] = (sy + sigma_j * m_rcp_period) * m_rcp_subpixel_grid_size;
110     }
111 }
112 
sample(const int sx,const int sy,foundation::Vector2d & sample_position,size_t & initial_instance)113 APPLESEED_FORCE_INLINE void PixelSampler::sample(
114     const int                   sx,
115     const int                   sy,
116     foundation::Vector2d&       sample_position,
117     size_t&                     initial_instance) const
118 {
119     const size_t j = sx & m_period_mask;
120     const size_t k = sy & m_period_mask;
121     const size_t sigma_k = m_sigma[k];
122 
123     // Compute the initial instance number of the sampling context.
124     initial_instance = (j << m_log_period) + sigma_k;
125 
126     // Compute the sample coordinates in image space.
127     if (m_subpixel_grid_size == 1)
128     {
129         sample_position[0] = sx + 0.5;
130         sample_position[1] = sy + 0.5;
131     }
132     else
133     {
134         const size_t sigma_j = m_sigma[j];
135 
136         sample_position[0] = (sx + sigma_k * m_rcp_period) * m_rcp_subpixel_grid_size;
137         sample_position[1] = (sy + sigma_j * m_rcp_period) * m_rcp_subpixel_grid_size;
138     }
139 }
140 
141 }   // namespace renderer
142