Visual Computing Library  devel
Loading...
Searching...
No Matches
materials.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MESH_COMPONENTS_MATERIALS_H
24#define VCL_MESH_COMPONENTS_MATERIALS_H
25
26#include "base/component.h"
27
28#include <vclib/space/core.h>
29
30namespace vcl::comp {
31
32namespace detail {
33
34struct MData
35{
36 // The base path of the mesh, used to resolve relative texture paths.
37 std::string meshPath;
38 // A vector containing all the materials defined for the mesh.
39 std::vector<Material> materials;
40 // A map where keys are texture paths and values are the loaded Image data.
41 std::map<std::string, Image> textureImages;
42};
43
44} // namespace detail
45
61class Materials :
62 public Component<
63 Materials,
64 CompId::MATERIALS,
65 detail::MData,
66 void,
67 false,
68 false>
69{
70 using Base = Component<
72 CompId::MATERIALS,
73 detail::MData,
74 void,
75 false,
76 false>;
77
78 inline static const Image EMPTY_IMAGE;
79
80public:
83
84 // iterators
85
87 using MaterialIterator = std::vector<Material>::iterator;
89 using ConstMaterialIterator = std::vector<Material>::const_iterator;
91 using TextureImageIterator = std::map<std::string, Image>::iterator;
94 std::map<std::string, Image>::const_iterator;
95
99 Materials() = default;
100
101 /* Member functions */
102
107 uint materialsNumber() const { return mats().size(); }
108
113 uint textureImagesNumber() const { return txtImgs().size(); }
114
122 const std::string& meshBasePath() const { return Base::data().meshPath; }
123
128 std::string& meshBasePath() { return Base::data().meshPath; }
129
135 const Material& material(uint i) const { return mats()[i]; }
136
143 Material& material(uint i) { return mats()[i]; }
144
155 const Image& textureImage(const std::string& texturePath) const
156 {
157 auto it = txtImgs().find(texturePath);
158 if (it == txtImgs().end()) {
159 return EMPTY_IMAGE;
160 }
161 return it->second;
162 }
163
171 {
172 mats().clear();
173 txtImgs().clear();
174 }
175
180 void pushMaterial(const Material& mat) { mats().push_back(mat); }
181
191 void pushTextureImage(const std::string& texturePath, const Image& img)
192 {
193 txtImgs()[texturePath] = img;
194 }
195
205 void pushTextureImage(const std::string& texturePath, Image&& img)
206 {
207 txtImgs()[texturePath] = std::move(img);
208 }
209
214 MaterialIterator materialBegin() { return mats().begin(); }
215
220 MaterialIterator materialEnd() { return mats().end(); }
221
227 ConstMaterialIterator materialBegin() const { return mats().begin(); }
228
233 ConstMaterialIterator materialEnd() const { return mats().end(); }
234
240 TextureImageIterator textureImageBegin() { return txtImgs().begin(); }
241
246 TextureImageIterator textureImageEnd() { return txtImgs().end(); }
247
254 {
255 return txtImgs().begin();
256 }
257
264 {
265 return txtImgs().end();
266 }
267
287
307
327
347
348protected:
349 // Component interface functions
350 template<typename Element>
351 void importFrom(const Element& e, bool = true);
352
353 void serialize(std::ostream& os) const
354 {
355 vcl::serialize(os, meshBasePath());
356 vcl::serialize(os, mats());
357 vcl::serialize(os, txtImgs());
358 }
359
360 void deserialize(std::istream& is)
361 {
362 vcl::deserialize(is, meshBasePath());
363 vcl::deserialize(is, mats());
364 vcl::deserialize(is, txtImgs());
365 }
366
367private:
368 // members that allow to access the data
369 std::vector<Material>& mats() { return Base::data().materials; }
370
371 const std::vector<Material>& mats() const { return Base::data().materials; }
372
373 std::map<std::string, Image>& txtImgs()
374 {
375 return Base::data().textureImages;
376 }
377
378 const std::map<std::string, Image>& txtImgs() const
379 {
380 return Base::data().textureImages;
381 }
382};
383
384/* concept */
385
397template<typename T>
398concept HasMaterials = std::derived_from<std::remove_cvref_t<T>, Materials>;
399
400/* imoportFrom function */
401
402template<typename Element>
403void Materials::importFrom(const Element& e, bool)
404{
405 if constexpr (HasMaterials<Element>) {
406 mats().clear();
407 txtImgs().clear();
408 for (const auto& mat : e.materials()) {
409 mats().push_back(mat);
410 }
411 for (const auto& [path, img] : e.textureImages()) {
412 txtImgs()[path] = img;
413 }
414 meshBasePath() = e.meshBasePath();
415 }
416}
417
418} // namespace vcl::comp
419
420#endif // VCL_MESH_COMPONENTS_MATERIALS_H
A class representing a box in N-dimensional space.
Definition box.h:46
The Element class.
Definition element.h:75
A class for representing and manipulating 2D images.
Definition image.h:48
Represents a Physically-Based Rendering (PBR) material.
Definition material.h:45
The View class is a simple class that stores and exposes two iterators begin and end.
Definition view.h:67
A component that manages materials and textures for a mesh.
Definition materials.h:69
View< ConstMaterialIterator > materials() const
Returns a lightweight const view object that stores the begin and end iterators of the vector of mate...
Definition materials.h:303
View< ConstTextureImageIterator > textureImages() const
Returns a lightweight const view object that stores the begin and end iterators of the map of texture...
Definition materials.h:343
std::map< std::string, Image >::const_iterator ConstTextureImageIterator
Const iterator for the map of texture images.
Definition materials.h:94
ConstMaterialIterator materialEnd() const
Returns a const iterator to the end of the vector of materials.
Definition materials.h:233
std::string & meshBasePath()
Returns a reference to the mesh base path.
Definition materials.h:128
void pushTextureImage(const std::string &texturePath, const Image &img)
Adds a texture image to the map of texture images.
Definition materials.h:191
std::vector< Material >::iterator MaterialIterator
Iterator for the vector of materials.
Definition materials.h:87
MaterialIterator materialBegin()
Returns an iterator to the beginning of the vector of materials.
Definition materials.h:214
const Material & material(uint i) const
Returns the material at the specified index.
Definition materials.h:135
uint textureImagesNumber() const
Returns the number of texture images stored in the component.
Definition materials.h:113
Material & material(uint i)
Returns a mutable reference to the material at the specified index.
Definition materials.h:143
void pushTextureImage(const std::string &texturePath, Image &&img)
Adds a texture image to the map of texture images (move version).
Definition materials.h:205
std::vector< Material >::const_iterator ConstMaterialIterator
Const iterator for the vector of materials.
Definition materials.h:89
std::map< std::string, Image >::iterator TextureImageIterator
Iterator for the map of texture images.
Definition materials.h:91
ConstMaterialIterator materialBegin() const
Returns a const iterator to the beginning of the vector of materials.
Definition materials.h:227
const std::string & meshBasePath() const
Returns the mesh base path.
Definition materials.h:122
View< TextureImageIterator > textureImages()
Returns a lightweight view object that stores the begin and end iterators of the map of texture image...
Definition materials.h:323
void clearMaterials()
Clears the vector of materials.
Definition materials.h:170
const Image & textureImage(const std::string &texturePath) const
Returns the texture image associated to the given texture path.
Definition materials.h:155
ConstTextureImageIterator textureImageEnd() const
Returns a const iterator to the end of the map of texture images.
Definition materials.h:263
TextureImageIterator textureImageBegin()
Returns an iterator to the beginning of the map of texture images.
Definition materials.h:240
View< MaterialIterator > materials()
Returns a lightweight view object that stores the begin and end iterators of the vector of materials....
Definition materials.h:283
Materials()=default
Default constructor for the Materials component.
TextureImageIterator textureImageEnd()
Returns an iterator to the end of the map of texture images.
Definition materials.h:246
uint materialsNumber() const
Returns the number of materials of the mesh.
Definition materials.h:107
MaterialIterator materialEnd()
Returns an iterator to the end of the vector of materials.
Definition materials.h:220
void pushMaterial(const Material &mat)
Adds a material to the vector of materials.
Definition materials.h:180
ConstTextureImageIterator textureImageBegin() const
Returns a const iterator to the beginning of the map of texture images.
Definition materials.h:253
A concept that checks whether a type T (that should be a Mesh) has the Materials component (inherits ...
Definition materials.h:398