add a method to find connected components
This commit is contained in:
parent
6cb0dbc21e
commit
810ddaee03
44
src/util.cpp
44
src/util.cpp
@ -1,5 +1,8 @@
|
||||
#include "util.h"
|
||||
|
||||
#include <stack>
|
||||
#include <OpenMesh/Core/Utils/PropertyManager.hh>
|
||||
|
||||
|
||||
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<VertexHandle> &out,
|
||||
VertexHandle start) {
|
||||
auto prop =
|
||||
OpenMesh::makeTemporaryProperty<MyMesh::VertexHandle, bool>(mesh);
|
||||
for (VertexHandle vh : mesh.vertices()) {
|
||||
prop[vh] = false;
|
||||
}
|
||||
std::stack<VertexHandle> 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<std::vector<VertexHandle>> find_connected_components(
|
||||
MyMesh &mesh) {
|
||||
auto prop =
|
||||
OpenMesh::makeTemporaryProperty<MyMesh::VertexHandle, bool>(mesh);
|
||||
for (VertexHandle vh : mesh.vertices()) {
|
||||
prop[vh] = false;
|
||||
}
|
||||
std::vector<std::vector<VertexHandle>> 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;
|
||||
}
|
||||
|
@ -101,4 +101,8 @@ standard_deviation(const ForwardIt first,
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::vector<VertexHandle>> find_connected_components(
|
||||
MyMesh &mesh);
|
||||
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user