Visual Computing Library
Loading...
Searching...
No Matches
save.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_SAVE_H
24#define VCL_LOAD_SAVE_PLY_SAVE_H
25
26#include "detail/edge.h"
27#include "detail/extra.h"
28#include "detail/face.h"
29#include "detail/vertex.h"
30
31#include <vclib/exceptions/io.h>
32#include <vclib/load_save/settings.h>
33#include <vclib/misc/logger.h>
34
35namespace vcl {
36
37template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
38void savePly(
39 const MeshType& m,
40 std::ostream& fp,
41 LogType& log = nullLogger,
42 const SaveSettings& settings = SaveSettings())
43{
44 using namespace detail;
45 MeshInfo meshInfo(m);
46
47 // make sure that the given info contains only components that are actually
48 // available in the mesh. meshInfo will contain the intersection between the
49 // components that the user wants to save and the components that are
50 // available in the mesh.
51 if (!settings.info.isEmpty())
52 meshInfo = settings.info.intersect(meshInfo);
53
54 PlyHeader header(
55 settings.binary ? ply::BINARY_LITTLE_ENDIAN : ply::ASCII, meshInfo);
56 header.setNumberVertices(m.vertexNumber());
57
58 if constexpr (HasFaces<MeshType>) {
59 if (header.hasFaces()) {
60 header.setNumberFaces(m.faceNumber());
61 }
62 }
63 if constexpr (HasEdges<MeshType>) {
64 if (header.hasEdges()) {
65 header.setNumberEdges(m.edgeNumber());
66 }
67 }
68 writePlyTextures(header, m, log, settings);
69
70 // this should never happen
71 if (!header.isValid())
72 throw std::runtime_error("Ply Header not valid.");
73
74 fp << header.toString();
75
76 writePlyVertices(fp, header, m);
77
78 if constexpr (HasFaces<MeshType>) {
79 if (header.hasFaces()) {
80 writePlyFaces(fp, header, m);
81 }
82 }
83
84 if constexpr (HasEdges<MeshType>) {
85 if (header.hasEdges()) {
86 writePlyEdges(fp, header, m);
87 }
88 }
89}
90
91template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
92void savePly(
93 const MeshType& m,
94 std::ostream& fp,
95 const SaveSettings& settings,
96 LogType& log = nullLogger)
97{
98 savePly(m, fp, log, settings);
99}
100
101template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
102void savePly(
103 const MeshType& m,
104 const std::string& filename,
105 LogType& log = nullLogger,
106 const SaveSettings& settings = SaveSettings())
107{
108 std::ofstream fp = openOutputFileStream(filename, "ply");
109
110 savePly(m, fp, log, settings);
111}
112
113template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
114void savePly(
115 const MeshType& m,
116 const std::string& filename,
117 const SaveSettings& settings,
118 LogType& log = nullLogger)
119{
120 savePly(m, filename, log, settings);
121}
122
123} // namespace vcl
124
125#endif // VCL_LOAD_SAVE_PLY_SAVE_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