2013-07-06 13:26:32 +00:00
|
|
|
#ifndef slic3r_TriangleMesh_hpp_
|
|
|
|
#define slic3r_TriangleMesh_hpp_
|
|
|
|
|
2015-12-07 23:39:54 +00:00
|
|
|
#include "libslic3r.h"
|
2013-06-24 17:35:49 +00:00
|
|
|
#include <admesh/stl.h>
|
2018-03-28 15:05:31 +00:00
|
|
|
#include <functional>
|
2013-09-07 12:06:09 +00:00
|
|
|
#include <vector>
|
2014-01-06 17:29:10 +00:00
|
|
|
#include "BoundingBox.hpp"
|
2015-01-06 19:58:07 +00:00
|
|
|
#include "Line.hpp"
|
2013-08-05 17:52:37 +00:00
|
|
|
#include "Point.hpp"
|
2013-09-07 12:06:09 +00:00
|
|
|
#include "Polygon.hpp"
|
2013-11-23 18:41:40 +00:00
|
|
|
#include "ExPolygon.hpp"
|
2013-06-24 17:35:49 +00:00
|
|
|
|
2013-07-07 20:36:14 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
2013-09-09 20:27:58 +00:00
|
|
|
class TriangleMesh;
|
2014-01-15 19:31:38 +00:00
|
|
|
class TriangleMeshSlicer;
|
2013-09-09 20:27:58 +00:00
|
|
|
typedef std::vector<TriangleMesh*> TriangleMeshPtrs;
|
|
|
|
|
2013-06-24 17:35:49 +00:00
|
|
|
class TriangleMesh
|
|
|
|
{
|
2017-02-26 21:17:39 +00:00
|
|
|
public:
|
2019-06-13 14:33:50 +00:00
|
|
|
TriangleMesh() : repaired(false) {}
|
2020-03-25 10:07:26 +00:00
|
|
|
TriangleMesh(const Pointf3s &points, const std::vector<Vec3i> &facets);
|
2020-01-23 09:57:51 +00:00
|
|
|
explicit TriangleMesh(const indexed_triangle_set &M);
|
2021-05-17 18:25:59 +00:00
|
|
|
void clear() { this->stl.clear(); this->its.clear(); this->repaired = false; }
|
2019-06-10 15:17:36 +00:00
|
|
|
bool ReadSTLFile(const char* input_file) { return stl_open(&stl, input_file); }
|
|
|
|
bool write_ascii(const char* output_file) { return stl_write_ascii(&this->stl, output_file, ""); }
|
|
|
|
bool write_binary(const char* output_file) { return stl_write_binary(&this->stl, output_file, ""); }
|
2019-06-11 15:08:47 +00:00
|
|
|
void repair(bool update_shared_vertices = true);
|
2017-08-02 14:05:18 +00:00
|
|
|
float volume();
|
|
|
|
void check_topology();
|
2018-12-07 13:10:16 +00:00
|
|
|
bool is_manifold() const { return this->stl.stats.connected_facets_3_edge == (int)this->stl.stats.number_of_facets; }
|
2019-10-03 12:23:03 +00:00
|
|
|
void WriteOBJFile(const char* output_file) const;
|
2013-08-04 19:34:26 +00:00
|
|
|
void scale(float factor);
|
2018-08-21 15:43:05 +00:00
|
|
|
void scale(const Vec3d &versor);
|
2013-08-05 08:48:38 +00:00
|
|
|
void translate(float x, float y, float z);
|
2019-04-05 08:08:34 +00:00
|
|
|
void translate(const Vec3f &displacement);
|
2015-04-16 19:22:04 +00:00
|
|
|
void rotate(float angle, const Axis &axis);
|
2018-08-27 12:00:53 +00:00
|
|
|
void rotate(float angle, const Vec3d& axis);
|
2018-08-22 13:34:03 +00:00
|
|
|
void rotate_x(float angle) { this->rotate(angle, X); }
|
|
|
|
void rotate_y(float angle) { this->rotate(angle, Y); }
|
|
|
|
void rotate_z(float angle) { this->rotate(angle, Z); }
|
2015-11-04 22:11:30 +00:00
|
|
|
void mirror(const Axis &axis);
|
2018-08-22 13:34:03 +00:00
|
|
|
void mirror_x() { this->mirror(X); }
|
|
|
|
void mirror_y() { this->mirror(Y); }
|
|
|
|
void mirror_z() { this->mirror(Z); }
|
2019-04-03 09:12:03 +00:00
|
|
|
void transform(const Transform3d& t, bool fix_left_handed = false);
|
2021-05-17 18:25:59 +00:00
|
|
|
void transform(const Matrix3d& t, bool fix_left_handed = false);
|
2013-08-05 17:22:33 +00:00
|
|
|
void align_to_origin();
|
2013-08-05 17:52:37 +00:00
|
|
|
void rotate(double angle, Point* center);
|
2013-09-09 20:27:58 +00:00
|
|
|
TriangleMeshPtrs split() const;
|
2014-08-03 18:33:16 +00:00
|
|
|
void merge(const TriangleMesh &mesh);
|
2015-01-19 17:53:04 +00:00
|
|
|
ExPolygons horizontal_projection() const;
|
2019-06-04 20:06:42 +00:00
|
|
|
const float* first_vertex() const { return this->stl.facet_start.empty() ? nullptr : &this->stl.facet_start.front().vertex[0](0); }
|
2019-01-14 10:06:52 +00:00
|
|
|
// 2D convex hull of a 3D mesh projected into the Z=0 plane.
|
2015-01-19 17:53:04 +00:00
|
|
|
Polygon convex_hull();
|
2014-09-21 08:51:36 +00:00
|
|
|
BoundingBoxf3 bounding_box() const;
|
2018-08-23 13:37:38 +00:00
|
|
|
// Returns the bbox of this TriangleMesh transformed by the given transformation
|
2019-01-26 17:51:34 +00:00
|
|
|
BoundingBoxf3 transformed_bounding_box(const Transform3d &trafo) const;
|
2019-10-15 11:49:28 +00:00
|
|
|
// 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.
|
2021-05-17 18:25:59 +00:00
|
|
|
Vec3d center() const { return this->bounding_box().center(); }
|
2018-08-15 10:50:06 +00:00
|
|
|
// Returns the convex hull of this TriangleMesh
|
|
|
|
TriangleMesh convex_hull_3d() const;
|
2019-10-15 11:49:28 +00:00
|
|
|
// Slice this mesh at the provided Z levels and return the vector
|
2021-05-20 07:09:19 +00:00
|
|
|
std::vector<ExPolygons> slice(const std::vector<double>& z) const;
|
2014-04-25 15:14:39 +00:00
|
|
|
void reset_repair_stats();
|
2014-08-08 19:48:59 +00:00
|
|
|
bool needed_repair() const;
|
2019-04-15 19:58:13 +00:00
|
|
|
void require_shared_vertices();
|
2019-06-10 16:30:54 +00:00
|
|
|
bool has_shared_vertices() const { return ! this->its.vertices.empty(); }
|
2018-08-22 13:34:03 +00:00
|
|
|
size_t facets_count() const { return this->stl.stats.number_of_facets; }
|
2018-11-17 16:23:56 +00:00
|
|
|
bool empty() const { return this->facets_count() == 0; }
|
2019-04-01 10:27:45 +00:00
|
|
|
bool is_splittable() const;
|
2019-07-18 09:51:06 +00:00
|
|
|
// Estimate of the memory occupied by this structure, important for keeping an eye on the Undo / Redo stack allocation.
|
2019-07-17 13:48:53 +00:00
|
|
|
size_t memsize() const;
|
2019-07-18 09:51:06 +00:00
|
|
|
// Release optional data from the mesh if the object is on the Undo / Redo stack only. Returns the amount of memory released.
|
|
|
|
size_t release_optional();
|
2021-05-17 18:25:59 +00:00
|
|
|
// Restore optional data possibly released by release_optional().
|
|
|
|
void restore_optional();
|
2017-02-26 21:17:39 +00:00
|
|
|
|
2019-01-14 10:06:52 +00:00
|
|
|
stl_file stl;
|
2019-06-10 16:30:54 +00:00
|
|
|
indexed_triangle_set its;
|
2013-09-09 21:09:56 +00:00
|
|
|
bool repaired;
|
2019-04-15 19:58:13 +00:00
|
|
|
|
2017-02-26 21:17:39 +00:00
|
|
|
private:
|
2019-04-01 12:50:40 +00:00
|
|
|
std::deque<uint32_t> find_unvisited_neighbors(std::vector<unsigned char> &facet_visited) const;
|
2013-06-24 17:35:49 +00:00
|
|
|
};
|
|
|
|
|
2021-06-02 13:45:11 +00:00
|
|
|
// Index of face indices incident with a vertex index.
|
|
|
|
struct VertexFaceIndex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using iterator = std::vector<size_t>::const_iterator;
|
|
|
|
|
|
|
|
VertexFaceIndex(const indexed_triangle_set &its) { this->create(its); }
|
|
|
|
VertexFaceIndex() {}
|
|
|
|
|
|
|
|
void create(const indexed_triangle_set &its);
|
|
|
|
void clear() { m_vertex_to_face_start.clear(); m_vertex_faces_all.clear(); }
|
|
|
|
|
|
|
|
// Iterators of face indices incident with the input vertex_id.
|
|
|
|
iterator begin(size_t vertex_id) const throw() { return m_vertex_faces_all.begin() + m_vertex_to_face_start[vertex_id]; }
|
|
|
|
iterator end (size_t vertex_id) const throw() { return m_vertex_faces_all.begin() + m_vertex_to_face_start[vertex_id + 1]; }
|
|
|
|
// Vertex incidence.
|
|
|
|
size_t count(size_t vertex_id) const throw() { return m_vertex_to_face_start[vertex_id + 1] - m_vertex_to_face_start[vertex_id]; }
|
|
|
|
|
|
|
|
const Range<iterator> operator[](size_t vertex_id) const { return {begin(vertex_id), end(vertex_id)}; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<size_t> m_vertex_to_face_start;
|
|
|
|
std::vector<size_t> m_vertex_faces_all;
|
|
|
|
};
|
2021-03-03 08:29:13 +00:00
|
|
|
|
2021-05-18 13:05:23 +00:00
|
|
|
// Map from a face edge to a unique edge identifier or -1 if no neighbor exists.
|
|
|
|
// Two neighbor faces share a unique edge identifier even if they are flipped.
|
|
|
|
// Used for chaining slice lines into polygons.
|
2021-06-20 13:21:12 +00:00
|
|
|
std::vector<Vec3i> its_face_edge_ids(const indexed_triangle_set &its);
|
|
|
|
std::vector<Vec3i> its_face_edge_ids(const indexed_triangle_set &its, std::function<void()> throw_on_cancel_callback);
|
2021-07-26 15:02:56 +00:00
|
|
|
std::vector<Vec3i> its_face_edge_ids(const indexed_triangle_set &its, const std::vector<bool> &face_mask);
|
2021-06-20 13:21:12 +00:00
|
|
|
// Having the face neighbors available, assign unique edge IDs to face edges for chaining of polygons over slices.
|
|
|
|
std::vector<Vec3i> its_face_edge_ids(const indexed_triangle_set &its, std::vector<Vec3i> &face_neighbors, bool assign_unbound_edges = false, int *num_edges = nullptr);
|
2021-05-18 15:57:35 +00:00
|
|
|
|
2021-06-04 12:11:39 +00:00
|
|
|
// Create index that gives neighbor faces for each face. Ignores face orientations.
|
2021-06-20 13:21:12 +00:00
|
|
|
std::vector<Vec3i> its_face_neighbors(const indexed_triangle_set &its);
|
|
|
|
std::vector<Vec3i> its_face_neighbors_par(const indexed_triangle_set &its);
|
2021-06-04 12:11:39 +00:00
|
|
|
|
2021-05-21 15:57:37 +00:00
|
|
|
// After applying a transformation with negative determinant, flip the faces to keep the transformed mesh volume positive.
|
|
|
|
void its_flip_triangles(indexed_triangle_set &its);
|
|
|
|
|
2021-05-18 15:57:35 +00:00
|
|
|
// Merge duplicate vertices, return number of vertices removed.
|
|
|
|
// This function will happily create non-manifolds if more than two faces share the same vertex position
|
|
|
|
// or more than two faces share the same edge position!
|
|
|
|
int its_merge_vertices(indexed_triangle_set &its, bool shrink_to_fit = true);
|
|
|
|
|
2021-05-17 18:25:59 +00:00
|
|
|
// Remove degenerate faces, return number of faces removed.
|
|
|
|
int its_remove_degenerate_faces(indexed_triangle_set &its, bool shrink_to_fit = true);
|
2021-05-18 15:57:35 +00:00
|
|
|
|
2021-05-17 18:25:59 +00:00
|
|
|
// Remove vertices, which none of the faces references. Return number of freed vertices.
|
|
|
|
int its_compactify_vertices(indexed_triangle_set &its, bool shrink_to_fit = true);
|
2021-05-18 15:57:35 +00:00
|
|
|
|
2021-08-16 09:53:37 +00:00
|
|
|
// store part of index triangle set
|
|
|
|
bool its_store_triangle(const indexed_triangle_set &its, const char *obj_filename, size_t triangle_index);
|
|
|
|
bool its_store_triangles(const indexed_triangle_set &its, const char *obj_filename, const std::vector<size_t>& triangles);
|
|
|
|
|
2021-06-01 13:49:19 +00:00
|
|
|
std::vector<indexed_triangle_set> its_split(const indexed_triangle_set &its);
|
2021-05-28 12:55:25 +00:00
|
|
|
|
2021-06-01 13:49:19 +00:00
|
|
|
bool its_is_splittable(const indexed_triangle_set &its);
|
2021-05-26 14:41:34 +00:00
|
|
|
|
2021-05-18 15:57:35 +00:00
|
|
|
// Shrink the vectors of its.vertices and its.faces to a minimum size by reallocating the two vectors.
|
2021-05-17 18:25:59 +00:00
|
|
|
void its_shrink_to_fit(indexed_triangle_set &its);
|
2019-12-05 12:38:04 +00:00
|
|
|
|
2021-05-19 11:39:56 +00:00
|
|
|
// For convex hull calculation: Transform mesh, trim it by the Z plane and collect all vertices. Duplicate vertices will be produced.
|
|
|
|
void its_collect_mesh_projection_points_above(const indexed_triangle_set &its, const Matrix3f &m, const float z, Points &all_pts);
|
|
|
|
void its_collect_mesh_projection_points_above(const indexed_triangle_set &its, const Transform3f &t, const float z, Points &all_pts);
|
|
|
|
|
|
|
|
// Calculate 2D convex hull of a transformed and clipped mesh. Uses the function above.
|
2021-05-19 09:35:27 +00:00
|
|
|
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);
|
|
|
|
|
2021-06-20 13:21:12 +00:00
|
|
|
// Index of a vertex inside triangle_indices.
|
|
|
|
inline int its_triangle_vertex_index(const stl_triangle_vertex_indices &triangle_indices, int vertex_idx)
|
|
|
|
{
|
|
|
|
return vertex_idx == triangle_indices[0] ? 0 :
|
|
|
|
vertex_idx == triangle_indices[1] ? 1 :
|
|
|
|
vertex_idx == triangle_indices[2] ? 2 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Vec2i its_triangle_edge(const stl_triangle_vertex_indices &triangle_indices, int edge_idx)
|
|
|
|
{
|
|
|
|
int next_edge_idx = (edge_idx == 2) ? 0 : edge_idx + 1;
|
|
|
|
return { triangle_indices[edge_idx], triangle_indices[next_edge_idx] };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index of an edge inside triangle.
|
|
|
|
inline int its_triangle_edge_index(const stl_triangle_vertex_indices &triangle_indices, const Vec2i &triangle_edge)
|
|
|
|
{
|
|
|
|
return triangle_edge(0) == triangle_indices[0] && triangle_edge(1) == triangle_indices[1] ? 0 :
|
|
|
|
triangle_edge(0) == triangle_indices[1] && triangle_edge(1) == triangle_indices[2] ? 1 :
|
|
|
|
triangle_edge(0) == triangle_indices[2] && triangle_edge(1) == triangle_indices[0] ? 2 : -1;
|
|
|
|
}
|
|
|
|
|
2021-05-21 12:08:05 +00:00
|
|
|
using its_triangle = std::array<stl_vertex, 3>;
|
|
|
|
|
|
|
|
inline its_triangle its_triangle_vertices(const indexed_triangle_set &its,
|
|
|
|
size_t face_id)
|
|
|
|
{
|
|
|
|
return {its.vertices[its.indices[face_id](0)],
|
|
|
|
its.vertices[its.indices[face_id](1)],
|
|
|
|
its.vertices[its.indices[face_id](2)]};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline stl_normal its_unnormalized_normal(const indexed_triangle_set &its,
|
|
|
|
size_t face_id)
|
|
|
|
{
|
|
|
|
its_triangle tri = its_triangle_vertices(its, face_id);
|
|
|
|
return (tri[1] - tri[0]).cross(tri[2] - tri[0]);
|
|
|
|
}
|
|
|
|
|
2021-05-26 14:41:34 +00:00
|
|
|
float its_volume(const indexed_triangle_set &its);
|
|
|
|
|
2021-05-21 12:08:05 +00:00
|
|
|
void its_merge(indexed_triangle_set &A, const indexed_triangle_set &B);
|
|
|
|
void its_merge(indexed_triangle_set &A, const std::vector<Vec3f> &triangles);
|
|
|
|
void its_merge(indexed_triangle_set &A, const Pointf3s &triangles);
|
|
|
|
|
2021-06-01 15:52:11 +00:00
|
|
|
indexed_triangle_set its_make_cube(double x, double y, double z);
|
2016-11-27 22:06:45 +00:00
|
|
|
TriangleMesh make_cube(double x, double y, double z);
|
2021-06-01 15:52:11 +00:00
|
|
|
|
|
|
|
// Generate a TriangleMesh of a cylinder
|
|
|
|
indexed_triangle_set its_make_cylinder(double r, double h, double fa=(2*PI/360));
|
2016-11-28 01:15:27 +00:00
|
|
|
TriangleMesh make_cylinder(double r, double h, double fa=(2*PI/360));
|
2021-06-01 15:52:11 +00:00
|
|
|
|
|
|
|
indexed_triangle_set its_make_sphere(double rho, double fa=(2*PI/360));
|
2021-03-12 09:29:17 +00:00
|
|
|
TriangleMesh make_cone(double r, double h, double fa=(2*PI/360));
|
2016-12-05 22:43:55 +00:00
|
|
|
TriangleMesh make_sphere(double rho, double fa=(2*PI/360));
|
|
|
|
|
2021-05-21 12:08:05 +00:00
|
|
|
inline BoundingBoxf3 bounding_box(const TriangleMesh &m) { return m.bounding_box(); }
|
|
|
|
inline BoundingBoxf3 bounding_box(const indexed_triangle_set& its)
|
|
|
|
{
|
|
|
|
if (its.vertices.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Vec3f bmin = its.vertices.front(), bmax = its.vertices.front();
|
|
|
|
|
|
|
|
for (const Vec3f &p : its.vertices) {
|
|
|
|
bmin = p.cwiseMin(bmin);
|
|
|
|
bmax = p.cwiseMax(bmax);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {bmin.cast<double>(), bmax.cast<double>()};
|
|
|
|
}
|
|
|
|
|
2013-07-07 20:36:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 14:42:23 +00:00
|
|
|
// Serialization through the Cereal library
|
2019-07-04 21:34:18 +00:00
|
|
|
#include <cereal/access.hpp>
|
2019-07-02 14:42:23 +00:00
|
|
|
namespace cereal {
|
2021-05-17 18:25:59 +00:00
|
|
|
template <class Archive> struct specialize<Archive, Slic3r::TriangleMesh, cereal::specialization::non_member_load_save> {};
|
|
|
|
template<class Archive> void load(Archive &archive, Slic3r::TriangleMesh &mesh) {
|
2019-07-02 14:42:23 +00:00
|
|
|
stl_file &stl = mesh.stl;
|
|
|
|
stl.stats.type = inmemory;
|
2021-05-17 18:25:59 +00:00
|
|
|
archive(stl.stats.number_of_facets, stl.stats.original_num_facets);
|
2019-07-02 14:42:23 +00:00
|
|
|
stl_allocate(&stl);
|
2021-05-17 18:25:59 +00:00
|
|
|
archive.loadBinary((char*)stl.facet_start.data(), stl.facet_start.size() * 50);
|
2019-07-02 14:42:23 +00:00
|
|
|
stl_get_size(&stl);
|
|
|
|
mesh.repair();
|
2021-05-17 18:25:59 +00:00
|
|
|
}
|
|
|
|
template<class Archive> void save(Archive &archive, const Slic3r::TriangleMesh &mesh) {
|
|
|
|
const stl_file& stl = mesh.stl;
|
|
|
|
archive(stl.stats.number_of_facets, stl.stats.original_num_facets);
|
|
|
|
archive.saveBinary((char*)stl.facet_start.data(), stl.facet_start.size() * 50);
|
|
|
|
}
|
2019-07-02 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 13:26:32 +00:00
|
|
|
#endif
|