DGtal  1.2.0
MeshWriter.ih
1 /**
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.
6  *
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.
11  *
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/>.
14  *
15  **/
16 
17 /**
18  * @file MeshWriter.ih
19  * @author Bertrand Kerautret (\c kerautre@loria.fr )
20  * LORIA (CNRS, UMR 7503), University of Nancy, France
21  *
22  *
23  * @date 2012/07/08
24  *
25  * Implementation of inline methods defined in MeshWriter.h
26  *
27  * This file is part of the DGtal library.
28  */
29 
30 
31 //////////////////////////////////////////////////////////////////////////////
32 #include <cstdlib>
33 #include <fstream>
34 #include <set>
35 #include <map>
36 #include "DGtal/io/Color.h"
37 //////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // IMPLEMENTATION of inline methods.
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 
44 
45 template<typename TPoint>
46 inline
47 bool
48 DGtal::MeshWriter<TPoint>::export2OFF(std::ostream & out,
49  const DGtal::Mesh<TPoint> & aMesh, bool exportColor) {
50  DGtal::IOException dgtalio;
51  try
52  {
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;
56 
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;
59  }
60 
61  for (unsigned int i=0; i< aMesh.nbFaces(); i++){
62  std::vector<unsigned int> aFace = aMesh.getFace(i);
63  out << aFace.size() << " " ;
64  for(unsigned int j=0; j<aFace.size(); j++){
65  unsigned int indexVertex = aFace.at(j);
66  out << indexVertex << " " ;
67  }
68  DGtal::Color col = aMesh.getFaceColor(i);
69  if(exportColor && aMesh.isStoringFaceColors() )
70  {
71  out << " ";
72  out << ((double) col.red())/255.0 << " "
73  << ((double) col.green())/255.0 << " "<< ((double) col.blue())/255.0
74  << " " << ((double) col.alpha())/255.0 ;
75  }
76  out << std::endl;
77  }
78  }catch( ... )
79  {
80  trace.error() << "OFF writer IO error on export " << std::endl;
81  throw dgtalio;
82  }
83 
84  return true;
85 }
86 
87 template<typename TPoint>
88 inline
89 bool
90 DGtal::MeshWriter<TPoint>::export2OBJ(std::ostream &out,
91  const DGtal::Mesh<TPoint> & aMesh) {
92  DGtal::IOException dgtalio;
93  try
94  {
95  out << "# OBJ format"<< std::endl;
96  out << "# generated from MeshWriter from the DGTal library"<< std::endl;
97  out << std::endl;
98  out << "o anObj" << std::endl;
99  out << std::endl;
100  std::vector<DGtal::Color> vCol;
101  // processing vertex
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;
104  }
105  out << std::endl;
106  // processing faces:
107  for (unsigned int i=0; i< aMesh.nbFaces(); i++){
108  std::vector<unsigned int> aFace = aMesh.getFace(i);
109  out << "f " ;
110  for(unsigned int j=0; j<aFace.size(); j++){
111  unsigned int indexVertex = aFace.at(j);
112  out << (indexVertex+1) << " " ;
113  }
114  out << std::endl;
115  }
116  out << std::endl;
117  }catch( ... )
118  {
119  trace.error() << "OBJ writer IO error on export " << std::endl;
120  throw dgtalio;
121  }
122  return true;
123 }
124 
125 template<typename TPoint>
126 inline
127 bool
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;
131  try
132  {
133  out << "# OBJ format"<< std::endl;
134  out << "# generated from MeshWriter from the DGTal library"<< std::endl;
135  out << std::endl;
136  out << "o anObj" << std::endl;
137  out << std::endl;
138  out << "mtllib " << nameMTLFile << std::endl;
139 
140 
141  outMTL << "# MTL format"<< std::endl;
142  outMTL << "# generated from MeshWriter from the DGTal library"<< std::endl;
143 
144  std::map<DGtal::Color, unsigned int > mapMaterial;
145 
146  // processing vertex
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;
149  }
150  out << std::endl;
151  // processing faces:
152  for (unsigned int i=0; i< aMesh.nbFaces(); i++){
153  // Getting face color index.
154  std::vector<unsigned int> 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;
160  colF.first = c;
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);
168  }else{
169  materialIndex = mapMaterial[c];
170  }
171 
172  out << "usemtl material_"<< materialIndex << std::endl;
173  out << "f " ;
174  for(unsigned int j=0; j<aFace.size(); j++){
175  unsigned int indexVertex = aFace.at(j);
176  out << (indexVertex+1) << " " ;
177  }
178  out << std::endl;
179  }
180  out << std::endl;
181  }catch( ... )
182  {
183  trace.error() << "OBJ writer IO error on export " << std::endl;
184  throw dgtalio;
185  }
186  return true;
187 }
188 
189 
190 
191 
192 
193 template <typename TPoint>
194 inline
195 bool
196 DGtal::operator>> ( Mesh<TPoint> & aMesh, const std::string & aFilename ){
197  std::string extension = aFilename.substr(aFilename.find_last_of(".") + 1);
198  std::ofstream out;
199  out.open(aFilename.c_str());
200  if(extension== "off")
201  {
202  return DGtal::MeshWriter<TPoint>::export2OFF(out, aMesh, true);
203  }
204  else if(extension== "obj")
205  {
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);
211  }else{
212  return DGtal::MeshWriter<TPoint>::export2OBJ(out, aMesh);
213  }
214 
215 
216  }
217  out.close();
218  return false;
219 }
220 
221