From 6b0af4366540976235a91d1041f075218de1a420 Mon Sep 17 00:00:00 2001
From: tamasmeszaros <meszaros.q@gmail.com>
Date: Thu, 9 Jan 2020 11:19:52 +0100
Subject: [PATCH] Use mesh boolean in the backend to drill holes.

---
 src/libslic3r/SLA/Hollowing.cpp | 12 ++------
 src/libslic3r/SLAPrint.hpp      |  9 +++++-
 src/libslic3r/SLAPrintSteps.cpp | 52 ++++++++++++++++-----------------
 src/slic3r/GUI/GLCanvas3D.cpp   |  6 ++--
 4 files changed, 39 insertions(+), 40 deletions(-)

diff --git a/src/libslic3r/SLA/Hollowing.cpp b/src/libslic3r/SLA/Hollowing.cpp
index 1ce0c4c67..ecc42b889 100644
--- a/src/libslic3r/SLA/Hollowing.cpp
+++ b/src/libslic3r/SLA/Hollowing.cpp
@@ -123,6 +123,7 @@ Contour3D DrainHole::to_mesh() const
     Eigen::Quaterniond q;
     q.setFromTwoVectors(Vec3d{0., 0., 1.}, normal.cast<double>());
     for(auto& p : hole.points) p = q * p + pos.cast<double>();
+    
     return hole;
 }
 
@@ -251,15 +252,8 @@ void cut_drainholes(std::vector<ExPolygons> & obj_slices,
                     std::function<void(void)> thr)
 {
     TriangleMesh mesh;
-    for (const sla::DrainHole &holept : holes) {
-        auto r = double(holept.radius);
-        auto h = double(holept.height);
-        sla::Contour3D hole = sla::cylinder(r, h);
-        Eigen::Quaterniond q;
-        q.setFromTwoVectors(Vec3d{0., 0., 1.}, holept.normal.cast<double>());
-        for(auto& p : hole.points) p = q * p + holept.pos.cast<double>();
-        mesh.merge(sla::to_triangle_mesh(hole));
-    }
+    for (const sla::DrainHole &holept : holes)
+        mesh.merge(sla::to_triangle_mesh(holept.to_mesh()));
     
     if (mesh.empty()) return;
     
diff --git a/src/libslic3r/SLAPrint.hpp b/src/libslic3r/SLAPrint.hpp
index 1b8e18a2a..20580772b 100644
--- a/src/libslic3r/SLAPrint.hpp
+++ b/src/libslic3r/SLAPrint.hpp
@@ -80,6 +80,13 @@ public:
     
     // Ready after this->is_step_done(slaposHollowing) is true
     const TriangleMesh&     hollowed_interior_mesh() const;
+    
+    // Get the mesh that is going to be printed with all the modifications
+    // like hollowing and drilled holes.
+    const TriangleMesh & get_mesh_to_print() const {
+        bool is_hollowing = m_config.hollowing_enable.getBool() && m_hollowing_data;
+        return is_hollowing ? m_hollowing_data->hollow_mesh_with_holes : transformed_mesh();
+    }
 
     // This will return the transformed mesh which is cached
     const TriangleMesh&     transformed_mesh() const;
@@ -318,7 +325,7 @@ private:
     public:
         
         TriangleMesh interior;
-        // std::vector<drillpoints>
+        mutable TriangleMesh hollow_mesh_with_holes; // caching the complete hollowed mesh
     };
     
     std::unique_ptr<HollowingData> m_hollowing_data;
diff --git a/src/libslic3r/SLAPrintSteps.cpp b/src/libslic3r/SLAPrintSteps.cpp
index 0ae0e66a4..e94110cf6 100644
--- a/src/libslic3r/SLAPrintSteps.cpp
+++ b/src/libslic3r/SLAPrintSteps.cpp
@@ -1,5 +1,5 @@
 #include <libslic3r/SLAPrintSteps.hpp>
-
+#include <libslic3r/MeshBoolean.hpp>
 
 // Need the cylinder method for the the drainholes in hollowing step
 #include <libslic3r/SLA/SupportTreeBuilder.hpp>
@@ -99,6 +99,23 @@ void SLAPrint::Steps::hollow_model(SLAPrintObject &po)
     
     if (po.m_hollowing_data->interior.empty())
         BOOST_LOG_TRIVIAL(warning) << "Hollowed interior is empty!";
+    
+    auto &hollowed_mesh = po.m_hollowing_data->hollow_mesh_with_holes;
+    hollowed_mesh = po.transformed_mesh();
+    hollowed_mesh.merge(po.m_hollowing_data->interior);
+    hollowed_mesh.require_shared_vertices();
+    
+    sla::DrainHoles drainholes = po.transformed_drainhole_points();
+    
+    TriangleMesh holes_mesh;
+    
+    for (const sla::DrainHole &holept : drainholes)
+        holes_mesh.merge(sla::to_triangle_mesh(holept.to_mesh()));
+    
+    holes_mesh.require_shared_vertices();
+    MeshBoolean::minus(hollowed_mesh, holes_mesh);
+    
+    hollowed_mesh.require_shared_vertices();
 }
 
 // The slicing will be performed on an imaginary 1D grid which starts from
