AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
Morton.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_MORTON_H
18#define AGX_MORTON_H
19
20#include <agx/Math.h>
21#include <agx/Vec3.h>
22
23// Utility functions for calculating Z-order (Morton order) indices
24// http://en.wikipedia.org/wiki/Z-order_curve
25
26// ULL suffix necessary for 32bit build
27#define AGX_LONG_CONSTANT(x) x ## ULL
28
29namespace agx
30{
35
37 // 32 bit
39
40 // From chapter 7.3.5 of Christer Ericson's book "Real time collision detection"
41 // Is approximately 3x faster than naive loop implementation.
43 {
44 n = n & 0x3FF; // Only keep low 10 bits
45 n = (n | (n << 16)) & 0xFF0000FF;
46 n = (n | (n << 8)) & 0x0300F00F;
47 n = (n | (n << 4)) & 0x030C30C3;
48 n = (n | (n << 2)) & 0x09249249;
49 return n;
50 }
51
52
54 {
55 UInt32 result = (morton_part1By2(z) << 2) + (morton_part1By2(y) << 1) + morton_part1By2(x);
56
57 #ifdef AGX_DEBUG
58 agxAssert(result == morton_reference(x, y, z));
59 #endif
60
61 return result;
62 }
63
64 // Reference implementation
66 {
67 UInt32 result = 0;
68
69 for(size_t i = 0; i < 10; i++)
70 {
71 UInt32 mask = 1 << i;
72 result |= (x & mask) << (i * 2) | (y & mask) << (i * 2 + 1) | (z & mask) << (i * 2 + 2);
73 }
74
75 return result;
76 }
77
78
79 // 2d
81 {
82 n = n & 0xFFFF; // Only keep low 16 bits
83 n = (n | (n << 8)) & 0x00FF00FF;
84 n = (n | (n << 4)) & 0x0F0F0F0F;
85 n = (n | (n << 2)) & 0x33333333;
86 n = (n | (n << 1)) & 0x55555555;
87 return n;
88 }
89
91 {
92 UInt32 result = (morton_part1By1(y) << 1) + morton_part1By1(x);
93
94 #ifdef AGX_DEBUG
95 agxAssert(result == morton_reference(x, y));
96 #endif
97
98 return result;
99 }
100
101 // Reference implementation
103 {
104 UInt32 result = 0;
105
106 for(size_t i = 0; i < 16; i++)
107 {
108 UInt32 mask = 1 << i;
109 result |= (x & mask) << i | (y & mask) << (i + 1);
110 }
111
112 return result;
113 }
114
115
116 // Convenience methods
117 AGX_FORCE_INLINE UInt32 morton(const Vec3u32& vec) { return morton((UInt32)vec[0], (UInt32)vec[1], (UInt32)vec[2]); }
119 {
120 agxAssert(vec[0] >= 0 && vec[1] >= 0 && vec[2] >= 0);
121 return morton((UInt32)vec[0], (UInt32)vec[1], (UInt32)vec[2]);
122 }
123
124 AGX_FORCE_INLINE UInt32 morton(const Vec2u32& vec) { return morton((UInt32)vec[0], (UInt32)vec[1]); }
126 {
127 agxAssert(vec[0] >= 0 && vec[1] >= 0);
128 return morton((UInt32)vec[0], (UInt32)vec[1]);
129 }
130
131
133 // 64 bit
135
136
137 // Based on chapter 7.3.5 of Christer Ericson's book "Real time collision detection"
138 // At least 5x faster than naive loop implementation
140 {
141 n = n & 0x1FFFFF; // Only keep low 21 bits
142 n = (n | (n << 32)) & AGX_LONG_CONSTANT(0x001F00000000FFFF);
143 n = (n | (n << 16)) & AGX_LONG_CONSTANT(0x001F0000FF0000FF);
144 n = (n | (n << 8)) & AGX_LONG_CONSTANT(0x100F00F00F00F00F);
145 n = (n | (n << 4)) & AGX_LONG_CONSTANT(0x10C30C30C30C30C3);
146 n = (n | (n << 2)) & AGX_LONG_CONSTANT(0x1249249249249249);
147 return n;
148 }
149
151 {
152 UInt64 result = (morton_part1By2(z) << 2) + (morton_part1By2(y) << 1) + morton_part1By2(x);
153
154 #ifdef AGX_DEBUG
155 agxAssert(result == morton_reference(x, y, z));
156 #endif
157
158 return result;
159 }
160
161 // Reference implementation
163 {
164 UInt64 result = 0;
165
166 for(UInt64 i = 0; i < 21; i++)
167 {
168 UInt64 mask = static_cast<UInt64>(1) << i;
169 result |= (x & mask) << (i * 2) | (y & mask) << (i * 2 + 1) | (z & mask) << (i * 2 + 2);
170 }
171
172 return result;
173 }
174
175 // 2d
177 {
178 n = n & 0xFFFFFFFF; // Only keep low 32 bits
179 n = (n | (n << 16)) & AGX_LONG_CONSTANT(0x0000FFFF0000FFFF);
180 n = (n | (n << 8)) & AGX_LONG_CONSTANT(0x00FF00FF00FF00FF);
181 n = (n | (n << 4)) & AGX_LONG_CONSTANT(0x0F0F0F0F0F0F0F0F);
182 n = (n | (n << 2)) & AGX_LONG_CONSTANT(0x3333333333333333);
183 n = (n | (n << 1)) & AGX_LONG_CONSTANT(0x5555555555555555);
184 return n;
185 }
186
188 {
189 UInt64 result = (morton_part1By1(y) << 1) + morton_part1By1(x);
190
191 #ifdef AGX_DEBUG
192 agxAssert(result == morton_reference(x, y));
193 #endif
194
195 return result;
196 }
197
198 // Reference implementation
200 {
201 UInt64 result = 0;
202
203 for(UInt64 i = 0; i < 32; i++)
204 {
205 UInt64 mask = static_cast<UInt64>(1) << i;
206 result |= (x & mask) << i | (y & mask) << (i + 1);
207 }
208
209 return result;
210 }
211
212
213 // Convenience methods
214 AGX_FORCE_INLINE UInt64 morton(const Vec3u64& vec) { return morton((UInt64)vec[0], (UInt64)vec[1], (UInt64)vec[2]); }
216 {
217 agxAssert(vec[0] >= 0 && vec[1] >= 0 && vec[2] >= 0);
218 return morton((UInt64)vec[0], (UInt64)vec[1], (UInt64)vec[2]);
219 }
220
221 AGX_FORCE_INLINE UInt64 morton(const Vec2u64& vec) { return morton((UInt64)vec[0], (UInt64)vec[1]); }
223 {
224 agxAssert(vec[0] >= 0 && vec[1] >= 0);
225 return morton((UInt64)vec[0], (UInt64)vec[1]);
226 }
227
228}
229
230#undef AGX_LONG_CONSTANT
231
232#endif /* _AGX_MORTON_H_ */
#define AGX_LONG_CONSTANT(x)
Definition Morton.h:27
A class holding 3 dimensional vectors and providing basic arithmetic.
#define agxAssert(expr)
Definition debug.h:143
#define AGX_FORCE_INLINE
Definition macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
UInt32 morton_part1By2(UInt32 n)
Definition Morton.h:42
uint32_t UInt32
Definition Integer.h:32
uint64_t UInt64
Definition Integer.h:33
UInt32 morton_part1By1(UInt32 n)
Definition Morton.h:80
UInt32 morton_reference(UInt32 x, UInt32 y, UInt32 z)
Definition Morton.h:65
UInt32 morton(UInt32 x, UInt32 y, UInt32 z)
Definition Morton.h:53