-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
381 lines (297 loc) · 12.6 KB
/
main.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//#include <QtCore/QCoreApplication>
#include <QtDebug>
//#include <QVector>
#include <vtkSphereSource.h>
#include <vtkModifiedBSPTree.h>
#include <vtkDoubleArray.h>
#include <vtkMath.h>
#include <vtkDataArray.h>
#include <vtkPolyDataNormals.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPLYReader.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkLookupTable.h>
#include <vtkScalarBarActor.h>
#include <vtkPointData.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkGlyph3D.h>
#include <vtkProperty.h>
#include <vtkCellLocator.h>
#include <vtkCellData.h>
int main(int argc, char *argv[])
{
//Calc dist of B relative to A
//QCoreApplication a(argc, argv);
//settings
double SEARCHLENGTH = 15; // half length of search line
double TOL = 0.001; // bspTree tolerance
char *name = "dist";
bool DISPLAYNORMALS = false;
bool USESIMULATEDMESHES = false;
bool USEREVERSECHECK = true;
bool USECLOSESTPOINT = false;
char *scan1name = "banana4.ply";
char *scan2name = "banana1.ply";
// Create source meshes
vtkSmartPointer<vtkPolyData> polyA =
vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkPolyData> polyB =
vtkSmartPointer<vtkPolyData>::New();
if(USESIMULATEDMESHES)
{
double res = 100.;
vtkSmartPointer<vtkSphereSource> meshA =
vtkSmartPointer<vtkSphereSource>::New();
meshA->SetRadius(10.);
meshA->SetCenter(0.,0.,0.);
meshA->SetThetaResolution(res);
meshA->SetPhiResolution(res);
meshA->Update();
polyA = meshA->GetOutput();
vtkSmartPointer<vtkSphereSource> meshB =
vtkSmartPointer<vtkSphereSource>::New();
meshB->SetRadius(11.);
meshB->SetCenter(1.,0.,0.);
meshB->SetThetaResolution(res);
meshB->SetPhiResolution(res);
meshB->Update();
polyB = meshB->GetOutput();
}
else
{
vtkSmartPointer<vtkPLYReader> reader =
vtkSmartPointer<vtkPLYReader>::New();
reader->SetFileName(scan1name);
reader->Update();
polyA->DeepCopy(reader->GetOutput());
polyA->Update();
reader->SetFileName(scan2name);
reader->Update();
polyB->DeepCopy(reader->GetOutput());
polyB->Update();
}
// Calculate BSP tree
vtkSmartPointer<vtkModifiedBSPTree> bspTree =
vtkSmartPointer<vtkModifiedBSPTree>::New();
bspTree->SetDataSet(polyB);
bspTree->BuildLocator();
vtkSmartPointer<vtkModifiedBSPTree> bspTreeCell =
vtkSmartPointer<vtkModifiedBSPTree>::New();
bspTreeCell->SetDataSet(polyA);
bspTreeCell->BuildLocator();
vtkSmartPointer<vtkCellLocator> cellLoc =
vtkSmartPointer<vtkCellLocator>::New();
cellLoc->SetDataSet(polyB);
cellLoc->BuildLocator();
// Calculate meshA normals
vtkSmartPointer<vtkPolyDataNormals> normals =
vtkSmartPointer<vtkPolyDataNormals>::New();
normals->SetInput(polyA);
normals->ComputeCellNormalsOn();
normals->ComputePointNormalsOn();
//normals->SplittingOff();
normals->Update();
vtkSmartPointer<vtkDataArray> normalData = normals->GetOutput()->GetPointData()->GetNormals();
vtkSmartPointer<vtkDataArray> normalDataCell = normals->GetOutput()->GetCellData()->GetNormals();
/*! Diff Calculation */
//------------------------------------------------------------------------------------------------
double normal[3]; //stores normal extracted for a single point
double normalCell[3];
double normalLength; //length of the normal, used to ensure that it is normalized
double refPt[3]; //stores the current point on meshA
double vectPtN[3]; //end point of search line in -'ve normal direction
double vectPtP[3]; //end point of search line in +'ve normal direction
double xP[3]; //intersection point on cell
double xN[3];
double x[3];
double tP; //length along +'ve search line
double tN; //length along -'ve search line
double tCell;
double pcoords[3];
int subId;
int intersectP; //check if intersection occured
int intersectN; //check if intersection occured
int intersectCell; //check if intersection occured
double dist; //calculated distance for the current point on meshA to meshB
double distCell;
double max = -1.e22; //store max distance for scalarbar
double min = 1.e22; //store min distance for scalarbar
bool minAtP = false;
//QVector<double> *distVect = new QVector<double>();
vtkIdType a;
vtkIdType cellIdN;
vtkIdType cellIdP;
// create array to store distances
vtkSmartPointer<vtkDoubleArray> distArray =
vtkSmartPointer<vtkDoubleArray>::New();
distArray->SetNumberOfComponents(1);
distArray->SetName(name);
distArray->SetNumberOfValues(polyA->GetNumberOfPoints());
// loop throug each point in meshA
for(int ii = 0; ii < polyA->GetNumberOfPoints(); ii ++)
{
if(int(float(ii)/float(polyA->GetNumberOfPoints())*100.) % 10 == 0)
qDebug() << ii;
normalData->GetTuple(ii, normal); //get normal at point
normalDataCell->GetTuple(ii,normalCell);
polyA->GetPoint(ii, refPt); //get point location
if(!USECLOSESTPOINT)
{
// Create end points for lines about the +'ve and -'ve direction of the point normal
normalLength = sqrt(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2]);
vectPtN[0] = refPt[0] - SEARCHLENGTH * normal[0]/normalLength;
vectPtN[1] = refPt[1] - SEARCHLENGTH * normal[1]/normalLength;
vectPtN[2] = refPt[2] - SEARCHLENGTH * normal[2]/normalLength;
vectPtP[0] = refPt[0] + SEARCHLENGTH * normal[0]/normalLength;
vectPtP[1] = refPt[1] + SEARCHLENGTH * normal[1]/normalLength;
vectPtP[2] = refPt[2] + SEARCHLENGTH * normal[2]/normalLength;
// Check the intersection in the +'ve and -'ve directions: these must be seperated since bspTree returns the first intersection when
// traversing the line segment, which may not be the closest cell. This uses to line segments, both traversed from the point of interest.
// this then resutls in the closest intersection in both the +'ve and -'ve directions
intersectN = bspTree->IntersectWithLine(refPt, vectPtN, TOL, tN, xN, pcoords, subId,cellIdN);
intersectP = bspTree->IntersectWithLine(refPt, vectPtP,TOL, tP, xP, pcoords, subId, cellIdP);
// Logic to check for the closest intersection
if(intersectN == 0 && intersectP == 0) {
dist = 0; //case of no intersection, I don't know how to handle this value should really be NaN
minAtP = false;
}
else if(intersectN == 0 && intersectP != 0) {
dist = tP*SEARCHLENGTH; //case of only an intersection in the +'ve direction
minAtP = true;
}
else if(intersectP == 0 && intersectN !=0 ) {
dist = -tN*SEARCHLENGTH; //case of only an intersection in the -'ve direction
minAtP = false;
}
else //case of an intersection in both directions
{
if(tN <= tP) { //find the closest interseection
dist = -tN*SEARCHLENGTH;
minAtP = false;
}
else {
dist = tP*SEARCHLENGTH;
minAtP = true;
}
}
if(USEREVERSECHECK) {
vtkMath::Normalize(normalCell);
if(minAtP) {
vectPtN[0] = xP[0] - SEARCHLENGTH * normalCell[0];
vectPtN[1] = xP[1] - SEARCHLENGTH * normalCell[1];
vectPtN[2] = xP[2] - SEARCHLENGTH * normalCell[2];
intersectCell = bspTreeCell->IntersectWithLine(xP, vectPtN, TOL, tCell, x, pcoords, subId);
if(intersectCell != 0 && tCell < tP) {
dist = tCell * SEARCHLENGTH;
}
}
else{
vectPtP[0] = xN[0] + SEARCHLENGTH * normalCell[0];
vectPtP[1] = xN[1] + SEARCHLENGTH * normalCell[1];
vectPtP[2] = xN[2] + SEARCHLENGTH * normalCell[2];
intersectCell = bspTreeCell->IntersectWithLine(xN, vectPtP, TOL, tCell, x, pcoords, subId);
if(intersectCell != 0 && tCell < tN) {
dist = -tCell * SEARCHLENGTH;
}
}
}
}
else
{
vtkMath::Normalize(normal);
vectPtP[0] = refPt[0] + normal[0]/normalLength;
vectPtP[1] = refPt[1] + normal[1]/normalLength;
vectPtP[2] = refPt[2] + normal[2]/normalLength;
cellLoc->FindClosestPoint(refPt,x,a,subId,dist);
if(vtkMath::Dot(vectPtP,x) >= 0)
dist = sqrt(dist);
else
dist = -sqrt(dist);
}
// store the min and max values
if(dist > max)
max = dist;
else if (dist < min)
min = dist;
distArray->SetValue(ii,dist); //set the distance to the array
//distVect->append(dist);
}
// qDebug() << *distVect;
// qDebug() << "(min, max) = " << min << max;
// qDebug() << "T's = " << tN << tP;
// qDebug() << "Inter = " << intersectN << intersectP;
polyA->GetPointData()->AddArray(distArray); //add the array to the polydata
polyA->GetPointData()->SetActiveScalars(name); //set the active scalars of the polydata to the distance array
polyA->Update();
// create lookup table
vtkSmartPointer<vtkLookupTable> lookupTable =
vtkSmartPointer<vtkLookupTable>::New();
lookupTable->SetRange(min,max);
lookupTable->SetNumberOfColors(1000);
lookupTable->Build();
// create scalarbar
vtkSmartPointer<vtkScalarBarActor> scalarBar =
vtkSmartPointer<vtkScalarBarActor>::New();
scalarBar->SetLookupTable(lookupTable);
scalarBar->SetMaximumNumberOfColors(1000);
scalarBar->SetTitle("Mesh Distance");
// Create normal glyph
vtkSmartPointer<vtkGlyph3D> glyph =
vtkSmartPointer<vtkGlyph3D>::New();
glyph->SetInput(normals->GetOutput());
glyph->SetVectorModeToUseNormal();
glyph->OrientOn();
// Set to mappers
vtkSmartPointer<vtkPolyDataMapper> mapperA =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapperA->SetInput(polyA);
mapperA->SetScalarRange(min,max);
mapperA->SetLookupTable(lookupTable);
vtkSmartPointer<vtkPolyDataMapper> mapperB =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapperB->SetInput(polyB);
vtkSmartPointer<vtkPolyDataMapper> normalMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
normalMapper->SetInput(glyph->GetOutput());
// Set to Actors
vtkSmartPointer<vtkActor> actorA =
vtkSmartPointer<vtkActor>::New();
actorA->SetMapper(mapperA);
//actorA->GetProperty()->LightingOff();;
vtkSmartPointer<vtkActor> actorB =
vtkSmartPointer<vtkActor>::New();
actorB->SetMapper(mapperB);
actorB->GetProperty()->SetOpacity(.3);
vtkSmartPointer<vtkActor> normalActor =
vtkSmartPointer<vtkActor>::New();
normalActor->SetMapper(normalMapper);
normalActor->GetProperty()->SetOpacity(.8);
normalActor->GetProperty()->SetColor(0.,.2,0.);
// Set renderer
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actorA);
if(DISPLAYNORMALS)
renderer->AddActor(normalActor);
//renderer->AddActor(actorB);
renderer->AddActor2D(scalarBar);
// Set render window
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(renderer);
// set render window interactor
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
interactor->SetRenderWindow(renWin);
interactor->SetInteractorStyle(style);
interactor->Initialize();
interactor->Start();
// return a.exec();
}