Visual Computing Library  devel
Loading...
Searching...
No Matches
texture_paths.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_TEXTURE_PATHS_H
24#define VCL_MESH_COMPONENTS_TEXTURE_PATHS_H
25
26#include "base/component.h"
27#include "concepts/textures.h"
28
29#include <vclib/base.h>
30
31#include <string>
32#include <vector>
33
34namespace vcl::comp {
35
36namespace detail {
37
38struct TPData
39{
40 std::vector<std::string> texPaths;
41 std::string meshPath;
42};
43
44} // namespace detail
45
72 public Component<
73 TexturePaths,
74 CompId::TEXTURE_PATHS,
75 detail::TPData,
76 void,
77 false,
78 false>
79{
80 using Base = Component<
82 CompId::TEXTURE_PATHS,
83 detail::TPData,
84 void,
85 false,
86 false>;
87
88public:
89 // iterators
90 using TexFileNamesIterator = std::vector<std::string>::iterator;
91 using ConstTexFileNamesIterator = std::vector<std::string>::const_iterator;
92
93 /* Constructors */
94
99 TexturePaths() = default;
100
101 /* Member functions */
102
106 uint textureNumber() const { return texPaths().size(); }
107
113 const std::string& texturePath(uint i) const { return texPaths()[i]; }
114
120 std::string& texturePath(uint i) { return texPaths()[i]; }
121
125 const std::string& meshBasePath() const { return Base::data().meshPath; }
126
130 std::string& meshBasePath() { return Base::data().meshPath; }
131
138 uint indexOfTexturePath(const std::string& path) const
139 {
140 auto it = std::find(texPaths().begin(), texPaths().end(), path);
141 if (it != texPaths().end()) {
142 return static_cast<uint>(std::distance(texPaths().begin(), it));
143 }
144 return UINT_NULL;
145 }
146
150 void clearTexturePaths() { texPaths().clear(); }
151
156 void pushTexturePath(const std::string& textName)
157 {
158 texPaths().push_back(textName);
159 }
160
166 TexFileNamesIterator texturePathBegin() { return texPaths().begin(); }
167
172 TexFileNamesIterator texturePathEnd() { return texPaths().end(); }
173
179 ConstTexFileNamesIterator texturePathBegin() const
180 {
181 return texPaths().begin();
182 }
183
189 ConstTexFileNamesIterator texturePathEnd() const
190 {
191 return texPaths().end();
192 }
193
214
234
235protected:
236 // Component interface functions
237 template<typename Element>
238 void importFrom(const Element& e, bool = true)
239 {
240 if constexpr (HasTexturePaths<Element>) {
241 texPaths().clear();
242 for (const auto& tex : e.textures()) {
243 texPaths().push_back(tex.path());
244 }
245 meshBasePath() = e.meshBasePath();
246 }
247 }
248
249 void serialize(std::ostream& os) const
250 {
251 vcl::serialize(os, texPaths());
252 vcl::serialize(os, meshBasePath());
253 }
254
255 void deserialize(std::istream& is)
256 {
257 vcl::deserialize(is, texPaths());
258 vcl::deserialize(is, meshBasePath());
259 }
260
261private:
262 // members that allow to access the data
263 std::vector<std::string>& texPaths() { return Base::data().texPaths; }
264
265 const std::vector<std::string>& texPaths() const
266 {
267 return Base::data().texPaths;
268 }
269};
270
271} // namespace vcl::comp
272
273#endif // VCL_MESH_COMPONENTS_TEXTURE_PATHS_H
A class representing a box in N-dimensional space.
Definition box.h:46
The Element class.
Definition element.h:75
The View class is a simple class that stores and exposes two iterators begin and end.
Definition view.h:67
The TexturePaths class represents a component that stores the paths of the textures used by a mesh....
Definition texture_paths.h:79
uint textureNumber() const
Returns the number of texture paths of the mesh.
Definition texture_paths.h:106
uint indexOfTexturePath(const std::string &path) const
Returns the index of the texture with the given path, or UINT_NULL if the texture is not found.
Definition texture_paths.h:138
TexFileNamesIterator texturePathBegin()
Returns an iterator to the beginning of the vector of texture paths.
Definition texture_paths.h:166
void clearTexturePaths()
Clears the vector of texture paths.
Definition texture_paths.h:150
TexFileNamesIterator texturePathEnd()
Returns an iterator to the end of the vector of texture paths.
Definition texture_paths.h:172
TexturePaths()=default
Initializes the component with an empty vector of texture paths, and an empty string as mesh base pat...
std::string & meshBasePath()
Returns a reference to the mesh base path.
Definition texture_paths.h:130
View< ConstTexFileNamesIterator > texturePaths() const
Returns a lightweigth const view object that stores the begin and end iterators of the vector of text...
Definition texture_paths.h:230
std::string & texturePath(uint i)
Returns a reference to the path of the i-th texture of the mesh. The path is relative to the mesh bas...
Definition texture_paths.h:120
ConstTexFileNamesIterator texturePathEnd() const
Returns a const iterator to the end of the vector of texture paths.
Definition texture_paths.h:189
const std::string & meshBasePath() const
Returns the mesh base path.
Definition texture_paths.h:125
View< TexFileNamesIterator > texturePaths()
Returns a lightweigth view object that stores the begin and end iterators of the vector of texture pa...
Definition texture_paths.h:210
ConstTexFileNamesIterator texturePathBegin() const
Returns a const iterator to the beginning of the vector of texture paths.
Definition texture_paths.h:179
void pushTexturePath(const std::string &textName)
Adds a texture path to the vector of texture paths.
Definition texture_paths.h:156
const std::string & texturePath(uint i) const
Returns the path of the i-th texture of the mesh. The path is relative to the mesh base path.
Definition texture_paths.h:113
A concept that checks whether a type T (that should be a Mesh) has the TexturePaths or TextureImages ...
Definition textures.h:61
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48