Skip to content

Commit 9f9f271

Browse files
committed
fix bad triangulation
1 parent fc5a037 commit 9f9f271

File tree

5 files changed

+52
-35
lines changed

5 files changed

+52
-35
lines changed

Contact.cxx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,19 @@ void Contact::InterPolys (vtkIdType idA, vtkIdType idB) {
634634
bool hasReplB = replsB.count(idB) == 1;
635635

636636
if (!isPlanarA && !hasReplA && newPdA->GetCellType(idA) != VTK_TRIANGLE) {
637-
auto newIds = PreventEqualCaptPoints::TriangulateCell(newPdA, idA, {});
638-
replsA.emplace(idA, newIds);
639-
hasReplA = true;
637+
try {
638+
auto newIds = PreventEqualCaptPoints::TriangulateCell(newPdA, idA, {});
639+
replsA.emplace(idA, newIds);
640+
hasReplA = true;
641+
} catch (...) {}
640642
}
641643

642644
if (!isPlanarB && !hasReplB && newPdB->GetCellType(idB) != VTK_TRIANGLE) {
643-
auto newIds = PreventEqualCaptPoints::TriangulateCell(newPdB, idB, {});
644-
replsB.emplace(idB, newIds);
645-
hasReplB = true;
645+
try {
646+
auto newIds = PreventEqualCaptPoints::TriangulateCell(newPdB, idB, {});
647+
replsB.emplace(idB, newIds);
648+
hasReplB = true;
649+
} catch (...) {}
646650
}
647651

648652
if (hasReplA || hasReplB) {

Optimize.cxx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,22 @@ IdsType PreventEqualCaptPoints::TriangulateCell (vtkPolyData *pd, vtkIdType cell
450450
ids->InsertNextId(p.id);
451451
}
452452

453+
double pA[3], pB[3], pC[3];
454+
455+
for (i = 0; i < triangles->GetNumberOfIds(); i += 3) {
456+
pd->GetPoint(ids->GetId(triangles->GetId(i)), pA);
457+
pd->GetPoint(ids->GetId(triangles->GetId(i+1)), pB);
458+
pd->GetPoint(ids->GetId(triangles->GetId(i+2)), pC);
459+
460+
Poly p = { {pA[0], pA[1], pA[2]}, {pB[0], pB[1], pB[2]}, {pC[0], pC[1], pC[2]} };
461+
462+
double quality = GetTringleQuality(p);
463+
464+
if (quality < 0.001) {
465+
throw std::runtime_error("");
466+
}
467+
}
468+
453469
vtkIdType origId = origCellIds->GetValue(cellId);
454470

455471
auto triangle = vtkSmartPointer<vtkIdList>::New();

Utilities.cxx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ void FindPoints (vtkKdTreePointLocator *pl, const double *pt, vtkIdList *pts, do
7676

7777
#ifdef DEBUG
7878
void WriteVTK (const char *name, vtkPolyData *pd) {
79+
std::cout << "Writing " << name << std::endl;
80+
7981
vtkPolyDataWriter *w = vtkPolyDataWriter::New();
8082
w->SetInputData(pd);
8183
w->SetFileName(name);
@@ -347,3 +349,25 @@ vtkSmartPointer<vtkPolyData> CreatePolyData (const PolysType &polys) {
347349

348350
return pd;
349351
}
352+
353+
double GetTringleQuality (const Poly &poly) {
354+
double n[3];
355+
356+
double l = ComputeNormal(poly, n);
357+
358+
double d = 0;
359+
360+
Poly::const_iterator itrA, itrB;
361+
362+
for (itrA = poly.begin(); itrA != poly.end(); ++itrA) {
363+
itrB = itrA+1;
364+
365+
if (itrB == poly.end()) {
366+
itrB = poly.begin();
367+
}
368+
369+
d += std::sqrt(Point3d::GetDist(*itrA, *itrB));
370+
}
371+
372+
return 10.392304845413264*l/(d*d);
373+
}

Utilities.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,6 @@ void ProjOnLine (vtkPolyData *pd, const Pair &line, const Point3d &p, std::share
216216

217217
vtkSmartPointer<vtkPolyData> CreatePolyData (const PolysType &polys);
218218

219+
double GetTringleQuality (const Poly &poly);
220+
219221
#endif

vtkPolyDataBooleanFilter.cxx

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
119119
modPdB->EditableOn();
120120

121121
#ifdef DEBUG
122-
std::cout << "Exporting modPdA.vtk" << std::endl;
123122
WriteVTK("modPdA.vtk", modPdA);
124-
125-
std::cout << "Exporting modPdB.vtk" << std::endl;
126123
WriteVTK("modPdB.vtk", modPdB);
127124
#endif
128125

@@ -151,13 +148,8 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
151148
times.push_back(clock::now()-start);
152149

153150
#ifdef DEBUG
154-
std::cout << "Exporting modPdA_1.vtk" << std::endl;
155151
WriteVTK("modPdA_1.vtk", modPdA);
156-
157-
std::cout << "Exporting modPdB_1.vtk" << std::endl;
158152
WriteVTK("modPdB_1.vtk", modPdB);
159-
160-
std::cout << "Exporting contLines.vtk" << std::endl;
161153
WriteVTK("contLines.vtk", contLines);
162154
#endif
163155

@@ -238,10 +230,7 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
238230
times.push_back(clock::now()-start);
239231

240232
#ifdef DEBUG
241-
std::cout << "Exporting modPdA_2.vtk" << std::endl;
242233
WriteVTK("modPdA_2.vtk", modPdA);
243-
244-
std::cout << "Exporting modPdB_2.vtk" << std::endl;
245234
WriteVTK("modPdB_2.vtk", modPdB);
246235
#endif
247236

@@ -253,10 +242,7 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
253242
times.push_back(clock::now()-start);
254243

255244
#ifdef DEBUG
256-
std::cout << "Exporting modPdA_3.vtk" << std::endl;
257245
WriteVTK("modPdA_3.vtk", modPdA);
258-
259-
std::cout << "Exporting modPdB_3.vtk" << std::endl;
260246
WriteVTK("modPdB_3.vtk", modPdB);
261247
#endif
262248

@@ -268,10 +254,7 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
268254
times.push_back(clock::now()-start);
269255

270256
#ifdef DEBUG
271-
std::cout << "Exporting modPdA_4.vtk" << std::endl;
272257
WriteVTK("modPdA_4.vtk", modPdA);
273-
274-
std::cout << "Exporting modPdB_4.vtk" << std::endl;
275258
WriteVTK("modPdB_4.vtk", modPdB);
276259
#endif
277260

@@ -283,10 +266,7 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
283266
times.push_back(clock::now()-start);
284267

285268
#ifdef DEBUG
286-
std::cout << "Exporting modPdA_5.vtk" << std::endl;
287269
WriteVTK("modPdA_5.vtk", modPdA);
288-
289-
std::cout << "Exporting modPdB_5.vtk" << std::endl;
290270
WriteVTK("modPdB_5.vtk", modPdB);
291271
#endif
292272

@@ -298,10 +278,7 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
298278
times.push_back(clock::now()-start);
299279

300280
#ifdef DEBUG
301-
std::cout << "Exporting modPdA_6.vtk" << std::endl;
302281
WriteVTK("modPdA_6.vtk", modPdA);
303-
304-
std::cout << "Exporting modPdB_6.vtk" << std::endl;
305282
WriteVTK("modPdB_6.vtk", modPdB);
306283
#endif
307284

@@ -313,10 +290,7 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
313290
times.push_back(clock::now()-start);
314291

315292
#ifdef DEBUG
316-
std::cout << "Exporting modPdA_7.vtk" << std::endl;
317293
WriteVTK("modPdA_7.vtk", modPdA);
318-
319-
std::cout << "Exporting modPdB_7.vtk" << std::endl;
320294
WriteVTK("modPdB_7.vtk", modPdB);
321295
#endif
322296

@@ -2717,10 +2691,7 @@ bool vtkPolyDataBooleanFilter::CombineRegions () {
27172691
vtkPolyData *pdB = cfB->GetOutput();
27182692

27192693
#ifdef DEBUG
2720-
std::cout << "Exporting modPdA_8.vtk" << std::endl;
27212694
WriteVTK("modPdA_8.vtk", pdA);
2722-
2723-
std::cout << "Exporting modPdB_8.vtk" << std::endl;
27242695
WriteVTK("modPdB_8.vtk", pdB);
27252696
#endif
27262697

0 commit comments

Comments
 (0)