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()) { for (const VertexHandle &vh : mesh.vertices()) {
mesh.set_color(vh, MyMesh::Color(0, 0, 0)); 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}); setBorderColor(mesh, border, {1, 0, 0});
fillHoleDumb(mesh, border);
} }
if (glm != nullptr) mesh_viewer.removeOpenGLMesh(glm);
glm = mesh_viewer.addOpenGLMeshFromOpenMesh(&mesh); glm = mesh_viewer.addOpenGLMeshFromOpenMesh(&mesh);
for (QAction *a : toolbar_actions) { for (QAction *a : toolbar_actions) {
a->setEnabled(true); a->setEnabled(true);

View File

@ -1,30 +1,54 @@
#include "mesh_processing.h" #include "mesh_processing.h"
#include <list> #include <QGenericMatrix>
#include <unordered_set>
std::vector<HalfedgeStrip> findBorders(MyMesh &mesh) { std::vector<HalfedgeHandle> findBorders(MyMesh &mesh) {
std::vector<HalfedgeStrip> borders(1); std::vector<HalfedgeHandle> borders;
size_t current = 0; std::set<HalfedgeHandle> ignore;
for (auto it = mesh.halfedges_begin(); it != mesh.halfedges_end(); ++it) { for (HalfedgeHandle it : mesh.halfedges()) {
while (mesh.is_boundary(*it)) { if (mesh.is_boundary(it)
borders[current].push_back(*it); && ignore.find(it) == ignore.end()) {
++it; borders.push_back(it);
} ignore.insert(it);
if (borders[current].size() > 0) { for (HalfedgeHandle it2 : HalfedgeLoopRange(mesh, it)) {
borders.emplace_back(); ignore.insert(it2);
current++;
} }
} }
if (borders.back().size() == 0) {
borders.pop_back();
} }
return borders; return borders;
} }
void setBorderColor(MyMesh &mesh, const HalfedgeStrip &border, void setBorderColor(MyMesh &mesh, const HalfedgeHandle &border,
MyMesh::Color color) { MyMesh::Color color) {
for (MyMesh::HalfedgeHandle it : border) { for (HalfedgeHandle it : HalfedgeLoopRange(mesh, border)) {
mesh.set_color(mesh.to_vertex_handle(it), color); 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> #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 HalfedgeHandle &border,
void setBorderColor(MyMesh &mesh, const HalfedgeStrip &border,
MyMesh::Color color); MyMesh::Color color);
void fillHoleDumb(MyMesh &mesh, HalfedgeHandle &border);
#endif #endif