22#ifndef EOS_WRITE_OBJ_HPP
23#define EOS_WRITE_OBJ_HPP
25#include "eos/core/Mesh.hpp"
46 assert(mesh.
tvi.size() == mesh.
tti.size() || mesh.
tti.empty());
48 std::ofstream obj_file(filename);
52 for (std::size_t i = 0; i < mesh.
vertices.size(); ++i)
59 for (std::size_t i = 0; i < mesh.
vertices.size(); ++i)
63 << mesh.
colors[i][2] << std::endl;
71 obj_file <<
"vt " << tc[0] <<
" " << 1.0f - tc[1] << std::endl;
76 for (std::size_t i = 0; i < mesh.
tvi.size(); ++i)
78 const auto& vi = mesh.
tvi[i];
82 obj_file <<
"f " << vi[0] + 1 <<
" " << vi[1] + 1 <<
" " << vi[2] + 1 << std::endl;
89 obj_file <<
"f " << vi[0] + 1 <<
"/" << vi[0] + 1 <<
" " << vi[1] + 1 <<
"/" << vi[1] + 1
90 <<
" " << vi[2] + 1 <<
"/" << vi[2] + 1 << std::endl;
94 const auto& ti = mesh.
tti[i];
95 obj_file <<
"f " << vi[0] + 1 <<
"/" << ti[0] + 1 <<
" " << vi[1] + 1 <<
"/" << ti[1] + 1
96 <<
" " << vi[2] + 1 <<
"/" << ti[2] + 1 << std::endl;
119 assert(mesh.
tvi.size() == mesh.
tti.size() || mesh.
tti.empty());
121 if (filename.at(filename.size() - 4) !=
'.')
123 throw std::runtime_error(
124 "Error in given filename: Expected a dot and a 3-letter extension at the end (i.e. '.obj'). " +
129 const auto get_filename = [](
const std::string& path) {
130 auto last_slash = path.find_last_of(
"/\\");
131 if (last_slash == std::string::npos)
135 return path.substr(last_slash + 1, path.size());
138 std::ofstream obj_file(filename);
140 std::string mtl_filename(filename);
142 mtl_filename.replace(std::end(mtl_filename) - 4, std::end(mtl_filename),
".mtl");
144 obj_file <<
"mtllib " << get_filename(mtl_filename) << std::endl;
149 for (std::size_t i = 0; i < mesh.
vertices.size(); ++i)
151 obj_file <<
"v " << mesh.
vertices[i][0] <<
" " << mesh.
vertices[i][1] <<
" "
152 << mesh.
vertices[i][2] <<
" " << std::endl;
156 for (std::size_t i = 0; i < mesh.
vertices.size(); ++i)
158 obj_file <<
"v " << mesh.
vertices[i][0] <<
" " << mesh.
vertices[i][1] <<
" "
160 << mesh.
colors[i][2] <<
" " << std::endl;
165 for (std::size_t i = 0; i < mesh.
texcoords.size(); ++i)
167 obj_file <<
"vt " << mesh.
texcoords[i][0] <<
" " << 1.0f - mesh.
texcoords[i][1] << std::endl;
172 obj_file <<
"usemtl FaceTexture" << std::endl;
174 for (std::size_t i = 0; i < mesh.
tvi.size(); ++i)
176 const auto& vi = mesh.
tvi[i];
178 if (mesh.
tti.empty())
182 obj_file <<
"f " << vi[0] + 1 <<
"/" << vi[0] + 1 <<
" " << vi[1] + 1 <<
"/" << vi[1] + 1 <<
" "
183 << vi[2] + 1 <<
"/" << vi[2] + 1 << std::endl;
187 const auto& ti = mesh.
tti[i];
188 obj_file <<
"f " << vi[0] + 1 <<
"/" << ti[0] + 1 <<
" " << vi[1] + 1 <<
"/" << ti[1] + 1 <<
" "
189 << vi[2] + 1 <<
"/" << ti[2] + 1 << std::endl;
193 std::ofstream mtl_file(mtl_filename);
194 std::string texture_filename(filename);
196 texture_filename.replace(std::end(texture_filename) - 4, std::end(texture_filename),
".texture.png");
198 mtl_file <<
"newmtl FaceTexture" << std::endl;
199 mtl_file <<
"map_Kd " << get_filename(texture_filename) << std::endl;
void write_obj(const Mesh &mesh, std::string filename)
Writes the given Mesh to an obj file that for example can be read by MeshLab.
Definition: write_obj.hpp:43
void write_textured_obj(const Mesh &mesh, std::string filename)
Writes an obj file of the given Mesh, including texture coordinates, and an mtl file containing a ref...
Definition: write_obj.hpp:115
Namespace containing all of eos's 3D model fitting functionality.
This class represents a 3D mesh consisting of vertices, vertex colour information and texture coordin...
Definition: Mesh.hpp:45
std::vector< Eigen::Vector3f > colors
Colour information for each vertex. Expected to be in RGB order.
Definition: Mesh.hpp:47
std::vector< Eigen::Vector3f > vertices
3D vertex positions.
Definition: Mesh.hpp:46
std::vector< std::array< int, 3 > > tvi
Triangle vertex indices.
Definition: Mesh.hpp:50
std::vector< std::array< int, 3 > > tti
Triangle texture indices.
Definition: Mesh.hpp:52
std::vector< Eigen::Vector2f > texcoords
Texture coordinates.
Definition: Mesh.hpp:48