@@ -111,18 +128,8 @@ void SLAPrint::Steps::hollow_model(SLAPrintObject &po)
 // same imaginary grid (the height vector argument to TriangleMeshSlicer).
 void SLAPrint::Steps::slice_model(SLAPrintObject &po)
 {   
-    TriangleMesh hollowed_mesh;
-    
-    bool is_hollowing = po.m_config.hollowing_enable.getBool() && po.m_hollowing_data;
-    
-    if (is_hollowing) {
-        hollowed_mesh = po.transformed_mesh();
-        hollowed_mesh.merge(po.m_hollowing_data->interior);
-        hollowed_mesh.require_shared_vertices();
-    }
-    
-    const TriangleMesh &mesh = is_hollowing ? hollowed_mesh : po.transformed_mesh();
-    
+    const TriangleMesh &mesh = po.get_mesh_to_print();
+
     // We need to prepare the slice index...
     
     double  lhd  = m_print->m_objects.front()->m_config.layer_height.getFloat();
@@ -168,8 +175,8 @@ void SLAPrint::Steps::slice_model(SLAPrintObject &po)
     auto &slice_grid = po.m_model_height_levels;
     slicer.slice(slice_grid, closing_r, &po.m_model_slices, thr);
     
-    sla::DrainHoles drainholes = po.transformed_drainhole_points();
-    cut_drainholes(po.m_model_slices, slice_grid, closing_r, drainholes, thr);
+//    sla::DrainHoles drainholes = po.transformed_drainhole_points();
+//    cut_drainholes(po.m_model_slices, slice_grid, closing_r, drainholes, thr);
     
     auto mit = slindex_it;
     double doffs = m_print->m_printer_config.absolute_correction.getFloat();
@@ -199,16 +206,7 @@ void SLAPrint::Steps::support_points(SLAPrintObject &po)
     // If supports are disabled, we can skip the model scan.
     if(!po.m_config.supports_enable.getBool()) return;
     
-    bool is_hollowing = po.m_config.hollowing_enable.getBool() && po.m_hollowing_data;
-    
-    TriangleMesh hollowed_mesh;
-    if (is_hollowing) {
-        hollowed_mesh = po.transformed_mesh();
-        hollowed_mesh.merge(po.m_hollowing_data->interior);
-        hollowed_mesh.require_shared_vertices();
-    }
-    
-    const TriangleMesh &mesh = is_hollowing ? hollowed_mesh : po.transformed_mesh();
+    const TriangleMesh &mesh = po.get_mesh_to_print();
     
     if (!po.m_supportdata)
         po.m_supportdata.reset(new SLAPrintObject::SupportData(mesh));
@@ -229,7 +227,7 @@ void SLAPrint::Steps::support_points(SLAPrintObject &po)
         // Tell the mesh where drain holes are. Although the points are
         // calculated on slices, the algorithm then raycasts the points
         // so they actually lie on the mesh.
-        po.m_supportdata->emesh.load_holes(po.transformed_drainhole_points());
+//        po.m_supportdata->emesh.load_holes(po.transformed_drainhole_points());
         
         throw_if_canceled();
         sla::SupportPointGenerator::Config config;
@@ -298,7 +296,7 @@ void SLAPrint::Steps::support_tree(SLAPrintObject &po)
         po.m_supportdata->emesh.ground_level_offset(pcfg.wall_thickness_mm);
     
     po.m_supportdata->cfg = make_support_cfg(po.m_config);
-    po.m_supportdata->emesh.load_holes(po.transformed_drainhole_points());
+//    po.m_supportdata->emesh.load_holes(po.transformed_drainhole_points());
     
     // scaling for the sub operations
     double d = objectstep_scale * OBJ_STEP_LEVELS[slaposSupportTree] / 100.0;
diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp
index 2e90a87ee..8805dc92c 100644
--- a/src/slic3r/GUI/GLCanvas3D.cpp
+++ b/src/slic3r/GUI/GLCanvas3D.cpp
@@ -6060,9 +6060,9 @@ void GLCanvas3D::_load_sla_shells()
         if (obj->is_step_done(slaposSliceSupports)) {
             unsigned int initial_volumes_count = (unsigned int)m_volumes.volumes.size();
             for (const SLAPrintObject::Instance& instance : obj->instances()) {
-                add_volume(*obj, 0, instance, obj->transformed_mesh(), GLVolume::MODEL_COLOR[0], true);
-                if (! obj->hollowed_interior_mesh().empty())
-                    add_volume(*obj, -int(slaposHollowing), instance, obj->hollowed_interior_mesh(), GLVolume::MODEL_COLOR[0], false);
+                add_volume(*obj, 0, instance, obj->get_mesh_to_print(), GLVolume::MODEL_COLOR[0], true);
+//                if (! obj->hollowed_interior_mesh().empty())
+//                    add_volume(*obj, -int(slaposHollowing), instance, obj->hollowed_interior_mesh(), GLVolume::MODEL_COLOR[0], false);
                 // Set the extruder_id and volume_id to achieve the same color as in the 3D scene when
                 // through the update_volumes_colors_by_extruder() call.
                 m_volumes.volumes.back()->extruder_id = obj->model_object()->volumes.front()->extruder_id();