Compare commits

...

2 Commits

Author SHA1 Message Date
63074306c2 fix hole detection and dumb filling 2021-10-02 12:03:46 +02:00
fb86669559 add some sugar 2021-10-02 12:03:23 +02:00
4 changed files with 72 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

View File

@ -14,6 +14,27 @@ struct MyTraits : public OpenMesh::DefaultTraits {
typedef OpenMesh::Vec3f Color; typedef OpenMesh::Vec3f Color;
}; };
typedef OpenMesh::PolyMesh_ArrayKernelT<MyTraits> MyMesh; typedef OpenMesh::PolyMesh_ArrayKernelT<MyTraits> MyMesh;
typedef MyMesh::FaceHandle FaceHandle;
typedef MyMesh::VertexHandle VertexHandle;
typedef MyMesh::HalfedgeHandle HalfedgeHandle;
typedef MyMesh::EdgeHandle EdgeHandle;
typedef MyMesh::Point Point;
class HalfedgeLoopRange {
MyMesh &mesh;
const HalfedgeHandle &start;
public:
HalfedgeLoopRange(MyMesh &mesh, const HalfedgeHandle &start)
:mesh(mesh), start(start) {}
MyMesh::HalfedgeLoopIter begin() {
return mesh.hl_begin(start);
}
MyMesh::HalfedgeLoopIter end() {
return mesh.hl_end(start);
}
};
#endif #endif