-
Notifications
You must be signed in to change notification settings - Fork 39
/
ProfileExtractor.cpp
227 lines (199 loc) · 7.03 KB
/
ProfileExtractor.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
/***********************************************************************
ProfileExtractor - Algorithm to extract straight-line profiles from 2.5D
LiDAR data.
Copyright (c) 2010-2011 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 "ProfileExtractor.h"
#include <vector>
#include <Cluster/MulticastPipe.h>
#include <SceneGraph/GroupNode.h>
#include <SceneGraph/ColorNode.h>
#include <SceneGraph/CoordinateNode.h>
#include <SceneGraph/IndexedLineSetNode.h>
#include <SceneGraph/ShapeNode.h>
#include "LidarOctree.h"
#include "SceneGraph.h"
class Sampler // Class to sample the implicit surface defined by a LiDAR point cloud at an (x, y) position
{
/* Elements: */
private:
double pos[2]; // Sample point's (x, y) position
double dx[2],dy[2]; // Sample frame orientations in x and y
double step[2]; // Sample step size along dx and dy orientations
double numLobes; // Number of lobes in Lanczos reconstruction filter
double accumulator; // Accumulated convolution between point cloud and filter
double weightSum; // Sum of weights for all accumulated points
double absWeightSum; // Sum of absolute weights for all accumulated points
/* Constructors and destructors: */
public:
Sampler(const double sDx[2],const double sDy[2],const double sStep[2],int sNumLobes) // Creates an empty sampler
:numLobes(double(sNumLobes))
{
for(int i=0;i<2;++i)
{
dx[i]=sDx[i];
dy[i]=sDy[i];
step[i]=sStep[i];
}
}
/* Methods: */
void setPos(const Point& p)
{
/* Copy the sample position: */
for(int i=0;i<2;++i)
pos[i]=double(p[i]);
/* Reset the filter accumulator: */
accumulator=0.0;
weightSum=0.0;
absWeightSum=0.0;
}
void operator()(const LidarPoint& p)
{
/* Calculate the sample coordinates of the LiDAR point: */
double lp[2];
lp[0]=double(p[0]-pos[0])*dx[0]+double(p[1]-pos[1])*dx[1];
lp[1]=double(p[0]-pos[0])*dy[0]+double(p[1]-pos[1])*dy[1];
/* Calculate the Lanczos filter weight for the LiDAR point: */
double weight=1.0;
for(int i=0;i<2;++i)
{
double x=Math::Constants<double>::pi*lp[i]/step[i];
if(Math::abs(x)>=numLobes)
weight=0.0;
else if(x!=0.0)
{
weight*=Math::sin(x)/x;
x/=numLobes;
weight*=Math::sin(x)/x;
}
}
/* Accumulate the weighted point: */
accumulator+=double(p[2])*weight;
weightSum+=weight;
absWeightSum+=Math::abs(weight);
}
double getWeightSum(void) const // Returns the sum of point weights
{
return weightSum;
}
double getAbsWeightSum(void) const // Returns the sum of absolute point weights
{
return absWeightSum;
}
double getValue(void) const // Returns the convolution result
{
/* Return the weighted average: */
return accumulator/weightSum;
}
};
void extractProfile(const LidarOctree* octree,const Point& p0,const Point& p1,double segmentLength,int oversampling,double filterWidth,int numLobes,Cluster::MulticastPipe* pipe)
{
std::vector<Point> profilePoints;
if(pipe==0||pipe->isMaster())
{
/* Calculate the distribution of sample points along the profile: */
double pLen=Geometry::dist(p0,p1);
int numSegments=int(pLen/segmentLength+0.5);
segmentLength=pLen/double(numSegments);
numSegments*=oversampling;
/* Create the profile point list: */
profilePoints.reserve(numSegments+1);
profilePoints.push_back(p0);
for(int i=1;i<numSegments;++i)
{
float lambda=float(double(i)/double(numSegments));
profilePoints.push_back(Geometry::affineCombination(p0,p1,lambda));
}
profilePoints.push_back(p1);
/* Set up the sampling filter frame: */
double dx[2],dy[2],step[2];
dx[0]=double(p1[0])-double(p0[0]);
dx[1]=double(p1[1])-double(p0[1]);
double dxMag=Math::sqrt(Math::sqr(dx[0])+Math::sqr(dx[1]));
dx[0]/=dxMag;
dx[1]/=dxMag;
dy[0]=dx[1];
dy[1]=-dx[0];
step[0]=segmentLength;
step[1]=filterWidth;
Sampler sampler(dx,dy,step,numLobes);
/* Sample all profile points: */
for(std::vector<Point>::iterator ppIt=profilePoints.begin();ppIt!=profilePoints.end();++ppIt)
{
/* Calculate the bounding box of the sampling filter's support: */
Vector bx=Vector(dx[0],dx[1],0.0f)*(float(segmentLength)*float(numLobes));
Vector by=Vector(dy[0],dy[1],0.0f)*(float(filterWidth)*float(numLobes));
Box frame=Box::empty;
frame.min[2]=Math::Constants<float>::min;
frame.max[2]=Math::Constants<float>::max;
frame.addPoint(*ppIt-bx-by);
frame.addPoint(*ppIt-bx+by);
frame.addPoint(*ppIt+bx-by);
frame.addPoint(*ppIt+bx+by);
/* Sample the point: */
sampler.setPos(*ppIt);
octree->processPointsInBox(frame,sampler);
/* Set the profile point's elevation: */
(*ppIt)[2]=sampler.getValue();
}
if(pipe!=0)
{
/* Send the extracted profile points to the slaves: */
pipe->write<unsigned int>(profilePoints.size());
for(std::vector<Point>::iterator ppIt=profilePoints.begin();ppIt!=profilePoints.end();++ppIt)
pipe->write<Point::Scalar>(ppIt->getComponents(),3);
pipe->flush();
}
}
else
{
/* Retrieve the extracted profile points from the master: */
unsigned int numProfilePoints=pipe->read<unsigned int>();
profilePoints.reserve(numProfilePoints);
for(unsigned int i=0;i<numProfilePoints;++i)
{
Point pp;
pipe->read<Point::Scalar>(pp.getComponents(),3);
profilePoints.push_back(pp);
}
}
/* Add the profile to the scene graph root: */
SceneGraph::GroupNode* root=new SceneGraph::GroupNode;
getSceneGraphRoot().children.appendValue(root);
getSceneGraphRoot().update();
SceneGraph::ShapeNode* s=new SceneGraph::ShapeNode;
root->children.appendValue(s);
{
SceneGraph::IndexedLineSetNode* ils=new SceneGraph::IndexedLineSetNode;
s->geometry.setValue(ils);
SceneGraph::ColorNode* color=new SceneGraph::ColorNode;
ils->color.setValue(color);
color->color.appendValue(SceneGraph::Color(0.0f,0.5f,0.0f));
color->update();
SceneGraph::CoordinateNode* coord=new SceneGraph::CoordinateNode;
ils->coord.setValue(coord);
for(std::vector<Point>::iterator ppIt=profilePoints.begin();ppIt!=profilePoints.end();++ppIt)
coord->point.appendValue(*ppIt);
coord->update();
for(unsigned int i=0;i<profilePoints.size();++i)
ils->coordIndex.appendValue(i);
ils->colorPerVertex.setValue(false);
ils->lineWidth.setValue(3.0f);
ils->update();
}
s->update();
root->update();
}