From 810ddaee0387b976e9a861e90c7dfe2ce06f8a95 Mon Sep 17 00:00:00 2001 From: papush! Date: Wed, 5 Jan 2022 15:54:46 +0100 Subject: [PATCH] add a method to find connected components --- src/util.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 4 ++++ 2 files changed, 48 insertions(+) diff --git a/src/util.cpp b/src/util.cpp index 1600f9f..5757a99 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,5 +1,8 @@ #include "util.h" +#include +#include + QDebug operator<<(QDebug dbg, const Point &p) { return dbg << p[0] << p[1] << p[2]; @@ -9,3 +12,44 @@ QDebug operator<<(QDebug dbg, const Point &p) { qreal cotan(const qreal x) { return 1. / qTan(x); } + + +static void traverse_connected(MyMesh &mesh, std::vector &out, + VertexHandle start) { + auto prop = + OpenMesh::makeTemporaryProperty(mesh); + for (VertexHandle vh : mesh.vertices()) { + prop[vh] = false; + } + std::stack stack; + stack.push(start); + prop[start] = true; + while (!stack.empty()) { + VertexHandle v = stack.top(); + stack.pop(); + out.emplace_back(v); + for (VertexHandle v2 : mesh.vv_range(v)) { + if (!prop[v2]) { + stack.push(v2); + prop[v2] = true; + } + } + } +} + + +std::vector> find_connected_components( + MyMesh &mesh) { + auto prop = + OpenMesh::makeTemporaryProperty(mesh); + for (VertexHandle vh : mesh.vertices()) { + prop[vh] = false; + } + std::vector> connected_components; + for (VertexHandle vh : mesh.vertices()) { + if (prop[vh]) continue; + connected_components.push_back({}); + traverse_connected(mesh, connected_components.back(), vh); + } + return connected_components; +} diff --git a/src/util.h b/src/util.h index 10985a2..8fc01e7 100644 --- a/src/util.h +++ b/src/util.h @@ -101,4 +101,8 @@ standard_deviation(const ForwardIt first, } +std::vector> find_connected_components( + MyMesh &mesh); + + #endif