Visual Computing Library  devel
Loading...
Searching...
No Matches
texture_images.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_IMAGES_H
24#define VCL_MESH_COMPONENTS_TEXTURE_IMAGES_H
25
26#include "base/component.h"
27#include "concepts/textures.h"
28
29#include <vclib/space/core.h>
30#include <vclib/base.h>
31
32#include <string>
33#include <vector>
34
35namespace vcl::comp {
36
37namespace detail {
38
39struct TData
40{
41 std::vector<Texture> textures;
42 std::string meshPath;
43
44 auto paths()
45 {
46 return textures | std::views::transform([](Texture& t) -> std::string& {
47 return t.path();
48 });
49 }
50
51 auto paths() const
52 {
53 return textures | std::views::transform(
54 [](const Texture& t) -> const std::string& {
55 return t.path();
56 });
57 }
58
59 auto pathBegin() { return std::begin(paths()); }
60
61 auto pathBegin() const { return std::begin(paths()); }
62
63 auto pathEnd() { return std::end(paths()); }
64
65 auto pathEnd() const { return std::end(paths()); }
66};
67
68} // namespace detail
69
95 public Component<
96 TextureImages,
97 CompId::TEXTURE_IMAGES,
98 detail::TData,
99 void,
100 false,
101 false>
102{
103 using Base = Component<
105 CompId::TEXTURE_IMAGES,
106 detail::TData,
107 void,
108 false,
109 false>;
110
111public:
116
117 // iterators
118 using TextureIterator = std::vector<Texture>::iterator;
119 using ConstTextureIterator = std::vector<Texture>::const_iterator;
120
121 using TexFileNamesIterator =
122 decltype(std::declval<detail::TData>().pathBegin());
123 using ConstTexFileNamesIterator =
124 decltype(std::declval<const detail::TData>().pathBegin());
125
126 /* Constructors */
127
132 TextureImages() = default;
133
134 /* Member functions */
135
139 uint textureNumber() const { return texs().size(); }
140
146 const Texture& texture(uint i) const { return texs()[i]; }
147
153 Texture& texture(uint i) { return texs()[i]; }
154
165 const std::string& texturePath(uint i) const { return texs()[i].path(); }
166
177 std::string& texturePath(uint i) { return texs()[i].path(); }
178
182 const std::string& meshBasePath() const { return Base::data().meshPath; }
183
187 std::string& meshBasePath() { return Base::data().meshPath; }
188
195 uint indexOfTexturePath(const std::string& path) const
196 {
197 auto it =
198 std::find(Base::data().pathBegin(), Base::data().pathEnd(), path);
199 if (it != Base::data().pathEnd()) {
200 return static_cast<uint>(
201 std::distance(Base::data().pathBegin(), it));
202 }
203 return UINT_NULL;
204 }
205
209 void clearTextures() { texs().clear(); }
210
219
224 void pushTexture(const Texture& texture) { texs().push_back(texture); }
225
236 void pushTexturePath(const std::string& textPath)
237 {
238 texs().push_back(Texture());
239 texs().back().path() = textPath;
240 }
241
246 TextureIterator textureBegin() { return texs().begin(); }
247
252 TextureIterator textureEnd() { return texs().end(); }
253
259 ConstTextureIterator textureBegin() const { return texs().begin(); }
260
265 ConstTextureIterator textureEnd() const { return texs().end(); }
266
277 TexFileNamesIterator texturePathBegin() { return Base::data().pathBegin(); }
278
288 TexFileNamesIterator texturePathEnd() { return Base::data().pathEnd(); }
289
300 ConstTexFileNamesIterator texturePathBegin() const
301 {
302 return Base::data().pathBegin();
303 }
304
315 ConstTexFileNamesIterator texturePathEnd() const
316 {
317 return Base::data().pathEnd();
318 }
319
339
356 {
357 return View(textureBegin(), textureEnd());
358 }
359
380 auto texturePaths() { return Base::data().paths(); }
381
401 auto texturePaths() const { return Base::data().paths(); }
402
403protected:
404 // Component interface functions
405 template<typename Element>
406 void importFrom(const Element& e, bool = true)
407 {
408 if constexpr (HasTextureImages<Element>) {
409 texs() = e.texs();
410 meshBasePath() = e.meshBasePath();
411 }
412 else if constexpr (HasTexturePaths<Element>) {
413 texs().clear();
414 for (const auto& tpath : e.texturePaths()) {
416 }
417 meshBasePath() = e.meshBasePath();
418 }
419 }
420
421 void serialize(std::ostream& os) const
422 {
423 vcl::serialize(os, texs());
424 vcl::serialize(os, meshBasePath());
425 }
426
427 void deserialize(std::istream& is)
428 {
429 vcl::deserialize(is, texs());
430 vcl::deserialize(is, meshBasePath());
431 }
432
433private:
434 // members that allow to access the data
435 std::vector<Texture>& texs() { return Base::data().textures; }
436
437 const std::vector<Texture>& texs() const { return Base::data().textures; }
438};
439
440} // namespace vcl::comp
441
442#endif // VCL_MESH_COMPONENTS_TEXTURE_IMAGES_H
A class representing a box in N-dimensional space.
Definition box.h:46
The Element class.
Definition element.h:75
Definition texture.h:33
The View class is a simple class that stores and exposes two iterators begin and end.
Definition view.h:67
The TextureImages class represents a component that stores the textures used by a mesh....
Definition texture_images.h:102
auto texturePaths()
Returns a lightweigth view object that stores the begin and end iterators of the vector of texture pa...
Definition texture_images.h:380
ConstTextureIterator textureBegin() const
Returns a const iterator to the beginning of the vector of textures.
Definition texture_images.h:259
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_images.h:177
void pushTexturePath(const std::string &textPath)
Adds a texture to the vector of textures. The image of the texture is left empty.
Definition texture_images.h:236
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_images.h:165
std::string & meshBasePath()
Returns a reference to the mesh base path.
Definition texture_images.h:187
void clearTextures()
Clears the vector of textures.
Definition texture_images.h:209
TextureImages()=default
Initializes the component with an empty vector of textures and an empty string as mesh base path.
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_images.h:195
void clearTexturePaths()
Clears the vector of textures.
Definition texture_images.h:218
TexFileNamesIterator texturePathBegin()
Returns an iterator to the beginning of the vector of texture paths.
Definition texture_images.h:277
View< TextureIterator > textures()
Returns a lightweigth view object that stores the begin and end iterators of the vector of textures....
Definition texture_images.h:335
TexFileNamesIterator texturePathEnd()
Returns an iterator to the end of the vector of texture paths.
Definition texture_images.h:288
TextureIterator textureBegin()
Returns an iterator to the beginning of the vector of textures.
Definition texture_images.h:246
const Texture & texture(uint i) const
Returns the i-th texture of the mesh. The path of the texture is relative to the mesh base path.
Definition texture_images.h:146
TextureIterator textureEnd()
Returns an iterator to the end of the vector of textures.
Definition texture_images.h:252
ConstTexFileNamesIterator texturePathBegin() const
Returns a const iterator to the beginning of the vector of texture paths.
Definition texture_images.h:300
void pushTexture(const Texture &texture)
Adds a texture to the vector of textures.
Definition texture_images.h:224
ConstTextureIterator textureEnd() const
Returns a const iterator to the end of the vector of textures.
Definition texture_images.h:265
auto texturePaths() const
Returns a lightweigth const view object that stores the begin and end iterators of the vector of text...
Definition texture_images.h:401
View< ConstTextureIterator > textures() const
Returns a lightweigth const view object that stores the begin and end iterators of the vector of text...
Definition texture_images.h:355
ConstTexFileNamesIterator texturePathEnd() const
Returns a const iterator to the end of the vector of texture paths.
Definition texture_images.h:315
const std::string & meshBasePath() const
Returns the mesh base path.
Definition texture_images.h:182
Texture & texture(uint i)
Returns a reference to the i-th texture of the mesh. The path of the texture is relative to the mesh ...
Definition texture_images.h:153
uint textureNumber() const
Returns the number of texture of the mesh.
Definition texture_images.h:139
Concept that checks if a Mesh has the TexturePaths component.
Definition mesh_requirements.h:105
A concept that checks whether a type T (that should be a Mesh) has the TextureImages component (inher...
Definition textures.h:45
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