cut_mesh(): new parameter to optionally not triangulate the caps.

This commit is contained in:
Vojtech Bubnik 2021-05-19 13:52:47 +02:00
parent 66cf7ea9d3
commit e952aded78
2 changed files with 33 additions and 28 deletions

View file

@ -1179,6 +1179,7 @@ std::vector<ExPolygons> slice_mesh_ex(
return layers; return layers;
} }
// Remove duplicates of slice_vertices, optionally triangulate the cut.
static void triangulate_slice( static void triangulate_slice(
indexed_triangle_set &its, indexed_triangle_set &its,
IntersectionLines &lines, IntersectionLines &lines,
@ -1186,7 +1187,8 @@ static void triangulate_slice(
// Vertices of the original (unsliced) mesh. Newly added vertices are those on the slice. // Vertices of the original (unsliced) mesh. Newly added vertices are those on the slice.
int num_original_vertices, int num_original_vertices,
// Z height of the slice. // Z height of the slice.
float z) float z,
bool triangulate)
{ {
sort_remove_duplicates(slice_vertices); sort_remove_duplicates(slice_vertices);
@ -1230,33 +1232,35 @@ static void triangulate_slice(
f(i) = map_duplicate_vertex[f(i) - num_original_vertices]; f(i) = map_duplicate_vertex[f(i) - num_original_vertices];
} }
size_t idx_vertex_new_first = its.vertices.size(); if (triangulate) {
Pointf3s triangles = triangulate_expolygons_3d(make_expolygons_simple(lines), z, true); size_t idx_vertex_new_first = its.vertices.size();
for (size_t i = 0; i < triangles.size(); ) { Pointf3s triangles = triangulate_expolygons_3d(make_expolygons_simple(lines), z, true);
stl_triangle_vertex_indices facet; for (size_t i = 0; i < triangles.size(); ) {
for (size_t j = 0; j < 3; ++ j) { stl_triangle_vertex_indices facet;
Vec3f v = triangles[i ++].cast<float>(); for (size_t j = 0; j < 3; ++ j) {
auto it = lower_bound_by_predicate(map_vertex_to_index.begin(), map_vertex_to_index.end(), Vec3f v = triangles[i ++].cast<float>();
[&v](const std::pair<Vec2f, int> &l) { return l.first.x() < v.x() || (l.first.x() == v.x() && l.first.y() < v.y()); }); auto it = lower_bound_by_predicate(map_vertex_to_index.begin(), map_vertex_to_index.end(),
int idx = -1; [&v](const std::pair<Vec2f, int> &l) { return l.first.x() < v.x() || (l.first.x() == v.x() && l.first.y() < v.y()); });
if (it != map_vertex_to_index.end() && it->first.x() == v.x() && it->first.y() == v.y()) int idx = -1;
idx = it->second; if (it != map_vertex_to_index.end() && it->first.x() == v.x() && it->first.y() == v.y())
else { idx = it->second;
// Try to find the vertex in the list of newly added vertices. Those vertices are not matched on the cut and they shall be rare. else {
for (size_t k = idx_vertex_new_first; k < its.vertices.size(); ++ k) // Try to find the vertex in the list of newly added vertices. Those vertices are not matched on the cut and they shall be rare.
if (its.vertices[k] == v) { for (size_t k = idx_vertex_new_first; k < its.vertices.size(); ++ k)
idx = int(k); if (its.vertices[k] == v) {
break; idx = int(k);
break;
}
if (idx == -1) {
idx = int(its.vertices.size());
its.vertices.emplace_back(v);
} }
if (idx == -1) {
idx = int(its.vertices.size());
its.vertices.emplace_back(v);
} }
facet(j) = idx;
} }
facet(j) = idx; if (facet(0) != facet(1) && facet(0) != facet(2) && facet(1) != facet(2))
its.indices.emplace_back(facet);
} }
if (facet(0) != facet(1) && facet(0) != facet(2) && facet(1) != facet(2))
its.indices.emplace_back(facet);
} }
// Remove vertices, which are not referenced by any face. // Remove vertices, which are not referenced by any face.
@ -1266,7 +1270,7 @@ static void triangulate_slice(
// its_remove_degenerate_faces(its); // its_remove_degenerate_faces(its);
} }
void cut_mesh(const indexed_triangle_set &mesh, float z, indexed_triangle_set *upper, indexed_triangle_set *lower) void cut_mesh(const indexed_triangle_set &mesh, float z, indexed_triangle_set *upper, indexed_triangle_set *lower, bool triangulate_caps)
{ {
assert(upper || lower); assert(upper || lower);
if (upper == nullptr && lower == nullptr) if (upper == nullptr && lower == nullptr)
@ -1413,10 +1417,10 @@ void cut_mesh(const indexed_triangle_set &mesh, float z, indexed_triangle_set *u
} }
if (upper != nullptr) if (upper != nullptr)
triangulate_slice(*upper, upper_lines, upper_slice_vertices, int(mesh.vertices.size()), z); triangulate_slice(*upper, upper_lines, upper_slice_vertices, int(mesh.vertices.size()), z, triangulate_caps);
if (lower != nullptr) if (lower != nullptr)
triangulate_slice(*lower, lower_lines, lower_slice_vertices, int(mesh.vertices.size()), z); triangulate_slice(*lower, lower_lines, lower_slice_vertices, int(mesh.vertices.size()), z, triangulate_caps);
} }
} }

View file

@ -76,7 +76,8 @@ void cut_mesh(
const indexed_triangle_set &mesh, const indexed_triangle_set &mesh,
float z, float z,
indexed_triangle_set *upper, indexed_triangle_set *upper,
indexed_triangle_set *lower); indexed_triangle_set *lower,
bool triangulate_caps = true);
} }