-
Notifications
You must be signed in to change notification settings - Fork 39
/
LidarProcessOctree.cpp
371 lines (314 loc) · 9.92 KB
/
LidarProcessOctree.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/***********************************************************************
LidarProcessOctree - Class to process multiresolution LiDAR point sets.
Copyright (c) 2008-2013 Oliver Kreylos
This file is part of the LiDAR processing and analysis package.
The LiDAR processing and analysis package is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
The LiDAR processing and analysis package is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with the LiDAR processing and analysis package; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include <string>
#include <iostream>
#include <Misc/Utility.h>
#include <Misc/ThrowStdErr.h>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <Math/Math.h>
#include "LidarProcessOctree.h"
/*****************************************
Methods of class LidarProcessOctree::Node:
*****************************************/
LidarProcessOctree::Node::Node(void)
:
#if ALLOW_THREADING
numProcessedChildren(0),
#endif
parent(0),children(0),points(0),
processCounter(0),
lruPred(0),lruSucc(0)
{
}
LidarProcessOctree::Node::~Node(void)
{
/* Delete point array: */
delete[] points;
/* Delete children: */
delete[] children;
}
/***********************************
Methods of class LidarProcessOctree:
***********************************/
void LidarProcessOctree::subdivide(LidarProcessOctree::Node& node)
{
{
#if ALLOW_THREADING
Threads::Mutex::Lock lruLock(lruMutex);
#endif
++numSubdivideCalls;
}
#if ALLOW_THREADING
Threads::Mutex::Lock nodeLock(node.mutex);
#endif
/* Bail out if the node's children have magically appeared since the last check: */
if(node.children!=0)
return;
/* Check if the node cache is full: */
{
#if ALLOW_THREADING
Threads::Mutex::Lock lruLock(lruMutex);
#endif
if(numCachedNodes+8>cacheSize)
{
/* Find an unused leaf node parent and remove its children: */
Node* coarsenNode;
#if ALLOW_THREADING
for(coarsenNode=lruHead;coarsenNode!=0;coarsenNode=coarsenNode->lruSucc)
{
/* Check if the leaf parent is currently unused: */
coarsenNode->mutex.lock();
if(coarsenNode->processCounter==0)
break;
coarsenNode->mutex.unlock();
}
#else
for(coarsenNode=lruHead;coarsenNode!=0&&coarsenNode->processCounter!=0;coarsenNode=coarsenNode->lruSucc)
;
#endif
/* Remove the found node's children: */
delete[] coarsenNode->children;
coarsenNode->children=0;
#if ALLOW_THREADING
coarsenNode->mutex.unlock();
#endif
/* Remove the found node from the lru list: */
if(coarsenNode->lruPred!=0)
coarsenNode->lruPred->lruSucc=coarsenNode->lruSucc;
else
lruHead=coarsenNode->lruSucc;
if(coarsenNode->lruSucc!=0)
coarsenNode->lruSucc->lruPred=coarsenNode->lruPred;
else
lruTail=coarsenNode->lruPred;
coarsenNode->lruPred=0;
coarsenNode->lruSucc=0;
/* Update the node cache: */
numCachedNodes-=8;
/* Check if the found node's parent is now a leaf parent node: */
Node* parent=coarsenNode->parent;
if(parent!=0)
{
/* Check if all children of the parent are leaves: */
bool leafParent=true;
for(int childIndex=0;childIndex<8&&leafParent;++childIndex)
leafParent=parent->children[childIndex].children==0;
if(leafParent)
{
/* Add the parent to the end of the leaf parent node list: */
parent->lruPred=lruTail;
parent->lruSucc=0;
if(lruTail!=0)
lruTail->lruSucc=parent;
else
lruHead=parent;
lruTail=parent;
}
}
}
/* Check if the node's parent was a leaf parent: */
Node* parent=node.parent;
if(parent!=0&&(parent->lruPred!=0||parent->lruSucc!=0||lruHead==parent))
{
/* Remove the node's parent from the leaf parent node list: */
if(parent->lruPred!=0)
parent->lruPred->lruSucc=parent->lruSucc;
else
lruHead=parent->lruSucc;
if(parent->lruSucc!=0)
parent->lruSucc->lruPred=parent->lruPred;
else
lruTail=parent->lruPred;
parent->lruPred=0;
parent->lruSucc=0;
}
/* Add the node to the leaf parent node list: */
node.lruPred=lruTail;
node.lruSucc=0;
if(lruTail!=0)
lruTail->lruSucc=&node;
else
lruHead=&node;
lruTail=&node;
/* Update the node cache: */
numCachedNodes+=8U;
}
/* Create and load the node's children: */
{
#if ALLOW_THREADING
Threads::Mutex::Lock fileLock(fileMutex);
#endif
Node* children=new Node[8];
indexFile.setReadPosAbs(node.childrenOffset);
for(int childIndex=0;childIndex<8;++childIndex)
{
Node& child=children[childIndex];
/* Read the child node's structure: */
LidarOctreeFileNode ofn;
ofn.read(indexFile);
child.parent=&node;
child.childrenOffset=ofn.childrenOffset;
child.domain=Cube(node.domain,childIndex);
child.numPoints=ofn.numPoints;
child.dataOffset=ofn.dataOffset;
child.detailSize=ofn.detailSize;
if(child.numPoints>0)
{
/* Load the child node's points: */
child.points=new LidarPoint[maxNumPointsPerNode]; // Always allocate maximum to prevent memory fragmentation
pointsFile.setReadPosAbs(LidarDataFileHeader::getFileSize()+pointsRecordSize*child.dataOffset);
pointsFile.read(child.points,child.numPoints);
}
++numLoadedNodes;
}
/* Install the node's children array: */
node.children=children;
}
}
void LidarProcessOctree::enterNodeRec(LidarProcessOctree::Node* node)
{
/* Enter all nodes from the root to the given node: */
while(node!=0)
{
node->enter();
node=node->parent;
}
}
void LidarProcessOctree::leaveNodeRec(LidarProcessOctree::Node* node)
{
/* Leave all nodes from the root to the given node: */
while(node!=0)
{
node->leave();
node=node->parent;
}
}
LidarProcessOctree::Node* LidarProcessOctree::nextNodePrefix(LidarProcessOctree::Node* node)
{
/* Check if the node is an interior node: */
if(node->childrenOffset!=0)
{
/* Subdivide the node if necessary: */
if(node->children==0)
subdivide(*node);
/* Go to the node's first child: */
return &node->children[0];
}
else
{
while(node->parent!=0)
{
/* Find the node's index among its parent's children: */
int childIndex;
for(childIndex=0;childIndex<8&&node!=&node->parent->children[childIndex];++childIndex)
;
/* Return the node's next sibling if there is one: */
if(childIndex+1<8)
return &node->parent->children[childIndex+1];
/* Try the node's parent: */
node=node->parent;
}
/* The root doesn't have siblings; we're done here: */
return 0;
}
}
namespace {
/***********************************************************
Helper functions to load LiDAR files given a base file name:
***********************************************************/
std::string getLidarPartFileName(const char* lidarFileName,const char* partFileName)
{
std::string result=lidarFileName;
result.push_back('/');
result.append(partFileName);
return result;
}
}
LidarProcessOctree::LidarProcessOctree(const char* lidarFileName,size_t sCacheSize)
:indexFile(getLidarPartFileName(lidarFileName,"Index").c_str()),
pointsFile(getLidarPartFileName(lidarFileName,"Points").c_str()),
offset(OffsetVector::zero),
numSubdivideCalls(0),numLoadedNodes(0),
lruHead(0),lruTail(0)
{
indexFile.setEndianness(Misc::LittleEndian);
pointsFile.setEndianness(Misc::LittleEndian);
/* Read the octree file header: */
LidarOctreeFileHeader ofh(indexFile);
/* Initialize the root node's domain: */
root.domain=ofh.domain;
/* Initialize the tree structure: */
maxNumPointsPerNode=ofh.maxNumPointsPerNode;
/* Calculate the memory and GPU cache sizes: */
size_t memNodeSize=sizeof(Node)+size_t(maxNumPointsPerNode)*sizeof(LidarPoint);
cacheSize=(unsigned int)(sCacheSize/memNodeSize);
if(cacheSize==0U)
Misc::throwStdErr("LidarProcessOctree::LidarProcessOctree: Specified memory cache size too small");
std::cout<<"Cache size: "<<cacheSize<<" memory nodes"<<std::endl;
/* Read the root node's structure: */
LidarOctreeFileNode rootfn;
rootfn.read(indexFile);
root.childrenOffset=rootfn.childrenOffset;
root.numPoints=rootfn.numPoints;
root.dataOffset=rootfn.dataOffset;
root.detailSize=rootfn.detailSize;
/* Get the total number of nodes by dividing the index file's size by the size of one octree node: */
numNodes=size_t((indexFile.getSize()-LidarOctreeFileHeader::getFileSize())/LidarFile::Offset(LidarOctreeFileNode::getFileSize()));
/* Read the point file's header: */
LidarDataFileHeader dfh(pointsFile);
pointsRecordSize=LidarFile::Offset(dfh.recordSize);
if(root.numPoints>0)
{
/* Load the root node's points: */
root.points=new LidarPoint[maxNumPointsPerNode]; // Always allocate maximum to prevent memory fragmentation
pointsFile.setReadPosAbs(LidarDataFileHeader::getFileSize()+pointsRecordSize*root.dataOffset);
pointsFile.read(root.points,root.numPoints);
}
++numLoadedNodes;
/* Try loading an offset file: */
try
{
IO::FilePtr offsetFile(IO::openFile(getLidarPartFileName(lidarFileName,"Offset").c_str()));
offsetFile->setEndianness(Misc::LittleEndian);
/* Read the original offset vector: */
offsetFile->read<OffsetVector::Scalar>(offset.getComponents(),3);
/* Invert the offset transformation: */
offset=-offset;
}
catch(IO::File::OpenError err)
{
/* Ignore the error */
}
/* Initialize the node cache: */
numCachedNodes=1U;
}
LidarProcessOctree::~LidarProcessOctree(void)
{
}
Point LidarProcessOctree::getRootCenter(void) const
{
return root.domain.getCenter();
}
Scalar LidarProcessOctree::getRootSize(void) const
{
Scalar size=Scalar(0);
for(int i=0;i<3;++i)
size+=root.domain.getMax()[i]-root.domain.getMin()[i];
return size/Scalar(6);
}