From fb9d8b2025a2d44f53c3c044f95dcca716485870 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Sun, 24 May 2020 00:01:34 +0200 Subject: [PATCH 01/12] Include cleanup: do not include Model.hpp from 3DScene.hpp --- src/libslic3r/Model.cpp | 4 ++-- src/libslic3r/Model.hpp | 29 +++++++++++++++-------------- src/slic3r/GUI/3DScene.cpp | 12 ++++++------ src/slic3r/GUI/3DScene.hpp | 8 ++++++-- src/slic3r/GUI/GLCanvas3D.cpp | 10 +++++----- src/slic3r/GUI/GLCanvas3D.hpp | 8 +++++--- src/slic3r/GUI/GUI_Preview.hpp | 2 +- src/slic3r/GUI/Plater.cpp | 4 ++-- src/slic3r/GUI/Selection.hpp | 2 ++ 9 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index aabc44f28..5e940b842 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -1383,8 +1383,8 @@ unsigned int ModelObject::check_instances_print_volume_state(const BoundingBoxf3 inside_outside |= OUTSIDE; } model_instance->print_volume_state = - (inside_outside == (INSIDE | OUTSIDE)) ? ModelInstance::PVS_Partly_Outside : - (inside_outside == INSIDE) ? ModelInstance::PVS_Inside : ModelInstance::PVS_Fully_Outside; + (inside_outside == (INSIDE | OUTSIDE)) ? ModelInstancePVS_Partly_Outside : + (inside_outside == INSIDE) ? ModelInstancePVS_Inside : ModelInstancePVS_Fully_Outside; if (inside_outside == INSIDE) ++ num_printable; } diff --git a/src/libslic3r/Model.hpp b/src/libslic3r/Model.hpp index e3a136e11..f7b63bff4 100644 --- a/src/libslic3r/Model.hpp +++ b/src/libslic3r/Model.hpp @@ -3,7 +3,6 @@ #include "libslic3r.h" #include "Geometry.hpp" -#include "Layer.hpp" #include "ObjectID.hpp" #include "Point.hpp" #include "PrintConfig.hpp" @@ -641,25 +640,26 @@ private: } }; + +enum ModelInstanceEPrintVolumeState : unsigned char +{ + ModelInstancePVS_Inside, + ModelInstancePVS_Partly_Outside, + ModelInstancePVS_Fully_Outside, + ModelInstanceNum_BedStates +}; + + // A single instance of a ModelObject. // Knows the affine transformation of an object. class ModelInstance final : public ObjectBase { -public: - enum EPrintVolumeState : unsigned char - { - PVS_Inside, - PVS_Partly_Outside, - PVS_Fully_Outside, - Num_BedStates - }; - private: Geometry::Transformation m_transformation; public: // flag showing the position of this instance with respect to the print volume (set by Print::validate() using ModelObject::check_instances_print_volume_state()) - EPrintVolumeState print_volume_state; + ModelInstanceEPrintVolumeState print_volume_state; // Whether or not this instance is printable bool printable; @@ -706,7 +706,7 @@ public: const Transform3d& get_matrix(bool dont_translate = false, bool dont_rotate = false, bool dont_scale = false, bool dont_mirror = false) const { return m_transformation.get_matrix(dont_translate, dont_rotate, dont_scale, dont_mirror); } - bool is_printable() const { return object->printable && printable && (print_volume_state == PVS_Inside); } + bool is_printable() const { return object->printable && printable && (print_volume_state == ModelInstancePVS_Inside); } // Getting the input polygon for arrange arrangement::ArrangePolygon get_arrange_polygon() const; @@ -735,10 +735,10 @@ private: ModelObject* object; // Constructor, which assigns a new unique ID. - explicit ModelInstance(ModelObject* object) : print_volume_state(PVS_Inside), printable(true), object(object) { assert(this->id().valid()); } + explicit ModelInstance(ModelObject* object) : print_volume_state(ModelInstancePVS_Inside), printable(true), object(object) { assert(this->id().valid()); } // Constructor, which assigns a new unique ID. explicit ModelInstance(ModelObject *object, const ModelInstance &other) : - m_transformation(other.m_transformation), print_volume_state(PVS_Inside), printable(other.printable), object(object) { assert(this->id().valid() && this->id() != other.id()); } + m_transformation(other.m_transformation), print_volume_state(ModelInstancePVS_Inside), printable(other.printable), object(object) { assert(this->id().valid() && this->id() != other.id()); } explicit ModelInstance(ModelInstance &&rhs) = delete; ModelInstance& operator=(const ModelInstance &rhs) = delete; @@ -753,6 +753,7 @@ private: } }; + class ModelWipeTower final : public ObjectBase { public: diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index c28c93752..2fee7050a 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -724,7 +724,7 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, bool disab glsafe(::glDisable(GL_BLEND)); } -bool GLVolumeCollection::check_outside_state(const DynamicPrintConfig* config, ModelInstance::EPrintVolumeState* out_state) +bool GLVolumeCollection::check_outside_state(const DynamicPrintConfig* config, ModelInstanceEPrintVolumeState* out_state) { if (config == nullptr) return false; @@ -738,7 +738,7 @@ bool GLVolumeCollection::check_outside_state(const DynamicPrintConfig* config, M // Allow the objects to protrude below the print bed print_volume.min(2) = -1e10; - ModelInstance::EPrintVolumeState state = ModelInstance::PVS_Inside; + ModelInstanceEPrintVolumeState state = ModelInstancePVS_Inside; bool contained_min_one = false; @@ -757,11 +757,11 @@ bool GLVolumeCollection::check_outside_state(const DynamicPrintConfig* config, M if (contained) contained_min_one = true; - if ((state == ModelInstance::PVS_Inside) && volume->is_outside) - state = ModelInstance::PVS_Fully_Outside; + if ((state == ModelInstancePVS_Inside) && volume->is_outside) + state = ModelInstancePVS_Fully_Outside; - if ((state == ModelInstance::PVS_Fully_Outside) && volume->is_outside && print_volume.intersects(bb)) - state = ModelInstance::PVS_Partly_Outside; + if ((state == ModelInstancePVS_Fully_Outside) && volume->is_outside && print_volume.intersects(bb)) + state = ModelInstancePVS_Partly_Outside; } if (out_state != nullptr) diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index 07c5cd53e..b6decf6c8 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -6,7 +6,8 @@ #include "libslic3r/Line.hpp" #include "libslic3r/TriangleMesh.hpp" #include "libslic3r/Utils.hpp" -#include "libslic3r/Model.hpp" +//#include "libslic3r/Model.hpp" +#include "libslic3r/Geometry.hpp" #include @@ -40,6 +41,9 @@ class ExtrusionMultiPath; class ExtrusionLoop; class ExtrusionEntity; class ExtrusionEntityCollection; +class ModelObject; +class ModelVolume; +enum ModelInstanceEPrintVolumeState : unsigned char; // A container for interleaved arrays of 3D vertices and normals, // possibly indexed by triangles and / or quads. @@ -578,7 +582,7 @@ public: // returns true if all the volumes are completely contained in the print volume // returns the containment state in the given out_state, if non-null - bool check_outside_state(const DynamicPrintConfig* config, ModelInstance::EPrintVolumeState* out_state); + bool check_outside_state(const DynamicPrintConfig* config, ModelInstanceEPrintVolumeState* out_state); void reset_outside_state(); void update_colors_by_extruder(const DynamicPrintConfig* config); diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 03e95b8a9..90a82f76d 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -1694,7 +1694,7 @@ void GLCanvas3D::reset_volumes() int GLCanvas3D::check_volumes_outside_state() const { - ModelInstance::EPrintVolumeState state; + ModelInstanceEPrintVolumeState state; m_volumes.check_outside_state(m_config, &state); return (int)state; } @@ -2578,15 +2578,15 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re // checks for geometry outside the print volume to render it accordingly if (!m_volumes.empty()) { - ModelInstance::EPrintVolumeState state; + ModelInstanceEPrintVolumeState state; const bool contained_min_one = m_volumes.check_outside_state(m_config, &state); - _set_warning_texture(WarningTexture::ObjectClashed, state == ModelInstance::PVS_Partly_Outside); - _set_warning_texture(WarningTexture::ObjectOutside, state == ModelInstance::PVS_Fully_Outside); + _set_warning_texture(WarningTexture::ObjectClashed, state == ModelInstancePVS_Partly_Outside); + _set_warning_texture(WarningTexture::ObjectOutside, state == ModelInstancePVS_Fully_Outside); post_event(Event(EVT_GLCANVAS_ENABLE_ACTION_BUTTONS, - contained_min_one && !m_model->objects.empty() && state != ModelInstance::PVS_Partly_Outside)); + contained_min_one && !m_model->objects.empty() && state != ModelInstancePVS_Partly_Outside)); } else { diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 0317eb0b3..499c9f85b 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -15,6 +15,8 @@ #include "GLSelectionRectangle.hpp" #include "MeshUtils.hpp" +#include "libslic3r/Slicing.hpp" + #include #include @@ -33,13 +35,13 @@ class wxGLContext; namespace Slic3r { -class Bed3D; struct Camera; class BackgroundSlicingProcess; class GCodePreviewData; struct ThumbnailData; -struct SlicingParameters; -enum LayerHeightEditActionType : unsigned int; +class ModelObject; +class ModelInstance; +class PrintObject; namespace GUI { diff --git a/src/slic3r/GUI/GUI_Preview.hpp b/src/slic3r/GUI/GUI_Preview.hpp index 9d7c6c031..5df3e38c6 100644 --- a/src/slic3r/GUI/GUI_Preview.hpp +++ b/src/slic3r/GUI/GUI_Preview.hpp @@ -3,9 +3,9 @@ #include #include "libslic3r/Point.hpp" +#include "libslic3r/CustomGCode.hpp" #include -#include "libslic3r/Model.hpp" class wxNotebook; class wxGLCanvas; diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index cea671c4f..a9bfcfbac 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -2626,7 +2626,7 @@ void Plater::priv::object_list_changed() { const bool export_in_progress = this->background_process.is_export_scheduled(); // || ! send_gcode_file.empty()); // XXX: is this right? - const bool model_fits = view3D->check_volumes_outside_state() == ModelInstance::PVS_Inside; + const bool model_fits = view3D->check_volumes_outside_state() == ModelInstancePVS_Inside; sidebar->enable_buttons(!model.objects.empty() && !export_in_progress && model_fits); } @@ -3318,7 +3318,7 @@ void Plater::priv::set_current_panel(wxPanel* panel) // see: Plater::priv::object_list_changed() // FIXME: it may be better to have a single function making this check and let it be called wherever needed bool export_in_progress = this->background_process.is_export_scheduled(); - bool model_fits = view3D->check_volumes_outside_state() != ModelInstance::PVS_Partly_Outside; + bool model_fits = view3D->check_volumes_outside_state() != ModelInstancePVS_Partly_Outside; if (!model.objects.empty() && !export_in_progress && model_fits) this->q->reslice(); // keeps current gcode preview, if any diff --git a/src/slic3r/GUI/Selection.hpp b/src/slic3r/GUI/Selection.hpp index c27b4cc29..fb93bef64 100644 --- a/src/slic3r/GUI/Selection.hpp +++ b/src/slic3r/GUI/Selection.hpp @@ -3,8 +3,10 @@ #include #include "libslic3r/Geometry.hpp" +#include "libslic3r/Model.hpp" #include "3DScene.hpp" + #if ENABLE_RENDER_SELECTION_CENTER class GLUquadric; typedef class GLUquadric GLUquadricObj; From cc5fe02cde44a36c677666e2df0270641b2987cf Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Wed, 4 Mar 2020 11:32:22 +0100 Subject: [PATCH 02/12] Include cleanup: GUI_ObjectManipulation.hpp, GUI, GUI_Preview --- src/slic3r/GUI/GUI.cpp | 21 ++++++--------------- src/slic3r/GUI/GUI_App.cpp | 1 + src/slic3r/GUI/GUI_ObjectManipulation.cpp | 1 + src/slic3r/GUI/GUI_ObjectManipulation.hpp | 3 ++- src/slic3r/GUI/GUI_Preview.cpp | 1 - src/slic3r/GUI/GUI_Preview.hpp | 2 ++ 6 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/slic3r/GUI/GUI.cpp b/src/slic3r/GUI/GUI.cpp index 7deed0786..18d4bee94 100644 --- a/src/slic3r/GUI/GUI.cpp +++ b/src/slic3r/GUI/GUI.cpp @@ -1,12 +1,9 @@ #include "GUI.hpp" #include "GUI_App.hpp" #include "I18N.hpp" -#include "WipeTowerDialog.hpp" -#include #include -#include #include #if __APPLE__ @@ -18,22 +15,16 @@ #include "boost/nowide/convert.hpp" #endif -#include - -#include "wxExtensions.hpp" -#include "GUI_Preview.hpp" #include "AboutDialog.hpp" -#include "AppConfig.hpp" -#include "ConfigWizard.hpp" -#include "PresetBundle.hpp" -#include "UpdateDialogs.hpp" +#include "MsgDialog.hpp" -#include "libslic3r/Utils.hpp" #include "libslic3r/Print.hpp" -#include "Tab.hpp" -#include "GUI_ObjectList.hpp" -namespace Slic3r { namespace GUI { +namespace Slic3r { + +class AppConfig; + +namespace GUI { #if __APPLE__ IOPMAssertionID assertionID; diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 88018c87f..4c53a5b94 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -36,6 +36,7 @@ #include "GUI_Utils.hpp" #include "AppConfig.hpp" #include "PresetBundle.hpp" +#include "3DScene.hpp" #include "../Utils/PresetUpdater.hpp" #include "../Utils/PrintHost.hpp" diff --git a/src/slic3r/GUI/GUI_ObjectManipulation.cpp b/src/slic3r/GUI/GUI_ObjectManipulation.cpp index 32de03c98..6a9e2ddc1 100644 --- a/src/slic3r/GUI/GUI_ObjectManipulation.cpp +++ b/src/slic3r/GUI/GUI_ObjectManipulation.cpp @@ -2,6 +2,7 @@ #include "GUI_ObjectList.hpp" #include "I18N.hpp" +#include "GLCanvas3D.hpp" #include "OptionsGroup.hpp" #include "wxExtensions.hpp" #include "PresetBundle.hpp" diff --git a/src/slic3r/GUI/GUI_ObjectManipulation.hpp b/src/slic3r/GUI/GUI_ObjectManipulation.hpp index f002491f0..560fbb400 100644 --- a/src/slic3r/GUI/GUI_ObjectManipulation.hpp +++ b/src/slic3r/GUI/GUI_ObjectManipulation.hpp @@ -4,7 +4,8 @@ #include #include "GUI_ObjectSettings.hpp" -#include "GLCanvas3D.hpp" +#include "libslic3r/Point.hpp" +#include class wxBitmapComboBox; class wxStaticText; diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index 8cf524f7d..ba5aab2e5 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -4,7 +4,6 @@ #include "GUI_App.hpp" #include "GUI.hpp" #include "I18N.hpp" -#include "AppConfig.hpp" #include "3DScene.hpp" #include "BackgroundSlicingProcess.hpp" #include "OpenGLManager.hpp" diff --git a/src/slic3r/GUI/GUI_Preview.hpp b/src/slic3r/GUI/GUI_Preview.hpp index 5df3e38c6..bbf2774b8 100644 --- a/src/slic3r/GUI/GUI_Preview.hpp +++ b/src/slic3r/GUI/GUI_Preview.hpp @@ -2,11 +2,13 @@ #define slic3r_GUI_Preview_hpp_ #include + #include "libslic3r/Point.hpp" #include "libslic3r/CustomGCode.hpp" #include + class wxNotebook; class wxGLCanvas; class wxBoxSizer; From 02838eaa30ec5d81f4d1995bdde79456e11cded3 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Sun, 24 May 2020 00:50:11 +0200 Subject: [PATCH 03/12] Slight include cleanup --- src/libslic3r/Arrange.cpp | 3 ++- src/libslic3r/Arrange.hpp | 7 +++++-- src/libslic3r/Fill/Fill.hpp | 1 - src/libslic3r/MTUtils.hpp | 2 -- src/libslic3r/SLA/SupportPointGenerator.hpp | 1 - src/slic3r/GUI/GUI.cpp | 1 + src/slic3r/Utils/SLAImport.hpp | 1 - 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libslic3r/Arrange.cpp b/src/libslic3r/Arrange.cpp index 3f535db86..de6b34c1f 100644 --- a/src/libslic3r/Arrange.cpp +++ b/src/libslic3r/Arrange.cpp @@ -1,7 +1,8 @@ #include "Arrange.hpp" -//#include "Geometry.hpp" #include "SVG.hpp" +#include "BoundingBox.hpp" + #include #include #include diff --git a/src/libslic3r/Arrange.hpp b/src/libslic3r/Arrange.hpp index 8ff1e37a9..7630ab3e8 100644 --- a/src/libslic3r/Arrange.hpp +++ b/src/libslic3r/Arrange.hpp @@ -2,9 +2,12 @@ #define ARRANGE_HPP #include "ExPolygon.hpp" -#include "BoundingBox.hpp" -namespace Slic3r { namespace arrangement { +namespace Slic3r { + +class BoundingBox; + +namespace arrangement { /// A geometry abstraction for a circular print bed. Similarly to BoundingBox. class CircleBed { diff --git a/src/libslic3r/Fill/Fill.hpp b/src/libslic3r/Fill/Fill.hpp index 9e3545084..64963495a 100644 --- a/src/libslic3r/Fill/Fill.hpp +++ b/src/libslic3r/Fill/Fill.hpp @@ -6,7 +6,6 @@ #include #include "../libslic3r.h" -#include "../BoundingBox.hpp" #include "../PrintConfig.hpp" #include "FillBase.hpp" diff --git a/src/libslic3r/MTUtils.hpp b/src/libslic3r/MTUtils.hpp index 294f9ae6f..a6c8d6162 100644 --- a/src/libslic3r/MTUtils.hpp +++ b/src/libslic3r/MTUtils.hpp @@ -10,8 +10,6 @@ #include #include "libslic3r.h" -#include "Point.hpp" -#include "BoundingBox.hpp" namespace Slic3r { diff --git a/src/libslic3r/SLA/SupportPointGenerator.hpp b/src/libslic3r/SLA/SupportPointGenerator.hpp index 738fc5730..1621cde50 100644 --- a/src/libslic3r/SLA/SupportPointGenerator.hpp +++ b/src/libslic3r/SLA/SupportPointGenerator.hpp @@ -9,7 +9,6 @@ #include #include -#include #include diff --git a/src/slic3r/GUI/GUI.cpp b/src/slic3r/GUI/GUI.cpp index 18d4bee94..b9516b12f 100644 --- a/src/slic3r/GUI/GUI.cpp +++ b/src/slic3r/GUI/GUI.cpp @@ -5,6 +5,7 @@ #include #include +#include #if __APPLE__ #import diff --git a/src/slic3r/Utils/SLAImport.hpp b/src/slic3r/Utils/SLAImport.hpp index a819bd7e7..73995014f 100644 --- a/src/slic3r/Utils/SLAImport.hpp +++ b/src/slic3r/Utils/SLAImport.hpp @@ -4,7 +4,6 @@ #include #include -#include #include namespace Slic3r { From c2cd43094130d45522c565f26900a346265e0d48 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Tue, 26 May 2020 11:09:38 +0200 Subject: [PATCH 04/12] Few more include chains broken --- src/libslic3r/GCode.cpp | 1 + src/libslic3r/GCode.hpp | 5 ++++- src/libslic3r/GCode/PrintExtents.cpp | 1 + src/libslic3r/GCode/ThumbnailData.cpp | 1 - src/libslic3r/GCode/ThumbnailData.hpp | 2 +- src/libslic3r/GCode/ToolOrdering.cpp | 2 +- src/libslic3r/GCode/ToolOrdering.hpp | 2 ++ src/libslic3r/Print.hpp | 20 ++++++----------- src/libslic3r/PrintObject.cpp | 25 +++++++++++++++++++++ src/slic3r/GUI/BackgroundSlicingProcess.hpp | 10 ++++----- src/slic3r/GUI/GLCanvas3D.cpp | 1 + 11 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index b5890f578..51dd9f929 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -7,6 +7,7 @@ #include "GCode/PrintExtents.hpp" #include "GCode/WipeTower.hpp" #include "ShortestPath.hpp" +#include "Print.hpp" #include "Utils.hpp" #include "libslic3r.h" diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index 2daf0fe16..8d4733783 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -8,7 +8,6 @@ #include "MotionPlanner.hpp" #include "Point.hpp" #include "PlaceholderParser.hpp" -#include "Print.hpp" #include "PrintConfig.hpp" #include "GCode/CoolingBuffer.hpp" #include "GCode/SpiralVase.hpp" @@ -32,6 +31,10 @@ namespace Slic3r { class GCode; class GCodePreviewData; +namespace { struct Item; } +struct PrintInstance; +using PrintObjectPtrs = std::vector; + class AvoidCrossingPerimeters { public: diff --git a/src/libslic3r/GCode/PrintExtents.cpp b/src/libslic3r/GCode/PrintExtents.cpp index 7a8271e30..4a6624531 100644 --- a/src/libslic3r/GCode/PrintExtents.cpp +++ b/src/libslic3r/GCode/PrintExtents.cpp @@ -6,6 +6,7 @@ #include "../BoundingBox.hpp" #include "../ExtrusionEntity.hpp" #include "../ExtrusionEntityCollection.hpp" +#include "../Layer.hpp" #include "../Print.hpp" #include "PrintExtents.hpp" diff --git a/src/libslic3r/GCode/ThumbnailData.cpp b/src/libslic3r/GCode/ThumbnailData.cpp index 09d564e95..a5941bff1 100644 --- a/src/libslic3r/GCode/ThumbnailData.cpp +++ b/src/libslic3r/GCode/ThumbnailData.cpp @@ -1,4 +1,3 @@ -#include "libslic3r/libslic3r.h" #include "ThumbnailData.hpp" namespace Slic3r { diff --git a/src/libslic3r/GCode/ThumbnailData.hpp b/src/libslic3r/GCode/ThumbnailData.hpp index 473ef274b..2a302ed85 100644 --- a/src/libslic3r/GCode/ThumbnailData.hpp +++ b/src/libslic3r/GCode/ThumbnailData.hpp @@ -24,4 +24,4 @@ typedef std::function -#include "../GCodeWriter.hpp" namespace Slic3r { diff --git a/src/libslic3r/GCode/ToolOrdering.hpp b/src/libslic3r/GCode/ToolOrdering.hpp index 5fe27516d..e2e07533f 100644 --- a/src/libslic3r/GCode/ToolOrdering.hpp +++ b/src/libslic3r/GCode/ToolOrdering.hpp @@ -14,6 +14,8 @@ namespace Slic3r { class Print; class PrintObject; class LayerTools; +namespace CustomGCode { struct Item; } +class PrintRegion; diff --git a/src/libslic3r/Print.hpp b/src/libslic3r/Print.hpp index 54ebceeb6..5fb395fb1 100644 --- a/src/libslic3r/Print.hpp +++ b/src/libslic3r/Print.hpp @@ -4,10 +4,9 @@ #include "PrintBase.hpp" #include "BoundingBox.hpp" +#include "ExtrusionEntityCollection.hpp" #include "Flow.hpp" #include "Point.hpp" -#include "Layer.hpp" -#include "Model.hpp" #include "Slicing.hpp" #include "GCode/ToolOrdering.hpp" #include "GCode/WipeTower.hpp" @@ -23,6 +22,8 @@ class ModelObject; class GCode; class GCodePreviewData; enum class SlicingMode : uint32_t; +class Layer; +class SupportLayer; // Print step IDs for keeping track of the print state. enum PrintStep { @@ -147,18 +148,11 @@ public: const Layer* get_layer(int idx) const { return m_layers[idx]; } Layer* get_layer(int idx) { return m_layers[idx]; } // Get a layer exactly at print_z. - const Layer* get_layer_at_printz(coordf_t print_z) const { - auto it = Slic3r::lower_bound_by_predicate(m_layers.begin(), m_layers.end(), [print_z](const Layer *layer) { return layer->print_z < print_z; }); - return (it == m_layers.end() || (*it)->print_z != print_z) ? nullptr : *it; - } - Layer* get_layer_at_printz(coordf_t print_z) { return const_cast(std::as_const(*this).get_layer_at_printz(print_z)); } + const Layer* get_layer_at_printz(coordf_t print_z) const; + Layer* get_layer_at_printz(coordf_t print_z); // Get a layer approximately at print_z. - const Layer* get_layer_at_printz(coordf_t print_z, coordf_t epsilon) const { - coordf_t limit = print_z - epsilon; - auto it = Slic3r::lower_bound_by_predicate(m_layers.begin(), m_layers.end(), [limit](const Layer *layer) { return layer->print_z < limit; }); - return (it == m_layers.end() || (*it)->print_z > print_z + epsilon) ? nullptr : *it; - } - Layer* get_layer_at_printz(coordf_t print_z, coordf_t epsilon) { return const_cast(std::as_const(*this).get_layer_at_printz(print_z, epsilon)); } + const Layer* get_layer_at_printz(coordf_t print_z, coordf_t epsilon) const; + Layer* get_layer_at_printz(coordf_t print_z, coordf_t epsilon); // print_z: top of the layer; slice_z: center of the layer. Layer* add_layer(int id, coordf_t height, coordf_t print_z, coordf_t slice_z); diff --git a/src/libslic3r/PrintObject.cpp b/src/libslic3r/PrintObject.cpp index 5174fd6e9..a5cb54485 100644 --- a/src/libslic3r/PrintObject.cpp +++ b/src/libslic3r/PrintObject.cpp @@ -4,6 +4,7 @@ #include "ElephantFootCompensation.hpp" #include "Geometry.hpp" #include "I18N.hpp" +#include "Layer.hpp" #include "SupportMaterial.hpp" #include "Surface.hpp" #include "Slicing.hpp" @@ -2829,4 +2830,28 @@ void PrintObject::project_and_append_custom_supports( } // loop over ModelVolumes } + + +const Layer* PrintObject::get_layer_at_printz(coordf_t print_z) const { + auto it = Slic3r::lower_bound_by_predicate(m_layers.begin(), m_layers.end(), [print_z](const Layer *layer) { return layer->print_z < print_z; }); + return (it == m_layers.end() || (*it)->print_z != print_z) ? nullptr : *it; +} + + + +Layer* PrintObject::get_layer_at_printz(coordf_t print_z) { return const_cast(std::as_const(*this).get_layer_at_printz(print_z)); } + + + +// Get a layer approximately at print_z. +const Layer* PrintObject::get_layer_at_printz(coordf_t print_z, coordf_t epsilon) const { + coordf_t limit = print_z - epsilon; + auto it = Slic3r::lower_bound_by_predicate(m_layers.begin(), m_layers.end(), [limit](const Layer *layer) { return layer->print_z < limit; }); + return (it == m_layers.end() || (*it)->print_z > print_z + epsilon) ? nullptr : *it; +} + + + +Layer* PrintObject::get_layer_at_printz(coordf_t print_z, coordf_t epsilon) { return const_cast(std::as_const(*this).get_layer_at_printz(print_z, epsilon)); } + } // namespace Slic3r diff --git a/src/slic3r/GUI/BackgroundSlicingProcess.hpp b/src/slic3r/GUI/BackgroundSlicingProcess.hpp index efaea1d11..38e9e1075 100644 --- a/src/slic3r/GUI/BackgroundSlicingProcess.hpp +++ b/src/slic3r/GUI/BackgroundSlicingProcess.hpp @@ -5,22 +5,22 @@ #include #include -#include - #include -#include "libslic3r/Print.hpp" +#include "libslic3r/PrintBase.hpp" +#include "libslic3r/GCode/ThumbnailData.hpp" #include "libslic3r/Format/SL1.hpp" #include "slic3r/Utils/PrintHost.hpp" +namespace boost { namespace filesystem { class path; } } + namespace Slic3r { class DynamicPrintConfig; class GCodePreviewData; class Model; class SLAPrint; -class SL1Archive; class SlicingStatusEvent : public wxEvent { @@ -86,7 +86,7 @@ public: // Apply config over the print. Returns false, if the new config values caused any of the already // processed steps to be invalidated, therefore the task will need to be restarted. - Print::ApplyStatus apply(const Model &model, const DynamicPrintConfig &config); + PrintBase::ApplyStatus apply(const Model &model, const DynamicPrintConfig &config); // After calling the apply() function, set_task() may be called to limit the task to be processed by process(). // This is useful for calculating SLA supports for a single object only. void set_task(const PrintBase::TaskParams ¶ms); diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 90a82f76d..1266fef4c 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -9,6 +9,7 @@ #include "libslic3r/GCode/ThumbnailData.hpp" #include "libslic3r/Geometry.hpp" #include "libslic3r/ExtrusionEntity.hpp" +#include "libslic3r/Layer.hpp" #include "libslic3r/Utils.hpp" #include "libslic3r/Technologies.hpp" #include "libslic3r/Tesselate.hpp" From 94b0ab603f491d02bf0a9f855d88c007753d9c14 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Tue, 26 May 2020 11:48:09 +0200 Subject: [PATCH 05/12] Include cleanup: Selection.hpp It does not need 3DScene.hpp and Model.hpp And it does not to be included by GLGizmoBase.hpp --- src/slic3r/GUI/GLCanvas3D.hpp | 3 + src/slic3r/GUI/Gizmos/GLGizmoBase.cpp | 5 -- src/slic3r/GUI/Gizmos/GLGizmoBase.hpp | 3 +- src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.hpp | 3 + src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp | 2 + src/slic3r/GUI/Gizmos/GLGizmoFlatten.hpp | 5 ++ src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp | 2 + src/slic3r/GUI/Gizmos/GLGizmoHollow.hpp | 5 ++ src/slic3r/GUI/Gizmos/GLGizmoScale.hpp | 2 + src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp | 6 +- src/slic3r/GUI/Gizmos/GLGizmosManager.cpp | 2 + src/slic3r/GUI/Gizmos/GLGizmosManager.hpp | 2 + src/slic3r/GUI/Jobs/ArrangeJob.cpp | 1 + src/slic3r/GUI/Jobs/RotoptimizeJob.cpp | 1 + src/slic3r/GUI/Jobs/SLAImportJob.cpp | 2 + src/slic3r/GUI/Plater.hpp | 3 + src/slic3r/GUI/Selection.cpp | 63 +++++++++++++++----- src/slic3r/GUI/Selection.hpp | 40 +++++++++---- 18 files changed, 117 insertions(+), 33 deletions(-) diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 499c9f85b..e43a761b5 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -42,6 +42,9 @@ struct ThumbnailData; class ModelObject; class ModelInstance; class PrintObject; +class Print; +class SLAPrint; +namespace CustomGCode { struct Item; } namespace GUI { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoBase.cpp b/src/slic3r/GUI/Gizmos/GLGizmoBase.cpp index 46abe8a95..9bc34f990 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoBase.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoBase.cpp @@ -4,14 +4,9 @@ #include #include "slic3r/GUI/GUI_App.hpp" -#include "slic3r/GUI/GLCanvas3D.hpp" - - // TODO: Display tooltips quicker on Linux - - namespace Slic3r { namespace GUI { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoBase.hpp b/src/slic3r/GUI/Gizmos/GLGizmoBase.hpp index 8a2b71976..3ab58c258 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoBase.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoBase.hpp @@ -4,7 +4,6 @@ #include "libslic3r/Point.hpp" #include "slic3r/GUI/I18N.hpp" -#include "slic3r/GUI/Selection.hpp" #include @@ -31,9 +30,9 @@ static const float CONSTRAINED_COLOR[4] = { 0.5f, 0.5f, 0.5f, 1.0f }; class ImGuiWrapper; class GLCanvas3D; -class ClippingPlane; enum class CommonGizmosDataID; class CommonGizmosDataPool; +class Selection; class GLGizmoBase { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.hpp b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.hpp index f481afc37..d765a8da5 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.hpp @@ -5,6 +5,8 @@ #include "slic3r/GUI/3DScene.hpp" +#include "libslic3r/ObjectID.hpp" + #include @@ -15,6 +17,7 @@ enum class FacetSupportType : int8_t; namespace GUI { enum class SLAGizmoEventType : unsigned char; +class ClippingPlane; class GLGizmoFdmSupports : public GLGizmoBase { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp index ee5c64818..262b685a6 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp @@ -3,6 +3,8 @@ #include "slic3r/GUI/GLCanvas3D.hpp" #include "slic3r/GUI/Gizmos/GLGizmosCommon.hpp" +#include "libslic3r/Model.hpp" + #include #include diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.hpp b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.hpp index 92d36d9d6..f6fe06451 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.hpp @@ -2,9 +2,14 @@ #define slic3r_GLGizmoFlatten_hpp_ #include "GLGizmoBase.hpp" +#include "slic3r/GUI/3DScene.hpp" namespace Slic3r { + +enum class ModelVolumeType : int; + + namespace GUI { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp b/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp index 8d4616da3..1dffee6be 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoHollow.cpp @@ -11,6 +11,8 @@ #include "slic3r/GUI/Plater.hpp" #include "slic3r/GUI/PresetBundle.hpp" +#include "libslic3r/Model.hpp" + namespace Slic3r { namespace GUI { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoHollow.hpp b/src/slic3r/GUI/Gizmos/GLGizmoHollow.hpp index 93e577743..5d34f04d6 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoHollow.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoHollow.hpp @@ -5,12 +5,17 @@ #include "slic3r/GUI/GLSelectionRectangle.hpp" #include +#include #include #include namespace Slic3r { + +class ConfigOption; +class ConfigOptionDef; + namespace GUI { enum class SLAGizmoEventType : unsigned char; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoScale.hpp b/src/slic3r/GUI/Gizmos/GLGizmoScale.hpp index 71f793279..0d8f3f7fa 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoScale.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoScale.hpp @@ -3,6 +3,8 @@ #include "GLGizmoBase.hpp" +#include "libslic3r/BoundingBox.hpp" + namespace Slic3r { namespace GUI { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp index b6cad8f9a..f9cf2f935 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp @@ -4,13 +4,17 @@ #include "GLGizmoBase.hpp" #include "slic3r/GUI/GLSelectionRectangle.hpp" -#include "libslic3r/SLA/Common.hpp" +#include "libslic3r/SLA/SupportPoint.hpp" +#include "libslic3r/ObjectID.hpp" #include #include namespace Slic3r { + +class ConfigOption; + namespace GUI { enum class SLAGizmoEventType : unsigned char; diff --git a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp index 8f8bebc8e..324c50011 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp @@ -17,6 +17,8 @@ #include "slic3r/GUI/Gizmos/GLGizmoCut.hpp" #include "slic3r/GUI/Gizmos/GLGizmoHollow.hpp" +#include "libslic3r/Model.hpp" + #include namespace Slic3r { diff --git a/src/slic3r/GUI/Gizmos/GLGizmosManager.hpp b/src/slic3r/GUI/Gizmos/GLGizmosManager.hpp index a7fb82126..e643c0b3b 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosManager.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosManager.hpp @@ -6,6 +6,8 @@ #include "slic3r/GUI/Gizmos/GLGizmoBase.hpp" #include "slic3r/GUI/Gizmos/GLGizmosCommon.hpp" +#include "libslic3r/ObjectID.hpp" + #include namespace Slic3r { diff --git a/src/slic3r/GUI/Jobs/ArrangeJob.cpp b/src/slic3r/GUI/Jobs/ArrangeJob.cpp index 00b9fb654..41fd717da 100644 --- a/src/slic3r/GUI/Jobs/ArrangeJob.cpp +++ b/src/slic3r/GUI/Jobs/ArrangeJob.cpp @@ -1,6 +1,7 @@ #include "ArrangeJob.hpp" #include "libslic3r/MTUtils.hpp" +#include "libslic3r/Model.hpp" #include "slic3r/GUI/Plater.hpp" #include "slic3r/GUI/GLCanvas3D.hpp" diff --git a/src/slic3r/GUI/Jobs/RotoptimizeJob.cpp b/src/slic3r/GUI/Jobs/RotoptimizeJob.cpp index 4cc34e71c..c847c84b4 100644 --- a/src/slic3r/GUI/Jobs/RotoptimizeJob.cpp +++ b/src/slic3r/GUI/Jobs/RotoptimizeJob.cpp @@ -3,6 +3,7 @@ #include "libslic3r/MTUtils.hpp" #include "libslic3r/SLA/Rotfinder.hpp" #include "libslic3r/MinAreaBoundingBox.hpp" +#include "libslic3r/Model.hpp" #include "slic3r/GUI/Plater.hpp" diff --git a/src/slic3r/GUI/Jobs/SLAImportJob.cpp b/src/slic3r/GUI/Jobs/SLAImportJob.cpp index e791bf94e..4e9f08ff2 100644 --- a/src/slic3r/GUI/Jobs/SLAImportJob.cpp +++ b/src/slic3r/GUI/Jobs/SLAImportJob.cpp @@ -8,6 +8,8 @@ #include "slic3r/GUI/GUI_ObjectList.hpp" #include "slic3r/Utils/SLAImport.hpp" +#include "libslic3r/Model.hpp" + #include #include #include diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index dcbda00f2..970005055 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -25,10 +25,13 @@ namespace Slic3r { class Model; class ModelObject; +class ModelInstance; class Print; class SLAPrint; enum SLAPrintObjectStep : unsigned int; +using ModelInstancePtrs = std::vector; + namespace UndoRedo { class Stack; struct Snapshot; diff --git a/src/slic3r/GUI/Selection.cpp b/src/slic3r/GUI/Selection.cpp index 87f8b60ed..106583400 100644 --- a/src/slic3r/GUI/Selection.cpp +++ b/src/slic3r/GUI/Selection.cpp @@ -1,15 +1,17 @@ #include "libslic3r/libslic3r.h" #include "Selection.hpp" +#include "3DScene.hpp" #include "GLCanvas3D.hpp" #include "GUI_App.hpp" #include "GUI.hpp" #include "GUI_ObjectManipulation.hpp" #include "GUI_ObjectList.hpp" #include "Gizmos/GLGizmoBase.hpp" -#include "3DScene.hpp" #include "Camera.hpp" +#include "libslic3r/Model.hpp" + #include #include @@ -54,7 +56,7 @@ bool Selection::Clipboard::is_sla_compliant() const if (m_mode == Selection::Volume) return false; - for (const ModelObject* o : m_model.objects) + for (const ModelObject* o : m_model->objects) { if (o->is_multiparts()) return false; @@ -69,6 +71,35 @@ bool Selection::Clipboard::is_sla_compliant() const return true; } +Selection::Clipboard::Clipboard() +{ + m_model.reset(new Model); +} + +void Selection::Clipboard::reset() { + m_model->clear_objects(); +} + +bool Selection::Clipboard::is_empty() const +{ + return m_model->objects.empty(); +} + +ModelObject* Selection::Clipboard::add_object() +{ + return m_model->add_object(); +} + +ModelObject* Selection::Clipboard::get_object(unsigned int id) +{ + return (id < (unsigned int)m_model->objects.size()) ? m_model->objects[id] : nullptr; +} + +const ModelObjectPtrs& Selection::Clipboard::get_objects() const +{ + return m_model->objects; +} + Selection::Selection() : m_volumes(nullptr) , m_model(nullptr) @@ -76,9 +107,11 @@ Selection::Selection() , m_mode(Instance) , m_type(Empty) , m_valid(false) - , m_curved_arrow(16) , m_scale_factor(1.0f) { + m_arrow.reset(new GLArrow); + m_curved_arrow.reset(new GLCurvedArrow(16)); + this->set_bounding_boxes_dirty(); #if ENABLE_RENDER_SELECTION_CENTER m_quadric = ::gluNewQuadric(); @@ -104,15 +137,15 @@ void Selection::set_volumes(GLVolumePtrs* volumes) // Init shall be called from the OpenGL render function, so that the OpenGL context is initialized! bool Selection::init() { - if (!m_arrow.init()) + if (!m_arrow->init()) return false; - m_arrow.set_scale(5.0 * Vec3d::Ones()); + m_arrow->set_scale(5.0 * Vec3d::Ones()); - if (!m_curved_arrow.init()) + if (!m_curved_arrow->init()) return false; - m_curved_arrow.set_scale(5.0 * Vec3d::Ones()); + m_curved_arrow->set_scale(5.0 * Vec3d::Ones()); return true; } @@ -2048,29 +2081,29 @@ void Selection::render_sidebar_layers_hints(const std::string& sidebar_field) co void Selection::render_sidebar_position_hint(Axis axis) const { - m_arrow.set_color(AXES_COLOR[axis], 3); - m_arrow.render(); + m_arrow->set_color(AXES_COLOR[axis], 3); + m_arrow->render(); } void Selection::render_sidebar_rotation_hint(Axis axis) const { - m_curved_arrow.set_color(AXES_COLOR[axis], 3); - m_curved_arrow.render(); + m_curved_arrow->set_color(AXES_COLOR[axis], 3); + m_curved_arrow->render(); glsafe(::glRotated(180.0, 0.0, 0.0, 1.0)); - m_curved_arrow.render(); + m_curved_arrow->render(); } void Selection::render_sidebar_scale_hint(Axis axis) const { - m_arrow.set_color(((requires_uniform_scale() || wxGetApp().obj_manipul()->get_uniform_scaling()) ? UNIFORM_SCALE_COLOR : AXES_COLOR[axis]), 3); + m_arrow->set_color(((requires_uniform_scale() || wxGetApp().obj_manipul()->get_uniform_scaling()) ? UNIFORM_SCALE_COLOR : AXES_COLOR[axis]), 3); glsafe(::glTranslated(0.0, 5.0, 0.0)); - m_arrow.render(); + m_arrow->render(); glsafe(::glTranslated(0.0, -10.0, 0.0)); glsafe(::glRotated(180.0, 0.0, 0.0, 1.0)); - m_arrow.render(); + m_arrow->render(); } void Selection::render_sidebar_size_hint(Axis axis, double length) const diff --git a/src/slic3r/GUI/Selection.hpp b/src/slic3r/GUI/Selection.hpp index fb93bef64..8654e2219 100644 --- a/src/slic3r/GUI/Selection.hpp +++ b/src/slic3r/GUI/Selection.hpp @@ -3,8 +3,8 @@ #include #include "libslic3r/Geometry.hpp" -#include "libslic3r/Model.hpp" -#include "3DScene.hpp" +//#include "libslic3r/Model.hpp" +//#include "3DScene.hpp" #if ENABLE_RENDER_SELECTION_CENTER @@ -13,7 +13,19 @@ typedef class GLUquadric GLUquadricObj; #endif // ENABLE_RENDER_SELECTION_CENTER namespace Slic3r { + class Shader; +class Model; +class ModelObject; +class GLVolume; +class GLArrow; +class GLCurvedArrow; +class DynamicPrintConfig; + +using GLVolumePtrs = std::vector; +using ModelObjectPtrs = std::vector; + + namespace GUI { class TransformationType { @@ -147,18 +159,23 @@ public: class Clipboard { - Model m_model; + // Model is stored through a pointer to avoid including heavy Model.hpp. + // It is created in constructor. + std::unique_ptr m_model; + Selection::EMode m_mode; public: - void reset() { m_model.clear_objects(); } - bool is_empty() const { return m_model.objects.empty(); } + Clipboard(); + + void reset(); + bool is_empty() const; bool is_sla_compliant() const; - ModelObject* add_object() { return m_model.add_object(); } - ModelObject* get_object(unsigned int id) { return (id < (unsigned int)m_model.objects.size()) ? m_model.objects[id] : nullptr; } - const ModelObjectPtrs& get_objects() const { return m_model.objects; } + ModelObject* add_object(); + ModelObject* get_object(unsigned int id); + const ModelObjectPtrs& get_objects() const; Selection::EMode get_mode() const { return m_mode; } void set_mode(Selection::EMode mode) { m_mode = mode; } @@ -202,8 +219,11 @@ private: #if ENABLE_RENDER_SELECTION_CENTER GLUquadricObj* m_quadric; #endif // ENABLE_RENDER_SELECTION_CENTER - mutable GLArrow m_arrow; - mutable GLCurvedArrow m_curved_arrow; + + // Arrows are saved through pointers to avoid including 3DScene.hpp. + // It also allows mutability. + std::unique_ptr m_arrow; + std::unique_ptr m_curved_arrow; mutable float m_scale_factor; From 3e855d36dc0784fd3fedff77fc5aba6bbdcaf38d Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Tue, 26 May 2020 14:34:07 +0200 Subject: [PATCH 06/12] Fixed unit tests after previous include manipulations --- src/slic3r/GUI/3DScene.hpp | 1 - src/slic3r/GUI/Selection.hpp | 2 -- tests/fff_print/test_print.cpp | 1 + tests/fff_print/test_printobject.cpp | 1 + tests/fff_print/test_support_material.cpp | 1 + xs/xsp/PerimeterGenerator.xsp | 1 + 6 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index b6decf6c8..1401e79fb 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -6,7 +6,6 @@ #include "libslic3r/Line.hpp" #include "libslic3r/TriangleMesh.hpp" #include "libslic3r/Utils.hpp" -//#include "libslic3r/Model.hpp" #include "libslic3r/Geometry.hpp" #include diff --git a/src/slic3r/GUI/Selection.hpp b/src/slic3r/GUI/Selection.hpp index 8654e2219..7a929926c 100644 --- a/src/slic3r/GUI/Selection.hpp +++ b/src/slic3r/GUI/Selection.hpp @@ -3,8 +3,6 @@ #include #include "libslic3r/Geometry.hpp" -//#include "libslic3r/Model.hpp" -//#include "3DScene.hpp" #if ENABLE_RENDER_SELECTION_CENTER diff --git a/tests/fff_print/test_print.cpp b/tests/fff_print/test_print.cpp index 484e6018e..3250443ed 100644 --- a/tests/fff_print/test_print.cpp +++ b/tests/fff_print/test_print.cpp @@ -2,6 +2,7 @@ #include "libslic3r/libslic3r.h" #include "libslic3r/Print.hpp" +#include "libslic3r/Layer.hpp" #include "test_data.hpp" diff --git a/tests/fff_print/test_printobject.cpp b/tests/fff_print/test_printobject.cpp index e7c441e8c..84df95201 100644 --- a/tests/fff_print/test_printobject.cpp +++ b/tests/fff_print/test_printobject.cpp @@ -2,6 +2,7 @@ #include "libslic3r/libslic3r.h" #include "libslic3r/Print.hpp" +#include "libslic3r/Layer.hpp" #include "test_data.hpp" diff --git a/tests/fff_print/test_support_material.cpp b/tests/fff_print/test_support_material.cpp index 0a38d138e..663b715a9 100644 --- a/tests/fff_print/test_support_material.cpp +++ b/tests/fff_print/test_support_material.cpp @@ -1,6 +1,7 @@ #include #include "libslic3r/GCodeReader.hpp" +#include "libslic3r/Layer.hpp" #include "test_data.hpp" // get access to init_print, etc diff --git a/xs/xsp/PerimeterGenerator.xsp b/xs/xsp/PerimeterGenerator.xsp index 07059de61..f2c0d025c 100644 --- a/xs/xsp/PerimeterGenerator.xsp +++ b/xs/xsp/PerimeterGenerator.xsp @@ -3,6 +3,7 @@ %{ #include #include "libslic3r/PerimeterGenerator.hpp" +#include "libslic3r/Layer.hpp" %} %name{Slic3r::Layer::PerimeterGenerator} class PerimeterGenerator { From 32a353058f038fd039dec9df7f339648aa92c34a Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Wed, 22 Apr 2020 12:49:52 +0200 Subject: [PATCH 07/12] Fixed few warnings --- src/libslic3r/Fill/FillRectilinear2.cpp | 34 ++++++++++++------------- src/libslic3r/GCodeTimeEstimator.cpp | 2 +- src/slic3r/GUI/AppConfig.cpp | 10 ++++---- src/slic3r/GUI/Camera.cpp | 3 --- src/slic3r/GUI/DoubleSlider.hpp | 2 +- src/slic3r/GUI/Gizmos/GLGizmoScale.cpp | 1 - src/slic3r/GUI/KBShortcutsDialog.cpp | 9 ------- src/slic3r/GUI/Preset.cpp | 2 +- src/slic3r/GUI/PresetHints.cpp | 3 +-- src/slic3r/GUI/Tab.cpp | 34 ++++++++++++------------- src/slic3r/GUI/fts_fuzzy_match.h | 2 +- 11 files changed, 44 insertions(+), 58 deletions(-) diff --git a/src/libslic3r/Fill/FillRectilinear2.cpp b/src/libslic3r/Fill/FillRectilinear2.cpp index d83249939..5b862e8cb 100644 --- a/src/libslic3r/Fill/FillRectilinear2.cpp +++ b/src/libslic3r/Fill/FillRectilinear2.cpp @@ -901,7 +901,7 @@ static void connect_segment_intersections_by_contours( const SegmentedIntersectionLine *il_prev = i_vline > 0 ? &segs[i_vline - 1] : nullptr; const SegmentedIntersectionLine *il_next = i_vline + 1 < segs.size() ? &segs[i_vline + 1] : nullptr; - for (int i_intersection = 0; i_intersection < il.intersections.size(); ++ i_intersection) { + for (int i_intersection = 0; i_intersection < int(il.intersections.size()); ++ i_intersection) { SegmentIntersection &itsct = il.intersections[i_intersection]; const Polygon &poly = poly_with_offset.contour(itsct.iContour); const bool forward = itsct.is_low(); // == poly_with_offset.is_contour_ccw(intrsctn->iContour); @@ -914,7 +914,7 @@ static void connect_segment_intersections_by_contours( int iprev = -1; int d_prev = std::numeric_limits::max(); if (il_prev) { - for (int i = 0; i < il_prev->intersections.size(); ++ i) { + for (int i = 0; i < int(il_prev->intersections.size()); ++ i) { const SegmentIntersection &itsct2 = il_prev->intersections[i]; if (itsct.iContour == itsct2.iContour && itsct.type == itsct2.type) { // The intersection points lie on the same contour and have the same orientation. @@ -932,7 +932,7 @@ static void connect_segment_intersections_by_contours( int inext = -1; int d_next = std::numeric_limits::max(); if (il_next) { - for (int i = 0; i < il_next->intersections.size(); ++ i) { + for (int i = 0; i < int(il_next->intersections.size()); ++ i) { const SegmentIntersection &itsct2 = il_next->intersections[i]; if (itsct.iContour == itsct2.iContour && itsct.type == itsct2.type) { // The intersection points lie on the same contour and have the same orientation. @@ -950,7 +950,7 @@ static void connect_segment_intersections_by_contours( bool same_prev = false; bool same_next = false; // Does the perimeter intersect the current vertical line above intrsctn? - for (int i = 0; i < il.intersections.size(); ++ i) + for (int i = 0; i < int(il.intersections.size()); ++ i) if (const SegmentIntersection &it2 = il.intersections[i]; i != i_intersection && it2.iContour == itsct.iContour && it2.type != itsct.type) { int d = distance_of_segmens(poly, it2.iSegment, itsct.iSegment, forward); @@ -1040,7 +1040,7 @@ static void connect_segment_intersections_by_contours( } // Make the LinkQuality::Invalid symmetric on vertical connections. - for (int i_intersection = 0; i_intersection < il.intersections.size(); ++ i_intersection) { + for (int i_intersection = 0; i_intersection < int(il.intersections.size()); ++ i_intersection) { SegmentIntersection &it = il.intersections[i_intersection]; if (it.has_left_vertical() && it.prev_on_contour_quality == SegmentIntersection::LinkQuality::Invalid) { SegmentIntersection &it2 = il.intersections[it.left_vertical()]; @@ -1157,9 +1157,9 @@ static void traverse_graph_generate_polylines( { // For each outer only chords, measure their maximum distance to the bow of the outer contour. // Mark an outer only chord as consumed, if the distance is low. - for (int i_vline = 0; i_vline < segs.size(); ++ i_vline) { + for (int i_vline = 0; i_vline < int(segs.size()); ++ i_vline) { SegmentedIntersectionLine &vline = segs[i_vline]; - for (int i_intersection = 0; i_intersection + 1 < vline.intersections.size(); ++ i_intersection) { + for (int i_intersection = 0; i_intersection + 1 < int(vline.intersections.size()); ++ i_intersection) { if (vline.intersections[i_intersection].type == SegmentIntersection::OUTER_LOW && vline.intersections[i_intersection + 1].type == SegmentIntersection::OUTER_HIGH) { bool consumed = false; @@ -1189,14 +1189,14 @@ static void traverse_graph_generate_polylines( if (i_intersection == -1) { // The path has been interrupted. Find a next starting point, closest to the previous extruder position. coordf_t dist2min = std::numeric_limits().max(); - for (int i_vline2 = 0; i_vline2 < segs.size(); ++ i_vline2) { + for (int i_vline2 = 0; i_vline2 < int(segs.size()); ++ i_vline2) { const SegmentedIntersectionLine &vline = segs[i_vline2]; if (! vline.intersections.empty()) { assert(vline.intersections.size() > 1); // Even number of intersections with the loops. assert((vline.intersections.size() & 1) == 0); assert(vline.intersections.front().type == SegmentIntersection::OUTER_LOW); - for (int i = 0; i < vline.intersections.size(); ++ i) { + for (int i = 0; i < int(vline.intersections.size()); ++ i) { const SegmentIntersection& intrsctn = vline.intersections[i]; if (intrsctn.is_outer()) { assert(intrsctn.is_low() || i > 0); @@ -1674,13 +1674,13 @@ static std::vector generate_montonous_regions(std::vector generate_montonous_regions(std::vectorconsumed_vertical_up = true; int num_lines = 1; - while (++ i_vline < segs.size()) { + while (++ i_vline < int(segs.size())) { SegmentedIntersectionLine &vline_left = segs[i_vline - 1]; SegmentedIntersectionLine &vline_right = segs[i_vline]; std::pair right = right_overlap(left, vline_left, vline_right); @@ -1860,7 +1860,7 @@ static void connect_monotonous_regions(std::vector ®ions, c } } } - if (region.right.vline + 1 < segs.size()) { + if (region.right.vline + 1 < int(segs.size())) { auto &vline = segs[region.right.vline]; auto &vline_right = segs[region.right.vline + 1]; auto [rbegin, rend] = right_overlap(vline.intersections[region.right.low], vline.intersections[region.right.high], vline, vline_right); @@ -2100,7 +2100,7 @@ static std::vector chain_monotonous_regions( AntPath &path2 = path_matrix(region, dir, *next, true); if (path1.visibility > next_candidate.probability) next_candidate = { next, &path1, &path1, path1.visibility, false }; - if (path2.visibility > next_candidate.probability) + if (path2.visibility > next_candidate.probability) next_candidate = { next, &path2, &path2, path2.visibility, true }; } } @@ -2111,7 +2111,7 @@ static std::vector chain_monotonous_regions( AntPath &path2 = path_matrix(region, dir, *next, true); if (path1.visibility > next_candidate.probability) next_candidate = { next, &path1, &path1, path1.visibility, false }; - if (path2.visibility > next_candidate.probability) + if (path2.visibility > next_candidate.probability) next_candidate = { next, &path2, &path2, path2.visibility, true }; } } diff --git a/src/libslic3r/GCodeTimeEstimator.cpp b/src/libslic3r/GCodeTimeEstimator.cpp index 6d7f5f659..f6f602d6c 100644 --- a/src/libslic3r/GCodeTimeEstimator.cpp +++ b/src/libslic3r/GCodeTimeEstimator.cpp @@ -282,7 +282,7 @@ namespace Slic3r { }; GCodeReader parser; - unsigned int g1_lines_count = 0; + int g1_lines_count = 0; int normal_g1_line_id = 0; float normal_last_recorded_time = 0.0f; int silent_g1_line_id = 0; diff --git a/src/slic3r/GUI/AppConfig.cpp b/src/slic3r/GUI/AppConfig.cpp index 0f603c265..ba13a25d8 100644 --- a/src/slic3r/GUI/AppConfig.cpp +++ b/src/slic3r/GUI/AppConfig.cpp @@ -300,13 +300,13 @@ void AppConfig::set_mouse_device(const std::string& name, double translation_spe std::vector AppConfig::get_mouse_device_names() const { - static constexpr char *prefix = "mouse_device:"; - static constexpr size_t prefix_len = 13; // strlen(prefix); reports error C2131: expression did not evaluate to a constant on VS2019 - std::vector out; + static constexpr const char *prefix = "mouse_device:"; + static const size_t prefix_len = strlen(prefix); + std::vector out; for (const std::pair>& key_value_pair : m_storage) - if (boost::starts_with(key_value_pair.first, "mouse_device:") && key_value_pair.first.size() > prefix_len) + if (boost::starts_with(key_value_pair.first, prefix) && key_value_pair.first.size() > prefix_len) out.emplace_back(key_value_pair.first.substr(prefix_len)); - return out; + return out; } void AppConfig::update_config_dir(const std::string &dir) diff --git a/src/slic3r/GUI/Camera.cpp b/src/slic3r/GUI/Camera.cpp index 7ae4a7a1b..ac32767c4 100644 --- a/src/slic3r/GUI/Camera.cpp +++ b/src/slic3r/GUI/Camera.cpp @@ -425,9 +425,6 @@ double Camera::calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, double if ((dx <= 0.0) || (dy <= 0.0)) return -1.0f; - double med_x = 0.5 * (max_x + min_x); - double med_y = 0.5 * (max_y + min_y); - dx *= margin_factor; dy *= margin_factor; diff --git a/src/slic3r/GUI/DoubleSlider.hpp b/src/slic3r/GUI/DoubleSlider.hpp index bf8f54d6c..6fc5a61c1 100644 --- a/src/slic3r/GUI/DoubleSlider.hpp +++ b/src/slic3r/GUI/DoubleSlider.hpp @@ -22,7 +22,7 @@ namespace DoubleSlider { /* For exporting GCode in GCodeWriter is used XYZF_NUM(val) = PRECISION(val, 3) for XYZ values. * So, let use same value as a permissible error for layer height. */ -static double epsilon() { return 0.0011;} +constexpr double epsilon() { return 0.0011; } // custom message the slider sends to its parent to notify a tick-change: wxDECLARE_EVENT(wxCUSTOMEVT_TICKSCHANGED, wxEvent); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoScale.cpp b/src/slic3r/GUI/Gizmos/GLGizmoScale.cpp index 80cc16ba2..fa2069d0a 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoScale.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoScale.cpp @@ -26,7 +26,6 @@ std::string GLGizmoScale3D::get_tooltip() const bool single_instance = selection.is_single_full_instance(); bool single_volume = selection.is_single_modifier() || selection.is_single_volume(); - bool single_selection = single_instance || single_volume; Vec3f scale = 100.0f * Vec3f::Ones(); if (single_instance) diff --git a/src/slic3r/GUI/KBShortcutsDialog.cpp b/src/slic3r/GUI/KBShortcutsDialog.cpp index 9f31d23d8..556b610e9 100644 --- a/src/slic3r/GUI/KBShortcutsDialog.cpp +++ b/src/slic3r/GUI/KBShortcutsDialog.cpp @@ -25,15 +25,6 @@ #include #endif // BOOK_TYPE -#if ENABLE_SCROLLABLE -static wxSize get_screen_size(wxWindow* window) -{ - const auto idx = wxDisplay::GetFromWindow(window); - wxDisplay display(idx != wxNOT_FOUND ? idx : 0u); - return display.GetClientArea().GetSize(); -} -#endif // ENABLE_SCROLLABLE - namespace Slic3r { namespace GUI { diff --git a/src/slic3r/GUI/Preset.cpp b/src/slic3r/GUI/Preset.cpp index a1f83141a..16a15f1e8 100644 --- a/src/slic3r/GUI/Preset.cpp +++ b/src/slic3r/GUI/Preset.cpp @@ -962,7 +962,7 @@ void PresetCollection::load_bitmap_add(const std::string &file_name) const Preset* PresetCollection::get_selected_preset_parent() const { - if (this->get_selected_idx() == -1) + if (this->get_selected_idx() == size_t(-1)) // This preset collection has no preset activated yet. Only the get_edited_preset() is valid. return nullptr; diff --git a/src/slic3r/GUI/PresetHints.cpp b/src/slic3r/GUI/PresetHints.cpp index 22fa09f6c..24afeb526 100644 --- a/src/slic3r/GUI/PresetHints.cpp +++ b/src/slic3r/GUI/PresetHints.cpp @@ -296,9 +296,8 @@ std::string PresetHints::top_bottom_shell_thickness_explanation(const PresetBund double bottom_solid_min_thickness = print_config.opt_float("bottom_solid_min_thickness"); double layer_height = print_config.opt_float("layer_height"); bool variable_layer_height = printer_config.opt_bool("variable_layer_height"); - //FIXME the following lines take into account the 1st extruder only. + //FIXME the following line takes into account the 1st extruder only. double min_layer_height = variable_layer_height ? Slicing::min_layer_height_from_nozzle(printer_config, 1) : layer_height; - double max_layer_height = variable_layer_height ? Slicing::max_layer_height_from_nozzle(printer_config, 1) : layer_height; if (layer_height <= 0.f) { out += _utf8(L("Top / bottom shell thickness hint: Not available due to invalid layer height.")); diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index da48224b3..3b78b2c9a 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -3297,28 +3297,28 @@ void Tab::save_preset(std::string name /*= ""*/, bool detach) wxGetApp().plater()->force_filament_colors_update(); { - // Profile compatiblity is updated first when the profile is saved. - // Update profile selection combo boxes at the depending tabs to reflect modifications in profile compatibility. - std::vector dependent; - switch (m_type) { - case Preset::TYPE_PRINT: - dependent = { Preset::TYPE_FILAMENT }; - break; - case Preset::TYPE_SLA_PRINT: - dependent = { Preset::TYPE_SLA_MATERIAL }; - break; - case Preset::TYPE_PRINTER: + // Profile compatiblity is updated first when the profile is saved. + // Update profile selection combo boxes at the depending tabs to reflect modifications in profile compatibility. + std::vector dependent; + switch (m_type) { + case Preset::TYPE_PRINT: + dependent = { Preset::TYPE_FILAMENT }; + break; + case Preset::TYPE_SLA_PRINT: + dependent = { Preset::TYPE_SLA_MATERIAL }; + break; + case Preset::TYPE_PRINTER: if (static_cast(this)->m_printer_technology == ptFFF) dependent = { Preset::TYPE_PRINT, Preset::TYPE_FILAMENT }; else dependent = { Preset::TYPE_SLA_PRINT, Preset::TYPE_SLA_MATERIAL }; - break; + break; default: - break; - } - for (Preset::Type preset_type : dependent) - wxGetApp().get_tab(preset_type)->update_tab_ui(); - } + break; + } + for (Preset::Type preset_type : dependent) + wxGetApp().get_tab(preset_type)->update_tab_ui(); + } } // Called for a currently selected preset. diff --git a/src/slic3r/GUI/fts_fuzzy_match.h b/src/slic3r/GUI/fts_fuzzy_match.h index e692232cc..34b2bbc5a 100644 --- a/src/slic3r/GUI/fts_fuzzy_match.h +++ b/src/slic3r/GUI/fts_fuzzy_match.h @@ -120,7 +120,7 @@ namespace fts { char *end = Slic3r::fold_to_ascii(*str, tmp); char *c = tmp; for (const wchar_t* d = pattern; c != end && *d != 0 && wchar_t(std::tolower(*c)) == std::tolower(*d); ++c, ++d); - if (c == end) { + if (c == end) { folded_match = true; num_matched = end - tmp; } From cdf80c3b3f9447437257b9fa2acd72b3d93dabb2 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 22 May 2020 13:27:00 +0200 Subject: [PATCH 08/12] Switched to new AABB tree implementation for raycasting --- src/libslic3r/SLA/Common.cpp | 86 +++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index b57039ad1..806dc4d66 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -7,6 +7,7 @@ #include #include #include +#include // Workaround: IGL signed_distance.h will define PI in the igl namespace. @@ -23,11 +24,23 @@ #pragma warning(disable: 4244) #pragma warning(disable: 4267) #endif -#include -#include + +#define USE_AABB_INDIRECT // Vojta's AABB (defined) vs igl::AABB (undefined) + +#ifdef SLIC3R_SLA_NEEDS_WINDTREE + #ifdef USE_AABB_INDIRECT + #error These two options contradict each other. + #endif + #include +#endif + +#ifndef USE_AABB_INDIRECT + #include +#endif + #include -#include -#include + + #ifdef _MSC_VER #pragma warning(pop) #endif @@ -40,7 +53,7 @@ namespace Slic3r { namespace sla { // Bring back PI from the igl namespace -using igl::PI; +//using igl::PI; /* ************************************************************************** * PointIndex implementation @@ -188,8 +201,59 @@ void BoxIndex::foreach(std::function fn) * EigenMesh3D implementation * ****************************************************************************/ +#ifdef USE_AABB_INDIRECT +class EigenMesh3D::AABBImpl { +#else class EigenMesh3D::AABBImpl: public igl::AABB { +#endif + public: +#ifdef USE_AABB_INDIRECT +private: + AABBTreeIndirect::Tree3f m_tree; + TriangleMesh m_triangle_mesh; // FIXME: There should be no extra copy + // maybe even the m_V and m_F are extra. This is just to see the new + // AABB tree in action +public: + void init(const TriangleMesh& tmesh) + { + m_triangle_mesh = tmesh; + m_tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set( + m_triangle_mesh.its.vertices, m_triangle_mesh.its.indices); + } + + void intersect_ray(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + const Vec3d& s, const Vec3d& dir, igl::Hit& hit) + { + AABBTreeIndirect::intersect_ray_first_hit(m_triangle_mesh.its.vertices, + m_triangle_mesh.its.indices, + m_tree, + s, dir, hit); + } + + void intersect_ray(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + const Vec3d& s, const Vec3d& dir, std::vector& hits) + { + AABBTreeIndirect::intersect_ray_all_hits(m_triangle_mesh.its.vertices, + m_triangle_mesh.its.indices, + m_tree, + s, dir, hits); + } + + double squared_distance(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + const Vec3d& point, int& i, Eigen::Matrix& closest) { + size_t idx_unsigned = 0; + Vec3d closest_vec3d(closest); + double dist = AABBTreeIndirect::squared_distance_to_indexed_triangle_set( + m_triangle_mesh.its.vertices, + m_triangle_mesh.its.indices, + m_tree, point, idx_unsigned, closest_vec3d); + i = int(idx_unsigned); + closest = closest_vec3d; + return dist; + } +#endif + #ifdef SLIC3R_SLA_NEEDS_WINDTREE igl::WindingNumberAABB windtree; #endif /* SLIC3R_SLA_NEEDS_WINDTREE */ @@ -225,7 +289,11 @@ void to_eigen_mesh(const TriangleMesh &tmesh, Eigen::MatrixXd &V, Eigen::MatrixX } } -void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, TriangleMesh &out) +void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, TriangleMesh &out); +#if 0 +Does this function really work? There seems to be an issue with it. +Currently it is not used anywhere, this way it stays visible but +trigger linking error when attempting to use it. { Pointf3s points(size_t(V.rows())); std::vector facets(size_t(F.rows())); @@ -238,6 +306,7 @@ void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, Triang out = {points, facets}; } +#endif EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { auto&& bb = tmesh.bounding_box(); @@ -246,7 +315,12 @@ EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { to_eigen_mesh(tmesh, m_V, m_F); // Build the AABB accelaration tree +#ifdef USE_AABB_INDIRECT + m_aabb->init(tmesh); +#else m_aabb->init(m_V, m_F); +#endif + #ifdef SLIC3R_SLA_NEEDS_WINDTREE m_aabb->windtree.set_mesh(m_V, m_F); #endif /* SLIC3R_SLA_NEEDS_WINDTREE */ From 9224a6a3e68b9f749beedf713cf01a3042d3ea41 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 22 May 2020 17:21:54 +0200 Subject: [PATCH 09/12] Removed some unused code - removed define USE_AABB_INDIRECT (which switched between old and new AABB implementation) - removed define SLIC3R_SLA_NEEDS_WINDTREE (relied on igl and was not used anyway) - new define SLIC3R_HOLE_RAYCASTER (hides currently unused code) - slight include cleanup - removed obsolete source file SupportTreeIGL.cpp --- src/libslic3r/SLA/Common.cpp | 95 +-- src/libslic3r/SLA/Common.hpp | 6 - src/libslic3r/SLA/EigenMesh3D.hpp | 36 +- src/libslic3r/SLA/SupportPointGenerator.hpp | 1 + src/libslic3r/SLA/SupportTreeIGL.cpp | 621 -------------------- 5 files changed, 45 insertions(+), 714 deletions(-) delete mode 100644 src/libslic3r/SLA/SupportTreeIGL.cpp diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index 806dc4d66..56c9d12ff 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -1,20 +1,12 @@ #include #include #include -#include #include #include #include #include -#include #include - -// Workaround: IGL signed_distance.h will define PI in the igl namespace. -#undef PI - -// HEAVY headers... takes eternity to compile - // for concave hull merging decisions #include #include "boost/geometry/index/rtree.hpp" @@ -25,35 +17,22 @@ #pragma warning(disable: 4267) #endif -#define USE_AABB_INDIRECT // Vojta's AABB (defined) vs igl::AABB (undefined) - -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - #ifdef USE_AABB_INDIRECT - #error These two options contradict each other. - #endif - #include -#endif - -#ifndef USE_AABB_INDIRECT - #include -#endif #include +#ifdef SLIC3R_HOLE_RAYCASTER + #include +#endif + #ifdef _MSC_VER #pragma warning(pop) #endif -#include - -#include "ClipperUtils.hpp" namespace Slic3r { namespace sla { -// Bring back PI from the igl namespace -//using igl::PI; /* ************************************************************************** * PointIndex implementation @@ -201,14 +180,8 @@ void BoxIndex::foreach(std::function fn) * EigenMesh3D implementation * ****************************************************************************/ -#ifdef USE_AABB_INDIRECT -class EigenMesh3D::AABBImpl { -#else -class EigenMesh3D::AABBImpl: public igl::AABB { -#endif -public: -#ifdef USE_AABB_INDIRECT +class EigenMesh3D::AABBImpl { private: AABBTreeIndirect::Tree3f m_tree; TriangleMesh m_triangle_mesh; // FIXME: There should be no extra copy @@ -252,11 +225,6 @@ public: closest = closest_vec3d; return dist; } -#endif - -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - igl::WindingNumberAABB windtree; -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ }; static const constexpr double MESH_EPS = 1e-6; @@ -315,15 +283,7 @@ EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { to_eigen_mesh(tmesh, m_V, m_F); // Build the AABB accelaration tree -#ifdef USE_AABB_INDIRECT m_aabb->init(tmesh); -#else - m_aabb->init(m_V, m_F); -#endif - -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - m_aabb->windtree.set_mesh(m_V, m_F); -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ } EigenMesh3D::~EigenMesh3D() {} @@ -371,24 +331,25 @@ EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const igl::Hit hit; hit.t = std::numeric_limits::infinity(); - if (m_holes.empty()) { - m_aabb->intersect_ray(m_V, m_F, s, dir, hit); - hit_result ret(*this); - ret.m_t = double(hit.t); - ret.m_dir = dir; - ret.m_source = s; - if(!std::isinf(hit.t) && !std::isnan(hit.t)) { - ret.m_normal = this->normal_by_face_id(hit.id); - ret.m_face_id = hit.id; - } - - return ret; - } - else { +#ifdef SLIC3R_HOLE_RAYCASTER + if (! m_holes.empty()) { // If there are holes, the hit_results will be made by // query_ray_hits (object) and filter_hits (holes): return filter_hits(query_ray_hits(s, dir)); } +#endif + + m_aabb->intersect_ray(m_V, m_F, s, dir, hit); + hit_result ret(*this); + ret.m_t = double(hit.t); + ret.m_dir = dir; + ret.m_source = s; + if(!std::isinf(hit.t) && !std::isnan(hit.t)) { + ret.m_normal = this->normal_by_face_id(hit.id); + ret.m_face_id = hit.id; + } + + return ret; } std::vector @@ -425,6 +386,8 @@ EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const return outs; } + +#ifdef SLIC3R_HOLE_RAYCASTER EigenMesh3D::hit_result EigenMesh3D::filter_hits( const std::vector& object_hits) const { @@ -519,20 +482,8 @@ EigenMesh3D::hit_result EigenMesh3D::filter_hits( // if we got here, the ray ended up in infinity return out; } +#endif -#ifdef SLIC3R_SLA_NEEDS_WINDTREE -EigenMesh3D::si_result EigenMesh3D::signed_distance(const Vec3d &p) const { - double sign = 0; double sqdst = 0; int i = 0; Vec3d c; - igl::signed_distance_winding_number(*m_aabb, m_V, m_F, m_aabb->windtree, - p, sign, sqdst, i, c); - - return si_result(sign * std::sqrt(sqdst), i, c); -} - -bool EigenMesh3D::inside(const Vec3d &p) const { - return m_aabb->windtree.inside(p); -} -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ double EigenMesh3D::squared_distance(const Vec3d &p, int& i, Vec3d& c) const { double sqdst = 0; diff --git a/src/libslic3r/SLA/Common.hpp b/src/libslic3r/SLA/Common.hpp index e1c5930e2..91bdc0eb1 100644 --- a/src/libslic3r/SLA/Common.hpp +++ b/src/libslic3r/SLA/Common.hpp @@ -7,12 +7,6 @@ #include #include -//#include "SLASpatIndex.hpp" - -//#include -//#include - -// #define SLIC3R_SLA_NEEDS_WINDTREE namespace Slic3r { diff --git a/src/libslic3r/SLA/EigenMesh3D.hpp b/src/libslic3r/SLA/EigenMesh3D.hpp index 014a57e82..248f3577f 100644 --- a/src/libslic3r/SLA/EigenMesh3D.hpp +++ b/src/libslic3r/SLA/EigenMesh3D.hpp @@ -2,7 +2,16 @@ #define SLA_EIGENMESH3D_H #include -#include "libslic3r/SLA/Hollowing.hpp" + + +// There is an implementation of a hole-aware raycaster that was eventually +// not used in production version. It is now hidden under following define +// for possible future use. +//#define SLIC3R_HOLE_RAYCASTER + +#ifdef SLIC3R_HOLE_RAYCASTER + #include "libslic3r/SLA/Hollowing.hpp" +#endif namespace Slic3r { @@ -27,9 +36,11 @@ class EigenMesh3D { std::unique_ptr m_aabb; +#ifdef SLIC3R_HOLE_RAYCASTER // This holds a copy of holes in the mesh. Initialized externally // by load_mesh setter. std::vector m_holes; +#endif public: @@ -88,25 +99,27 @@ public: return is_hit() && normal().dot(m_dir) > 0; } }; - + +#ifdef SLIC3R_HOLE_RAYCASTER // Inform the object about location of holes // creates internal copy of the vector void load_holes(const std::vector& holes) { m_holes = holes; } - // Casting a ray on the mesh, returns the distance where the hit occures. - hit_result query_ray_hit(const Vec3d &s, const Vec3d &dir) const; - - // Casts a ray on the mesh and returns all hits - std::vector query_ray_hits(const Vec3d &s, const Vec3d &dir) const; - // Iterates over hits and holes and returns the true hit, possibly // on the inside of a hole. // This function is currently not used anywhere, it was written when the // holes were subtracted on slices, that is, before we started using CGAL // to actually cut the holes into the mesh. hit_result filter_hits(const std::vector& obj_hits) const; +#endif + + // Casting a ray on the mesh, returns the distance where the hit occures. + hit_result query_ray_hit(const Vec3d &s, const Vec3d &dir) const; + + // Casts a ray on the mesh and returns all hits + std::vector query_ray_hits(const Vec3d &s, const Vec3d &dir) const; class si_result { double m_value; @@ -125,13 +138,6 @@ public: int F_idx() const { return m_fidx; } }; -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - // The signed distance from a point to the mesh. Outputs the distance, - // the index of the triangle and the closest point in mesh coordinate space. - si_result signed_distance(const Vec3d& p) const; - - bool inside(const Vec3d& p) const; -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ double squared_distance(const Vec3d& p, int& i, Vec3d& c) const; inline double squared_distance(const Vec3d &p) const diff --git a/src/libslic3r/SLA/SupportPointGenerator.hpp b/src/libslic3r/SLA/SupportPointGenerator.hpp index 1621cde50..2fe8e11fc 100644 --- a/src/libslic3r/SLA/SupportPointGenerator.hpp +++ b/src/libslic3r/SLA/SupportPointGenerator.hpp @@ -7,6 +7,7 @@ #include #include +#include #include #include diff --git a/src/libslic3r/SLA/SupportTreeIGL.cpp b/src/libslic3r/SLA/SupportTreeIGL.cpp deleted file mode 100644 index ea2be6be1..000000000 --- a/src/libslic3r/SLA/SupportTreeIGL.cpp +++ /dev/null @@ -1,621 +0,0 @@ -#include -#include "SLA/SLASupportTree.hpp" -#include "SLA/SLACommon.hpp" -#include "SLA/SLASpatIndex.hpp" - -// Workaround: IGL signed_distance.h will define PI in the igl namespace. -#undef PI - -// HEAVY headers... takes eternity to compile - -// for concave hull merging decisions -#include "SLABoostAdapter.hpp" -#include "boost/geometry/index/rtree.hpp" - -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable: 4244) -#pragma warning(disable: 4267) -#endif -#include -#include -#include -#include -#ifdef _MSC_VER -#pragma warning(pop) -#endif - -#include - -#include "SLASpatIndex.hpp" -#include "ClipperUtils.hpp" - -namespace Slic3r { -namespace sla { - -// Bring back PI from the igl namespace -using igl::PI; - -/* ************************************************************************** - * PointIndex implementation - * ************************************************************************** */ - -class PointIndex::Impl { -public: - using BoostIndex = boost::geometry::index::rtree< PointIndexEl, - boost::geometry::index::rstar<16, 4> /* ? */ >; - - BoostIndex m_store; -}; - -PointIndex::PointIndex(): m_impl(new Impl()) {} -PointIndex::~PointIndex() {} - -PointIndex::PointIndex(const PointIndex &cpy): m_impl(new Impl(*cpy.m_impl)) {} -PointIndex::PointIndex(PointIndex&& cpy): m_impl(std::move(cpy.m_impl)) {} - -PointIndex& PointIndex::operator=(const PointIndex &cpy) -{ - m_impl.reset(new Impl(*cpy.m_impl)); - return *this; -} - -PointIndex& PointIndex::operator=(PointIndex &&cpy) -{ - m_impl.swap(cpy.m_impl); - return *this; -} - -void PointIndex::insert(const PointIndexEl &el) -{ - m_impl->m_store.insert(el); -} - -bool PointIndex::remove(const PointIndexEl& el) -{ - return m_impl->m_store.remove(el) == 1; -} - -std::vector -PointIndex::query(std::function fn) const -{ - namespace bgi = boost::geometry::index; - - std::vector ret; - m_impl->m_store.query(bgi::satisfies(fn), std::back_inserter(ret)); - return ret; -} - -std::vector PointIndex::nearest(const Vec3d &el, unsigned k = 1) const -{ - namespace bgi = boost::geometry::index; - std::vector ret; ret.reserve(k); - m_impl->m_store.query(bgi::nearest(el, k), std::back_inserter(ret)); - return ret; -} - -size_t PointIndex::size() const -{ - return m_impl->m_store.size(); -} - -void PointIndex::foreach(std::function fn) -{ - for(auto& el : m_impl->m_store) fn(el); -} - -void PointIndex::foreach(std::function fn) const -{ - for(const auto &el : m_impl->m_store) fn(el); -} - -/* ************************************************************************** - * BoxIndex implementation - * ************************************************************************** */ - -class BoxIndex::Impl { -public: - using BoostIndex = boost::geometry::index:: - rtree /* ? */>; - - BoostIndex m_store; -}; - -BoxIndex::BoxIndex(): m_impl(new Impl()) {} -BoxIndex::~BoxIndex() {} - -BoxIndex::BoxIndex(const BoxIndex &cpy): m_impl(new Impl(*cpy.m_impl)) {} -BoxIndex::BoxIndex(BoxIndex&& cpy): m_impl(std::move(cpy.m_impl)) {} - -BoxIndex& BoxIndex::operator=(const BoxIndex &cpy) -{ - m_impl.reset(new Impl(*cpy.m_impl)); - return *this; -} - -BoxIndex& BoxIndex::operator=(BoxIndex &&cpy) -{ - m_impl.swap(cpy.m_impl); - return *this; -} - -void BoxIndex::insert(const BoxIndexEl &el) -{ - m_impl->m_store.insert(el); -} - -bool BoxIndex::remove(const BoxIndexEl& el) -{ - return m_impl->m_store.remove(el) == 1; -} - -std::vector BoxIndex::query(const BoundingBox &qrbb, - BoxIndex::QueryType qt) -{ - namespace bgi = boost::geometry::index; - - std::vector ret; ret.reserve(m_impl->m_store.size()); - - switch (qt) { - case qtIntersects: - m_impl->m_store.query(bgi::intersects(qrbb), std::back_inserter(ret)); - break; - case qtWithin: - m_impl->m_store.query(bgi::within(qrbb), std::back_inserter(ret)); - } - - return ret; -} - -size_t BoxIndex::size() const -{ - return m_impl->m_store.size(); -} - -void BoxIndex::foreach(std::function fn) -{ - for(auto& el : m_impl->m_store) fn(el); -} - -/* **************************************************************************** - * EigenMesh3D implementation - * ****************************************************************************/ - -class EigenMesh3D::AABBImpl: public igl::AABB { -public: -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - igl::WindingNumberAABB windtree; -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ -}; - -EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { - static const double dEPS = 1e-6; - - const stl_file& stl = tmesh.stl; - - auto&& bb = tmesh.bounding_box(); - m_ground_level += bb.min(Z); - - Eigen::MatrixXd V; - Eigen::MatrixXi F; - - V.resize(3*stl.stats.number_of_facets, 3); - F.resize(stl.stats.number_of_facets, 3); - for (unsigned int i = 0; i < stl.stats.number_of_facets; ++i) { - const stl_facet &facet = stl.facet_start[i]; - V.block<1, 3>(3 * i + 0, 0) = facet.vertex[0].cast(); - V.block<1, 3>(3 * i + 1, 0) = facet.vertex[1].cast(); - V.block<1, 3>(3 * i + 2, 0) = facet.vertex[2].cast(); - F(i, 0) = int(3*i+0); - F(i, 1) = int(3*i+1); - F(i, 2) = int(3*i+2); - } - - // We will convert this to a proper 3d mesh with no duplicate points. - Eigen::VectorXi SVI, SVJ; - igl::remove_duplicate_vertices(V, F, dEPS, m_V, SVI, SVJ, m_F); - - // Build the AABB accelaration tree - m_aabb->init(m_V, m_F); -#ifdef SLIC3R_SLA_NEEDS_WINDTREE - m_aabb->windtree.set_mesh(m_V, m_F); -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ -} - -EigenMesh3D::~EigenMesh3D() {} - -EigenMesh3D::EigenMesh3D(const EigenMesh3D &other): - m_V(other.m_V), m_F(other.m_F), m_ground_level(other.m_ground_level), - m_aabb( new AABBImpl(*other.m_aabb) ) {} - -EigenMesh3D::EigenMesh3D(const Contour3D &other) -{ - m_V.resize(Eigen::Index(other.points.size()), 3); - m_F.resize(Eigen::Index(other.faces3.size() + 2 * other.faces4.size()), 3); - - for (Eigen::Index i = 0; i < Eigen::Index(other.points.size()); ++i) - m_V.row(i) = other.points[size_t(i)]; - - for (Eigen::Index i = 0; i < Eigen::Index(other.faces3.size()); ++i) - m_F.row(i) = other.faces3[size_t(i)]; - - size_t N = other.faces3.size() + 2 * other.faces4.size(); - for (size_t i = other.faces3.size(); i < N; i += 2) { - size_t quad_idx = (i - other.faces3.size()) / 2; - auto & quad = other.faces4[quad_idx]; - m_F.row(Eigen::Index(i)) = Vec3i{quad(0), quad(1), quad(2)}; - m_F.row(Eigen::Index(i + 1)) = Vec3i{quad(2), quad(3), quad(0)}; - } -} - -EigenMesh3D &EigenMesh3D::operator=(const EigenMesh3D &other) -{ - m_V = other.m_V; - m_F = other.m_F; - m_ground_level = other.m_ground_level; - m_aabb.reset(new AABBImpl(*other.m_aabb)); return *this; -} - -EigenMesh3D::hit_result -EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const -{ - igl::Hit hit; - hit.t = std::numeric_limits::infinity(); - m_aabb->intersect_ray(m_V, m_F, s, dir, hit); - - hit_result ret(*this); - ret.m_t = double(hit.t); - ret.m_dir = dir; - ret.m_source = s; - if(!std::isinf(hit.t) && !std::isnan(hit.t)) ret.m_face_id = hit.id; - - return ret; -} - -std::vector -EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const -{ - std::vector outs; - std::vector hits; - m_aabb->intersect_ray(m_V, m_F, s, dir, hits); - - // The sort is necessary, the hits are not always sorted. - std::sort(hits.begin(), hits.end(), - [](const igl::Hit& a, const igl::Hit& b) { return a.t < b.t; }); - - // Convert the igl::Hit into hit_result - outs.reserve(hits.size()); - for (const igl::Hit& hit : hits) { - outs.emplace_back(EigenMesh3D::hit_result(*this)); - outs.back().m_t = double(hit.t); - outs.back().m_dir = dir; - outs.back().m_source = s; - if(!std::isinf(hit.t) && !std::isnan(hit.t)) - outs.back().m_face_id = hit.id; - } - - return outs; -} - -#ifdef SLIC3R_SLA_NEEDS_WINDTREE -EigenMesh3D::si_result EigenMesh3D::signed_distance(const Vec3d &p) const { - double sign = 0; double sqdst = 0; int i = 0; Vec3d c; - igl::signed_distance_winding_number(*m_aabb, m_V, m_F, m_aabb->windtree, - p, sign, sqdst, i, c); - - return si_result(sign * std::sqrt(sqdst), i, c); -} - -bool EigenMesh3D::inside(const Vec3d &p) const { - return m_aabb->windtree.inside(p); -} -#endif /* SLIC3R_SLA_NEEDS_WINDTREE */ - -double EigenMesh3D::squared_distance(const Vec3d &p, int& i, Vec3d& c) const { - double sqdst = 0; - Eigen::Matrix pp = p; - Eigen::Matrix cc; - sqdst = m_aabb->squared_distance(m_V, m_F, pp, i, cc); - c = cc; - return sqdst; -} - -/* **************************************************************************** - * Misc functions - * ****************************************************************************/ - -namespace { - -bool point_on_edge(const Vec3d& p, const Vec3d& e1, const Vec3d& e2, - double eps = 0.05) -{ - using Line3D = Eigen::ParametrizedLine; - - auto line = Line3D::Through(e1, e2); - double d = line.distance(p); - return std::abs(d) < eps; -} - -template double distance(const Vec& pp1, const Vec& pp2) { - auto p = pp2 - pp1; - return std::sqrt(p.transpose() * p); -} - -} - -PointSet normals(const PointSet& points, - const EigenMesh3D& mesh, - double eps, - std::function thr, // throw on cancel - const std::vector& pt_indices) -{ - if(points.rows() == 0 || mesh.V().rows() == 0 || mesh.F().rows() == 0) - return {}; - - std::vector range = pt_indices; - if(range.empty()) { - range.resize(size_t(points.rows()), 0); - std::iota(range.begin(), range.end(), 0); - } - - PointSet ret(range.size(), 3); - -// for (size_t ridx = 0; ridx < range.size(); ++ridx) - tbb::parallel_for(size_t(0), range.size(), - [&ret, &range, &mesh, &points, thr, eps](size_t ridx) - { - thr(); - auto eidx = Eigen::Index(range[ridx]); - int faceid = 0; - Vec3d p; - - mesh.squared_distance(points.row(eidx), faceid, p); - - auto trindex = mesh.F().row(faceid); - - const Vec3d& p1 = mesh.V().row(trindex(0)); - const Vec3d& p2 = mesh.V().row(trindex(1)); - const Vec3d& p3 = mesh.V().row(trindex(2)); - - // We should check if the point lies on an edge of the hosting triangle. - // If it does then all the other triangles using the same two points - // have to be searched and the final normal should be some kind of - // aggregation of the participating triangle normals. We should also - // consider the cases where the support point lies right on a vertex - // of its triangle. The procedure is the same, get the neighbor - // triangles and calculate an average normal. - - // mark the vertex indices of the edge. ia and ib marks and edge ic - // will mark a single vertex. - int ia = -1, ib = -1, ic = -1; - - if(std::abs(distance(p, p1)) < eps) { - ic = trindex(0); - } - else if(std::abs(distance(p, p2)) < eps) { - ic = trindex(1); - } - else if(std::abs(distance(p, p3)) < eps) { - ic = trindex(2); - } - else if(point_on_edge(p, p1, p2, eps)) { - ia = trindex(0); ib = trindex(1); - } - else if(point_on_edge(p, p2, p3, eps)) { - ia = trindex(1); ib = trindex(2); - } - else if(point_on_edge(p, p1, p3, eps)) { - ia = trindex(0); ib = trindex(2); - } - - // vector for the neigboring triangles including the detected one. - std::vector neigh; - if(ic >= 0) { // The point is right on a vertex of the triangle - for(int n = 0; n < mesh.F().rows(); ++n) { - thr(); - Vec3i ni = mesh.F().row(n); - if((ni(X) == ic || ni(Y) == ic || ni(Z) == ic)) - neigh.emplace_back(ni); - } - } - else if(ia >= 0 && ib >= 0) { // the point is on and edge - // now get all the neigboring triangles - for(int n = 0; n < mesh.F().rows(); ++n) { - thr(); - Vec3i ni = mesh.F().row(n); - if((ni(X) == ia || ni(Y) == ia || ni(Z) == ia) && - (ni(X) == ib || ni(Y) == ib || ni(Z) == ib)) - neigh.emplace_back(ni); - } - } - - // Calculate the normals for the neighboring triangles - std::vector neighnorms; neighnorms.reserve(neigh.size()); - for(const Vec3i& tri : neigh) { - const Vec3d& pt1 = mesh.V().row(tri(0)); - const Vec3d& pt2 = mesh.V().row(tri(1)); - const Vec3d& pt3 = mesh.V().row(tri(2)); - Eigen::Vector3d U = pt2 - pt1; - Eigen::Vector3d V = pt3 - pt1; - neighnorms.emplace_back(U.cross(V).normalized()); - } - - // Throw out duplicates. They would cause trouble with summing. We will - // use std::unique which works on sorted ranges. We will sort by the - // coefficient-wise sum of the normals. It should force the same - // elements to be consecutive. - std::sort(neighnorms.begin(), neighnorms.end(), - [](const Vec3d& v1, const Vec3d& v2){ - return v1.sum() < v2.sum(); - }); - - auto lend = std::unique(neighnorms.begin(), neighnorms.end(), - [](const Vec3d& n1, const Vec3d& n2) { - // Compare normals for equivalence. This is controvers stuff. - auto deq = [](double a, double b) { return std::abs(a-b) < 1e-3; }; - return deq(n1(X), n2(X)) && deq(n1(Y), n2(Y)) && deq(n1(Z), n2(Z)); - }); - - if(!neighnorms.empty()) { // there were neighbors to count with - // sum up the normals and then normalize the result again. - // This unification seems to be enough. - Vec3d sumnorm(0, 0, 0); - sumnorm = std::accumulate(neighnorms.begin(), lend, sumnorm); - sumnorm.normalize(); - ret.row(long(ridx)) = sumnorm; - } - else { // point lies safely within its triangle - Eigen::Vector3d U = p2 - p1; - Eigen::Vector3d V = p3 - p1; - ret.row(long(ridx)) = U.cross(V).normalized(); - } - }); - - return ret; -} - -namespace bgi = boost::geometry::index; -using Index3D = bgi::rtree< PointIndexEl, bgi::rstar<16, 4> /* ? */ >; - -namespace { - -bool cmp_ptidx_elements(const PointIndexEl& e1, const PointIndexEl& e2) -{ - return e1.second < e2.second; -}; - -ClusteredPoints cluster(Index3D &sindex, - unsigned max_points, - std::function( - const Index3D &, const PointIndexEl &)> qfn) -{ - using Elems = std::vector; - - // Recursive function for visiting all the points in a given distance to - // each other - std::function group = - [&sindex, &group, max_points, qfn](Elems& pts, Elems& cluster) - { - for(auto& p : pts) { - std::vector tmp = qfn(sindex, p); - - std::sort(tmp.begin(), tmp.end(), cmp_ptidx_elements); - - Elems newpts; - std::set_difference(tmp.begin(), tmp.end(), - cluster.begin(), cluster.end(), - std::back_inserter(newpts), cmp_ptidx_elements); - - int c = max_points && newpts.size() + cluster.size() > max_points? - int(max_points - cluster.size()) : int(newpts.size()); - - cluster.insert(cluster.end(), newpts.begin(), newpts.begin() + c); - std::sort(cluster.begin(), cluster.end(), cmp_ptidx_elements); - - if(!newpts.empty() && (!max_points || cluster.size() < max_points)) - group(newpts, cluster); - } - }; - - std::vector clusters; - for(auto it = sindex.begin(); it != sindex.end();) { - Elems cluster = {}; - Elems pts = {*it}; - group(pts, cluster); - - for(auto& c : cluster) sindex.remove(c); - it = sindex.begin(); - - clusters.emplace_back(cluster); - } - - ClusteredPoints result; - for(auto& cluster : clusters) { - result.emplace_back(); - for(auto c : cluster) result.back().emplace_back(c.second); - } - - return result; -} - -std::vector distance_queryfn(const Index3D& sindex, - const PointIndexEl& p, - double dist, - unsigned max_points) -{ - std::vector tmp; tmp.reserve(max_points); - sindex.query( - bgi::nearest(p.first, max_points), - std::back_inserter(tmp) - ); - - for(auto it = tmp.begin(); it < tmp.end(); ++it) - if(distance(p.first, it->first) > dist) it = tmp.erase(it); - - return tmp; -} - -} // namespace - -// Clustering a set of points by the given criteria -ClusteredPoints cluster( - const std::vector& indices, - std::function pointfn, - double dist, - unsigned max_points) -{ - // A spatial index for querying the nearest points - Index3D sindex; - - // Build the index - for(auto idx : indices) sindex.insert( std::make_pair(pointfn(idx), idx)); - - return cluster(sindex, max_points, - [dist, max_points](const Index3D& sidx, const PointIndexEl& p) - { - return distance_queryfn(sidx, p, dist, max_points); - }); -} - -// Clustering a set of points by the given criteria -ClusteredPoints cluster( - const std::vector& indices, - std::function pointfn, - std::function predicate, - unsigned max_points) -{ - // A spatial index for querying the nearest points - Index3D sindex; - - // Build the index - for(auto idx : indices) sindex.insert( std::make_pair(pointfn(idx), idx)); - - return cluster(sindex, max_points, - [max_points, predicate](const Index3D& sidx, const PointIndexEl& p) - { - std::vector tmp; tmp.reserve(max_points); - sidx.query(bgi::satisfies([p, predicate](const PointIndexEl& e){ - return predicate(p, e); - }), std::back_inserter(tmp)); - return tmp; - }); -} - -ClusteredPoints cluster(const PointSet& pts, double dist, unsigned max_points) -{ - // A spatial index for querying the nearest points - Index3D sindex; - - // Build the index - for(Eigen::Index i = 0; i < pts.rows(); i++) - sindex.insert(std::make_pair(Vec3d(pts.row(i)), unsigned(i))); - - return cluster(sindex, max_points, - [dist, max_points](const Index3D& sidx, const PointIndexEl& p) - { - return distance_queryfn(sidx, p, dist, max_points); - }); -} - -} // namespace sla -} // namespace Slic3r From d85fa8e9ab5a3a4d245487dca43c0aeabf1062b9 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 22 May 2020 18:21:52 +0200 Subject: [PATCH 10/12] EigenMesh3D now stores TriangleMesh inside, not a mesh in Eigen format Rotfinder was apparently building the AABB tree needlessly --- src/libslic3r/SLA/Common.cpp | 73 +++++++++++++++---------------- src/libslic3r/SLA/Contour3D.cpp | 12 ++--- src/libslic3r/SLA/EigenMesh3D.hpp | 26 ++++++----- src/libslic3r/SLA/Rotfinder.cpp | 20 +++------ 4 files changed, 62 insertions(+), 69 deletions(-) diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index 56c9d12ff..3182a2feb 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -184,42 +184,39 @@ void BoxIndex::foreach(std::function fn) class EigenMesh3D::AABBImpl { private: AABBTreeIndirect::Tree3f m_tree; - TriangleMesh m_triangle_mesh; // FIXME: There should be no extra copy - // maybe even the m_V and m_F are extra. This is just to see the new - // AABB tree in action + public: - void init(const TriangleMesh& tmesh) + void init(const TriangleMesh& tm) { - m_triangle_mesh = tmesh; m_tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set( - m_triangle_mesh.its.vertices, m_triangle_mesh.its.indices); + tm.its.vertices, tm.its.indices); } - void intersect_ray(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + void intersect_ray(const TriangleMesh& tm, const Vec3d& s, const Vec3d& dir, igl::Hit& hit) { - AABBTreeIndirect::intersect_ray_first_hit(m_triangle_mesh.its.vertices, - m_triangle_mesh.its.indices, + AABBTreeIndirect::intersect_ray_first_hit(tm.its.vertices, + tm.its.indices, m_tree, s, dir, hit); } - void intersect_ray(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + void intersect_ray(const TriangleMesh& tm, const Vec3d& s, const Vec3d& dir, std::vector& hits) { - AABBTreeIndirect::intersect_ray_all_hits(m_triangle_mesh.its.vertices, - m_triangle_mesh.its.indices, + AABBTreeIndirect::intersect_ray_all_hits(tm.its.vertices, + tm.its.indices, m_tree, s, dir, hits); } - double squared_distance(const Eigen::MatrixXd&, const Eigen::MatrixXi&, + double squared_distance(const TriangleMesh& tm, const Vec3d& point, int& i, Eigen::Matrix& closest) { size_t idx_unsigned = 0; Vec3d closest_vec3d(closest); double dist = AABBTreeIndirect::squared_distance_to_indexed_triangle_set( - m_triangle_mesh.its.vertices, - m_triangle_mesh.its.indices, + tm.its.vertices, + tm.its.indices, m_tree, point, idx_unsigned, closest_vec3d); i = int(idx_unsigned); closest = closest_vec3d; @@ -276,12 +273,12 @@ trigger linking error when attempting to use it. } #endif -EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { +EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh) + : m_aabb(new AABBImpl()), m_tm(tmesh) +{ auto&& bb = tmesh.bounding_box(); m_ground_level += bb.min(Z); - to_eigen_mesh(tmesh, m_V, m_F); - // Build the AABB accelaration tree m_aabb->init(tmesh); } @@ -289,10 +286,10 @@ EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { EigenMesh3D::~EigenMesh3D() {} EigenMesh3D::EigenMesh3D(const EigenMesh3D &other): - m_V(other.m_V), m_F(other.m_F), m_ground_level(other.m_ground_level), + m_tm(other.m_tm), m_ground_level(other.m_ground_level), m_aabb( new AABBImpl(*other.m_aabb) ) {} -EigenMesh3D::EigenMesh3D(const Contour3D &other) +/*EigenMesh3D::EigenMesh3D(const Contour3D &other) { m_V.resize(Eigen::Index(other.points.size()), 3); m_F.resize(Eigen::Index(other.faces3.size() + 2 * other.faces4.size()), 3); @@ -310,12 +307,11 @@ EigenMesh3D::EigenMesh3D(const Contour3D &other) m_F.row(Eigen::Index(i)) = Vec3i{quad(0), quad(1), quad(2)}; m_F.row(Eigen::Index(i + 1)) = Vec3i{quad(2), quad(3), quad(0)}; } -} +}*/ EigenMesh3D &EigenMesh3D::operator=(const EigenMesh3D &other) { - m_V = other.m_V; - m_F = other.m_F; + m_tm = other.m_tm; m_ground_level = other.m_ground_level; m_aabb.reset(new AABBImpl(*other.m_aabb)); return *this; } @@ -333,13 +329,14 @@ EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const #ifdef SLIC3R_HOLE_RAYCASTER if (! m_holes.empty()) { + // If there are holes, the hit_results will be made by // query_ray_hits (object) and filter_hits (holes): return filter_hits(query_ray_hits(s, dir)); } #endif - m_aabb->intersect_ray(m_V, m_F, s, dir, hit); + m_aabb->intersect_ray(m_tm, s, dir, hit); hit_result ret(*this); ret.m_t = double(hit.t); ret.m_dir = dir; @@ -357,7 +354,7 @@ EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const { std::vector outs; std::vector hits; - m_aabb->intersect_ray(m_V, m_F, s, dir, hits); + m_aabb->intersect_ray(m_tm, s, dir, hits); // The sort is necessary, the hits are not always sorted. std::sort(hits.begin(), hits.end(), @@ -489,7 +486,7 @@ double EigenMesh3D::squared_distance(const Vec3d &p, int& i, Vec3d& c) const { double sqdst = 0; Eigen::Matrix pp = p; Eigen::Matrix cc; - sqdst = m_aabb->squared_distance(m_V, m_F, pp, i, cc); + sqdst = m_aabb->squared_distance(m_tm, pp, i, cc); c = cc; return sqdst; } @@ -523,7 +520,7 @@ PointSet normals(const PointSet& points, std::function thr, // throw on cancel const std::vector& pt_indices) { - if (points.rows() == 0 || mesh.V().rows() == 0 || mesh.F().rows() == 0) + if (points.rows() == 0 || mesh.vertices().empty() || mesh.indices().empty()) return {}; std::vector range = pt_indices; @@ -545,11 +542,11 @@ PointSet normals(const PointSet& points, mesh.squared_distance(points.row(eidx), faceid, p); - auto trindex = mesh.F().row(faceid); + auto trindex = mesh.indices(faceid); - const Vec3d &p1 = mesh.V().row(trindex(0)); - const Vec3d &p2 = mesh.V().row(trindex(1)); - const Vec3d &p3 = mesh.V().row(trindex(2)); + const Vec3d &p1 = mesh.vertices(trindex(0)).cast(); + const Vec3d &p2 = mesh.vertices(trindex(1)).cast(); + const Vec3d &p3 = mesh.vertices(trindex(2)).cast(); // We should check if the point lies on an edge of the hosting // triangle. If it does then all the other triangles using the @@ -584,17 +581,17 @@ PointSet normals(const PointSet& points, // vector for the neigboring triangles including the detected one. std::vector neigh; if (ic >= 0) { // The point is right on a vertex of the triangle - for (int n = 0; n < mesh.F().rows(); ++n) { + for (size_t n = 0; n < mesh.indices().size(); ++n) { thr(); - Vec3i ni = mesh.F().row(n); + Vec3i ni = mesh.indices(n); if ((ni(X) == ic || ni(Y) == ic || ni(Z) == ic)) neigh.emplace_back(ni); } } else if (ia >= 0 && ib >= 0) { // the point is on and edge // now get all the neigboring triangles - for (int n = 0; n < mesh.F().rows(); ++n) { + for (size_t n = 0; n < mesh.indices().size(); ++n) { thr(); - Vec3i ni = mesh.F().row(n); + Vec3i ni = mesh.indices(n); if ((ni(X) == ia || ni(Y) == ia || ni(Z) == ia) && (ni(X) == ib || ni(Y) == ib || ni(Z) == ib)) neigh.emplace_back(ni); @@ -605,9 +602,9 @@ PointSet normals(const PointSet& points, std::vector neighnorms; neighnorms.reserve(neigh.size()); for (const Vec3i &tri : neigh) { - const Vec3d & pt1 = mesh.V().row(tri(0)); - const Vec3d & pt2 = mesh.V().row(tri(1)); - const Vec3d & pt3 = mesh.V().row(tri(2)); + const Vec3d & pt1 = mesh.vertices(tri(0)).cast(); + const Vec3d & pt2 = mesh.vertices(tri(1)).cast(); + const Vec3d & pt3 = mesh.vertices(tri(2)).cast(); Eigen::Vector3d U = pt2 - pt1; Eigen::Vector3d V = pt3 - pt1; neighnorms.emplace_back(U.cross(V).normalized()); diff --git a/src/libslic3r/SLA/Contour3D.cpp b/src/libslic3r/SLA/Contour3D.cpp index e39672b8b..408465d43 100644 --- a/src/libslic3r/SLA/Contour3D.cpp +++ b/src/libslic3r/SLA/Contour3D.cpp @@ -28,14 +28,14 @@ Contour3D::Contour3D(TriangleMesh &&trmesh) } Contour3D::Contour3D(const EigenMesh3D &emesh) { - points.reserve(size_t(emesh.V().rows())); - faces3.reserve(size_t(emesh.F().rows())); + points.reserve(emesh.vertices().size()); + faces3.reserve(emesh.indices().size()); - for (int r = 0; r < emesh.V().rows(); r++) - points.emplace_back(emesh.V().row(r).cast()); + for (const Vec3f& vert : emesh.vertices()) + points.emplace_back(vert.cast()); - for (int i = 0; i < emesh.F().rows(); i++) - faces3.emplace_back(emesh.F().row(i)); + for (const auto& ind : emesh.indices()) + faces3.emplace_back(ind); } Contour3D &Contour3D::merge(const Contour3D &ctr) diff --git a/src/libslic3r/SLA/EigenMesh3D.hpp b/src/libslic3r/SLA/EigenMesh3D.hpp index 248f3577f..bd42020b2 100644 --- a/src/libslic3r/SLA/EigenMesh3D.hpp +++ b/src/libslic3r/SLA/EigenMesh3D.hpp @@ -2,6 +2,7 @@ #define SLA_EIGENMESH3D_H #include +#include // There is an implementation of a hole-aware raycaster that was eventually @@ -15,8 +16,6 @@ namespace Slic3r { -class TriangleMesh; - namespace sla { struct Contour3D; @@ -30,8 +29,7 @@ void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, Triang class EigenMesh3D { class AABBImpl; - Eigen::MatrixXd m_V; - Eigen::MatrixXi m_F; + TriangleMesh m_tm; double m_ground_level = 0, m_gnd_offset = 0; std::unique_ptr m_aabb; @@ -59,8 +57,14 @@ public: inline void ground_level_offset(double o) { m_gnd_offset = o; } inline double ground_level_offset() const { return m_gnd_offset; } - inline const Eigen::MatrixXd& V() const { return m_V; } - inline const Eigen::MatrixXi& F() const { return m_F; } + const std::vector& vertices() const { return m_tm.its.vertices; } + const std::vector& indices() const { return m_tm.its.indices; } + const stl_vertex& vertices(size_t idx) const { + return m_tm.its.vertices[idx]; + } + const stl_triangle_vertex_indices& indices(size_t idx) const { + return m_tm.its.indices[idx]; + } // Result of a raycast class hit_result { @@ -148,10 +152,12 @@ public: } Vec3d normal_by_face_id(int face_id) const { - auto trindex = F().row(face_id); - const Vec3d& p1 = V().row(trindex(0)); - const Vec3d& p2 = V().row(trindex(1)); - const Vec3d& p3 = V().row(trindex(2)); + // FIXME: normals should be cached in TriangleMesh, there should be + // no need to recalculate them. + auto trindex = this->indices(face_id); + const Vec3d& p1 = this->vertices(trindex(0)).cast(); + const Vec3d& p2 = this->vertices(trindex(1)).cast(); + const Vec3d& p3 = this->vertices(trindex(2)).cast(); Eigen::Vector3d U = p2 - p1; Eigen::Vector3d V = p3 - p1; return U.cross(V).normalized(); diff --git a/src/libslic3r/SLA/Rotfinder.cpp b/src/libslic3r/SLA/Rotfinder.cpp index 9ec9837e2..fda8383b1 100644 --- a/src/libslic3r/SLA/Rotfinder.cpp +++ b/src/libslic3r/SLA/Rotfinder.cpp @@ -28,7 +28,7 @@ std::array find_best_rotation(const ModelObject& modelobj, // We will use only one instance of this converted mesh to examine different // rotations - EigenMesh3D emesh(modelobj.raw_mesh()); + const TriangleMesh& mesh = modelobj.raw_mesh(); // For current iteration number unsigned status = 0; @@ -44,10 +44,10 @@ std::array find_best_rotation(const ModelObject& modelobj, // call the status callback in each iteration but the actual value may be // the same for subsequent iterations (status goes from 0 to 100 but // iterations can be many more) - auto objfunc = [&emesh, &status, &statuscb, &stopcond, max_tries] + auto objfunc = [&mesh, &status, &statuscb, &stopcond, max_tries] (double rx, double ry, double rz) { - EigenMesh3D& m = emesh; + const TriangleMesh& m = mesh; // prepare the rotation transformation Transform3d rt = Transform3d::Identity(); @@ -68,18 +68,8 @@ std::array find_best_rotation(const ModelObject& modelobj, // area. The current function is only an example of how to optimize. // Later we can add more criteria like the number of overhangs, etc... - for(int i = 0; i < m.F().rows(); i++) { - auto idx = m.F().row(i); - - Vec3d p1 = m.V().row(idx(0)); - Vec3d p2 = m.V().row(idx(1)); - Vec3d p3 = m.V().row(idx(2)); - - Eigen::Vector3d U = p2 - p1; - Eigen::Vector3d V = p3 - p1; - - // So this is the normal - auto n = U.cross(V).normalized(); + for(size_t i = 0; i < m.stl.facet_start.size(); i++) { + Vec3d n = m.stl.facet_start[i].normal.cast(); // rotate the normal with the current rotation given by the solver n = rt * n; From 1f833921a20e2f161a19b28a6339641d7ea2e023 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Sat, 23 May 2020 00:45:53 +0200 Subject: [PATCH 11/12] More code cleaning,... optimizations regarding normals calculation removed unused EigenMesh3D(const Contour3D &other) constructor removed unused class si_result --- src/libslic3r/SLA/Common.cpp | 82 ++----------------------------- src/libslic3r/SLA/EigenMesh3D.hpp | 34 +------------ 2 files changed, 6 insertions(+), 110 deletions(-) diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index 3182a2feb..6eb6629c3 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -226,53 +226,6 @@ public: static const constexpr double MESH_EPS = 1e-6; -void to_eigen_mesh(const TriangleMesh &tmesh, Eigen::MatrixXd &V, Eigen::MatrixXi &F) -{ - const stl_file& stl = tmesh.stl; - - V.resize(3*stl.stats.number_of_facets, 3); - F.resize(stl.stats.number_of_facets, 3); - for (unsigned int i = 0; i < stl.stats.number_of_facets; ++i) { - const stl_facet &facet = stl.facet_start[i]; - V.block<1, 3>(3 * i + 0, 0) = facet.vertex[0].cast(); - V.block<1, 3>(3 * i + 1, 0) = facet.vertex[1].cast(); - V.block<1, 3>(3 * i + 2, 0) = facet.vertex[2].cast(); - F(i, 0) = int(3*i+0); - F(i, 1) = int(3*i+1); - F(i, 2) = int(3*i+2); - } - - if (!tmesh.has_shared_vertices()) - { - Eigen::MatrixXd rV; - Eigen::MatrixXi rF; - // We will convert this to a proper 3d mesh with no duplicate points. - Eigen::VectorXi SVI, SVJ; - igl::remove_duplicate_vertices(V, F, MESH_EPS, rV, SVI, SVJ, rF); - V = std::move(rV); - F = std::move(rF); - } -} - -void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, TriangleMesh &out); -#if 0 -Does this function really work? There seems to be an issue with it. -Currently it is not used anywhere, this way it stays visible but -trigger linking error when attempting to use it. -{ - Pointf3s points(size_t(V.rows())); - std::vector facets(size_t(F.rows())); - - for (Eigen::Index i = 0; i < V.rows(); ++i) - points[size_t(i)] = V.row(i); - - for (Eigen::Index i = 0; i < F.rows(); ++i) - facets[size_t(i)] = F.row(i); - - out = {points, facets}; -} -#endif - EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh) : m_aabb(new AABBImpl()), m_tm(tmesh) { @@ -289,25 +242,6 @@ EigenMesh3D::EigenMesh3D(const EigenMesh3D &other): m_tm(other.m_tm), m_ground_level(other.m_ground_level), m_aabb( new AABBImpl(*other.m_aabb) ) {} -/*EigenMesh3D::EigenMesh3D(const Contour3D &other) -{ - m_V.resize(Eigen::Index(other.points.size()), 3); - m_F.resize(Eigen::Index(other.faces3.size() + 2 * other.faces4.size()), 3); - - for (Eigen::Index i = 0; i < Eigen::Index(other.points.size()); ++i) - m_V.row(i) = other.points[size_t(i)]; - - for (Eigen::Index i = 0; i < Eigen::Index(other.faces3.size()); ++i) - m_F.row(i) = other.faces3[size_t(i)]; - - size_t N = other.faces3.size() + 2 * other.faces4.size(); - for (size_t i = other.faces3.size(); i < N; i += 2) { - size_t quad_idx = (i - other.faces3.size()) / 2; - auto & quad = other.faces4[quad_idx]; - m_F.row(Eigen::Index(i)) = Vec3i{quad(0), quad(1), quad(2)}; - m_F.row(Eigen::Index(i + 1)) = Vec3i{quad(2), quad(3), quad(0)}; - } -}*/ EigenMesh3D &EigenMesh3D::operator=(const EigenMesh3D &other) { @@ -579,13 +513,13 @@ PointSet normals(const PointSet& points, } // vector for the neigboring triangles including the detected one. - std::vector neigh; + std::vector neigh; if (ic >= 0) { // The point is right on a vertex of the triangle for (size_t n = 0; n < mesh.indices().size(); ++n) { thr(); Vec3i ni = mesh.indices(n); if ((ni(X) == ic || ni(Y) == ic || ni(Z) == ic)) - neigh.emplace_back(ni); + neigh.emplace_back(n); } } else if (ia >= 0 && ib >= 0) { // the point is on and edge // now get all the neigboring triangles @@ -594,21 +528,15 @@ PointSet normals(const PointSet& points, Vec3i ni = mesh.indices(n); if ((ni(X) == ia || ni(Y) == ia || ni(Z) == ia) && (ni(X) == ib || ni(Y) == ib || ni(Z) == ib)) - neigh.emplace_back(ni); + neigh.emplace_back(n); } } // Calculate the normals for the neighboring triangles std::vector neighnorms; neighnorms.reserve(neigh.size()); - for (const Vec3i &tri : neigh) { - const Vec3d & pt1 = mesh.vertices(tri(0)).cast(); - const Vec3d & pt2 = mesh.vertices(tri(1)).cast(); - const Vec3d & pt3 = mesh.vertices(tri(2)).cast(); - Eigen::Vector3d U = pt2 - pt1; - Eigen::Vector3d V = pt3 - pt1; - neighnorms.emplace_back(U.cross(V).normalized()); - } + for (size_t &tri_id : neigh) + neighnorms.emplace_back(mesh.normal_by_face_id(tri_id)); // Throw out duplicates. They would cause trouble with summing. We // will use std::unique which works on sorted ranges. We will sort diff --git a/src/libslic3r/SLA/EigenMesh3D.hpp b/src/libslic3r/SLA/EigenMesh3D.hpp index bd42020b2..8ab198efb 100644 --- a/src/libslic3r/SLA/EigenMesh3D.hpp +++ b/src/libslic3r/SLA/EigenMesh3D.hpp @@ -18,11 +18,6 @@ namespace Slic3r { namespace sla { -struct Contour3D; - -void to_eigen_mesh(const TriangleMesh &mesh, Eigen::MatrixXd &V, Eigen::MatrixXi &F); -void to_triangle_mesh(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, TriangleMesh &); - /// An index-triangle structure for libIGL functions. Also serves as an /// alternative (raw) input format for the SLASupportTree. // Implemented in libslic3r/SLA/Common.cpp @@ -43,7 +38,6 @@ class EigenMesh3D { public: explicit EigenMesh3D(const TriangleMesh&); - explicit EigenMesh3D(const Contour3D &other); EigenMesh3D(const EigenMesh3D& other); EigenMesh3D& operator=(const EigenMesh3D&); @@ -125,24 +119,6 @@ public: // Casts a ray on the mesh and returns all hits std::vector query_ray_hits(const Vec3d &s, const Vec3d &dir) const; - class si_result { - double m_value; - int m_fidx; - Vec3d m_p; - si_result(double val, int i, const Vec3d& c): - m_value(val), m_fidx(i), m_p(c) {} - friend class EigenMesh3D; - public: - - si_result() = delete; - - double value() const { return m_value; } - operator double() const { return m_value; } - const Vec3d& point_on_mesh() const { return m_p; } - int F_idx() const { return m_fidx; } - }; - - double squared_distance(const Vec3d& p, int& i, Vec3d& c) const; inline double squared_distance(const Vec3d &p) const { @@ -152,15 +128,7 @@ public: } Vec3d normal_by_face_id(int face_id) const { - // FIXME: normals should be cached in TriangleMesh, there should be - // no need to recalculate them. - auto trindex = this->indices(face_id); - const Vec3d& p1 = this->vertices(trindex(0)).cast(); - const Vec3d& p2 = this->vertices(trindex(1)).cast(); - const Vec3d& p3 = this->vertices(trindex(2)).cast(); - Eigen::Vector3d U = p2 - p1; - Eigen::Vector3d V = p3 - p1; - return U.cross(V).normalized(); + return m_tm.stl.facet_start[face_id].normal.cast(); } }; From 55395e046f00fb6509b41c9b06a50ed6c8dfbd49 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Sat, 23 May 2020 13:54:41 +0200 Subject: [PATCH 12/12] EigenMesh3D does not store a copy of the mesh Instead, it stores a pointer to extern TriangleMesh (which must not be destroyed before the EigenMesh3D object) --- src/libslic3r/SLA/Common.cpp | 44 ++++++++++++++++++++++++++++--- src/libslic3r/SLA/Common.hpp | 1 + src/libslic3r/SLA/EigenMesh3D.hpp | 21 ++++++--------- src/slic3r/GUI/MeshUtils.hpp | 4 +-- 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/libslic3r/SLA/Common.cpp b/src/libslic3r/SLA/Common.cpp index 6eb6629c3..a7420a7fb 100644 --- a/src/libslic3r/SLA/Common.cpp +++ b/src/libslic3r/SLA/Common.cpp @@ -227,7 +227,7 @@ public: static const constexpr double MESH_EPS = 1e-6; EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh) - : m_aabb(new AABBImpl()), m_tm(tmesh) + : m_aabb(new AABBImpl()), m_tm(&tmesh) { auto&& bb = tmesh.bounding_box(); m_ground_level += bb.min(Z); @@ -254,6 +254,42 @@ EigenMesh3D &EigenMesh3D::operator=(EigenMesh3D &&other) = default; EigenMesh3D::EigenMesh3D(EigenMesh3D &&other) = default; + + +const std::vector& EigenMesh3D::vertices() const +{ + return m_tm->its.vertices; +} + + + +const std::vector& EigenMesh3D::indices() const +{ + return m_tm->its.indices; +} + + + +const Vec3f& EigenMesh3D::vertices(size_t idx) const +{ + return m_tm->its.vertices[idx]; +} + + + +const Vec3i& EigenMesh3D::indices(size_t idx) const +{ + return m_tm->its.indices[idx]; +} + + + +Vec3d EigenMesh3D::normal_by_face_id(int face_id) const { + return m_tm->stl.facet_start[face_id].normal.cast(); +} + + + EigenMesh3D::hit_result EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const { @@ -270,7 +306,7 @@ EigenMesh3D::query_ray_hit(const Vec3d &s, const Vec3d &dir) const } #endif - m_aabb->intersect_ray(m_tm, s, dir, hit); + m_aabb->intersect_ray(*m_tm, s, dir, hit); hit_result ret(*this); ret.m_t = double(hit.t); ret.m_dir = dir; @@ -288,7 +324,7 @@ EigenMesh3D::query_ray_hits(const Vec3d &s, const Vec3d &dir) const { std::vector outs; std::vector hits; - m_aabb->intersect_ray(m_tm, s, dir, hits); + m_aabb->intersect_ray(*m_tm, s, dir, hits); // The sort is necessary, the hits are not always sorted. std::sort(hits.begin(), hits.end(), @@ -420,7 +456,7 @@ double EigenMesh3D::squared_distance(const Vec3d &p, int& i, Vec3d& c) const { double sqdst = 0; Eigen::Matrix pp = p; Eigen::Matrix cc; - sqdst = m_aabb->squared_distance(m_tm, pp, i, cc); + sqdst = m_aabb->squared_distance(*m_tm, pp, i, cc); c = cc; return sqdst; } diff --git a/src/libslic3r/SLA/Common.hpp b/src/libslic3r/SLA/Common.hpp index 91bdc0eb1..ca616cabc 100644 --- a/src/libslic3r/SLA/Common.hpp +++ b/src/libslic3r/SLA/Common.hpp @@ -13,6 +13,7 @@ namespace Slic3r { // Typedefs from Point.hpp typedef Eigen::Matrix Vec3f; typedef Eigen::Matrix Vec3d; +typedef Eigen::Matrix Vec3i; typedef Eigen::Matrix Vec4i; namespace sla { diff --git a/src/libslic3r/SLA/EigenMesh3D.hpp b/src/libslic3r/SLA/EigenMesh3D.hpp index 8ab198efb..a427dde3c 100644 --- a/src/libslic3r/SLA/EigenMesh3D.hpp +++ b/src/libslic3r/SLA/EigenMesh3D.hpp @@ -2,7 +2,6 @@ #define SLA_EIGENMESH3D_H #include -#include // There is an implementation of a hole-aware raycaster that was eventually @@ -16,6 +15,8 @@ namespace Slic3r { +class TriangleMesh; + namespace sla { /// An index-triangle structure for libIGL functions. Also serves as an @@ -24,7 +25,7 @@ namespace sla { class EigenMesh3D { class AABBImpl; - TriangleMesh m_tm; + const TriangleMesh* m_tm; double m_ground_level = 0, m_gnd_offset = 0; std::unique_ptr m_aabb; @@ -51,14 +52,10 @@ public: inline void ground_level_offset(double o) { m_gnd_offset = o; } inline double ground_level_offset() const { return m_gnd_offset; } - const std::vector& vertices() const { return m_tm.its.vertices; } - const std::vector& indices() const { return m_tm.its.indices; } - const stl_vertex& vertices(size_t idx) const { - return m_tm.its.vertices[idx]; - } - const stl_triangle_vertex_indices& indices(size_t idx) const { - return m_tm.its.indices[idx]; - } + const std::vector& vertices() const; + const std::vector& indices() const; + const Vec3f& vertices(size_t idx) const; + const Vec3i& indices(size_t idx) const; // Result of a raycast class hit_result { @@ -127,9 +124,7 @@ public: return squared_distance(p, i, c); } - Vec3d normal_by_face_id(int face_id) const { - return m_tm.stl.facet_start[face_id].normal.cast(); - } + Vec3d normal_by_face_id(int face_id) const; }; // Calculate the normals for the selected points (from 'points' set) on the diff --git a/src/slic3r/GUI/MeshUtils.hpp b/src/slic3r/GUI/MeshUtils.hpp index 3cb9b58f2..2758577a2 100644 --- a/src/slic3r/GUI/MeshUtils.hpp +++ b/src/slic3r/GUI/MeshUtils.hpp @@ -104,8 +104,8 @@ private: // whether certain points are visible or obscured by the mesh etc. class MeshRaycaster { public: - // The class makes a copy of the mesh as EigenMesh3D. - // The pointer can be invalidated after constructor returns. + // The class references extern TriangleMesh, which must stay alive + // during MeshRaycaster existence. MeshRaycaster(const TriangleMesh& mesh) : m_emesh(mesh) {