SPH
Morton.cpp
Go to the documentation of this file.
1 #include "math/Morton.h"
2 #include "objects/geometry/Box.h"
3 
5 
6 // https://devblogs.nvidia.com/parallelforall/thinking-parallel-part-iii-tree-construction-gpu/
7 inline Size expandBits(Size v) {
8  v = (v * 0x00010001u) & 0xFF0000FFu;
9  v = (v * 0x00000101u) & 0x0F00F00Fu;
10  v = (v * 0x00000011u) & 0xC30C30C3u;
11  v = (v * 0x00000005u) & 0x49249249u;
12  return v;
13 }
14 
15 Size morton(const Vector& v) {
16  const Vector u = v * 1024._f;
17  const int x = int(u[X]);
18  const int y = int(u[Y]);
19  const int z = int(u[Z]);
20  SPH_ASSERT(x >= 0 && x <= 1023);
21  SPH_ASSERT(y >= 0 && y <= 1023);
22  SPH_ASSERT(z >= 0 && z <= 1023);
23  const int xx = expandBits(x);
24  const int yy = expandBits(y);
25  const int zz = expandBits(z);
26  return xx * 4 + yy * 2 + zz;
27 }
28 
29 Size morton(const Vector& v, const Box& box) {
30  return morton((v - box.lower()) / box.size());
31 }
32 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Object representing a three-dimensional axis-aligned box.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
NAMESPACE_SPH_BEGIN Size expandBits(Size v)
Definition: Morton.cpp:7
Size morton(const Vector &v)
Calculates a 30-bit Morton code for the given vector located within the unit cube [0,...
Definition: Morton.cpp:15
#define NAMESPACE_SPH_END
Definition: Object.h:12
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
INLINE const Vector & lower() const
Returns lower bounds of the box.
Definition: Box.h:82
INLINE Vector size() const
Returns box dimensions.
Definition: Box.h:106