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 // Interface header.
31 #include "ordering.h"
32
33 // appleseed.foundation headers.
34 #include "foundation/math/scalar.h"
35 #include "foundation/math/vector.h"
36
37 // Standard headers.
38 #include <algorithm>
39
40 using namespace foundation;
41 using namespace std;
42
43 namespace foundation
44 {
45
46 //
47 // 2D ordering generators implementation.
48 //
49
linear_ordering(vector<size_t> & ordering,const size_t size)50 void linear_ordering(
51 vector<size_t>& ordering,
52 const size_t size)
53 {
54 assert(ordering.empty());
55 assert(size > 0);
56
57 ordering.resize(size);
58
59 identity_permutation(size, &ordering[0]);
60 }
61
spiral_ordering(vector<size_t> & ordering,const size_t size_x,const size_t size_y)62 void spiral_ordering(
63 vector<size_t>& ordering,
64 const size_t size_x,
65 const size_t size_y)
66 {
67 assert(ordering.empty());
68
69 ordering.reserve(size_x * size_y);
70
71 const int size = static_cast<int>(size_x * size_y);
72 const int tw = static_cast<int>(size_x);
73 const int th = static_cast<int>(size_y);
74 const int center = (min(tw, th) - 1) / 2;
75
76 for (int i = 0; i < size; ++i)
77 {
78 int tx = tw;
79 int ty = th;
80
81 while (i < (tx * ty))
82 {
83 tx--;
84 ty--;
85 }
86
87 const int mintxty = min(tx, ty);
88 const int txty = tx * ty;
89 int x = center;
90 int y = center;
91
92 if (mintxty % 2)
93 {
94 if (i <= (txty + ty))
95 {
96 // Down-right side.
97 x += tx - mintxty / 2;
98 y += -mintxty / 2 + i - txty;
99 }
100 else
101 {
102 // Back across bottom.
103 x += tx - mintxty / 2 - (i - (txty + ty));
104 y += ty - mintxty / 2;
105 }
106 }
107 else
108 {
109 if (i <= (txty + ty))
110 {
111 // Up-left side.
112 x += -mintxty / 2;
113 y += ty - mintxty / 2 - (i - txty);
114 }
115 else
116 {
117 // Across top.
118 x += -mintxty / 2 + (i - (txty + ty));
119 y += -mintxty / 2;
120 }
121 }
122
123 assert(x >= 0);
124 assert(y >= 0);
125
126 ordering.push_back(static_cast<size_t>(y * tw + x));
127 }
128 }
129
130 namespace
131 {
hilbert(vector<size_t> & ordering,const int size_x,const int size_y,Vector2i point,int size,const Vector2i & dx,const Vector2i & dy)132 void hilbert(
133 vector<size_t>& ordering,
134 const int size_x,
135 const int size_y,
136 Vector2i point,
137 int size,
138 const Vector2i& dx,
139 const Vector2i& dy)
140 {
141 if (size > 1)
142 {
143 // Recursively visit all four sub quads.
144 size >>= 1;
145 hilbert(ordering, size_x, size_y, point, size, dy, dx);
146 point += dy * size;
147 hilbert(ordering, size_x, size_y, point, size, dx, dy);
148 point += dx * size;
149 hilbert(ordering, size_x, size_y, point, size, dx, dy);
150 point += dx * (size - 1) - dy;
151 hilbert(ordering, size_x, size_y, point, size, -dy, -dx);
152 }
153 else
154 {
155 // Insert this tile into the tile array if it's inside the boundaries of the frame.
156 assert(size == 1);
157 assert(point.x >= 0);
158 assert(point.y >= 0);
159 if (point.x < size_x && point.y < size_y)
160 ordering.push_back(static_cast<size_t>(point.y * size_x + point.x));
161 }
162 }
163 }
164
hilbert_ordering(vector<size_t> & ordering,const size_t size_x,const size_t size_y)165 void hilbert_ordering(
166 vector<size_t>& ordering,
167 const size_t size_x,
168 const size_t size_y)
169 {
170 assert(ordering.empty());
171
172 ordering.reserve(size_x * size_y);
173
174 // This Hilbert curve generator only works on a square grid whose size is a power of two.
175 const size_t root_size = next_pow2(max(size_x, size_y));
176
177 hilbert(
178 ordering,
179 static_cast<int>(size_x),
180 static_cast<int>(size_y),
181 Vector2i(0, 0),
182 static_cast<int>(root_size),
183 Vector2i(1, 0),
184 Vector2i(0, 1));
185 }
186
187 } // namespace foundation
188