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