Skip to content

Commit 5dc9c7e

Browse files
authored
Silently remove duplicate consecutive prism vertices (#60)
* Check whether consecutive prism vertices are duplicates * remove duplicate consecutive vertices * realloc the vertices array, if necessary
1 parent 76deb2a commit 5dc9c7e

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

utils/geom.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2552,7 +2552,7 @@ vector3 triangle_normal(vector3 v1, vector3 v2, vector3 v3) {
25522552

25532553
/***************************************************************/
25542554
/* On entry, the only fields in o->prism that are assumed to */
2555-
/* be initialized are: vertices, height, (optionally) */
2555+
/* be initialized are: vertices, height, (optionally) */
25562556
/* axis, and sidewall_angle. If axis has not been initialized */
25572557
/* (i.e. it is set to its default value, which is the zero */
25582558
/* vector) then the prism axis is automatically computed as */
@@ -2570,6 +2570,20 @@ void init_prism(geometric_object *o) {
25702570
int num_vertices = prsm->vertices.num_items;
25712571
CHECK(num_vertices >= 3, "fewer than 3 vertices in init_prism");
25722572

2573+
// remove duplicate consecutive prism vertices
2574+
int i = 0; // last non-deleted vertex
2575+
for (int j = 1; j < num_vertices; ++j) {
2576+
if (!vector3_equal(vertices[i], vertices[j])) {
2577+
i += 1;
2578+
if (i < j) vertices[i] = vertices[j];
2579+
}
2580+
}
2581+
num_vertices = i + 1 - vector3_equal(vertices[0], vertices[i]);
2582+
if (prsm->vertices.num_items != num_vertices) {
2583+
prsm->vertices.num_items = num_vertices;
2584+
vertices = (vector3 *)realloc(vertices, num_vertices * sizeof(vector3));
2585+
}
2586+
25732587
// compute centroid of vertices
25742588
vector3 centroid = {0.0, 0.0, 0.0};
25752589
int nv;

0 commit comments

Comments
 (0)