MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
mesh_io_smf.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 <iostream>
11#include <fstream>
12#include <sstream>
13#include <cerrno>
14#include <cstring>
15
16#include "util/exception.h"
17#include "mve/mesh_io_smf.h"
18
21
22TriangleMesh::Ptr
23load_smf_mesh (std::string const& filename)
24{
25 if (filename.empty())
26 throw std::invalid_argument("No filename given");
27
28 TriangleMesh::Ptr mesh = TriangleMesh::create();
29 TriangleMesh::VertexList& verts(mesh->get_vertices());
30 TriangleMesh::FaceList& faces(mesh->get_faces());
31
32 std::ifstream in(filename.c_str());
33 if (!in.good())
34 throw util::FileException(filename, std::strerror(errno));
35
36 while (!in.eof())
37 {
38 char row_type;
39 in >> row_type;
40 if (in.eof())
41 break;
42
43 switch (row_type)
44 {
45 case 'v':
46 {
48 in >> v[0] >> v[1] >> v[2];
49 verts.push_back(v);
50 break;
51 }
52
53 case 'f':
54 {
55 unsigned int vid;
56 in >> vid;
57 faces.push_back(vid - 1);
58 in >> vid;
59 faces.push_back(vid - 1);
60 in >> vid;
61 faces.push_back(vid - 1);
62 break;
63 }
64
65 default:
66 std::cerr << "Unknown element type '" << row_type
67 << "'. Skipping." << std::endl;
68 break;
69 }
70 }
71 in.close();
72
73 return mesh;
74}
75
76void
77save_smf_mesh (mve::TriangleMesh::ConstPtr mesh, std::string const& filename)
78{
79 if (mesh == nullptr)
80 throw std::invalid_argument("Null mesh given");
81 if (filename.empty())
82 throw std::invalid_argument("No filename given");
83
84 TriangleMesh::VertexList const& verts(mesh->get_vertices());
85 TriangleMesh::FaceList const& faces(mesh->get_faces());
86
87 /* Open output file. */
88 std::ofstream out(filename.c_str(), std::ios::binary);
89 if (!out.good())
90 throw util::FileException(filename, std::strerror(errno));
91
92 std::cout << "Writing SMF: " << verts.size() << " verts..." << std::flush;
93 for (std::size_t i = 0; i < verts.size(); ++i)
94 {
95 math::Vec3f const& v = verts[i];
96 out << "v " << v[0] << " " << v[1] << " " << v[2] << "\n";
97 }
98
99 std::cout << " " << (faces.size() / 3) << " faces..." << std::flush;
100 for (std::size_t i = 0; i < faces.size(); i += 3)
101 {
102 out << "f " << faces[i + 0] + 1 << " "
103 << faces[i + 1] + 1 << " "
104 << faces[i + 2] + 1 << "\n";
105 }
106 std::cout << " done." << std::endl;
107
108 out.close();
109}
110
Vector class for arbitrary dimensions and types.
Definition vector.h:87
std::vector< math::Vec3f > VertexList
Definition mesh.h:33
std::shared_ptr< TriangleMesh > Ptr
Definition mesh.h:92
std::vector< VertexID > FaceList
Definition mesh.h:97
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
void save_smf_mesh(mve::TriangleMesh::ConstPtr mesh, std::string const &filename)
Saves a triangle mesh to a file in SMF file format.
TriangleMesh::Ptr load_smf_mesh(std::string const &filename)
Loads a triangle mesh from a SMF file format.