diff --git a/cMesher.hpp b/cMesher.hpp index ad3d66a..cdac22f 100755 --- a/cMesher.hpp +++ b/cMesher.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include struct MeshObject { @@ -62,7 +63,12 @@ class CMesher { return empty_obj; } - std::vector< zi::vl::vec< PositionType, 3> > triangles = marchingcubes_.get_triangles(segid); + std::vector< zi::vl::vec< PositionType, 3> > triangles = std::move(marchingcubes_.get_triangles(segid)); + + // faster + if (simplification_factor == 0 && !generate_normals) { + return trianges2mesh(triangles); + } return simplify( triangles, @@ -72,6 +78,61 @@ class CMesher { ); } + // This is only for converting triangles + // into a vertex and face triangle soup object + // with no simplification or normal calculation. + MeshObject trianges2mesh( + const std::vector< zi::vl::vec< PositionType, 3> >& triangles + ) { + MeshObject obj; + + uint32_t idx = 0; + zi::unordered_map pts; + pts.reserve(3 * triangles.size()); + + + for (auto &tri : triangles) { + if (!pts.count(tri.at(0))) { + pts.insert(std::make_pair(tri.at(0), idx++)); + } + if (!pts.count(tri.at(1))) { + pts.insert(std::make_pair(tri.at(1), idx++)); + } + if (!pts.count(tri.at(2))) { + pts.insert(std::make_pair(tri.at(2), idx++)); + } + } + + zi::unordered_map ipts; + for (auto it : pts) { + ipts[it.second] = it.first; + } + + obj.points.reserve(pts.size()); + obj.faces.reserve(3 * triangles.size()); + + for (uint32_t i = 0; i < idx; i++) { + PositionType vert = ipts[i]; + obj.points.push_back( + zi::mesh::marching_cubes::template unpack_z(vert) + ); + obj.points.push_back( + zi::mesh::marching_cubes::template unpack_y(vert) + ); + obj.points.push_back( + zi::mesh::marching_cubes::template unpack_x(vert) + ); + } + + for (auto &tri : triangles) { + obj.faces.push_back(pts[tri.at(0)]); + obj.faces.push_back(pts[tri.at(2)]); + obj.faces.push_back(pts[tri.at(1)]); + } + + return obj; + } + MeshObject simplify( const std::vector< zi::vl::vec< PositionType, 3> >& triangles, bool generate_normals,