Skip to content

Commit 6411035

Browse files
committed
Merge pull request #4884 from MaelRL/PMP-fix_polygon_soup_simplification-GF
Fix and simplify polygon soup repairing subfunction "simplify_polygon"
2 parents 8b313b7 + 9d820d6 commit 6411035

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/repair_polygon_soup.h

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -118,43 +118,21 @@ bool simplify_polygon(PointRange& points,
118118
{
119119
const std::size_t ini_polygon_size = polygon.size();
120120

121-
// Start at the last since if two points are identical, the second one gets removed.
122-
// By starting at 'last', we ensure that 'to_remove' is ordered from closest to .begin()
123-
// to closest to .end()
124-
std::size_t last = ini_polygon_size - 1, i = last;
125-
bool stop = false;
126-
std::vector<std::size_t> to_remove;
127-
128-
do
121+
for(std::size_t i=0; i<polygon.size(); ++i)
129122
{
130-
std::size_t next_i = (i == last) ? 0 : i+1;
131-
stop = (next_i == last);
123+
const std::size_t s = polygon.size();
124+
if(s == 1)
125+
break;
132126

133-
while(polygon[i] == polygon[next_i] || // combinatorial equality
134-
traits.equal_3_object()(points[polygon[i]], points[polygon[next_i]])) // geometric equality
127+
const std::size_t ni = (i + 1) % s;
128+
if(polygon[i] == polygon[ni] ||
129+
traits.equal_3_object()(points[polygon[i]], points[polygon[ni]]))
135130
{
136-
to_remove.push_back(next_i);
137131
#ifdef CGAL_PMP_REPAIR_POLYGON_SOUP_VERBOSE_PP
138-
std::cout << "Duplicate point: polygon[" << next_i << "] = " << polygon[next_i] << std::endl;
132+
std::cout << "Duplicate point: polygon[" << ni << "] = " << polygon[ni] << std::endl;
139133
#endif
140-
next_i = (next_i == last) ? 0 : next_i+1;
141-
142-
// Every duplicate in front of 'last' (circularly-speaking) has already been cleared
143-
if(next_i == last)
144-
{
145-
stop = true;
146-
break;
147-
}
134+
polygon.erase(polygon.begin() + i--);
148135
}
149-
150-
i = next_i;
151-
}
152-
while(!stop);
153-
154-
while(!to_remove.empty())
155-
{
156-
polygon.erase(polygon.begin() + to_remove.back());
157-
to_remove.pop_back();
158136
}
159137

160138
const std::size_t removed_points_n = ini_polygon_size - polygon.size();

Polygon_mesh_processing/test/Polygon_mesh_processing/test_repair_polygon_soup.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,24 @@ void test_simplify_polygons(const bool /*verbose*/ = false)
289289
points.push_back(Point_3(1,3,0)); // #3
290290
points.push_back(Point_3(0,1,0)); // #4
291291
points.push_back(Point_3(1,1,0)); // #5
292-
points.push_back(Point_3(0,0,0)); // #6
292+
points.push_back(Point_3(0,0,0)); // #6 == #0
293293

294294
// ------
295295
CGAL_polygon polygon;
296-
polygon.push_back(0); polygon.push_back(2); polygon.push_back(4);
296+
polygon.push_back(0); polygon.push_back(2); polygon.push_back(4); polygon.push_back(0); polygon.push_back(0);
297297
polygons.push_back(polygon);
298298

299299
std::size_t res = PMP::internal::simplify_polygons_in_polygon_soup<K>(points, polygons);
300+
std::cout << "res: " << res << " / size: " << polygons.back().size() << std::endl;
301+
assert(res == 1 && polygons.back().size() == 3);
302+
303+
// ------
304+
polygon.clear();
305+
polygon.push_back(0); polygon.push_back(2); polygon.push_back(4);
306+
polygons.push_back(polygon);
307+
308+
res = PMP::internal::simplify_polygons_in_polygon_soup<K>(points, polygons);
309+
std::cout << "res: " << res << " / size: " << polygons.back().size() << std::endl;
300310
assert(res == 0 && polygons.back().size() == 3);
301311

302312
// ------
@@ -305,6 +315,7 @@ void test_simplify_polygons(const bool /*verbose*/ = false)
305315
polygons.push_back(polygon);
306316

307317
res = PMP::internal::simplify_polygons_in_polygon_soup(points, polygons, K());
318+
std::cout << "res: " << res << " / size: " << polygons.back().size() << std::endl;
308319
assert(res == 1 && polygons.back().size() == 1);
309320

310321
// ------
@@ -313,6 +324,7 @@ void test_simplify_polygons(const bool /*verbose*/ = false)
313324
polygons.push_back(polygon);
314325

315326
res = PMP::internal::simplify_polygons_in_polygon_soup(points, polygons, K());
327+
std::cout << "res: " << res << " / size: " << polygons.back().size() << std::endl;
316328
assert(res == 1 && polygons.back().size() == 1);
317329

318330
// ------
@@ -321,15 +333,26 @@ void test_simplify_polygons(const bool /*verbose*/ = false)
321333
polygons.push_back(polygon);
322334

323335
res = PMP::internal::simplify_polygons_in_polygon_soup(points, polygons, K());
336+
std::cout << "res: " << res << " / size: " << polygons.back().size() << std::endl;
324337
assert(res == 1 && polygons.back().size() == 2);
325338

339+
// ------
340+
polygon.clear();
341+
polygon.push_back(0); polygon.push_back(2); polygon.push_back(0); polygon.push_back(4);
342+
polygons.push_back(polygon);
343+
344+
res = PMP::internal::simplify_polygons_in_polygon_soup(points, polygons, K());
345+
std::cout << "res: " << res << " / size: " << polygons.back().size() << std::endl;
346+
assert(res == 0 && polygons.back().size() == 4);
347+
326348
// ------
327349
// Now with the same geometric positions, but different combinatorial information
328350
polygon.clear();
329351
polygon.push_back(0); polygon.push_back(2); polygon.push_back(1); polygon.push_back(6);
330352
polygons.push_back(polygon);
331353

332354
res = PMP::internal::simplify_polygons_in_polygon_soup(points, polygons, K());
355+
std::cout << "res: " << res << " / size: " << polygons.back().size() << std::endl;
333356
assert(res == 1 && polygons.back().size() == 3);
334357

335358
// ------
@@ -338,6 +361,7 @@ void test_simplify_polygons(const bool /*verbose*/ = false)
338361
polygons.push_back(polygon);
339362

340363
res = PMP::internal::simplify_polygons_in_polygon_soup(points, polygons, K());
364+
std::cout << "res: " << res << " / size: " << polygons.back().size() << std::endl;
341365
assert(res == 1 && polygons.back().size() == 2);
342366

343367
// ------
@@ -346,6 +370,7 @@ void test_simplify_polygons(const bool /*verbose*/ = false)
346370
polygons.push_back(polygon);
347371

348372
res = PMP::internal::simplify_polygons_in_polygon_soup(points, polygons, K());
373+
std::cout << "res: " << res << " / size: " << polygons.back().size() << std::endl;
349374
assert(res == 1 && polygons.back().size() == 3);
350375
}
351376

0 commit comments

Comments
 (0)