Debian Codegraph @

src/vcglib/wrap/io_trimesh/import_asc.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23
24
25 #ifndef __VCGLIB_IMPORT_ASC
26 #define __VCGLIB_IMPORT_ASC
27
28 #include <stdio.h>
29 #include <fstream>
30 #include <iostream>
31 #include <vcg/complex/algorithms/create/platonic.h>
32 #include <vcg/complex/algorithms/clean.h>
33
34 namespace vcg {
35 namespace tri {
36 namespace io {
37
38 /**
39 This class encapsulate a filter for importing raw format pointcloud.
40 there exists many raw formats. each one with a particular sintax even if they only contains
41
42 */
43 template <class MESH_TYPE>
44 class ImporterASC
45 {
46 public:
47
48 typedef typename MESH_TYPE::VertexPointer VertexPointer;
49 typedef typename MESH_TYPE::ScalarType ScalarType;
50 typedef typename MESH_TYPE::VertexType VertexType;
51 typedef typename MESH_TYPE::FaceType FaceType;
52 typedef typename MESH_TYPE::VertexIterator VertexIterator;
53 typedef typename MESH_TYPE::FaceIterator FaceIterator;
54
55 enum RAWError {
56 E_NOERROR, // 0
57 // Error open
58 E_CANTOPEN, // 1
59 E_UNESPECTEDEOF, // 2
60 E_NO_POINTS, //3
61 };
62
63 static const char *ErrorMsg(int error)
64 {
65 static const char * raw_error_msg[] =
66 {
67 "No errors",
68 "Can't open file",
69 "Premature End of file",
70 "Failed to import any point. Use simple ascii files with just x y z coords."
71 };
72
73 if(error>3 || error<0) return "Unknown error";
74 else return raw_error_msg[error];
75 };
76
77 /*!
78 * Standard call for reading a mesh
79 * \param m the destination mesh
80 * \param filename the name of the file to read from
81 * \param triangulate if true, the mesh will be triangulated, otherwise only points will be stored
82 * \param lineskip number of lines to be skipped at the begin of the file
83 * \return the operation result
84 */
85 static int Open( MESH_TYPE &m, const char * filename, CallBackPos *cb=0, bool triangulate=false, int lineskip=0)
86 {
87 FILE *fp;
88 fp = fopen(filename, "r");
89 if(fp == NULL)
90 {
91 qDebug("Failed opening of %s",filename);
92 return E_CANTOPEN;
93 }
94 long currentPos = ftell(fp);
95 fseek(fp,0L,SEEK_END);
96 long fileLen = ftell(fp);
97 fseek(fp,currentPos,SEEK_SET);
98
99 m.Clear();
100
101 Point3f pp;
102 float q;
103 size_t cnt=0;
104 int ret;
105 char buf[1024];
106
107 // skip the first <lineskip> lines
108 for(int i=0;i<lineskip;++i)
109 fgets(buf,1024,fp);
110
111 /* Read a single triplet of coords from an ASCII file of coords*/
112 while(!feof(fp))
113 {
114 if(cb && (++cnt)%1000) cb( (ftell(fp)*100)/fileLen, "ASC Mesh Loading");
115 if(feof(fp)) break;
116 bool fgetOut=fgets(buf,1024,fp);
117 if( fgetOut == 0 ) continue;
118 ret=sscanf(buf, "%f, %f, %f, %f\n", &pp.X(), &pp.Y(), &pp.Z(),&q);
119 if(ret==1) // lets try also non comma separated values
120 ret=sscanf(buf, "%f %f %f %f\n", &pp.X(), &pp.Y(), &pp.Z(),&q);
121
122 if(ret>=3)
123 {
124 VertexIterator vi=Allocator<MESH_TYPE>::AddVertices(m,1);
125 (*vi).P().Import(pp);
126 if(ret==4) (*vi).Q()=q;
127 }
128 }
129
130 fclose(fp);
131 if(m.vn==0) return E_NO_POINTS;
132 if(!triangulate) return E_NOERROR;
133 // now try to triangulate.
134 // search for the first jump
135 float baseY = m.vert[0].P().Y();
136 unsigned int i;
137 for(i=1;i<m.vert.size();++i)
138 {
139 if(m.vert[i].P().Y()!= baseY) break;
140 }
141 cnt=m.vert.size();
142 qDebug("Grid is %i x %li = %li (%zu) ",i,cnt/i,i* (cnt/i),cnt);
143 tri::FaceGrid(m,i,int(cnt/i));
144 tri::Clean<MESH_TYPE>::FlipMesh(m);
145 return E_NOERROR;
146 }
147
148 }; // end class
149 } // end Namespace tri
150 } // end Namespace io
151 } // end Namespace vcg
152
153 #endif