From 8fdb0fddc09c005bd48d3163bb4f4f2b814782ec Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Wed, 26 May 2021 17:38:57 +0200 Subject: [PATCH] Get rid of Contour3D --- src/libslic3r/CMakeLists.txt | 2 - src/libslic3r/Format/SL1.cpp | 4 +- src/libslic3r/Format/SL1.hpp | 6 +- src/libslic3r/OpenVDBUtils.hpp | 2 - src/libslic3r/SLA/Contour3D.cpp | 149 ----------------------- src/libslic3r/SLA/Contour3D.hpp | 48 -------- src/libslic3r/SLA/Hollowing.cpp | 6 - src/libslic3r/SLA/Hollowing.hpp | 4 +- src/libslic3r/SlicesToTriangleMesh.cpp | 115 +++++++++-------- src/libslic3r/SlicesToTriangleMesh.hpp | 8 +- src/slic3r/GUI/Jobs/SLAImportJob.cpp | 19 +-- tests/libslic3r/test_marchingsquares.cpp | 8 +- tests/sla_print/sla_print_tests.cpp | 9 -- 13 files changed, 86 insertions(+), 294 deletions(-) delete mode 100644 src/libslic3r/SLA/Contour3D.cpp delete mode 100644 src/libslic3r/SLA/Contour3D.hpp diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index 47e9ff5ae..ed7a3c220 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -266,8 +266,6 @@ add_library(libslic3r STATIC SLA/SupportPoint.hpp SLA/SupportPointGenerator.hpp SLA/SupportPointGenerator.cpp - SLA/Contour3D.hpp - SLA/Contour3D.cpp SLA/IndexedMesh.hpp SLA/IndexedMesh.cpp SLA/Clustering.hpp diff --git a/src/libslic3r/Format/SL1.cpp b/src/libslic3r/Format/SL1.cpp index 1809c3e4f..f556b0ead 100644 --- a/src/libslic3r/Format/SL1.cpp +++ b/src/libslic3r/Format/SL1.cpp @@ -296,7 +296,7 @@ void import_sla_archive(const std::string &zipfname, DynamicPrintConfig &out) void import_sla_archive( const std::string & zipfname, Vec2i windowsize, - TriangleMesh & out, + indexed_triangle_set & out, DynamicPrintConfig & profile, std::function progr) { @@ -316,7 +316,7 @@ void import_sla_archive( extract_slices_from_sla_archive(arch, rstp, progr); if (!slices.empty()) - out = slices_to_triangle_mesh(slices, 0, slicp.layerh, slicp.initial_layerh); + out = slices_to_mesh(slices, 0, slicp.layerh, slicp.initial_layerh); } using ConfMap = std::map; diff --git a/src/libslic3r/Format/SL1.hpp b/src/libslic3r/Format/SL1.hpp index ab731ff84..a3e6ce26c 100644 --- a/src/libslic3r/Format/SL1.hpp +++ b/src/libslic3r/Format/SL1.hpp @@ -14,7 +14,7 @@ class SL1Archive: public SLAPrinter { protected: uqptr create_raster() const override; sla::RasterEncoder get_encoder() const override; - + public: SL1Archive() = default; @@ -43,14 +43,14 @@ void import_sla_archive(const std::string &zipfname, DynamicPrintConfig &out); void import_sla_archive( const std::string & zipfname, Vec2i windowsize, - TriangleMesh & out, + indexed_triangle_set & out, DynamicPrintConfig & profile, std::function progr = [](int) { return true; }); inline void import_sla_archive( const std::string & zipfname, Vec2i windowsize, - TriangleMesh & out, + indexed_triangle_set & out, std::function progr = [](int) { return true; }) { DynamicPrintConfig profile; diff --git a/src/libslic3r/OpenVDBUtils.hpp b/src/libslic3r/OpenVDBUtils.hpp index c101ddc11..490b3e222 100644 --- a/src/libslic3r/OpenVDBUtils.hpp +++ b/src/libslic3r/OpenVDBUtils.hpp @@ -2,7 +2,6 @@ #define OPENVDBUTILS_HPP #include -#include #ifdef _MSC_VER // Suppress warning C4146 in include/gmp.h(2177,31): unary minus operator applied to unsigned type, result still unsigned @@ -19,7 +18,6 @@ namespace Slic3r { inline Vec3f to_vec3f(const openvdb::Vec3s &v) { return Vec3f{v.x(), v.y(), v.z()}; } inline Vec3d to_vec3d(const openvdb::Vec3s &v) { return to_vec3f(v).cast(); } inline Vec3i to_vec3i(const openvdb::Vec3I &v) { return Vec3i{int(v[0]), int(v[1]), int(v[2])}; } -inline Vec4i to_vec4i(const openvdb::Vec4I &v) { return Vec4i{int(v[0]), int(v[1]), int(v[2]), int(v[3])}; } // Here voxel_scale defines the scaling of voxels which affects the voxel count. // 1.0 value means a voxel for every unit cube. 2 means the model is scaled to diff --git a/src/libslic3r/SLA/Contour3D.cpp b/src/libslic3r/SLA/Contour3D.cpp deleted file mode 100644 index 96d10af20..000000000 --- a/src/libslic3r/SLA/Contour3D.cpp +++ /dev/null @@ -1,149 +0,0 @@ -#include -#include - -#include - -namespace Slic3r { namespace sla { - -Contour3D::Contour3D(const TriangleMesh &trmesh) -{ - points.reserve(trmesh.its.vertices.size()); - faces3.reserve(trmesh.its.indices.size()); - - for (auto &v : trmesh.its.vertices) - points.emplace_back(v.cast()); - - std::copy(trmesh.its.indices.begin(), trmesh.its.indices.end(), - std::back_inserter(faces3)); -} - -Contour3D::Contour3D(TriangleMesh &&trmesh) -{ - points.reserve(trmesh.its.vertices.size()); - - for (auto &v : trmesh.its.vertices) - points.emplace_back(v.cast()); - - faces3.swap(trmesh.its.indices); -} - -Contour3D::Contour3D(const IndexedMesh &emesh) { - points.reserve(emesh.vertices().size()); - faces3.reserve(emesh.indices().size()); - - for (const Vec3f& vert : emesh.vertices()) - points.emplace_back(vert.cast()); - - for (const auto& ind : emesh.indices()) - faces3.emplace_back(ind); -} - -Contour3D &Contour3D::merge(const Contour3D &ctr) -{ - auto N = coord_t(points.size()); - auto N_f3 = faces3.size(); - auto N_f4 = faces4.size(); - - points.insert(points.end(), ctr.points.begin(), ctr.points.end()); - faces3.insert(faces3.end(), ctr.faces3.begin(), ctr.faces3.end()); - faces4.insert(faces4.end(), ctr.faces4.begin(), ctr.faces4.end()); - - for(size_t n = N_f3; n < faces3.size(); n++) { - auto& idx = faces3[n]; idx.x() += N; idx.y() += N; idx.z() += N; - } - - for(size_t n = N_f4; n < faces4.size(); n++) { - auto& idx = faces4[n]; for (int k = 0; k < 4; k++) idx(k) += N; - } - - return *this; -} - -Contour3D &Contour3D::merge(const Pointf3s &triangles) -{ - const size_t offs = points.size(); - points.insert(points.end(), triangles.begin(), triangles.end()); - faces3.reserve(faces3.size() + points.size() / 3); - - for(int i = int(offs); i < int(points.size()); i += 3) - faces3.emplace_back(i, i + 1, i + 2); - - return *this; -} - -void Contour3D::to_obj(std::ostream &stream) -{ - for(auto& p : points) - stream << "v " << p.transpose() << "\n"; - - for(auto& f : faces3) - stream << "f " << (f + Vec3i(1, 1, 1)).transpose() << "\n"; - - for(auto& f : faces4) - stream << "f " << (f + Vec4i(1, 1, 1, 1)).transpose() << "\n"; -} - -void Contour3D::from_obj(std::istream &stream) -{ - ObjParser::ObjData data; - ObjParser::objparse(stream, data); - - points.reserve(data.coordinates.size() / 4 + 1); - auto &coords = data.coordinates; - for (size_t i = 0; i < coords.size(); i += 4) - points.emplace_back(coords[i], coords[i + 1], coords[i + 2]); - - Vec3i triangle; - Vec4i quad; - size_t v = 0; - while(v < data.vertices.size()) { - size_t N = 0; - size_t i = v; - while (data.vertices[v++].coordIdx != -1) ++N; - - std::function setfn; - if (N < 3 || N > 4) continue; - else if (N == 3) setfn = [&triangle](int k, int f) { triangle(k) = f; }; - else setfn = [&quad](int k, int f) { quad(k) = f; }; - - for (size_t j = 0; j < N; ++j) - setfn(int(j), data.vertices[i + j].coordIdx); - } -} - -TriangleMesh to_triangle_mesh(const Contour3D &ctour) { - if (ctour.faces4.empty()) return {ctour.points, ctour.faces3}; - - std::vector triangles; - - triangles.reserve(ctour.faces3.size() + 2 * ctour.faces4.size()); - std::copy(ctour.faces3.begin(), ctour.faces3.end(), - std::back_inserter(triangles)); - - for (auto &quad : ctour.faces4) { - triangles.emplace_back(quad(0), quad(1), quad(2)); - triangles.emplace_back(quad(2), quad(3), quad(0)); - } - - return {ctour.points, std::move(triangles)}; -} - -TriangleMesh to_triangle_mesh(Contour3D &&ctour) { - if (ctour.faces4.empty()) - return {std::move(ctour.points), std::move(ctour.faces3)}; - - std::vector triangles; - - triangles.reserve(ctour.faces3.size() + 2 * ctour.faces4.size()); - std::copy(ctour.faces3.begin(), ctour.faces3.end(), - std::back_inserter(triangles)); - - for (auto &quad : ctour.faces4) { - triangles.emplace_back(quad(0), quad(1), quad(2)); - triangles.emplace_back(quad(2), quad(3), quad(0)); - } - - return {std::move(ctour.points), std::move(triangles)}; -} - -}} // namespace Slic3r::sla diff --git a/src/libslic3r/SLA/Contour3D.hpp b/src/libslic3r/SLA/Contour3D.hpp deleted file mode 100644 index 3380cd6ab..000000000 --- a/src/libslic3r/SLA/Contour3D.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef SLA_CONTOUR3D_HPP -#define SLA_CONTOUR3D_HPP - -#include - -namespace Slic3r { - -// Used for quads (TODO: remove this, and convert quads to triangles in OpenVDBUtils) -using Vec4i = Eigen::Matrix; - -namespace sla { - -class IndexedMesh; - -/// Dumb vertex mesh consisting of triangles (or) quads. Capable of merging with -/// other meshes of this type and converting to and from other mesh formats. -struct Contour3D { - std::vector points; - std::vector faces3; - std::vector faces4; - - Contour3D() = default; - Contour3D(const TriangleMesh &trmesh); - Contour3D(TriangleMesh &&trmesh); - Contour3D(const IndexedMesh &emesh); - - Contour3D& merge(const Contour3D& ctr); - Contour3D& merge(const Pointf3s& triangles); - - // Write the index triangle structure to OBJ file for debugging purposes. - void to_obj(std::ostream& stream); - void from_obj(std::istream &stream); - - inline bool empty() const - { - return points.empty() || (faces4.empty() && faces3.empty()); - } -}; - -/// Mesh from an existing contour. -TriangleMesh to_triangle_mesh(const Contour3D& ctour); - -/// Mesh from an evaporating 3D contour -TriangleMesh to_triangle_mesh(Contour3D&& ctour); - -}} // namespace Slic3r::sla - -#endif // CONTOUR3D_HPP diff --git a/src/libslic3r/SLA/Hollowing.cpp b/src/libslic3r/SLA/Hollowing.cpp index 03d2fc721..770e52a35 100644 --- a/src/libslic3r/SLA/Hollowing.cpp +++ b/src/libslic3r/SLA/Hollowing.cpp @@ -22,12 +22,6 @@ namespace Slic3r { namespace sla { -template> -inline void _scale(S s, TriangleMesh &m) { m.scale(float(s)); } - -template> -inline void _scale(S s, Contour3D &m) { for (auto &p : m.points) p *= s; } - struct Interior { indexed_triangle_set mesh; openvdb::FloatGrid::Ptr gridptr; diff --git a/src/libslic3r/SLA/Hollowing.hpp b/src/libslic3r/SLA/Hollowing.hpp index 3516509dd..b57513fe7 100644 --- a/src/libslic3r/SLA/Hollowing.hpp +++ b/src/libslic3r/SLA/Hollowing.hpp @@ -2,13 +2,11 @@ #define SLA_HOLLOWING_HPP #include -#include +#include #include namespace Slic3r { -class TriangleMesh; - namespace sla { struct HollowingConfig diff --git a/src/libslic3r/SlicesToTriangleMesh.cpp b/src/libslic3r/SlicesToTriangleMesh.cpp index d6a546961..7a2975d12 100644 --- a/src/libslic3r/SlicesToTriangleMesh.cpp +++ b/src/libslic3r/SlicesToTriangleMesh.cpp @@ -1,8 +1,8 @@ #include "SlicesToTriangleMesh.hpp" -#include "libslic3r/MTUtils.hpp" -#include "libslic3r/SLA/Contour3D.hpp" +//#include "libslic3r/MTUtils.hpp" +#include "libslic3r/Execution/ExecutionTBB.hpp" #include "libslic3r/ClipperUtils.hpp" #include "libslic3r/Tesselate.hpp" @@ -11,71 +11,73 @@ namespace Slic3r { -inline sla::Contour3D wall_strip(const Polygon &poly, - double lower_z_mm, - double upper_z_mm) +inline indexed_triangle_set wall_strip(const Polygon &poly, + double lower_z_mm, + double upper_z_mm) { - sla::Contour3D ret; + indexed_triangle_set ret; - size_t startidx = ret.points.size(); + size_t startidx = ret.vertices.size(); size_t offs = poly.points.size(); - ret.points.reserve(ret.points.size() + 2 *offs); + ret.vertices.reserve(ret.vertices.size() + 2 *offs); for (const Point &p : poly.points) - ret.points.emplace_back(to_3d(unscaled(p), lower_z_mm)); + ret.vertices.emplace_back(to_3d(unscaled(p), float(lower_z_mm))); for (const Point &p : poly.points) - ret.points.emplace_back(to_3d(unscaled(p), upper_z_mm)); + ret.vertices.emplace_back(to_3d(unscaled(p), float(upper_z_mm))); for (size_t i = startidx + 1; i < startidx + offs; ++i) { - ret.faces3.emplace_back(i - 1, i, i + offs - 1); - ret.faces3.emplace_back(i, i + offs, i + offs - 1); + ret.indices.emplace_back(i - 1, i, i + offs - 1); + ret.indices.emplace_back(i, i + offs, i + offs - 1); } - ret.faces3.emplace_back(startidx + offs - 1, startidx, startidx + 2 * offs - 1); - ret.faces3.emplace_back(startidx, startidx + offs, startidx + 2 * offs - 1); + ret.indices.emplace_back(startidx + offs - 1, startidx, startidx + 2 * offs - 1); + ret.indices.emplace_back(startidx, startidx + offs, startidx + 2 * offs - 1); return ret; } // Same as walls() but with identical higher and lower polygons. -sla::Contour3D inline straight_walls(const Polygon &plate, +indexed_triangle_set inline straight_walls(const Polygon &plate, double lo_z, double hi_z) { return wall_strip(plate, lo_z, hi_z); } -sla::Contour3D inline straight_walls(const ExPolygon &plate, +indexed_triangle_set inline straight_walls(const ExPolygon &plate, double lo_z, double hi_z) { - sla::Contour3D ret; - ret.merge(straight_walls(plate.contour, lo_z, hi_z)); - for (auto &h : plate.holes) ret.merge(straight_walls(h, lo_z, hi_z)); + indexed_triangle_set ret = straight_walls(plate.contour, lo_z, hi_z); + for (auto &h : plate.holes) + its_merge(ret, straight_walls(h, lo_z, hi_z)); + return ret; } -sla::Contour3D inline straight_walls(const ExPolygons &slice, +indexed_triangle_set inline straight_walls(const ExPolygons &slice, double lo_z, double hi_z) -{ - sla::Contour3D ret; +{ + indexed_triangle_set ret; for (const ExPolygon &poly : slice) - ret.merge(straight_walls(poly, lo_z, hi_z)); - + its_merge(ret, straight_walls(poly, lo_z, hi_z)); + return ret; } -sla::Contour3D slices_to_triangle_mesh(const std::vector &slices, - double zmin, - const std::vector & grid) +indexed_triangle_set slices_to_mesh( + const std::vector &slices, + double zmin, + const std::vector & grid) { assert(slices.size() == grid.size()); - using Layers = std::vector; - std::vector layers(slices.size()); + using Layers = std::vector; + Layers layers(slices.size()); size_t len = slices.size() - 1; tbb::parallel_for(size_t(0), len, [&slices, &layers, &grid](size_t i) { @@ -84,45 +86,52 @@ sla::Contour3D slices_to_triangle_mesh(const std::vector &slices, ExPolygons dff1 = diff_ex(lower, upper); ExPolygons dff2 = diff_ex(upper, lower); - layers[i].merge(triangulate_expolygons_3d(dff1, grid[i], NORMALS_UP)); - layers[i].merge(triangulate_expolygons_3d(dff2, grid[i], NORMALS_DOWN)); - layers[i].merge(straight_walls(upper, grid[i], grid[i + 1])); + its_merge(layers[i], triangulate_expolygons_3d(dff1, grid[i], NORMALS_UP)); + its_merge(layers[i], triangulate_expolygons_3d(dff2, grid[i], NORMALS_DOWN)); + its_merge(layers[i], straight_walls(upper, grid[i], grid[i + 1])); }); + + auto merge_fn = []( const indexed_triangle_set &a, const indexed_triangle_set &b ) { + indexed_triangle_set res{a}; its_merge(res, b); return res; + }; + + auto ret = execution::reduce(ex_tbb, layers.begin(), layers.end(), + indexed_triangle_set{}, merge_fn); + + // sla::Contour3D ret = tbb::parallel_reduce( + // tbb::blocked_range(layers.begin(), layers.end()), + // sla::Contour3D{}, + // [](const tbb::blocked_range& r, sla::Contour3D + // init) { + // for(auto it = r.begin(); it != r.end(); ++it ) + // init.merge(*it); return init; + // }, + // []( const sla::Contour3D &a, const sla::Contour3D &b ) { + // sla::Contour3D res{a}; res.merge(b); return res; + // }); - sla::Contour3D ret = tbb::parallel_reduce( - tbb::blocked_range(layers.begin(), layers.end()), - sla::Contour3D{}, - [](const tbb::blocked_range& r, sla::Contour3D init) { - for(auto it = r.begin(); it != r.end(); ++it ) init.merge(*it); - return init; - }, - []( const sla::Contour3D &a, const sla::Contour3D &b ) { - sla::Contour3D res{a}; res.merge(b); return res; - }); - - ret.merge(triangulate_expolygons_3d(slices.front(), zmin, NORMALS_DOWN)); - ret.merge(straight_walls(slices.front(), zmin, grid.front())); - ret.merge(triangulate_expolygons_3d(slices.back(), grid.back(), NORMALS_UP)); + its_merge(ret, triangulate_expolygons_3d(slices.front(), zmin, NORMALS_DOWN)); + its_merge(ret, straight_walls(slices.front(), zmin, grid.front())); + its_merge(ret, triangulate_expolygons_3d(slices.back(), grid.back(), NORMALS_UP)); return ret; } -void slices_to_triangle_mesh(TriangleMesh & mesh, +void slices_to_mesh(indexed_triangle_set & mesh, const std::vector &slices, double zmin, double lh, double ilh) { - std::vector wall_meshes(slices.size()); + std::vector wall_meshes(slices.size()); std::vector grid(slices.size(), zmin + ilh); - for (size_t i = 1; i < grid.size(); ++i) grid[i] = grid[i - 1] + lh; + for (size_t i = 1; i < grid.size(); ++i) + grid[i] = grid[i - 1] + lh; - sla::Contour3D cntr = slices_to_triangle_mesh(slices, zmin, grid); - mesh.merge(sla::to_triangle_mesh(cntr)); - mesh.repaired = true; - mesh.require_shared_vertices(); + indexed_triangle_set cntr = slices_to_mesh(slices, zmin, grid); + its_merge(mesh, cntr); } } // namespace Slic3r diff --git a/src/libslic3r/SlicesToTriangleMesh.hpp b/src/libslic3r/SlicesToTriangleMesh.hpp index 133312d56..2fd177885 100644 --- a/src/libslic3r/SlicesToTriangleMesh.hpp +++ b/src/libslic3r/SlicesToTriangleMesh.hpp @@ -6,16 +6,18 @@ namespace Slic3r { -void slices_to_triangle_mesh(TriangleMesh & mesh, +void slices_to_mesh(indexed_triangle_set & mesh, const std::vector &slices, double zmin, double lh, double ilh); -inline TriangleMesh slices_to_triangle_mesh( +inline indexed_triangle_set slices_to_mesh( const std::vector &slices, double zmin, double lh, double ilh) { - TriangleMesh out; slices_to_triangle_mesh(out, slices, zmin, lh, ilh); + indexed_triangle_set out; + slices_to_mesh(out, slices, zmin, lh, ilh); + return out; } diff --git a/src/slic3r/GUI/Jobs/SLAImportJob.cpp b/src/slic3r/GUI/Jobs/SLAImportJob.cpp index 3da937f0a..9998f42c7 100644 --- a/src/slic3r/GUI/Jobs/SLAImportJob.cpp +++ b/src/slic3r/GUI/Jobs/SLAImportJob.cpp @@ -113,14 +113,14 @@ public: Plater *plater; Sel sel = Sel::modelAndProfile; - - TriangleMesh mesh; - DynamicPrintConfig profile; - wxString path; - Vec2i win = {2, 2}; - std::string err; - - priv(Plater *plt): plater{plt} {} + + indexed_triangle_set mesh; + DynamicPrintConfig profile; + wxString path; + Vec2i win = {2, 2}; + std::string err; + + priv(Plater *plt) : plater{plt} {} }; SLAImportJob::SLAImportJob(std::shared_ptr pri, Plater *plater) @@ -222,7 +222,8 @@ void SLAImportJob::finalize() if (!p->mesh.empty()) { bool is_centered = false; - p->plater->sidebar().obj_list()->load_mesh_object(p->mesh, name, is_centered); + p->plater->sidebar().obj_list()->load_mesh_object(TriangleMesh{p->mesh}, + name, is_centered); } reset(); diff --git a/tests/libslic3r/test_marchingsquares.cpp b/tests/libslic3r/test_marchingsquares.cpp index 14481f14a..a0b27f6d8 100644 --- a/tests/libslic3r/test_marchingsquares.cpp +++ b/tests/libslic3r/test_marchingsquares.cpp @@ -17,7 +17,6 @@ #include #include #include -#include using namespace Slic3r; @@ -368,10 +367,9 @@ static void recreate_object_from_rasters(const std::string &objname, float lh) { layer = std::move(layer_); } - TriangleMesh out = slices_to_triangle_mesh(layers, bb.min.z(), double(lh), double(lh)); - - out.require_shared_vertices(); - out.WriteOBJFile("out_from_rasters.obj"); + indexed_triangle_set out = slices_to_mesh(layers, bb.min.z(), double(lh), double(lh)); + + its_write_obj(out, "out_from_rasters.obj"); } TEST_CASE("Recreate object from rasters", "[SL1Import]") { diff --git a/tests/sla_print/sla_print_tests.cpp b/tests/sla_print/sla_print_tests.cpp index 9433e2d25..d26d6e2a2 100644 --- a/tests/sla_print/sla_print_tests.cpp +++ b/tests/sla_print/sla_print_tests.cpp @@ -223,15 +223,6 @@ TEST_CASE("RasterizedPolygonAreaShouldMatch", "[SLARasterOutput]") { REQUIRE(raster_pxsum(raster0) == 0); } -TEST_CASE("Triangle mesh conversions should be correct", "[SLAConversions]") -{ - sla::Contour3D cntr; - - { - std::fstream infile{"extruder_idler_quads.obj", std::ios::in}; - cntr.from_obj(infile); - } -} TEST_CASE("halfcone test", "[halfcone]") { sla::DiffBridge br{Vec3d{1., 1., 1.}, Vec3d{10., 10., 10.}, 0.25, 0.5};