From d218789d836c6d80dd1c82e7aba95b9d7a62b584 Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Wed, 18 Dec 2019 23:46:52 +0930 Subject: [PATCH] Add mesh recombiner optional edge loop optimization This optimization would make two combining edge loops move closer. The vertex postion been updated, this will affect the quad recovery and triangle source resolver, so this optimization is been commented out currently. --- src/meshrecombiner.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++ src/meshrecombiner.h | 1 + 2 files changed, 60 insertions(+) diff --git a/src/meshrecombiner.cpp b/src/meshrecombiner.cpp index 7e2118f1..a93fe9f5 100644 --- a/src/meshrecombiner.cpp +++ b/src/meshrecombiner.cpp @@ -202,6 +202,8 @@ bool MeshRecombiner::recombine() for (auto &it: islandsMap) { if (1 == it.second.edgeLoops[0].size() && it.second.edgeLoops[0].size() == it.second.edgeLoops[1].size()) { + //updateEdgeLoopNeighborVertices(it.second.edgeLoops[0][0]); + //updateEdgeLoopNeighborVertices(it.second.edgeLoops[1][0]); if (bridge(it.second.edgeLoops[0][0], it.second.edgeLoops[1][0])) { m_goodSeams.insert(it.first); } @@ -219,6 +221,63 @@ bool MeshRecombiner::recombine() return true; } +/* +void MeshRecombiner::updateEdgeLoopNeighborVertices(const std::vector &edgeLoop) +{ + std::map>> neighborPositionMap; + float sumOfDistance = 0.0f; + size_t countOfDistance = 0; + for (size_t i = 0; i < edgeLoop.size(); ++i) { + size_t j = (i + 1) % edgeLoop.size(); + auto edge = std::make_pair(edgeLoop[i], edgeLoop[j]); + auto findFace = m_halfEdgeToFaceMap.find(edge); + if (findFace == m_halfEdgeToFaceMap.end()) { + continue; + } + const auto &face = (*m_faces)[findFace->second]; + for (const auto &vertexIndex: face) { + if (edge.first == vertexIndex || edge.second == vertexIndex) + continue; + const auto &otherPosition = (*m_vertices)[vertexIndex]; + const auto &firstPosition = (*m_vertices)[edge.first]; + { + auto distance = (firstPosition - otherPosition).length(); + sumOfDistance += distance; + ++countOfDistance; + neighborPositionMap[edge.first].push_back(std::make_pair(otherPosition, distance)); + } + const auto &secondPosition = (*m_vertices)[edge.second]; + { + auto distance = (secondPosition - otherPosition).length(); + sumOfDistance += distance; + ++countOfDistance; + neighborPositionMap[edge.second].push_back(std::make_pair(otherPosition, distance)); + } + break; + } + } + if (0 == countOfDistance) + return; + auto averageDistance = sumOfDistance / countOfDistance; + for (const auto &vertex: neighborPositionMap) { + const auto &originalPosition = (*m_vertices)[vertex.first]; + if (qFuzzyIsNull(originalPosition.x())) + continue; + QVector3D sumOfPosition; + size_t countOfPosition = 0; + for (const auto &line: vertex.second) { + if (line.second > averageDistance) { + sumOfPosition += originalPosition + (line.first - originalPosition).normalized() * (line.second - averageDistance) * 0.5; + ++countOfPosition; + } + } + if (0 == countOfPosition) + continue; + (*m_vertices)[vertex.first] = sumOfPosition / countOfPosition; + } +} +*/ + size_t MeshRecombiner::adjustTrianglesFromSeam(std::vector &edgeLoop, size_t seamIndex) { if (edgeLoop.size() <= 3) diff --git a/src/meshrecombiner.h b/src/meshrecombiner.h index 5f8fb697..2dcd76e9 100644 --- a/src/meshrecombiner.h +++ b/src/meshrecombiner.h @@ -42,6 +42,7 @@ private: size_t nearestIndex(const QVector3D &position, const std::vector &edgeLoop); void removeReluctantVertices(); void fillPairs(const std::vector &small, const std::vector &large); + void updateEdgeLoopNeighborVertices(const std::vector &edgeLoop); }; #endif