Coverage Report

Created: 2026-09-14 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/IFC/IFCMaterial.cpp
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2026, assimp team
6
7
All rights reserved.
8
9
Redistribution and use of this software in source and binary forms,
10
with or without modification, are permitted provided that the
11
following conditions are met:
12
13
* Redistributions of source code must retain the above
14
  copyright notice, this list of conditions and the
15
  following disclaimer.
16
17
* Redistributions in binary form must reproduce the above
18
  copyright notice, this list of conditions and the
19
  following disclaimer in the documentation and/or other
20
  materials provided with the distribution.
21
22
* Neither the name of the assimp team, nor the names of its
23
  contributors may be used to endorse or promote products
24
  derived from this software without specific prior
25
  written permission of the assimp team.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39
----------------------------------------------------------------------
40
*/
41
42
/// @file  IFCMaterial.cpp
43
/// @brief Implementation of conversion routines to convert IFC materials to aiMaterial
44
45
#ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
46
47
#include "IFCUtil.h"
48
#include <limits>
49
#include <assimp/material.h>
50
51
namespace Assimp {
52
namespace IFC {
53
54
// ------------------------------------------------------------------------------------------------
55
74
static int ConvertShadingMode(const std::string& name) {
56
74
    if (name == "BLINN") {
57
0
        return aiShadingMode_Blinn;
58
0
    }
59
74
    else if (name == "FLAT" || name == "NOTDEFINED") {
60
13
        return aiShadingMode_NoShading;
61
13
    }
62
61
    else if (name == "PHONG") {
63
0
        return aiShadingMode_Phong;
64
0
    }
65
61
    IFCImporter::LogWarn("shading mode ", name, " not recognized by Assimp, using Phong instead");
66
61
    return aiShadingMode_Phong;
67
74
}
68
69
// ------------------------------------------------------------------------------------------------
70
2.86k
static void FillMaterial(aiMaterial* mat,const IFC::Schema_2x3::IfcSurfaceStyle* surf,ConversionData& conv) {
71
2.86k
    aiString name;
72
2.86k
    name.Set((surf->Name? surf->Name.Get() : "IfcSurfaceStyle_Unnamed"));
73
2.86k
    mat->AddProperty(&name,AI_MATKEY_NAME);
74
75
    // now see which kinds of surface information are present
76
2.92k
    for (const std::shared_ptr<const IFC::Schema_2x3::IfcSurfaceStyleElementSelect> &sel2 : surf->Styles) {
77
2.92k
        if (const IFC::Schema_2x3::IfcSurfaceStyleShading* shade = sel2->ResolveSelectPtr<IFC::Schema_2x3::IfcSurfaceStyleShading>(conv.db)) {
78
2.67k
            aiColor4D col_base,col;
79
80
2.67k
            ConvertColor(col_base, shade->SurfaceColour);
81
2.67k
            mat->AddProperty(&col_base,1, AI_MATKEY_COLOR_DIFFUSE);
82
83
2.67k
            if (const IFC::Schema_2x3::IfcSurfaceStyleRendering* ren = shade->ToPtr<IFC::Schema_2x3::IfcSurfaceStyleRendering>()) {
84
85
2.65k
                if (ren->Transparency) {
86
2.65k
                    const float t = 1.f-static_cast<float>(ren->Transparency.Get());
87
2.65k
                    mat->AddProperty(&t,1, AI_MATKEY_OPACITY);
88
2.65k
                }
89
90
2.65k
                if (ren->DiffuseColour) {
91
2.64k
                    ConvertColor(col, *ren->DiffuseColour.Get(),conv,&col_base);
92
2.64k
                    mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE);
93
2.64k
                }
94
95
2.65k
                if (ren->SpecularColour) {
96
2.65k
                    ConvertColor(col, *ren->SpecularColour.Get(),conv,&col_base);
97
2.65k
                    mat->AddProperty(&col,1, AI_MATKEY_COLOR_SPECULAR);
98
2.65k
                }
99
100
2.65k
                if (ren->TransmissionColour) {
101
26
                    ConvertColor(col, *ren->TransmissionColour.Get(),conv,&col_base);
102
26
                    mat->AddProperty(&col,1, AI_MATKEY_COLOR_TRANSPARENT);
103
26
                }
104
105
2.65k
                if (ren->ReflectionColour) {
106
16
                    ConvertColor(col, *ren->ReflectionColour.Get(),conv,&col_base);
107
16
                    mat->AddProperty(&col,1, AI_MATKEY_COLOR_REFLECTIVE);
108
16
                }
109
110
2.65k
                const int shading = (ren->SpecularHighlight && ren->SpecularColour)?ConvertShadingMode(ren->ReflectanceMethod):static_cast<int>(aiShadingMode_Gouraud);
111
2.65k
                mat->AddProperty(&shading,1, AI_MATKEY_SHADING_MODEL);
112
113
2.65k
                if (ren->SpecularHighlight) {
114
74
                    if(const ::Assimp::STEP::EXPRESS::REAL* rt = ren->SpecularHighlight.Get()->ToPtr<::Assimp::STEP::EXPRESS::REAL>()) {
115
                        // at this point we don't distinguish between the two distinct ways of
116
                        // specifying highlight intensities. leave this to the user.
117
0
                        const float e = static_cast<float>(*rt);
118
0
                        mat->AddProperty(&e,1,AI_MATKEY_SHININESS);
119
0
                    }
120
74
                    else {
121
74
                        IFCImporter::LogWarn("unexpected type error, SpecularHighlight should be a REAL");
122
74
                    }
123
74
                }
124
2.65k
            }
125
2.67k
        }
126
2.92k
    }
127
2.86k
}
128
129
// ------------------------------------------------------------------------------------------------
130
64.9k
unsigned int ProcessMaterials(uint64_t id, unsigned int prevMatId, ConversionData& conv, bool forceDefaultMat) {
131
64.9k
    STEP::DB::RefMapRange range = conv.db.GetRefs().equal_range(id);
132
73.9k
    for(;range.first != range.second; ++range.first) {
133
54.0k
        if(const IFC::Schema_2x3::IfcStyledItem* const styled = conv.db.GetObject((*range.first).second)->ToPtr<IFC::Schema_2x3::IfcStyledItem>()) {
134
52.0k
            for(const IFC::Schema_2x3::IfcPresentationStyleAssignment& as : styled->Styles) {
135
51.7k
                for (const std::shared_ptr<const IFC::Schema_2x3::IfcPresentationStyleSelect> &sel : as.Styles) {
136
137
51.3k
                    if( const IFC::Schema_2x3::IfcSurfaceStyle* const surf = sel->ResolveSelectPtr<IFC::Schema_2x3::IfcSurfaceStyle>(conv.db) ) {
138
                        // try to satisfy from cache
139
44.9k
                        ConversionData::MaterialCache::iterator mit = conv.cached_materials.find(surf);
140
44.9k
                        if( mit != conv.cached_materials.end() )
141
42.1k
                            return mit->second;
142
143
                        // not found, create new material
144
2.86k
                        const std::string side = static_cast<std::string>(surf->Side);
145
2.86k
                        if( side != "BOTH" ) {
146
77
                            IFCImporter::LogWarn("ignoring surface side marker on IFC::IfcSurfaceStyle: ", side);
147
77
                        }
148
149
2.86k
                        std::unique_ptr<aiMaterial> mat(new aiMaterial());
150
151
2.86k
                        FillMaterial(mat.get(), surf, conv);
152
153
2.86k
                        conv.materials.push_back(mat.release());
154
2.86k
                        unsigned int matindex = static_cast<unsigned int>(conv.materials.size() - 1);
155
2.86k
                        conv.cached_materials[surf] = matindex;
156
2.86k
                        return matindex;
157
44.9k
                    }
158
51.3k
                }
159
51.7k
            }
160
52.0k
        }
161
54.0k
    }
162
163
    // no local material defined. If there's global one, use that instead
164
19.9k
    if ( prevMatId != std::numeric_limits<uint32_t>::max() ) {
165
1.11k
        return prevMatId;
166
1.11k
    }
167
168
    // we're still here - create an default material if required, or simply fail otherwise
169
18.8k
    if ( !forceDefaultMat ) {
170
14.3k
        return std::numeric_limits<uint32_t>::max();
171
14.3k
    }
172
173
4.51k
    aiString name;
174
4.51k
    name.Set("<IFCDefault>");
175
176
    // look if there's already a default material with this base color
177
9.08k
    for( size_t a = 0; a < conv.materials.size(); ++a ) {
178
8.48k
        aiString mname;
179
8.48k
        conv.materials[a]->Get(AI_MATKEY_NAME, mname);
180
8.48k
        if ( name == mname ) {
181
3.91k
            return ( unsigned int )a;
182
3.91k
        }
183
8.48k
    }
184
185
    // we're here, yet - no default material with suitable color available. Generate one
186
599
    std::unique_ptr<aiMaterial> mat(new aiMaterial());
187
599
    mat->AddProperty(&name,AI_MATKEY_NAME);
188
189
599
    const aiColor4D col = aiColor4D( 0.6f, 0.6f, 0.6f, 1.0f); // aiColor4D( color.r, color.g, color.b, 1.0f);
190
599
    mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE);
191
192
599
    conv.materials.push_back(mat.release());
193
599
    return (unsigned int) conv.materials.size() - 1;
194
4.51k
}
195
196
} // ! IFC
197
} // ! Assimp
198
199
#endif // ASSIMP_BUILD_NO_IFC_IMPORTER