Visual Computing Library
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 "bases/component.h"
27
28#include <vclib/concepts/mesh/components/texture_images.h>
29#include <vclib/concepts/mesh/components/texture_paths.h>
30#include <vclib/space/core/texture.h>
31#include <vclib/types/view.h>
32
33#include <string>
34#include <vector>
35
36namespace vcl::comp {
37
38namespace detail {
39
40struct TData
41{
42 std::vector<Texture> textures;
43 std::string meshPath;
44
45 auto paths()
46 {
47 return textures | std::views::transform([](Texture& t) -> std::string& {
48 return t.path();
49 });
50 }
51
52 auto paths() const
53 {
54 return textures | std::views::transform(
55 [](const Texture& t) -> const std::string& {
56 return t.path();
57 });
58 }
59
60 auto pathBegin() { return std::begin(paths()); }
61
62 auto pathBegin() const { return std::begin(paths()); }
63
64 auto pathEnd() { return std::end(paths()); }
65
66 auto pathEnd() const { return std::end(paths()); }
67};
68
69} // namespace detail
70
96 public Component<
97 TextureImages,
98 CompId::TEXTURE_IMAGES,
99 detail::TData,
100 void,
101 false,
102 false>
103{
104 using Base = Component<
106 CompId::TEXTURE_IMAGES,
107 detail::TData,
108 void,
109 false,
110 false>;
111
112public:
117
118 // iterators
119 using TextureIterator = std::vector<Texture>::iterator;
120 using ConstTextureIterator = std::vector<Texture>::const_iterator;
121
122 using TexFileNamesIterator =
123 decltype(std::declval<detail::TData>().pathBegin());
124 using ConstTexFileNamesIterator =
125 decltype(std::declval<const detail::TData>().pathBegin());
126
127 /* Constructors */
128
133 TextureImages() = default;
134
135 /* Member functions */
136
140 uint textureNumber() const { return texs().size(); }
141
147 const Texture& texture(uint i) const { return texs()[i]; }
148
154 Texture& texture(uint i) { return texs()[i]; }
155
166 const std::string& texturePath(uint i) const { return texs()[i].path(); }
167
178 std::string& texturePath(uint i) { return texs()[i].path(); }
179
183 const std::string& meshBasePath() const { return Base::data().meshPath; }
184
188 std::string& meshBasePath() { return Base::data().meshPath; }
189
193 void clearTextures() { texs().clear(); }
194
203
208 void pushTexture(const Texture& texture) { texs().push_back(texture); }
209
220 void pushTexturePath(const std::string& textPath)
221 {
222 texs().push_back(Texture());
223 texs().back().path() = textPath;
224 }
225
230 TextureIterator textureBegin() { return texs().begin(); }
231
236 TextureIterator textureEnd() { return texs().end(); }
237
243 ConstTextureIterator textureBegin() const { return texs().begin(); }
244
249 ConstTextureIterator textureEnd() const { return texs().end(); }
250
261 TexFileNamesIterator texturePathBegin() { return Base::data().pathBegin(); }
262
272 TexFileNamesIterator texturePathEnd() { return Base::data().pathEnd(); }
273
284 ConstTexFileNamesIterator texturePathBegin() const
285 {
286 return Base::data().pathBegin();
287 }
288
299 ConstTexFileNamesIterator texturePathEnd() const
300 {
301 return Base::data().pathEnd();
302 }
303
323
340 {
341 return View(textureBegin(), textureEnd());
342 }
343
364 auto texturePaths() { return Base::data().paths(); }
365
385 auto texturePaths() const { return Base::data().paths(); }
386
387protected:
388 // Component interface functions
389 template<typename Element>
390 void importFrom(const Element& e, bool = true)
391 {
392 if constexpr (HasTextureImages<Element>) {
393 texs() = e.texs();
394 meshBasePath() = e.meshBasePath();
395 }
396 else if constexpr (HasTexturePaths<Element>) {
397 texs().clear();
398 for (const auto& tpath : e.texturePaths()) {
400 }
401 meshBasePath() = e.meshBasePath();
402 }
403 }
404
405 void serialize(std::ostream& os) const
406 {
407 vcl::serialize(os, texs());
408 vcl::serialize(os, meshBasePath());
409 }
410
411 void deserialize(std::istream& is)
412 {
413 vcl::deserialize(is, texs());
414 vcl::deserialize(is, meshBasePath());
415 }
416
417private:
418 // members that allow to access the data
419 std::vector<Texture>& texs() { return Base::data().textures; }
420
421 const std::vector<Texture>& texs() const { return Base::data().textures; }
422};
423
424} // namespace vcl::comp
425
426#endif // VCL_MESH_COMPONENTS_TEXTURE_IMAGES_H
The Element class.
Definition element.h:57
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Definition texture.h:34
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:103
auto texturePaths()
Returns a lightweigth view object that stores the begin and end iterators of the vector of texture pa...
Definition texture_images.h:364
ConstTextureIterator textureBegin() const
Returns a const iterator to the beginning of the vector of textures.
Definition texture_images.h:243
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:178
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:220
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:166
std::string & meshBasePath()
Returns a reference to the mesh base path.
Definition texture_images.h:188
void clearTextures()
Clears the vector of textures.
Definition texture_images.h:193
TextureImages()=default
Initializes the component with an empty vector of textures and an empty string as mesh base path.
void clearTexturePaths()
Clears the vector of textures.
Definition texture_images.h:202
TexFileNamesIterator texturePathBegin()
Returns an iterator to the beginning of the vector of texture paths.
Definition texture_images.h:261
View< TextureIterator > textures()
Returns a lightweigth view object that stores the begin and end iterators of the vector of textures....
Definition texture_images.h:319
TexFileNamesIterator texturePathEnd()
Returns an iterator to the end of the vector of texture paths.
Definition texture_images.h:272
TextureIterator textureBegin()
Returns an iterator to the beginning of the vector of textures.
Definition texture_images.h:230
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:147
TextureIterator textureEnd()
Returns an iterator to the end of the vector of textures.
Definition texture_images.h:236
ConstTexFileNamesIterator texturePathBegin() const
Returns a const iterator to the beginning of the vector of texture paths.
Definition texture_images.h:284
void pushTexture(const Texture &texture)
Adds a texture to the vector of textures.
Definition texture_images.h:208
ConstTextureIterator textureEnd() const
Returns a const iterator to the end of the vector of textures.
Definition texture_images.h:249
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:385
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:339
ConstTexFileNamesIterator texturePathEnd() const
Returns a const iterator to the end of the vector of texture paths.
Definition texture_images.h:299
const std::string & meshBasePath() const
Returns the mesh base path.
Definition texture_images.h:183
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:154
uint textureNumber() const
Returns the number of texture of the mesh.
Definition texture_images.h:140
Concept that checks if a Mesh has the TexturePaths component.
Definition per_mesh.h:112
HasTextureImages concept is satisfied only if a Mesh class provides the member functions specified in...
Definition texture_images.h:43