VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataSurfaceRemeshing.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkPolyDataSurfaceRemeshing.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
25#include "vtkPolyDataNormals.h"
26#include "vtkIdList.h"
27#include "vtkIntArray.h"
28#include "vtkCellArray.h"
29#include "vtkMeshQuality.h"
30#include "vtkCellLocator.h"
31#include "vtkTriangle.h"
32#include "vtkPointData.h"
33#include "vtkCellData.h"
34#include "vtkInformation.h"
35#include "vtkInformationVector.h"
36#include "vtkObjectFactory.h"
37#include "vtkVersion.h"
38
39#include <iostream>
40
42
44{
45 this->AspectRatioThreshold = 1.2;
46 this->InternalAngleTolerance = 0.0;
47 this->NormalAngleTolerance = 0.2;
48 this->CollapseAngleThreshold = 0.5;
49 this->Relaxation = 0.5;
50 this->TargetArea = 1.0;
51 this->TargetAreaFactor = 1.0;
52 this->TriangleSplitFactor = 1.0;
53 this->MinAreaFactor = 0.5;
55 this->MinArea = 0.0;
56 this->TargetAreaArrayName = NULL;
57 this->TargetAreaArray = NULL;
59 this->NumberOfIterations = 10;
61 this->PreserveBoundaryEdges = 0;
62 this->CellEntityIdsArrayName = NULL;
63 this->CellEntityIdsArray = NULL;
64
65 this->Mesh = NULL;
66 this->InputBoundary = NULL;
67 this->InputEntityBoundary = NULL;
68 this->Locator = NULL;
69 this->BoundaryLocator = NULL;
70 this->EntityBoundaryLocator = NULL;
71
72 this->ExcludedEntityIds = NULL;
73}
74
76{
77 if (this->Mesh)
78 {
79 this->Mesh->Delete();
80 this->Mesh = NULL;
81 }
82
83 if (this->InputBoundary)
84 {
85 this->InputBoundary->Delete();
86 this->InputBoundary = NULL;
87 }
88
89 if (this->InputEntityBoundary)
90 {
91 this->InputEntityBoundary->Delete();
92 this->InputEntityBoundary = NULL;
93 }
94
95 if (this->Locator)
96 {
97 this->Locator->Delete();
98 this->Locator = NULL;
99 }
100
101 if (this->BoundaryLocator)
102 {
103 this->BoundaryLocator->Delete();
104 this->BoundaryLocator = NULL;
105 }
106
107 if (this->EntityBoundaryLocator)
108 {
109 this->EntityBoundaryLocator->Delete();
110 this->EntityBoundaryLocator = NULL;
111 }
112
113 if (this->TargetAreaArrayName)
114 {
115 delete[] this->TargetAreaArrayName;
116 this->TargetAreaArrayName = NULL;
117 }
118
119 if (this->CellEntityIdsArrayName)
120 {
121 delete[] this->CellEntityIdsArrayName;
122 this->CellEntityIdsArrayName = NULL;
123 }
124
125 if (this->CellEntityIdsArray)
126 {
127 this->CellEntityIdsArray->Delete();
128 this->CellEntityIdsArray = NULL;
129 }
130
131 if (this->ExcludedEntityIds)
132 {
133 this->ExcludedEntityIds->Delete();
134 this->ExcludedEntityIds = NULL;
135 }
136}
137
139 vtkInformation *vtkNotUsed(request),
140 vtkInformationVector **inputVector,
141 vtkInformationVector *outputVector)
142{
143 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
144 vtkInformation *outInfo = outputVector->GetInformationObject(0);
145
146 vtkPolyData *input = vtkPolyData::SafeDownCast(
147 inInfo->Get(vtkDataObject::DATA_OBJECT()));
148 vtkPolyData *output = vtkPolyData::SafeDownCast(
149 outInfo->Get(vtkDataObject::DATA_OBJECT()));
150
152 {
153 if (!this->TargetAreaArrayName)
154 {
155 vtkErrorMacro(<<"TargetAreaArrayName not specified");
156 return 1;
157 }
158 this->TargetAreaArray = input->GetPointData()->GetArray(this->TargetAreaArrayName);
159 if (!this->TargetAreaArray)
160 {
161 vtkErrorMacro(<<"TargetAreaArray with name specified does not exist");
162 return 1;
163 }
164 }
165
166 if (this->CellEntityIdsArray)
167 {
168 this->CellEntityIdsArray->Delete();
169 this->CellEntityIdsArray = NULL;
170 }
171
172 this->CellEntityIdsArray = vtkIntArray::New();
173 if (this->CellEntityIdsArrayName)
174 {
175 vtkDataArray* cellEntityIdsArray = input->GetCellData()->GetArray(this->CellEntityIdsArrayName);
176 if (!cellEntityIdsArray)
177 {
178 vtkErrorMacro(<<"CellEntityIdsArray with name specified does not exist");
179 return 1;
180 }
181 this->CellEntityIdsArray->DeepCopy(cellEntityIdsArray);
182 }
183 else
184 {
185 this->CellEntityIdsArray->SetNumberOfValues(input->GetNumberOfCells());
186 this->CellEntityIdsArray->FillComponent(0,0.0);
187 }
188
189 if (this->Mesh)
190 {
191 this->Mesh->Delete();
192 this->Mesh = NULL;
193 }
194
195 vtkPolyDataNormals* normals = vtkPolyDataNormals::New();
196 normals->SetInputData(input);
197 normals->ComputePointNormalsOff();
198 normals->ComputeCellNormalsOn();
199 normals->AutoOrientNormalsOn();
200 normals->ConsistencyOn();
201 normals->SplittingOff();
202 normals->Update();
203
204 vtkDataArray* cellNormals = normals->GetOutput()->GetCellData()->GetNormals();
205
206 this->Mesh = vtkPolyData::New();
207 vtkPoints* workingPoints = vtkPoints::New();
208 vtkCellArray* workingCells = vtkCellArray::New();
209 workingPoints->DeepCopy(input->GetPoints());
210
211 double point1[3], point2[3], point3[3];
212 double cellNormal[3], orientedCellNormal[3];
213 for (int i=0; i<input->GetNumberOfCells(); i++)
214 {
215 if (input->GetCellType(i) != VTK_TRIANGLE)
216 {
217 continue;
218 }
219 vtkCell* cell = input->GetCell(i);
220 cell->GetPoints()->GetPoint(0,point1);
221 cell->GetPoints()->GetPoint(1,point2);
222 cell->GetPoints()->GetPoint(2,point3);
223 vtkTriangle::ComputeNormal(point1,point2,point3,cellNormal);
224 cellNormals->GetTuple(i,orientedCellNormal);
225 workingCells->InsertNextCell(3);
226 if (vtkMath::Dot(cellNormal,orientedCellNormal) > 0.0)
227 {
228 workingCells->InsertCellPoint(cell->GetPointId(0));
229 workingCells->InsertCellPoint(cell->GetPointId(1));
230 workingCells->InsertCellPoint(cell->GetPointId(2));
231 }
232 else
233 {
234 workingCells->InsertCellPoint(cell->GetPointId(1));
235 workingCells->InsertCellPoint(cell->GetPointId(0));
236 workingCells->InsertCellPoint(cell->GetPointId(2));
237 }
238 }
239
240 this->Mesh->SetPoints(workingPoints);
241 this->Mesh->SetPolys(workingCells);
242
243 workingPoints->Delete();
244 workingCells->Delete();
245 normals->Delete();
246
247 this->Mesh->BuildCells();
248#if VTK_MAJOR_VERSION > 9 || (VTK_MAJOR_VERSION == 9 && VTK_MINOR_VERSION >= 4)
249 // Since VTK 9.4, vtkPolyData::BuildLinks() builds read-only
250 // vtkStaticCellLinks by default, but this filter edits the mesh topology
251 // in place through RemoveReferenceToCell/AddReferenceToCell/ResizeCellList,
252 // which require the editable vtkCellLinks and crash on static links.
253 // Marking the mesh editable makes BuildLinks() build the editable variant.
254 this->Mesh->SetEditable(true);
255#endif
256 this->Mesh->BuildLinks();
257
258 if (this->Locator)
259 {
260 this->Locator->Delete();
261 this->Locator = NULL;
262 }
263
264 this->Locator = vtkCellLocator::New();
265 this->Locator->SetDataSet(input);
266 this->Locator->SetNumberOfCellsPerBucket(5);
267 this->Locator->CacheCellBoundsOn();
268 this->Locator->BuildLocator();
269
270 if (this->InputBoundary)
271 {
272 this->InputBoundary->Delete();
273 this->InputBoundary = NULL;
274 }
275
277 boundaryExtractor->SetInputData(input);
278 boundaryExtractor->Update();
279
280 this->InputBoundary = vtkPolyData::New();
281 this->InputBoundary->DeepCopy(boundaryExtractor->GetOutput());
282
283 boundaryExtractor->Delete();
284
285 if (this->BoundaryLocator)
286 {
287 this->BoundaryLocator->Delete();
288 this->BoundaryLocator = NULL;
289 }
290
291 if (this->InputBoundary->GetNumberOfCells() > 0)
292 {
293 this->BoundaryLocator = vtkCellLocator::New();
294 this->BoundaryLocator->SetDataSet(this->InputBoundary);
295 this->BoundaryLocator->BuildLocator();
296 }
297
298 if (this->InputEntityBoundary)
299 {
300 this->InputEntityBoundary->Delete();
301 this->InputEntityBoundary = NULL;
302 }
303
304 this->InputEntityBoundary = vtkPolyData::New();
306
307 if (this->EntityBoundaryLocator)
308 {
309 this->EntityBoundaryLocator->Delete();
310 this->EntityBoundaryLocator = NULL;
311 }
312
313 if (this->InputEntityBoundary->GetNumberOfCells() > 0)
314 {
315 this->EntityBoundaryLocator = vtkCellLocator::New();
316 this->EntityBoundaryLocator->SetDataSet(this->InputEntityBoundary);
317 this->EntityBoundaryLocator->BuildLocator();
318 }
319
320 int relocationSuccess = RELOCATE_SUCCESS;
321 for (int n=0; n<this->NumberOfIterations; n++)
322 {
323 std::cout<<"Iteration "<<n+1<<"/"<<this->NumberOfIterations<<endl;
324 this->EdgeCollapseIteration();
325 this->EdgeFlipIteration();
326 this->EdgeSplitIteration();
327 this->EdgeFlipIteration();
328 relocationSuccess = this->PointRelocationIteration();
329 if (relocationSuccess == RELOCATE_FAILURE)
330 {
331 break;
332 }
333 this->EdgeFlipIteration();
334
335 for (int i=0; i<this->NumberOfConnectivityOptimizationIterations; i++)
336 {
338 }
339 }
340
341 std::cout<<"Final mesh improvement"<<endl;
342 bool projectToSurface = false;
343 for (int i=0; i<this->NumberOfIterations; i++)
344 {
345 if (i == this->NumberOfIterations/2)
346 {
347 projectToSurface = true;
348 }
349 relocationSuccess = this->PointRelocationIteration(projectToSurface);
350 if (relocationSuccess == RELOCATE_FAILURE)
351 {
352 break;
353 }
354 }
355
356 if (relocationSuccess == RELOCATE_FAILURE)
357 {
358 return 1;
359 }
360
362
363 vtkPoints* newPoints = vtkPoints::New();
364 vtkCellArray* newCells = vtkCellArray::New();
365 vtkIntArray* newCellEntityIdsArray = vtkIntArray::New();
366 newCellEntityIdsArray->SetName(this->CellEntityIdsArrayName);
367
368 newPoints->DeepCopy(this->Mesh->GetPoints());
369
370 int numberOfFinalCells = this->Mesh->GetNumberOfCells();
371 for (int i=0; i<numberOfFinalCells; i++)
372 {
373 if (this->Mesh->GetCellType(i) == VTK_EMPTY_CELL)
374 {
375 continue;
376 }
377 if (this->GetNumberOfBoundaryEdges(i) > 1)
378 {
379 continue;
380 }
381 newCells->InsertNextCell(this->Mesh->GetCell(i));
382 newCellEntityIdsArray->InsertNextValue(this->CellEntityIdsArray->GetValue(i));
383 }
384
385 output->SetPoints(newPoints);
386 output->SetPolys(newCells);
387 output->GetCellData()->AddArray(newCellEntityIdsArray);
388
389 newPoints->Delete();
390 newCells->Delete();
391 newCellEntityIdsArray->Delete();
392
393 return 1;
394}
395
396void vtkvmtkPolyDataSurfaceRemeshing::BuildEntityBoundary(vtkPolyData* input, vtkPolyData* entityBoundary)
397{
398 vtkPoints* entityBoundaryPoints = vtkPoints::New();
399 vtkCellArray* entityBoundaryCells = vtkCellArray::New();
400 vtkIdList* cellIds = vtkIdList::New();
401 vtkIdType numberOfCells = input->GetNumberOfCells();
402 vtkIdType npts;
403 const vtkIdType *pts;
404 for (int i=0; i<numberOfCells; i++)
405 {
406 if (input->GetCellType(i) != VTK_TRIANGLE)
407 {
408 continue;
409 }
410 input->GetCellPoints(i,npts,pts);
411 vtkIdType cellEntityId = this->CellEntityIdsArray->GetValue(i);
412 for (int j=0; j<npts; j++)
413 {
414 cellIds->Initialize();
415 vtkIdType pt1 = pts[j];
416 vtkIdType pt2 = pts[(j+1)%npts];
417 input->GetCellEdgeNeighbors(i,pt1,pt2,cellIds);
418 bool entityBoundaryEdge = false;
419 for (int k=0; k<cellIds->GetNumberOfIds(); k++)
420 {
421 if (this->CellEntityIdsArray->GetValue(cellIds->GetId(k)) != cellEntityId)
422 {
423 entityBoundaryEdge = true;
424 break;
425 }
426 }
427 if (entityBoundaryEdge)
428 {
429 vtkIdType pt1Id = entityBoundaryPoints->InsertNextPoint(input->GetPoint(pt1));
430 vtkIdType pt2Id = entityBoundaryPoints->InsertNextPoint(input->GetPoint(pt2));
431 entityBoundaryCells->InsertNextCell(2);
432 entityBoundaryCells->InsertCellPoint(pt1Id);
433 entityBoundaryCells->InsertCellPoint(pt2Id);
434 }
435 }
436 }
437
438 entityBoundary->SetPoints(entityBoundaryPoints);
439 entityBoundary->SetLines(entityBoundaryCells);
440
441 cellIds->Delete();
442 entityBoundaryPoints->Delete();
443 entityBoundaryCells->Delete();
444}
445
447{
448 int numberOfBoundaryEdges = 0;
449 vtkIdList* cellEdgeNeighbors = vtkIdList::New();
450 vtkIdType npts;
451 const vtkIdType *pts;
452 this->Mesh->GetCellPoints(cellId,npts,pts);
453 for (int j=0; j<npts; j++)
454 {
455 if (this->Mesh->IsEdge(pts[j],pts[(j+1)%npts]))
456 {
457 this->Mesh->GetCellEdgeNeighbors(cellId,pts[j],pts[(j+1)%npts],cellEdgeNeighbors);
458 if (cellEdgeNeighbors->GetNumberOfIds() == 0)
459 {
460 numberOfBoundaryEdges++;
461 }
462 }
463 }
464 cellEdgeNeighbors->Delete();
465 return numberOfBoundaryEdges;
466}
467
469{
470 vtkIdType ncells;
471 vtkIdType* cells;
472 this->Mesh->GetPointCells(pointId,ncells,cells);
473 vtkIdList* cellEdgeNeighbors = vtkIdList::New();
474 for (vtkIdType i=0; i<ncells; i++)
475 {
476 vtkIdType npts;
477 const vtkIdType *pts;
478 this->Mesh->GetCellPoints(cells[i],npts,pts);
479 for (int j=0; j<npts; j++)
480 {
481 if (pts[j] == pointId)
482 {
483 continue;
484 }
485 if (this->Mesh->IsEdge(pts[j],pointId))
486 {
487 this->Mesh->GetCellEdgeNeighbors(cells[i],pts[j],pointId,cellEdgeNeighbors);
488 if (cellEdgeNeighbors->GetNumberOfIds() == 0)
489 {
490 cellEdgeNeighbors->Delete();
491 return 1;
492 }
493 }
494 }
495 }
496 cellEdgeNeighbors->Delete();
497 return 0;
498}
499
501{
502 //TODO: randomize.
503 int numberOfChanges = 0;
504 int numberOfCells = this->Mesh->GetNumberOfCells();
505 for (int i=0; i<numberOfCells; i++)
506 {
507 if (this->Mesh->GetCellType(i) != VTK_TRIANGLE)
508 {
509 continue;
510 }
511 vtkIdType npts;
512 const vtkIdType *pts;
513 this->Mesh->GetCellPoints(i,npts,pts);
514 vtkIdType tripts[3];
515 tripts[0] = pts[0];
516 tripts[1] = pts[1];
517 tripts[2] = pts[2];
518 for (int j=0; j<3; j++)
519 {
520 int test = this->TestConnectivityFlipEdge(tripts[j],tripts[(j+1)%3]);
521 if (test == DO_NOTHING)
522 {
523 continue;
524 }
525 this->FlipEdge(tripts[j],tripts[(j+1)%3]);
526 numberOfChanges++;
527 break;
528 }
529 }
530 return numberOfChanges;
531}
532
534{
535 int numberOfChanges = 0;
536 int numberOfCells = this->Mesh->GetNumberOfCells();
537 for (int i=0; i<numberOfCells; i++)
538 {
539 if (this->Mesh->GetCellType(i) != VTK_TRIANGLE)
540 {
541 continue;
542 }
543 vtkIdType npts;
544 const vtkIdType *pts;
545 this->Mesh->GetCellPoints(i,npts,pts);
546 vtkIdType tripts[3];
547 tripts[0] = pts[0];
548 tripts[1] = pts[1];
549 tripts[2] = pts[2];
550 for (int j=0; j<3; j++)
551 {
552 int test = this->TestDelaunayFlipEdge(tripts[j],tripts[(j+1)%3]);
553 if (test == DO_NOTHING)
554 {
555 continue;
556 }
557 this->FlipEdge(tripts[j],tripts[(j+1)%3]);
558 numberOfChanges++;
559 break;
560 }
561 }
562 return numberOfChanges;
563}
564
566{
567 int numberOfChanges = 0;
568 int numberOfCells = this->Mesh->GetNumberOfCells();
569 for (int i=0; i<numberOfCells; i++)
570 {
571 if (this->Mesh->GetCellType(i) != VTK_TRIANGLE)
572 {
573 continue;
574 }
575 vtkIdType pt1, pt2;
576 int test = this->TestAspectRatioCollapseEdge(i,pt1,pt2);
577 if (test == DO_NOTHING)
578 {
579 continue;
580 }
581 this->CollapseEdge(pt1,pt2);
582 numberOfChanges++;
583 }
584 return numberOfChanges;
585}
586
588{
589 int numberOfChanges = 0;
590 int numberOfCells = this->Mesh->GetNumberOfCells();
591 for (int i=0; i<numberOfCells; i++)
592 {
593 if (this->Mesh->GetCellType(i) != VTK_TRIANGLE)
594 {
595 continue;
596 }
597 vtkIdType pt1, pt2;
598 int test = this->TestAreaSplitEdge(i,pt1,pt2);
599 if (test == DO_NOTHING)
600 {
601 continue;
602 }
603 this->SplitEdge(pt1,pt2);
604 numberOfChanges++;
605 }
606 return numberOfChanges;
607}
608
610{
611 int numberOfChanges = 0;
612 int numberOfCells = this->Mesh->GetNumberOfCells();
613 for (int i=0; i<numberOfCells; i++)
614 {
615 if (this->Mesh->GetCellType(i) != VTK_TRIANGLE)
616 {
617 continue;
618 }
619 int test = this->TestTriangleSplit(i);
620 if (test == DO_NOTHING)
621 {
622 continue;
623 }
624 this->SplitTriangle(i);
625 numberOfChanges++;
626 }
627 return numberOfChanges;
628}
629
631{
632 int numberOfPoints = this->Mesh->GetNumberOfPoints();
633 int success = RELOCATE_SUCCESS;
634 for (int i=0; i<numberOfPoints; i++)
635 {
636 success = this->RelocatePoint(i,projectToSurface);
637 if (success == RELOCATE_FAILURE)
638 {
639 return RELOCATE_FAILURE;
640 }
641 }
642 return RELOCATE_SUCCESS;
643}
644
646{
647 int isElementExcluded = 0;
648 if (this->ExcludedEntityIds == NULL)
649 {
650 return isElementExcluded;
651 }
652
653 vtkIdType cellEntityId = this->CellEntityIdsArray->GetValue(cellId);
654 if (this->ExcludedEntityIds->IsId(cellEntityId) != -1)
655 {
656 isElementExcluded = 1;
657 }
658 return isElementExcluded;
659}
660
662{
663 vtkIdList* ptCells = vtkIdList::New();
664 this->Mesh->GetPointCells(pointId,ptCells);
665 vtkIdType nptCells = ptCells->GetNumberOfIds();
666 vtkIdType neighborhoodCellEntityId = -1;
667 bool uniformCellEntityIds = true;
668 for (int i=0; i<nptCells; i++)
669 {
670 vtkIdType cellEntityId = this->CellEntityIdsArray->GetValue(ptCells->GetId(i));
671 if (cellEntityId == -1)
672 {
673 continue;
674 }
675 if (neighborhoodCellEntityId == -1)
676 {
677 neighborhoodCellEntityId = cellEntityId;
678 continue;
679 }
680 if (neighborhoodCellEntityId == cellEntityId)
681 {
682 continue;
683 }
684 uniformCellEntityIds = false;
685 break;
686 }
687
688 ptCells->Delete();
689
690 if (!uniformCellEntityIds)
691 {
692 return 1;
693 }
694
695 return 0;
696}
697
698int vtkvmtkPolyDataSurfaceRemeshing::FindOneRingNeighbors(vtkIdType pointId, vtkIdList* neighborIds)
699{
700 int pointLocation = INTERNAL_POINT;
701
702 vtkIdList* cellIds = vtkIdList::New();
703 this->Mesh->GetPointCells(pointId,cellIds);
704
705 vtkIdType numberOfCells = cellIds->GetNumberOfIds();
706 if (numberOfCells < 1)
707 {
708 cellIds->Delete();
709 return NO_NEIGHBORS;
710 }
711
712 vtkIdList* ptIds = vtkIdList::New();
713 this->Mesh->GetCellPoints(cellIds->GetId(0),ptIds);
714
715 vtkIdType bp1, bp2;
716 vtkIdType i, j;
717
718 vtkIdType p1, p2;
719 p2 = ptIds->GetId(0);
720 for (i=1; i<3; i++)
721 {
722 p2 = ptIds->GetId(i);
723 if (pointId != p2)
724 {
725 break;
726 }
727 }
728 neighborIds->InsertNextId(p2);
729
730 this->Mesh->GetCellEdgeNeighbors(-1,pointId,p2,cellIds);
731
732 vtkIdType startCell;
733 vtkIdType nextCell = cellIds->GetId(0);
734 if (cellIds->GetNumberOfIds() == 1)
735 {
736 startCell = -1;
737 }
738 else
739 {
740 startCell = cellIds->GetId(1);
741 }
742
743 bp2 = -1;
744 bp1 = p2;
745
746 // walk around the neighborhood counter-clockwise and get cells
747 vtkCell* cell = NULL;
748 for (j=0; j<numberOfCells; j++)
749 {
750 cell = this->Mesh->GetCell(nextCell);
751 p1 = -1;
752 for (i=0; i<3; i++)
753 {
754 p1 = cell->GetPointId(i);
755 if (p1 != pointId && p1 != p2)
756 {
757 break;
758 }
759 }
760
761 p2 = p1;
762 neighborIds->InsertNextId(p2);
763 this->Mesh->GetCellEdgeNeighbors(nextCell,pointId,p2,cellIds);
764 if (cellIds->GetNumberOfIds() != 1)
765 {
766 bp2 = p2;
767 j++;
768 break;
769 }
770 nextCell = cellIds->GetId(0);
771 }
772
773 // now walk around the other way. this will only happen if there
774 // is a boundary cell left that we have not visited
775 nextCell = startCell;
776 p2 = bp1;
777 for (; j<numberOfCells && startCell!=-1; j++)
778 {
779 cell = this->Mesh->GetCell(nextCell);
780 p1 = -1;
781 for (i=0; i<3; i++)
782 {
783 p1 = cell->GetPointId(i);
784 if (p1 != pointId && p1 != p2)
785 {
786 break;
787 }
788 }
789 p2 = p1;
790 neighborIds->InsertNextId(p2);
791 this->Mesh->GetCellEdgeNeighbors(nextCell,pointId,p2,cellIds);
792 if (cellIds->GetNumberOfIds() != 1)
793 {
794 bp1 = p2;
795 break;
796 }
797 nextCell = cellIds->GetId(0);
798 }
799
800 if (bp2 != -1) // boundary edge
801 {
802 pointLocation = POINT_ON_BOUNDARY;
803 vtkIdType id = neighborIds->IsId(bp2);
804 for (i=0; i<=id/2; i++)
805 {
806 p1 = neighborIds->GetId(i);
807 p2 = neighborIds->GetId(id-i);
808 neighborIds->SetId(i,p2);
809 neighborIds->SetId(id-i,p1);
810 }
811 }
812 else
813 {
814 // Remove last id. It's a duplicate of the first
815 neighborIds->SetNumberOfIds(neighborIds->GetNumberOfIds()-1);
816 }
817
818 cellIds->Delete();
819 ptIds->Delete();
820
821 return pointLocation;
822}
823#define USE_STENCIL
824int vtkvmtkPolyDataSurfaceRemeshing::RelocatePoint(vtkIdType pointId, bool projectToSurface)
825{
826 vtkIdList* cellIds = vtkIdList::New();
827 this->Mesh->GetPointCells(pointId,cellIds);
828 int pointOnExcludedEntity = 0;
829 for (int i=0; i<cellIds->GetNumberOfIds(); i++)
830 {
831 if (this->IsElementExcluded(cellIds->GetId(i)))
832 {
833 pointOnExcludedEntity = 1;
834 }
835 }
836 cellIds->Delete();
837
838 if (pointOnExcludedEntity)
839 {
840 return RELOCATE_SUCCESS;
841 }
842
843#ifndef USE_STENCIL
844 vtkIdList* neighborIds = vtkIdList::New();
845 int pointLocation = this->FindOneRingNeighbors(pointId,neighborIds);
846#else
848 stencil->SetDataSet(this->Mesh);
849 stencil->SetDataSetPointId(pointId);
850 stencil->NegateWeightsOff();
851 stencil->Build();
852#endif
853
854#ifndef USE_STENCIL
855 if (!pointLocation == INTERNAL_POINT)
856#else
857 if (!stencil->GetIsBoundary())
858#endif
859 {
860 double targetPoint[3];
861 targetPoint[0] = targetPoint[1] = targetPoint[2] = 0.0;
862#ifndef USE_STENCIL
863 int numberOfNeighbors = neighborIds->GetNumberOfIds();
864 double stencilWeight = 1.0 / numberOfNeighbors;
865 for (int i=0; i<numberOfNeighbors; i++)
866 {
867 double stencilPoint[3];
868 this->Mesh->GetPoint(neighborIds->GetId(i),stencilPoint);
869 targetPoint[0] += stencilWeight * stencilPoint[0];
870 targetPoint[1] += stencilWeight * stencilPoint[1];
871 targetPoint[2] += stencilWeight * stencilPoint[2];
872 }
873#else
874 for (int i=0; i<stencil->GetNumberOfPoints(); i++)
875 {
876 double stencilPoint[3];
877 this->Mesh->GetPoint((stencil->GetPointId(i)),stencilPoint);
878 double stencilWeight = stencil->GetWeight(i);
879 targetPoint[0] += stencilWeight * stencilPoint[0];
880 targetPoint[1] += stencilWeight * stencilPoint[1];
881 targetPoint[2] += stencilWeight * stencilPoint[2];
882 }
883#endif
884 double point[3];
885 this->Mesh->GetPoint(pointId,point);
886
887 double relocatedPoint[3];
888 relocatedPoint[0] = point[0] + this->Relaxation * (targetPoint[0] - point[0]);
889 relocatedPoint[1] = point[1] + this->Relaxation * (targetPoint[1] - point[1]);
890 relocatedPoint[2] = point[2] + this->Relaxation * (targetPoint[2] - point[2]);
891
892 double projectedRelocatedPoint[3];
893 vtkIdType cellId;
894 int subId;
895 double dist2;
896 if (this->IsPointOnEntityBoundary(pointId))
897 {
898 this->EntityBoundaryLocator->FindClosestPoint(relocatedPoint,projectedRelocatedPoint,cellId,subId,dist2);
899 }
900 else
901 {
902 if (projectToSurface)
903 {
904 this->Locator->FindClosestPoint(relocatedPoint,projectedRelocatedPoint,cellId,subId,dist2);
905 }
906 else
907 {
908 projectedRelocatedPoint[0] = relocatedPoint[0];
909 projectedRelocatedPoint[1] = relocatedPoint[1];
910 projectedRelocatedPoint[2] = relocatedPoint[2];
911 }
912 }
913 this->Mesh->GetPoints()->SetPoint(pointId,projectedRelocatedPoint);
914 }
915 else
916 {
917 double targetPoint[3];
918 targetPoint[0] = targetPoint[1] = targetPoint[2] = 0.0;
919
920 double stencilPoint[3];
921#ifndef USE_STENCIL
922 this->Mesh->GetPoint(neighborIds->GetId(0),stencilPoint);
923#else
924 this->Mesh->GetPoint((stencil->GetPointId(0)),stencilPoint);
925#endif
926 double stencilWeight = 0.5;
927 targetPoint[0] += stencilWeight * stencilPoint[0];
928 targetPoint[1] += stencilWeight * stencilPoint[1];
929 targetPoint[2] += stencilWeight * stencilPoint[2];
930#ifndef USE_STENCIL
931 this->Mesh->GetPoint(neighborIds->GetId(neighborIds->GetNumberOfIds()-1),stencilPoint);
932#else
933 this->Mesh->GetPoint((stencil->GetPointId(stencil->GetNumberOfPoints()-1)),stencilPoint);
934#endif
935 targetPoint[0] += stencilWeight * stencilPoint[0];
936 targetPoint[1] += stencilWeight * stencilPoint[1];
937 targetPoint[2] += stencilWeight * stencilPoint[2];
938
939 double point[3];
940 this->Mesh->GetPoint(pointId,point);
941
942 double relocatedPoint[3];
943 relocatedPoint[0] = point[0] + this->Relaxation * (targetPoint[0] - point[0]);
944 relocatedPoint[1] = point[1] + this->Relaxation * (targetPoint[1] - point[1]);
945 relocatedPoint[2] = point[2] + this->Relaxation * (targetPoint[2] - point[2]);
946
947 double projectedRelocatedPoint[3];
948 vtkIdType cellId;
949 int subId;
950 double dist2;
951 if (this->BoundaryLocator)
952 {
953 this->BoundaryLocator->FindClosestPoint(relocatedPoint,projectedRelocatedPoint,cellId,subId,dist2);
954 }
955 else
956 {
957// vtkErrorMacro(<<"Something's wrong: point on boundary but no BoundaryLocator is allocated.");
958 projectedRelocatedPoint[0] = relocatedPoint[0];
959 projectedRelocatedPoint[1] = relocatedPoint[1];
960 projectedRelocatedPoint[2] = relocatedPoint[2];
961// return RELOCATE_FAILURE;
962 }
963
964 this->Mesh->GetPoints()->SetPoint(pointId,projectedRelocatedPoint);
965 }
966#ifndef USE_STENCIL
967 neighborIds->Delete();
968#else
969 stencil->Delete();
970#endif
971 return RELOCATE_SUCCESS;
972}
973
974int vtkvmtkPolyDataSurfaceRemeshing::GetEdgeCellsAndOppositeEdge(vtkIdType pt1, vtkIdType pt2, vtkIdType& cell1, vtkIdType& cell2, vtkIdType& pt3, vtkIdType& pt4)
975{
976 cell1 = cell2 = -1;
977 pt3 = pt4 = -1;
978
979 if (!this->Mesh->IsEdge(pt1,pt2))
980 {
981 return NOT_EDGE;
982 }
983
984 vtkIdList* cellIds = vtkIdList::New();
985 this->Mesh->GetCellEdgeNeighbors(-1,pt1,pt2,cellIds);
986
987 int numberOfCellEdgeNeighbors = cellIds->GetNumberOfIds();
988 int numberOfNeighborTriangles = 0;
989
990 for (int i=0; i<numberOfCellEdgeNeighbors; i++)
991 {
992 vtkIdType cellId = cellIds->GetId(i);
993 if (this->Mesh->GetCellType(cellId) == VTK_TRIANGLE)
994 {
995 if (numberOfNeighborTriangles==0)
996 {
997 cell1 = cellId;
998 }
999 else if (numberOfNeighborTriangles==1)
1000 {
1001 cell2 = cellId;
1002 }
1003 numberOfNeighborTriangles++;
1004 }
1005 }
1006
1007 cellIds->Delete();
1008
1009 if (numberOfNeighborTriangles == 0)
1010 {
1011 return NOT_TRIANGLES;
1012 }
1013 else if (numberOfNeighborTriangles == 1)
1014 {
1015 vtkIdType npts;
1016 const vtkIdType *pts;
1017 this->Mesh->GetCellPoints(cell1,npts,pts);
1018 for (int i=0; i<3; i++)
1019 {
1020 if ((pts[i]==pt1 && pts[(i+1)%3]==pt2) || (pts[i]==pt2 && pts[(i+1)%3]==pt1))
1021 {
1022 pt3 = pts[(i+2)%3];
1023 break;
1024 }
1025 }
1026 return EDGE_ON_BOUNDARY;
1027 }
1028 else if (numberOfNeighborTriangles > 2)
1029 {
1030 return NON_MANIFOLD;
1031 }
1032
1033 vtkIdType pt3tmp = -1;
1034 vtkIdType pt4tmp = -1;
1035 bool reverse = false;
1036 vtkIdType npts;
1037 const vtkIdType *pts;
1038 this->Mesh->GetCellPoints(cell1,npts,pts);
1039 for (int i=0; i<3; i++)
1040 {
1041 if ((pts[i]==pt1 && pts[(i+1)%3]==pt2) || (pts[i]==pt2 && pts[(i+1)%3]==pt1))
1042 {
1043 pt3tmp = pts[(i+2)%3];
1044 if (pts[i]==pt2 && pts[(i+1)%3]==pt1)
1045 {
1046 reverse = true;
1047 }
1048 break;
1049 }
1050 }
1051
1052 this->Mesh->GetCellPoints(cell2,npts,pts);
1053 for (int i=0; i<3; i++)
1054 {
1055 if ((pts[i]==pt1 && pts[(i+1)%3]==pt2) || (pts[i]==pt2 && pts[(i+1)%3]==pt1))
1056 {
1057 pt4tmp = pts[(i+2)%3];
1058 break;
1059 }
1060 }
1061
1062 if (pt3tmp==-1 || pt4tmp==-1)
1063 {
1064 return DEGENERATE_TRIANGLES;
1065 }
1066
1067 pt3 = pt3tmp;
1068 pt4 = pt4tmp;
1069
1070 if (reverse)
1071 {
1072 vtkIdType tmp;
1073 tmp = pt3;
1074 pt3 = pt4;
1075 pt4 = tmp;
1076 tmp = cell1;
1077 cell1 = cell2;
1078 cell2 = tmp;
1079 }
1080
1081 if (this->CellEntityIdsArray->GetValue(cell1) != this->CellEntityIdsArray->GetValue(cell2))
1082 {
1083 return EDGE_BETWEEN_ENTITIES;
1084 }
1085
1086 return SUCCESS;
1087}
1088
1090{
1091 if (this->Mesh->GetCellType(cellId) != VTK_TRIANGLE)
1092 {
1093 return NOT_TRIANGLES;
1094 }
1095
1096 if (this->IsElementExcluded(cellId))
1097 {
1098 return TRIANGLE_LOCKED;
1099 }
1100
1101 vtkIdType npts;
1102 const vtkIdType *pts;
1103 this->Mesh->GetCellPoints(cellId,npts,pts);
1104 vtkIdType pt1 = pts[0];
1105 vtkIdType pt2 = pts[1];
1106 vtkIdType pt3 = pts[2];
1107
1108 vtkIdType cellEntityId = this->CellEntityIdsArray->GetValue(cellId);
1109
1110 double point1[3], point2[3], point3[3], newPoint[3];
1111 this->Mesh->GetPoint(pt1,point1);
1112 this->Mesh->GetPoint(pt2,point2);
1113 this->Mesh->GetPoint(pt3,point3);
1114 vtkTriangle::TriangleCenter(point1,point2,point3,newPoint);
1115
1116 double projectedNewPoint[3];
1117 vtkIdType closestCellId;
1118 int subId;
1119 double dist2;
1120 this->Locator->FindClosestPoint(newPoint,projectedNewPoint,closestCellId,subId,dist2);
1121
1122 vtkIdType newpt = this->Mesh->InsertNextLinkedPoint(projectedNewPoint,1);
1123
1124 this->Mesh->ReplaceCellPoint(cellId,pt3,newpt);
1125 this->Mesh->RemoveReferenceToCell(pt3,cellId);
1126 this->Mesh->AddReferenceToCell(newpt,cellId);
1127
1128 vtkIdType tri[3];
1129 tri[0] = pt2;
1130 tri[1] = pt3;
1131 tri[2] = newpt;
1132 vtkIdType cell1 = this->Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri);
1133 this->CellEntityIdsArray->InsertValue(cell1,cellEntityId);
1134
1135 tri[0] = pt3;
1136 tri[1] = pt1;
1137 tri[2] = newpt;
1138 vtkIdType cell2 = this->Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri);
1139 this->CellEntityIdsArray->InsertValue(cell2,cellEntityId);
1140
1141 //std::cout<<cell1<<" "<<cell2<<" "<<numberOfCells<<endl;
1142
1143 return SUCCESS;
1144}
1145
1147{
1148 vtkErrorMacro(<<"CollapseTriangle not yet implemented.");
1149 return -1;
1150}
1151
1152int vtkvmtkPolyDataSurfaceRemeshing::SplitEdge(vtkIdType pt1, vtkIdType pt2)
1153{
1154 vtkIdType cell1, cell2, pt3, pt4;
1155 int success = vtkvmtkPolyDataSurfaceRemeshing::GetEdgeCellsAndOppositeEdge(pt1,pt2,cell1,cell2,pt3,pt4);
1156
1157 if (this->IsElementExcluded(cell1) || this->IsElementExcluded(cell2))
1158 {
1159 return EDGE_LOCKED;
1160 }
1161
1162 bool proceed = false;
1163
1164 if (success == SUCCESS)
1165 {
1166 proceed = true;
1167 }
1168
1169 if (!this->PreserveBoundaryEdges && success == EDGE_ON_BOUNDARY)
1170 {
1171 proceed = true;
1172 }
1173
1174 if (success == EDGE_BETWEEN_ENTITIES)
1175 {
1176 proceed = true;
1177 }
1178
1179 if (!proceed)
1180 {
1181 return success;
1182 }
1183
1184 double point1[3], point2[3];
1185 this->Mesh->GetPoint(pt1,point1);
1186 this->Mesh->GetPoint(pt2,point2);
1187
1188 double newPoint[3];
1189 newPoint[0] = 0.5 * (point1[0] + point2[0]);
1190 newPoint[1] = 0.5 * (point1[1] + point2[1]);
1191 newPoint[2] = 0.5 * (point1[2] + point2[2]);
1192
1193 if (success != EDGE_ON_BOUNDARY)
1194 {
1195 double projectedNewPoint[3];
1196 vtkIdType cellId;
1197 int subId;
1198 double dist2;
1199
1200 if (success == EDGE_BETWEEN_ENTITIES)
1201 {
1202 this->EntityBoundaryLocator->FindClosestPoint(newPoint,projectedNewPoint,cellId,subId,dist2);
1203 }
1204 else
1205 {
1206 this->Locator->FindClosestPoint(newPoint,projectedNewPoint,cellId,subId,dist2);
1207 }
1208 vtkIdType newpt = this->Mesh->InsertNextLinkedPoint(projectedNewPoint,2);
1209
1210 this->Mesh->ReplaceCellPoint(cell1,pt2,newpt);
1211 this->Mesh->ReplaceCellPoint(cell2,pt2,newpt);
1212
1213 this->Mesh->RemoveReferenceToCell(pt2,cell1);
1214 this->Mesh->RemoveReferenceToCell(pt2,cell2);
1215
1216 this->Mesh->AddReferenceToCell(newpt,cell1);
1217 this->Mesh->AddReferenceToCell(newpt,cell2);
1218
1219 vtkIdType tri[3];
1220 tri[0] = pt3;
1221 tri[1] = newpt;
1222 tri[2] = pt2;
1223 vtkIdType cell3 = this->Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri);
1224 this->CellEntityIdsArray->InsertValue(cell3,this->CellEntityIdsArray->GetValue(cell1));
1225
1226 tri[0] = pt2;
1227 tri[1] = newpt;
1228 tri[2] = pt4;
1229 vtkIdType cell4 = this->Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri);
1230 this->CellEntityIdsArray->InsertValue(cell4,this->CellEntityIdsArray->GetValue(cell2));
1231 }
1232 else
1233 {
1234 double projectedNewPoint[3];
1235 vtkIdType cellId;
1236 int subId;
1237 double dist2;
1238
1239 if (this->BoundaryLocator)
1240 {
1241 this->BoundaryLocator->FindClosestPoint(newPoint,projectedNewPoint,cellId,subId,dist2);
1242 }
1243 else
1244 {
1245 vtkErrorMacro(<<"Something's wrong: point on boundary but no BoundaryLocator is allocated.");
1246 projectedNewPoint[0] = newPoint[0];
1247 projectedNewPoint[1] = newPoint[1];
1248 projectedNewPoint[2] = newPoint[2];
1249 }
1250 vtkIdType newpt = this->Mesh->InsertNextLinkedPoint(projectedNewPoint,2);
1251
1252 this->Mesh->ReplaceCellPoint(cell1,pt2,newpt);
1253 this->Mesh->RemoveReferenceToCell(pt2,cell1);
1254 this->Mesh->AddReferenceToCell(newpt,cell1);
1255
1256 vtkIdType tri[3];
1257 tri[0] = pt3;
1258 tri[1] = newpt;
1259 tri[2] = pt2;
1260 vtkIdType cell3 = this->Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri);
1261 this->CellEntityIdsArray->InsertValue(cell3,this->CellEntityIdsArray->GetValue(cell1));
1262 }
1263
1264 return SUCCESS;
1265}
1266
1267int vtkvmtkPolyDataSurfaceRemeshing::CollapseEdge(vtkIdType pt1, vtkIdType pt2)
1268{
1269 vtkIdType cell1, cell2, pt3, pt4;
1270 int success = vtkvmtkPolyDataSurfaceRemeshing::GetEdgeCellsAndOppositeEdge(pt1,pt2,cell1,cell2,pt3,pt4);
1271
1272 if (this->IsElementExcluded(cell1) || this->IsElementExcluded(cell2))
1273 {
1274 return EDGE_LOCKED;
1275 }
1276
1277 bool proceed = false;
1278
1279 if (success == SUCCESS)
1280 {
1281 proceed = true;
1282 }
1283
1284 if (!this->PreserveBoundaryEdges && success == EDGE_ON_BOUNDARY)
1285 {
1286 proceed = true;
1287 }
1288
1289 if (success == EDGE_BETWEEN_ENTITIES)
1290 {
1291 proceed = true;
1292 }
1293
1294 if (!proceed)
1295 {
1296 return success;
1297 }
1298
1299 if (success != EDGE_ON_BOUNDARY)
1300 {
1301 bool swappedForBoundary = false;
1302 int pt1OnBoundary = this->IsPointOnBoundary(pt1);
1303 int pt2OnBoundary = this->IsPointOnBoundary(pt2);
1304 if (!pt1OnBoundary && pt2OnBoundary)
1305 {
1306 vtkIdType tmp = pt1;
1307 pt1 = pt2;
1308 pt2 = tmp;
1309 swappedForBoundary = true;
1310 }
1311
1312 int pt1OnEntityBoundary = this->IsPointOnEntityBoundary(pt1);
1313 int pt2OnEntityBoundary = this->IsPointOnEntityBoundary(pt2);
1314 if (!pt1OnEntityBoundary && pt2OnEntityBoundary)
1315 {
1316 if (swappedForBoundary)
1317 {
1318 return EDGE_LOCKED;
1319 }
1320 vtkIdType tmp = pt1;
1321 pt1 = pt2;
1322 pt2 = tmp;
1323 }
1324
1325 vtkIdList* pt2Cells = vtkIdList::New();
1326 this->Mesh->GetPointCells(pt2,pt2Cells);
1327 vtkIdType npt2Cells = pt2Cells->GetNumberOfIds();
1328
1329 this->Mesh->RemoveReferenceToCell(pt1,cell1);
1330 this->Mesh->RemoveReferenceToCell(pt1,cell2);
1331 this->Mesh->RemoveReferenceToCell(pt3,cell1);
1332 this->Mesh->RemoveReferenceToCell(pt4,cell2);
1333 this->Mesh->DeletePoint(pt2);
1334 this->Mesh->DeleteCell(cell1);
1335 this->CellEntityIdsArray->InsertValue(cell1,-1);
1336 this->Mesh->DeleteCell(cell2);
1337 this->CellEntityIdsArray->InsertValue(cell2,-1);
1338
1339 this->Mesh->ResizeCellList(pt1,npt2Cells-2);
1340
1341 for (int i=0; i<npt2Cells; i++)
1342 {
1343 vtkIdType pt2Cell = pt2Cells->GetId(i);
1344 if (pt2Cell == cell1 || pt2Cell == cell2)
1345 {
1346 continue;
1347 }
1348 this->Mesh->AddReferenceToCell(pt1,pt2Cell);
1349 this->Mesh->ReplaceCellPoint(pt2Cell,pt2,pt1);
1350 }
1351
1352 pt2Cells->Delete();
1353 }
1354 else
1355 {
1356 vtkIdList* pt2Cells = vtkIdList::New();
1357 this->Mesh->GetPointCells(pt2,pt2Cells);
1358 vtkIdType npt2Cells = pt2Cells->GetNumberOfIds();
1359
1360 this->Mesh->RemoveReferenceToCell(pt1,cell1);
1361 this->Mesh->RemoveReferenceToCell(pt3,cell1);
1362 this->Mesh->DeletePoint(pt2);
1363 this->Mesh->DeleteCell(cell1);
1364 this->CellEntityIdsArray->InsertValue(cell1,-1);
1365
1366 this->Mesh->ResizeCellList(pt1,npt2Cells-1);
1367
1368 for (int i=0; i<npt2Cells; i++)
1369 {
1370 vtkIdType pt2Cell = pt2Cells->GetId(i);
1371 if (pt2Cell == cell1)
1372 {
1373 continue;
1374 }
1375 this->Mesh->AddReferenceToCell(pt1,pt2Cell);
1376 this->Mesh->ReplaceCellPoint(pt2Cell,pt2,pt1);
1377 }
1378
1379 pt2Cells->Delete();
1380 }
1381
1382 return SUCCESS;
1383}
1384
1385int vtkvmtkPolyDataSurfaceRemeshing::FlipEdge(vtkIdType pt1, vtkIdType pt2)
1386{
1387 vtkIdType cell1, cell2, pt3, pt4;
1388 int success = vtkvmtkPolyDataSurfaceRemeshing::GetEdgeCellsAndOppositeEdge(pt1,pt2,cell1,cell2,pt3,pt4);
1389
1390 if (this->IsElementExcluded(cell1) || this->IsElementExcluded(cell2))
1391 {
1392 return EDGE_LOCKED;
1393 }
1394
1395 if (success != SUCCESS)
1396 {
1397 return success;
1398 }
1399
1400 this->Mesh->RemoveCellReference(cell1);
1401 this->Mesh->RemoveCellReference(cell2);
1402 this->Mesh->DeleteCell(cell1);
1403 vtkIdType cell1EntityId = this->CellEntityIdsArray->GetValue(cell1);
1404 this->CellEntityIdsArray->InsertValue(cell1,-1);
1405 this->Mesh->DeleteCell(cell2);
1406 vtkIdType cell2EntityId = this->CellEntityIdsArray->GetValue(cell2);
1407 this->CellEntityIdsArray->InsertValue(cell2,-1);
1408
1409 vtkIdType tri[3];
1410 tri[0] = pt3;
1411 tri[1] = pt1;
1412 tri[2] = pt4;
1413 vtkIdType cell1b = this->Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri);
1414 this->CellEntityIdsArray->InsertValue(cell1b,cell1EntityId);
1415 tri[0] = pt2;
1416 tri[1] = pt3;
1417 tri[2] = pt4;
1418 vtkIdType cell2b = this->Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri);
1419 this->CellEntityIdsArray->InsertValue(cell2b,cell2EntityId);
1420
1421 vtkIdType pt3b, pt4b;
1422 if (vtkvmtkPolyDataSurfaceRemeshing::GetEdgeCellsAndOppositeEdge(pt3,pt4,cell1,cell2,pt3b,pt4b) != SUCCESS)
1423 {
1424 this->Mesh->RemoveCellReference(cell1b);
1425 this->Mesh->RemoveCellReference(cell2b);
1426 this->Mesh->DeleteCell(cell1b);
1427 this->CellEntityIdsArray->InsertValue(cell1b,-1);
1428 this->Mesh->DeleteCell(cell2b);
1429 this->CellEntityIdsArray->InsertValue(cell2b,-1);
1430 tri[0] = pt1;
1431 tri[1] = pt2;
1432 tri[2] = pt3;
1433 vtkIdType cell1c = this->Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri);
1434 this->CellEntityIdsArray->InsertValue(cell1c,cell1EntityId);
1435 tri[0] = pt1;
1436 tri[1] = pt4;
1437 tri[2] = pt2;
1438 vtkIdType cell2c = this->Mesh->InsertNextLinkedCell(VTK_TRIANGLE,3,tri);
1439 this->CellEntityIdsArray->InsertValue(cell2c,cell2EntityId);
1440 }
1441
1442 return SUCCESS;
1443}
1444
1445int vtkvmtkPolyDataSurfaceRemeshing::TestFlipEdgeValidity(vtkIdType pt1, vtkIdType pt2, vtkIdType cell1, vtkIdType cell2, vtkIdType pt3, vtkIdType pt4)
1446{
1447 vtkIdType tri[4][3];
1448 tri[0][0] = pt1;
1449 tri[0][1] = pt2;
1450 tri[0][2] = pt3;
1451
1452 tri[1][0] = pt1;
1453 tri[1][1] = pt4;
1454 tri[1][2] = pt2;
1455
1456 tri[2][0] = pt3;
1457 tri[2][1] = pt1;
1458 tri[2][2] = pt4;
1459
1460 tri[3][0] = pt2;
1461 tri[3][1] = pt3;
1462 tri[3][2] = pt4;
1463
1464 double point1[3], point2[3], point3[3];
1465
1466 double normal1[3], normal2[3], normal3[3], normal4[3];
1467 this->Mesh->GetPoint(tri[0][0],point1);
1468 this->Mesh->GetPoint(tri[0][1],point2);
1469 this->Mesh->GetPoint(tri[0][2],point3);
1470 vtkTriangle::ComputeNormal(point1,point2,point3,normal1);
1471 this->Mesh->GetPoint(tri[1][0],point1);
1472 this->Mesh->GetPoint(tri[1][1],point2);
1473 this->Mesh->GetPoint(tri[1][2],point3);
1474 vtkTriangle::ComputeNormal(point1,point2,point3,normal2);
1475 this->Mesh->GetPoint(tri[2][0],point1);
1476 this->Mesh->GetPoint(tri[2][1],point2);
1477 this->Mesh->GetPoint(tri[2][2],point3);
1478 vtkTriangle::ComputeNormal(point1,point2,point3,normal3);
1479 this->Mesh->GetPoint(tri[3][0],point1);
1480 this->Mesh->GetPoint(tri[3][1],point2);
1481 this->Mesh->GetPoint(tri[3][2],point3);
1482 vtkTriangle::ComputeNormal(point1,point2,point3,normal4);
1483
1484 if (vtkMath::Dot(normal3,normal1)<0.0 || vtkMath::Dot(normal4,normal1)<0.0 || vtkMath::Dot(normal3,normal2)<0.0 || vtkMath::Dot(normal4,normal2)<0.0)
1485 {
1486 return DO_NOTHING;
1487 }
1488
1489 if (fabs(acos(vtkMath::Dot(normal3,normal4)) - acos(vtkMath::Dot(normal1,normal2))) > this->NormalAngleTolerance)
1490 {
1491 return DO_NOTHING;
1492 }
1493
1494 return DO_CHANGE;
1495}
1496
1498{
1499 vtkIdType cell1, cell2, pt3, pt4;
1500 int success = vtkvmtkPolyDataSurfaceRemeshing::GetEdgeCellsAndOppositeEdge(pt1,pt2,cell1,cell2,pt3,pt4);
1501
1502 if (success != SUCCESS)
1503 {
1504 return DO_NOTHING;
1505 }
1506
1507 if (this->TestFlipEdgeValidity(pt1,pt2,cell1,cell2,pt3,pt4) == DO_NOTHING)
1508 {
1509 return DO_NOTHING;
1510 }
1511
1512 vtkIdType ncells1, ncells2, ncells3, ncells4;
1513 vtkIdType* cells;
1514 this->Mesh->GetPointCells(pt1,ncells1,cells);
1515 this->Mesh->GetPointCells(pt2,ncells2,cells);
1516 this->Mesh->GetPointCells(pt3,ncells3,cells);
1517 this->Mesh->GetPointCells(pt4,ncells4,cells);
1518
1519 int targetValence = 6;
1520
1521 //TODO: if ncells1 < targetValence and ncells2 < targetValence, then the edge should be collapsed.
1522 //TODO: if ncells1 > targetValence and ncells2 > targetValence, then the edge should be split.
1523 int currentCost = (ncells1-targetValence)*(ncells1-targetValence) + (ncells2-targetValence)*(ncells2-targetValence) + (ncells3-targetValence)*(ncells3-targetValence) + (ncells4-targetValence)*(ncells4-targetValence);
1524 int flippedCost = (ncells1-1-targetValence)*(ncells1-1-targetValence) + (ncells2-1-targetValence)*(ncells2-1-targetValence) + (ncells3+1-targetValence)*(ncells3+1-targetValence) + (ncells4+1-targetValence)*(ncells4+1-targetValence);
1525
1526 if (flippedCost >= currentCost)
1527 {
1528 return DO_NOTHING;
1529 }
1530
1531 return DO_CHANGE;
1532}
1533
1535{
1536 vtkIdType cell1, cell2, pt3, pt4;
1537 int success = vtkvmtkPolyDataSurfaceRemeshing::GetEdgeCellsAndOppositeEdge(pt1,pt2,cell1,cell2,pt3,pt4);
1538
1539 if (success != SUCCESS)
1540 {
1541 return DO_NOTHING;
1542 }
1543
1544 if (this->TestFlipEdgeValidity(pt1,pt2,cell1,cell2,pt3,pt4) == DO_NOTHING)
1545 {
1546 return DO_NOTHING;
1547 }
1548
1549 vtkIdType tri[4][3];
1550 tri[0][0] = pt1;
1551 tri[0][1] = pt2;
1552 tri[0][2] = pt3;
1553
1554 tri[1][0] = pt1;
1555 tri[1][1] = pt4;
1556 tri[1][2] = pt2;
1557
1558 tri[2][0] = pt3;
1559 tri[2][1] = pt1;
1560 tri[2][2] = pt4;
1561
1562 tri[3][0] = pt2;
1563 tri[3][1] = pt3;
1564 tri[3][2] = pt4;
1565
1566 double point1[3], point2[3], point3[3];
1567 double direction1[3], direction2[3];
1568
1569 double maxAngle01 = 0.0;
1570 double maxAngle23 = 0.0;
1571
1572 for (int n=0; n<4; n++)
1573 {
1574 for (int i=0; i<3; i++)
1575 {
1576 this->Mesh->GetPoint(tri[n][i],point1);
1577 this->Mesh->GetPoint(tri[n][(i+1)%3],point2);
1578 this->Mesh->GetPoint(tri[n][(i+2)%3],point3);
1579 direction1[0] = point2[0] - point1[0];
1580 direction1[1] = point2[1] - point1[1];
1581 direction1[2] = point2[2] - point1[2];
1582 vtkMath::Normalize(direction1);
1583 direction2[0] = point3[0] - point1[0];
1584 direction2[1] = point3[1] - point1[1];
1585 direction2[2] = point3[2] - point1[2];
1586 vtkMath::Normalize(direction2);
1587 double angle = acos(vtkMath::Dot(direction1,direction2));
1588 if ((n==0 || n==1) && (angle > maxAngle01))
1589 {
1590 maxAngle01 = angle;
1591 }
1592 else if ((n==2 || n==3) && (angle > maxAngle23))
1593 {
1594 maxAngle23 = angle;
1595 }
1596 }
1597 }
1598
1599 if (maxAngle01 < maxAngle23 + this->InternalAngleTolerance)
1600 {
1601 return DO_NOTHING;
1602 }
1603
1604 return DO_CHANGE;
1605}
1606
1608{
1609 double targetArea = 0.0;
1610 if (this->ElementSizeMode == TARGET_AREA)
1611 {
1612 targetArea = this->TargetArea;
1613 }
1614 else if (this->ElementSizeMode == TARGET_AREA_ARRAY)
1615 {
1616 vtkIdType npts;
1617 const vtkIdType *pts;
1618 this->Mesh->GetCellPoints(cellId,npts,pts);
1619 double point1[3], point2[3], point3[3];
1620 this->Mesh->GetPoint(pts[0],point1);
1621 this->Mesh->GetPoint(pts[1],point2);
1622 this->Mesh->GetPoint(pts[2],point3);
1623 double center[3];
1624 vtkTriangle::TriangleCenter(point1,point2,point3,center);
1625 double projectedCenter[3];
1626 vtkIdType centerCellId;
1627 int subId;
1628 double dist2;
1629 this->Locator->FindClosestPoint(center,projectedCenter,centerCellId,subId,dist2);
1630 vtkTriangle* centerCell = vtkTriangle::SafeDownCast(vtkPolyData::SafeDownCast(this->Locator->GetDataSet())->GetCell(centerCellId));
1631 double pcoords[3], weights[3];
1632 centerCell->EvaluatePosition(projectedCenter,NULL,subId,pcoords,dist2,weights);
1633 for (int i=0; i<3; i++)
1634 {
1635 targetArea += weights[i] * this->TargetAreaArray->GetTuple1(centerCell->GetPointId(i));
1636 }
1637 targetArea *= this->TargetAreaFactor;
1638 }
1639 else
1640 {
1641 vtkErrorMacro(<<"ElementSizeMode specified is unknown");
1642 return 0.0;
1643 }
1644 if (targetArea > this->MaxArea)
1645 {
1646 targetArea = this->MaxArea;
1647 }
1648 if (targetArea < this->MinArea)
1649 {
1650 targetArea = this->MinArea;
1651 }
1652 return targetArea;
1653}
1654
1656{
1657 vtkIdType npts;
1658 const vtkIdType *pts;
1659 this->Mesh->GetCellPoints(cellId,npts,pts);
1660
1661 vtkIdType tri[3];
1662 tri[0] = pts[0];
1663 tri[1] = pts[1];
1664 tri[2] = pts[2];
1665
1666 double point1[3], point2[3], point3[3];
1667 this->Mesh->GetPoint(tri[0],point1);
1668 this->Mesh->GetPoint(tri[1],point2);
1669 this->Mesh->GetPoint(tri[2],point3);
1670
1671 double area = vtkTriangle::TriangleArea(point1,point2,point3);
1672
1673 double targetArea = this->ComputeTriangleTargetArea(cellId);
1674
1675 // TODO: make this a parameter
1676 if (area < this->TriangleSplitFactor * targetArea)
1677 {
1678 return DO_NOTHING;
1679 }
1680
1681 //std::cout<<cellId<<" "<<area<<" "<<targetArea<<endl;
1682 return DO_CHANGE;
1683}
1684
1685int vtkvmtkPolyDataSurfaceRemeshing::TestAspectRatioCollapseEdge(vtkIdType cellId, vtkIdType& pt1, vtkIdType& pt2)
1686{
1687 pt1 = -1;
1688 pt2 = -1;
1689
1690 vtkIdType npts;
1691 const vtkIdType *pts;
1692 this->Mesh->GetCellPoints(cellId,npts,pts);
1693
1694 vtkIdType tri[3];
1695 tri[0] = pts[0];
1696 tri[1] = pts[1];
1697 tri[2] = pts[2];
1698
1699 double point1[3], point2[3], point3[3];
1700 this->Mesh->GetPoint(tri[0],point1);
1701 this->Mesh->GetPoint(tri[1],point2);
1702 this->Mesh->GetPoint(tri[2],point3);
1703
1704 double area = vtkTriangle::TriangleArea(point1,point2,point3);
1705
1706 double targetArea = this->ComputeTriangleTargetArea(cellId);
1707
1708 if (area > targetArea)
1709 {
1710 return DO_NOTHING;
1711 }
1712
1713 double side1Squared = vtkMath::Distance2BetweenPoints(point1,point2);
1714 double side2Squared = vtkMath::Distance2BetweenPoints(point2,point3);
1715 double side3Squared = vtkMath::Distance2BetweenPoints(point3,point1);
1716
1717 double frobeniusAspectRatio = (side1Squared + side2Squared + side3Squared) / (4.0 * sqrt(3.0) * area);
1718
1719 if (frobeniusAspectRatio < this->AspectRatioThreshold && area > targetArea * this->MinAreaFactor)
1720 {
1721 return DO_NOTHING;
1722 }
1723
1724 if (side1Squared < side2Squared && side1Squared < side3Squared)
1725 {
1726 pt1 = tri[0];
1727 pt2 = tri[1];
1728 }
1729 else if (side2Squared < side1Squared && side2Squared < side3Squared)
1730 {
1731 pt1 = tri[1];
1732 pt2 = tri[2];
1733 }
1734 else
1735 {
1736 pt1 = tri[2];
1737 pt2 = tri[0];
1738 }
1739
1740 vtkIdType cell1, cell2, pt3, pt4;
1741 int success = vtkvmtkPolyDataSurfaceRemeshing::GetEdgeCellsAndOppositeEdge(pt1,pt2,cell1,cell2,pt3,pt4);
1742
1743 if (pt3 == pt4)
1744 {
1745 return DO_NOTHING;
1746 }
1747
1748 bool proceed = false;
1749
1750 if (success == SUCCESS)
1751 {
1752 proceed = true;
1753 }
1754
1755 if (!this->PreserveBoundaryEdges && success == EDGE_ON_BOUNDARY)
1756 {
1757 proceed = true;
1758 }
1759
1760 if (success == EDGE_BETWEEN_ENTITIES)
1761 {
1762 proceed = true;
1763 }
1764
1765 if (!proceed)
1766 {
1767 return DO_NOTHING;
1768 }
1769
1770 vtkIdType ncells3, ncells4;
1771 ncells3 = ncells4 = 0;
1772 vtkIdType *cells;
1773 this->Mesh->GetPointCells(pt3,ncells3,cells);
1774 if (success != EDGE_ON_BOUNDARY)
1775 {
1776 this->Mesh->GetPointCells(pt4,ncells4,cells);
1777 }
1778 if (ncells3==3 || ncells4==3)
1779 {
1780 return DO_NOTHING;
1781 }
1782
1783 vtkIdType ncells2;
1784 this->Mesh->GetPointCells(pt2,ncells2,cells);
1785 for (int i=0; i<ncells2; i++)
1786 {
1787 if (cells[i] == cell1 || cells[i] == cell2)
1788 {
1789 continue;
1790 }
1791 this->Mesh->GetCellPoints(cells[i],npts,pts);
1792
1793 double normal1[3], normal2[3];
1794 this->Mesh->GetPoint(pts[0],point1);
1795 this->Mesh->GetPoint(pts[1],point2);
1796 this->Mesh->GetPoint(pts[2],point3);
1797 vtkTriangle::ComputeNormal(point1,point2,point3,normal1);
1798
1799 for (int j=0; j<3; j++)
1800 {
1801 if (pts[j] != pt2)
1802 {
1803 tri[j] = pts[j];
1804 }
1805 else
1806 {
1807 tri[j] = pt1;
1808 }
1809 }
1810 this->Mesh->GetPoint(tri[0],point1);
1811 this->Mesh->GetPoint(tri[1],point2);
1812 this->Mesh->GetPoint(tri[2],point3);
1813 vtkTriangle::ComputeNormal(point1,point2,point3,normal2);
1814
1815 if (vtkMath::Dot(normal1,normal2) < 0.0)
1816 {
1817 return DO_NOTHING;
1818 }
1819
1820 if (acos(vtkMath::Dot(normal1,normal2)) > this->CollapseAngleThreshold)
1821 {
1822 return DO_NOTHING;
1823 }
1824 }
1825
1826 return DO_CHANGE;
1827}
1828
1829int vtkvmtkPolyDataSurfaceRemeshing::TestAreaSplitEdge(vtkIdType cellId, vtkIdType& pt1, vtkIdType& pt2)
1830{
1831 pt1 = -1;
1832 pt2 = -1;
1833
1834 vtkIdType npts;
1835 const vtkIdType *pts;
1836 this->Mesh->GetCellPoints(cellId,npts,pts);
1837
1838 vtkIdType tri[3];
1839 tri[0] = pts[0];
1840 tri[1] = pts[1];
1841 tri[2] = pts[2];
1842
1843 double point1[3], point2[3], point3[3];
1844 this->Mesh->GetPoint(tri[0],point1);
1845 this->Mesh->GetPoint(tri[1],point2);
1846 this->Mesh->GetPoint(tri[2],point3);
1847
1848 double area = vtkTriangle::TriangleArea(point1,point2,point3);
1849
1850 double side1Squared = vtkMath::Distance2BetweenPoints(point1,point2);
1851 double side2Squared = vtkMath::Distance2BetweenPoints(point2,point3);
1852 double side3Squared = vtkMath::Distance2BetweenPoints(point3,point1);
1853
1854 double targetArea = this->ComputeTriangleTargetArea(cellId);
1855
1856 if (area < targetArea)
1857 {
1858 return DO_NOTHING;
1859 }
1860
1861 if (side1Squared >= side2Squared && side1Squared >= side3Squared)
1862 {
1863 pt1 = tri[0];
1864 pt2 = tri[1];
1865 }
1866 else if (side2Squared >= side1Squared && side2Squared >= side3Squared)
1867 {
1868 pt1 = tri[1];
1869 pt2 = tri[2];
1870 }
1871 else if (side3Squared >= side1Squared && side3Squared >= side1Squared)
1872 {
1873 pt1 = tri[2];
1874 pt2 = tri[0];
1875 }
1876
1877 vtkIdType cell1, cell2, pt3, pt4;
1878 int success = vtkvmtkPolyDataSurfaceRemeshing::GetEdgeCellsAndOppositeEdge(pt1,pt2,cell1,cell2,pt3,pt4);
1879
1880 bool proceed = false;
1881
1882 if (success == SUCCESS)
1883 {
1884 proceed = true;
1885 }
1886
1887 if (!this->PreserveBoundaryEdges && success == EDGE_ON_BOUNDARY)
1888 {
1889 proceed = true;
1890 }
1891
1892 if (success == EDGE_BETWEEN_ENTITIES)
1893 {
1894 proceed = true;
1895 }
1896
1897 if (!proceed)
1898 {
1899 return DO_NOTHING;
1900 }
1901
1902 return DO_CHANGE;
1903}
1904
1905void vtkvmtkPolyDataSurfaceRemeshing::PrintSelf(std::ostream& os, vtkIndent indent)
1906{
1907 this->Superclass::PrintSelf(os,indent);
1908}
1909
virtual void SetDataSetPointId(vtkIdType)
void SetDataSet(vtkDataSet *dataSet)
vtkIdType GetPointId(vtkIdType i)
virtual bool GetIsBoundary()
Extract boundary edges as poly lines.
static vtkvmtkPolyDataBoundaryExtractor * New()
Remesh a triangulated surface into a mesh of more uniform, better-shaped ("quality") triangles.
int TestFlipEdgeValidity(vtkIdType pt1, vtkIdType pt2, vtkIdType cell1, vtkIdType cell2, vtkIdType pt3, vtkIdType pt4)
int RelocatePoint(vtkIdType pointId, bool projectToSurface)
void PrintSelf(std::ostream &os, vtkIndent indent) override
int TestDelaunayFlipEdge(vtkIdType pt1, vtkIdType pt2)
int TestConnectivityFlipEdge(vtkIdType pt1, vtkIdType pt2)
int FindOneRingNeighbors(vtkIdType pointId, vtkIdList *neighborIds)
int PointRelocationIteration(bool projectToSurface=true)
int TestAspectRatioCollapseEdge(vtkIdType cellId, vtkIdType &pt1, vtkIdType &pt2)
int GetEdgeCellsAndOppositeEdge(vtkIdType pt1, vtkIdType pt2, vtkIdType &cell1, vtkIdType &cell2, vtkIdType &pt3, vtkIdType &pt4)
int TestAreaSplitEdge(vtkIdType cellId, vtkIdType &pt1, vtkIdType &pt2)
int CollapseEdge(vtkIdType pt1, vtkIdType pt2)
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
void BuildEntityBoundary(vtkPolyData *input, vtkPolyData *entityBoundary)
Implements a uniform (unweighted) Laplacian stencil for the one-ring neighborhood of a surface mesh....
static vtkvmtkPolyDataUmbrellaStencil * New()
virtual void NegateWeightsOff()
double GetWeight(vtkIdType i)
#define VTK_VMTK_LARGE_FLOAT
vtkStandardNewMacro(vtkvmtkPolyDataSurfaceRemeshing)