WIP: Admesh - replacement of C memory allocation with std vectors

This commit is contained in:
bubnikv 2019-06-04 22:06:42 +02:00
parent 3ab886b747
commit 8da54139c4
17 changed files with 450 additions and 585 deletions

View file

@ -97,18 +97,10 @@ void stl_check_facets_exact(stl_file *stl)
stl->stats.freed = 0;
stl->stats.collisions = 0;
stl->M = (int)hash_size_from_nr_faces(stl->stats.number_of_facets);
for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) {
// initialize neighbors list to -1 to mark unconnected edges
stl->neighbors_start[i].neighbor[0] = -1;
stl->neighbors_start[i].neighbor[1] = -1;
stl->neighbors_start[i].neighbor[2] = -1;
}
stl->heads = (stl_hash_edge**)calloc(stl->M, sizeof(*stl->heads));
if (stl->heads == NULL)
perror("stl_initialize_facet_check_exact");
stl->tail = (stl_hash_edge*)malloc(sizeof(stl_hash_edge));
if (stl->tail == NULL)
perror("stl_initialize_facet_check_exact");
for (auto &neighbor : stl->neighbors_start)
neighbor.reset();
stl->heads.assign(stl->M, nullptr);
stl->tail = new stl_hash_edge;
stl->tail->next = stl->tail;
for (int i = 0; i < stl->M; ++ i)
stl->heads[i] = stl->tail;
@ -180,7 +172,7 @@ static void insert_hash_edge(stl_file *stl, stl_hash_edge edge,
stl_hash_edge *temp;
if(link == stl->tail) {
/* This list doesn't have any edges currently in it. Add this one. */
new_edge = (stl_hash_edge*)malloc(sizeof(stl_hash_edge));
new_edge = new stl_hash_edge;
if(new_edge == NULL) perror("insert_hash_edge");
stl->stats.malloced++;
*new_edge = edge;
@ -192,7 +184,7 @@ static void insert_hash_edge(stl_file *stl, stl_hash_edge edge,
match_neighbors(stl, &edge, link);
/* Delete the matched edge from the list. */
stl->heads[chain_number] = link->next;
free(link);
delete link;
stl->stats.freed++;
return;
} else {
@ -200,7 +192,7 @@ static void insert_hash_edge(stl_file *stl, stl_hash_edge edge,
for(;;) {
if(link->next == stl->tail) {
/* This is the last item in the list. Insert a new edge. */
new_edge = (stl_hash_edge*)malloc(sizeof(stl_hash_edge));
new_edge = new stl_hash_edge;
if(new_edge == NULL) perror("insert_hash_edge");
stl->stats.malloced++;
*new_edge = edge;
@ -215,7 +207,7 @@ static void insert_hash_edge(stl_file *stl, stl_hash_edge edge,
/* Delete the matched edge from the list. */
temp = link->next;
link->next = link->next->next;
free(temp);
delete temp;
stl->stats.freed++;
return;
} else {
@ -307,48 +299,38 @@ static void stl_free_edges(stl_file *stl)
for (int i = 0; i < stl->M; i++) {
for (stl_hash_edge *temp = stl->heads[i]; stl->heads[i] != stl->tail; temp = stl->heads[i]) {
stl->heads[i] = stl->heads[i]->next;
free(temp);
delete temp;
++ stl->stats.freed;
}
}
}
free(stl->heads);
stl->heads = nullptr;
free(stl->tail);
stl->heads.clear();
delete stl->tail;
stl->tail = nullptr;
}
static void stl_initialize_facet_check_nearby(stl_file *stl)
{
int i;
if (stl->error)
return;
if (stl->error) return;
stl->stats.malloced = 0;
stl->stats.freed = 0;
stl->stats.collisions = 0;
stl->stats.malloced = 0;
stl->stats.freed = 0;
stl->stats.collisions = 0;
/* tolerance = STL_MAX(stl->stats.shortest_edge, tolerance);*/
/* tolerance = STL_MAX((stl->stats.bounding_diameter / 500000.0), tolerance);*/
/* tolerance *= 0.5;*/
stl->M = (int)hash_size_from_nr_faces(stl->stats.number_of_facets);
/* tolerance = STL_MAX(stl->stats.shortest_edge, tolerance);*/
/* tolerance = STL_MAX((stl->stats.bounding_diameter / 500000.0), tolerance);*/
/* tolerance *= 0.5;*/
stl->heads.assign(stl->M, nullptr);
stl->tail = new stl_hash_edge;
stl->tail->next = stl->tail;
stl->M = (int)hash_size_from_nr_faces(stl->stats.number_of_facets);
stl->heads = (stl_hash_edge**)calloc(stl->M, sizeof(*stl->heads));
if(stl->heads == NULL) perror("stl_initialize_facet_check_nearby");
stl->tail = (stl_hash_edge*)malloc(sizeof(stl_hash_edge));
if(stl->tail == NULL) perror("stl_initialize_facet_check_nearby");
stl->tail->next = stl->tail;
for(i = 0; i < stl->M; i++) {
stl->heads[i] = stl->tail;
}
for (int i = 0; i < stl->M; ++ i)
stl->heads[i] = stl->tail;
}
static void
stl_record_neighbors(stl_file *stl,
stl_hash_edge *edge_a, stl_hash_edge *edge_b) {
@ -358,29 +340,19 @@ stl_record_neighbors(stl_file *stl,
if (stl->error) return;
/* Facet a's neighbor is facet b */
stl->neighbors_start[edge_a->facet_number].neighbor[edge_a->which_edge % 3] =
edge_b->facet_number; /* sets the .neighbor part */
stl->neighbors_start[edge_a->facet_number].
which_vertex_not[edge_a->which_edge % 3] =
(edge_b->which_edge + 2) % 3; /* sets the .which_vertex_not part */
stl->neighbors_start[edge_a->facet_number].neighbor[edge_a->which_edge % 3] = edge_b->facet_number; /* sets the .neighbor part */
stl->neighbors_start[edge_a->facet_number].which_vertex_not[edge_a->which_edge % 3] = (edge_b->which_edge + 2) % 3; /* sets the .which_vertex_not part */
/* Facet b's neighbor is facet a */
stl->neighbors_start[edge_b->facet_number].neighbor[edge_b->which_edge % 3] =
edge_a->facet_number; /* sets the .neighbor part */
stl->neighbors_start[edge_b->facet_number].
which_vertex_not[edge_b->which_edge % 3] =
(edge_a->which_edge + 2) % 3; /* sets the .which_vertex_not part */
stl->neighbors_start[edge_b->facet_number].neighbor[edge_b->which_edge % 3] = edge_a->facet_number; /* sets the .neighbor part */
stl->neighbors_start[edge_b->facet_number].which_vertex_not[edge_b->which_edge % 3] = (edge_a->which_edge + 2) % 3; /* sets the .which_vertex_not part */
if( ((edge_a->which_edge < 3) && (edge_b->which_edge < 3))
|| ((edge_a->which_edge > 2) && (edge_b->which_edge > 2))) {
/* these facets are oriented in opposite directions. */
/* their normals are probably messed up. */
stl->neighbors_start[edge_a->facet_number].
which_vertex_not[edge_a->which_edge % 3] += 3;
stl->neighbors_start[edge_b->facet_number].
which_vertex_not[edge_b->which_edge % 3] += 3;
stl->neighbors_start[edge_a->facet_number].which_vertex_not[edge_a->which_edge % 3] += 3;
stl->neighbors_start[edge_b->facet_number].which_vertex_not[edge_b->which_edge % 3] += 3;
}
@ -561,8 +533,7 @@ stl_which_vertices_to_change(stl_file *stl, stl_hash_edge *edge_a,
*facet1 = -1;
} else {
if( (stl->neighbors_start[edge_a->facet_number].neighbor[v1a] == -1)
&& (stl->neighbors_start[edge_a->facet_number].
neighbor[(v1a + 2) % 3] == -1)) {
&& (stl->neighbors_start[edge_a->facet_number].neighbor[(v1a + 2) % 3] == -1)) {
/* This vertex has no neighbors. This is a good one to change */
*facet1 = edge_a->facet_number;
*vertex1 = v1a;
@ -581,8 +552,7 @@ stl_which_vertices_to_change(stl_file *stl, stl_hash_edge *edge_a,
*facet2 = -1;
} else {
if( (stl->neighbors_start[edge_a->facet_number].neighbor[v2a] == -1)
&& (stl->neighbors_start[edge_a->facet_number].
neighbor[(v2a + 2) % 3] == -1)) {
&& (stl->neighbors_start[edge_a->facet_number].neighbor[(v2a + 2) % 3] == -1)) {
/* This vertex has no neighbors. This is a good one to change */
*facet2 = edge_a->facet_number;
*vertex2 = v2a;
@ -595,140 +565,6 @@ stl_which_vertices_to_change(stl_file *stl, stl_hash_edge *edge_a,
}
}
static void remove_facet(stl_file *stl, int facet_number)
{
assert(! stl->error);
++ stl->stats.facets_removed;
/* Update list of connected edges */
stl_neighbors &neighbors = stl->neighbors_start[facet_number];
// Update statistics on unconnected triangle edges.
switch ((neighbors.neighbor[0] == -1) + (neighbors.neighbor[1] == -1) + (neighbors.neighbor[2] == -1)) {
case 0: // Facet has 3 neighbors
-- stl->stats.connected_facets_3_edge;
-- stl->stats.connected_facets_2_edge;
-- stl->stats.connected_facets_1_edge;
break;
case 1: // Facet has 2 neighbors
-- stl->stats.connected_facets_2_edge;
-- stl->stats.connected_facets_1_edge;
break;
case 2: // Facet has 1 neighbor
-- stl->stats.connected_facets_1_edge;
case 3: // Facet has 0 neighbors
break;
default:
assert(false);
}
if (facet_number == -- stl->stats.number_of_facets)
// Removing the last face is easy, just forget the last face.
return;
// Copy the face and neighborship from the last face to facet_number.
stl->facet_start[facet_number] = stl->facet_start[stl->stats.number_of_facets];
neighbors = stl->neighbors_start[stl->stats.number_of_facets];
// Update neighborship of faces, which used to point to the last face, now moved to facet_number.
for (int i = 0; i < 3; ++ i)
if (neighbors.neighbor[i] != -1) {
int &other_face_idx = stl->neighbors_start[neighbors.neighbor[i]].neighbor[(neighbors.which_vertex_not[i] + 1) % 3];
if (other_face_idx != stl->stats.number_of_facets) {
printf("in remove_facet: neighbor = %d numfacets = %d this is wrong\n", other_face_idx, stl->stats.number_of_facets);
return;
}
other_face_idx = facet_number;
}
}
static void remove_degenerate(stl_file *stl, int facet)
{
assert(! stl->error);
// Update statistics on face connectivity.
auto stl_update_connects_remove_1 = [stl](int facet_num) {
assert(! stl->error);
//FIXME when decreasing 3_edge, should I increase 2_edge etc?
switch ((stl->neighbors_start[facet_num].neighbor[0] == -1) + (stl->neighbors_start[facet_num].neighbor[1] == -1) + (stl->neighbors_start[facet_num].neighbor[2] == -1)) {
case 0: // Facet has 3 neighbors
-- stl->stats.connected_facets_3_edge; break;
case 1: // Facet has 2 neighbors
-- stl->stats.connected_facets_2_edge; break;
case 2: // Facet has 1 neighbor
-- stl->stats.connected_facets_1_edge; break;
case 3: // Facet has 0 neighbors
break;
default:
assert(false);
}
};
int edge_to_collapse = 0;
if (stl->facet_start[facet].vertex[0] == stl->facet_start[facet].vertex[1]) {
if (stl->facet_start[facet].vertex[1] == stl->facet_start[facet].vertex[2]) {
// All 3 vertices are equal. Collapse the edge with no neighbor if it exists.
const int *nbr = stl->neighbors_start[facet].neighbor;
edge_to_collapse = (nbr[0] == -1) ? 0 : (nbr[1] == -1) ? 1 : 2;
} else {
edge_to_collapse = 0;
}
} else if (stl->facet_start[facet].vertex[1] == stl->facet_start[facet].vertex[2]) {
edge_to_collapse = 1;
} else if (stl->facet_start[facet].vertex[2] == stl->facet_start[facet].vertex[0]) {
edge_to_collapse = 2;
} else {
// No degenerate. Function shouldn't have been called.
return;
}
int edge[3] = { (edge_to_collapse + 1) % 3, (edge_to_collapse + 2) % 3, edge_to_collapse };
int neighbor[] = {
stl->neighbors_start[facet].neighbor[edge[0]],
stl->neighbors_start[facet].neighbor[edge[1]],
stl->neighbors_start[facet].neighbor[edge[2]]
};
int vnot[] = {
stl->neighbors_start[facet].which_vertex_not[edge[0]],
stl->neighbors_start[facet].which_vertex_not[edge[1]],
stl->neighbors_start[facet].which_vertex_not[edge[2]]
};
// Update statistics on edge connectivity.
if (neighbor[0] == -1)
stl_update_connects_remove_1(neighbor[1]);
if (neighbor[1] == -1)
stl_update_connects_remove_1(neighbor[0]);
if (neighbor[0] >= 0) {
if (neighbor[1] >= 0) {
// Adjust the "flip" flag for the which_vertex_not values.
if (vnot[0] > 2) {
if (vnot[1] > 2) {
// The face to be removed has its normal flipped compared to the left & right neighbors, therefore after removing this face
// the two remaining neighbors will be oriented correctly.
vnot[0] -= 3;
vnot[1] -= 3;
} else
// One neighbor has its normal inverted compared to the face to be removed, the other is oriented equally.
// After removal, the two neighbors will have their normals flipped.
vnot[1] += 3;
} else if (vnot[1] > 2)
// One neighbor has its normal inverted compared to the face to be removed, the other is oriented equally.
// After removal, the two neighbors will have their normals flipped.
vnot[0] += 3;
}
stl->neighbors_start[neighbor[0]].neighbor[(vnot[0] + 1) % 3] = (neighbor[0] == neighbor[1]) ? -1 : neighbor[1];
stl->neighbors_start[neighbor[0]].which_vertex_not[(vnot[0] + 1) % 3] = vnot[1];
}
if (neighbor[1] >= 0) {
stl->neighbors_start[neighbor[1]].neighbor[(vnot[1] + 1) % 3] = (neighbor[0] == neighbor[1]) ? -1 : neighbor[0];
stl->neighbors_start[neighbor[1]].which_vertex_not[(vnot[1] + 1) % 3] = vnot[0];
}
if (neighbor[2] >= 0) {
stl_update_connects_remove_1(neighbor[2]);
stl->neighbors_start[neighbor[2]].neighbor[(vnot[2] + 1) % 3] = -1;
}
remove_facet(stl, facet);
}
void stl_remove_unconnected_facets(stl_file *stl)
{
// A couple of things need to be done here. One is to remove any completely unconnected facets (0 edges connected) since these are
@ -737,12 +573,143 @@ void stl_remove_unconnected_facets(stl_file *stl)
if (stl->error)
return;
auto remove_facet = [stl](int facet_number)
{
++ stl->stats.facets_removed;
/* Update list of connected edges */
stl_neighbors &neighbors = stl->neighbors_start[facet_number];
// Update statistics on unconnected triangle edges.
switch ((neighbors.neighbor[0] == -1) + (neighbors.neighbor[1] == -1) + (neighbors.neighbor[2] == -1)) {
case 0: // Facet has 3 neighbors
-- stl->stats.connected_facets_3_edge;
-- stl->stats.connected_facets_2_edge;
-- stl->stats.connected_facets_1_edge;
break;
case 1: // Facet has 2 neighbors
-- stl->stats.connected_facets_2_edge;
-- stl->stats.connected_facets_1_edge;
break;
case 2: // Facet has 1 neighbor
-- stl->stats.connected_facets_1_edge;
case 3: // Facet has 0 neighbors
break;
default:
assert(false);
}
if (facet_number == -- stl->stats.number_of_facets)
// Removing the last face is easy, just forget the last face.
return;
// Copy the face and neighborship from the last face to facet_number.
stl->facet_start[facet_number] = stl->facet_start[stl->stats.number_of_facets];
neighbors = stl->neighbors_start[stl->stats.number_of_facets];
// Update neighborship of faces, which used to point to the last face, now moved to facet_number.
for (int i = 0; i < 3; ++ i)
if (neighbors.neighbor[i] != -1) {
int &other_face_idx = stl->neighbors_start[neighbors.neighbor[i]].neighbor[(neighbors.which_vertex_not[i] + 1) % 3];
if (other_face_idx != stl->stats.number_of_facets) {
printf("in remove_facet: neighbor = %d numfacets = %d this is wrong\n", other_face_idx, stl->stats.number_of_facets);
return;
}
other_face_idx = facet_number;
}
};
auto remove_degenerate = [stl, remove_facet](int facet)
{
// Update statistics on face connectivity.
auto stl_update_connects_remove_1 = [stl](int facet_num) {
assert(! stl->error);
//FIXME when decreasing 3_edge, should I increase 2_edge etc?
switch ((stl->neighbors_start[facet_num].neighbor[0] == -1) + (stl->neighbors_start[facet_num].neighbor[1] == -1) + (stl->neighbors_start[facet_num].neighbor[2] == -1)) {
case 0: // Facet has 3 neighbors
-- stl->stats.connected_facets_3_edge; break;
case 1: // Facet has 2 neighbors
-- stl->stats.connected_facets_2_edge; break;
case 2: // Facet has 1 neighbor
-- stl->stats.connected_facets_1_edge; break;
case 3: // Facet has 0 neighbors
break;
default:
assert(false);
}
};
int edge_to_collapse = 0;
if (stl->facet_start[facet].vertex[0] == stl->facet_start[facet].vertex[1]) {
if (stl->facet_start[facet].vertex[1] == stl->facet_start[facet].vertex[2]) {
// All 3 vertices are equal. Collapse the edge with no neighbor if it exists.
const int *nbr = stl->neighbors_start[facet].neighbor;
edge_to_collapse = (nbr[0] == -1) ? 0 : (nbr[1] == -1) ? 1 : 2;
} else {
edge_to_collapse = 0;
}
} else if (stl->facet_start[facet].vertex[1] == stl->facet_start[facet].vertex[2]) {
edge_to_collapse = 1;
} else if (stl->facet_start[facet].vertex[2] == stl->facet_start[facet].vertex[0]) {
edge_to_collapse = 2;
} else {
// No degenerate. Function shouldn't have been called.
return;
}
int edge[3] = { (edge_to_collapse + 1) % 3, (edge_to_collapse + 2) % 3, edge_to_collapse };
int neighbor[] = {
stl->neighbors_start[facet].neighbor[edge[0]],
stl->neighbors_start[facet].neighbor[edge[1]],
stl->neighbors_start[facet].neighbor[edge[2]]
};
int vnot[] = {
stl->neighbors_start[facet].which_vertex_not[edge[0]],
stl->neighbors_start[facet].which_vertex_not[edge[1]],
stl->neighbors_start[facet].which_vertex_not[edge[2]]
};
// Update statistics on edge connectivity.
if (neighbor[0] == -1)
stl_update_connects_remove_1(neighbor[1]);
if (neighbor[1] == -1)
stl_update_connects_remove_1(neighbor[0]);
if (neighbor[0] >= 0) {
if (neighbor[1] >= 0) {
// Adjust the "flip" flag for the which_vertex_not values.
if (vnot[0] > 2) {
if (vnot[1] > 2) {
// The face to be removed has its normal flipped compared to the left & right neighbors, therefore after removing this face
// the two remaining neighbors will be oriented correctly.
vnot[0] -= 3;
vnot[1] -= 3;
} else
// One neighbor has its normal inverted compared to the face to be removed, the other is oriented equally.
// After removal, the two neighbors will have their normals flipped.
vnot[1] += 3;
} else if (vnot[1] > 2)
// One neighbor has its normal inverted compared to the face to be removed, the other is oriented equally.
// After removal, the two neighbors will have their normals flipped.
vnot[0] += 3;
}
stl->neighbors_start[neighbor[0]].neighbor[(vnot[0] + 1) % 3] = (neighbor[0] == neighbor[1]) ? -1 : neighbor[1];
stl->neighbors_start[neighbor[0]].which_vertex_not[(vnot[0] + 1) % 3] = vnot[1];
}
if (neighbor[1] >= 0) {
stl->neighbors_start[neighbor[1]].neighbor[(vnot[1] + 1) % 3] = (neighbor[0] == neighbor[1]) ? -1 : neighbor[0];
stl->neighbors_start[neighbor[1]].which_vertex_not[(vnot[1] + 1) % 3] = vnot[0];
}
if (neighbor[2] >= 0) {
stl_update_connects_remove_1(neighbor[2]);
stl->neighbors_start[neighbor[2]].neighbor[(vnot[2] + 1) % 3] = -1;
}
remove_facet(facet);
};
// remove degenerate facets
for (uint32_t i = 0; i < stl->stats.number_of_facets;)
if (stl->facet_start[i].vertex[0] == stl->facet_start[i].vertex[1] ||
stl->facet_start[i].vertex[0] == stl->facet_start[i].vertex[2] ||
stl->facet_start[i].vertex[1] == stl->facet_start[i].vertex[2]) {
remove_degenerate(stl, i);
remove_degenerate(i);
// assert(stl_validate(stl));
} else
++ i;
@ -754,7 +721,7 @@ void stl_remove_unconnected_facets(stl_file *stl)
stl->neighbors_start[i].neighbor[1] == -1 &&
stl->neighbors_start[i].neighbor[2] == -1) {
// This facet is completely unconnected. Remove it.
remove_facet(stl, i);
remove_facet(i);
assert(stl_validate(stl));
} else
++ i;
@ -850,8 +817,7 @@ stl_fill_holes(stl_file *stl) {
}
break;
} else {
vnot = stl->neighbors_start[facet_num].
which_vertex_not[next_edge];
vnot = stl->neighbors_start[facet_num].which_vertex_not[next_edge];
facet_num = next_facet;
}
@ -867,27 +833,14 @@ Try using a smaller tolerance or don't do a nearby check\n");
}
}
void
stl_add_facet(stl_file *stl, stl_facet *new_facet) {
if (stl->error) return;
stl->stats.facets_added += 1;
if(stl->stats.facets_malloced < (int)stl->stats.number_of_facets + 1) {
stl->facet_start = (stl_facet*)realloc(stl->facet_start,
(sizeof(stl_facet) * (stl->stats.facets_malloced + 256)));
if(stl->facet_start == NULL) perror("stl_add_facet");
stl->neighbors_start = (stl_neighbors*)realloc(stl->neighbors_start,
(sizeof(stl_neighbors) * (stl->stats.facets_malloced + 256)));
if(stl->neighbors_start == NULL) perror("stl_add_facet");
stl->stats.facets_malloced += 256;
}
stl->facet_start[stl->stats.number_of_facets] = *new_facet;
/* note that the normal vector is not set here, just initialized to 0 */
stl->facet_start[stl->stats.number_of_facets].normal = stl_normal::Zero();
stl->neighbors_start[stl->stats.number_of_facets].neighbor[0] = -1;
stl->neighbors_start[stl->stats.number_of_facets].neighbor[1] = -1;
stl->neighbors_start[stl->stats.number_of_facets].neighbor[2] = -1;
stl->stats.number_of_facets += 1;
void stl_add_facet(stl_file *stl, const stl_facet *new_facet)
{
if (stl->error)
return;
++ stl->stats.facets_added;
++ stl->stats.number_of_facets;
stl->facet_start.emplace_back(*new_facet);
// note that the normal vector is not set here, just initialized to 0.
stl->facet_start[stl->stats.number_of_facets].normal = stl_normal::Zero();
stl->neighbors_start.emplace_back();
}

View file

@ -84,7 +84,6 @@ stl_reverse_facet(stl_file *stl, int facet_num) {
void
stl_fix_normal_directions(stl_file *stl) {
char *norm_sw;
/* int edge_num;*/
/* int vnot;*/
int checked = 0;
@ -101,7 +100,6 @@ stl_fix_normal_directions(stl_file *stl) {
struct stl_normal *newn;
struct stl_normal *temp;
int* reversed_ids;
int reversed_count = 0;
int id;
int force_exit = 0;
@ -112,20 +110,15 @@ stl_fix_normal_directions(stl_file *stl) {
if (stl->stats.number_of_facets == 0) return;
/* Initialize linked list. */
head = (struct stl_normal*)malloc(sizeof(struct stl_normal));
if(head == NULL) perror("stl_fix_normal_directions");
tail = (struct stl_normal*)malloc(sizeof(struct stl_normal));
if(tail == NULL) perror("stl_fix_normal_directions");
head = new stl_normal;
tail = new stl_normal;
head->next = tail;
tail->next = tail;
/* Initialize list that keeps track of already fixed facets. */
norm_sw = (char*)calloc(stl->stats.number_of_facets, sizeof(char));
if(norm_sw == NULL) perror("stl_fix_normal_directions");
std::vector<char> norm_sw(stl->stats.number_of_facets, 0);
/* Initialize list that keeps track of reversed facets. */
reversed_ids = (int*)calloc(stl->stats.number_of_facets, sizeof(int));
if (reversed_ids == NULL) perror("stl_fix_normal_directions reversed_ids");
std::vector<int> reversed_ids(stl->stats.number_of_facets, 0);
facet_num = 0;
/* If normal vector is not within tolerance and backwards:
@ -166,8 +159,7 @@ stl_fix_normal_directions(stl_file *stl) {
/* If we haven't fixed this facet yet, add it to the list: */
if(norm_sw[stl->neighbors_start[facet_num].neighbor[j]] != 1) {
/* Add node to beginning of list. */
newn = (struct stl_normal*)malloc(sizeof(struct stl_normal));
if(newn == NULL) perror("stl_fix_normal_directions");
newn = new stl_normal;
newn->facet_num = stl->neighbors_start[facet_num].neighbor[j];
newn->next = head->next;
head->next = newn;
@ -187,7 +179,7 @@ stl_fix_normal_directions(stl_file *stl) {
}
temp = head->next; /* Delete this facet from the list. */
head->next = head->next->next;
free(temp);
delete temp;
} else { /* if we ran out of facets to fix: */
/* All of the facets in this part have been fixed. */
stl->stats.number_of_parts += 1;
@ -213,10 +205,8 @@ stl_fix_normal_directions(stl_file *stl) {
}
}
}
free(head);
free(tail);
free(reversed_ids);
free(norm_sw);
delete head;
delete tail;
}
static int stl_check_normal_vector(stl_file *stl, int facet_num, int normal_fix_flag) {

View file

@ -31,17 +31,8 @@
void stl_invalidate_shared_vertices(stl_file *stl)
{
if (stl->error)
return;
if (stl->v_indices != nullptr) {
free(stl->v_indices);
stl->v_indices = nullptr;
}
if (stl->v_shared != nullptr) {
free(stl->v_shared);
stl->v_shared = nullptr;
}
stl->v_indices.clear();
stl->v_shared.clear();
}
void stl_generate_shared_vertices(stl_file *stl)
@ -53,23 +44,11 @@ void stl_generate_shared_vertices(stl_file *stl)
stl_invalidate_shared_vertices(stl);
// 3 indices to vertex per face
stl->v_indices = (v_indices_struct*)calloc(stl->stats.number_of_facets, sizeof(v_indices_struct));
if (stl->v_indices == nullptr)
perror("stl_generate_shared_vertices");
stl->v_indices.assign(stl->stats.number_of_facets, v_indices_struct());
// Shared vertices (3D coordinates)
stl->v_shared = (stl_vertex*)calloc((stl->stats.number_of_facets / 2), sizeof(stl_vertex));
if (stl->v_shared == nullptr)
perror("stl_generate_shared_vertices");
stl->stats.shared_malloced = stl->stats.number_of_facets / 2;
stl->v_shared.assign(stl->stats.number_of_facets / 2, stl_vertex());
stl->stats.shared_vertices = 0;
for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) {
// vertex index -1 means no shared vertex was assigned yet.
stl->v_indices[i].vertex[0] = -1;
stl->v_indices[i].vertex[1] = -1;
stl->v_indices[i].vertex[2] = -1;
}
// A degenerate mesh may contain loops: Traversing a fan will end up in an endless loop
// while never reaching the starting face. To avoid these endless loops, traversed faces at each fan traversal
// are marked with a unique fan_traversal_stamp.
@ -82,13 +61,7 @@ void stl_generate_shared_vertices(stl_file *stl)
// Shared vertex was already assigned.
continue;
// Create a new shared vertex.
if (stl->stats.shared_vertices == stl->stats.shared_malloced) {
stl->stats.shared_malloced += 1024;
stl->v_shared = (stl_vertex*)realloc(stl->v_shared, stl->stats.shared_malloced * sizeof(stl_vertex));
if(stl->v_shared == nullptr)
perror("stl_generate_shared_vertices");
}
stl->v_shared[stl->stats.shared_vertices] = stl->facet_start[facet_idx].vertex[j];
stl->v_shared.emplace_back(stl->facet_start[facet_idx].vertex[j]);
// Traverse the fan around the j-th vertex of the i-th face, assign the newly created shared vertex index to all the neighboring triangles in the triangle fan.
int facet_in_fan_idx = facet_idx;
bool edge_direction = false;

View file

@ -27,6 +27,7 @@
#include <stdint.h>
#include <stddef.h>
#include <vector>
#include <Eigen/Geometry>
// Size of the binary STL header, free form.
@ -44,18 +45,18 @@ static_assert(sizeof(stl_vertex) == 12, "size of stl_vertex incorrect");
static_assert(sizeof(stl_normal) == 12, "size of stl_normal incorrect");
struct stl_facet {
stl_normal normal;
stl_vertex vertex[3];
char extra[2];
stl_normal normal;
stl_vertex vertex[3];
char extra[2];
stl_facet rotated(const Eigen::Quaternion<float, Eigen::DontAlign> &rot) {
stl_facet out;
out.normal = rot * this->normal;
out.vertex[0] = rot * this->vertex[0];
out.vertex[1] = rot * this->vertex[1];
out.vertex[2] = rot * this->vertex[2];
return out;
}
stl_facet rotated(const Eigen::Quaternion<float, Eigen::DontAlign> &rot) const {
stl_facet out;
out.normal = rot * this->normal;
out.vertex[0] = rot * this->vertex[0];
out.vertex[1] = rot * this->vertex[1];
out.vertex[2] = rot * this->vertex[2];
return out;
}
};
#define SIZEOF_STL_FACET 50
@ -67,86 +68,100 @@ static_assert(sizeof(stl_facet) >= SIZEOF_STL_FACET, "size of stl_facet incorrec
typedef enum {binary, ascii, inmemory} stl_type;
typedef struct {
stl_vertex p1;
stl_vertex p2;
int facet_number;
} stl_edge;
struct stl_edge {
stl_vertex p1;
stl_vertex p2;
int facet_number;
};
typedef struct stl_hash_edge {
// Key of a hash edge: sorted vertices of the edge.
uint32_t key[6];
// Compare two keys.
bool operator==(const stl_hash_edge &rhs) { return memcmp(key, rhs.key, sizeof(key)) == 0; }
bool operator!=(const stl_hash_edge &rhs) { return ! (*this == rhs); }
int hash(int M) const { return ((key[0] / 11 + key[1] / 7 + key[2] / 3) ^ (key[3] / 11 + key[4] / 7 + key[5] / 3)) % M; }
// Index of a facet owning this edge.
int facet_number;
// Index of this edge inside the facet with an index of facet_number.
// If this edge is stored backwards, which_edge is increased by 3.
int which_edge;
struct stl_hash_edge *next;
} stl_hash_edge;
struct stl_hash_edge {
// Key of a hash edge: sorted vertices of the edge.
uint32_t key[6];
// Compare two keys.
bool operator==(const stl_hash_edge &rhs) { return memcmp(key, rhs.key, sizeof(key)) == 0; }
bool operator!=(const stl_hash_edge &rhs) { return ! (*this == rhs); }
int hash(int M) const { return ((key[0] / 11 + key[1] / 7 + key[2] / 3) ^ (key[3] / 11 + key[4] / 7 + key[5] / 3)) % M; }
// Index of a facet owning this edge.
int facet_number;
// Index of this edge inside the facet with an index of facet_number.
// If this edge is stored backwards, which_edge is increased by 3.
int which_edge;
struct stl_hash_edge *next;
};
typedef struct {
// Index of a neighbor facet.
int neighbor[3];
// Index of an opposite vertex at the neighbor face.
char which_vertex_not[3];
} stl_neighbors;
struct stl_neighbors {
stl_neighbors() { reset(); }
void reset() {
neighbor[0] = -1;
neighbor[1] = -1;
neighbor[2] = -1;
which_vertex_not[0] = -1;
which_vertex_not[1] = -1;
which_vertex_not[2] = -1;
}
typedef struct {
int vertex[3];
} v_indices_struct;
// Index of a neighbor facet.
int neighbor[3];
// Index of an opposite vertex at the neighbor face.
char which_vertex_not[3];
};
typedef struct {
char header[81];
stl_type type;
uint32_t number_of_facets;
stl_vertex max;
stl_vertex min;
stl_vertex size;
float bounding_diameter;
float shortest_edge;
float volume;
unsigned number_of_blocks;
int connected_edges;
int connected_facets_1_edge;
int connected_facets_2_edge;
int connected_facets_3_edge;
int facets_w_1_bad_edge;
int facets_w_2_bad_edge;
int facets_w_3_bad_edge;
int original_num_facets;
int edges_fixed;
int degenerate_facets;
int facets_removed;
int facets_added;
int facets_reversed;
int backwards_edges;
int normals_fixed;
int number_of_parts;
int malloced;
int freed;
int facets_malloced;
int collisions;
int shared_vertices;
int shared_malloced;
} stl_stats;
struct v_indices_struct {
// -1 means no vertex index has been assigned yet
v_indices_struct() { vertex[0] = -1; vertex[1] = -1; vertex[2] = -1; }
int vertex[3];
};
typedef struct {
FILE *fp;
stl_facet *facet_start;
stl_hash_edge **heads;
stl_hash_edge *tail;
int M;
stl_neighbors *neighbors_start;
v_indices_struct *v_indices;
stl_vertex *v_shared;
stl_stats stats;
char error;
} stl_file;
struct stl_stats {
char header[81];
stl_type type;
uint32_t number_of_facets;
stl_vertex max;
stl_vertex min;
stl_vertex size;
float bounding_diameter;
float shortest_edge;
float volume;
unsigned number_of_blocks;
int connected_edges;
int connected_facets_1_edge;
int connected_facets_2_edge;
int connected_facets_3_edge;
int facets_w_1_bad_edge;
int facets_w_2_bad_edge;
int facets_w_3_bad_edge;
int original_num_facets;
int edges_fixed;
int degenerate_facets;
int facets_removed;
int facets_added;
int facets_reversed;
int backwards_edges;
int normals_fixed;
int number_of_parts;
int shared_vertices;
// hash table statistics
int malloced;
int freed;
int collisions;
};
struct stl_file {
FILE *fp;
std::vector<stl_facet> facet_start;
std::vector<stl_neighbors> neighbors_start;
// Hash table on edges
std::vector<stl_hash_edge*> heads;
stl_hash_edge* tail;
int M;
// Indexed face set
std::vector<v_indices_struct> v_indices;
std::vector<stl_vertex> v_shared;
// Statistics
stl_stats stats;
char error;
};
extern void stl_open(stl_file *stl, const char *file);
extern void stl_close(stl_file *stl);
@ -272,7 +287,7 @@ extern void stl_allocate(stl_file *stl);
extern void stl_read(stl_file *stl, int first_facet, bool first);
extern void stl_facet_stats(stl_file *stl, stl_facet facet, bool &first);
extern void stl_reallocate(stl_file *stl);
extern void stl_add_facet(stl_file *stl, stl_facet *new_facet);
extern void stl_add_facet(stl_file *stl, const stl_facet *new_facet);
extern void stl_clear_error(stl_file *stl);
extern int stl_get_error(stl_file *stl);

View file

@ -109,7 +109,6 @@ Normals fixed : %5d\n", stl->stats.normals_fixed);
void
stl_write_ascii(stl_file *stl, const char *file, const char *label) {
int i;
char *error_msg;
if (stl->error) return;
@ -129,7 +128,7 @@ stl_write_ascii(stl_file *stl, const char *file, const char *label) {
fprintf(fp, "solid %s\n", label);
for(i = 0; i < stl->stats.number_of_facets; i++) {
for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) {
fprintf(fp, " facet normal % .8E % .8E % .8E\n",
stl->facet_start[i].normal(0), stl->facet_start[i].normal(1),
stl->facet_start[i].normal(2));
@ -154,7 +153,6 @@ stl_write_ascii(stl_file *stl, const char *file, const char *label) {
void
stl_print_neighbors(stl_file *stl, char *file) {
int i;
FILE *fp;
char *error_msg;
@ -173,7 +171,7 @@ stl_print_neighbors(stl_file *stl, char *file) {
return;
}
for(i = 0; i < stl->stats.number_of_facets; i++) {
for (uint32_t i = 0; i < stl->stats.number_of_facets; i++) {
fprintf(fp, "%d, %d,%d, %d,%d, %d,%d\n",
i,
stl->neighbors_start[i].neighbor[0],
@ -200,7 +198,6 @@ void stl_internal_reverse_quads(char *buf, size_t cnt)
void
stl_write_binary(stl_file *stl, const char *file, const char *label) {
FILE *fp;
int i;
char *error_msg;
if (stl->error) return;
@ -219,13 +216,13 @@ stl_write_binary(stl_file *stl, const char *file, const char *label) {
}
fprintf(fp, "%s", label);
for(i = strlen(label); i < LABEL_SIZE; i++) putc(0, fp);
for(size_t i = strlen(label); i < LABEL_SIZE; i++) putc(0, fp);
fseek(fp, LABEL_SIZE, SEEK_SET);
#ifdef BOOST_LITTLE_ENDIAN
fwrite(&stl->stats.number_of_facets, 4, 1, fp);
for (i = 0; i < stl->stats.number_of_facets; ++ i)
fwrite(stl->facet_start + i, SIZEOF_STL_FACET, 1, fp);
for (const stl_facet &facet : stl->facet_start)
fwrite(&facet, SIZEOF_STL_FACET, 1, fp);
#else /* BOOST_LITTLE_ENDIAN */
char buffer[50];
// Convert the number of facets to little endian.
@ -288,8 +285,6 @@ stl_write_neighbor(stl_file *stl, int facet) {
void
stl_write_quad_object(stl_file *stl, char *file) {
FILE *fp;
int i;
int j;
char *error_msg;
stl_vertex connect_color = stl_vertex::Zero();
stl_vertex uncon_1_color = stl_vertex::Zero();
@ -313,10 +308,10 @@ stl_write_quad_object(stl_file *stl, char *file) {
}
fprintf(fp, "CQUAD\n");
for(i = 0; i < stl->stats.number_of_facets; i++) {
j = ((stl->neighbors_start[i].neighbor[0] == -1) +
(stl->neighbors_start[i].neighbor[1] == -1) +
(stl->neighbors_start[i].neighbor[2] == -1));
for (uint32_t i = 0; i < stl->stats.number_of_facets; i++) {
int j = ((stl->neighbors_start[i].neighbor[0] == -1) +
(stl->neighbors_start[i].neighbor[1] == -1) +
(stl->neighbors_start[i].neighbor[2] == -1));
if(j == 0) {
color = connect_color;
} else if(j == 1) {
@ -346,9 +341,8 @@ stl_write_quad_object(stl_file *stl, char *file) {
fclose(fp);
}
void
stl_write_dxf(stl_file *stl, const char *file, char *label) {
int i;
void stl_write_dxf(stl_file *stl, const char *file, char *label)
{
FILE *fp;
char *error_msg;
@ -375,7 +369,7 @@ stl_write_dxf(stl_file *stl, const char *file, char *label) {
fprintf(fp, "0\nSECTION\n2\nENTITIES\n");
for(i = 0; i < stl->stats.number_of_facets; i++) {
for (uint32_t i = 0; i < stl->stats.number_of_facets; i++) {
fprintf(fp, "0\n3DFACE\n8\n0\n");
fprintf(fp, "10\n%f\n20\n%f\n30\n%f\n",
stl->facet_start[i].vertex[0](0), stl->facet_start[i].vertex[0](1),

View file

@ -35,22 +35,38 @@
#error "SEEK_SET not defined"
#endif
void
stl_open(stl_file *stl, const char *file) {
stl_initialize(stl);
stl_count_facets(stl, file);
stl_allocate(stl);
stl_read(stl, 0, true);
if (stl->fp != nullptr) {
fclose(stl->fp);
stl->fp = nullptr;
}
void stl_open(stl_file *stl, const char *file)
{
stl_initialize(stl);
stl_count_facets(stl, file);
stl_allocate(stl);
stl_read(stl, 0, true);
if (stl->fp != nullptr) {
fclose(stl->fp);
stl->fp = nullptr;
}
}
void
stl_initialize(stl_file *stl) {
memset(stl, 0, sizeof(stl_file));
stl->stats.volume = -1.0;
void stl_initialize(stl_file *stl)
{
stl->fp = nullptr;
stl->tail = nullptr;
stl->M = 0;
stl->error = 0;
stl->facet_start.clear();
stl->neighbors_start.clear();
stl->v_indices.clear();
stl->v_shared.clear();
memset(&stl->stats, 0, sizeof(stl_stats));
stl->stats.volume = -1.0;
}
void stl_close(stl_file *stl)
{
assert(stl->fp == nullptr);
assert(stl->heads.empty());
assert(stl->tail == nullptr);
stl_initialize(stl);
}
#ifndef BOOST_LITTLE_ENDIAN
@ -175,20 +191,14 @@ stl_count_facets(stl_file *stl, const char *file) {
stl->stats.original_num_facets = stl->stats.number_of_facets;
}
void
stl_allocate(stl_file *stl) {
if (stl->error) return;
/* Allocate memory for the entire .STL file */
stl->facet_start = (stl_facet*)calloc(stl->stats.number_of_facets,
sizeof(stl_facet));
if(stl->facet_start == NULL) perror("stl_initialize");
stl->stats.facets_malloced = stl->stats.number_of_facets;
/* Allocate memory for the neighbors list */
stl->neighbors_start = (stl_neighbors*)
calloc(stl->stats.number_of_facets, sizeof(stl_neighbors));
if(stl->facet_start == NULL) perror("stl_initialize");
void stl_allocate(stl_file *stl)
{
if (stl->error)
return;
// Allocate memory for the entire .STL file.
stl->facet_start.assign(stl->stats.number_of_facets, stl_facet());
// Allocate memory for the neighbors list.
stl->neighbors_start.assign(stl->stats.number_of_facets, stl_neighbors());
}
void
@ -237,23 +247,14 @@ stl_open_merge(stl_file *stl, char *file_to_merge) {
stl->fp=origFp;
}
extern void
stl_reallocate(stl_file *stl) {
if (stl->error) return;
/* Reallocate more memory for the .STL file(s) */
stl->facet_start = (stl_facet*)realloc(stl->facet_start, stl->stats.number_of_facets *
sizeof(stl_facet));
if(stl->facet_start == NULL) perror("stl_initialize");
stl->stats.facets_malloced = stl->stats.number_of_facets;
/* Reallocate more memory for the neighbors list */
stl->neighbors_start = (stl_neighbors*)
realloc(stl->neighbors_start, stl->stats.number_of_facets *
sizeof(stl_neighbors));
if(stl->facet_start == NULL) perror("stl_initialize");
void stl_reallocate(stl_file *stl)
{
if (stl->error)
return;
stl->facet_start.resize(stl->stats.number_of_facets);
stl->neighbors_start.resize(stl->stats.number_of_facets);
}
/* Reads the contents of the file pointed to by stl->fp into the stl structure,
starting at facet first_facet. The second argument says if it's our first
time running this for the stl and therefore we should reset our max and min stats. */
@ -366,20 +367,3 @@ void stl_facet_stats(stl_file *stl, stl_facet facet, bool &first)
stl->stats.max = stl->stats.max.cwiseMax(facet.vertex[i]);
}
}
void stl_close(stl_file *stl)
{
assert(stl->fp == nullptr);
assert(stl->heads == nullptr);
assert(stl->tail == nullptr);
if (stl->facet_start != NULL)
free(stl->facet_start);
if (stl->neighbors_start != NULL)
free(stl->neighbors_start);
if (stl->v_indices != NULL)
free(stl->v_indices);
if (stl->v_shared != NULL)
free(stl->v_shared);
memset(stl, 0, sizeof(stl_file));
}

View file

@ -32,45 +32,39 @@ static float get_area(stl_facet *facet);
static float get_volume(stl_file *stl);
void
stl_verify_neighbors(stl_file *stl) {
int i;
int j;
stl_edge edge_a;
stl_edge edge_b;
int neighbor;
int vnot;
void stl_verify_neighbors(stl_file *stl)
{
if (stl->error)
return;
if (stl->error) return;
stl->stats.backwards_edges = 0;
stl->stats.backwards_edges = 0;
for(i = 0; i < stl->stats.number_of_facets; i++) {
for(j = 0; j < 3; j++) {
edge_a.p1 = stl->facet_start[i].vertex[j];
edge_a.p2 = stl->facet_start[i].vertex[(j + 1) % 3];
neighbor = stl->neighbors_start[i].neighbor[j];
vnot = stl->neighbors_start[i].which_vertex_not[j];
if(neighbor == -1)
continue; /* this edge has no neighbor... Continue. */
if(vnot < 3) {
edge_b.p1 = stl->facet_start[neighbor].vertex[(vnot + 2) % 3];
edge_b.p2 = stl->facet_start[neighbor].vertex[(vnot + 1) % 3];
} else {
stl->stats.backwards_edges += 1;
edge_b.p1 = stl->facet_start[neighbor].vertex[(vnot + 1) % 3];
edge_b.p2 = stl->facet_start[neighbor].vertex[(vnot + 2) % 3];
}
if (edge_a.p1 != edge_b.p1 || edge_a.p2 != edge_b.p2) {
/* These edges should match but they don't. Print results. */
printf("edge %d of facet %d doesn't match edge %d of facet %d\n",
j, i, vnot + 1, neighbor);
stl_write_facet(stl, (char*)"first facet", i);
stl_write_facet(stl, (char*)"second facet", neighbor);
}
}
}
for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) {
for (int j = 0; j < 3; ++ j) {
stl_edge edge_a;
edge_a.p1 = stl->facet_start[i].vertex[j];
edge_a.p2 = stl->facet_start[i].vertex[(j + 1) % 3];
int neighbor = stl->neighbors_start[i].neighbor[j];
if (neighbor == -1)
continue; // this edge has no neighbor... Continue.
int vnot = stl->neighbors_start[i].which_vertex_not[j];
stl_edge edge_b;
if (vnot < 3) {
edge_b.p1 = stl->facet_start[neighbor].vertex[(vnot + 2) % 3];
edge_b.p2 = stl->facet_start[neighbor].vertex[(vnot + 1) % 3];
} else {
stl->stats.backwards_edges += 1;
edge_b.p1 = stl->facet_start[neighbor].vertex[(vnot + 1) % 3];
edge_b.p2 = stl->facet_start[neighbor].vertex[(vnot + 2) % 3];
}
if (edge_a.p1 != edge_b.p1 || edge_a.p2 != edge_b.p2) {
// These edges should match but they don't. Print results.
printf("edge %d of facet %d doesn't match edge %d of facet %d\n", j, i, vnot + 1, neighbor);
stl_write_facet(stl, (char*)"first facet", i);
stl_write_facet(stl, (char*)"second facet", neighbor);
}
}
}
}
void stl_translate(stl_file *stl, float x, float y, float z)
@ -263,21 +257,19 @@ void stl_mirror_yz(stl_file *stl)
void stl_mirror_xz(stl_file *stl)
{
if (stl->error)
return;
if (stl->error)
return;
for (int i = 0; i < stl->stats.number_of_facets; i++) {
for (int j = 0; j < 3; j++) {
stl->facet_start[i].vertex[j](1) *= -1.0;
}
}
float temp_size = stl->stats.min(1);
stl->stats.min(1) = stl->stats.max(1);
stl->stats.max(1) = temp_size;
stl->stats.min(1) *= -1.0;
stl->stats.max(1) *= -1.0;
stl_reverse_all_facets(stl);
stl->stats.facets_reversed -= stl->stats.number_of_facets; /* for not altering stats */
for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i)
for (int j = 0; j < 3; ++ j)
stl->facet_start[i].vertex[j](1) *= -1.0;
float temp_size = stl->stats.min(1);
stl->stats.min(1) = stl->stats.max(1);
stl->stats.max(1) = temp_size;
stl->stats.min(1) *= -1.0;
stl->stats.max(1) *= -1.0;
stl_reverse_all_facets(stl);
stl->stats.facets_reversed -= stl->stats.number_of_facets; // for not altering stats
}
static float get_volume(stl_file *stl)
@ -463,18 +455,18 @@ bool stl_validate(stl_file *stl)
{
assert(! stl->error);
assert(stl->fp == nullptr);
assert(stl->facet_start != nullptr);
assert(stl->heads == nullptr);
assert(! stl->facet_start.empty());
assert(stl->heads.empty());
assert(stl->tail == nullptr);
assert(stl->neighbors_start != nullptr);
assert((stl->v_indices == nullptr) == (stl->v_shared == nullptr));
assert(! stl->neighbors_start.empty());
assert((stl->v_indices.empty()) == (stl->v_shared.empty()));
assert(stl->stats.number_of_facets > 0);
#ifdef _DEBUG
// Verify validity of neighborship data.
for (int facet_idx = 0; facet_idx < (int)stl->stats.number_of_facets; ++ facet_idx) {
const stl_neighbors &nbr = stl->neighbors_start[facet_idx];
const int *vertices = (stl->v_indices == nullptr) ? nullptr : stl->v_indices[facet_idx].vertex;
const int *vertices = (stl->v_indices.empty()) ? nullptr : stl->v_indices[facet_idx].vertex;
for (int nbr_idx = 0; nbr_idx < 3; ++ nbr_idx) {
int nbr_face = stl->neighbors_start[facet_idx].neighbor[nbr_idx];
assert(nbr_face < (int)stl->stats.number_of_facets);