VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataDiscreteElasticaFilter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkPolyDataDiscreteElasticaFilter.cxx,v $
5 Language: C++
6 Date: $Date: 2006/04/06 16:46:44 $
7 Version: $Revision: 1.5 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
24#include "vtkPolyDataNormals.h"
25#include "vtkPointData.h"
26#include "vtkDoubleArray.h"
27#include "vtkvmtkMath.h"
28#include "vtkInformation.h"
29#include "vtkInformationVector.h"
30#include "vtkObjectFactory.h"
31#include "vtkVersion.h"
32
33#include <iostream>
34
36
40
44#define CURVATURES
45#ifdef CURVATURES
46#include "vtkCurvatures.h"
47#if 0
49 vtkInformation *vtkNotUsed(request),
50 vtkInformationVector **inputVector,
51 vtkInformationVector *outputVector)
52{
53 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
54 vtkInformation *outInfo = outputVector->GetInformationObject(0);
55
56 vtkPolyData *input = vtkPolyData::SafeDownCast(
57 inInfo->Get(vtkDataObject::DATA_OBJECT()));
58 vtkPolyData *output = vtkPolyData::SafeDownCast(
59 outInfo->Get(vtkDataObject::DATA_OBJECT()));
60
61 vtkPolyData* surface = vtkPolyData::New();
62 surface->DeepCopy(input);
63
64 vtkPolyDataNormals* normalsFilter = vtkPolyDataNormals::New();
65 normalsFilter->SetInput(surface);
66 normalsFilter->FlipNormalsOff();
67 normalsFilter->ConsistencyOn();
68 normalsFilter->ComputePointNormalsOn();
69 normalsFilter->ComputeCellNormalsOn();
70
71 vtkCurvatures* curvaturesFilter = vtkCurvatures::New();
72 curvaturesFilter->SetInput(surface);
73// curvaturesFilter->InvertMeanCurvatureOn();
74
76 neighborhood->SetDataSet(surface);
77
78 int numberOfPoints = surface->GetNumberOfPoints();
79
80 vtkPoints* newPoints = vtkPoints::New();
81 newPoints->SetNumberOfPoints(numberOfPoints);
82
83 vtkDoubleArray* meanCurvatureArray = vtkDoubleArray::New();
84 vtkDoubleArray* gaussianCurvatureArray = vtkDoubleArray::New();
85
86 double deltaT = 1.0;
87
88 int numberOfIterations = 1000;
89
90 for (int n=0; n<numberOfIterations; n++)
91 {
92 surface->Modified();
93 curvaturesFilter->SetCurvatureTypeToMean();
94 curvaturesFilter->Update();
95 meanCurvatureArray->DeepCopy(curvaturesFilter->GetOutput()->GetPointData()->GetScalars());
96 curvaturesFilter->SetCurvatureTypeToGaussian();
97 curvaturesFilter->Update();
98 gaussianCurvatureArray->DeepCopy(curvaturesFilter->GetOutput()->GetPointData()->GetScalars());
99 normalsFilter->Update();
100 vtkDataArray* normals = normalsFilter->GetOutput()->GetPointData()->GetNormals();
101
102 newPoints->DeepCopy(surface->GetPoints());
103
104 double minTriangleArea = 1E8;
105
106 for (int i=0; i<numberOfPoints; i++)
107 {
108 neighborhood->SetDataSetPointId(i);
109 neighborhood->Build();
110
111 if (neighborhood->GetIsBoundary())
112 {
113 continue;
114 }
115
116 double point[3];
117 surface->GetPoint(i,point);
118
119 if (surface->GetPointData()->GetArray("Clamp")->GetTuple1(i) > 0.5)
120 {
121 newPoints->SetPoint(i,point);
122 continue;
123 }
124
125 double meanCurvature = meanCurvatureArray->GetTuple1(i);
126 double gaussianCurvature = gaussianCurvatureArray->GetTuple1(i);
127
128 double laplaceBeltramiH = 0.0;
129
130 int numberOfNeighborhoodPoints = neighborhood->GetNumberOfPoints();
131 double weight = 1.0 / double(numberOfNeighborhoodPoints);
132 for (int j=0; j<numberOfNeighborhoodPoints; j++)
133 {
134 double neighborMeanCurvature = meanCurvatureArray->GetTuple1(neighborhood->GetPointId(j));
135 laplaceBeltramiH += weight * (neighborMeanCurvature - meanCurvature);
136 }
137
138 double point1[3], point2[3];
139 double area = 0.0;
140 double triangleArea;
141 for (int j=0; j<numberOfNeighborhoodPoints; j++)
142 {
143 surface->GetPoint(neighborhood->GetPointId(j),point1);
144 surface->GetPoint(neighborhood->GetPointId((j+1)%numberOfNeighborhoodPoints),point2);
145 triangleArea = vtkvmtkMath::TriangleArea(point,point1,point2);
146 area += triangleArea;
147 if (triangleArea < minTriangleArea)
148 {
149 minTriangleArea = triangleArea;
150 }
151 }
152
153 double normal[3];
154 normals->GetTuple(i,normal);
155
156 double newPoint[3];
157 newPoint[0] = point[0];
158 newPoint[1] = point[1];
159 newPoint[2] = point[2];
160
161 double f = laplaceBeltramiH - 2.0 * meanCurvature * (meanCurvature*meanCurvature - gaussianCurvature);
162
163 if (meanCurvature*meanCurvature - gaussianCurvature < 0.0)
164 {
165 f = laplaceBeltramiH;
166 }
167
168// f = laplaceBeltramiH;
169// std::cout<<laplaceBeltramiH<<" "<<meanCurvature<<" "<<gaussianCurvature<<" "<<f<<endl;
170// f = - meanCurvature;
171
172 deltaT = minTriangleArea*minTriangleArea / 1.0;
173// deltaT = minTriangleArea / 10.0;
174
175 newPoint[0] += deltaT * f * normal[0];
176 newPoint[1] += deltaT * f * normal[1];
177 newPoint[2] += deltaT * f * normal[2];
178
179 newPoints->SetPoint(i,newPoint);
180 }
181
182 std::cout<<deltaT<<endl;
183
184 surface->GetPoints()->DeepCopy(newPoints);
185 }
186
187 output->DeepCopy(surface);
188
189 curvaturesFilter->Delete();
190 normalsFilter->Delete();
191 neighborhood->Delete();
192 meanCurvatureArray->Delete();
193 gaussianCurvatureArray->Delete();
194 surface->Delete();
195 newPoints->Delete();
196
197 return 1;
198}
199#endif
201 vtkInformation *vtkNotUsed(request),
202 vtkInformationVector **inputVector,
203 vtkInformationVector *outputVector)
204{
205 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
206 vtkInformation *outInfo = outputVector->GetInformationObject(0);
207
208 vtkPolyData *input = vtkPolyData::SafeDownCast(
209 inInfo->Get(vtkDataObject::DATA_OBJECT()));
210 vtkPolyData *output = vtkPolyData::SafeDownCast(
211 outInfo->Get(vtkDataObject::DATA_OBJECT()));
212
213 vtkPolyData* surface = vtkPolyData::New();
214 surface->DeepCopy(input);
215
216 vtkPolyDataNormals* normalsFilter = vtkPolyDataNormals::New();
217 normalsFilter->SetInputData(surface);
218 normalsFilter->FlipNormalsOff();
219 normalsFilter->ConsistencyOn();
220 normalsFilter->ComputePointNormalsOn();
221 normalsFilter->ComputeCellNormalsOn();
222
223 vtkCurvatures* curvaturesFilter = vtkCurvatures::New();
224 curvaturesFilter->SetInputData(surface);
225// curvaturesFilter->InvertMeanCurvatureOn();
226
228 neighborhood->SetDataSet(surface);
229
230 int numberOfPoints = surface->GetNumberOfPoints();
231
232 vtkPoints* newPoints = vtkPoints::New();
233 newPoints->SetNumberOfPoints(numberOfPoints);
234
235 vtkDoubleArray* meanCurvatureArray = vtkDoubleArray::New();
236
237 double deltaT = 1.0;
238
239 int numberOfIterations = 100;
240
241 for (int n=0; n<numberOfIterations; n++)
242 {
243 surface->Modified();
244 curvaturesFilter->SetCurvatureTypeToMean();
245 curvaturesFilter->Update();
246 meanCurvatureArray->DeepCopy(curvaturesFilter->GetOutput()->GetPointData()->GetScalars());
247 normalsFilter->Update();
248 vtkDataArray* normals = normalsFilter->GetOutput()->GetPointData()->GetNormals();
249
250 newPoints->DeepCopy(surface->GetPoints());
251
252 double minTriangleArea = 1E8;
253
254 for (int i=0; i<numberOfPoints; i++)
255 {
256 neighborhood->SetDataSetPointId(i);
257 neighborhood->Build();
258
259 if (neighborhood->GetIsBoundary())
260 {
261 continue;
262 }
263
264 double point[3];
265 surface->GetPoint(i,point);
266
267 if (surface->GetPointData()->GetArray("Clamp")->GetTuple1(i) > 0.5)
268 {
269 newPoints->SetPoint(i,point);
270 continue;
271 }
272
273 double meanCurvature = meanCurvatureArray->GetTuple1(i);
274
275 double laplaceH = 0.0;
276
277 double neighborPoint[3];
278 double laplace[3];
279 laplace[0] = laplace[1] = laplace[2] = 0.0;
280 int numberOfNeighborhoodPoints = neighborhood->GetNumberOfPoints();
281 double weight = 1.0 / double(numberOfNeighborhoodPoints);
282 for (int j=0; j<numberOfNeighborhoodPoints; j++)
283 {
284 double neighborMeanCurvature = meanCurvatureArray->GetTuple1(neighborhood->GetPointId(j));
285 laplaceH += weight * (neighborMeanCurvature - meanCurvature);
286 surface->GetPoint(neighborhood->GetPointId(j),neighborPoint);
287 laplace[0] += weight * (neighborPoint[0] - point[0]);
288 laplace[1] += weight * (neighborPoint[1] - point[1]);
289 laplace[2] += weight * (neighborPoint[2] - point[2]);
290 }
291
292 double point1[3], point2[3];
293 double area = 0.0;
294 double triangleArea;
295 for (int j=0; j<numberOfNeighborhoodPoints; j++)
296 {
297 surface->GetPoint(neighborhood->GetPointId(j),point1);
298 surface->GetPoint(neighborhood->GetPointId((j+1)%numberOfNeighborhoodPoints),point2);
299 triangleArea = vtkvmtkMath::TriangleArea(point,point1,point2);
300 area += triangleArea;
301 if (triangleArea < minTriangleArea)
302 {
303 minTriangleArea = triangleArea;
304 }
305 }
306
307 double normal[3];
308 normals->GetTuple(i,normal);
309
310 double newPoint[3];
311 newPoint[0] = point[0];
312 newPoint[1] = point[1];
313 newPoint[2] = point[2];
314
315 deltaT = minTriangleArea / 10.0;
316
317// newPoint[0] += deltaT * laplaceH * normal[0];
318// newPoint[1] += deltaT * laplaceH * normal[1];
319// newPoint[2] += deltaT * laplaceH * normal[2];
320
321// deltaT = 1E0;
322 double alpha = 1.0;
323 double beta = 0.0;
324
325 newPoint[0] += deltaT * (alpha * laplaceH * normal[0] + beta * laplace[0]);
326 newPoint[1] += deltaT * (alpha * laplaceH * normal[1] + beta * laplace[1]);
327 newPoint[2] += deltaT * (alpha * laplaceH * normal[2] + beta * laplace[2]);
328
329 newPoints->SetPoint(i,newPoint);
330 }
331
332 std::cout<<deltaT<<endl;
333
334 surface->GetPoints()->DeepCopy(newPoints);
335 }
336
337 output->DeepCopy(surface);
338
339 curvaturesFilter->Delete();
340 normalsFilter->Delete();
341 neighborhood->Delete();
342 meanCurvatureArray->Delete();
343 surface->Delete();
344 newPoints->Delete();
345
346 return 1;
347}
348#else
350 vtkInformation *vtkNotUsed(request),
351 vtkInformationVector **inputVector,
352 vtkInformationVector *outputVector)
353{
354 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
355 vtkInformation *outInfo = outputVector->GetInformationObject(0);
356
357 vtkPolyData *input = vtkPolyData::SafeDownCast(
358 inInfo->Get(vtkDataObject::DATA_OBJECT()));
359 vtkPolyData *output = vtkPolyData::SafeDownCast(
360 outInfo->Get(vtkDataObject::DATA_OBJECT()));
361
362 vtkPolyData* surface = vtkPolyData::New();
363 surface->DeepCopy(input);
364
365 vtkPolyDataNormals* normalsFilter = vtkPolyDataNormals::New();
366 normalsFilter->SetInput(surface);
367 normalsFilter->FlipNormalsOff();
368 normalsFilter->ConsistencyOn();
369 normalsFilter->ComputePointNormalsOn();
370 normalsFilter->ComputeCellNormalsOn();
371
373 neighborhood->SetDataSet(surface);
374
375 // Explicit filtering (not Gauss-Seidel)
376 // For each iteration
377 // 1. Compute normals
378 // 2. Compute mean curvature (using laplace beltrami) and store it in an array
379 // 3. Compute F by computing laplace beltrami of mean curvature, mean curvature and Gauss curvature
380 // P_i^{k+1} = P_i^k + \tau^k * F_i^k * N_i^k
381 // F = - \Delta_B H - 2 H (H^2 - K)
382 // H = 0.5 * N * \Delta_B P
383 // K = 3 / A * (2\pi - \sum_i \phi_i)
384 // \tau_k = A_k^2 / 150
385 // A_k: minimal triangle area
386
387 int numberOfPoints = surface->GetNumberOfPoints();
388
389 vtkPoints* newPoints = vtkPoints::New();
390 newPoints->SetNumberOfPoints(numberOfPoints);
391
392 vtkDoubleArray* meanCurvatureArray = vtkDoubleArray::New();
393 meanCurvatureArray->SetNumberOfTuples(numberOfPoints);
394
395 double deltaT = 1.0;
396
397 int numberOfIterations = 300;
398
399 for (int n=0; n<numberOfIterations; n++)
400 {
401 surface->Modified();
402 normalsFilter->Update();
403 vtkDataArray* normals = normalsFilter->GetOutput()->GetPointData()->GetNormals();
404
405 newPoints->DeepCopy(surface->GetPoints());
406
407 double minTriangleArea = 1E8;
408
409 for (int i=0; i<numberOfPoints; i++)
410 {
411 neighborhood->SetDataSetPointId(i);
412 neighborhood->Build();
413
414 if (neighborhood->GetIsBoundary())
415 {
416 continue;
417 }
418
419 double normal[3];
420 normals->GetTuple(i,normal);
421
422 double point[3];
423 surface->GetPoint(i,point);
424
425 double laplaceBeltrami[3];
426 laplaceBeltrami[0] = 0.0;
427 laplaceBeltrami[1] = 0.0;
428 laplaceBeltrami[2] = 0.0;
429
430 double point0[3], point1[3], point2[3];
431 int numberOfNeighborhoodPoints = neighborhood->GetNumberOfPoints();
432 for (int j=0; j<numberOfNeighborhoodPoints; j++)
433 {
434 surface->GetPoint(neighborhood->GetPointId((j-1+numberOfNeighborhoodPoints)%numberOfNeighborhoodPoints),point0);
435 surface->GetPoint(neighborhood->GetPointId(j),point1);
436 surface->GetPoint(neighborhood->GetPointId((j+1)%numberOfNeighborhoodPoints),point2);
437 double weight = vtkvmtkMath::Cotangent(point,point0,point1) + vtkvmtkMath::Cotangent(point,point2,point1);
438 laplaceBeltrami[0] += weight * (point1[0] - point[0]);
439 laplaceBeltrami[1] += weight * (point1[1] - point[1]);
440 laplaceBeltrami[2] += weight * (point1[2] - point[2]);
441 }
442
443 double area = 0.0;
444 double triangleArea;
445 for (int j=0; j<numberOfNeighborhoodPoints; j++)
446 {
447 surface->GetPoint(neighborhood->GetPointId(j),point1);
448 surface->GetPoint(neighborhood->GetPointId((j+1)%numberOfNeighborhoodPoints),point2);
449 triangleArea = vtkvmtkMath::TriangleArea(point,point1,point2);
450 area += triangleArea;
451 if (triangleArea < minTriangleArea)
452 {
453 minTriangleArea = triangleArea;
454 }
455 }
456
457 laplaceBeltrami[0] *= 3.0 / area;
458 laplaceBeltrami[1] *= 3.0 / area;
459 laplaceBeltrami[2] *= 3.0 / area;
460
461 double meanCurvature = 0.5 * vtkMath::Dot(normal,laplaceBeltrami);
462
463 meanCurvatureArray->SetTuple1(i,meanCurvature);
464 }
465
466 for (int i=0; i<numberOfPoints; i++)
467 {
468 neighborhood->SetDataSetPointId(i);
469 neighborhood->Build();
470
471 if (neighborhood->GetIsBoundary())
472 {
473 continue;
474 }
475
476 double normal[3];
477 normals->GetTuple(i,normal);
478
479 double point[3];
480 surface->GetPoint(i,point);
481
482 double meanCurvature = meanCurvatureArray->GetTuple1(i);
483
484 double laplaceBeltramiH = 0.0;
485
486 double point0[3], point1[3], point2[3];
487 int numberOfNeighborhoodPoints = neighborhood->GetNumberOfPoints();
488 for (int j=0; j<numberOfNeighborhoodPoints; j++)
489 {
490 surface->GetPoint(neighborhood->GetPointId((j-1+numberOfNeighborhoodPoints)%numberOfNeighborhoodPoints),point0);
491 surface->GetPoint(neighborhood->GetPointId(j),point1);
492 surface->GetPoint(neighborhood->GetPointId((j+1)%numberOfNeighborhoodPoints),point2);
493 double weight = vtkvmtkMath::Cotangent(point,point0,point1) + vtkvmtkMath::Cotangent(point,point2,point1);
494 double neighborMeanCurvature = meanCurvatureArray->GetTuple1(neighborhood->GetPointId(j));
495 laplaceBeltramiH += weight * (neighborMeanCurvature - meanCurvature);
496 }
497
498 double area = 0.0;
499 for (int j=0; j<numberOfNeighborhoodPoints; j++)
500 {
501 surface->GetPoint(neighborhood->GetPointId(j),point1);
502 surface->GetPoint(neighborhood->GetPointId((j+1)%numberOfNeighborhoodPoints),point2);
503 area += vtkvmtkMath::TriangleArea(point,point1,point2);
504 }
505
506 laplaceBeltramiH *= 3.0 / area;
507
508 double newPoint[3];
509 newPoint[0] = point[0];
510 newPoint[1] = point[1];
511 newPoint[2] = point[2];
512
513 double normal1[3], normal2[3];
514 double angleSum = 0.0;
515 for (int j=0; j<numberOfNeighborhoodPoints; j++)
516 {
517 surface->GetPoint(neighborhood->GetPointId(j),point1);
518 surface->GetPoint(neighborhood->GetPointId((j+1)%numberOfNeighborhoodPoints),point2);
519 normal1[0] = point1[0] - point[0];
520 normal1[1] = point1[1] - point[1];
521 normal1[2] = point1[2] - point[2];
522 vtkMath::Normalize(normal1);
523 normal2[0] = point2[0] - point[0];
524 normal2[1] = point2[1] - point[1];
525 normal2[2] = point2[2] - point[2];
526 vtkMath::Normalize(normal2);
527 angleSum += vtkvmtkMath::AngleBetweenNormals(normal1,normal2);
528 }
529 double gaussianCurvature = 3.0 / area * (2.0 * vtkMath::Pi() - angleSum);
530
531 double f = - laplaceBeltramiH - 2.0 * meanCurvature * (meanCurvature*meanCurvature - gaussianCurvature);
532
533 if (meanCurvature*meanCurvature - gaussianCurvature < 0.0)
534 {
535 f = - laplaceBeltramiH;
536 }
537
538// f = - meanCurvature;
539// std::cout<<laplaceBeltramiH<<" "<<meanCurvature<<" "<<gaussianCurvature<<" "<<f<<endl;
540
541 deltaT = minTriangleArea*minTriangleArea / 10.0;
542
543 newPoint[0] += deltaT * f * normal[0];
544 newPoint[1] += deltaT * f * normal[1];
545 newPoint[2] += deltaT * f * normal[2];
546
547 newPoints->SetPoint(i,newPoint);
548 }
549
550 std::cout<<deltaT<<endl;
551
552 surface->GetPoints()->DeepCopy(newPoints);
553 }
554
555 output->DeepCopy(surface);
556
557 normalsFilter->Delete();
558 neighborhood->Delete();
559 meanCurvatureArray->Delete();
560 surface->Delete();
561 newPoints->Delete();
562
563 return 1;
564}
565#endif
566void vtkvmtkPolyDataDiscreteElasticaFilter::PrintSelf(std::ostream& os, vtkIndent indent)
567{
568 this->Superclass::PrintSelf(os,indent);
569}
570
virtual void SetDataSetPointId(vtkIdType)
void SetDataSet(vtkDataSet *dataSet)
static double AngleBetweenNormals(double normal0[3], double normal1[3])
static double Cotangent(double point0[3], double point1[3], double point2[3])
static double TriangleArea(double point0[3], double point1[3], double point2[3])
vtkIdType GetPointId(vtkIdType i)
virtual bool GetIsBoundary()
Not implemented; smooths a surface via an elastic-forces method.
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
Apply a neighborhood to the points of a manifold surface.
static vtkvmtkPolyDataManifoldNeighborhood * New()
vtkStandardNewMacro(vtkvmtkPolyDataDiscreteElasticaFilter)