import_asc.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *   
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 
00025 #ifndef __VCGLIB_IMPORT_ASC
00026 #define __VCGLIB_IMPORT_ASC
00027 
00028 #include <stdio.h>
00029 #include <fstream>
00030 #include <iostream>
00031 #include <vcg/complex/algorithms/create/platonic.h>
00032 #include <vcg/complex/algorithms/clean.h>
00033 
00034 namespace vcg {
00035 namespace tri {
00036 namespace io {
00037 
00043 template <class MESH_TYPE>
00044 class ImporterASC
00045 {
00046 public:
00047 
00048 typedef typename MESH_TYPE::VertexPointer VertexPointer;
00049 typedef typename MESH_TYPE::ScalarType ScalarType;
00050 typedef typename MESH_TYPE::VertexType VertexType;
00051 typedef typename MESH_TYPE::FaceType FaceType;
00052 typedef typename MESH_TYPE::VertexIterator VertexIterator;
00053 typedef typename MESH_TYPE::FaceIterator FaceIterator;
00054 
00055 enum RAWError {
00056         E_NOERROR,                              // 0
00057                 // Error open
00058         E_CANTOPEN,                             // 1
00059         E_UNESPECTEDEOF,        // 2
00060         E_NO_POINTS,                            //3
00061 };
00062 
00063 static const char *ErrorMsg(int error)
00064 {
00065   static const char * raw_error_msg[] =
00066   {
00067         "No errors",
00068         "Can't open file",
00069         "Premature End of file",
00070         "Failed to import any point. Use simple ascii files with just x y z coords."
00071         };
00072 
00073   if(error>3 || error<0) return "Unknown error";
00074   else return raw_error_msg[error];
00075 };
00076 
00085 static int Open( MESH_TYPE &m, const char * filename, CallBackPos *cb=0, bool triangulate=false, int lineskip=0)
00086 {
00087     FILE *fp;
00088     fp = fopen(filename, "r");
00089     if(fp == NULL)
00090     {
00091                         qDebug("Failed opening of %s",filename);
00092       return E_CANTOPEN;
00093     }
00094                 long currentPos = ftell(fp);
00095                 fseek(fp,0L,SEEK_END);
00096                 long fileLen = ftell(fp);
00097                 fseek(fp,currentPos,SEEK_SET);
00098 
00099     m.Clear();
00100   
00101     Point3f pp;
00102                 float q;
00103     size_t cnt=0;
00104                 int ret;
00105                 char buf[1024];
00106 
00107                 // skip the first <lineskip> lines
00108                 for(int i=0;i<lineskip;++i)
00109                                 fgets(buf,1024,fp);
00110 
00111     /* Read a single triplet of coords from an ASCII file of coords*/
00112     while(!feof(fp))
00113     {
00114       if(cb && (++cnt)%1000) cb( (ftell(fp)*100)/fileLen, "ASC Mesh Loading");  
00115                         if(feof(fp)) break;
00116             bool fgetOut=fgets(buf,1024,fp);
00117             if( fgetOut == 0 ) continue;
00118                         ret=sscanf(buf, "%f, %f, %f, %f\n", &pp.X(), &pp.Y(), &pp.Z(),&q);
00119                         if(ret==1) // lets try also non comma separated values
00120                                 ret=sscanf(buf, "%f %f %f %f\n", &pp.X(), &pp.Y(), &pp.Z(),&q);
00121                         
00122                         if(ret>=3)
00123                                 {
00124                                         VertexIterator vi=Allocator<MESH_TYPE>::AddVertices(m,1);
00125                                         (*vi).P().Import(pp); 
00126                                         if(ret==4)      (*vi).Q()=q; 
00127                                 }                       
00128     }
00129                 
00130     fclose(fp);
00131                 if(m.vn==0) return E_NO_POINTS;
00132                 if(!triangulate) return E_NOERROR;
00133                 // now try to triangulate.
00134                 // search for the first jump
00135                 float baseY = m.vert[0].P().Y();
00136                 int i;
00137                 for(i=1;i<m.vert.size();++i)
00138                                 {
00139                                  if(m.vert[i].P().Y()!= baseY)  break;
00140                                 }
00141                 cnt=m.vert.size();
00142                 qDebug("Grid is %i x %i = %i (%i) ",i,cnt/i,i* (cnt/i),cnt);
00143                 tri::FaceGrid(m,i,int(cnt/i));
00144                 tri::Clean<MESH_TYPE>::FlipMesh(m);
00145     return E_NOERROR;
00146   }
00147 
00148 }; // end class
00149 } // end Namespace tri
00150 } // end Namespace io
00151 } // end Namespace vcg
00152 
00153 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:55