170 lines
8.2 KiB
C++
170 lines
8.2 KiB
C++
|
// This file is part of libigl, a simple c++ geometry processing library.
|
||
|
//
|
||
|
// Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
|
||
|
//
|
||
|
// This Source Code Form is subject to the terms of the Mozilla Public License
|
||
|
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||
|
// obtain one at http://mozilla.org/MPL/2.0/.
|
||
|
#include "writeOBJ.h"
|
||
|
|
||
|
#include <iostream>
|
||
|
#include <limits>
|
||
|
#include <iomanip>
|
||
|
#include <fstream>
|
||
|
#include <cstdio>
|
||
|
#include <cassert>
|
||
|
|
||
|
template <
|
||
|
typename DerivedV,
|
||
|
typename DerivedF,
|
||
|
typename DerivedCN,
|
||
|
typename DerivedFN,
|
||
|
typename DerivedTC,
|
||
|
typename DerivedFTC>
|
||
|
IGL_INLINE bool igl::writeOBJ(
|
||
|
const std::string str,
|
||
|
const Eigen::MatrixBase<DerivedV>& V,
|
||
|
const Eigen::MatrixBase<DerivedF>& F,
|
||
|
const Eigen::MatrixBase<DerivedCN>& CN,
|
||
|
const Eigen::MatrixBase<DerivedFN>& FN,
|
||
|
const Eigen::MatrixBase<DerivedTC>& TC,
|
||
|
const Eigen::MatrixBase<DerivedFTC>& FTC)
|
||
|
{
|
||
|
FILE * obj_file = fopen(str.c_str(),"w");
|
||
|
if(NULL==obj_file)
|
||
|
{
|
||
|
printf("IOError: %s could not be opened for writing...",str.c_str());
|
||
|
return false;
|
||
|
}
|
||
|
// Loop over V
|
||
|
for(int i = 0;i<(int)V.rows();i++)
|
||
|
{
|
||
|
fprintf(obj_file,"v");
|
||
|
for(int j = 0;j<(int)V.cols();++j)
|
||
|
{
|
||
|
fprintf(obj_file," %0.17g", V(i,j));
|
||
|
}
|
||
|
fprintf(obj_file,"\n");
|
||
|
}
|
||
|
bool write_N = CN.rows() >0;
|
||
|
|
||
|
if(write_N)
|
||
|
{
|
||
|
for(int i = 0;i<(int)CN.rows();i++)
|
||
|
{
|
||
|
fprintf(obj_file,"vn %0.17g %0.17g %0.17g\n",
|
||
|
CN(i,0),
|
||
|
CN(i,1),
|
||
|
CN(i,2)
|
||
|
);
|
||
|
}
|
||
|
fprintf(obj_file,"\n");
|
||
|
}
|
||
|
|
||
|
bool write_texture_coords = TC.rows() >0;
|
||
|
|
||
|
if(write_texture_coords)
|
||
|
{
|
||
|
for(int i = 0;i<(int)TC.rows();i++)
|
||
|
{
|
||
|
fprintf(obj_file, "vt %0.17g %0.17g\n",TC(i,0),TC(i,1));
|
||
|
}
|
||
|
fprintf(obj_file,"\n");
|
||
|
}
|
||
|
|
||
|
// loop over F
|
||
|
for(int i = 0;i<(int)F.rows();++i)
|
||
|
{
|
||
|
fprintf(obj_file,"f");
|
||
|
for(int j = 0; j<(int)F.cols();++j)
|
||
|
{
|
||
|
// OBJ is 1-indexed
|
||
|
fprintf(obj_file," %u",F(i,j)+1);
|
||
|
|
||
|
if(write_texture_coords)
|
||
|
fprintf(obj_file,"/%u",FTC(i,j)+1);
|
||
|
if(write_N)
|
||
|
{
|
||
|
if (write_texture_coords)
|
||
|
fprintf(obj_file,"/%u",FN(i,j)+1);
|
||
|
else
|
||
|
fprintf(obj_file,"//%u",FN(i,j)+1);
|
||
|
}
|
||
|
}
|
||
|
fprintf(obj_file,"\n");
|
||
|
}
|
||
|
fclose(obj_file);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
template <typename DerivedV, typename DerivedF>
|
||
|
IGL_INLINE bool igl::writeOBJ(
|
||
|
const std::string str,
|
||
|
const Eigen::MatrixBase<DerivedV>& V,
|
||
|
const Eigen::MatrixBase<DerivedF>& F)
|
||
|
{
|
||
|
using namespace std;
|
||
|
using namespace Eigen;
|
||
|
assert(V.cols() == 3 && "V should have 3 columns");
|
||
|
ofstream s(str);
|
||
|
if(!s.is_open())
|
||
|
{
|
||
|
fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str());
|
||
|
return false;
|
||
|
}
|
||
|
s<<
|
||
|
V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","v ","","","\n"))<<
|
||
|
(F.array()+1).format(IOFormat(FullPrecision,DontAlignCols," ","\n","f ","","","\n"));
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
template <typename DerivedV, typename T>
|
||
|
IGL_INLINE bool igl::writeOBJ(
|
||
|
const std::string &str,
|
||
|
const Eigen::MatrixBase<DerivedV>& V,
|
||
|
const std::vector<std::vector<T> >& F)
|
||
|
{
|
||
|
using namespace std;
|
||
|
using namespace Eigen;
|
||
|
assert(V.cols() == 3 && "V should have 3 columns");
|
||
|
ofstream s(str);
|
||
|
if(!s.is_open())
|
||
|
{
|
||
|
fprintf(stderr,"IOError: writeOBJ() could not open %s\n",str.c_str());
|
||
|
return false;
|
||
|
}
|
||
|
s<<V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","v ","","","\n"));
|
||
|
|
||
|
for(const auto& face : F)
|
||
|
{
|
||
|
int face_size = face.size();
|
||
|
assert(face_size != 0);
|
||
|
|
||
|
s << (face_size == 2 ? "l" : "f");
|
||
|
|
||
|
for(const auto& vi : face)
|
||
|
{
|
||
|
s<<" "<<vi;
|
||
|
}
|
||
|
s<<"\n";
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
#ifdef IGL_STATIC_LIBRARY
|
||
|
// Explicit template instantiation
|
||
|
// generated by autoexplicit.sh
|
||
|
template bool igl::writeOBJ<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&);
|
||
|
// generated by autoexplicit.sh
|
||
|
template bool igl::writeOBJ<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&);
|
||
|
// generated by autoexplicit.sh
|
||
|
template bool igl::writeOBJ<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&);
|
||
|
// generated by autoexplicit.sh
|
||
|
template bool igl::writeOBJ<Eigen::Matrix<double, 8, 3, 0, 8, 3>, Eigen::Matrix<int, 12, 3, 0, 12, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, 8, 3, 0, 8, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, 12, 3, 0, 12, 3> > const&);
|
||
|
template bool igl::writeOBJ<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
|
||
|
template bool igl::writeOBJ<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
|
||
|
template bool igl::writeOBJ<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
|
||
|
template bool igl::writeOBJ<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
|
||
|
template bool igl::writeOBJ<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
|
||
|
#endif
|