TriangleMeshSlicer: Got rid of admesh!

This commit is contained in:
Vojtech Bubnik 2021-05-18 15:05:23 +02:00
parent 1256aebd88
commit 70b4915f9c
18 changed files with 443 additions and 452 deletions

View file

@ -1,42 +1,36 @@
#ifndef slic3r_TriangleMeshSlicer_hpp_
#define slic3r_TriangleMeshSlicer_hpp_
#include "libslic3r.h"
#include <admesh/stl.h>
#include <functional>
#include <vector>
#include <boost/thread.hpp>
#include "BoundingBox.hpp"
#include "Line.hpp"
#include "Point.hpp"
#include "Polygon.hpp"
#include "ExPolygon.hpp"
namespace Slic3r {
class TriangleMesh;
enum class SlicingMode : uint32_t {
// Regular slicing, maintain all contours and their orientation.
Regular,
// Maintain all contours, orient all contours CCW, therefore all holes are being closed.
Positive,
// Orient all contours CCW and keep only the contour with the largest area.
// This mode is useful for slicing complex objects in vase mode.
PositiveLargestContour,
};
struct MeshSlicingParams
{
enum class SlicingMode : uint32_t {
// Regular slicing, maintain all contours and their orientation.
Regular,
// Maintain all contours, orient all contours CCW, therefore all holes are being closed.
Positive,
// Orient all contours CCW and keep only the contour with the largest area.
// This mode is useful for slicing complex objects in vase mode.
PositiveLargestContour,
};
SlicingMode mode { SlicingMode::Regular };
// For vase mode: below this layer a different slicing mode will be used to produce a single contour.
// 0 = ignore.
size_t slicing_mode_normal_below_layer { 0 };
// Mode to apply below slicing_mode_normal_below_layer. Ignored if slicing_mode_nromal_below_layer == 0.
SlicingMode mode_below { SlicingMode::Regular };
// Transforming faces during the slicing.
Transform3f trafo { Transform3f::Identity() };
};
struct MeshSlicingParamsExtended : public MeshSlicingParams
struct MeshSlicingParamsEx : public MeshSlicingParams
{
// Morphological closing operation when creating output expolygons.
float closing_radius { 0 };
@ -45,96 +39,44 @@ struct MeshSlicingParamsExtended : public MeshSlicingParams
// Resolution for contour simplification.
// 0 = don't simplify.
double resolution { 0 };
// Transformation of the object owning the ModelVolume.
// Transform3d object_trafo;
};
class TriangleMeshSlicer
std::vector<Polygons> slice_mesh(
const indexed_triangle_set &mesh,
const std::vector<float> &zs,
const MeshSlicingParams &params,
std::function<void()> throw_on_cancel = []{});
std::vector<ExPolygons> slice_mesh_ex(
const indexed_triangle_set &mesh,
const std::vector<float> &zs,
const MeshSlicingParamsEx &params,
std::function<void()> throw_on_cancel = []{});
inline std::vector<ExPolygons> slice_mesh_ex(
const indexed_triangle_set &mesh,
const std::vector<float> &zs,
std::function<void()> throw_on_cancel = []{})
{
public:
using throw_on_cancel_callback_type = std::function<void()>;
TriangleMeshSlicer() = default;
TriangleMeshSlicer(const TriangleMesh *mesh) { this->init(mesh, []{}); }
TriangleMeshSlicer(const indexed_triangle_set *its) { this->init(its, []{}); }
void init(const TriangleMesh *mesh, throw_on_cancel_callback_type throw_on_cancel);
void init(const indexed_triangle_set *its, throw_on_cancel_callback_type);
void slice(
const std::vector<float> &z,
const MeshSlicingParams &params,
std::vector<Polygons> *layers,
throw_on_cancel_callback_type throw_on_cancel = []{}) const;
void slice(
// Where to slice.
const std::vector<float> &z,
const MeshSlicingParamsExtended &params,
std::vector<ExPolygons> *layers,
throw_on_cancel_callback_type throw_on_cancel = []{}) const;
void cut(float z, indexed_triangle_set *upper, indexed_triangle_set *lower) const;
void cut(float z, TriangleMesh* upper, TriangleMesh* lower) const;
void set_up_direction(const Vec3f& up);
private:
const indexed_triangle_set *m_its { nullptr };
// const TriangleMesh *mesh { nullptr };
// 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;
// Quaternion that will be used to rotate every facet before the slicing
Eigen::Quaternion<float, Eigen::DontAlign> m_quaternion;
// Whether or not the above quaterion should be used
bool m_use_quaternion = false;
};
inline void slice_mesh(
const TriangleMesh &mesh,
const std::vector<float> &z,
std::vector<Polygons> &layers,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = []{})
{
if (! mesh.empty()) {
TriangleMeshSlicer slicer(&mesh);
slicer.slice(z, MeshSlicingParams{}, &layers, thr);
}
return slice_mesh_ex(mesh, zs, MeshSlicingParamsEx{}, throw_on_cancel);
}
inline void slice_mesh(
const TriangleMesh &mesh,
const std::vector<float> &z,
const MeshSlicingParamsExtended &params,
std::vector<ExPolygons> &layers,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = []{})
inline std::vector<ExPolygons> slice_mesh_ex(
const indexed_triangle_set &mesh,
const std::vector<float> &zs,
float closing_radius,
std::function<void()> throw_on_cancel = []{})
{
if (! mesh.empty()) {
TriangleMeshSlicer slicer(&mesh);
slicer.slice(z, params, &layers, thr);
}
}
inline void slice_mesh(
const TriangleMesh &mesh,
const std::vector<float> &z,
float closing_radius,
std::vector<ExPolygons> &layers,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = []{})
{
MeshSlicingParamsExtended params;
MeshSlicingParamsEx params;
params.closing_radius = closing_radius;
slice_mesh(mesh, z, params, layers);
return slice_mesh_ex(mesh, zs, params, throw_on_cancel);
}
inline void slice_mesh(
const TriangleMesh &mesh,
const std::vector<float> &z,
std::vector<ExPolygons> &layers,
TriangleMeshSlicer::throw_on_cancel_callback_type thr = []{})
{
slice_mesh(mesh, z, MeshSlicingParamsExtended{}, layers);
}
void cut_mesh(
const indexed_triangle_set &mesh,
float z,
indexed_triangle_set *upper,
indexed_triangle_set *lower);
}