forked from KeckCAVES/LidarViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RidgeFinder.h
273 lines (238 loc) · 8.53 KB
/
RidgeFinder.h
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
/***********************************************************************
RidgeFinder - Functor classes to find ridges in LiDAR data sets.
Copyright (c) 2009 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
***********************************************************************/
#ifndef RIDGEFINDER_INCLUDED
#define RIDGEFINDER_INCLUDED
#include <Geometry/AffineTransformation.h>
#include <Geometry/PCACalculator.h>
#include "LidarTypes.h"
#include "LidarOctree.h"
#include "PointPCACalculator.h"
class RidgeFinder
{
/* Embedded classes: */
private:
class PointClassifier // Class to classify a point as ridge or else
{
/* Embedded classes: */
public:
typedef Geometry::AffineTransformation<double,3> ATransform; // Type for transformations into a plane's local coordinate system
/* Elements: */
private:
Point queryPoint; // The query point for which to calculate a plane equation
Scalar radius,radius2; // (Squared) search radius around query point
ATransform toPlane; // Transformation to plane's local coordinates
Geometry::PCACalculator<2> pcas[2]; // PCA calculators for points below and above the plane, respectively
/* Constructors and destructors: */
public:
PointClassifier(const Point& sQueryPoint,Scalar sRadius2,const PointPCACalculator::Plane& sPlane)
:queryPoint(sQueryPoint),radius2(sRadius2)
{
/* Calculate the transformation into the plane's local coordinate system: */
Vector z=sPlane.getNormal();
z.normalize();
Vector x=Geometry::normal(z);
x.normalize();
Vector y=Geometry::cross(z,x);
z.normalize();
Point o=sPlane.project(queryPoint);
ATransform::Matrix m;
for(int i=0;i<3;++i)
{
m(i,0)=x[i];
m(i,1)=y[i];
m(i,2)=z[i];
m(i,3)=o[i];
}
toPlane=ATransform(m);
}
/* Methods: */
void operator()(const LidarPoint& lp) // Process the given LiDAR point
{
/* Convert the point to the plane's local coordinate system: */
ATransform::Point pp=toPlane.transform(lp);
/* Accumulate the point into the lower or upper PCA calculator: */
if(pp[2]<0.0)
pcas[0].accumulatePoint(pp);
else
pcas[1].accumulatePoint(pp);
}
const Point& getQueryPoint(void) const
{
return queryPoint;
}
Scalar getQueryRadius2(void) const
{
return radius2;
}
bool isRidge(void) // Returns true if the query point is a ridge point
{
if(pcas[0].getNumPoints()>=2&&pcas[1].getNumPoints()>=2)
{
double radius=Math::sqrt(radius2);
/* Calculate eigenvalues of both PCAs: */
double evs[2][2];
unsigned int numEvs[2];
bool isRidges[2];
for(int i=0;i<2;++i)
{
pcas[i].calcCovariance();
numEvs[i]=pcas[i].calcEigenvalues(evs[i]);
isRidges[i]=numEvs[i]==2&&Math::abs(evs[i][0])>=radius*0.75&&Math::abs(evs[i][1])<=radius*0.333;
}
if(isRidges[0]&&!isRidges[1])
{
ATransform::Point qp=toPlane.transform(queryPoint);
return pcas[0].calcEigenvector(evs[0][1])*(Geometry::PCACalculator<2>::Point(qp[0],qp[1])-pcas[0].calcCentroid())<=1.0;
}
else if(!isRidges[0]&&isRidges[1])
{
ATransform::Point qp=toPlane.transform(queryPoint);
return pcas[1].calcEigenvector(evs[1][1])*(Geometry::PCACalculator<2>::Point(qp[0],qp[1])-pcas[1].calcCentroid())<=1.0;
}
else
return false;
}
else
return false;
}
};
/* Elements: */
private:
const LidarOctree* lidarOctree; // Pointer to the LiDAR octree to traverse
Scalar radius2; // Squared radius of the neighborhood for each point
/* Constructors and destructors: */
public:
RidgeFinder(const LidarOctree* sLidarOctree,Scalar sRadius) // Creates a ridge finder for the given LiDAR octree with the given neighborhood radius
:lidarOctree(sLidarOctree),
radius2(Math::sqr(sRadius))
{
}
/* Methods: */
void operator()(const LidarPoint& lp,LidarOctree::Color& color) // Colors the given LiDAR point based on its classification
{
/* Calculate the PCA of the point's neighborhood: */
PointPCACalculator ppca(lp,radius2);
lidarOctree->processPointsDirected(ppca);
if(ppca.getNumPoints()>=3)
{
/* Run a ridge classifier on the point: */
PointClassifier pc(lp,radius2,ppca.getPlane());
lidarOctree->processPointsDirected(pc);
/* Color the point: */
if(pc.isRidge())
color=GLColor<GLfloat,3>(1.0,1.0,0.0);
else
color=GLColor<GLfloat,3>(0.0,0.0,1.0);
}
else
color=GLColor<GLfloat,3>(0.0,0.0,1.0);
};
};
#if 0
#include <Geometry/Plane.h>
#include <Geometry/PCACalculator.h>
class PlaneCalculator // Class to find the best-fitting plane for a point neighborhood
{
/* Embedded classes: */
public:
typedef Geometry::Plane<double,3> Plane; // Type for planes
/* Elements: */
private:
Point queryPoint; // The query point for which to calculate a plane equation
Scalar radius2; // Squared search radius around query point
Geometry::PCACalculator<3> pca; // Helper structure to calculate principal components of traversed point set
/* Constructors and destructors: */
public:
PlaneCalculator(const Point& sQueryPoint,Scalar sRadius2) // Creates an empty plane calculator
:queryPoint(sQueryPoint),radius2(sRadius2)
{
}
/* Methods: */
void operator()(const LidarPoint& lp) // Process the given LiDAR point
{
/* Accumulate the point into the PCA calculator: */
pca.accumulatePoint(lp);
}
const Point& getQueryPoint(void) const
{
return queryPoint;
}
Scalar getQueryRadius2(void) const
{
return radius2;
}
size_t getNumPoints(void) const // Returns the number of processed points
{
return pca.getNumPoints();
}
Plane calcPlane(void) const; // Returns the least-squares plane fitting the processed points
};
class PointClassifier // Class to classify a point as ridge or else
{
/* Embedded classes: */
public:
typedef Geometry::AffineTransformation<double,3> ATransform; // Type for transformations into a plane's local coordinate system
/* Elements: */
private:
Point queryPoint; // The query point for which to calculate a plane equation
Scalar radius,radius2; // (Squared) search radius around query point
PlaneCalculator::Plane plane; // The neighborhood's best-fitting plane
ATransform toPlane; // Transformation to plane's local coordinates
PCACalculator<2> pcas[2]; // PCA calculators for points below and above the plane, respectively
/* Constructors and destructors: */
PointClassifier(const Point& sQueryPoint,Scalar sRadius2,const PlaneCalculator::Plane& sPlane);
/* Methods: */
void operator()(const LidarPoint& lp) // Process the given LiDAR point
{
/* Convert the point to the plane's local coordinate system: */
ATransform::Point pp=toPlane.transform(lp);
/* Accumulate the point into the lower or upper PCA calculator: */
if(pp[2]<0.0)
pcas[0].accumulatePoint(pp);
else
pcas[1].accumulatePoint(pp);
}
const Point& getQueryPoint(void) const
{
return queryPoint;
}
Scalar getQueryRadius2(void) const
{
return radius2;
}
bool isRidge(void) const; // Returns true if the query point is a ridge point
};
class RidgeFinder // Class to classify points in a LiDAR data set as ridges or else
{
/* Elements: */
private:
LidarProcessOctree& lpo; // The processed LiDAR octree
Scalar radius2; // The squared search radius around each LiDAR point
Color* colorBuffer; // Buffer to collect a node's point colors
LidarFile::Offset colorDataSize; // Size of each record in the color file
LidarFile colorFile; // The file to which to write the classification color data
size_t numProcessedNodes; // Number of already processed nodes
/* Constructors and destructors: */
public:
RidgeFinder(LidarProcessOctree& sLpo,Scalar sRadius,const char* colorFileName); // Creates a ridge finder with the given parameters
~RidgeFinder(void);
/* Methods: */
void operator()(LidarProcessOctree::Node& node,unsigned int nodeLevel);
};
#endif
#endif