PolyFEM
Loading...
Searching...
No Matches
OBJWriter.cpp
Go to the documentation of this file.
1// Modified version of read_obj from libigl to include reading polyline elements
2// as edges.
3//
4// Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla Public License
7// v. 2.0. If a copy of the MPL was not distributed with this file, You can
8// obtain one at http://mozilla.org/MPL/2.0/.
9
10#include "OBJWriter.hpp"
11
13
14#include <fstream>
15
16namespace polyfem::io
17{
18 bool OBJWriter::write(const std::string &path, const Eigen::MatrixXd &v, const Eigen::MatrixXi &e, const Eigen::MatrixXi &f)
19 {
20 const Eigen::IOFormat OBJ_VERTEX_FORMAT(
21 /*precision=*/Eigen::FullPrecision,
22 /*flags=*/Eigen::DontAlignCols,
23 /*coeffSeparator=*/" ",
24 /*rowSeparator=*/"",
25 /*rowPrefix=*/"v ",
26 /*rowSuffix=*/v.cols() == 2 ? " 0\n" : "\n",
27 /*matPrefix=*/"",
28 /*fill=*/"");
29
30 std::ofstream obj(path, std::ios::out);
31 if (!obj.is_open())
32 return false;
33
34 obj << fmt::format(
35 "# Vertices: {:d}\n# Edges: {:d}\n# Faces: {:d}\n",
36 v.rows(), e.rows(), f.rows());
37
38 for (int i = 0; i < v.rows(); ++i)
39 obj << v.row(i).format(OBJ_VERTEX_FORMAT);
40
41 for (int i = 0; i < e.rows(); ++i)
42 obj << fmt::format("l {} {}\n", e(i, 0) + 1, e(i, 1) + 1);
43
44 for (int i = 0; i < f.rows(); ++i)
45 obj << fmt::format("f {} {} {}\n", f(i, 0) + 1, f(i, 1) + 1, f(i, 2) + 1);
46
47 return true;
48 }
49} // namespace polyfem::io
static bool write(const std::string &path, const Eigen::MatrixXd &v, const Eigen::MatrixXi &e, const Eigen::MatrixXi &f)
Definition OBJWriter.cpp:18