2020-09-14 14:27:55 +00:00
|
|
|
#include "Exception.hpp"
|
2019-12-13 14:08:44 +00:00
|
|
|
#include "MeshBoolean.hpp"
|
2020-01-16 12:20:17 +00:00
|
|
|
#include "libslic3r/TriangleMesh.hpp"
|
|
|
|
#undef PI
|
2019-12-13 14:08:44 +00:00
|
|
|
|
|
|
|
// Include igl first. It defines "L" macro which then clashes with our localization
|
|
|
|
#include <igl/copyleft/cgal/mesh_boolean.h>
|
|
|
|
#undef L
|
|
|
|
|
2020-01-16 12:20:17 +00:00
|
|
|
// CGAL headers
|
|
|
|
#include <CGAL/Polygon_mesh_processing/corefinement.h>
|
|
|
|
#include <CGAL/Exact_integer.h>
|
|
|
|
#include <CGAL/Surface_mesh.h>
|
2020-02-05 16:39:56 +00:00
|
|
|
#include <CGAL/Polygon_mesh_processing/orient_polygon_soup.h>
|
|
|
|
#include <CGAL/Polygon_mesh_processing/repair.h>
|
|
|
|
#include <CGAL/Polygon_mesh_processing/remesh.h>
|
|
|
|
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
|
|
|
|
#include <CGAL/Polygon_mesh_processing/orientation.h>
|
|
|
|
#include <CGAL/Cartesian_converter.h>
|
2019-12-13 14:08:44 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
namespace MeshBoolean {
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
using MapMatrixXfUnaligned = Eigen::Map<const Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::DontAlign>>;
|
|
|
|
using MapMatrixXiUnaligned = Eigen::Map<const Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::DontAlign>>;
|
2019-12-13 14:08:44 +00:00
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
TriangleMesh eigen_to_triangle_mesh(const EigenMesh &emesh)
|
2019-12-13 14:08:44 +00:00
|
|
|
{
|
2020-02-05 16:39:56 +00:00
|
|
|
auto &VC = emesh.first; auto &FC = emesh.second;
|
|
|
|
|
2020-01-16 12:20:17 +00:00
|
|
|
Pointf3s points(size_t(VC.rows()));
|
2020-03-25 10:07:26 +00:00
|
|
|
std::vector<Vec3i> facets(size_t(FC.rows()));
|
2020-01-16 12:20:17 +00:00
|
|
|
|
|
|
|
for (Eigen::Index i = 0; i < VC.rows(); ++i)
|
|
|
|
points[size_t(i)] = VC.row(i);
|
|
|
|
|
|
|
|
for (Eigen::Index i = 0; i < FC.rows(); ++i)
|
|
|
|
facets[size_t(i)] = FC.row(i);
|
|
|
|
|
|
|
|
TriangleMesh out{points, facets};
|
2019-12-13 14:08:44 +00:00
|
|
|
out.require_shared_vertices();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
EigenMesh triangle_mesh_to_eigen(const TriangleMesh &mesh)
|
2020-01-16 12:20:17 +00:00
|
|
|
{
|
|
|
|
EigenMesh emesh;
|
|
|
|
emesh.first = MapMatrixXfUnaligned(mesh.its.vertices.front().data(),
|
|
|
|
Eigen::Index(mesh.its.vertices.size()),
|
|
|
|
3).cast<double>();
|
|
|
|
|
|
|
|
emesh.second = MapMatrixXiUnaligned(mesh.its.indices.front().data(),
|
|
|
|
Eigen::Index(mesh.its.indices.size()),
|
|
|
|
3);
|
|
|
|
return emesh;
|
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
void minus(EigenMesh &A, const EigenMesh &B)
|
2019-12-13 14:08:44 +00:00
|
|
|
{
|
2020-02-05 16:39:56 +00:00
|
|
|
auto &[VA, FA] = A;
|
|
|
|
auto &[VB, FB] = B;
|
|
|
|
|
2019-12-13 14:08:44 +00:00
|
|
|
Eigen::MatrixXd VC;
|
|
|
|
Eigen::MatrixXi FC;
|
|
|
|
igl::MeshBooleanType boolean_type(igl::MESH_BOOLEAN_TYPE_MINUS);
|
|
|
|
igl::copyleft::cgal::mesh_boolean(VA, FA, VB, FB, boolean_type, VC, FC);
|
2020-02-05 16:39:56 +00:00
|
|
|
|
|
|
|
VA = std::move(VC); FA = std::move(FC);
|
2019-12-13 14:08:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
void minus(TriangleMesh& A, const TriangleMesh& B)
|
2019-12-13 14:08:44 +00:00
|
|
|
{
|
2020-02-05 16:39:56 +00:00
|
|
|
EigenMesh eA = triangle_mesh_to_eigen(A);
|
|
|
|
minus(eA, triangle_mesh_to_eigen(B));
|
|
|
|
A = eigen_to_triangle_mesh(eA);
|
|
|
|
}
|
2019-12-13 14:08:44 +00:00
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
void self_union(EigenMesh &A)
|
|
|
|
{
|
|
|
|
EigenMesh result;
|
|
|
|
auto &[V, F] = A;
|
|
|
|
auto &[VC, FC] = result;
|
2019-12-13 14:08:44 +00:00
|
|
|
|
|
|
|
igl::MeshBooleanType boolean_type(igl::MESH_BOOLEAN_TYPE_UNION);
|
|
|
|
igl::copyleft::cgal::mesh_boolean(V, F, Eigen::MatrixXd(), Eigen::MatrixXi(), boolean_type, VC, FC);
|
2020-01-16 12:20:17 +00:00
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
A = std::move(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void self_union(TriangleMesh& mesh)
|
|
|
|
{
|
|
|
|
auto eM = triangle_mesh_to_eigen(mesh);
|
|
|
|
self_union(eM);
|
|
|
|
mesh = eigen_to_triangle_mesh(eM);
|
2019-12-13 14:08:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 12:20:17 +00:00
|
|
|
namespace cgal {
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
namespace CGALProc = CGAL::Polygon_mesh_processing;
|
|
|
|
namespace CGALParams = CGAL::Polygon_mesh_processing::parameters;
|
2020-01-16 12:20:17 +00:00
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
using EpecKernel = CGAL::Exact_predicates_exact_constructions_kernel;
|
|
|
|
using EpicKernel = CGAL::Exact_predicates_inexact_constructions_kernel;
|
|
|
|
using _EpicMesh = CGAL::Surface_mesh<EpicKernel::Point_3>;
|
|
|
|
using _EpecMesh = CGAL::Surface_mesh<EpecKernel::Point_3>;
|
2020-01-16 12:20:17 +00:00
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
struct CGALMesh { _EpicMesh m; };
|
2020-01-16 12:20:17 +00:00
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Converions from and to CGAL mesh
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
2020-01-16 12:20:17 +00:00
|
|
|
|
2021-03-10 17:21:36 +00:00
|
|
|
template<class _Mesh>
|
|
|
|
void triangle_mesh_to_cgal(const std::vector<stl_vertex> & V,
|
|
|
|
const std::vector<stl_triangle_vertex_indices> &F,
|
|
|
|
_Mesh &out)
|
|
|
|
{
|
|
|
|
if (F.empty()) return;
|
2020-02-05 16:39:56 +00:00
|
|
|
|
2021-03-10 17:21:36 +00:00
|
|
|
for (auto &v : V)
|
2021-03-03 13:30:05 +00:00
|
|
|
out.add_vertex(typename _Mesh::Point{v.x(), v.y(), v.z()});
|
|
|
|
|
|
|
|
using VI = typename _Mesh::Vertex_index;
|
2021-03-10 17:21:36 +00:00
|
|
|
for (auto &f : F)
|
2021-03-03 13:30:05 +00:00
|
|
|
out.add_face(VI(f(0)), VI(f(1)), VI(f(2)));
|
2020-01-16 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
inline Vec3d to_vec3d(const _EpicMesh::Point &v)
|
|
|
|
{
|
|
|
|
return {v.x(), v.y(), v.z()};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Vec3d to_vec3d(const _EpecMesh::Point &v)
|
|
|
|
{
|
|
|
|
CGAL::Cartesian_converter<EpecKernel, EpicKernel> cvt;
|
|
|
|
auto iv = cvt(v);
|
|
|
|
return {iv.x(), iv.y(), iv.z()};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _Mesh> TriangleMesh cgal_to_triangle_mesh(const _Mesh &cgalmesh)
|
2020-01-16 12:20:17 +00:00
|
|
|
{
|
|
|
|
Pointf3s points;
|
2020-03-25 10:07:26 +00:00
|
|
|
std::vector<Vec3i> facets;
|
2020-01-16 12:20:17 +00:00
|
|
|
points.reserve(cgalmesh.num_vertices());
|
|
|
|
facets.reserve(cgalmesh.num_faces());
|
2020-02-05 16:39:56 +00:00
|
|
|
|
2020-01-16 12:20:17 +00:00
|
|
|
for (auto &vi : cgalmesh.vertices()) {
|
|
|
|
auto &v = cgalmesh.point(vi); // Don't ask...
|
2020-02-05 16:39:56 +00:00
|
|
|
points.emplace_back(to_vec3d(v));
|
2020-01-16 12:20:17 +00:00
|
|
|
}
|
2020-02-05 16:39:56 +00:00
|
|
|
|
2020-01-16 12:20:17 +00:00
|
|
|
for (auto &face : cgalmesh.faces()) {
|
2021-03-17 08:42:03 +00:00
|
|
|
auto vtc = cgalmesh.vertices_around_face(cgalmesh.halfedge(face));
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
Vec3i facet;
|
2021-03-19 12:39:54 +00:00
|
|
|
for (auto v : vtc) {
|
2021-03-17 08:42:03 +00:00
|
|
|
if (i > 2) { i = 0; break; }
|
|
|
|
facet(i++) = v;
|
|
|
|
}
|
|
|
|
|
2021-03-19 13:02:20 +00:00
|
|
|
if (i == 3)
|
2021-03-17 08:42:03 +00:00
|
|
|
facets.emplace_back(facet);
|
2020-01-16 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TriangleMesh out{points, facets};
|
2021-03-10 17:21:36 +00:00
|
|
|
out.repair();
|
2020-01-16 12:20:17 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-03-10 17:21:36 +00:00
|
|
|
std::unique_ptr<CGALMesh, CGALMeshDeleter>
|
|
|
|
triangle_mesh_to_cgal(const std::vector<stl_vertex> &V,
|
|
|
|
const std::vector<stl_triangle_vertex_indices> &F)
|
2020-01-16 12:20:17 +00:00
|
|
|
{
|
2020-02-05 16:39:56 +00:00
|
|
|
std::unique_ptr<CGALMesh, CGALMeshDeleter> out(new CGALMesh{});
|
2021-03-10 17:21:36 +00:00
|
|
|
triangle_mesh_to_cgal(V, F, out->m);
|
2020-01-16 12:20:17 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
TriangleMesh cgal_to_triangle_mesh(const CGALMesh &cgalmesh)
|
2020-01-16 12:20:17 +00:00
|
|
|
{
|
2020-02-05 16:39:56 +00:00
|
|
|
return cgal_to_triangle_mesh(cgalmesh.m);
|
2020-01-16 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Boolean operations for CGAL meshes
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static bool _cgal_diff(CGALMesh &A, CGALMesh &B, CGALMesh &R)
|
2020-01-16 12:20:17 +00:00
|
|
|
{
|
2020-02-05 16:39:56 +00:00
|
|
|
const auto &p = CGALParams::throw_on_self_intersection(true);
|
|
|
|
return CGALProc::corefine_and_compute_difference(A.m, B.m, R.m, p, p);
|
2020-01-16 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
static bool _cgal_union(CGALMesh &A, CGALMesh &B, CGALMesh &R)
|
2020-01-16 12:20:17 +00:00
|
|
|
{
|
2020-02-05 16:39:56 +00:00
|
|
|
const auto &p = CGALParams::throw_on_self_intersection(true);
|
|
|
|
return CGALProc::corefine_and_compute_union(A.m, B.m, R.m, p, p);
|
2020-01-16 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
static bool _cgal_intersection(CGALMesh &A, CGALMesh &B, CGALMesh &R)
|
|
|
|
{
|
|
|
|
const auto &p = CGALParams::throw_on_self_intersection(true);
|
|
|
|
return CGALProc::corefine_and_compute_intersection(A.m, B.m, R.m, p, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Op> void _cgal_do(Op &&op, CGALMesh &A, CGALMesh &B)
|
|
|
|
{
|
2020-01-29 14:57:49 +00:00
|
|
|
bool success = false;
|
|
|
|
try {
|
2020-02-05 16:39:56 +00:00
|
|
|
CGALMesh result;
|
|
|
|
success = op(A, B, result);
|
|
|
|
A = std::move(result); // In-place operation does not work
|
|
|
|
} catch (...) {
|
2020-01-29 14:57:49 +00:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
if (! success)
|
2020-09-14 14:27:55 +00:00
|
|
|
throw Slic3r::RuntimeError("CGAL mesh boolean operation failed.");
|
2020-01-16 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
void minus(CGALMesh &A, CGALMesh &B) { _cgal_do(_cgal_diff, A, B); }
|
|
|
|
void plus(CGALMesh &A, CGALMesh &B) { _cgal_do(_cgal_union, A, B); }
|
|
|
|
void intersect(CGALMesh &A, CGALMesh &B) { _cgal_do(_cgal_intersection, A, B); }
|
|
|
|
bool does_self_intersect(const CGALMesh &mesh) { return CGALProc::does_self_intersect(mesh.m); }
|
|
|
|
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Now the public functions for TriangleMesh input:
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template<class Op> void _mesh_boolean_do(Op &&op, TriangleMesh &A, const TriangleMesh &B)
|
2020-01-16 12:20:17 +00:00
|
|
|
{
|
2020-02-05 16:39:56 +00:00
|
|
|
CGALMesh meshA;
|
|
|
|
CGALMesh meshB;
|
2021-03-10 17:21:36 +00:00
|
|
|
triangle_mesh_to_cgal(A.its.vertices, A.its.indices, meshA.m);
|
|
|
|
triangle_mesh_to_cgal(B.its.vertices, B.its.indices, meshB.m);
|
2020-02-05 16:39:56 +00:00
|
|
|
|
|
|
|
_cgal_do(op, meshA, meshB);
|
2020-01-16 12:20:17 +00:00
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
A = cgal_to_triangle_mesh(meshA.m);
|
|
|
|
}
|
|
|
|
|
|
|
|
void minus(TriangleMesh &A, const TriangleMesh &B)
|
|
|
|
{
|
|
|
|
_mesh_boolean_do(_cgal_diff, A, B);
|
|
|
|
}
|
|
|
|
|
|
|
|
void plus(TriangleMesh &A, const TriangleMesh &B)
|
|
|
|
{
|
|
|
|
_mesh_boolean_do(_cgal_union, A, B);
|
|
|
|
}
|
|
|
|
|
|
|
|
void intersect(TriangleMesh &A, const TriangleMesh &B)
|
|
|
|
{
|
|
|
|
_mesh_boolean_do(_cgal_intersection, A, B);
|
2020-01-16 12:20:17 +00:00
|
|
|
}
|
2019-12-13 14:08:44 +00:00
|
|
|
|
2020-02-05 16:39:56 +00:00
|
|
|
bool does_self_intersect(const TriangleMesh &mesh)
|
|
|
|
{
|
|
|
|
CGALMesh cgalm;
|
2021-03-10 17:21:36 +00:00
|
|
|
triangle_mesh_to_cgal(mesh.its.vertices, mesh.its.indices, cgalm.m);
|
2020-02-05 16:39:56 +00:00
|
|
|
return CGALProc::does_self_intersect(cgalm.m);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGALMeshDeleter::operator()(CGALMesh *ptr) { delete ptr; }
|
|
|
|
|
2020-01-16 12:20:17 +00:00
|
|
|
} // namespace cgal
|
2019-12-13 14:08:44 +00:00
|
|
|
|
|
|
|
} // namespace MeshBoolean
|
|
|
|
} // namespace Slic3r
|