From 2b44576f224784c36db29201cab83533d3f89fab Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Tue, 26 Feb 2019 22:54:58 +0930 Subject: [PATCH] Fix cut face base normal. --- src/texturegenerator.cpp | 5 ++- src/texturegenerator.h | 2 + thirdparty/nodemesh/nodemesh/builder.cpp | 47 ++++++++++++++++++++---- thirdparty/nodemesh/nodemesh/builder.h | 2 + 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/texturegenerator.cpp b/src/texturegenerator.cpp index f3fd56e5..82ce70b3 100644 --- a/src/texturegenerator.cpp +++ b/src/texturegenerator.cpp @@ -10,6 +10,7 @@ #include "material.h" int TextureGenerator::m_textureSize = 1024; +QColor TextureGenerator::m_defaultTextureColor = Qt::darkGray; TextureGenerator::TextureGenerator(const Outcome &outcome, Snapshot *snapshot) : m_resultTextureGuideImage(nullptr), @@ -233,7 +234,7 @@ void TextureGenerator::generate() auto createImageBeginTime = countTimeConsumed.elapsed(); m_resultTextureColorImage = new QImage(TextureGenerator::m_textureSize, TextureGenerator::m_textureSize, QImage::Format_ARGB32); - m_resultTextureColorImage->fill(Theme::white); + m_resultTextureColorImage->fill(m_defaultTextureColor); m_resultTextureBorderImage = new QImage(TextureGenerator::m_textureSize, TextureGenerator::m_textureSize, QImage::Format_ARGB32); m_resultTextureBorderImage->fill(Qt::transparent); @@ -315,7 +316,7 @@ void TextureGenerator::generate() if (findSourceNodeResult != nodeMap.end() && nullptr != findSourceNodeResult->second) { texturePainter.fillPath(path, QBrush(findSourceNodeResult->second->color)); } else { - texturePainter.fillPath(path, QBrush(Theme::white)); + texturePainter.fillPath(path, QBrush(m_defaultTextureColor)); } } // Copy normal texture if there is one diff --git a/src/texturegenerator.h b/src/texturegenerator.h index 43699b65..be819770 100644 --- a/src/texturegenerator.h +++ b/src/texturegenerator.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "outcome.h" #include "meshloader.h" #include "snapshot.h" @@ -36,6 +37,7 @@ public slots: void process(); public: static int m_textureSize; + static QColor m_defaultTextureColor; private: void prepare(); QPainterPath expandedPainterPath(const QPainterPath &painterPath, int expandSize=7); diff --git a/thirdparty/nodemesh/nodemesh/builder.cpp b/thirdparty/nodemesh/nodemesh/builder.cpp index 7f998e87..56c9bdc1 100644 --- a/thirdparty/nodemesh/nodemesh/builder.cpp +++ b/thirdparty/nodemesh/nodemesh/builder.cpp @@ -104,6 +104,8 @@ void Builder::prepareNode(size_t nodeIndex) neighborRadius); node.initialBaseNormal = baseNormalResult.first; node.hasInitialBaseNormal = baseNormalResult.second; + if (node.hasInitialBaseNormal) + node.initialBaseNormal = revisedBaseNormalAcordingToCutNormal(node.initialBaseNormal, node.traverseDirection); } void Builder::resolveBaseNormalRecursively(size_t nodeIndex) @@ -260,6 +262,9 @@ bool Builder::build() for (const auto &nodeIndex: m_sortedNodeIndices) { resolveBaseNormalRecursively(nodeIndex); } + + unifyBaseNormals(); + for (const auto &nodeIndex: m_sortedNodeIndices) { if (!generateCutsForNode(nodeIndex)) succeed = false; @@ -555,13 +560,24 @@ bool Builder::swallowEdgeForNode(size_t nodeIndex, size_t edgeOrder) return true; } -void Builder::makeCut(const QVector3D &position, - float radius, - const std::vector &cutTemplate, - QVector3D &baseNormal, - const QVector3D &cutNormal, - const QVector3D &traverseDirection, - std::vector &resultCut) +void Builder::unifyBaseNormals() +{ + std::vector nodeIndices(m_nodes.size()); + for (size_t i = 0; i < m_nodes.size(); ++i) { + const auto &node = m_nodes[i]; + nodeIndices[node.reversedTraverseOrder] = i; + } + for (size_t i = 1; i < nodeIndices.size(); ++i) { + size_t lastIndex = nodeIndices[i - 1]; + size_t nodeIndex = nodeIndices[i]; + auto &node = m_nodes[nodeIndex]; + const auto &last = m_nodes[lastIndex]; + if (QVector3D::dotProduct(node.baseNormal, last.baseNormal) <= 0) + node.baseNormal = -node.baseNormal; + } +} + +QVector3D Builder::revisedBaseNormalAcordingToCutNormal(const QVector3D &baseNormal, const QVector3D &cutNormal) { QVector3D orientedBaseNormal = QVector3D::dotProduct(cutNormal, baseNormal) > 0 ? baseNormal : -baseNormal; @@ -573,13 +589,28 @@ void Builder::makeCut(const QVector3D &position, orientedBaseNormal = QVector3D(1, 0, 0); } } - baseNormal = orientedBaseNormal.normalized(); + return orientedBaseNormal.normalized(); +} + +void Builder::makeCut(const QVector3D &position, + float radius, + const std::vector &cutTemplate, + QVector3D &baseNormal, + const QVector3D &cutNormal, + const QVector3D &traverseDirection, + std::vector &resultCut) +{ + baseNormal = revisedBaseNormalAcordingToCutNormal(baseNormal, cutNormal); auto finalCutTemplate = cutTemplate; auto finalCutNormal = cutNormal; if (QVector3D::dotProduct(cutNormal, traverseDirection) <= 0) { baseNormal = -baseNormal; finalCutNormal = -finalCutNormal; std::reverse(finalCutTemplate.begin(), finalCutTemplate.end()); + //for (auto &it: finalCutTemplate) { + // it.setX(-it.x()); + // it.setY(-it.y()); + //} } QVector3D u = QVector3D::crossProduct(finalCutNormal, baseNormal).normalized(); QVector3D v = QVector3D::crossProduct(u, finalCutNormal).normalized(); diff --git a/thirdparty/nodemesh/nodemesh/builder.h b/thirdparty/nodemesh/nodemesh/builder.h index 90b79bab..79893957 100644 --- a/thirdparty/nodemesh/nodemesh/builder.h +++ b/thirdparty/nodemesh/nodemesh/builder.h @@ -102,7 +102,9 @@ private: void resolveBaseNormalRecursively(size_t nodeIndex); void resolveBaseNormalForLeavesRecursively(size_t nodeIndex, const QVector3D &baseNormal); std::pair searchBaseNormalFromNeighborsRecursively(size_t nodeIndex); + QVector3D revisedBaseNormalAcordingToCutNormal(const QVector3D &baseNormal, const QVector3D &cutNormal); void resolveInitialTraverseDirectionRecursively(size_t nodeIndex, const QVector3D *from, std::set *visited); + void unifyBaseNormals(); void resolveTraverseDirection(size_t nodeIndex); bool generateCutsForNode(size_t nodeIndex); bool tryWrapMultipleBranchesForNode(size_t nodeIndex, std::vector &offsets, bool &offsetsChanged);