-
Notifications
You must be signed in to change notification settings - Fork 39
/
SpherePrimitive.cpp
219 lines (176 loc) · 6.28 KB
/
SpherePrimitive.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
/***********************************************************************
SpherePrimitive - Class for spheres extracted from point clouds.
Copyright (c) 2007-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 "SpherePrimitive.h"
#include <iostream>
#include <Misc/ThrowStdErr.h>
#include <IO/File.h>
#include <Cluster/MulticastPipe.h>
#include <Math/Math.h>
#include <Geometry/Vector.h>
#include <GL/gl.h>
#include <GL/GLColorTemplates.h>
#include <GL/GLContextData.h>
#include <GL/GLModels.h>
#include <GL/GLGeometryWrappers.h>
#include "LidarOctree.h"
#include "LidarSelectionExtractor.h"
#include "SphereFitter.h"
#include "LevenbergMarquardtMinimizer.h"
/******************************************
Methods of class SpherePrimitive::DataItem:
******************************************/
SpherePrimitive::DataItem::DataItem(void)
:displayListId(glGenLists(2))
{
}
SpherePrimitive::DataItem::~DataItem(void)
{
glDeleteLists(displayListId,2);
}
/********************************
Methods of class SpherePrimitive:
********************************/
SpherePrimitive::SpherePrimitive(const LidarOctree* octree,const Primitive::Vector& translation,Cluster::MulticastPipe* pipe)
{
/* Extract all selected points from the octree: */
LidarSelectionExtractor<SphereFitter::Point> lse;
octree->processSelectedPoints(lse);
if(lse.getPoints().size()>=4)
{
/* Create a sphere fitter: */
SphereFitter sf(lse.getPoints());
/* Minimize the target function: */
Scalar f=LevenbergMarquardtMinimizer<SphereFitter>::minimize(sf);
/* Store the number of points and the RMS residual: */
numPoints=lse.getPoints().size();
rms=Math::sqrt(f*Scalar(2)/Scalar(numPoints));
/* Extract the sphere parameters: */
point=sf.getCenter();
radius=sf.getRadius();
/* Print the sphere's equation: */
std::cout<<"Sphere fitting "<<numPoints<<" points"<<std::endl;
Point tPoint=point;
tPoint+=translation;
std::cout<<"Center point: ("<<tPoint[0]<<", "<<tPoint[1]<<", "<<tPoint[2]<<")"<<std::endl;
std::cout<<"Radius: "<<radius<<std::endl;
std::cout<<"RMS approximation residual: "<<rms<<std::endl;
if(pipe!=0)
{
/* Send the extracted primitive over the pipe: */
pipe->write<int>(1);
pipe->write<unsigned int>((unsigned int)(numPoints));
pipe->write<Scalar>(rms);
pipe->write<Scalar>(point.getComponents(),3);
pipe->write<Scalar>(radius);
pipe->flush();
}
}
else
{
if(pipe!=0)
{
pipe->write<int>(0);
pipe->flush();
}
Misc::throwStdErr("SpherePrimitive::SpherePrimitive: Not enough selected points");
}
}
SpherePrimitive::SpherePrimitive(Cluster::MulticastPipe* pipe)
{
/* Read the status flag from the pipe: */
if(!pipe->read<int>())
Misc::throwStdErr("SpherePrimitive::SpherePrimitive: Not enough selected points");
/* Read the number of points and the RMS residual: */
numPoints=pipe->read<unsigned int>();
rms=pipe->read<Scalar>();
/* Read the sphere parameters: */
pipe->read<Scalar>(point.getComponents(),3);
radius=pipe->read<Scalar>();
}
SpherePrimitive::SpherePrimitive(IO::File& file,const Primitive::Vector& translation)
{
/* Read the number of points and the RMS residual: */
numPoints=file.read<unsigned int>();
rms=file.read<Scalar>();
/* Read the sphere parameters: */
file.read<Scalar>(point.getComponents(),3);
point+=translation;
radius=file.read<Scalar>();
}
Primitive::Scalar SpherePrimitive::pick(const Primitive::Point& pickPoint,Primitive::Scalar maxPickDistance) const
{
/* Calculate the pick point's distance from the sphere's center: */
Scalar centerDist=Geometry::dist(pickPoint,point);
/* Return the pick point's distance from the sphere: */
return Math::abs(centerDist-radius);
}
void SpherePrimitive::initContext(GLContextData& contextData) const
{
/* Create a data item and store it in the context: */
DataItem* dataItem=new DataItem;
contextData.addDataItem(this,dataItem);
/* Create the sphere rendering display lists: */
glNewList(dataItem->displayListId,GL_COMPILE);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glDrawSphereIcosahedron(1.0,5);
glEndList();
glNewList(dataItem->displayListId+1,GL_COMPILE);
glBlendFunc(GL_ONE,GL_ONE);
glDisable(GL_CULL_FACE);
glLineWidth(1.0f);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glDrawSphereIcosahedron(1.0,5);
glEndList();
}
void SpherePrimitive::glRenderAction(GLContextData& contextData) const
{
/* Retrieve the data item: */
DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this);
glPushAttrib(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ENABLE_BIT|GL_LINE_BIT|GL_POINT_BIT|GL_POLYGON_BIT);
glDisable(GL_LIGHTING);
/* Draw the sphere's center: */
glPointSize(5.0f);
glColor(surfaceColor);
glBegin(GL_POINTS);
glVertex(point);
glEnd();
glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
glPushMatrix();
glTranslate(point-Point::origin);
glScale(radius);
/* Draw the surface: */
glColor(surfaceColor);
glCallList(dataItem->displayListId);
/* Draw the grid: */
glColor(gridColor);
glCallList(dataItem->displayListId+1);
glPopMatrix();
glPopAttrib();
}
void SpherePrimitive::write(IO::File& file,const Primitive::Vector& translation) const
{
/* Write the number of points and the RMS residual: */
file.write<unsigned int>((unsigned int)(numPoints));
file.write<Scalar>(rms);
/* Write the sphere parameters: */
file.write<Scalar>((point+translation).getComponents(),3);
file.write<Scalar>(radius);
}