fix hole detection and dumb filling

This commit is contained in:
papush! 2021-10-02 12:03:46 +02:00
parent fb86669559
commit 63074306c2
3 changed files with 51 additions and 23 deletions

View File

@ -43,10 +43,13 @@ void MainWindow::open(const QString &path) {
for (const VertexHandle &vh : mesh.vertices()) {
mesh.set_color(vh, MyMesh::Color(0, 0, 0));
}
if (glm != nullptr) mesh_viewer.removeOpenGLMesh(glm);
for (HalfedgeStrip border : findBorders(mesh)) {
for (HalfedgeHandle border : findBorders(mesh)) {
setBorderColor(mesh, border, {1, 0, 0});
fillHoleDumb(mesh, border);
}
if (glm != nullptr) mesh_viewer.removeOpenGLMesh(glm);
glm = mesh_viewer.addOpenGLMeshFromOpenMesh(&mesh);
for (QAction *a : toolbar_actions) {
a->setEnabled(true);

View File

@ -1,30 +1,54 @@
#include "mesh_processing.h"
#include <list>
#include <QGenericMatrix>
#include <unordered_set>
std::vector<HalfedgeStrip> findBorders(MyMesh &mesh) {
std::vector<HalfedgeStrip> borders(1);
size_t current = 0;
for (auto it = mesh.halfedges_begin(); it != mesh.halfedges_end(); ++it) {
while (mesh.is_boundary(*it)) {
borders[current].push_back(*it);
++it;
std::vector<HalfedgeHandle> findBorders(MyMesh &mesh) {
std::vector<HalfedgeHandle> borders;
std::set<HalfedgeHandle> ignore;
for (HalfedgeHandle it : mesh.halfedges()) {
if (mesh.is_boundary(it)
&& ignore.find(it) == ignore.end()) {
borders.push_back(it);
ignore.insert(it);
for (HalfedgeHandle it2 : HalfedgeLoopRange(mesh, it)) {
ignore.insert(it2);
}
}
if (borders[current].size() > 0) {
borders.emplace_back();
current++;
}
}
if (borders.back().size() == 0) {
borders.pop_back();
}
return borders;
}
void setBorderColor(MyMesh &mesh, const HalfedgeStrip &border,
void setBorderColor(MyMesh &mesh, const HalfedgeHandle &border,
MyMesh::Color color) {
for (MyMesh::HalfedgeHandle it : border) {
for (HalfedgeHandle it : HalfedgeLoopRange(mesh, border)) {
mesh.set_color(mesh.to_vertex_handle(it), color);
}
}
void fillHoleDumb(MyMesh &mesh, HalfedgeHandle &border) {
mesh.request_vertex_status();
mesh.request_edge_status();
mesh.request_face_status();
Point center(0, 0, 0);
size_t length = 0;
for (HalfedgeHandle it : HalfedgeLoopRange(mesh, border)) {
VertexHandle vert = mesh.to_vertex_handle(it);
center += mesh.point(vert);
length++;
}
center /= length;
VertexHandle center_handle = mesh.new_vertex_dirty(center);
for (HalfedgeHandle it = border;
mesh.to_vertex_handle(it) != center_handle;) {
HalfedgeHandle next = mesh.next_halfedge_handle(it);
mesh.add_face(mesh.from_vertex_handle(it),
mesh.to_vertex_handle(it),
center_handle);
it = next;
}
}

View File

@ -6,11 +6,12 @@
#include <vector>
typedef std::vector<MyMesh::HalfedgeHandle> HalfedgeStrip;
std::vector<HalfedgeHandle> findBorders(MyMesh &mesh);
std::vector<HalfedgeStrip> findBorders(MyMesh &mesh);
void setBorderColor(MyMesh &mesh, const HalfedgeStrip &border,
void setBorderColor(MyMesh &mesh, const HalfedgeHandle &border,
MyMesh::Color color);
void fillHoleDumb(MyMesh &mesh, HalfedgeHandle &border);
#endif