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 ///////////////////////////////////////////////////////////////////////////////
45 template<typename TPoint>
48 DGtal::MeshWriter<TPoint>::export2OFF(std::ostream & out,
49 const 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;
87 template<typename TPoint>
90 DGtal::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;
125 template<typename TPoint>
128 DGtal::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 if (nameMTLFile != "")
140 out << "mtllib " << nameMTLFile << std::endl;
141 outMTL << "# MTL format"<< std::endl;
142 outMTL << "# generated from MeshWriter from the DGTal library"<< std::endl;
146 std::map<DGtal::Color, unsigned int > mapMaterial;
149 for(unsigned int i=0; i< aMesh.nbVertex(); i++){
150 out << "v " << aMesh.getVertex(i)[0] << " " << aMesh.getVertex(i)[1] << " "<< aMesh.getVertex(i)[2] << std::endl;
154 for (unsigned int i=0; i< aMesh.nbFaces(); i++){
155 // Getting face color index.
156 std::vector<typename DGtal::Mesh<TPoint>::Index> aFace = aMesh.getFace(i);
157 DGtal::Color c = aMesh.getFaceColor(i);
158 size_t materialIndex = 0;
159 if(mapMaterial.count(c)==0){
160 materialIndex = mapMaterial.size();
161 std::pair<DGtal::Color, std::size_t> colF;
163 colF.second = mapMaterial.size();
164 // add new color in material
165 (nameMTLFile != "" ? outMTL : out) << "newmtl material_" << mapMaterial.size() << std::endl;
166 (nameMTLFile != "" ? outMTL : out) << "Ka 0.200000 0.200000 0.200000" << std::endl;
167 (nameMTLFile != "" ? outMTL : out) << "Kd " << colF.first.red()/255.0 << " " << colF.first.green()/255.0 << " " << colF.first.blue()/255.0 << std::endl;
168 (nameMTLFile != "" ? outMTL : out) << "Ks 1.000000 1.000000 1.000000" << std::endl;
169 mapMaterial.insert(colF);
171 materialIndex = mapMaterial[c];
174 out << "usemtl material_"<< materialIndex << std::endl;
176 for(typename DGtal::Mesh<TPoint>::Index j=0; j<aFace.size(); j++){
177 typename DGtal::Mesh<TPoint>::Index indexVertex = aFace.at(j);
178 out << (indexVertex+1) << " " ;
185 trace.error() << "OBJ writer IO error on export " << std::endl;
191 template<typename TPoint>
194 DGtal::MeshWriter<TPoint>::export2OBJ_colors(std::ostream &out,
195 const DGtal::Mesh<TPoint> & aMesh) {
196 return DGtal::MeshWriter<TPoint>::export2OBJ_colors(out, out, "", aMesh);
202 template <typename TPoint>
205 DGtal::operator>> ( Mesh<TPoint> & aMesh, const std::string & aFilename ){
206 std::string extension = aFilename.substr(aFilename.find_last_of(".") + 1);
208 out.open(aFilename.c_str());
209 if(extension== "off")
211 return DGtal::MeshWriter<TPoint>::export2OFF(out, aMesh, true);
213 else if(extension== "obj")
215 if(aMesh.isStoringFaceColors()){
216 std::fstream exportObjMtl;
217 std::string nameMtl = (aFilename.substr(0,aFilename.find_last_of("."))).append(".mtl");
218 exportObjMtl.open(nameMtl.c_str(), std::fstream::out);
219 return DGtal::MeshWriter<TPoint>::export2OBJ_colors(out, exportObjMtl, nameMtl, aMesh);
221 return DGtal::MeshWriter<TPoint>::export2OBJ(out, aMesh);