Visual Computing Library  devel
Loading...
Searching...
No Matches
extra.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_IO_MESH_PLY_DETAIL_EXTRA_H
24#define VCL_IO_MESH_PLY_DETAIL_EXTRA_H
25
26#include "header.h"
27
28#include <vclib/io/image.h>
29#include <vclib/io/mesh/settings.h>
30#include <vclib/io/read.h>
31
32#include <vclib/mesh.h>
33#include <vclib/space/core.h>
34
35namespace vcl::detail {
36
37template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
38void readPlyTextures(
39 const PlyHeader& header,
40 MeshType& mesh,
41 LogType& log = nullLogger,
42 const LoadSettings& settings = LoadSettings())
43{
44 if constexpr (HasMaterials<MeshType>) {
45 for (const std::string& str : header.textureFileNames()) {
46 Material mat;
47 mat.name() = FileInfo::fileNameWithExtension(str);
48 mat.baseColorTextureDescriptor().path() = str;
49 if (settings.loadTextureImages) {
50 Image img = loadImage(mesh.meshBasePath() + str);
51 if (img.isNull()) {
52 log.log("Cannot load texture " + str, LogType::WARNING_LOG);
53 }
54 else {
55 mesh.pushTextureImage(str, std::move(img));
56 }
57 }
58 mesh.pushMaterial(mat);
59 }
60 }
61}
62
63template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
64void writePlyTextures(
65 PlyHeader& header,
66 const MeshType& mesh,
67 const std::string& basePath,
68 LogType& log,
69 const SaveSettings& settings)
70{
71 if constexpr (HasMaterials<MeshType>) {
72 for (uint k = 0; const Material& mat : mesh.materials()) {
73 header.pushTextureFileName(mat.baseColorTextureDescriptor().path());
74 if (settings.saveTextureImages) {
75 const Image& img =
76 mesh.textureImage(mat.baseColorTextureDescriptor().path());
77 if (img.isNull()) {
78 log.log(
79 "Cannot save empty texture " +
80 mat.baseColorTextureDescriptor().path(),
81 LogType::WARNING_LOG);
82 }
83 else {
84 try {
85 saveImage(
86 img,
87 basePath + mat.baseColorTextureDescriptor().path());
88 }
89 catch (const std::runtime_error& e) {
90 log.log(e.what(), LogType::WARNING_LOG);
91 }
92 }
93 }
94 ++k;
95 }
96 }
97}
98
99template<LoggerConcept LogType>
100void readPlyUnknownElement(
101 std::istream& file,
102 const PlyHeader& header,
103 PlyElement el,
104 LogType& log)
105{
106 log.startProgress("Reading unknown elements", el.numberElements);
107
108 if (header.format() == ply::ASCII) {
109 for (uint i = 0; i < el.numberElements; ++i) {
110 readAndTokenizeNextNonEmptyLine(file);
111 log.progress(i);
112 }
113 }
114 else {
115 for (uint i = 0; i < el.numberElements; ++i) {
116 for (const PlyProperty& p : el.properties) {
117 if (p.list) {
118 uint s = io::readPrimitiveType<int>(file, p.listSizeType);
119 for (uint i = 0; i < s; ++i)
120 io::readPrimitiveType<int>(file, p.type);
121 }
122 else {
123 io::readPrimitiveType<int>(file, p.type);
124 }
125 }
126 log.progress(i);
127 }
128 }
129
130 log.endProgress();
131}
132
133template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
134void readPlyMaterialIndexPostProcessing(
135 MeshType& mesh,
136 MeshInfo& loadedInfo,
137 const LoadSettings& settings)
138{
139 if constexpr (HasMaterials<MeshType>) {
140 if (mesh.materialsNumber() > 0) {
141 if (loadedInfo.hasPerVertexTexCoord() &&
142 !loadedInfo.hasPerVertexMaterialIndex()) {
143 if constexpr (HasPerVertexMaterialIndex<MeshType>) {
144 if (settings.enableOptionalComponents) {
145 enableIfPerVertexMaterialIndexOptional(mesh);
146 loadedInfo.setPerVertexMaterialIndex();
147 }
148 if (loadedInfo.hasPerVertexMaterialIndex()) {
149 for (auto& v : mesh.vertices()) {
150 v.materialIndex() = 0;
151 }
152 }
153 }
154 }
155 if (loadedInfo.hasPerFaceWedgeTexCoords() &&
156 !loadedInfo.hasPerFaceMaterialIndex()) {
157 if constexpr (HasPerFaceMaterialIndex<MeshType>) {
158 if (settings.enableOptionalComponents) {
160 loadedInfo.setPerFaceMaterialIndex();
161 }
162 if (loadedInfo.hasPerFaceMaterialIndex()) {
163 for (auto& f : mesh.faces()) {
164 f.materialIndex() = 0;
165 }
166 }
167 }
168 }
169 }
170 }
171}
172
173} // namespace vcl::detail
174
175#endif // VCL_IO_MESH_PLY_DETAIL_EXTRA_H
static std::string fileNameWithExtension(const std::string &fullpath)
Get the filename with extension of a file.
Definition file_info.h:240
NullLogger nullLogger
The nullLogger object is an object of type NullLogger that is used as default argument in the functio...
Definition null_logger.h:123
bool enableIfPerFaceMaterialIndexOptional(MeshType &m)
If the input mesh has a FaceContainer, and the Face Element has a MaterialIndex Component,...
Definition face_requirements.h:633
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:84
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:92