DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MeshReader.ih
1 
29 
30 // IMPLEMENTATION of inline methods.
32 
34 #include <cstdlib>
35 #include <cstdlib>
36 #include <iostream>
37 #include <fstream>
38 #include <sstream>
40 
41 
42 
44 // Implementation of inline methods //
45 
46 
47 
48 
49 template <typename TPoint>
50 inline
51 bool
52 DGtal::MeshReader<TPoint>::importOFFFile(const std::string & aFilename,
54  bool invertVertexOrder) throw(DGtal::IOException)
55 {
56  ifstream infile;
57  DGtal::IOException dgtalio;
58  try
59  {
60  infile.open (aFilename.c_str(), ifstream::in);
61  }
62  catch( ... )
63  {
64  trace.error() << "MeshReader : can't open " << aFilename << endl;
65  throw dgtalio;
66  }
67  string str;
68  getline( infile, str );
69 
70  if ( ! infile.good() )
71  {
72  trace.error() << "MeshReader : can't read " << aFilename << endl;
73  throw dgtalio;
74  }
75  if ( str.substr(0,3) != "OFF")
76  {
77  cerr <<"*" <<str<<"*"<< endl;
78  trace.error() << "MeshReader : No OFF format in " << aFilename << endl;
79  throw dgtalio;
80  }
81 
82  // Processing comments
83  do
84  {
85  getline( infile, str );
86  if ( ! infile.good() ){
87  trace.error() << "MeshReader : Invalid format in " << aFilename << endl;
88  throw dgtalio;
89  }
90  }
91  while ( str[ 0 ] == '#' || str=="");
92  istringstream str_in( str );
93  int nbPoints, nbFaces, nbEdges;
94  str_in >> nbPoints;
95  str_in >> nbFaces;
96  str_in >> nbEdges;
97 
98  // Reading mesh vertex
99  for(int i=0; i<nbPoints; i++){
100  TPoint p;
101  infile >> p[0];
102  infile >> p[1];
103  infile >> p[2];
104  aMesh.addVertex(p);
105  // Needed since a line can also contain vertex colors
106  getline(infile, str);
107  }
108 
109  // Reading mesh faces
110  for(int i=0; i<nbFaces; i++){
111  // Reading the number of face vertex
112  unsigned int aNbFaceVertex;
113  infile >> aNbFaceVertex;
114  vector<unsigned int> aFace;
115  for (unsigned int j=0; j< aNbFaceVertex; j++){
116  unsigned int anIndex;
117  infile >> anIndex;
118  aFace.push_back(anIndex);
119  }
120  if( invertVertexOrder ){
121  for(unsigned int j=0; j < aFace.size()/2; j++){
122  unsigned int tmp=aFace.at(j);
123  aFace.at(j)=aFace.at(aFace.size()-1-j);
124  aFace.at(aFace.size()-1-j)=tmp;
125  }
126  }
127 
128  // Needed since a can also contain vertex colors
129  getline(infile, str);
130  // Contains colors:
131  bool findValidColor=true;
132  if(str!=""){
133  istringstream inColor(str);
134  double colorR, colorG, colorB, colorT;
135  findValidColor=inColor.good();
136  if(findValidColor && inColor.good()){
137  inColor >> colorR;
138  }
139  findValidColor &=!inColor.fail();
140  if(findValidColor && inColor.good()){
141  inColor >> colorG;
142  }
143  findValidColor &=!inColor.fail();
144  if(findValidColor && inColor.good()){
145  inColor >> colorB;
146  }
147  findValidColor &=!inColor.fail();
148 
149  if(findValidColor && inColor.good()){
150  inColor >> colorT;
151  // Since alpha is optional:
152  if(inColor.fail()){
153  colorT=1.0;
154  }
155 
156  }else{
157  colorT=1.0;
158  }
159  if(findValidColor){
160  DGtal::Color c((unsigned int)(colorR*255.0), (unsigned int)(colorG*255.0),
161  (unsigned int)(colorB*255.0), (unsigned int)(colorT*255.0));
162  aMesh.addFace(aFace, c);
163  }else{
164  aMesh.addFace(aFace);
165  }
166  }else{
167  aMesh.addFace(aFace);
168  }
169  }
170 
171  return true;
172 }
173 
174 
175 
176 
177 
178 
179 template <typename TPoint>
180 inline
181 bool
182 DGtal::MeshReader<TPoint>::importOFSFile(const std::string & aFilename,
184  bool invertVertexOrder, double scale) throw(DGtal::IOException)
185 {
186  ifstream infile;
187  DGtal::IOException dgtalio;
188  try
189  {
190  infile.open (aFilename.c_str(), ifstream::in);
191  }
192  catch( ... )
193  {
194  trace.error() << "MeshReader : can't open " << aFilename << endl;
195  throw dgtalio;
196  }
197  string str;
198  getline( infile, str );
199 
200  if ( ! infile.good() )
201  {
202  trace.error() << "MeshReader : can't read " << aFilename << endl;
203  throw dgtalio;
204  }
205  if ( str.substr(0,3) != "OFS")
206  {
207  trace.error() << "MeshReader : No OFS format in " << aFilename << endl;
208  throw dgtalio;
209  }
210 
211  // Processing comments
212  do
213  {
214  getline( infile, str );
215  if ( ! infile.good() ){
216  trace.error() << "MeshReader : Invalid format in " << aFilename << endl;
217  throw dgtalio;
218  }
219  }
220  while ( str[ 0 ] == '#' || str=="");
221  istringstream str_in( str );
222  int nbPoints;
223  str_in >> nbPoints;
224 
225  // Reading mesh vertex
226  for(int i=0; i<nbPoints; i++){
227  TPoint p;
228  infile >> p[0];
229  infile >> p[1];
230  infile >> p[2];
231  p[0]*=scale;
232  p[1]*=scale;
233  p[2]*=scale;
234  aMesh.addVertex(p);
235  // Needed since a line can also contain vertex colors
236  getline(infile, str);
237  }
238  do
239  {
240  getline( infile, str );
241  if ( ! infile.good() ){
242  trace.error() << "MeshReader : Invalid format in " << aFilename << endl;
243  throw dgtalio;
244  }
245  }
246  while ( str[ 0 ] == '#' || str=="");
247  istringstream str_in2( str );
248  unsigned int nbFaces;
249  str_in2 >> nbFaces;
250  // Reading mesh faces
251  for(unsigned int i=0; i<nbFaces; i++){
252  // Reading the number of face vertex
253  vector<unsigned int> aFace;
254  for (unsigned int j=0; j< 3; j++){
255  unsigned int anIndex;
256  infile >> anIndex;
257  aFace.push_back(anIndex);
258  }
259  if( invertVertexOrder ){
260  unsigned int tmp=aFace.at(0);
261  aFace.at(0)=aFace.at(2);
262  aFace.at(2)=tmp;
263  }
264  aMesh.addFace(aFace);
265  getline(infile, str);
266 
267  }
268 
269  return true;
270 
271 }
272 
273 
274  template <typename TPoint>
275  bool
276  DGtal::operator<< ( MeshFromPoints<TPoint> & mesh, const std::string &filename ){
277  string extension = filename.substr(filename.find_last_of(".") + 1);
278  if(extension== "off") {
280  return true;
281  }else if(extension== "ofs") {
283  return true;
284  }
285 
286  return false;
287  }
288 
289 
290 
291 
292 
293 
294 
295 // //
297 
298 
299 
300 
301