Ported test_trianglemesh from upstream slic3r, thanks @lordofhyphens

This commit is contained in:
bubnikv 2019-10-15 13:49:28 +02:00
parent 67e1eba8e6
commit c99e7cb0df
7 changed files with 506 additions and 9 deletions

View file

@ -134,12 +134,30 @@ inline bool is_approx(const Point &p1, const Point &p2, coord_t epsilon = coord_
return d.x() < epsilon && d.y() < epsilon;
}
inline bool is_approx(const Vec2f &p1, const Vec2f &p2, float epsilon = float(EPSILON))
{
Vec2f d = (p2 - p1).cwiseAbs();
return d.x() < epsilon && d.y() < epsilon;
}
inline bool is_approx(const Vec2d &p1, const Vec2d &p2, double epsilon = EPSILON)
{
Vec2d d = (p2 - p1).cwiseAbs();
return d.x() < epsilon && d.y() < epsilon;
}
inline bool is_approx(const Vec3f &p1, const Vec3f &p2, float epsilon = float(EPSILON))
{
Vec3f d = (p2 - p1).cwiseAbs();
return d.x() < epsilon && d.y() < epsilon && d.z() < epsilon;
}
inline bool is_approx(const Vec3d &p1, const Vec3d &p2, double epsilon = EPSILON)
{
Vec3d d = (p2 - p1).cwiseAbs();
return d.x() < epsilon && d.y() < epsilon && d.z() < epsilon;
}
namespace int128 {
// Exact orientation predicate,
// returns +1: CCW, 0: collinear, -1: CW.

View file

@ -593,6 +593,16 @@ TriangleMesh TriangleMesh::convex_hull_3d() const
return output_mesh;
}
std::vector<ExPolygons> TriangleMesh::slice(const std::vector<double> &z)
{
// convert doubles to floats
std::vector<float> z_f(z.begin(), z.end());
TriangleMeshSlicer mslicer(this);
std::vector<ExPolygons> layers;
mslicer.slice(z_f, 0.0004f, &layers, [](){});
return layers;
}
void TriangleMesh::require_shared_vertices()
{
BOOST_LOG_TRIVIAL(trace) << "TriangleMeshSlicer::require_shared_vertices - start";
@ -1861,7 +1871,8 @@ void TriangleMeshSlicer::cut(float z, TriangleMesh* upper, TriangleMesh* lower)
}
// Generate the vertex list for a cube solid of arbitrary size in X/Y/Z.
TriangleMesh make_cube(double x, double y, double z) {
TriangleMesh make_cube(double x, double y, double z)
{
Vec3d pv[8] = {
Vec3d(x, y, 0), Vec3d(x, 0, 0), Vec3d(0, 0, 0),
Vec3d(0, y, 0), Vec3d(x, y, z), Vec3d(0, y, z),
@ -1878,7 +1889,8 @@ TriangleMesh make_cube(double x, double y, double z) {
Pointf3s vertices(&pv[0], &pv[0]+8);
TriangleMesh mesh(vertices ,facets);
return mesh;
mesh.repair();
return mesh;
}
// Generate the mesh for a cylinder and return it, using
@ -1922,7 +1934,9 @@ TriangleMesh make_cylinder(double r, double h, double fa)
facets.emplace_back(Vec3crd(id, 2, 3));
facets.emplace_back(Vec3crd(id, id - 1, 2));
return TriangleMesh(std::move(vertices), std::move(facets));
TriangleMesh mesh(std::move(vertices), std::move(facets));
mesh.repair();
return mesh;
}
// Generates mesh for a sphere centered about the origin, using the generated angle
@ -1978,7 +1992,9 @@ TriangleMesh make_sphere(double radius, double fa)
k2 = k2_next;
}
}
return TriangleMesh(std::move(vertices), std::move(facets));
TriangleMesh mesh(std::move(vertices), std::move(facets));
mesh.repair();
return mesh;
}
}

View file

@ -58,8 +58,14 @@ public:
BoundingBoxf3 bounding_box() const;
// Returns the bbox of this TriangleMesh transformed by the given transformation
BoundingBoxf3 transformed_bounding_box(const Transform3d &trafo) const;
// Return the size of the mesh in coordinates.
Vec3d size() const { return stl.stats.size.cast<double>(); }
/// Return the center of the related bounding box.
Vec3d center() const { return this->bounding_box().center(); }
// Returns the convex hull of this TriangleMesh
TriangleMesh convex_hull_3d() const;
// Slice this mesh at the provided Z levels and return the vector
std::vector<ExPolygons> slice(const std::vector<double>& z);
void reset_repair_stats();
bool needed_repair() const;
void require_shared_vertices();