Skip to content

Commit

Permalink
Mesh: [skip ci] refactor MeshTexture class to avoid code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jan 10, 2020
1 parent 7dd808a commit 19c3701
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 64 deletions.
92 changes: 28 additions & 64 deletions src/Mod/Mesh/App/MeshTexture.cpp
Expand Up @@ -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<App::Color> textureColor = materialRefMesh.diffuseColor;
Expand All @@ -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; index<points.size(); index++) {
unsigned long pos = kdTree->FindExact(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);
}
}
Expand All @@ -81,11 +104,11 @@ void MeshTexture::apply(const Mesh::MeshObject& mesh, const App::Color& defaultC
std::vector<unsigned long> pointMap;
pointMap.reserve(points.size());
for (size_t index=0; index<points.size(); index++) {
unsigned long pos = kdTree->FindExact(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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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<App::Color> textureColor = materialRefMesh.diffuseColor;
material.diffuseColor.clear();
material.binding = MeshCore::MeshIO::OVERALL;

if (kdTree.get()) {
// the points of the current mesh
std::vector<App::Color> 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; index<points.size(); index++) {
unsigned long pos = kdTree->FindExact(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<unsigned long> pointMap;
pointMap.reserve(points.size());
for (size_t index=0; index<points.size(); index++) {
unsigned long pos = kdTree->FindExact(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<unsigned long> 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;
}
}
}
}
25 changes: 25 additions & 0 deletions src/Mod/Mesh/App/MeshTexture.h
Expand Up @@ -53,12 +53,37 @@ class MeshExport MeshTexture
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;
Expand Down

0 comments on commit 19c3701

Please sign in to comment.