From 406f34f66d7f06f2884354e08b2e291165465c94 Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Sat, 15 Jan 2022 04:13:44 +0000 Subject: [PATCH 1/3] Check whether consecutive prism vertices are duplicates --- utils/geom.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/utils/geom.c b/utils/geom.c index 6cbfd01..80ccc08 100644 --- a/utils/geom.c +++ b/utils/geom.c @@ -2552,7 +2552,7 @@ vector3 triangle_normal(vector3 v1, vector3 v2, vector3 v3) { /***************************************************************/ /* On entry, the only fields in o->prism that are assumed to */ -/* be initialized are: vertices, height, (optionally) */ +/* be initialized are: vertices, height, (optionally) */ /* axis, and sidewall_angle. If axis has not been initialized */ /* (i.e. it is set to its default value, which is the zero */ /* vector) then the prism axis is automatically computed as */ @@ -2570,9 +2570,17 @@ void init_prism(geometric_object *o) { int num_vertices = prsm->vertices.num_items; CHECK(num_vertices >= 3, "fewer than 3 vertices in init_prism"); + // check for any consecutive vertices which are duplicates + int nv; + for (nv = 1; nv < num_vertices; nv++) { + CHECK(!vector3_equal(vertices[nv], vertices[nv-1]), + "consecutive prism vertices are duplicates."); + } + CHECK(!vector3_equal(vertices[num_vertices-1], vertices[0]), + "consecutive prism vertices are duplicates."); + // compute centroid of vertices vector3 centroid = {0.0, 0.0, 0.0}; - int nv; for (nv = 0; nv < num_vertices; nv++) centroid = vector3_plus(centroid, vertices[nv]); prsm->centroid = centroid = vector3_scale(1.0 / ((double)num_vertices), centroid); From eb8768497f33c84f14c7c7f7029e4f528755a1fd Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Wed, 26 Jan 2022 17:52:40 +0000 Subject: [PATCH 2/3] remove duplicate consecutive vertices --- utils/geom.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/utils/geom.c b/utils/geom.c index 80ccc08..cf5c4b9 100644 --- a/utils/geom.c +++ b/utils/geom.c @@ -2570,17 +2570,20 @@ void init_prism(geometric_object *o) { int num_vertices = prsm->vertices.num_items; CHECK(num_vertices >= 3, "fewer than 3 vertices in init_prism"); - // check for any consecutive vertices which are duplicates - int nv; - for (nv = 1; nv < num_vertices; nv++) { - CHECK(!vector3_equal(vertices[nv], vertices[nv-1]), - "consecutive prism vertices are duplicates."); + // remove duplicate consecutive prism vertices + int i = 0; // last non-deleted vertex + for (int j = 1; j < num_vertices; ++j) { + if (!vector3_equal(vertices[i], vertices[j])) { + i += 1; + if (i < j) vertices[i] = vertices[j]; + } } - CHECK(!vector3_equal(vertices[num_vertices-1], vertices[0]), - "consecutive prism vertices are duplicates."); + num_vertices = i + 1 - vector3_equal(vertices[0], vertices[i]); + prsm->vertices.num_items = num_vertices; // compute centroid of vertices vector3 centroid = {0.0, 0.0, 0.0}; + int nv; for (nv = 0; nv < num_vertices; nv++) centroid = vector3_plus(centroid, vertices[nv]); prsm->centroid = centroid = vector3_scale(1.0 / ((double)num_vertices), centroid); From 90e7d79bc036589753de410698ae7cdd7e56d8cf Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Wed, 26 Jan 2022 18:41:54 +0000 Subject: [PATCH 3/3] realloc the vertices array, if necessary --- utils/geom.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/utils/geom.c b/utils/geom.c index cf5c4b9..fdd95b1 100644 --- a/utils/geom.c +++ b/utils/geom.c @@ -2579,7 +2579,10 @@ void init_prism(geometric_object *o) { } } num_vertices = i + 1 - vector3_equal(vertices[0], vertices[i]); - prsm->vertices.num_items = num_vertices; + if (prsm->vertices.num_items != num_vertices) { + prsm->vertices.num_items = num_vertices; + vertices = (vector3 *)realloc(vertices, num_vertices * sizeof(vector3)); + } // compute centroid of vertices vector3 centroid = {0.0, 0.0, 0.0};