Visual Computing Library
All Classes Functions Variables Typedefs Enumerations Friends Modules Pages Concepts
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_LOAD_SAVE_PLY_DETAIL_EXTRA_H
24#define VCL_LOAD_SAVE_PLY_DETAIL_EXTRA_H
25
26#include "header.h"
27
28#include <vclib/exceptions/io.h>
29#include <vclib/io/read.h>
30#include <vclib/load_save/settings.h>
31#include <vclib/mesh/requirements.h>
32#include <vclib/misc/logger.h>
33#include <vclib/space/core/texture.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 (HasTexturePaths<MeshType>) {
45 for (const std::string& str : header.textureFileNames()) {
46 mesh.pushTexturePath(str);
47 if constexpr (HasTextureImages<MeshType>) {
48 uint k = mesh.textureNumber() - 1;
49 if (settings.loadTextureImages) {
50 bool b =
51 mesh.texture(k).image().load(mesh.meshBasePath() + str);
52 if (!b) {
53 log.log(
54 "Cannot load texture " + str, LogType::WARNING_LOG);
55 }
56 }
57 }
58 }
59 }
60}
61
62template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
63void writePlyTextures(
64 PlyHeader& header,
65 const MeshType& mesh,
66 LogType& log,
67 const SaveSettings& settings)
68{
69 if constexpr (HasTexturePaths<MeshType>) {
70 uint k = 0;
71 for (const std::string& str : mesh.texturePaths()) {
72 header.pushTextureFileName(str);
73 k++;
74 if constexpr (HasTextureImages<MeshType>) {
75 if (settings.saveTextureImages) {
76 try {
77 mesh.texture(k).image().save(mesh.meshBasePath() + str);
78 }
79 catch (const std::runtime_error& e) {
80 log.log(e.what(), LogType::WARNING_LOG);
81 }
82 }
83 }
84 }
85 }
86}
87
88template<LoggerConcept LogType>
89void readPlyUnknownElement(
90 std::istream& file,
91 const PlyHeader& header,
92 PlyElement el,
93 LogType& log)
94{
95 log.startProgress("Reading unknown elements", el.numberElements);
96
97 if (header.format() == ply::ASCII) {
98 for (uint i = 0; i < el.numberElements; ++i) {
99 readAndTokenizeNextNonEmptyLine(file);
100 log.progress(i);
101 }
102 }
103 else {
104 for (uint i = 0; i < el.numberElements; ++i) {
105 for (const PlyProperty& p : el.properties) {
106 if (p.list) {
107 uint s = io::readPrimitiveType<int>(file, p.listSizeType);
108 for (uint i = 0; i < s; ++i)
109 io::readPrimitiveType<int>(file, p.type);
110 }
111 else {
112 io::readPrimitiveType<int>(file, p.type);
113 }
114 }
115 log.progress(i);
116 }
117 }
118
119 log.endProgress();
120}
121
122} // namespace vcl::detail
123
124#endif // VCL_LOAD_SAVE_PLY_DETAIL_EXTRA_H
NullLogger nullLogger
The nullLogger object is an object of type NullLogger that is used as default argument in the functio...
Definition null_logger.h:125