DGtal  1.5.beta
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<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 << " " ;
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<typename DGtal::Mesh<TPoint>::Index> aFace = aMesh.getFace(i);
109  out << "f " ;
110  for(typename Mesh<TPoint>::Index j=0; j<aFace.size(); j++){
111  const auto 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  if (nameMTLFile != "")
139  {
140  out << "mtllib " << nameMTLFile << std::endl;
141  outMTL << "# MTL format"<< std::endl;
142  outMTL << "# generated from MeshWriter from the DGTal library"<< std::endl;
143 
144  }
145 
146  std::map<DGtal::Color, unsigned int > mapMaterial;
147 
148  // processing vertex
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;
151  }
152  out << std::endl;
153  // processing faces:
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;
162  colF.first = c;
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);
170  }else{
171  materialIndex = mapMaterial[c];
172  }
173 
174  out << "usemtl material_"<< materialIndex << std::endl;
175  out << "f " ;
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) << " " ;
179  }
180  out << std::endl;
181  }
182  out << std::endl;
183  }catch( ... )
184  {
185  trace.error() << "OBJ writer IO error on export " << std::endl;
186  throw dgtalio;
187  }
188  return true;
189 }
190 
191 template<typename TPoint>
192 inline
193 bool
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);
197 }
198 
199 
200 
201 
202 template <typename TPoint>
203 inline
204 bool
205 DGtal::operator>> ( Mesh<TPoint> & aMesh, const std::string & aFilename ){
206  std::string extension = aFilename.substr(aFilename.find_last_of(".") + 1);
207  std::ofstream out;
208  out.open(aFilename.c_str());
209  if(extension== "off")
210  {
211  return DGtal::MeshWriter<TPoint>::export2OFF(out, aMesh, true);
212  }
213  else if(extension== "obj")
214  {
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);
220  }else{
221  return DGtal::MeshWriter<TPoint>::export2OBJ(out, aMesh);
222  }
223 
224 
225  }
226  out.close();
227  return false;
228 }
229 
230