New functions: its_convex_hull_2d_above()

This commit is contained in:
Vojtech Bubnik 2021-05-19 11:35:27 +02:00
parent e892550cb0
commit 6b54f34ce1
2 changed files with 36 additions and 0 deletions

View File

@ -899,6 +899,39 @@ void its_shrink_to_fit(indexed_triangle_set &its)
its.vertices.shrink_to_fit();
}
template<typename TransformVertex>
Polygon its_convex_hull_2d_above(const indexed_triangle_set &its, const TransformVertex &transform_fn, const float z)
{
Points all_pts;
for (const stl_triangle_vertex_indices &tri : its.indices) {
const Vec3f pts[3] = { transform_fn(its.vertices[tri(0)]), transform_fn(its.vertices[tri(1)]), transform_fn(its.vertices[tri(2)]) };
int iprev = 3;
for (int iedge = 0; iedge < 3; ++ iedge) {
const Vec3f &p1 = pts[iprev];
const Vec3f &p2 = pts[iedge];
if ((p1.z() < z && p2.z() > z) || (p2.z() < z && p1.z() > z)) {
// Edge crosses the z plane. Calculate intersection point with the plane.
float t = z / (p2.z() - p1.z());
all_pts.emplace_back(scaled<coord_t>(p1.x() + (p2.x() - p1.x()) * t), scaled<coord_t>(p2.x() + (p2.y() - p2.y()) * t));
}
if (p2.z() > z)
all_pts.emplace_back(scaled<coord_t>(p2.x()), scaled<coord_t>(p2.y()));
iprev = iedge;
}
}
return Geometry::convex_hull(std::move(all_pts));
}
Polygon its_convex_hull_2d_above(const indexed_triangle_set &its, const Matrix3f &m, const float z)
{
return its_convex_hull_2d_above(its, [m](const Vec3f &p){ return m * p; }, z);
}
Polygon its_convex_hull_2d_above(const indexed_triangle_set &its, const Transform3f &t, const float z)
{
return its_convex_hull_2d_above(its, [t](const Vec3f &p){ return t * p; }, z);
}
// Generate the vertex list for a cube solid of arbitrary size in X/Y/Z.
TriangleMesh make_cube(double x, double y, double z)
{

View File

@ -113,6 +113,9 @@ int its_compactify_vertices(indexed_triangle_set &its, bool shrink_to_fit = true
// Shrink the vectors of its.vertices and its.faces to a minimum size by reallocating the two vectors.
void its_shrink_to_fit(indexed_triangle_set &its);
Polygon its_convex_hull_2d_above(const indexed_triangle_set &its, const Matrix3f &m, const float z);
Polygon its_convex_hull_2d_above(const indexed_triangle_set &its, const Transform3f &t, const float z);
TriangleMesh make_cube(double x, double y, double z);
// Generate a TriangleMesh of a cylinder