Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add fast path for extracting meshes w/o simplification #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion cMesher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <zi/mesh/marching_cubes.hpp>
#include <zi/mesh/int_mesh.hpp>
#include <zi/mesh/quadratic_simplifier.hpp>
#include <zi/bits/unordered_map.hpp>
#include <zi/vl/vec.hpp>

struct MeshObject {
Expand Down Expand Up @@ -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,
Expand All @@ -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<PositionType, uint32_t> 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<uint32_t, PositionType> 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<PositionType, LabelType>::template unpack_z<SimplifierType>(vert)
);
obj.points.push_back(
zi::mesh::marching_cubes<PositionType, LabelType>::template unpack_y<SimplifierType>(vert)
);
obj.points.push_back(
zi::mesh::marching_cubes<PositionType, LabelType>::template unpack_x<SimplifierType>(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,
Expand Down