OrcaSlicer/src/libslic3r/MeshBoolean.cpp
SoftFever 3cb573dcb9
Feature/merge 1.8.4 (#3827)
* FIX: the logic of buried points that were not buried

JIRA: none

Signed-off-by: Kunlong Ma <kunlong.ma@bambulab.com>
Change-Id: Id95174659c5fce7feba409eb5e14916608745fa4

* ci: update network module based on commit bc7ca98

Change-Id: I923526f0bf9ce5a288144fa1f9b0f2fc640f41b7

* Fix Firefox
Co-authored-by: hadess <hadess@hadess.net>

* FIX: cali: custom created filament from AMS displayed as incompatible

jira: new

remove the condition: is_system

Change-Id: Ib1366966bbdbe01bc9e2483d9914d270ebefa976

* FIX: duplicated items in comboBox at calibration completed page

jira: new

Change-Id: I4749a2206df16c438e0d3098e36274b2a20f313e

* ENH:update support for P1S plus

jira:[for p1s plus]

Change-Id: Id577d4e94e2162cb0045d261dfaa5f396ecded2f

* ENH: CLI: add mk information support

JIRA: no jira
Change-Id: Idd89b143d439de50d9f52eb8aec95b262d66875d

* ENH:calibration support p1p plus

jira:[plus]

Change-Id: Ia290d3a8a8b9adaac7a6ee26d9a8b5ea0c1b3aee

* FIX: add log for base_id and filament_id

github: #3087

Change-Id: Iebfbd0f224fce49f33fc81c71e6108f6e3abb5ff

* FIX: sync whole preset vendor directory

Change-Id: I191dbe979a87ff35d38cab1149b7975664344838
Jira: STUDIO-5534
(cherry picked from commit 628866608116336453804aa1217dd55db04d47ad)

* FIX: use t_utc for debug only

Change-Id: Ia05d8969d4de3dd38908980d6e17a3ebb11ca279
Github 3045

Change-Id: I77935df53bbf2772b1146e5c330c537165a3a2e6

* FIX:make sort_volumes right

Jira: STUDIO-5645
Change-Id: If324c9115bfaaf0c1b7b4be7c7ee96ba6b8ac890

* ENH:keep an unload logic

jira:[for unload]

Change-Id: Id30ec71ffa5b2dac89346ea47ca48a62479e3ab1

* FIX: several problems with mesh boolean

1. Cut with multiple volumes are OK now.
2. Close mesh boolean fail error with new object or open object
3. Fix wrong name and config of boolean resulting object

github: #3118
jira: none

Change-Id: If2c9dbfb36cbdfe4917a2371217923891bb7909c
(cherry picked from commit 982c0ecb92cf7c2b5ae5972ab900a6b10e7dda50)

* NEW:limit the length of project name

jira:[project name]

Change-Id: I955620f7073b3b7fda280d1118524f561d047751

* ENH:adjusting the warning level of timelpase

jira:[STUDIO-5662]

Change-Id: I4902b22d316f5e09a97a62c88b8a98e55c405434

* FIX: 3mf specification: change namespace form slic3rpe to BambuStudio

Jira: XXXX

Change-Id: Id705affc875ef23fdf2ac6d79f0cb0aafc4f7050

* NEW: Open MakerWorld With BambuStudio GetParam

JIRA: none
Change-Id: I0d65b364f1cd2d634a88882ab072c3e61ea89167
(cherry picked from commit 8eaf45e5359439a7c796fd79876c86775abcf48e)

* FIX: Filament issue generated when creating a printer

Jira: XXXX

Change-Id: I976770b69b47641bd54aa7a9c56fba7f58d1ab68
(cherry picked from commit ba42188b93c58b3954234d72acdd9769a68e3d3c)

* FIX: Blank page appears when editing presets

Jira: 5563

Change-Id: I4c49e05515b1beff55991e92f8079c4499c27eab
(cherry picked from commit e86517d290f4cd0765a230d811b0ddf2c9f34c17)

* FIX: context menu didn't update UI

jira: STUDIO-5691

Change-Id: Ia66b8623d832eba805aff5320941233a68ff258b

* FIX: crash of "filling bed"

"get_arrange_settings() const" gets trapped in infinite recursive calling.
Now we delete this function.

jira: STUDIO-5688
Change-Id: Ia39974734bb37b2a2f06b5bf78185f01be726872

* FIX: boolean hangs in the middle of color painting

Can't do splits in combine_mesh_fff, as do_boolean of mcut will split meshes.

jira: STUDIO-5693
Change-Id: Idddb7d20dd7ca386c39ddd3d87d9defc3136aa5d
(cherry picked from commit 6c67d015941458e37faaf0015b6509b5a0eadc0e)

* Fix: Fix a number of compilation problems

issues found when using gcc version 13.2.0 (GCC) in a Flatpak sandbox

github : https://github.com/bambulab/BambuStudio/issues/3074
github pull request: https://github.com/bambulab/BambuStudio/pull/3096

Change-Id: I08aeac593eb1ce7675894df72e8489200bae713d
(cherry picked from commit 069d133d66bfa682de4a860e379d5dc16b3d907c)

* fix: macos icns issue when icon was not attached

github pull request:https://github.com/bambulab/BambuStudio/pull/3116

Change-Id: I49072ad49f3af7669a6d307c791594ade210da50
(cherry picked from commit c977e5582e3a30ad16dd267810037423aad9a53c)

* FIX: Add flush_length for change_filament_gcode

Change-Id: I30f4b97d3d61c2a57f0e92f157cbd31c38aa7265
Jira: XXXX
(cherry picked from commit 92eb2bac977a0c4095b316cbbc6580fb5228b710)

* FIX: edit preset dialog can't close on mac

Jira: 5696

Change-Id: Ib33dfd07cc588ddd3805e3490a4d8c36dcd890ac

* ENH: add dev_ota_version in ssdp

JIRA: STUDIO-5740

Change-Id: Ic80e6d4b9bf82813fdc4a76604a3d36213d12b03
Signed-off-by: Stone Li <stone.li@bambulab.com>

* NEW:Adapt to multicolour and gradient colour

JIRA:xxxx
Change-Id: I8084cab603d5681cbcaf2d6f5e0d7ad5419cb2af

* NEW:Adaptation of semi transparent materials

JIRA: XXXX
Change-Id: Ie32d8ce67c37b85eb6d7d6594cb514a696307e68

* FIX: disable flush options if prime tower is unchecked

jira: STUDIO-5639

Change-Id: I25081584d430bc7d062e14bcc2cdbf7522cf9d99

* ENH: refine GetVersion for HMS query

JIRA: STUDIO-5763

Change-Id: Ia3ccc07d79cc0736eb12e9782da50211abb74772
Signed-off-by: Stone Li <stone.li@bambulab.com>

* FIX: Prefer old selection when sync AMS not compatible

Change-Id: I6b18db51887132a997cf78d70fff9a92e23bc44a
Jira: STUDIO-5416
(cherry picked from commit 077fae29823cf4f3071d408b1b40f55ee0cb33c6)

* FIX: The flushing was not auto-calc when sync ams list

JIRA: STUDIO-5551

1. flushing volume auto-calc when sync ams list
2. flushing volume takes the larger calculation value when filament has
   multi-colors

Signed-off-by: Kunlong Ma <kunlong.ma@bambulab.com>
Change-Id: I72e6f9780ea56103a44c2da6068440a4615c254d

* FIX:fixed invalid links

jira:[fixed link]

Change-Id: I036a38b6e8e94da762f93805bd7be706538771fe

* FIX: Prompt to delete problematic presets

Jira: XXXX

Change-Id: Ic43f7bb782794d7ab0b6acbffbb5d73e94f6ed73

* FIX:fixed incorrect HMS content

jira:[STUDIO-5818]

Change-Id: Ia2896d6f0ab1ffedbc850e54168acece8e47bdbb

* FIX:external transparent material display error

JIRA: STUDIO-5845
Change-Id: I0a4f05ac5d5c0ac49d85a704ee65a7221c5f1e1d

* FIX: [5846] Custom Filament Page show System Filament

Simultaneously solve: When downloading Preset from the cloud, the filament_id of the preset in m_preset is null.

Jira: 5846

Change-Id: I6ba1b46fe92e345614b6a4af3fffa87d81fa2456

* FIX:A1 and p1 series do not support custom materials

JIRA:XXXX
Change-Id: Ib0459273d1f9a7152a5563757204634a8d0cd6f5

* FIX: exception when comparing profiles

jira:[NEW]

Signed-off-by: XunZhangBambu <xun.zhang@bambulab.com>
Change-Id: I946b5fcd35f779d271df2b3de731fdcada5aab29
(cherry picked from commit 00e739570812e5c4be3e0f7702ce8c72c0f9e72b)

* FIX: hide_id_middle_string

Change-Id: I28f32ec526b443d31d7992971b80ab1cb737deb6
Github: STUDIO-5825

* ENH: modify some logs level

JIRA: STUDIO-5958

Change-Id: I5a8592dfb8ffa9a81952535cb30944f867aa0e22
Signed-off-by: Stone Li <stone.li@bambulab.com>

* NEW:build plate marker detect

Change-Id: I70f03efea688bb6ce71c3f5990bb3c50605ab184

* FIX: Studio UI Freeze when saving user preset

github: #3335

Change-Id: Idaf53f673a3e46408826c06bdde2c592395d358b

* update bbl plugin version

* fix build errors

* update bbl profiles

* update color

---------

Signed-off-by: Kunlong Ma <kunlong.ma@bambulab.com>
Signed-off-by: Stone Li <stone.li@bambulab.com>
Co-authored-by: Kunlong Ma <kunlong.ma@bambulab.com>
Co-authored-by: gerrit <gerrit@bambulab.com>
Co-authored-by: liz.li <liz.li@bambulab.com>
Co-authored-by: tao wang <tao.wang@bambulab.com>
Co-authored-by: lane.wei <lane.wei@bambulab.com>
Co-authored-by: maosheng.wei <maosheng.wei@bambulab.com>
Co-authored-by: chunmao.guo <chunmao.guo@bambulab.com>
Co-authored-by: zhou.xu <zhou.xu@bambulab.com>
Co-authored-by: Arthur <arthur.tang@bambulab.com>
Co-authored-by: Bastien Nocera <hadess@hadess.net>
Co-authored-by: zhimin.zeng <zhimin.zeng@bambulab.com>
Co-authored-by: hu.wang <hu.wang@bambulab.com>
Co-authored-by: Stone Li <stone.li@bambulab.com>
Co-authored-by: XunZhangBambu <xun.zhang@bambulab.com>
2024-01-26 20:18:10 +08:00

821 lines
30 KiB
C++

#include "Exception.hpp"
#include "MeshBoolean.hpp"
#include "libslic3r/TriangleMesh.hpp"
#include "libslic3r/TryCatchSignal.hpp"
#include "libslic3r/format.hpp"
#undef PI
#include <boost/next_prior.hpp>
// Include igl first. It defines "L" macro which then clashes with our localization
#include <igl/copyleft/cgal/mesh_boolean.h>
#undef L
// CGAL headers
#include <CGAL/Polygon_mesh_processing/corefinement.h>
#include <CGAL/Exact_integer.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Cartesian_converter.h>
#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>
// BBS: for segment
#include <CGAL/mesh_segmentation.h>
#include <CGAL/property_map.h>
#include <CGAL/boost/graph/copy_face_graph.h>
#include <CGAL/boost/graph/Face_filtered_graph.h>
// BBS: for boolean using mcut
#include "mcut/include/mcut/mcut.h"
#include "boost/log/trivial.hpp"
namespace Slic3r {
namespace MeshBoolean {
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>>;
TriangleMesh eigen_to_triangle_mesh(const EigenMesh &emesh)
{
auto &VC = emesh.first; auto &FC = emesh.second;
indexed_triangle_set its;
its.vertices.reserve(size_t(VC.rows()));
its.indices.reserve(size_t(FC.rows()));
for (Eigen::Index i = 0; i < VC.rows(); ++i)
its.vertices.emplace_back(VC.row(i).cast<float>());
for (Eigen::Index i = 0; i < FC.rows(); ++i)
its.indices.emplace_back(FC.row(i));
return TriangleMesh { std::move(its) };
}
EigenMesh triangle_mesh_to_eigen(const TriangleMesh &mesh)
{
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;
}
void minus(EigenMesh &A, const EigenMesh &B)
{
auto &[VA, FA] = A;
auto &[VB, FB] = B;
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);
VA = std::move(VC); FA = std::move(FC);
}
void minus(TriangleMesh& A, const TriangleMesh& B)
{
EigenMesh eA = triangle_mesh_to_eigen(A);
minus(eA, triangle_mesh_to_eigen(B));
A = eigen_to_triangle_mesh(eA);
}
void self_union(EigenMesh &A)
{
EigenMesh result;
auto &[V, F] = A;
auto &[VC, FC] = result;
igl::MeshBooleanType boolean_type(igl::MESH_BOOLEAN_TYPE_UNION);
igl::copyleft::cgal::mesh_boolean(V, F, Eigen::MatrixXd(), Eigen::MatrixXi(), boolean_type, VC, FC);
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);
}
namespace cgal {
namespace CGALProc = CGAL::Polygon_mesh_processing;
namespace CGALParams = CGAL::Polygon_mesh_processing::parameters;
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>;
struct CGALMesh {
_EpicMesh m;
CGALMesh() = default;
CGALMesh(const _EpicMesh& _m) :m(_m) {}
};
void save_CGALMesh(const std::string& fname, const CGALMesh& cgal_mesh) {
std::ofstream os(fname);
os << cgal_mesh.m;
os.close();
}
// /////////////////////////////////////////////////////////////////////////////
// Converions from and to CGAL mesh
// /////////////////////////////////////////////////////////////////////////////
template<class _Mesh> void triangle_mesh_to_cgal(const TriangleMesh& M, _Mesh& out)
{
using Index3 = std::array<size_t, 3>;
if (M.empty()) return;
std::vector<typename _Mesh::Point> points;
std::vector<Index3> indices;
points.reserve(M.its.vertices.size());
indices.reserve(M.its.indices.size());
for (auto& v : M.its.vertices) points.emplace_back(v.x(), v.y(), v.z());
for (auto& _f : M.its.indices) {
auto f = _f.cast<size_t>();
indices.emplace_back(Index3{ f(0), f(1), f(2) });
}
CGALProc::orient_polygon_soup(points, indices);
CGALProc::polygon_soup_to_polygon_mesh(points, indices, out);
// Number the faces because 'orient_to_bound_a_volume' needs a face <--> index map
unsigned index = 0;
for (auto face : out.faces()) face = CGAL::SM_Face_index(index++);
if (CGAL::is_closed(out))
CGALProc::orient_to_bound_a_volume(out);
else
throw Slic3r::RuntimeError("Mesh not watertight");
}
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;
size_t vertices_count = V.size();
size_t edges_count = (F.size()* 3) / 2;
size_t faces_count = F.size();
out.reserve(vertices_count, edges_count, faces_count);
for (auto &v : V)
out.add_vertex(typename _Mesh::Point{v.x(), v.y(), v.z()});
using VI = typename _Mesh::Vertex_index;
for (auto &f : F)
out.add_face(VI(f(0)), VI(f(1)), VI(f(2)));
}
inline Vec3f to_vec3f(const _EpicMesh::Point& v)
{
return { float(v.x()), float(v.y()), float(v.z()) };
}
inline Vec3f to_vec3f(const _EpecMesh::Point& v)
{
CGAL::Cartesian_converter<EpecKernel, EpicKernel> cvt;
auto iv = cvt(v);
return { float(iv.x()), float(iv.y()), float(iv.z()) };
}
template<class _Mesh>
indexed_triangle_set cgal_to_indexed_triangle_set(const _Mesh &cgalmesh)
{
indexed_triangle_set its;
its.vertices.reserve(cgalmesh.num_vertices());
its.indices.reserve(cgalmesh.num_faces());
const auto &faces = cgalmesh.faces();
const auto &vertices = cgalmesh.vertices();
int vsize = int(vertices.size());
for (const auto &vi : vertices) {
auto &v = cgalmesh.point(vi); // Don't ask...
its.vertices.emplace_back(to_vec3f(v));
}
for (const auto &face : faces) {
auto vtc = cgalmesh.vertices_around_face(cgalmesh.halfedge(face));
int i = 0;
Vec3i facet;
for (auto v : vtc) {
int iv = v;
if (i > 2 || iv < 0 || iv >= vsize) { i = 0; break; }
facet(i++) = iv;
}
if (i == 3)
its.indices.emplace_back(facet);
}
return its;
}
template<class _Mesh> TriangleMesh cgal_to_triangle_mesh(const _Mesh &cgalmesh)
{
indexed_triangle_set its = cgal_to_indexed_triangle_set(cgalmesh);
return TriangleMesh(std::move(its));
}
std::unique_ptr<CGALMesh, CGALMeshDeleter>
triangle_mesh_to_cgal(const std::vector<stl_vertex> &V,
const std::vector<stl_triangle_vertex_indices> &F)
{
std::unique_ptr<CGALMesh, CGALMeshDeleter> out(new CGALMesh{});
triangle_mesh_to_cgal(V, F, out->m);
return out;
}
TriangleMesh cgal_to_triangle_mesh(const CGALMesh &cgalmesh)
{
return TriangleMesh{cgal_to_indexed_triangle_set(cgalmesh.m)};
}
indexed_triangle_set cgal_to_indexed_triangle_set(const CGALMesh &cgalmesh)
{
return cgal_to_indexed_triangle_set(cgalmesh.m);
}
// /////////////////////////////////////////////////////////////////////////////
// Boolean operations for CGAL meshes
// /////////////////////////////////////////////////////////////////////////////
static bool _cgal_diff(CGALMesh &A, CGALMesh &B, CGALMesh &R)
{
const auto &p = CGALParams::throw_on_self_intersection(true);
return CGALProc::corefine_and_compute_difference(A.m, B.m, R.m, p, p);
}
static bool _cgal_union(CGALMesh &A, CGALMesh &B, CGALMesh &R)
{
const auto &p = CGALParams::throw_on_self_intersection(true);
return CGALProc::corefine_and_compute_union(A.m, B.m, R.m, p, p);
}
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)
{
bool success = false;
bool hw_fail = false;
try {
CGALMesh result;
try_catch_signal({SIGSEGV, SIGFPE}, [&success, &A, &B, &result, &op] {
success = op(A, B, result);
}, [&] { hw_fail = true; });
A = std::move(result); // In-place operation does not work
} catch (...) {
success = false;
}
if (hw_fail)
throw Slic3r::HardCrash("CGAL mesh boolean operation crashed.");
if (! success)
throw Slic3r::RuntimeError("CGAL mesh boolean operation failed.");
}
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); }
// BBS
void segment(CGALMesh& src, std::vector<CGALMesh>& dst, double smoothing_alpha = 0.5, int segment_number=5)
{
typedef boost::graph_traits<_EpicMesh>::face_descriptor face_descriptor;
typedef _EpicMesh::Property_map<face_descriptor, double> Facet_double_map;
typedef CGAL::Face_filtered_graph<_EpicMesh> Filtered_graph;
_EpicMesh mesh = src.m;
Facet_double_map sdf_property_map;
sdf_property_map = mesh.add_property_map<face_descriptor, double>("f:sdf").first;
CGAL::sdf_values(mesh, sdf_property_map);
// create a property-map for segment-ids
typedef _EpicMesh::Property_map<face_descriptor, std::size_t> Facet_int_map;
Facet_int_map segment_property_map = mesh.add_property_map<face_descriptor, std::size_t>("f:sid").first;;
// segment the mesh using default parameters for number of levels, and smoothing lambda
// Any other scalar values can be used instead of using SDF values computed using the CGAL function
std::size_t number_of_segments = CGAL::segmentation_from_sdf_values(mesh, sdf_property_map, segment_property_map, segment_number, smoothing_alpha);
//print area of each segment and then put it in a Mesh and print it in an OFF file
Filtered_graph segment_mesh(mesh);
_EpicMesh mesh_merged;
for (std::size_t id = 0; id < number_of_segments; ++id)
{
segment_mesh.set_selected_faces(id, segment_property_map);
//std::cout << "Segment " << id << "'s area is : " << CGAL::Polygon_mesh_processing::area(segment_mesh) << std::endl;
_EpicMesh out;
CGAL::copy_face_graph(segment_mesh, out);
// save_CGALMesh("out.off", out);
// fill holes
typedef boost::graph_traits<_EpicMesh>::halfedge_descriptor halfedge_descriptor;
typedef boost::graph_traits<_EpicMesh>::vertex_descriptor vertex_descriptor;
std::vector<halfedge_descriptor> border_cycles;
CGAL::Polygon_mesh_processing::extract_boundary_cycles(out, std::back_inserter(border_cycles));
for (halfedge_descriptor h : border_cycles)
{
std::vector<face_descriptor> patch_facets;
#if 0
std::vector<vertex_descriptor> patch_vertices;
CGAL::Polygon_mesh_processing::triangulate_and_refine_hole(out, h, std::back_inserter(patch_facets),
std::back_inserter(patch_vertices));
std::cout << "* Number of facets in constructed patch: " << patch_facets.size() << std::endl;
std::cout << " Number of vertices in constructed patch: " << patch_vertices.size() << std::endl;
#else
CGAL::Polygon_mesh_processing::triangulate_hole(out, h, std::back_inserter(patch_facets));
#endif
}
//if (id > 2) {
// mesh_merged.join(out);
//}
//else
{
dst.emplace_back(std::move(CGALMesh(out)));
}
}
//if (mesh_merged.is_empty() == false) {
// CGAL::Polygon_mesh_processing::stitch_borders(mesh_merged);
// dst.emplace_back(std::move(CGALMesh(mesh_merged)));
//}
}
std::vector<TriangleMesh> segment(const TriangleMesh& src, double smoothing_alpha, int segment_number)
{
CGALMesh in_cgal_mesh;
MeshBoolean::cgal::triangle_mesh_to_cgal(src, in_cgal_mesh.m);
std::vector<CGALMesh> out_cgal_meshes;
segment(in_cgal_mesh, out_cgal_meshes, smoothing_alpha, segment_number);
std::vector<TriangleMesh> out_meshes;
for (auto& outf_cgal_mesh: out_cgal_meshes)
{
out_meshes.emplace_back(std::move(cgal_to_triangle_mesh(outf_cgal_mesh.m)));
}
return out_meshes;
}
void merge(std::vector<_EpicMesh>& srcs, _EpicMesh& dst)
{
_EpicMesh mesh_merged;
for (size_t i = 0; i < srcs.size(); i++)
{
mesh_merged.join(srcs[i]);
}
if (mesh_merged.is_empty() == false) {
CGAL::Polygon_mesh_processing::stitch_borders(mesh_merged);
dst = std::move(mesh_merged);
}
}
TriangleMesh merge(std::vector<TriangleMesh> meshes)
{
std::vector<_EpicMesh> srcs(meshes.size());
for (size_t i = 0; i < meshes.size(); i++)
{
MeshBoolean::cgal::triangle_mesh_to_cgal(meshes[i], srcs[i]);
}
_EpicMesh dst;
merge(srcs, dst);
return cgal_to_triangle_mesh(dst);
}
template<class Op> void _mesh_boolean_do(Op &&op, indexed_triangle_set &A, const indexed_triangle_set &B)
{
CGALMesh meshA;
CGALMesh meshB;
triangle_mesh_to_cgal(A.vertices, A.indices, meshA.m);
triangle_mesh_to_cgal(B.vertices, B.indices, meshB.m);
_cgal_do(op, meshA, meshB);
A = cgal_to_indexed_triangle_set(meshA.m);
}
template<class Op> void _mesh_boolean_do(Op &&op, TriangleMesh &A, const TriangleMesh &B)
{
CGALMesh meshA;
CGALMesh meshB;
triangle_mesh_to_cgal(A.its.vertices, A.its.indices, meshA.m);
triangle_mesh_to_cgal(B.its.vertices, B.its.indices, meshB.m);
_cgal_do(op, meshA, meshB);
A = cgal_to_triangle_mesh(meshA);
}
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);
}
void minus(indexed_triangle_set &A, const indexed_triangle_set &B)
{
_mesh_boolean_do(_cgal_diff, A, B);
}
void plus(indexed_triangle_set &A, const indexed_triangle_set &B)
{
_mesh_boolean_do(_cgal_union, A, B);
}
void intersect(indexed_triangle_set &A, const indexed_triangle_set &B)
{
_mesh_boolean_do(_cgal_intersection, A, B);
}
bool does_self_intersect(const TriangleMesh &mesh)
{
CGALMesh cgalm;
triangle_mesh_to_cgal(mesh.its.vertices, mesh.its.indices, cgalm.m);
return CGALProc::does_self_intersect(cgalm.m);
}
void CGALMeshDeleter::operator()(CGALMesh *ptr) { delete ptr; }
bool does_bound_a_volume(const CGALMesh &mesh)
{
return CGAL::is_closed(mesh.m) && CGALProc::does_bound_a_volume(mesh.m);
}
bool empty(const CGALMesh &mesh)
{
return mesh.m.is_empty();
}
CGALMeshPtr clone(const CGALMesh &m)
{
return CGALMeshPtr{new CGALMesh{m}};
}
} // namespace cgal
namespace mcut {
/* BBS: MusangKing
* mcut mesh array format for Boolean Opts calculation
*/
struct McutMesh
{
// variables for mesh data in a format suited for mcut
std::vector<uint32_t> faceSizesArray;
std::vector<uint32_t> faceIndicesArray;
std::vector<double> vertexCoordsArray;
};
void McutMeshDeleter::operator()(McutMesh *ptr) { delete ptr; }
bool empty(const McutMesh &mesh) { return mesh.vertexCoordsArray.empty() || mesh.faceIndicesArray.empty(); }
void triangle_mesh_to_mcut(const TriangleMesh &src_mesh, McutMesh &srcMesh, const Transform3d &src_nm = Transform3d::Identity())
{
// vertices precision convention and copy
srcMesh.vertexCoordsArray.reserve(src_mesh.its.vertices.size() * 3);
for (int i = 0; i < src_mesh.its.vertices.size(); ++i) {
const Vec3d v = src_nm * src_mesh.its.vertices[i].cast<double>();
srcMesh.vertexCoordsArray.push_back(v[0]);
srcMesh.vertexCoordsArray.push_back(v[1]);
srcMesh.vertexCoordsArray.push_back(v[2]);
}
// faces copy
srcMesh.faceIndicesArray.reserve(src_mesh.its.indices.size() * 3);
srcMesh.faceSizesArray.reserve(src_mesh.its.indices.size());
for (int i = 0; i < src_mesh.its.indices.size(); ++i) {
const int &f0 = src_mesh.its.indices[i][0];
const int &f1 = src_mesh.its.indices[i][1];
const int &f2 = src_mesh.its.indices[i][2];
srcMesh.faceIndicesArray.push_back(f0);
srcMesh.faceIndicesArray.push_back(f1);
srcMesh.faceIndicesArray.push_back(f2);
srcMesh.faceSizesArray.push_back((uint32_t) 3);
}
}
McutMeshPtr triangle_mesh_to_mcut(const indexed_triangle_set &M)
{
std::unique_ptr<McutMesh, McutMeshDeleter> out(new McutMesh{});
TriangleMesh trimesh(M);
triangle_mesh_to_mcut(trimesh, *out.get());
return out;
}
TriangleMesh mcut_to_triangle_mesh(const McutMesh &mcutmesh)
{
uint32_t ccVertexCount = mcutmesh.vertexCoordsArray.size() / 3;
auto &ccVertices = mcutmesh.vertexCoordsArray;
auto &ccFaceIndices = mcutmesh.faceIndicesArray;
auto &faceSizes = mcutmesh.faceSizesArray;
uint32_t ccFaceCount = faceSizes.size();
// rearrange vertices/faces and save into result mesh
std::vector<Vec3f> vertices(ccVertexCount);
for (uint32_t i = 0; i < ccVertexCount; i++) {
vertices[i][0] = (float) ccVertices[(uint64_t) i * 3 + 0];
vertices[i][1] = (float) ccVertices[(uint64_t) i * 3 + 1];
vertices[i][2] = (float) ccVertices[(uint64_t) i * 3 + 2];
}
// output faces
int faceVertexOffsetBase = 0;
// for each face in CC
std::vector<Vec3i> faces(ccFaceCount);
for (uint32_t f = 0; f < ccFaceCount; ++f) {
int faceSize = faceSizes.at(f);
// for each vertex in face
for (int v = 0; v < faceSize; v++) { faces[f][v] = ccFaceIndices[(uint64_t) faceVertexOffsetBase + v]; }
faceVertexOffsetBase += faceSize;
}
TriangleMesh out(vertices, faces);
return out;
}
void merge_mcut_meshes(McutMesh& src, const McutMesh& cut) {
indexed_triangle_set all_its;
TriangleMesh tri_src = mcut_to_triangle_mesh(src);
TriangleMesh tri_cut = mcut_to_triangle_mesh(cut);
its_merge(all_its, tri_src.its);
its_merge(all_its, tri_cut.its);
src = *triangle_mesh_to_mcut(all_its);
}
MCAPI_ATTR void MCAPI_CALL mcDebugOutput(McDebugSource source,
McDebugType type,
unsigned int id,
McDebugSeverity severity,
size_t length,
const char* message,
const void* userParam)
{
BOOST_LOG_TRIVIAL(debug)<<Slic3r::format("mcut mcDebugOutput message ( %d ): %s ", id, message);
switch (source) {
case MC_DEBUG_SOURCE_API:
BOOST_LOG_TRIVIAL(debug)<<("Source: API");
break;
case MC_DEBUG_SOURCE_KERNEL:
BOOST_LOG_TRIVIAL(debug)<<("Source: Kernel");
break;
}
switch (type) {
case MC_DEBUG_TYPE_ERROR:
BOOST_LOG_TRIVIAL(debug)<<("Type: Error");
break;
case MC_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
BOOST_LOG_TRIVIAL(debug)<<("Type: Deprecated Behaviour");
break;
case MC_DEBUG_TYPE_OTHER:
BOOST_LOG_TRIVIAL(debug)<<("Type: Other");
break;
}
switch (severity) {
case MC_DEBUG_SEVERITY_HIGH:
BOOST_LOG_TRIVIAL(debug)<<("Severity: high");
break;
case MC_DEBUG_SEVERITY_MEDIUM:
BOOST_LOG_TRIVIAL(debug)<<("Severity: medium");
break;
case MC_DEBUG_SEVERITY_LOW:
BOOST_LOG_TRIVIAL(debug)<<("Severity: low");
break;
case MC_DEBUG_SEVERITY_NOTIFICATION:
BOOST_LOG_TRIVIAL(debug)<<("Severity: notification");
break;
}
}
bool do_boolean_single(McutMesh &srcMesh, const McutMesh &cutMesh, const std::string &boolean_opts)
{
// create context
McContext context = MC_NULL_HANDLE;
McResult err = mcCreateContext(&context, 0);
// add debug callback according to https://cutdigital.github.io/mcut.site/tutorials/debugging/
mcDebugMessageCallback(context, mcDebugOutput, nullptr);
mcDebugMessageControl(
context,
MC_DEBUG_SOURCE_ALL,
MC_DEBUG_TYPE_ERROR,
MC_DEBUG_SEVERITY_MEDIUM,
true);
// We can either let MCUT compute all possible meshes (including patches etc.), or we can
// constrain the library to compute exactly the boolean op mesh we want. This 'constrained' case
// is done with the following flags.
// NOTE#1: you can extend these flags by bitwise ORing with additional flags (see `McDispatchFlags' in mcut.h)
// NOTE#2: below order of columns MATTERS
const std::map<std::string, McFlags> booleanOpts = {
{"A_NOT_B", MC_DISPATCH_FILTER_FRAGMENT_SEALING_INSIDE | MC_DISPATCH_FILTER_FRAGMENT_LOCATION_ABOVE},
{"B_NOT_A", MC_DISPATCH_FILTER_FRAGMENT_SEALING_OUTSIDE | MC_DISPATCH_FILTER_FRAGMENT_LOCATION_BELOW},
{"UNION", MC_DISPATCH_FILTER_FRAGMENT_SEALING_OUTSIDE | MC_DISPATCH_FILTER_FRAGMENT_LOCATION_ABOVE},
{"INTERSECTION", MC_DISPATCH_FILTER_FRAGMENT_SEALING_INSIDE | MC_DISPATCH_FILTER_FRAGMENT_LOCATION_BELOW},
};
std::map<std::string, McFlags>::const_iterator it = booleanOpts.find(boolean_opts);
McFlags boolOpFlags = it->second;
if (srcMesh.vertexCoordsArray.empty() && (boolean_opts == "UNION" || boolean_opts == "B_NOT_A")) {
srcMesh = cutMesh;
mcReleaseContext(context);
return true;
}
err = mcDispatch(context,
MC_DISPATCH_VERTEX_ARRAY_DOUBLE | // vertices are in array of doubles
MC_DISPATCH_ENFORCE_GENERAL_POSITION | // perturb if necessary
boolOpFlags, // filter flags which specify the type of output we want
// source mesh
reinterpret_cast<const void *>(srcMesh.vertexCoordsArray.data()), reinterpret_cast<const uint32_t *>(srcMesh.faceIndicesArray.data()),
srcMesh.faceSizesArray.data(), static_cast<uint32_t>(srcMesh.vertexCoordsArray.size() / 3), static_cast<uint32_t>(srcMesh.faceSizesArray.size()),
// cut mesh
reinterpret_cast<const void *>(cutMesh.vertexCoordsArray.data()), cutMesh.faceIndicesArray.data(), cutMesh.faceSizesArray.data(),
static_cast<uint32_t>(cutMesh.vertexCoordsArray.size() / 3), static_cast<uint32_t>(cutMesh.faceSizesArray.size()));
if (err != MC_NO_ERROR) {
BOOST_LOG_TRIVIAL(debug) << "MCUT mcDispatch fails! err=" << err;
mcReleaseContext(context);
if (boolean_opts == "UNION") {
merge_mcut_meshes(srcMesh, cutMesh);
return true;
}
return false;
}
// query the number of available connected component
uint32_t numConnComps;
err = mcGetConnectedComponents(context, MC_CONNECTED_COMPONENT_TYPE_FRAGMENT, 0, NULL, &numConnComps);
if (err != MC_NO_ERROR || numConnComps==0) {
BOOST_LOG_TRIVIAL(debug) << "MCUT mcGetConnectedComponents fails! err=" << err << ", numConnComps" << numConnComps;
mcReleaseContext(context);
if (numConnComps == 0 && boolean_opts == "UNION") {
merge_mcut_meshes(srcMesh, cutMesh);
return true;
}
return false;
}
std::vector<McConnectedComponent> connectedComponents(numConnComps, MC_NULL_HANDLE);
err = mcGetConnectedComponents(context, MC_CONNECTED_COMPONENT_TYPE_FRAGMENT, (uint32_t) connectedComponents.size(), connectedComponents.data(), NULL);
McutMesh outMesh;
int N_vertices = 0;
// traversal of all connected components
for (int n = 0; n < numConnComps; ++n) {
// query the data of each connected component from MCUT
McConnectedComponent connComp = connectedComponents[n];
// query the vertices
McSize numBytes = 0;
err = mcGetConnectedComponentData(context, connComp, MC_CONNECTED_COMPONENT_DATA_VERTEX_DOUBLE, 0, NULL, &numBytes);
uint32_t ccVertexCount = (uint32_t) (numBytes / (sizeof(double) * 3));
std::vector<double> ccVertices((uint64_t) ccVertexCount * 3u, 0);
err = mcGetConnectedComponentData(context, connComp, MC_CONNECTED_COMPONENT_DATA_VERTEX_DOUBLE, numBytes, (void *) ccVertices.data(), NULL);
// query the faces
numBytes = 0;
err = mcGetConnectedComponentData(context, connComp, MC_CONNECTED_COMPONENT_DATA_FACE_TRIANGULATION, 0, NULL, &numBytes);
std::vector<uint32_t> ccFaceIndices(numBytes / sizeof(uint32_t), 0);
err = mcGetConnectedComponentData(context, connComp, MC_CONNECTED_COMPONENT_DATA_FACE_TRIANGULATION, numBytes, ccFaceIndices.data(), NULL);
std::vector<uint32_t> faceSizes(ccFaceIndices.size() / 3, 3);
const uint32_t ccFaceCount = static_cast<uint32_t>(faceSizes.size());
// Here we show, how to know when connected components, pertain particular boolean operations.
McPatchLocation patchLocation = (McPatchLocation) 0;
err = mcGetConnectedComponentData(context, connComp, MC_CONNECTED_COMPONENT_DATA_PATCH_LOCATION, sizeof(McPatchLocation), &patchLocation, NULL);
McFragmentLocation fragmentLocation = (McFragmentLocation) 0;
err = mcGetConnectedComponentData(context, connComp, MC_CONNECTED_COMPONENT_DATA_FRAGMENT_LOCATION, sizeof(McFragmentLocation), &fragmentLocation, NULL);
outMesh.vertexCoordsArray.insert(outMesh.vertexCoordsArray.end(), ccVertices.begin(), ccVertices.end());
// add offset to face index
for (size_t i = 0; i < ccFaceIndices.size(); i++) {
ccFaceIndices[i] += N_vertices;
}
int faceVertexOffsetBase = 0;
// for each face in CC
std::vector<Vec3i> faces(ccFaceCount);
for (uint32_t f = 0; f < ccFaceCount; ++f) {
bool reverseWindingOrder = (fragmentLocation == MC_FRAGMENT_LOCATION_BELOW) && (patchLocation == MC_PATCH_LOCATION_OUTSIDE);
int faceSize = faceSizes.at(f);
if (reverseWindingOrder) {
std::vector<uint32_t> faceIndex(faceSize);
// for each vertex in face
for (int v = faceSize - 1; v >= 0; v--) { faceIndex[v] = ccFaceIndices[(uint64_t) faceVertexOffsetBase + v]; }
std::copy(faceIndex.begin(), faceIndex.end(), ccFaceIndices.begin() + faceVertexOffsetBase);
}
faceVertexOffsetBase += faceSize;
}
outMesh.faceIndicesArray.insert(outMesh.faceIndicesArray.end(), ccFaceIndices.begin(), ccFaceIndices.end());
outMesh.faceSizesArray.insert(outMesh.faceSizesArray.end(), faceSizes.begin(), faceSizes.end());
N_vertices += ccVertexCount;
}
// free connected component data
err = mcReleaseConnectedComponents(context, 0, NULL);
// destroy context
err = mcReleaseContext(context);
srcMesh = outMesh;
return true;
}
void do_boolean(McutMesh& srcMesh, const McutMesh& cutMesh, const std::string& boolean_opts)
{
TriangleMesh tri_src = mcut_to_triangle_mesh(srcMesh);
std::vector<indexed_triangle_set> src_parts = its_split(tri_src.its);
TriangleMesh tri_cut = mcut_to_triangle_mesh(cutMesh);
std::vector<indexed_triangle_set> cut_parts = its_split(tri_cut.its);
if (src_parts.empty() && boolean_opts == "UNION") {
srcMesh = cutMesh;
return;
}
if(cut_parts.empty()) return;
// when src mesh has multiple connected components, mcut refuses to work.
// But we can force it to work by spliting the src mesh into disconnected components,
// and do booleans seperately, then merge all the results.
indexed_triangle_set all_its;
if (boolean_opts == "UNION" || boolean_opts == "A_NOT_B") {
for (size_t i = 0; i < src_parts.size(); i++) {
auto src_part = triangle_mesh_to_mcut(src_parts[i]);
for (size_t j = 0; j < cut_parts.size(); j++) {
auto cut_part = triangle_mesh_to_mcut(cut_parts[j]);
bool success = do_boolean_single(*src_part, *cut_part, boolean_opts);
}
TriangleMesh tri_part = mcut_to_triangle_mesh(*src_part);
its_merge(all_its, tri_part.its);
}
}
else if (boolean_opts == "INTERSECTION") {
for (size_t i = 0; i < src_parts.size(); i++) {
for (size_t j = 0; j < cut_parts.size(); j++) {
auto src_part = triangle_mesh_to_mcut(src_parts[i]);
auto cut_part = triangle_mesh_to_mcut(cut_parts[j]);
bool success = do_boolean_single(*src_part, *cut_part, boolean_opts);
if (success) {
TriangleMesh tri_part = mcut_to_triangle_mesh(*src_part);
its_merge(all_its, tri_part.its);
}
}
}
}
srcMesh = *triangle_mesh_to_mcut(all_its);
}
void make_boolean(const TriangleMesh &src_mesh, const TriangleMesh &cut_mesh, std::vector<TriangleMesh> &dst_mesh, const std::string &boolean_opts)
{
McutMesh srcMesh, cutMesh;
triangle_mesh_to_mcut(src_mesh, srcMesh);
triangle_mesh_to_mcut(cut_mesh, cutMesh);
//dst_mesh = make_boolean(srcMesh, cutMesh, boolean_opts);
do_boolean(srcMesh, cutMesh, boolean_opts);
dst_mesh.push_back(mcut_to_triangle_mesh(srcMesh));
}
} // namespace mcut
} // namespace MeshBoolean
} // namespace Slic3r