MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
mesh_io_npts.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <cstring>
11#include <cerrno>
12#include <stdexcept>
13#include <fstream>
14#include <string>
15
16#include "util/exception.h"
17#include "math/vector.h"
18#include "mve/mesh.h"
19#include "mve/mesh_io_npts.h"
20
23
24TriangleMesh::Ptr
25load_npts_mesh (std::string const& filename, bool format_binary)
26{
27 if (filename.empty())
28 throw std::invalid_argument("No filename given");
29
30 std::ifstream input(filename.c_str());
31 if (!input.good())
32 throw util::FileException(filename, std::strerror(errno));
33
34 TriangleMesh::Ptr mesh = TriangleMesh::create();
35 TriangleMesh::VertexList& verts = mesh->get_vertices();
36 TriangleMesh::NormalList& vnorm = mesh->get_vertex_normals();
37
38 while (true)
39 {
40 math::Vec3f v, n;
41 if (format_binary)
42 {
43 input.read(reinterpret_cast<char*>(*v), sizeof(float) * 3);
44 input.read(reinterpret_cast<char*>(*n), sizeof(float) * 3);
45 }
46 else
47 {
48 for (int i = 0; i < 3; ++i)
49 input >> v[i];
50 for (int i = 0; i < 3; ++i)
51 input >> n[i];
52 }
53 if (input.eof())
54 break;
55
56 verts.push_back(v);
57 vnorm.push_back(n);
58 }
59
60 return mesh;
61}
62
63/* ---------------------------------------------------------------- */
64
65void
67 std::string const& filename, bool format_binary)
68{
69 if (mesh == nullptr || mesh->get_vertices().empty())
70 throw std::invalid_argument("Input mesh is empty");
71 if (filename.empty())
72 throw std::invalid_argument("No filename given");
73 if (mesh->get_vertex_normals().size() != mesh->get_vertices().size())
74 throw std::invalid_argument("No vertex normals given");
75
76 std::ofstream out(filename.c_str(), std::ios::binary);
77 if (!out.good())
78 throw util::FileException(filename, std::strerror(errno));
79
80 TriangleMesh::VertexList const& verts = mesh->get_vertices();
81 TriangleMesh::NormalList const& vnorm = mesh->get_vertex_normals();
82 for (std::size_t i = 0; i < verts.size(); ++i)
83 {
84 math::Vec3f const& v = verts[i];
85 math::Vec3f const& n = vnorm[i];
86 if (format_binary)
87 {
88 out.write(reinterpret_cast<char const*>(*v), sizeof(float) * 3);
89 out.write(reinterpret_cast<char const*>(*n), sizeof(float) * 3);
90 }
91 else
92 {
93 out << v[0] << " " << v[1] << " " << v[2] << " "
94 << n[0] << " " << n[1] << " " << n[2] << std::endl;
95 }
96 }
97 out.close();
98}
99
Vector class for arbitrary dimensions and types.
Definition vector.h:87
std::vector< math::Vec3f > VertexList
Definition mesh.h:33
std::vector< math::Vec3f > NormalList
Definition mesh.h:95
std::shared_ptr< TriangleMesh > Ptr
Definition mesh.h:92
std::shared_ptr< TriangleMesh const > ConstPtr
Definition mesh.h:93
Exception class for file exceptions with additional filename.
Definition exception.h:53
#define MVE_NAMESPACE_BEGIN
Definition defines.h:13
#define MVE_NAMESPACE_END
Definition defines.h:14
#define MVE_GEOM_NAMESPACE_END
Definition defines.h:20
#define MVE_GEOM_NAMESPACE_BEGIN
Definition defines.h:19
TriangleMesh::Ptr load_npts_mesh(std::string const &filename, bool format_binary)
Simple importer for Kazhdan's .npts ASCII and binary files.
void save_npts_mesh(TriangleMesh::ConstPtr mesh, std::string const &filename, bool format_binary)
Simple exporter for Kazhdan's .npts ASCII and binary files.