1 /****************************************************************************
2 * MeshLab                                                           o o     *
3 * An extendible mesh processor                                    o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2005, 2009                                          \/)\/    *
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 #ifndef __VCGLIB_IMPORTERTS
25 #define __VCGLIB_IMPORTERTS
26 #define NULL 0
27 #include <vcg/space/point3.h>
28 #include <vcg/space/point4.h>
29 
30 namespace vcg {
31 namespace tetra {
32 namespace io {
33 
34 template <typename  MESHTYPE>
35 class ImporterTS{
36 	typedef MESHTYPE Tetramesh;
37 	typedef typename Tetramesh::VertexPointer VertexPointer;
38 	typedef typename Tetramesh::VertexType VertexType;
39 	typedef typename Tetramesh::TetraType FaceType;
40 	typedef typename Tetramesh::VertexIterator VertexIterator;
41 	typedef typename Tetramesh::TetraIterator FaceIterator;
42 	typedef typename Tetramesh::ScalarType ScalarType;
43 	typedef Point3<ScalarType> Point3x;
44 
F()45 	static FILE *& F(){static FILE * f; return f;}
46 
ReadPos(Point3<double> & p)47 	inline static void ReadPos( Point3<double> &p){
48 		fscanf(F(),"%lg %lg %lg",&p[0],&p[1],&p[2]);
49 	}
ReadPos(Point3<float> & p)50 	inline static void  ReadPos( Point3<float> &p){
51 		fscanf(F(),"%f %f %f",&p[0],&p[1],&p[2]);
52 	}
ReadPos(Point4<ScalarType> & p)53 	inline static void  ReadPos( Point4<ScalarType> &p){
54 		fscanf(F(),"%g %g %g %g",&p[0],&p[1],&p[2],&p[3]);
55 	}
56 public:
Open(Tetramesh & m,const char * filename)57 static int Open( Tetramesh & m, const char * filename )
58 {
59 	int nvertex;
60 	int ntetra;
61 	int tp0;
62 	int tp1;
63 	int tp2;
64 	int tp3;
65 	typename Tetramesh::VertexType p1;
66 	F() = fopen(filename,"r");
67 	if(F() == NULL )
68 		{
69 			printf( "The file was not opened\n" );
70 			return -1;
71 		}
72    else
73    {
74 		fscanf(F(), "%i", &nvertex );
75 		fscanf(F(), "%i", &ntetra );
76 		m.tetra.reserve(ntetra);
77     m.vert.reserve(nvertex);
78 		int j;
79 		for (j=0;j<nvertex;j++)
80 		{
81 			m.vert.push_back(VertexType());
82 			ReadPos(m.vert.back().P());
83       m.vert.back().ClearFlags();
84 		}
85 		for (j=0;j<ntetra;j++)
86 		{
87 			fscanf(F(), "%i", &tp0 );
88 			fscanf(F(), "%i", &tp1 );
89 			fscanf(F(), "%i", &tp2 );
90 			fscanf(F(), "%i", &tp3 );
91 
92 			m.tetra.push_back(typename Tetramesh::TetraType());
93 			m.tetra.back().V(0) = &m.vert[tp0];
94 			m.tetra.back().V(1) = &m.vert[tp1];
95 			m.tetra.back().V(2) = &m.vert[tp2];
96 			m.tetra.back().V(3) = &m.vert[tp3];
97 			m.tetra.back().Flags() = 0;
98 		}
99 	 }
100 	 m.vn = nvertex;
101 	 m.tn = ntetra;
102 
103 		return 0;
104 	 }
105 	};// end class
106 }// end of io
107 }// end of tetra
108 }// end of vcg
109 #endif
110