forked from KeckCAVES/LidarViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RidgeFinder.cpp
174 lines (150 loc) · 5.15 KB
/
RidgeFinder.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
/***********************************************************************
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
***********************************************************************/
#include "RidgeFinder.h"
/********************************
Methods of class PlaneCalculator:
********************************/
PlaneCalculator::calcPlane(void) const
{
/* Calculate the covariance matrix of the point neighborhood: */
pca.calcCovariance();
/* Calculate the eigenvalues of the covariance matrix: */
double evs[3];
pca.calcEigenvalues(evs);
/* Calculate the eigenvector of the smallest eigenvalue: */
Plane::Vector normal=pca.calcEigenvector(evs[2]);
/* Calculate the centroid of the point neighborhood: */
Plane::Point centroid=pca.calcCentroid();
/* Return the resulting plane equation: */
return Plane(normal,centroid);
}
/********************************
Methods of class PointClassifier:
********************************/
PointClassifier::PointClassifier(const Point& sQueryPoint,Scalar sRadius2,const PlaneCalculator::Plane& sPlane)
:queryPoint(sQueryPoint),radius2(sRadius2),plane(sPlane)
{
/* Calculate the transformation into the plane's local coordinate system: */
Vector z=plane.getNormal();
z.normalize();
Vector x=Geometry::normal(z);
x.normalize();
Vector y=Geometry::cross(z,x);
z.normalize();
Point o=plane.project(node[i]);
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);
toPlane.doInvert();
}
bool PointClassifier::isRidge(void) const
{
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.9&&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])*(PCACalculator<2>::Point(qp[0],qp[1])-pcas[0].getCentroid())<=1.0;
}
else if(!isRidges[0]&&isRidges[1])
{
ATransform::Point qp=toPlane.transform(queryPoint);
return pcas[1].calcEigenvector(evs[1][1])*(PCACalculator<2>::Point(qp[0],qp[1])-pcas[1].getCentroid())<=1.0;
}
else
return false;
}
else
return false;
}
/****************************
Methods of class RidgeFinder:
****************************/
RidgeFinder::RidgeFinder(LidarProcessOctree& sLpo,Scalar sRadius,const char* colorFileName)
:lpo(sLpo),
radius2(Math::sqr(sRadius)),
colorBuffer(new Color[lpo.getMaxNumPointsPerNode()]),
colorDataSize(sizeof(Color)),
colorFile(colorFileName,"w+b",LidarFile::LittleEndian),
numProcessedNodes(0)
{
/* Write the color file's header: */
LidarDataFileHeader dfh((unsigned int)(colorDataSize));
dfh.write(colorFile);
}
RidgeFinder::~RidgeFinder(void)
{
delete[] colorBuffer;
}
void RidgeFinder::operator()(LidarProcessOctree::Node& node,unsigned int nodeLevel)
{
/* Check if this node is a leaf or an interior node: */
if(node.isLeaf())
{
/* Process each point in the node: */
for(unsigned int i=0;i<node.getNumPoints();++i)
{
/* Calculate a best-fitting plane equation for the point's neighborhood: */
PlaneCalculator planeCalculator(node[i],radius2);
lpo.processPointsDirected(planeCalculator);
/* Check if the point has neighbors: */
if(planeCalculator.getNumPoints()>=3)
{
/* Run a ridge classifier on the point: */
PointClassifier pc(queryPoint,radius2,planeCalculator.getPlane());
lpo.processPointDirected(pc);
if(pc.isRidge())
colorBuffer[i]=Color(255,255,255);
else
colorBuffer[i]=Color(0,0,0);
}
else
{
/* Declare the point a non-ridge: */
colorBuffer[i]=Color(0,0,0);
}
}
}
else
{
}
/* Write the node's colors to the color file: */
colorFile.seekSet(LidarDataFileHeader::getFileSize()+colorDataSize*node.getDataOffset());
colorFile.write(colorBuffer,node.getNumPoints());
/* Update the progress counter: */
++numProcessedNodes;
int percent=int((numProcessedNodes*100+lpo.getNumNodes()/2)/lpo.getNumNodes());
std::cout<<"\b\b\b\b"<<std::setw(3)<<percent<<"%"<<std::flush;
}