-
Notifications
You must be signed in to change notification settings - Fork 1
/
NTree.hpp
250 lines (198 loc) · 6.23 KB
/
NTree.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#ifndef NTREE_H
#define NTREE_H
#include "General.hpp"
#include "Vec3.hpp"
#include "Box.hpp"
// A class templated to create
// DIM = 1: BinaryTrees Vec3's are (x,0,0)
// DIM = 2: QuadTrees Vec3's are (x,y,0)
// DIM = 3: OctTrees Vec3's are (x,y,z)
template <int DIM>
class NTree
{
double rootSize; // The size of the root box
Vec3 pmin; // Handle to the bottom left of the root box
int nLevels; // The number of levels: 0 = root, nLevel = leaf
int nBoxes;
// Should sparsify this if very few boxes are actually required
vector<Box*> boxes; // The boxes in this tree
public:
const static int BRANCH = 1 << DIM; // The branching factor = 2^DIM
// Constructor of an NTree with levels [0,L]
NTree( vector<Vec3>& p, int L )
: nLevels(L),
nBoxes( ((1 << (DIM*(L+1)))-1)/(BRANCH-1) ),
boxes( nBoxes )
{
int N = p.size();
// Find normalization to bounding box [0,1]^DIM
pmin = Vec3( INFINITY, INFINITY, INFINITY );
Vec3 pmax = -pmin;
for( int k = 0; k < N; ++k ) {
Vec3& t = p[k];
if( DIM <= 1 ) p[k].y = 0;
if( DIM <= 2 ) p[k].z = 0;
if( t.x < pmin.x ) pmin.x = t.x;
if( t.x > pmax.x ) pmax.x = t.x;
if( t.y < pmin.y ) pmin.y = t.y;
if( t.y > pmax.y ) pmax.y = t.y;
if( t.z < pmin.z ) pmin.z = t.z;
if( t.z > pmax.z ) pmax.z = t.z;
}
if( DIM <= 2 ) assert(pmin.z == pmax.z);
if( DIM <= 1 ) assert(pmin.y == pmax.y);
// The sidelength of the largest (encompassing) box
rootSize = (1+1e-14)*max(max(pmax.x-pmin.x,pmax.y-pmin.y),pmax.z-pmin.z);
// Remember: Using ints as bit array only allows finite L
int max_bits = 30;
int coord_bit_count = max_bits/DIM;
assert( nLevels <= coord_bit_count );
// Define and order points in the normalized box
double scale = (1 << coord_bit_count)/rootSize;
vector< pair<int,int> > mPair(N);
for( int k = 0; k < N; ++k ) {
Vec3& t = p[k];
int pNum = interleave( (int)(scale*(t.x-pmin.x)),
(int)(scale*(t.y-pmin.y)),
(int)(scale*(t.z-pmin.z)) );
mPair[k] = pair<int,int>( pNum, k );
}
sort( mPair.begin(), mPair.end() );
// Create lowest level of boxes
Box* currentBox = NULL;
int currentBoxN = -1;
// For each (ordered) point
for( int k = 0; k < N; ++k ) {
pair<int,int>& currentPoint = mPair[k];
// Determine the box number it belongs in
int boxN = parent( currentPoint.first, coord_bit_count - nLevels);
int pointN = currentPoint.second;
// If this is a new box number, create and add the box to the level
if( boxN != currentBoxN ) {
currentBox = new Box( boxN, boxCenter(boxN, nLevels) );
boxes[ getBoxIndex(boxN, nLevels) ] = currentBox;
currentBoxN = boxN;
}
// Add the point index to the box it is contained in
currentBox->addPointIndex( pointN );
}
// Create the boxes for the rest of the levels
for( int L = nLevels; L > 0; --L ) {
Box* currentParent = NULL;
int currentParentN = -1;
// For each box
for( int n = 0; n < (1 << (DIM*L)); ++n ) {
Box* b = boxes[ getBoxIndex(n,L) ];
if( b == NULL ) continue;
// Determine the parent box number it belongs in
int parentN = parent( b->n );
// If this is a new box number, create and add the box to the level
if( parentN != currentParentN ) {
currentParent = new Box( parentN, boxCenter( parentN, L-1 ) );
boxes[ getBoxIndex(parentN, L-1) ] = currentParent;
currentParentN = parentN;
}
// Set the parent pointer
b->parent = currentParent;
}
}
}
// Destructor
~NTree() {
// Delete Boxes
for( int n = 0; n < nBoxes; ++n ) {
delete boxes[n];
}
}
inline int numLevels()
{
return nLevels;
}
inline int getBoxIndex( int n, int L )
{
assert( L <= nLevels );
assert( n < (1 << (DIM*L)) );
//cerr << n << ", " << L << ": " << ((1 << (DIM*L))-1)/(BRANCH-1) + n << " -- " << nBoxes << endl;
return ((1 << (DIM*L))-1)/(BRANCH-1) + n;
}
inline Box* getBox( int n, int L )
{
return boxes[ getBoxIndex(n,L) ];
}
// Note that L = 0 is the root
inline double getBoxSize( int L )
{
return rootSize / (1 << L);
}
inline Vec3 boxCenter( int n, int L )
{
Vec3 p = deinterleave(n);
if( DIM >= 1 ) p.x += 0.5;
if( DIM >= 2 ) p.y += 0.5;
if( DIM >= 3 ) p.z += 0.5;
p *= getBoxSize(L);
p += pmin;
return p;
}
// The kth-order parent of box n
inline int parent( int n, int k = 1 )
{
return n >> (DIM*k);
}
// The mth kth-order child of box n
inline int child( int n, int m = 0, int k = 1 )
{
assert( m < (1 << (DIM*k)) );
return m + (n << (DIM*k));
}
inline void addPoint( const Vec3& p ) {}
inline void removePoint( int index ) {}
friend ostream& operator<<(ostream& os, NTree& t)
{
int nLevels = t.numLevels();
for( int n = 0; n < (1 << (DIM*nLevels)); ++n ) {
Box* b = t.getBox(n,nLevels);
if( b == NULL ) continue;
for( int l = nLevels; l >= 0; --l ) {
os << t.parent(b->n,l) << "\t";
}
os << b->center << endl;
}
return os;
}
private:
inline int interleave( int x, int y, int z )
{
int n = 0;
for( int i = -1; x != 0 || y != 0 || z != 0; ) {
if( DIM >= 3 ) { n |= (z & 1) << ++i; z >>= 1; }
if( DIM >= 2 ) { n |= (y & 1) << ++i; y >>= 1; }
if( DIM >= 1 ) { n |= (x & 1) << ++i; x >>= 1; }
}
return n;
}
inline Vec3 deinterleave( int n )
{
int x = 0, y = 0, z = 0;
for( int i = 0; n != 0; ++i ) {
if( DIM >= 3 ) { z |= (n & 1) << i; n >>= 1; }
if( DIM >= 2 ) { y |= (n & 1) << i; n >>= 1; }
if( DIM >= 1 ) { x |= (n & 1) << i; n >>= 1; }
}
return Vec3(x,y,z);
}
inline vector<int> siblings( int n )
{
vector<int> siblings(BRANCH);
siblings[0] = child(parent(n));
for( int k = 1; k < BRANCH; ++k ) {
siblings[k] = siblings[k-1] + 1;
}
return siblings;
}
private:
// Disable Copy and Assignment
NTree( const NTree& S ) {}
void operator=( const NTree& S ) {}
};
#endif