diff options
author | tamasmeszaros <meszaros.q@gmail.com> | 2019-12-16 16:04:26 +0300 |
---|---|---|
committer | tamasmeszaros <meszaros.q@gmail.com> | 2019-12-16 16:04:26 +0300 |
commit | 878f8a8ead6ffa4b53e77b3dd7ff226dc74fe0b1 (patch) | |
tree | c7d50eb070a3788afa63503dda2c42e37ad26bdf /sandboxes | |
parent | b1186e339dd7b756d5995f2e74de7fdc6144d696 (diff) |
Add convexity to csgsettings. Defer all rendering to Display.
Diffstat (limited to 'sandboxes')
-rw-r--r-- | sandboxes/opencsg/GLScene.cpp | 143 | ||||
-rw-r--r-- | sandboxes/opencsg/GLScene.hpp | 51 | ||||
-rw-r--r-- | sandboxes/opencsg/main.cpp | 65 |
3 files changed, 169 insertions, 90 deletions
diff --git a/sandboxes/opencsg/GLScene.cpp b/sandboxes/opencsg/GLScene.cpp index 60eb0ce03..5037ef6a5 100644 --- a/sandboxes/opencsg/GLScene.cpp +++ b/sandboxes/opencsg/GLScene.cpp @@ -99,13 +99,15 @@ void Display::render_scene() GLfloat color[] = {1.f, 1.f, 0.f, 0.f}; glsafe(::glColor4fv(color)); - OpenCSG::render(m_scene->csg_primitives()); + if (m_csgsettings.is_enabled()) { + OpenCSG::render(m_scene_cache.primitives_csg); + glDepthFunc(GL_EQUAL); + } - glDepthFunc(GL_EQUAL); - for (auto& p : m_scene->csg_primitives()) p->render(); - glDepthFunc(GL_LESS); + for (auto& p : m_scene_cache.primitives_csg) p->render(); + if (m_csgsettings.is_enabled()) glDepthFunc(GL_LESS); - for (auto& p : m_scene->free_primitives()) p->render(); + for (auto& p : m_scene_cache.primitives_free) p->render(); glFlush(); } @@ -127,53 +129,8 @@ std::vector<V> transform_pts( } void Scene::set_print(uqptr<SLAPrint> &&print) -{ +{ m_print = std::move(print); - - for (const SLAPrintObject *po : m_print->objects()) { - const ModelObject *mo = po->model_object(); - TriangleMesh msh = mo->raw_mesh(); - - sla::DrainHoles holedata = mo->sla_drain_holes; - - for (const ModelInstance *mi : mo->instances) { - - TriangleMesh mshinst = msh; - auto interior = po->hollowed_interior_mesh(); - interior.transform(po->trafo().inverse()); - - mshinst.merge(interior); - mshinst.require_shared_vertices(); - - mi->transform_mesh(&mshinst); - - auto bb = mshinst.bounding_box(); - auto center = bb.center().cast<float>(); - mshinst.translate(-center); - - mshinst.require_shared_vertices(); - add_mesh(mshinst, OpenCSG::Intersection, 15); - - auto tr = Transform3f::Identity(); - tr.translate(-center); - - transform_pts(holedata.begin(), holedata.end(), tr, - [](const sla::DrainHole &dh) { - return dh.pos; - }); - - transform_pts(holedata.begin(), holedata.end(), tr, - [](const sla::DrainHole &dh) { - return dh.normal; - }); - } - - for (const sla::DrainHole &holept : holedata) { - TriangleMesh holemesh = sla::to_triangle_mesh(holept.to_mesh()); - holemesh.require_shared_vertices(); - add_mesh(holemesh, OpenCSG::Subtraction, 1); - } - } // Notify displays call(&Display::on_scene_updated, m_displays); @@ -184,21 +141,30 @@ BoundingBoxf3 Scene::get_bounding_box() const return m_print->model().bounding_box(); } -shptr<Primitive> Scene::add_mesh(const TriangleMesh &mesh) +void Display::SceneCache::clear() +{ + primitives_csg.clear(); + primitives_free.clear(); + primitives.clear(); +} + +shptr<Primitive> Display::SceneCache::add_mesh(const TriangleMesh &mesh) { auto p = std::make_shared<Primitive>(); p->load_mesh(mesh); - m_primitives.emplace_back(p); - m_primitives_free.emplace_back(p.get()); + primitives.emplace_back(p); + primitives_free.emplace_back(p.get()); return p; } -shptr<Primitive> Scene::add_mesh(const TriangleMesh &mesh, OpenCSG::Operation o, unsigned c) +shptr<Primitive> Display::SceneCache::add_mesh(const TriangleMesh &mesh, + OpenCSG::Operation o, + unsigned c) { auto p = std::make_shared<Primitive>(o, c); p->load_mesh(mesh); - m_primitives.emplace_back(p); - m_primitives_csg.emplace_back(p.get()); + primitives.emplace_back(p); + primitives_csg.emplace_back(p.get()); return p; } @@ -457,18 +423,79 @@ void Display::on_moved_to(long x, long y) void Display::apply_csgsettings(const CSGSettings &settings) { using namespace OpenCSG; + + bool update = m_csgsettings.get_convexity() != settings.get_convexity(); + m_csgsettings = settings; setOption(AlgorithmSetting, m_csgsettings.get_algo()); setOption(DepthComplexitySetting, m_csgsettings.get_depth_algo()); setOption(DepthBoundsOptimization, m_csgsettings.get_optimization()); + + if (update) on_scene_updated(); + + repaint(); } void Display::on_scene_updated() { + const SLAPrint *print = m_scene->get_print(); + if (!print) return; + + { auto bb = m_scene->get_bounding_box(); double d = std::max(std::max(bb.size().x(), bb.size().y()), bb.size().z()); m_wheel_pos = long(2 * d); m_camera->set_zoom(m_wheel_pos); + } + + m_scene_cache.clear(); + + for (const SLAPrintObject *po : print->objects()) { + const ModelObject *mo = po->model_object(); + TriangleMesh msh = mo->raw_mesh(); + + sla::DrainHoles holedata = mo->sla_drain_holes; + + for (const ModelInstance *mi : mo->instances) { + + TriangleMesh mshinst = msh; + auto interior = po->hollowed_interior_mesh(); + interior.transform(po->trafo().inverse()); + + mshinst.merge(interior); + mshinst.require_shared_vertices(); + + mi->transform_mesh(&mshinst); + + auto bb = mshinst.bounding_box(); + auto center = bb.center().cast<float>(); + mshinst.translate(-center); + + mshinst.require_shared_vertices(); + m_scene_cache.add_mesh(mshinst, OpenCSG::Intersection, + m_csgsettings.get_convexity()); + + auto tr = Transform3f::Identity(); + tr.translate(-center); + + transform_pts(holedata.begin(), holedata.end(), tr, + [](const sla::DrainHole &dh) { + return dh.pos; + }); + + transform_pts(holedata.begin(), holedata.end(), tr, + [](const sla::DrainHole &dh) { + return dh.normal; + }); + } + + for (const sla::DrainHole &holept : holedata) { + TriangleMesh holemesh = sla::to_triangle_mesh(holept.to_mesh()); + holemesh.require_shared_vertices(); + m_scene_cache.add_mesh(holemesh, OpenCSG::Subtraction, 1); + } + } + repaint(); } @@ -513,4 +540,6 @@ bool enable_multisampling(bool e) else return false; } +MouseInput::Listener::~Listener() = default; + }} // namespace Slic3r::GL diff --git a/sandboxes/opencsg/GLScene.hpp b/sandboxes/opencsg/GLScene.hpp index 6beecb79a..a127ab90b 100644 --- a/sandboxes/opencsg/GLScene.hpp +++ b/sandboxes/opencsg/GLScene.hpp @@ -45,8 +45,7 @@ public: class Listener { public: - - virtual ~Listener() = default; + virtual ~Listener(); virtual void on_left_click_down() {} virtual void on_left_click_up() {} @@ -219,9 +218,16 @@ public: }; class CSGSettings { +public: + static const constexpr unsigned DEFAULT_CONVEXITY = 10; + +private: OpenCSG::Algorithm m_csgalg = OpenCSG::Algorithm::Automatic; OpenCSG::DepthComplexityAlgorithm m_depth_algo = OpenCSG::DepthComplexityAlgorithm::NoDepthComplexitySampling; OpenCSG::Optimization m_optim = OpenCSG::Optimization::OptimizationDefault; + bool m_enable = true; + unsigned int m_convexity = DEFAULT_CONVEXITY; + public: int get_algo() const { return int(m_csgalg); } void set_algo(OpenCSG::Algorithm alg) { m_csgalg = alg; } @@ -231,6 +237,12 @@ public: int get_optimization() const { return int(m_optim); } void set_optimization(OpenCSG::Optimization o) { m_optim = o; } + + void enable_csg(bool en = true) { m_enable = en; } + bool is_enabled() const { return m_enable; } + + unsigned get_convexity() const { return m_convexity; } + void set_convexity(unsigned c) { m_convexity = c; } }; class Display : public std::enable_shared_from_this<Display>, @@ -247,6 +259,19 @@ protected: shptr<Camera> m_camera; + struct SceneCache { + Collection<shptr<Primitive>> primitives; + Collection<Primitive *> primitives_free; + Collection<OpenCSG::Primitive *> primitives_csg; + + void clear(); + + shptr<Primitive> add_mesh(const TriangleMesh &mesh); + shptr<Primitive> add_mesh(const TriangleMesh &mesh, + OpenCSG::Operation op, + unsigned covexity); + } m_scene_cache; + public: Display(shptr<Scene> scene = nullptr, shptr<Camera> camera = nullptr) : m_scene(scene) @@ -284,26 +309,12 @@ public: class Scene: public MouseInput::Listener { - Collection<shptr<Primitive>> m_primitives; - Collection<Primitive *> m_primitives_free; - Collection<OpenCSG::Primitive *> m_primitives_csg; - uqptr<SLAPrint> m_print; public: Scene(); ~Scene(); - const Collection<Primitive*>& free_primitives() const - { - return m_primitives_free; - } - - const Collection<OpenCSG::Primitive*>& csg_primitives() const - { - return m_primitives_csg; - } - void add_display(shptr<Display> disp) { m_displays.emplace_back(disp); @@ -311,16 +322,10 @@ public: } void set_print(uqptr<SLAPrint> &&print); + const SLAPrint * get_print() const { return m_print.get(); } BoundingBoxf3 get_bounding_box() const; -protected: - - shptr<Primitive> add_mesh(const TriangleMesh &mesh); - shptr<Primitive> add_mesh(const TriangleMesh &mesh, - OpenCSG::Operation op, - unsigned covexity); - private: Collection<wkptr<Display>> m_displays; diff --git a/sandboxes/opencsg/main.cpp b/sandboxes/opencsg/main.cpp index 8c106fa85..6274f264a 100644 --- a/sandboxes/opencsg/main.cpp +++ b/sandboxes/opencsg/main.cpp @@ -14,6 +14,7 @@ #include <wx/slider.h> #include <wx/tglbtn.h> #include <wx/combobox.h> +#include <wx/spinctrl.h> #include <wx/glcanvas.h> #include "Canvas.hpp" @@ -111,24 +112,52 @@ public: auto controlsizer = new wxBoxSizer(wxHORIZONTAL); auto slider_sizer = new wxBoxSizer(wxVERTICAL); auto console_sizer = new wxBoxSizer(wxVERTICAL); - - auto slider = new wxSlider(control_panel, wxID_ANY, 0, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL); + + auto slider = new wxSlider(control_panel, wxID_ANY, 0, 0, 100, + wxDefaultPosition, wxDefaultSize, + wxSL_VERTICAL); slider_sizer->Add(slider, 1, wxEXPAND); - auto toggle = new wxToggleButton(control_panel, wxID_ANY, "Multisampling"); - console_sizer->Add(toggle, 0, wxALL | wxEXPAND, 5); - + auto ms_toggle = new wxToggleButton(control_panel, wxID_ANY, "Multisampling"); + console_sizer->Add(ms_toggle, 0, wxALL | wxEXPAND, 5); + + auto csg_toggle = new wxToggleButton(control_panel, wxID_ANY, "CSG"); + csg_toggle->SetValue(true); + console_sizer->Add(csg_toggle, 0, wxALL | wxEXPAND, 5); + auto add_combobox = [control_panel, console_sizer] - (const wxString &label, std::vector<wxString> &&list) { + (const wxString &label, std::vector<wxString> &&list) + { auto widget = new wxComboBox(control_panel, wxID_ANY, list[0], wxDefaultPosition, wxDefaultSize, int(list.size()), list.data()); + + auto sz = new wxBoxSizer(wxHORIZONTAL); + sz->Add(new wxStaticText(control_panel, wxID_ANY, label), 0, + wxALL | wxALIGN_CENTER, 5); + sz->Add(widget, 1, wxALL | wxEXPAND, 5); + console_sizer->Add(sz, 0, wxEXPAND); + return widget; + }; + + auto add_spinctl = [control_panel, console_sizer] + (const wxString &label, int initial, int min, int max) + { + auto widget = new wxSpinCtrl( + control_panel, wxID_ANY, + wxString::Format("%d", initial), + wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, min, max, + initial); + auto sz = new wxBoxSizer(wxHORIZONTAL); - sz->Add(new wxStaticText(control_panel, wxID_ANY, label), 0, wxALL | wxALIGN_CENTER, 5); + sz->Add(new wxStaticText(control_panel, wxID_ANY, label), 0, + wxALL | wxALIGN_CENTER, 5); sz->Add(widget, 1, wxALL | wxEXPAND, 5); console_sizer->Add(sz, 0, wxEXPAND); return widget; }; + + auto convexity_spin = add_spinctl("Convexity", CSGSettings::DEFAULT_CONVEXITY, 0, 100); auto alg_select = add_combobox("Algorithm", {"Auto", "Goldfeather", "SCS"}); auto depth_select = add_combobox("Depth Complexity", {"Off", "OcclusionQuery", "On"}); @@ -152,10 +181,16 @@ public: m_canvas->move_clip_plane(double(slider->GetValue())); }, slider->GetId()); - Bind(wxEVT_TOGGLEBUTTON, [this, toggle](wxCommandEvent &){ - enable_multisampling(toggle->GetValue()); + Bind(wxEVT_TOGGLEBUTTON, [this, ms_toggle](wxCommandEvent &){ + enable_multisampling(ms_toggle->GetValue()); m_canvas->repaint(); - }, toggle->GetId()); + }, ms_toggle->GetId()); + + Bind(wxEVT_TOGGLEBUTTON, [this, csg_toggle](wxCommandEvent &){ + CSGSettings settings = m_canvas->get_csgsettings(); + settings.enable_csg(csg_toggle->GetValue()); + m_canvas->apply_csgsettings(settings); + }, csg_toggle->GetId()); Bind(wxEVT_COMBOBOX, [this, alg_select, depth_select](wxCommandEvent &) { @@ -182,6 +217,16 @@ public: m_canvas->apply_csgsettings(settings); }, depth_select->GetId()); + Bind(wxEVT_SPINCTRL, [this, convexity_spin](wxSpinEvent &) { + CSGSettings settings = m_canvas->get_csgsettings(); + int c = convexity_spin->GetValue(); + + if (c > 0) { + settings.set_convexity(unsigned(c)); + m_canvas->apply_csgsettings(settings); + } + }, convexity_spin->GetId()); + m_canvas->set_scene(std::make_shared<Slic3r::GL::Scene>()); } |