Skip to content

Silently remove duplicate consecutive prism vertices#60

Merged
stevengj merged 3 commits intoNanoComp:masterfrom
oskooi:prism_vertex_dup
Feb 2, 2022
Merged

Silently remove duplicate consecutive prism vertices#60
stevengj merged 3 commits intoNanoComp:masterfrom
oskooi:prism_vertex_dup

Conversation

@oskooi
Copy link
Collaborator

@oskooi oskooi commented Jan 15, 2022

utils/geom.c Outdated
"consecutive prism vertices are duplicates.");
}
CHECK(!vector3_equal(vertices[num_vertices-1], vertices[0]),
"consecutive prism vertices are duplicates.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that a CHECK failure will call exit, which won't be that nice from e.g. Python.

Copy link
Collaborator

@stevengj stevengj Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, to delete consecutive equal vertices, including the last point, you could do:

int i = 0; // last non-deleted vertex
for (j = 1; j < num_vertices; ++j) {
    if (!vector3_equal(vertices[i], vertices[j])) {
        i += 1;
        if (i < j) vertices[i] = vertices[j];
    }
}
num_vertices = i + 1 - vector3_equal(vertices[0], vertices[i]);
prsm->vertices.num_items = num_vertices;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(In principle, you could then call realloc to save a bit of memory in vertices, but it's probably not worth it — realloc is potentially unsafe if the memory was not allocated with malloc.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I included a realloc if the number of vertices has changed since the vertices array (via the items member of prsm->vertices) is allocated with malloc:

libctl/utils/ctl-io.scm

Lines 246 to 248 in 76deb2a

(print c-var-name-str ".items = "
(malloc (c-type-string type-name) c-var-name-str ".num_items")
";\n")

@oskooi oskooi changed the title Check whether consecutive prism vertices are duplicates Silently remove duplicate consecutive prism vertices Jan 26, 2022
@stevengj stevengj merged commit 5dc9c7e into NanoComp:master Feb 2, 2022
@oskooi oskooi deleted the prism_vertex_dup branch February 2, 2022 03:53
stevengj added a commit that referenced this pull request Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants