Visual Computing Library  devel
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_IO_MESH_PLY_SAVE_H
24#define VCL_IO_MESH_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/io/mesh/settings.h>
32
33namespace vcl {
34
35template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
36void savePly(
37 const MeshType& m,
38 std::ostream& fp,
39 const std::string& fileBasePath,
40 const SaveSettings& settings = SaveSettings(),
41 LogType& log = nullLogger)
42{
43 using namespace detail;
44 MeshInfo meshInfo(m);
45
46 // make sure that the given info contains only components that are actually
47 // available in the mesh. meshInfo will contain the intersection between the
48 // components that the user wants to save and the components that are
49 // available in the mesh.
50 if (!settings.info.isEmpty())
51 meshInfo = settings.info.intersect(meshInfo);
52
53 PlyHeader header(
54 settings.binary ? ply::BINARY_LITTLE_ENDIAN : ply::ASCII, meshInfo);
55 header.setNumberVertices(m.vertexNumber());
56
57 if constexpr (HasFaces<MeshType>) {
58 if (header.hasFaces()) {
59 header.setNumberFaces(m.faceNumber());
60 }
61 }
62 if constexpr (HasEdges<MeshType>) {
63 if (header.hasEdges()) {
64 header.setNumberEdges(m.edgeNumber());
65 }
66 }
67 writePlyTextures(header, m, fileBasePath, log, settings);
68
69 // this should never happen
70 if (!header.isValid())
71 throw std::runtime_error("Ply Header not valid.");
72
73 fp << header.toString();
74
75 writePlyVertices(fp, header, m);
76
77 if constexpr (HasFaces<MeshType>) {
78 if (header.hasFaces()) {
79 writePlyFaces(fp, header, m);
80 }
81 }
82
83 if constexpr (HasEdges<MeshType>) {
84 if (header.hasEdges()) {
85 writePlyEdges(fp, header, m);
86 }
87 }
88}
89
90template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
91void savePly(
92 const MeshType& m,
93 const std::string& filename,
94 const SaveSettings& settings = SaveSettings(),
95 LogType& log = nullLogger)
96{
97 std::ofstream fp = openOutputFileStream(filename, "ply");
98
99 std::string basePath = FileInfo::pathWithoutFileName(filename);
100
101 savePly(m, fp, basePath, settings, log);
102}
103
104} // namespace vcl
105
106#endif // VCL_IO_MESH_PLY_SAVE_H
static std::string pathWithoutFileName(const std::string &fullpath)
Get the path of a file.
Definition file_info.h:200
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