From 09aebfb9f823774ca855e137232aecbcfde86b21 Mon Sep 17 00:00:00 2001 From: huxingyi Date: Wed, 15 Apr 2020 07:27:01 +0930 Subject: [PATCH] Optmize hole fixer --- src/fixholes.cpp | 59 +++++++++++++++++++++++++++++++--------- src/triangulatefaces.cpp | 2 +- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/fixholes.cpp b/src/fixholes.cpp index be7db33d..9b2d882f 100644 --- a/src/fixholes.cpp +++ b/src/fixholes.cpp @@ -2,29 +2,49 @@ #include #include #include +#include +#include #include "fixholes.h" #include "util.h" -static void buildEdgeToFaceMap(const std::vector> &faces, std::map, size_t> &edgeToFaceMap) +void fixHoles(const std::vector &verticies, std::vector> &faces) { - edgeToFaceMap.clear(); + std::map, std::unordered_set> edgeToFaceSetMap; + for (decltype(faces.size()) index = 0; index < faces.size(); ++index) { const auto &face = faces[index]; for (size_t i = 0; i < face.size(); i++) { size_t j = (i + 1) % face.size(); - edgeToFaceMap[{face[i], face[j]}] = index; + edgeToFaceSetMap[{face[i], face[j]}].insert(index); } } -} - -void fixHoles(const std::vector &verticies, std::vector> &faces) -{ + + std::unordered_set invalidFaces; + std::map, size_t> edgeToFaceMap; - buildEdgeToFaceMap(faces, edgeToFaceMap); + for (const auto &it: edgeToFaceSetMap) { + if (1 == it.second.size()) { + continue; + } + for (const auto &loopFaceIndex: it.second) { + invalidFaces.insert(loopFaceIndex); + } + } + for (const auto &it: edgeToFaceSetMap) { + if (1 == it.second.size()) { + size_t index = *it.second.begin(); + if (invalidFaces.find(index) == invalidFaces.end()) { + edgeToFaceMap.insert({it.first, index}); + } + } + } std::map> holeVertexLink; std::vector startVertices; - for (const auto &face: faces) { + for (size_t index = 0; index < faces.size(); ++index) { + if (invalidFaces.find(index) != invalidFaces.end()) + continue; + const auto &face = faces[index]; for (size_t i = 0; i < face.size(); i++) { size_t j = (i + 1) % face.size(); auto findOppositeFaceResult = edgeToFaceMap.find({face[j], face[i]}); @@ -130,14 +150,27 @@ void fixHoles(const std::vector &verticies, std::vector> addedFaces; + //std::vector> removedFaces; + std::vector> fixedFaces; + for (size_t i = 0; i < faces.size(); ++i) { + if (invalidFaces.find(i) != invalidFaces.end()) { + //removedFaces.push_back(faces[i]); + continue; + } + fixedFaces.push_back(faces[i]); + } + //saveAsObj("fixholes_output_without_newfaces.obj", verticies, fixedFaces); for (const auto &it: newFaces) { - faces.push_back(std::vector {it.indices[0], it.indices[1], it.indices[2]}); + fixedFaces.push_back(std::vector {it.indices[0], it.indices[1], it.indices[2]}); //addedFaces.push_back(std::vector {it.indices[0], it.indices[1], it.indices[2]}); } - //saveAsObj("fixholes_output.obj", verticies, faces); + //saveAsObj("fixholes_output.obj", verticies, fixedFaces); //saveAsObj("fixholes_added.obj", verticies, addedFaces); + //saveAsObj("fixholes_removed.obj", verticies, removedFaces); - qDebug() << "fixHoles holeRings:" << holeRings.size() << "newFaces:" << newFaces.size(); + faces = fixedFaces; + + //qDebug() << "fixHoles holeRings:" << holeRings.size() << "newFaces:" << newFaces.size() << "removedFaces:" << removedFaces.size(); } diff --git a/src/triangulatefaces.cpp b/src/triangulatefaces.cpp index 65d0d34a..bba7a3b3 100644 --- a/src/triangulatefaces.cpp +++ b/src/triangulatefaces.cpp @@ -49,7 +49,7 @@ bool triangulateFacesWithoutKeepVertices(std::vector &vertices, const bool isEar = true; for (size_t x = 0; x < fillRing.size() - 3; ++x) { auto h = (i + 3 + x) % fillRing.size(); - auto fourth = vertexToEigenVector3d(vertices[fillRing[h]]); + auto fourth = vertices[fillRing[h]]; if (pointInTriangle(enter, cone, leave, fourth)) { isEar = false; break;