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 (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 mesh.texture(k).image() =
51 loadImage(mesh.meshBasePath() + str);
52 if (mesh.texture(k).image().isNull()) {
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 const std::string& basePath,
67 LogType& log,
68 const SaveSettings& settings)
69{
70 if constexpr (HasTexturePaths<MeshType>) {
71 for (uint k = 0; const std::string& str : mesh.texturePaths()) {
72 header.pushTextureFileName(str);
73 if constexpr (HasTextureImages<MeshType>) {
74 if (settings.saveTextureImages) {
75 try {
76 saveImage(mesh.texture(k).image(), basePath + str);
77 }
78 catch (const std::runtime_error& e) {
79 log.log(e.what(), LogType::WARNING_LOG);
80 }
81 }
82 }
83 ++k;
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_IO_MESH_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:123