From 6020ffee843d5fde55b72c77f2f8155219b741ae Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 10 Jan 2020 14:05:42 +0100 Subject: [PATCH] Mesh: [skip ci] refactor MeshTexture class to avoid code duplication --- src/Mod/Mesh/App/MeshTexture.cpp | 92 ++++++++++---------------------- src/Mod/Mesh/App/MeshTexture.h | 25 +++++++++ 2 files changed, 53 insertions(+), 64 deletions(-) diff --git a/src/Mod/Mesh/App/MeshTexture.cpp b/src/Mod/Mesh/App/MeshTexture.cpp index 7f1b346991..72b3a3fd2c 100644 --- a/src/Mod/Mesh/App/MeshTexture.cpp +++ b/src/Mod/Mesh/App/MeshTexture.cpp @@ -47,6 +47,29 @@ MeshTexture::MeshTexture(const Mesh::MeshObject& mesh, const MeshCore::Material } void MeshTexture::apply(const Mesh::MeshObject& mesh, const App::Color& defaultColor, MeshCore::Material &material) +{ + apply(mesh, true, defaultColor, -1.0f, material); +} + +void MeshTexture::apply(const Mesh::MeshObject& mesh, const App::Color& defaultColor, float max_dist, MeshCore::Material &material) +{ + apply(mesh, true, defaultColor, max_dist, material); +} + +void MeshTexture::apply(const Mesh::MeshObject& mesh, MeshCore::Material &material) +{ + App::Color defaultColor; + apply(mesh, false, defaultColor, -1.0f, material); +} + +void MeshTexture::apply(const Mesh::MeshObject& mesh, float max_dist, MeshCore::Material &material) +{ + App::Color defaultColor; + apply(mesh, false, defaultColor, max_dist, material); +} + +void MeshTexture::apply(const Mesh::MeshObject& mesh, bool addDefaultColor, const App::Color& defaultColor, + float max_dist, MeshCore::Material &material) { // copy the color values because the passed material could be the same instance as 'materialRefMesh' std::vector textureColor = materialRefMesh.diffuseColor; @@ -62,11 +85,11 @@ void MeshTexture::apply(const Mesh::MeshObject& mesh, const App::Color& defaultC if (binding == MeshCore::MeshIO::PER_VERTEX) { diffuseColor.reserve(points.size()); for (size_t index=0; indexFindExact(points[index]); + unsigned long pos = findIndex(points[index], max_dist); if (pos < countPointsRefMesh) { diffuseColor.push_back(textureColor[pos]); } - else { + else if (addDefaultColor) { diffuseColor.push_back(defaultColor); } } @@ -81,11 +104,11 @@ void MeshTexture::apply(const Mesh::MeshObject& mesh, const App::Color& defaultC std::vector pointMap; pointMap.reserve(points.size()); for (size_t index=0; indexFindExact(points[index]); + unsigned long pos = findIndex(points[index], max_dist); if (pos < countPointsRefMesh) { pointMap.push_back(pos); } - else { + else if (addDefaultColor) { pointMap.push_back(ULONG_MAX); } } @@ -103,7 +126,7 @@ void MeshTexture::apply(const Mesh::MeshObject& mesh, const App::Color& defaultC diffuseColor.push_back(textureColor[found.front()]); } } - else { + else if (addDefaultColor) { diffuseColor.push_back(defaultColor); } } @@ -116,62 +139,3 @@ void MeshTexture::apply(const Mesh::MeshObject& mesh, const App::Color& defaultC } } } - -void MeshTexture::apply(const Mesh::MeshObject& mesh, MeshCore::Material &material) -{ - // copy the color values because the passed material could be the same instance as 'materialRefMesh' - std::vector textureColor = materialRefMesh.diffuseColor; - material.diffuseColor.clear(); - material.binding = MeshCore::MeshIO::OVERALL; - - if (kdTree.get()) { - // the points of the current mesh - std::vector diffuseColor; - const MeshCore::MeshPointArray& points = mesh.getKernel().GetPoints(); - const MeshCore::MeshFacetArray& facets = mesh.getKernel().GetFacets(); - - if (binding == MeshCore::MeshIO::PER_VERTEX) { - diffuseColor.reserve(points.size()); - for (size_t index=0; indexFindExact(points[index]); - if (pos < countPointsRefMesh) { - diffuseColor.push_back(textureColor[pos]); - } - } - - if (diffuseColor.size() == points.size()) { - material.diffuseColor.swap(diffuseColor); - material.binding = MeshCore::MeshIO::PER_VERTEX; - } - } - else if (binding == MeshCore::MeshIO::PER_FACE) { - // the values of the map give the point indices before the cut - std::vector pointMap; - pointMap.reserve(points.size()); - for (size_t index=0; indexFindExact(points[index]); - if (pos < countPointsRefMesh) { - pointMap.push_back(pos); - } - } - - // now determine the facet indices before the cut - if (pointMap.size() == points.size()) { - diffuseColor.reserve(facets.size()); - for (auto it : facets) { - std::vector found = refPnt2Fac->GetIndices(pointMap[it._aulPoints[0]], - pointMap[it._aulPoints[1]], - pointMap[it._aulPoints[2]]); - if (found.size() == 1) { - diffuseColor.push_back(textureColor[found.front()]); - } - } - } - - if (diffuseColor.size() == facets.size()) { - material.diffuseColor.swap(diffuseColor); - material.binding = MeshCore::MeshIO::PER_FACE; - } - } - } -} diff --git a/src/Mod/Mesh/App/MeshTexture.h b/src/Mod/Mesh/App/MeshTexture.h index 45c9bd740a..b82c94812c 100644 --- a/src/Mod/Mesh/App/MeshTexture.h +++ b/src/Mod/Mesh/App/MeshTexture.h @@ -53,12 +53,37 @@ public: original material is used. */ void apply(const Mesh::MeshObject& mesh, const App::Color& defaultColor, MeshCore::Material &material); + /*! + Find common points or facets of this to the original mesh. For points or facets + that don't match \a defaultColor will be used instead, otherwise the color of the + original material is used. + */ + void apply(const Mesh::MeshObject& mesh, const App::Color& defaultColor, float max_dist, MeshCore::Material &material); /*! Find common points or facets of this to the original mesh and use the color of the original material. If for a point of \a mesh no matching point of the original mesh can be found the texture mapping will fail. */ void apply(const Mesh::MeshObject& mesh, MeshCore::Material &material); + /*! + Find common points or facets of this to the original mesh and use the color of the original material. + If for a point of \a mesh no matching point of the original mesh can be found the texture mapping will + fail. + */ + void apply(const Mesh::MeshObject& mesh, float max_dist, MeshCore::Material &material); + +private: + void apply(const Mesh::MeshObject& mesh, bool addDefaultColor, const App::Color& defaultColor, float max_dist, MeshCore::Material &material); + unsigned long findIndex(const Base::Vector3f& p, float max_dist) const { + if (max_dist < 0.0f) { + return kdTree->FindExact(p); + } + else { + Base::Vector3f n; + float dist; + return kdTree->FindNearest(p, max_dist, n, dist); + } + } private: const MeshCore::Material &materialRefMesh;