FreeCAD

Форк
0
/
MeshTexture.cpp 
152 строки · 6.6 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2019 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#include "MeshTexture.h"
26

27

28
using namespace Mesh;
29

30
MeshTexture::MeshTexture(const Mesh::MeshObject& mesh, const MeshCore::Material& material)
31
    : materialRefMesh(material)
32
    , countPointsRefMesh {mesh.countPoints()}
33
{
34
    unsigned long countFacets = mesh.countFacets();
35

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());
40
    }
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());
46
    }
47
}
48

49
void MeshTexture::apply(const Mesh::MeshObject& mesh,
50
                        const App::Color& defaultColor,
51
                        MeshCore::Material& material)
52
{
53
    apply(mesh, true, defaultColor, -1.0f, material);
54
}
55

56
void MeshTexture::apply(const Mesh::MeshObject& mesh,
57
                        const App::Color& defaultColor,
58
                        float max_dist,
59
                        MeshCore::Material& material)
60
{
61
    apply(mesh, true, defaultColor, max_dist, material);
62
}
63

64
void MeshTexture::apply(const Mesh::MeshObject& mesh, MeshCore::Material& material)
65
{
66
    App::Color defaultColor;
67
    apply(mesh, false, defaultColor, -1.0f, material);
68
}
69

70
void MeshTexture::apply(const Mesh::MeshObject& mesh, float max_dist, MeshCore::Material& material)
71
{
72
    App::Color defaultColor;
73
    apply(mesh, false, defaultColor, max_dist, material);
74
}
75

76
void MeshTexture::apply(const Mesh::MeshObject& mesh,
77
                        bool addDefaultColor,
78
                        const App::Color& defaultColor,
79
                        float max_dist,
80
                        MeshCore::Material& material)
81
{
82
    // copy the color values because the passed material could be the same instance as
83
    // 'materialRefMesh'
84
    std::vector<App::Color> textureColor = materialRefMesh.diffuseColor;
85
    material.diffuseColor.clear();
86
    material.binding = MeshCore::MeshIO::OVERALL;
87

88
    if (kdTree.get()) {
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();
93

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]);
100
                }
101
                else if (addDefaultColor) {
102
                    diffuseColor.push_back(defaultColor);
103
                }
104
            }
105

106
            if (diffuseColor.size() == points.size()) {
107
                material.diffuseColor.swap(diffuseColor);
108
                material.binding = MeshCore::MeshIO::PER_VERTEX;
109
            }
110
        }
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);
119
                }
120
                else if (addDefaultColor) {
121
                    pointMap.push_back(MeshCore::POINT_INDEX_MAX);
122
                }
123
            }
124

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()]);
138
                        }
139
                    }
140
                    else if (addDefaultColor) {
141
                        diffuseColor.push_back(defaultColor);
142
                    }
143
                }
144
            }
145

146
            if (diffuseColor.size() == facets.size()) {
147
                material.diffuseColor.swap(diffuseColor);
148
                material.binding = MeshCore::MeshIO::PER_FACE;
149
            }
150
        }
151
    }
152
}
153

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.