Visual Computing Library  devel
Loading...
Searching...
No Matches
extra.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2026 *
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 addMaterialsFromHeaderTextures(
39 const PlyHeader& header,
40 MeshType& mesh,
41 LogType& log = nullLogger)
42{
43 if constexpr (HasMaterials<MeshType>) {
44 for (const std::string& str : header.textureFileNames()) {
45 Material mat;
46 mat.name() = FileInfo::fileNameWithExtension(str);
47 mat.baseColorTextureDescriptor().path() = str;
48 mesh.pushMaterial(mat);
49 }
50 }
51}
52
53template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
54void addTexturesToHeader(PlyHeader& header, const MeshType& mesh)
55{
56 if constexpr (HasMaterials<MeshType>) {
57 for (const Material& mat : mesh.materials()) {
58 header.pushTextureFileName(mat.baseColorTextureDescriptor().path());
59 }
60 }
61}
62
63template<LoggerConcept LogType>
64void readPlyUnknownElement(
65 std::istream& file,
66 const PlyHeader& header,
67 PlyElement el,
68 LogType& log)
69{
70 log.startProgress("Reading unknown elements", el.elementCount);
71
72 if (header.format() == ply::ASCII) {
73 for (uint i = 0; i < el.elementCount; ++i) {
74 readAndTokenizeNextNonEmptyLine(file);
75 log.progress(i);
76 }
77 }
78 else {
79 for (uint i = 0; i < el.elementCount; ++i) {
80 for (const PlyProperty& p : el.properties) {
81 if (p.list) {
82 uint s = io::readPrimitiveType<int>(file, p.listSizeType);
83 for (uint i = 0; i < s; ++i)
84 io::readPrimitiveType<int>(file, p.type);
85 }
86 else {
87 io::readPrimitiveType<int>(file, p.type);
88 }
89 }
90 log.progress(i);
91 }
92 }
93
94 log.endProgress();
95}
96
97template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
98void readPlyMaterialIndexPostProcessing(
99 MeshType& mesh,
100 MeshInfo& loadedInfo,
101 const LoadSettings& settings)
102{
103 if constexpr (HasMaterials<MeshType>) {
104 if (mesh.materialCount() > 0) {
105 if (loadedInfo.hasPerVertexTexCoord() &&
106 !loadedInfo.hasPerVertexMaterialIndex()) {
107 if constexpr (HasPerVertexMaterialIndex<MeshType>) {
108 if (settings.enableOptionalComponents) {
109 enableIfPerVertexMaterialIndexOptional(mesh);
110 loadedInfo.setPerVertexMaterialIndex();
111 }
112 if (loadedInfo.hasPerVertexMaterialIndex()) {
113 for (auto& v : mesh.vertices()) {
114 v.materialIndex() = 0;
115 }
116 }
117 }
118 }
119 if (loadedInfo.hasPerFaceWedgeTexCoords() &&
120 !loadedInfo.hasPerFaceMaterialIndex()) {
121 if constexpr (HasPerFaceMaterialIndex<MeshType>) {
122 if (settings.enableOptionalComponents) {
124 loadedInfo.setPerFaceMaterialIndex();
125 }
126 if (loadedInfo.hasPerFaceMaterialIndex()) {
127 for (auto& f : mesh.faces()) {
128 f.materialIndex() = 0;
129 }
130 }
131 }
132 }
133 }
134 }
135}
136
137} // namespace vcl::detail
138
139#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