1
/***************************************************************************
2
* Copyright (c) 2019 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include "MeshTexture.h"
30
MeshTexture::MeshTexture(const Mesh::MeshObject& mesh, const MeshCore::Material& material)
31
: materialRefMesh(material)
32
, countPointsRefMesh {mesh.countPoints()}
34
unsigned long countFacets = mesh.countFacets();
36
if (material.binding == MeshCore::MeshIO::PER_VERTEX
37
&& material.diffuseColor.size() == countPointsRefMesh) {
38
binding = MeshCore::MeshIO::PER_VERTEX;
39
kdTree = std::make_unique<MeshCore::MeshKDTree>(mesh.getKernel().GetPoints());
41
else if (material.binding == MeshCore::MeshIO::PER_FACE
42
&& material.diffuseColor.size() == countFacets) {
43
binding = MeshCore::MeshIO::PER_FACE;
44
kdTree = std::make_unique<MeshCore::MeshKDTree>(mesh.getKernel().GetPoints());
45
refPnt2Fac = std::make_unique<MeshCore::MeshRefPointToFacets>(mesh.getKernel());
49
void MeshTexture::apply(const Mesh::MeshObject& mesh,
50
const App::Color& defaultColor,
51
MeshCore::Material& material)
53
apply(mesh, true, defaultColor, -1.0f, material);
56
void MeshTexture::apply(const Mesh::MeshObject& mesh,
57
const App::Color& defaultColor,
59
MeshCore::Material& material)
61
apply(mesh, true, defaultColor, max_dist, material);
64
void MeshTexture::apply(const Mesh::MeshObject& mesh, MeshCore::Material& material)
66
App::Color defaultColor;
67
apply(mesh, false, defaultColor, -1.0f, material);
70
void MeshTexture::apply(const Mesh::MeshObject& mesh, float max_dist, MeshCore::Material& material)
72
App::Color defaultColor;
73
apply(mesh, false, defaultColor, max_dist, material);
76
void MeshTexture::apply(const Mesh::MeshObject& mesh,
78
const App::Color& defaultColor,
80
MeshCore::Material& material)
82
// copy the color values because the passed material could be the same instance as
84
std::vector<App::Color> textureColor = materialRefMesh.diffuseColor;
85
material.diffuseColor.clear();
86
material.binding = MeshCore::MeshIO::OVERALL;
89
// the points of the current mesh
90
std::vector<App::Color> diffuseColor;
91
const MeshCore::MeshPointArray& points = mesh.getKernel().GetPoints();
92
const MeshCore::MeshFacetArray& facets = mesh.getKernel().GetFacets();
94
if (binding == MeshCore::MeshIO::PER_VERTEX) {
95
diffuseColor.reserve(points.size());
96
for (size_t index = 0; index < points.size(); index++) {
97
PointIndex pos = findIndex(points[index], max_dist);
98
if (pos < countPointsRefMesh) {
99
diffuseColor.push_back(textureColor[pos]);
101
else if (addDefaultColor) {
102
diffuseColor.push_back(defaultColor);
106
if (diffuseColor.size() == points.size()) {
107
material.diffuseColor.swap(diffuseColor);
108
material.binding = MeshCore::MeshIO::PER_VERTEX;
111
else if (binding == MeshCore::MeshIO::PER_FACE) {
112
// the values of the map give the point indices of the original mesh
113
std::vector<PointIndex> pointMap;
114
pointMap.reserve(points.size());
115
for (size_t index = 0; index < points.size(); index++) {
116
PointIndex pos = findIndex(points[index], max_dist);
117
if (pos < countPointsRefMesh) {
118
pointMap.push_back(pos);
120
else if (addDefaultColor) {
121
pointMap.push_back(MeshCore::POINT_INDEX_MAX);
125
// now determine the facet indices of the original mesh
126
if (pointMap.size() == points.size()) {
127
diffuseColor.reserve(facets.size());
128
for (const auto& it : facets) {
129
PointIndex index1 = pointMap[it._aulPoints[0]];
130
PointIndex index2 = pointMap[it._aulPoints[1]];
131
PointIndex index3 = pointMap[it._aulPoints[2]];
132
if (index1 != MeshCore::POINT_INDEX_MAX && index2 != MeshCore::POINT_INDEX_MAX
133
&& index3 != MeshCore::POINT_INDEX_MAX) {
134
std::vector<FacetIndex> found =
135
refPnt2Fac->GetIndices(index1, index2, index3);
136
if (found.size() == 1) {
137
diffuseColor.push_back(textureColor[found.front()]);
140
else if (addDefaultColor) {
141
diffuseColor.push_back(defaultColor);
146
if (diffuseColor.size() == facets.size()) {
147
material.diffuseColor.swap(diffuseColor);
148
material.binding = MeshCore::MeshIO::PER_FACE;