2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @author Bertrand Kerautret (\c kerautre@loria.fr )
20 * LORIA (CNRS, UMR 7503), University of Nancy, France
25 * Implementation of inline methods defined in MeshWriter.h
27 * This file is part of the DGtal library.
31//////////////////////////////////////////////////////////////////////////////
36#include "DGtal/io/Color.h"
37//////////////////////////////////////////////////////////////////////////////
39///////////////////////////////////////////////////////////////////////////////
40// IMPLEMENTATION of inline methods.
41///////////////////////////////////////////////////////////////////////////////
45template<typename TPoint>
48DGtal::MeshWriter<TPoint>::export2OFF(std::ostream & out,
49const DGtal::Mesh<TPoint> & aMesh, bool exportColor) {
50 DGtal::IOException dgtalio;
53 out << "OFF"<< std::endl;
54 out << "# generated from MeshWriter from the DGTal library"<< std::endl;
55 out << aMesh.nbVertex() << " " << aMesh.nbFaces() << " " << 0 << " " << std::endl;
57 for(unsigned int i=0; i< aMesh.nbVertex(); i++){
58 out << aMesh.getVertex(i)[0] << " " << aMesh.getVertex(i)[1] << " "<< aMesh.getVertex(i)[2] << std::endl;
61 for (unsigned int i=0; i< aMesh.nbFaces(); i++){
62 std::vector<typename DGtal::Mesh<TPoint>::Index> aFace = aMesh.getFace(i);
63 out << aFace.size() << " " ;
64 for(typename Mesh<TPoint>::Index j=0; j<aFace.size(); j++){
65 const auto indexVertex = aFace.at(j);
66 out << indexVertex << " " ;
68 DGtal::Color col = aMesh.getFaceColor(i);
69 if(exportColor && aMesh.isStoringFaceColors() )
72 out << ((double) col.red())/255.0 << " "
73 << ((double) col.green())/255.0 << " "<< ((double) col.blue())/255.0
74 << " " << ((double) col.alpha())/255.0 ;
80 trace.error() << "OFF writer IO error on export " << std::endl;
87template<typename TPoint>
90DGtal::MeshWriter<TPoint>::export2OBJ(std::ostream &out,
91 const DGtal::Mesh<TPoint> & aMesh) {
92 DGtal::IOException dgtalio;
95 out << "# OBJ format"<< std::endl;
96 out << "# generated from MeshWriter from the DGTal library"<< std::endl;
98 out << "o anObj" << std::endl;
100 std::vector<DGtal::Color> vCol;
102 for(unsigned int i=0; i< aMesh.nbVertex(); i++){
103 out << "v " << aMesh.getVertex(i)[0] << " " << aMesh.getVertex(i)[1] << " "<< aMesh.getVertex(i)[2] << std::endl;
107 for (unsigned int i=0; i< aMesh.nbFaces(); i++){
108 std::vector<typename DGtal::Mesh<TPoint>::Index> aFace = aMesh.getFace(i);
110 for(typename Mesh<TPoint>::Index j=0; j<aFace.size(); j++){
111 const auto indexVertex = aFace.at(j);
112 out << (indexVertex+1) << " " ;
119 trace.error() << "OBJ writer IO error on export " << std::endl;
125template<typename TPoint>
128DGtal::MeshWriter<TPoint>::export2OBJ_colors(std::ostream &out, std::ostream &outMTL, const std::string nameMTLFile,
129 const DGtal::Mesh<TPoint> & aMesh) {
130 DGtal::IOException dgtalio;
133 out << "# OBJ format"<< std::endl;
134 out << "# generated from MeshWriter from the DGTal library"<< std::endl;
136 out << "o anObj" << std::endl;
138 out << "mtllib " << nameMTLFile << std::endl;
141 outMTL << "# MTL format"<< std::endl;
142 outMTL << "# generated from MeshWriter from the DGTal library"<< std::endl;
144 std::map<DGtal::Color, unsigned int > mapMaterial;
147 for(unsigned int i=0; i< aMesh.nbVertex(); i++){
148 out << "v " << aMesh.getVertex(i)[0] << " " << aMesh.getVertex(i)[1] << " "<< aMesh.getVertex(i)[2] << std::endl;
152 for (unsigned int i=0; i< aMesh.nbFaces(); i++){
153 // Getting face color index.
154 std::vector<typename DGtal::Mesh<TPoint>::Index> aFace = aMesh.getFace(i);
155 DGtal::Color c = aMesh.getFaceColor(i);
156 size_t materialIndex = 0;
157 if(mapMaterial.count(c)==0){
158 materialIndex = mapMaterial.size();
159 std::pair<DGtal::Color, std::size_t> colF;
161 colF.second = mapMaterial.size();
162 // add new color in material
163 outMTL << "newmtl material_" << mapMaterial.size() << std::endl;
164 outMTL << "Ka 0.200000 0.200000 0.200000" << std::endl;
165 outMTL << "Kd " << colF.first.red()/255.0 << " " << colF.first.green()/255.0 << " " << colF.first.blue()/255.0 << std::endl;
166 outMTL << "Ks 1.000000 1.000000 1.000000" << std::endl;
167 mapMaterial.insert(colF);
169 materialIndex = mapMaterial[c];
172 out << "usemtl material_"<< materialIndex << std::endl;
174 for(typename DGtal::Mesh<TPoint>::Index j=0; j<aFace.size(); j++){
175 typename DGtal::Mesh<TPoint>::Index indexVertex = aFace.at(j);
176 out << (indexVertex+1) << " " ;
183 trace.error() << "OBJ writer IO error on export " << std::endl;
193template <typename TPoint>
196DGtal::operator>> ( Mesh<TPoint> & aMesh, const std::string & aFilename ){
197 std::string extension = aFilename.substr(aFilename.find_last_of(".") + 1);
199 out.open(aFilename.c_str());
200 if(extension== "off")
202 return DGtal::MeshWriter<TPoint>::export2OFF(out, aMesh, true);
204 else if(extension== "obj")
206 if(aMesh.isStoringFaceColors()){
207 std::fstream exportObjMtl;
208 std::string nameMtl = (aFilename.substr(0,aFilename.find_last_of("."))).append(".mtl");
209 exportObjMtl.open(nameMtl.c_str(), std::fstream::out);
210 return DGtal::MeshWriter<TPoint>::export2OBJ_colors(out, exportObjMtl, nameMtl, aMesh);
212 return DGtal::MeshWriter<TPoint>::export2OBJ(out, aMesh);