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>
|
2016-11-26 12:45:58 +00:00
|
|
|
#include <boost/thread.hpp>
|
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:
|
2018-08-22 13:34:03 +00:00
|
|
|
TriangleMesh() : repaired(false) { stl_initialize(&this->stl); }
|
2018-08-21 20:14:47 +00:00
|
|
|
TriangleMesh(const Pointf3s &points, const std::vector<Vec3crd> &facets);
|
2018-08-22 13:34:03 +00:00
|
|
|
TriangleMesh(const TriangleMesh &other) : repaired(false) { stl_initialize(&this->stl); *this = other; }
|
|
|
|
TriangleMesh(TriangleMesh &&other) : repaired(false) { stl_initialize(&this->stl); this->swap(other); }
|
|
|
|
~TriangleMesh() { stl_close(&this->stl); }
|
2017-06-15 13:38:15 +00:00
|
|
|
TriangleMesh& operator=(const TriangleMesh &other);
|
2018-08-22 13:34:03 +00:00
|
|
|
TriangleMesh& operator=(TriangleMesh &&other) { this->swap(other); return *this; }
|
|
|
|
void swap(TriangleMesh &other) { std::swap(this->stl, other.stl); std::swap(this->repaired, other.repaired); }
|
|
|
|
void ReadSTLFile(const char* input_file) { stl_open(&stl, input_file); }
|
|
|
|
void write_ascii(const char* output_file) { stl_write_ascii(&this->stl, output_file, ""); }
|
|
|
|
void write_binary(const char* output_file) { stl_write_binary(&this->stl, output_file, ""); }
|
2013-09-09 21:18:33 +00:00
|
|
|
void repair();
|
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-03-13 14:44:50 +00:00
|
|
|
void WriteOBJFile(const char* output_file);
|
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);
|
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); }
|
2018-11-02 12:47:47 +00:00
|
|
|
void transform(const Transform3d& t);
|
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-01-14 10:06:52 +00:00
|
|
|
const float* first_vertex() const { return this->stl.facet_start ? &this->stl.facet_start->vertex[0](0) : nullptr; }
|
|
|
|
// 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;
|
2018-08-15 10:50:06 +00:00
|
|
|
// Returns the convex hull of this TriangleMesh
|
|
|
|
TriangleMesh convex_hull_3d() 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;
|
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; }
|
2017-02-26 21:17:39 +00:00
|
|
|
|
|
|
|
// Returns true, if there are two and more connected patches in the mesh.
|
|
|
|
// Returns false, if one or zero connected patch is in the mesh.
|
|
|
|
bool has_multiple_patches() const;
|
|
|
|
|
|
|
|
// Count disconnected triangle patches.
|
|
|
|
size_t number_of_patches() const;
|
|
|
|
|
2019-01-14 10:06:52 +00:00
|
|
|
stl_file stl;
|
2013-09-09 21:09:56 +00:00
|
|
|
bool repaired;
|
2013-09-13 12:48:40 +00:00
|
|
|
|
2017-02-26 21:17:39 +00:00
|
|
|
private:
|
2013-12-18 15:34:31 +00:00
|
|
|
void require_shared_vertices();
|
2014-01-15 19:31:38 +00:00
|
|
|
friend class TriangleMeshSlicer;
|
2013-06-24 17:35:49 +00:00
|
|
|
};
|
|
|
|
|
2017-02-26 21:17:39 +00:00
|
|
|
enum FacetEdgeType {
|
|
|
|
// A general case, the cutting plane intersect a face at two different edges.
|
2018-09-17 13:12:13 +00:00
|
|
|
feGeneral,
|
2017-02-26 21:17:39 +00:00
|
|
|
// Two vertices are aligned with the cutting plane, the third vertex is below the cutting plane.
|
|
|
|
feTop,
|
|
|
|
// Two vertices are aligned with the cutting plane, the third vertex is above the cutting plane.
|
|
|
|
feBottom,
|
|
|
|
// All three vertices of a face are aligned with the cutting plane.
|
|
|
|
feHorizontal
|
|
|
|
};
|
2013-09-07 19:08:53 +00:00
|
|
|
|
2018-03-15 16:14:13 +00:00
|
|
|
class IntersectionReference
|
2013-09-07 19:08:53 +00:00
|
|
|
{
|
2017-02-26 21:17:39 +00:00
|
|
|
public:
|
2018-03-15 16:14:13 +00:00
|
|
|
IntersectionReference() : point_id(-1), edge_id(-1) {};
|
|
|
|
IntersectionReference(int point_id, int edge_id) : point_id(point_id), edge_id(edge_id) {}
|
2017-02-26 21:17:39 +00:00
|
|
|
// Where is this intersection point located? On mesh vertex or mesh edge?
|
|
|
|
// Only one of the following will be set, the other will remain set to -1.
|
|
|
|
// Index of the mesh vertex.
|
2013-09-07 19:08:53 +00:00
|
|
|
int point_id;
|
2017-02-26 21:17:39 +00:00
|
|
|
// Index of the mesh edge.
|
2013-09-07 19:08:53 +00:00
|
|
|
int edge_id;
|
|
|
|
};
|
|
|
|
|
2018-03-15 16:14:13 +00:00
|
|
|
class IntersectionPoint : public Point, public IntersectionReference
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IntersectionPoint() {};
|
|
|
|
IntersectionPoint(int point_id, int edge_id, const Point &pt) : IntersectionReference(point_id, edge_id), Point(pt) {}
|
|
|
|
IntersectionPoint(const IntersectionReference &ir, const Point &pt) : IntersectionReference(ir), Point(pt) {}
|
|
|
|
// Inherits coord_t x, y
|
|
|
|
};
|
|
|
|
|
2015-01-06 19:58:07 +00:00
|
|
|
class IntersectionLine : public Line
|
2013-09-07 19:08:53 +00:00
|
|
|
{
|
2017-02-26 21:17:39 +00:00
|
|
|
public:
|
2018-09-17 13:12:13 +00:00
|
|
|
IntersectionLine() : a_id(-1), b_id(-1), edge_a_id(-1), edge_b_id(-1), edge_type(feGeneral), flags(0) {}
|
|
|
|
|
|
|
|
bool skip() const { return (this->flags & SKIP) != 0; }
|
|
|
|
void set_skip() { this->flags |= SKIP; }
|
|
|
|
|
|
|
|
bool is_seed_candidate() const { return (this->flags & NO_SEED) == 0 && ! this->skip(); }
|
|
|
|
void set_no_seed(bool set) { if (set) this->flags |= NO_SEED; else this->flags &= ~NO_SEED; }
|
|
|
|
|
2017-02-26 21:17:39 +00:00
|
|
|
// Inherits Point a, b
|
|
|
|
// For each line end point, either {a,b}_id or {a,b}edge_a_id is set, the other is left to -1.
|
|
|
|
// Vertex indices of the line end points.
|
2013-09-07 19:08:53 +00:00
|
|
|
int a_id;
|
|
|
|
int b_id;
|
2017-02-26 21:17:39 +00:00
|
|
|
// Source mesh edges of the line end points.
|
2013-09-07 19:08:53 +00:00
|
|
|
int edge_a_id;
|
|
|
|
int edge_b_id;
|
2018-09-17 13:12:13 +00:00
|
|
|
// feGeneral, feTop, feBottom, feHorizontal
|
2013-09-07 19:08:53 +00:00
|
|
|
FacetEdgeType edge_type;
|
2018-09-17 13:12:13 +00:00
|
|
|
// Used by TriangleMeshSlicer::slice() to skip duplicate edges.
|
|
|
|
enum {
|
|
|
|
// Triangle edge added, because it has no neighbor.
|
|
|
|
EDGE0_NO_NEIGHBOR = 0x001,
|
|
|
|
EDGE1_NO_NEIGHBOR = 0x002,
|
|
|
|
EDGE2_NO_NEIGHBOR = 0x004,
|
|
|
|
// Triangle edge added, because it makes a fold with another horizontal edge.
|
|
|
|
EDGE0_FOLD = 0x010,
|
|
|
|
EDGE1_FOLD = 0x020,
|
|
|
|
EDGE2_FOLD = 0x040,
|
|
|
|
// The edge cannot be a seed of a greedy loop extraction (folds are not safe to become seeds).
|
|
|
|
NO_SEED = 0x100,
|
|
|
|
SKIP = 0x200,
|
|
|
|
};
|
|
|
|
uint32_t flags;
|
2013-09-07 19:08:53 +00:00
|
|
|
};
|
|
|
|
typedef std::vector<IntersectionLine> IntersectionLines;
|
2013-09-07 22:44:01 +00:00
|
|
|
typedef std::vector<IntersectionLine*> IntersectionLinePtrs;
|
2013-09-07 19:08:53 +00:00
|
|
|
|
2014-01-15 19:31:38 +00:00
|
|
|
class TriangleMeshSlicer
|
|
|
|
{
|
2017-02-26 21:17:39 +00:00
|
|
|
public:
|
2018-03-28 15:05:31 +00:00
|
|
|
typedef std::function<void()> throw_on_cancel_callback_type;
|
|
|
|
TriangleMeshSlicer() : mesh(nullptr) {}
|
|
|
|
// Not quite nice, but the constructor and init() methods require non-const mesh pointer to be able to call mesh->require_shared_vertices()
|
2018-03-28 17:47:26 +00:00
|
|
|
TriangleMeshSlicer(TriangleMesh* mesh) { this->init(mesh, [](){}); }
|
2018-03-28 15:05:31 +00:00
|
|
|
void init(TriangleMesh *mesh, throw_on_cancel_callback_type throw_on_cancel);
|
|
|
|
void slice(const std::vector<float> &z, std::vector<Polygons>* layers, throw_on_cancel_callback_type throw_on_cancel) const;
|
2019-03-01 16:53:02 +00:00
|
|
|
void slice(const std::vector<float> &z, const float closing_radius, std::vector<ExPolygons>* layers, throw_on_cancel_callback_type throw_on_cancel) const;
|
2018-09-17 13:12:13 +00:00
|
|
|
enum FacetSliceType {
|
|
|
|
NoSlice = 0,
|
|
|
|
Slicing = 1,
|
|
|
|
Cutting = 2
|
|
|
|
};
|
|
|
|
FacetSliceType slice_facet(float slice_z, const stl_facet &facet, const int facet_idx,
|
2017-02-26 21:17:39 +00:00
|
|
|
const float min_z, const float max_z, IntersectionLine *line_out) const;
|
2016-11-26 12:45:58 +00:00
|
|
|
void cut(float z, TriangleMesh* upper, TriangleMesh* lower) const;
|
2014-01-15 19:31:38 +00:00
|
|
|
|
2017-02-26 21:17:39 +00:00
|
|
|
private:
|
|
|
|
const TriangleMesh *mesh;
|
|
|
|
// Map from a facet to an edge index.
|
|
|
|
std::vector<int> facets_edges;
|
|
|
|
// Scaled copy of this->mesh->stl.v_shared
|
|
|
|
std::vector<stl_vertex> v_scaled_shared;
|
|
|
|
|
2016-11-26 15:07:36 +00:00
|
|
|
void _slice_do(size_t facet_idx, std::vector<IntersectionLines>* lines, boost::mutex* lines_mutex, const std::vector<float> &z) const;
|
2016-11-26 12:45:58 +00:00
|
|
|
void make_loops(std::vector<IntersectionLine> &lines, Polygons* loops) const;
|
2019-03-01 16:53:02 +00:00
|
|
|
void make_expolygons(const Polygons &loops, const float closing_radius, ExPolygons* slices) const;
|
2016-11-26 12:45:58 +00:00
|
|
|
void make_expolygons_simple(std::vector<IntersectionLine> &lines, ExPolygons* slices) const;
|
2019-03-01 16:53:02 +00:00
|
|
|
void make_expolygons(std::vector<IntersectionLine> &lines, const float closing_radius, ExPolygons* slices) const;
|
2014-01-15 19:31:38 +00:00
|
|
|
};
|
|
|
|
|
2016-11-27 22:06:45 +00:00
|
|
|
TriangleMesh make_cube(double x, double y, double z);
|
|
|
|
|
2016-11-28 01:15:27 +00:00
|
|
|
// Generate a TriangleMesh of a cylinder
|
|
|
|
TriangleMesh make_cylinder(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));
|
|
|
|
|
2013-07-07 20:36:14 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 13:26:32 +00:00
|
|
|
#endif
|