forked from midas-journal/midas-journal-709
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvtkRANSACPlane.cxx
executable file
·347 lines (273 loc) · 9.44 KB
/
vtkRANSACPlane.cxx
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
#include "vtkObjectFactory.h" //for new() macro
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStreamingDemandDrivenPipeline.h"
//#include "vtkCommand.h"
#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkPlane.h"
#include "vtkSmartPointer.h"
#include "vtkCellArray.h"
#include "vtkMath.h"
#include "vtkPlaneSource.h"
#include <set>
#include "vtkRANSACPlane.h"
vtkStandardNewMacro(vtkRANSACPlane);
//-----------------------------------------------------------------------------
vtkRANSACPlane::vtkRANSACPlane()
{
this->InlierThreshold = 1.0;
this->MaxIterations = 1000;
this->NumPointsToFit = 3;
this->GoodEnough = 1.0;
this->SetNumberOfInputPorts( 1 );
this->SetNumberOfOutputPorts( 1 );
}
//-----------------------------------------------------------------------------
vtkRANSACPlane::~vtkRANSACPlane()
{
}
//----------------------------------------------------------------------------
void vtkRANSACPlane::AddSourceConnection(vtkAlgorithmOutput* input)
{
this->AddInputConnection(1, input);
}
//----------------------------------------------------------------------------
void vtkRANSACPlane::RemoveAllSources()
{
this->SetInputConnection(1, 0);
}
//----------------------------------------------------------------------------
vtkPolyData* vtkRANSACPlane::GetOutput()
{
return vtkPolyData::SafeDownCast(this->GetOutputDataObject(0));
}
//----------------------------------------------------------------------------
int vtkRANSACPlane::FillInputPortInformation( int port, vtkInformation* info )
{
if (!this->Superclass::FillInputPortInformation(port, info))
{
return 0;
}
if ( port == 0 )
{
info->Set( vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet" );
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
int vtkRANSACPlane::RequestData(vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkPointSet *input = vtkPointSet::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
//track best model
//vtkSmartPointer<vtkPlane> bestPlane = vtkSmartPointer<vtkPlane>::New();
this->BestPlane = vtkSmartPointer<vtkPlane>::New();
//track number of inliers of best model
unsigned int maxInliers = 0;
//seed the random number gererator
// !!! ///
for(unsigned int iter = 0; iter < this->MaxIterations; iter++)
{
cout << "Iteration: " << iter << endl;
//pick NumPointsToFit random indices
std::vector<unsigned int> randomIndices = UniqueRandomIndices(input->GetNumberOfPoints(), NumPointsToFit);
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
ExtractPoints(input, randomIndices, points);
//find the best plane through these random points
vtkSmartPointer<vtkPlane> plane = vtkSmartPointer<vtkPlane>::New();
BestFitPlane(points, plane);
std::vector<unsigned int> inlierIndices = this->DetermineInliers(input->GetPoints(), plane);
cout << "Number of inliers: " << inlierIndices.size() << endl;
if(inlierIndices.size() > maxInliers)
{
maxInliers = inlierIndices.size();
//bestPlane->ShallowCopy(plane);
CopyPlane(plane, this->BestPlane);
double n[3];
this->BestPlane->GetNormal(n);
cout << "normal: " << n[0] << " " << n[1] << " " << n[2] << endl;
}
if(inlierIndices.size() > input->GetNumberOfPoints() * this->GoodEnough) //if GoodEnough % of the points fit the model, we can stop the search
{
break;
}
} //end ransac loop
cout << "Best plane: " << endl << "---------------" << endl;
cout << "Inliers: " << maxInliers << endl;
double n[3];
this->BestPlane->GetNormal(n);
cout << "normal: " << n[0] << " " << n[1] << " " << n[2] << endl;
//output->ShallowCopy(bestPlane);
vtkSmartPointer<vtkPlaneSource> planeSource = vtkSmartPointer<vtkPlaneSource>::New();
planeSource->SetNormal(n);
planeSource->SetCenter(this->BestPlane->GetOrigin());
planeSource->Update();
output->ShallowCopy(planeSource->GetOutput());
return 1;
}
void vtkRANSACPlane::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
std::vector<unsigned int> vtkRANSACPlane::DetermineInliers(vtkPoints* points, vtkPlane* plane)
{
//find the distance from every point to the plane
std::vector<unsigned int> inlierIndices;
for(unsigned int i = 0; i < points->GetNumberOfPoints(); i++)
{
//double distance = fabs(vgl_distance(Plane, Points[i]));
double point[3];
points->GetPoint(i,point);
//double distance = plane->DistanceToPlane(p);
double n[3];
double o[3];
plane->GetNormal(n);
plane->GetOrigin(o);
double distance = vtkPlane::DistanceToPlane(point, n, o);
if(distance < this->InlierThreshold)
{
inlierIndices.push_back(i);
}
}
return inlierIndices;
}
///////////////////////////////////////
////////// Helper Functions /////////////
////////////////////////////////////////
std::vector<unsigned int> UniqueRandomIndices(const unsigned int maxIndex, const unsigned int numIndices)
{
//generate Number unique random indices from 0 to MAX
std::vector<unsigned int> indices;
//SeedRandom();
//cannot generate more unique numbers than than the size of the set we are sampling
if(!(numIndices <= maxIndex+1))
{
return indices;
}
std::set<unsigned int> S;
while(S.size() < numIndices)
{
S.insert(vtkMath::Random(0, maxIndex));
}
for(std::set<unsigned int>::iterator iter = S.begin(); iter != S.end(); iter++)
{
indices.push_back(*iter);
}
return indices;
}
void ExtractPoints(vtkPointSet* points, std::vector<unsigned int> indices, vtkPoints* output)
{
for(unsigned int i = 0; i < indices.size(); i++)
{
double p[3];
points->GetPoint(indices[i], p);
output->InsertNextPoint(p[0], p[1], p[2]);
}
}
/* allocate memory for an nrow x ncol matrix */
template<class TReal>
TReal **create_matrix ( long nrow, long ncol )
{
typedef TReal* TRealPointer;
TReal **m = new TRealPointer[nrow];
TReal* block = ( TReal* ) calloc ( nrow*ncol, sizeof ( TReal ) );
m[0] = block;
for ( int row = 1; row < nrow; ++row )
{
m[ row ] = &block[ row * ncol ];
}
return m;
}
/* free a TReal matrix allocated with matrix() */
template<class TReal>
void free_matrix ( TReal **m )
{
free ( m[0] );
delete[] m;
}
void BestFitPlane(vtkPoints *points, vtkPlane *BestPlane)
{
//Compute the best fit (least squares) plane through a set of points.
vtkIdType NumPoints = points->GetNumberOfPoints();
double dNumPoints = static_cast<double>(NumPoints);
//find the center of mass of the points
double Center[3];
CenterOfMass(points, Center);
//std::cout << "Center of mass: " << Center[0] << " " << Center[1] << " " << Center[2] << std::endl;
//Compute sample covariance matrix
double **a = create_matrix<double> ( 3,3 );
a[0][0] = 0; a[0][1] = 0; a[0][2] = 0;
a[1][0] = 0; a[1][1] = 0; a[1][2] = 0;
a[2][0] = 0; a[2][1] = 0; a[2][2] = 0;
for(unsigned int pointId = 0; pointId < NumPoints; pointId++ )
{
double x[3];
double xp[3];
points->GetPoint(pointId, x);
xp[0] = x[0] - Center[0];
xp[1] = x[1] - Center[1];
xp[2] = x[2] - Center[2];
for (unsigned int i = 0; i < 3; i++)
{
a[0][i] += xp[0] * xp[i];
a[1][i] += xp[1] * xp[i];
a[2][i] += xp[2] * xp[i];
}
}
//divide by N-1
for(unsigned int i = 0; i < 3; i++)
{
a[0][i] /= dNumPoints-1;
a[1][i] /= dNumPoints-1;
a[2][i] /= dNumPoints-1;
}
// Extract eigenvectors from covariance matrix
double **eigvec = create_matrix<double> ( 3,3 );
double eigval[3];
vtkMath::Jacobi(a,eigval,eigvec);
//Jacobi iteration for the solution of eigenvectors/eigenvalues of a 3x3 real symmetric matrix. Square 3x3 matrix a; output eigenvalues in w; and output eigenvectors in v. Resulting eigenvalues/vectors are sorted in decreasing order; eigenvectors are normalized.
//Set the plane normal to the smallest eigen vector
BestPlane->SetNormal(eigvec[0][2], eigvec[1][2], eigvec[2][2]);
//cleanup
free_matrix(eigvec);
free_matrix(a);
//Set the plane origin to the center of mass
BestPlane->SetOrigin(Center[0], Center[1], Center[2]);
}
void CopyPlane(vtkPlane* plane, vtkPlane* output)
{
double n[3];
plane->GetNormal(n);
double o[3];
plane->GetOrigin(o);
output->SetNormal(n);
output->SetOrigin(o);
}
void CenterOfMass(vtkPoints* points, double* center)
{
center[0] = 0.0;
center[1] = 0.0;
center[2] = 0.0;
for(vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
{
double point[3];
points->GetPoint(i, point);
center[0] += point[0];
center[1] += point[1];
center[2] += point[2];
}
double numberOfPoints = static_cast<double>(points->GetNumberOfPoints());
center[0] = center[0]/numberOfPoints;
center[1] = center[1]/numberOfPoints;
center[2] = center[2]/numberOfPoints;
}