From ed4b71eb1535c5773fb789d41c874b13875ee3f2 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Fri, 14 Jun 2019 10:55:56 +0200 Subject: [PATCH 1/4] Layers editing overlay rendering modified to use a texture drawn on a quad lying in a plane parallel to the camera viewport --- resources/shaders/variable_layer_height.vs | 9 ++++++++- src/slic3r/GUI/GLCanvas3D.cpp | 22 ++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/resources/shaders/variable_layer_height.vs b/resources/shaders/variable_layer_height.vs index 9763859d0..4f98dfa56 100644 --- a/resources/shaders/variable_layer_height.vs +++ b/resources/shaders/variable_layer_height.vs @@ -15,6 +15,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074); #define INTENSITY_AMBIENT 0.3 uniform mat4 volume_world_matrix; +uniform float object_max_z; // x = tainted, y = specular; varying vec2 intensity; @@ -42,6 +43,12 @@ void main() intensity.x += NdotL * LIGHT_FRONT_DIFFUSE; // Scaled to widths of the Z texture. - object_z = (volume_world_matrix * gl_Vertex).z; + if (object_max_z > 0.0) + // when rendering the overlay + object_z = object_max_z * gl_MultiTexCoord0.y; + else + // when rendering the volumes + object_z = (volume_world_matrix * gl_Vertex).z; + gl_Position = ftransform(); } diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index dbfde926c..21f1d23cd 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -63,11 +63,6 @@ static const float GROUND_Z = -0.02f; static const float GIZMO_RESET_BUTTON_HEIGHT = 22.0f; static const float GIZMO_RESET_BUTTON_WIDTH = 70.f; -static const float UNIT_MATRIX[] = { 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f }; - static const float DEFAULT_BG_DARK_COLOR[3] = { 0.478f, 0.478f, 0.478f }; static const float DEFAULT_BG_LIGHT_COLOR[3] = { 0.753f, 0.753f, 0.753f }; static const float ERROR_BG_DARK_COLOR[3] = { 0.478f, 0.192f, 0.039f }; @@ -452,8 +447,7 @@ void GLCanvas3D::LayersEditing::_render_active_object_annotations(const GLCanvas m_shader.set_uniform("z_texture_row_to_normalized", 1.0f / (float)m_layers_texture.height); m_shader.set_uniform("z_cursor", m_object_max_z * this->get_cursor_z_relative(canvas)); m_shader.set_uniform("z_cursor_band_width", band_width); - // The shader requires the original model coordinates when rendering to the texture, so we pass it the unit matrix - m_shader.set_uniform("volume_world_matrix", UNIT_MATRIX); + m_shader.set_uniform("object_max_z", m_object_max_z); glsafe(::glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); glsafe(::glBindTexture(GL_TEXTURE_2D, m_z_texture_id)); @@ -466,10 +460,10 @@ void GLCanvas3D::LayersEditing::_render_active_object_annotations(const GLCanvas ::glBegin(GL_QUADS); ::glNormal3f(0.0f, 0.0f, 1.0f); - ::glVertex3f(l, b, 0.0f); - ::glVertex3f(r, b, 0.0f); - ::glVertex3f(r, t, m_object_max_z); - ::glVertex3f(l, t, m_object_max_z); + ::glTexCoord2f(0.0f, 0.0f); ::glVertex2f(l, b); + ::glTexCoord2f(1.0f, 0.0f); ::glVertex2f(r, b); + ::glTexCoord2f(1.0f, 1.0f); ::glVertex2f(r, t); + ::glTexCoord2f(0.0f, 1.0f); ::glVertex2f(l, t); glsafe(::glEnd()); glsafe(::glBindTexture(GL_TEXTURE_2D, 0)); @@ -522,6 +516,7 @@ void GLCanvas3D::LayersEditing::render_volumes(const GLCanvas3D& canvas, const G GLint z_cursor_id = ::glGetUniformLocation(shader_id, "z_cursor"); GLint z_cursor_band_width_id = ::glGetUniformLocation(shader_id, "z_cursor_band_width"); GLint world_matrix_id = ::glGetUniformLocation(shader_id, "volume_world_matrix"); + GLint object_max_z_id = ::glGetUniformLocation(shader_id, "object_max_z"); glcheck(); if (z_to_texture_row_id != -1 && z_texture_row_to_normalized_id != -1 && z_cursor_id != -1 && z_cursor_band_width_id != -1 && world_matrix_id != -1) @@ -548,7 +543,10 @@ void GLCanvas3D::LayersEditing::render_volumes(const GLCanvas3D& canvas, const G // Render the object using the layer editing shader and texture. if (! glvolume->is_active || glvolume->composite_id.object_id != this->last_object_id || glvolume->is_modifier) continue; - glsafe(::glUniformMatrix4fv(world_matrix_id, 1, GL_FALSE, (const GLfloat*)glvolume->world_matrix().cast().data())); + if (world_matrix_id != -1) + glsafe(::glUniformMatrix4fv(world_matrix_id, 1, GL_FALSE, (const GLfloat*)glvolume->world_matrix().cast().data())); + if (object_max_z_id != -1) + glsafe(::glUniform1f(object_max_z_id, GLfloat(0))); glvolume->render(); } // Revert back to the previous shader. From c5037540e97be3585839736d5070420a166697a7 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Fri, 14 Jun 2019 18:10:16 +0200 Subject: [PATCH 2/4] Warning hunt session. --- src/libslic3r/SLA/SLABasePool.cpp | 26 +++--- src/libslic3r/SLA/SLABoilerPlate.hpp | 4 +- src/libslic3r/SLA/SLASupportTree.cpp | 4 +- src/libslic3r/SLAPrint.cpp | 125 ++++++++++++++++----------- src/libslic3r/SLAPrint.hpp | 40 +++++---- src/libslic3r/libslic3r.h | 13 +++ 6 files changed, 126 insertions(+), 86 deletions(-) diff --git a/src/libslic3r/SLA/SLABasePool.cpp b/src/libslic3r/SLA/SLABasePool.cpp index 171d2b8d0..553920f53 100644 --- a/src/libslic3r/SLA/SLABasePool.cpp +++ b/src/libslic3r/SLA/SLABasePool.cpp @@ -53,7 +53,7 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, // Shorthand for the vertex arrays auto& upoints = upper.points, &lpoints = lower.points; - auto& rpts = ret.points; auto& rfaces = ret.indices; + auto& rpts = ret.points; auto& ind = ret.indices; // If the Z levels are flipped, or the offset difference is negative, we // will interpret that as the triangles normals should be inverted. @@ -61,7 +61,7 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, // Copy the points into the mesh, convert them from 2D to 3D rpts.reserve(upoints.size() + lpoints.size()); - rfaces.reserve(2*upoints.size() + 2*lpoints.size()); + ind.reserve(2*upoints.size() + 2*lpoints.size()); const double sf = SCALING_FACTOR; for(auto& p : upoints) rpts.emplace_back(p.x()*sf, p.y()*sf, upper_z_mm); for(auto& p : lpoints) rpts.emplace_back(p.x()*sf, p.y()*sf, lower_z_mm); @@ -121,9 +121,9 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, case Proceed::UPPER: if(!ustarted || uidx != uendidx) { // there are vertices remaining // Get the 3D vertices in order - const Vec3d& p_up1 = rpts[size_t(uidx)]; - const Vec3d& p_low = rpts[size_t(lidx)]; - const Vec3d& p_up2 = rpts[size_t(unextidx)]; + const Vec3d& p_up1 = rpts[uidx]; + const Vec3d& p_low = rpts[lidx]; + const Vec3d& p_up2 = rpts[unextidx]; // Calculate fitness: the average of the two connecting edges double a = offsdiff2 - (distfn(p_up1, p_low) - zdiff2); @@ -133,8 +133,9 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, if(current_fit > prev_fit) { // fit is worse than previously proceed = Proceed::LOWER; } else { // good to go, create the triangle - inverted? rfaces.emplace_back(unextidx, lidx, uidx) : - rfaces.emplace_back(uidx, lidx, unextidx) ; + inverted + ? ind.emplace_back(int(unextidx), int(lidx), int(uidx)) + : ind.emplace_back(int(uidx), int(lidx), int(unextidx)); // Increment the iterators, rotate if necessary ++uidx; ++unextidx; @@ -150,9 +151,9 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, case Proceed::LOWER: // Mode with lower segment, upper vertex. Same structure: if(!lstarted || lidx != lendidx) { - const Vec3d& p_low1 = rpts[size_t(lidx)]; - const Vec3d& p_low2 = rpts[size_t(lnextidx)]; - const Vec3d& p_up = rpts[size_t(uidx)]; + const Vec3d& p_low1 = rpts[lidx]; + const Vec3d& p_low2 = rpts[lnextidx]; + const Vec3d& p_up = rpts[uidx]; double a = offsdiff2 - (distfn(p_up, p_low1) - zdiff2); double b = offsdiff2 - (distfn(p_up, p_low2) - zdiff2); @@ -161,8 +162,9 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, if(current_fit > prev_fit) { proceed = Proceed::UPPER; } else { - inverted? rfaces.emplace_back(uidx, lnextidx, lidx) : - rfaces.emplace_back(lidx, lnextidx, uidx); + inverted + ? ind.emplace_back(int(uidx), int(lnextidx), int(lidx)) + : ind.emplace_back(int(lidx), int(lnextidx), int(uidx)); ++lidx; ++lnextidx; if(lnextidx == rpts.size()) lnextidx = offs; diff --git a/src/libslic3r/SLA/SLABoilerPlate.hpp b/src/libslic3r/SLA/SLABoilerPlate.hpp index 602121af9..be900f532 100644 --- a/src/libslic3r/SLA/SLABoilerPlate.hpp +++ b/src/libslic3r/SLA/SLABoilerPlate.hpp @@ -36,12 +36,10 @@ inline coord_t x(const Vec3crd& p) { return p(0); } inline coord_t y(const Vec3crd& p) { return p(1); } inline coord_t z(const Vec3crd& p) { return p(2); } -using Indices = std::vector; - /// Intermediate struct for a 3D mesh struct Contour3D { Pointf3s points; - Indices indices; + std::vector indices; void merge(const Contour3D& ctr) { auto s3 = coord_t(points.size()); diff --git a/src/libslic3r/SLA/SLASupportTree.cpp b/src/libslic3r/SLA/SLASupportTree.cpp index cb2001024..ae033c62f 100644 --- a/src/libslic3r/SLA/SLASupportTree.cpp +++ b/src/libslic3r/SLA/SLASupportTree.cpp @@ -236,13 +236,13 @@ Contour3D cylinder(double r, double h, size_t ssteps, const Vec3d sp = {0,0,0}) // According to the slicing algorithms, we need to aid them with generating // a watertight body. So we create a triangle fan for the upper and lower // ending of the cylinder to close the geometry. - points.emplace_back(jp); size_t ci = points.size() - 1; + points.emplace_back(jp); int ci = int(points.size() - 1); for(int i = 0; i < steps - 1; ++i) indices.emplace_back(i + offs + 1, i + offs, ci); indices.emplace_back(offs, steps + offs - 1, ci); - points.emplace_back(endp); ci = points.size() - 1; + points.emplace_back(endp); ci = int(points.size() - 1); for(int i = 0; i < steps - 1; ++i) indices.emplace_back(ci, i, i + 1); diff --git a/src/libslic3r/SLAPrint.cpp b/src/libslic3r/SLAPrint.cpp index f6a1c429e..324008cf0 100644 --- a/src/libslic3r/SLAPrint.cpp +++ b/src/libslic3r/SLAPrint.cpp @@ -28,14 +28,16 @@ namespace Slic3r { using SupportTreePtr = std::unique_ptr; -class SLAPrintObject::SupportData { +class SLAPrintObject::SupportData +{ public: - sla::EigenMesh3D emesh; // index-triangle representation - std::vector support_points; // all the support points (manual/auto) - SupportTreePtr support_tree_ptr; // the supports - SlicedSupports support_slices; // sliced supports + sla::EigenMesh3D emesh; // index-triangle representation + std::vector + support_points; // all the support points (manual/auto) + SupportTreePtr support_tree_ptr; // the supports + SlicedSupports support_slices; // sliced supports - inline SupportData(const TriangleMesh& trmesh): emesh(trmesh) {} + inline SupportData(const TriangleMesh &trmesh) : emesh(trmesh) {} }; namespace { @@ -666,11 +668,11 @@ void SLAPrint::process() double ilhd = m_material_config.initial_layer_height.getFloat(); auto ilh = float(ilhd); - auto ilhs = coord_t(ilhd / SCALING_FACTOR); + auto ilhs = scaled(ilhd); const size_t objcount = m_objects.size(); - const unsigned min_objstatus = 0; // where the per object operations start - const unsigned max_objstatus = 50; // where the per object operations end + static const unsigned min_objstatus = 0; // where the per object operations start + static const unsigned max_objstatus = 50; // where the per object operations end // the coefficient that multiplies the per object status values which // are set up for <0, 100>. They need to be scaled into the whole process @@ -687,31 +689,32 @@ void SLAPrint::process() // Slicing the model object. This method is oversimplified and needs to // be compared with the fff slicing algorithm for verification - auto slice_model = [this, ilhs, ilh, ilhd](SLAPrintObject& po) { + auto slice_model = [this, ilhs, ilh](SLAPrintObject& po) { const TriangleMesh& mesh = po.transformed_mesh(); // We need to prepare the slice index... double lhd = m_objects.front()->m_config.layer_height.getFloat(); float lh = float(lhd); - auto lhs = coord_t(lhd / SCALING_FACTOR); + auto lhs = scaled(lhd); - auto&& bb3d = mesh.bounding_box(); - double minZ = bb3d.min(Z) - po.get_elevation(); - double maxZ = bb3d.max(Z); + auto &&bb3d = mesh.bounding_box(); + double minZ = bb3d.min(Z) - po.get_elevation(); + double maxZ = bb3d.max(Z); + auto minZf = float(minZ); - auto minZs = coord_t(minZ / SCALING_FACTOR); - auto maxZs = coord_t(maxZ / SCALING_FACTOR); + auto minZs = scaled(minZ); + auto maxZs = scaled(maxZ); po.m_slice_index.clear(); size_t cap = size_t(1 + (maxZs - minZs - ilhs) / lhs); po.m_slice_index.reserve(cap); - po.m_slice_index.emplace_back(minZs + ilhs, minZ + ilhd / 2.0, ilh); + po.m_slice_index.emplace_back(minZs + ilhs, minZf + ilh / 2.f, ilh); - for(coord_t h = minZs + ilhs + lhs; h <= maxZs; h += lhs) - po.m_slice_index.emplace_back(h, h*SCALING_FACTOR - lhd / 2.0, lh); + for(coord_t h = minZs + ilhs + lhs; h <= maxZs; h += lhs) + po.m_slice_index.emplace_back(h, unscaled(h) - lh / 2.f, lh); // Just get the first record that is form the model: auto slindex_it = @@ -737,15 +740,15 @@ void SLAPrint::process() auto mit = slindex_it; double doffs = m_printer_config.absolute_correction.getFloat(); - coord_t clpr_offs = coord_t(doffs / SCALING_FACTOR); + coord_t clpr_offs = scaled(doffs); for(size_t id = 0; id < po.m_model_slices.size() && mit != po.m_slice_index.end(); id++) { // We apply the printer correction offset here. if(clpr_offs != 0) - po.m_model_slices[id] = - offset_ex(po.m_model_slices[id], clpr_offs); + po.m_model_slices[id] = + offset_ex(po.m_model_slices[id], float(clpr_offs)); mit->set_model_slice_idx(po, id); ++mit; } @@ -949,15 +952,15 @@ void SLAPrint::process() } double doffs = m_printer_config.absolute_correction.getFloat(); - coord_t clpr_offs = coord_t(doffs / SCALING_FACTOR); + coord_t clpr_offs = scaled(doffs); for(size_t i = 0; i < sd->support_slices.size() && i < po.m_slice_index.size(); ++i) { // We apply the printer correction offset here. if(clpr_offs != 0) - sd->support_slices[i] = - offset_ex(sd->support_slices[i], clpr_offs); + sd->support_slices[i] = + offset_ex(sd->support_slices[i], float(clpr_offs)); po.m_slice_index[i].set_support_slice_idx(po, i); } @@ -1063,8 +1066,8 @@ void SLAPrint::process() const int fade_layers_cnt = m_default_object_config.faded_layers.getInt();// 10 // [3;20] - const double width = m_printer_config.display_width.getFloat() / SCALING_FACTOR; - const double height = m_printer_config.display_height.getFloat() / SCALING_FACTOR; + const double width = scaled(m_printer_config.display_width.getFloat()); + const double height = scaled(m_printer_config.display_height.getFloat()); const double display_area = width*height; // get polygons for all instances in the object @@ -1170,13 +1173,20 @@ void SLAPrint::process() ClipperPolygons model_polygons; ClipperPolygons supports_polygons; - size_t c = std::accumulate(layer.slices().begin(), layer.slices().end(), 0u, [](size_t a, const SliceRecord& sr) { - return a + sr.get_slice(soModel).size(); + size_t c = std::accumulate(layer.slices().begin(), + layer.slices().end(), + size_t(0), + [](size_t a, const SliceRecord &sr) { + return a + sr.get_slice(soModel) + .size(); }); model_polygons.reserve(c); - c = std::accumulate(layer.slices().begin(), layer.slices().end(), 0u, [](size_t a, const SliceRecord& sr) { + c = std::accumulate(layer.slices().begin(), + layer.slices().end(), + size_t(0), + [](size_t a, const SliceRecord &sr) { return a + sr.get_slice(soModel).size(); }); @@ -1264,8 +1274,9 @@ void SLAPrint::process() // for(size_t i = 0; i < m_printer_input.size(); ++i) printlayerfn(i); tbb::parallel_for(0, m_printer_input.size(), printlayerfn); - m_print_statistics.support_used_material = supports_volume * SCALING_FACTOR * SCALING_FACTOR; - m_print_statistics.objects_used_material = models_volume * SCALING_FACTOR * SCALING_FACTOR; + auto SCALING2 = SCALING_FACTOR * SCALING_FACTOR; + m_print_statistics.support_used_material = supports_volume * SCALING2; + m_print_statistics.objects_used_material = models_volume * SCALING2; // Estimated printing time // A layers count o the highest object @@ -1281,7 +1292,7 @@ void SLAPrint::process() }; // Rasterizing the model objects, and their supports - auto rasterize = [this, max_objstatus]() { + auto rasterize = [this]() { if(canceled()) return; // collect all the keys @@ -1376,11 +1387,12 @@ void SLAPrint::process() tbb::parallel_for(0, lvlcnt, lvlfn); // Set statistics values to the printer - m_printer->set_statistics({(m_print_statistics.objects_used_material + m_print_statistics.support_used_material)/1000, - double(m_default_object_config.faded_layers.getInt()), - double(m_print_statistics.slow_layers_count), - double(m_print_statistics.fast_layers_count) - }); + m_printer->set_statistics( + {(m_print_statistics.objects_used_material + + m_print_statistics.support_used_material) / 1000, + double(m_default_object_config.faded_layers.getInt()), + double(m_print_statistics.slow_layers_count), + double(m_print_statistics.fast_layers_count)}); }; using slaposFn = std::function; @@ -1408,25 +1420,36 @@ void SLAPrint::process() // TODO: this loop could run in parallel but should not exhaust all the CPU // power available - // Calculate the support structures first before slicing the supports, so that the preview will get displayed ASAP for all objects. - std::vector step_ranges = { slaposObjectSlice, slaposSliceSupports, slaposCount }; - for (size_t idx_range = 0; idx_range + 1 < step_ranges.size(); ++ idx_range) { - for(SLAPrintObject * po : m_objects) { + // Calculate the support structures first before slicing the supports, + // so that the preview will get displayed ASAP for all objects. + std::vector step_ranges = {slaposObjectSlice, + slaposSliceSupports, + slaposCount}; - BOOST_LOG_TRIVIAL(info) << "Slicing object " << po->model_object()->name; + for (size_t idx_range = 0; idx_range + 1 < step_ranges.size(); ++idx_range) { + for (SLAPrintObject *po : m_objects) { - for (int s = int(step_ranges[idx_range]); s < int(step_ranges[idx_range + 1]); ++s) { + BOOST_LOG_TRIVIAL(info) + << "Slicing object " << po->model_object()->name; + + for (int s = int(step_ranges[idx_range]); + s < int(step_ranges[idx_range + 1]); + ++s) { auto currentstep = static_cast(s); - // Cancellation checking. Each step will check for cancellation - // on its own and return earlier gracefully. Just after it returns - // execution gets to this point and throws the canceled signal. + // Cancellation checking. Each step will check for + // cancellation on its own and return earlier gracefully. + // Just after it returns execution gets to this point and + // throws the canceled signal. throw_if_canceled(); st += incr * ostepd; - if(po->m_stepmask[currentstep] && po->set_started(currentstep)) { - m_report_status(*this, st, OBJ_STEP_LABELS(currentstep)); + if (po->m_stepmask[currentstep] + && po->set_started(currentstep)) { + m_report_status(*this, + st, + OBJ_STEP_LABELS(currentstep)); pobj_program[currentstep](*po); throw_if_canceled(); po->set_done(currentstep); @@ -1786,8 +1809,8 @@ std::vector SLAPrintObject::transformed_support_points() cons ret.reserve(spts.size()); for(sla::SupportPoint& sp : spts) { - Vec3d transformed_pos = trafo() * Vec3d(sp.pos(0), sp.pos(1), sp.pos(2)); - ret.emplace_back(transformed_pos(0), transformed_pos(1), transformed_pos(2), sp.head_front_radius, sp.is_new_island); + Vec3f transformed_pos = trafo().cast() * sp.pos; + ret.emplace_back(transformed_pos, sp.head_front_radius, sp.is_new_island); } return ret; diff --git a/src/libslic3r/SLAPrint.hpp b/src/libslic3r/SLAPrint.hpp index dea468e7a..0c7d92ff2 100644 --- a/src/libslic3r/SLAPrint.hpp +++ b/src/libslic3r/SLAPrint.hpp @@ -54,15 +54,15 @@ public: bool is_left_handed() const { return m_left_handed; } struct Instance { - Instance(ModelID instance_id, const Point &shift, float rotation) : instance_id(instance_id), shift(shift), rotation(rotation) {} - bool operator==(const Instance &rhs) const { return this->instance_id == rhs.instance_id && this->shift == rhs.shift && this->rotation == rhs.rotation; } - // ID of the corresponding ModelInstance. - ModelID instance_id; - // Slic3r::Point objects in scaled G-code coordinates - Point shift; - // Rotation along the Z axis, in radians. - float rotation; - }; + Instance(ModelID instance_id, const Point &shift, float rotation) : instance_id(instance_id), shift(shift), rotation(rotation) {} + bool operator==(const Instance &rhs) const { return this->instance_id == rhs.instance_id && this->shift == rhs.shift && this->rotation == rhs.rotation; } + // ID of the corresponding ModelInstance. + ModelID instance_id; + // Slic3r::Point objects in scaled G-code coordinates + Point shift; + // Rotation along the Z axis, in radians. + float rotation; + }; const std::vector& instances() const { return m_instances; } bool has_mesh(SLAPrintObjectStep step) const; @@ -142,15 +142,19 @@ public: }; private: - - template inline static T level(const SliceRecord& sr) { + template inline static T level(const SliceRecord &sr) + { static_assert(std::is_arithmetic::value, "Arithmetic only!"); - return std::is_integral::value ? T(sr.print_level()) : T(sr.slice_level()); + return std::is_integral::value ? T(sr.print_level()) + : T(sr.slice_level()); } - template inline static SliceRecord create_slice_record(T val) { + template inline static SliceRecord create_slice_record(T val) + { static_assert(std::is_arithmetic::value, "Arithmetic only!"); - return std::is_integral::value ? SliceRecord{ coord_t(val), 0.f, 0.f } : SliceRecord{ 0, float(val), 0.f }; + return std::is_integral::value + ? SliceRecord{coord_t(val), 0.f, 0.f} + : SliceRecord{0, float(val), 0.f}; } // This is a template method for searching the slice index either by @@ -241,11 +245,11 @@ protected: ~SLAPrintObject(); void config_apply(const ConfigBase &other, bool ignore_nonexistent = false) { this->m_config.apply(other, ignore_nonexistent); } - void config_apply_only(const ConfigBase &other, const t_config_option_keys &keys, bool ignore_nonexistent = false) - { this->m_config.apply_only(other, keys, ignore_nonexistent); } + void config_apply_only(const ConfigBase &other, const t_config_option_keys &keys, bool ignore_nonexistent = false) + { this->m_config.apply_only(other, keys, ignore_nonexistent); } void set_trafo(const Transform3d& trafo, bool left_handed) { - m_transformed_rmesh.invalidate([this, &trafo, left_handed](){ m_trafo = trafo; m_left_handed = left_handed; }); + m_transformed_rmesh.invalidate([this, &trafo, left_handed](){ m_trafo = trafo; m_left_handed = left_handed; }); } template inline void set_instances(InstVec&& instances) { m_instances = std::forward(instances); } @@ -380,7 +384,7 @@ public: void set_task(const TaskParams ¶ms) override; void process() override; void finalize() override; - // Returns true if an object step is done on all objects and there's at least one object. + // Returns true if an object step is done on all objects and there's at least one object. bool is_step_done(SLAPrintObjectStep step) const; // Returns true if the last step was finished with success. bool finished() const override { return this->is_step_done(slaposSliceSupports) && this->Inherited::is_step_done(slapsRasterize); } diff --git a/src/libslic3r/libslic3r.h b/src/libslic3r/libslic3r.h index 560d74696..e1d247894 100644 --- a/src/libslic3r/libslic3r.h +++ b/src/libslic3r/libslic3r.h @@ -48,6 +48,19 @@ typedef double coordf_t; //FIXME Better to use an inline function with an explicit return type. //inline coord_t scale_(coordf_t v) { return coord_t(floor(v / SCALING_FACTOR + 0.5f)); } #define scale_(val) ((val) / SCALING_FACTOR) + +template inline constexpr coord_t scaled(Tf val) +{ + static_assert (std::is_floating_point::value, "Floating point only"); + return coord_t(val / Tf(SCALING_FACTOR)); +} + +template inline constexpr Tf unscaled(coord_t val) +{ + static_assert (std::is_floating_point::value, "Floating point only"); + return Tf(val * Tf(SCALING_FACTOR)); +} + #define SCALED_EPSILON scale_(EPSILON) #define SLIC3R_DEBUG_OUT_PATH_PREFIX "out/" From 9ffd294f072c55e8afa04fc78e44354abf262544 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Mon, 17 Jun 2019 09:28:41 +0200 Subject: [PATCH 3/4] Fixed functions declaration template inline constexpr coord_t scaled(Tf val) and template inline constexpr Tf unscaled(coord_t val) to use constexpr on versions of Visual Studio which support it --- src/libslic3r/libslic3r.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libslic3r/libslic3r.h b/src/libslic3r/libslic3r.h index e1d247894..c95c95c1a 100644 --- a/src/libslic3r/libslic3r.h +++ b/src/libslic3r/libslic3r.h @@ -49,13 +49,21 @@ typedef double coordf_t; //inline coord_t scale_(coordf_t v) { return coord_t(floor(v / SCALING_FACTOR + 0.5f)); } #define scale_(val) ((val) / SCALING_FACTOR) +#if defined(_MSC_VER) && (_MSC_VER < 1910) +template inline coord_t scaled(Tf val) +#else template inline constexpr coord_t scaled(Tf val) +#endif // _MSC_VER { static_assert (std::is_floating_point::value, "Floating point only"); return coord_t(val / Tf(SCALING_FACTOR)); } +#if defined(_MSC_VER) && (_MSC_VER < 1910) +template inline Tf unscaled(coord_t val) +#else template inline constexpr Tf unscaled(coord_t val) +#endif // _MSC_VER { static_assert (std::is_floating_point::value, "Floating point only"); return Tf(val * Tf(SCALING_FACTOR)); From ce222517075c7da14af59f056fc8890b61e0987d Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Mon, 17 Jun 2019 10:05:46 +0200 Subject: [PATCH 4/4] Warning hunt session followup --- src/libslic3r/SLA/SLABasePool.cpp | 85 +++++++--------------------- src/libslic3r/SLA/SLABoilerPlate.hpp | 5 -- src/libslic3r/libslic3r.h | 26 +++++---- src/slic3r/GUI/MainFrame.cpp | 3 +- 4 files changed, 37 insertions(+), 82 deletions(-) diff --git a/src/libslic3r/SLA/SLABasePool.cpp b/src/libslic3r/SLA/SLABasePool.cpp index 553920f53..3b199c4eb 100644 --- a/src/libslic3r/SLA/SLABasePool.cpp +++ b/src/libslic3r/SLA/SLABasePool.cpp @@ -61,10 +61,11 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, // Copy the points into the mesh, convert them from 2D to 3D rpts.reserve(upoints.size() + lpoints.size()); - ind.reserve(2*upoints.size() + 2*lpoints.size()); - const double sf = SCALING_FACTOR; - for(auto& p : upoints) rpts.emplace_back(p.x()*sf, p.y()*sf, upper_z_mm); - for(auto& p : lpoints) rpts.emplace_back(p.x()*sf, p.y()*sf, lower_z_mm); + ind.reserve(2 * upoints.size() + 2 * lpoints.size()); + for (auto &p : upoints) + rpts.emplace_back(unscaled(p.x()), unscaled(p.y()), upper_z_mm); + for (auto &p : lpoints) + rpts.emplace_back(unscaled(p.x()), unscaled(p.y()), lower_z_mm); // Create pointing indices into vertex arrays. u-upper, l-lower size_t uidx = 0, lidx = offs, unextidx = 1, lnextidx = offs + 1; @@ -202,7 +203,7 @@ void offset(ExPolygon& sh, coord_t distance) { } ClipperOffset offs; - offs.ArcTolerance = 0.01*mm(1); + offs.ArcTolerance = 0.01*scaled(1.0); Paths result; offs.AddPath(ctour, jtRound, etClosedPolygon); offs.AddPaths(holes, jtRound, etClosedPolygon); @@ -305,16 +306,6 @@ ExPolygons unify(const ExPolygons& shapes) { return retv; } -/// Only a debug function to generate top and bottom plates from a 2D shape. -/// It is not used in the algorithm directly. -inline Contour3D roofs(const ExPolygon& poly, coord_t z_distance) { - auto lower = triangulate_expolygon_3d(poly); - auto upper = triangulate_expolygon_3d(poly, z_distance*SCALING_FACTOR, true); - Contour3D ret; - ret.merge(lower); ret.merge(upper); - return ret; -} - /// This method will create a rounded edge around a flat polygon in 3d space. /// 'base_plate' parameter is the target plate. /// 'radius' is the radius of the edges. @@ -360,7 +351,7 @@ Contour3D round_edges(const ExPolygon& base_plate, double x2 = xx*xx; double stepy = std::sqrt(r2 - x2); - offset(ob, s*mm(xx)); + offset(ob, s*scaled(xx)); wh = ceilheight_mm - radius_mm + stepy; Contour3D pwalls; @@ -384,7 +375,7 @@ Contour3D round_edges(const ExPolygon& base_plate, double xx = radius_mm - i*stepx; double x2 = xx*xx; double stepy = std::sqrt(r2 - x2); - offset(ob, s*mm(xx)); + offset(ob, s*scaled(xx)); wh = ceilheight_mm - radius_mm - stepy; Contour3D pwalls; @@ -404,41 +395,6 @@ Contour3D round_edges(const ExPolygon& base_plate, return curvedwalls; } -/// Generating the concave part of the 3D pool with the bottom plate and the -/// side walls. -Contour3D inner_bed(const ExPolygon& poly, - double depth_mm, - double begin_h_mm = 0) -{ - Contour3D bottom; - Pointf3s triangles = triangulate_expolygon_3d(poly, -depth_mm + begin_h_mm); - bottom.merge(triangles); - - coord_t depth = mm(depth_mm); - coord_t begin_h = mm(begin_h_mm); - - auto lines = poly.lines(); - - // Generate outer walls - auto fp = [](const Point& p, Point::coord_type z) { - return unscale(x(p), y(p), z); - }; - - for(auto& l : lines) { - auto s = coord_t(bottom.points.size()); - - bottom.points.emplace_back(fp(l.a, -depth + begin_h)); - bottom.points.emplace_back(fp(l.b, -depth + begin_h)); - bottom.points.emplace_back(fp(l.a, begin_h)); - bottom.points.emplace_back(fp(l.b, begin_h)); - - bottom.indices.emplace_back(s + 3, s + 1, s); - bottom.indices.emplace_back(s + 2, s + 3, s); - } - - return bottom; -} - inline Point centroid(Points& pp) { Point c; switch(pp.size()) { @@ -520,7 +476,7 @@ ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50, double dx = x(c) - x(cc), dy = y(c) - y(cc); double l = std::sqrt(dx * dx + dy * dy); double nx = dx / l, ny = dy / l; - double max_dist = mm(max_dist_mm); + double max_dist = scaled(max_dist_mm); ExPolygon& expo = punion[idx++]; BoundingBox querybb(expo); @@ -536,10 +492,10 @@ ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50, ctour.reserve(3); ctour.emplace_back(cc); - Point d(coord_t(mm(1)*nx), coord_t(mm(1)*ny)); + Point d(coord_t(scaled(1.)*nx), coord_t(scaled(1.)*ny)); ctour.emplace_back(c + Point( -y(d), x(d) )); ctour.emplace_back(c + Point( y(d), -x(d) )); - offset(r, mm(1)); + offset(r, scaled(1.)); return r; }); @@ -571,15 +527,16 @@ void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h, // Now we have to unify all slice layers which can be an expensive operation // so we will try to simplify the polygons ExPolygons tmp; tmp.reserve(count); - for(ExPolygons& o : out) for(ExPolygon& e : o) { - auto&& exss = e.simplify(0.1/SCALING_FACTOR); - for(ExPolygon& ep : exss) tmp.emplace_back(std::move(ep)); - } + for(ExPolygons& o : out) + for(ExPolygon& e : o) { + auto&& exss = e.simplify(scaled(0.1)); + for(ExPolygon& ep : exss) tmp.emplace_back(std::move(ep)); + } ExPolygons utmp = unify(tmp); for(auto& o : utmp) { - auto&& smp = o.simplify(0.1/SCALING_FACTOR); + auto&& smp = o.simplify(scaled(0.1)); output.insert(output.end(), smp.begin(), smp.end()); } } @@ -609,11 +566,11 @@ Contour3D create_base_pool(const ExPolygons &ground_layer, const double bottom_offs = (thickness + wingheight) / std::tan(slope); // scaled values - const coord_t s_thickness = mm(thickness); - const coord_t s_eradius = mm(cfg.edge_radius_mm); + const coord_t s_thickness = scaled(thickness); + const coord_t s_eradius = scaled(cfg.edge_radius_mm); const coord_t s_safety_dist = 2*s_eradius + coord_t(0.8*s_thickness); - const coord_t s_wingdist = mm(wingdist); - const coord_t s_bottom_offs = mm(bottom_offs); + const coord_t s_wingdist = scaled(wingdist); + const coord_t s_bottom_offs = scaled(bottom_offs); auto& thrcl = cfg.throw_on_cancel; diff --git a/src/libslic3r/SLA/SLABoilerPlate.hpp b/src/libslic3r/SLA/SLABoilerPlate.hpp index be900f532..86e90f3b7 100644 --- a/src/libslic3r/SLA/SLABoilerPlate.hpp +++ b/src/libslic3r/SLA/SLABoilerPlate.hpp @@ -11,11 +11,6 @@ namespace Slic3r { namespace sla { -using coord_t = Point::coord_type; - -/// get the scaled clipper units for a millimeter value -inline coord_t mm(double v) { return coord_t(v/SCALING_FACTOR); } - /// Get x and y coordinates (because we are eigenizing...) inline coord_t x(const Point& p) { return p(0); } inline coord_t y(const Point& p) { return p(1); } diff --git a/src/libslic3r/libslic3r.h b/src/libslic3r/libslic3r.h index c95c95c1a..8cafae17c 100644 --- a/src/libslic3r/libslic3r.h +++ b/src/libslic3r/libslic3r.h @@ -49,29 +49,31 @@ typedef double coordf_t; //inline coord_t scale_(coordf_t v) { return coord_t(floor(v / SCALING_FACTOR + 0.5f)); } #define scale_(val) ((val) / SCALING_FACTOR) -#if defined(_MSC_VER) && (_MSC_VER < 1910) -template inline coord_t scaled(Tf val) +#define SCALED_EPSILON scale_(EPSILON) + +#define SLIC3R_DEBUG_OUT_PATH_PREFIX "out/" + +#if defined(_MSC_VER) && _MSC_VER < 1900 +# define SLIC3R_CONSTEXPR +# define SLIC3R_NOEXCEPT #else -template inline constexpr coord_t scaled(Tf val) -#endif // _MSC_VER +#define SLIC3R_CONSTEXPR constexpr +#define SLIC3R_NOEXCEPT noexcept +#endif + +template inline SLIC3R_CONSTEXPR coord_t scaled(Tf val) { static_assert (std::is_floating_point::value, "Floating point only"); return coord_t(val / Tf(SCALING_FACTOR)); } -#if defined(_MSC_VER) && (_MSC_VER < 1910) -template inline Tf unscaled(coord_t val) -#else -template inline constexpr Tf unscaled(coord_t val) -#endif // _MSC_VER +template inline SLIC3R_CONSTEXPR Tf unscaled(coord_t val) { static_assert (std::is_floating_point::value, "Floating point only"); return Tf(val * Tf(SCALING_FACTOR)); } -#define SCALED_EPSILON scale_(EPSILON) - -#define SLIC3R_DEBUG_OUT_PATH_PREFIX "out/" +inline SLIC3R_CONSTEXPR float unscaledf(coord_t val) { return unscaled(val); } inline std::string debug_out_path(const char *name, ...) { diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 6091f10a1..7e5b3ec05 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -976,7 +976,8 @@ void MainFrame::load_config(const DynamicPrintConfig& config) if (! boost::algorithm::ends_with(opt_key, "_settings_id")) tab->get_config()->option(opt_key)->set(config.option(opt_key)); } - wxGetApp().load_current_presets(); + + wxGetApp().load_current_presets(); #endif }