DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MeshWriter.ih
1 
31 
32 #include <cstdlib>
33 #include <fstream>
34 #include <set>
35 #include "DGtal/io/Color.h"
37 
39 // IMPLEMENTATION of inline methods.
41 
42 
43 
44 template<typename TPoint>
45 inline
46 bool
48  const DGtal::MeshFromPoints<TPoint> & aMesh, bool exportColor) throw(DGtal::IOException){
49  DGtal::IOException dgtalio;
50  try
51  {
52  out << "OFF"<< endl;
53  out << "# generated from MeshWriter from the DGTal library"<< endl;
54  out << aMesh.nbVertex() << " " << aMesh.nbFaces() << " " << 0 << " " << endl;
55 
56  for(unsigned int i=0; i< aMesh.nbVertex(); i++){
57  out << aMesh.getVertex(i)[0] << " " << aMesh.getVertex(i)[1] << " "<< aMesh.getVertex(i)[2] << endl;
58  }
59 
60  for (unsigned int i=0; i< aMesh.nbFaces(); i++){
61  vector<unsigned int> aFace = aMesh.getFace(i);
62  out << aFace.size() << " " ;
63  for(unsigned int j=0; j<aFace.size(); j++){
64  unsigned int indexVertex = aFace.at(j);
65  out << indexVertex << " " ;
66  }
67  DGtal::Color col = aMesh.getFaceColor(i);
68  if(exportColor){
69  out << " ";
70  out << ((double) col.red())/255.0 << " "
71  << ((double) col.green())/255.0 << " "<< ((double) col.blue())/255.0
72  << " " << ((double) col.alpha())/255.0 ;
73  }
74  out << endl;
75  }
76  }catch( ... )
77  {
78  trace.error() << "OFF writer IO error on export " << endl;
79  throw dgtalio;
80  }
81 
82  return true;
83 }
84 
85 
86 
87 
88 
89 
90 template<typename TPoint>
91 inline
92 bool
95  DGtal::IOException dgtalio;
96  try
97  {
98  out << "# OBJ format"<< endl;
99  out << "# generated from MeshWriter from the DGTal library"<< endl;
100  out << endl;
101  out << "o anObj" << endl;
102  out << endl;
103 
104  // processing vertex
105  for(unsigned int i=0; i< aMesh.nbVertex(); i++){
106  out << "v " << aMesh.getVertex(i)[0] << " " << aMesh.getVertex(i)[1] << " "<< aMesh.getVertex(i)[2] << endl;
107  }
108  out << endl;
109  // processing faces:
110  for (unsigned int i=0; i< aMesh.nbFaces(); i++){
111  vector<unsigned int> aFace = aMesh.getFace(i);
112  out << "f " ;
113  for(unsigned int j=0; j<aFace.size(); j++){
114  unsigned int indexVertex = aFace.at(j);
115  out << (indexVertex+1) << " " ;
116  }
117  out << endl;
118  }
119  out << endl;
120  }catch( ... )
121  {
122  trace.error() << "OBJ writer IO error on export " << endl;
123  throw dgtalio;
124  }
125  return true;
126 }
127 
128 
129 
130 
131 
132 template <typename TPoint>
133 inline
134 bool
135 DGtal::operator>> ( MeshFromPoints<TPoint> & aMesh, const std::string & aFilename ){
136  string extension = aFilename.substr(aFilename.find_last_of(".") + 1);
137  ofstream out;
138  out.open(aFilename.c_str());
139  if(extension== "off") {
140  return DGtal::MeshWriter<TPoint>::export2OFF(out, aMesh, true);
141  }else if(extension== "obj") {
142  return DGtal::MeshWriter<TPoint>::export2OBJ(out, aMesh);
143  }
144  out.close();
145  return false;
146 }
147 
148