MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
mesh_io_pbrt.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 <stdexcept>
13#include <cerrno>
14#include <cstring>
15
16#include "util/exception.h"
17#include "mve/mesh_io_pbrt.h"
18
21
22void
23save_pbrt_mesh (TriangleMesh::ConstPtr mesh, std::string const& filename)
24{
25 if (mesh == nullptr)
26 throw std::invalid_argument("Null mesh given");
27 if (filename.empty())
28 throw std::invalid_argument("No filename given");
29
30 mve::TriangleMesh::VertexList const& verts(mesh->get_vertices());
31 mve::TriangleMesh::NormalList const& vnormals(mesh->get_vertex_normals());
32 mve::TriangleMesh::FaceList const& faces(mesh->get_faces());
33
34 /* Open output file. */
35 std::ofstream out(filename.c_str(), std::ios::binary);
36 if (!out.good())
37 throw util::FileException(filename, std::strerror(errno));
38
39 out << "Translate 0 0 0" << std::endl;
40 out << "Shape \"trianglemesh\"" << std::endl;
41
42 /* Issue vertices. */
43 out << "\"point P\" [" << std::endl;
44 for (std::size_t i = 0; i < verts.size(); ++i)
45 {
46 out << " "
47 << verts[i][0] << " "
48 << verts[i][1] << " "
49 << verts[i][2] << std::endl;
50 }
51 out << "]" << std::endl << std::endl;
52
53 /* Issue normals. */
54 if (vnormals.size() == verts.size())
55 {
56 out << "\"normal N\" [" << std::endl;
57 for (std::size_t i = 0; i < vnormals.size(); ++i)
58 {
59 out << " "
60 << vnormals[i][0] << " "
61 << vnormals[i][1] << " "
62 << vnormals[i][2] << std::endl;
63 }
64 out << "]" << std::endl << std::endl;
65 }
66
67 /* Issue face indices. */
68 out << "\"integer indices\" [" << std::endl;
69 for (std::size_t i = 0 ; i < faces.size() ; ++i)
70 {
71 if (!(i % 3))
72 out << " ";
73 else
74 out << " ";
75 out << faces[i];
76 if ((i % 3) == 2)
77 out << std::endl;
78 }
79 out << "]" << std::endl;
80
81 /* Close output file. */
82 out.close();
83}
84
85
std::vector< math::Vec3f > VertexList
Definition mesh.h:33
std::vector< math::Vec3f > NormalList
Definition mesh.h:95
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_pbrt_mesh(TriangleMesh::ConstPtr mesh, std::string const &filename)
Saves a PBRT compatible mesh from a triangle mesh.