File: export_gts.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (214 lines) | stat: -rw-r--r-- 6,441 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

/* This code has been adapted from the GTS exporter and Gael's GTS exporter from Expe */

/**
@name Save in GTS format
*/
//@{

#ifndef __VCGLIB_EXPORT_GTS
#define __VCGLIB_EXPORT_GTS

#include <QFile>
#include <QTextStream>
#include <map>
#include <wrap/io_trimesh/io_mask.h>


namespace vcg {
	namespace tri {
		namespace io {
			template <class SaveMeshType>
			class ExporterGTS
			{

			public:
				typedef typename SaveMeshType::VertexPointer VertexPointer;
				typedef typename SaveMeshType::ScalarType ScalarType;
				typedef typename SaveMeshType::VertexType VertexType;
				typedef typename SaveMeshType::FaceType FaceType;
				typedef typename SaveMeshType::FacePointer FacePointer;
				typedef typename SaveMeshType::VertexIterator VertexIterator;
				typedef typename SaveMeshType::FaceIterator FaceIterator;

				static int Save(SaveMeshType &m, const char * filename, int /*mask*/ )
				{
					QFile device(filename);
					if (!device.open(QFile::WriteOnly))
						return 1;

					QTextStream stream(&device);

					// update vertex indices
					std::vector<int> FlagV;
					VertexPointer  vp;
					VertexIterator vi;
					int j;
					for(j=0,vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
					{
						vp = &(*vi);
						FlagV.push_back(vp->Flags());
						if (!vp->IsD())
						{
							vp->Flags() = j;
							j++;
						}
					}
					assert(j==m.vn);

					// build the edge list, and count the number of edges
					typedef std::pair<int,int> Edge;
					typedef std::map<Edge,int> Edge2Index;
					Edge2Index edge2index;

					// loop over all edge's faces
					FacePointer fp;
					int edgeCount = 0;
					FaceIterator fi;
					for (j=0,fi=m.face.begin(); fi!=m.face.end(); ++fi)
					{
						fp = &(*fi);
						if (!fp->IsD() )
						{
							for (int k=0; k<3; ++k)
							{
								int a = fp->cV(k)->Flags();
								int b = fp->cV((k+1)%3)->Flags();
								if (a>b)
									std::swap(a,b);
								Edge e(a,b);
								Edge2Index::iterator it = edge2index.find(e);
								if (it==edge2index.end())
								{
									edge2index[e] = edgeCount;
									edgeCount++;
								}
							}
						}
					}

					stream  << m.vn << " "
									<< edgeCount << " "
									<< m.fn << " "
									<< "GtsSurface GtsFace GtsEdge GtsVertex\n";

					// export vertices
					for (vi=m.vert.begin();vi!=m.vert.end();++vi)
					{
						vp=&(*vi);
						if (!vp->IsD())
						{
							stream << vp->P()[0] << " " << vp->P()[1] << " " << vp->P()[2] << "\n";
						}
					}

					// export the edges
					for (j=0,fi=m.face.begin(); fi!=m.face.end(); ++fi)
					{
						fp = &(*fi);
						if (!fp->IsD() )
						{
							for (int k=0; k<3; ++k)
							{
								int a = fp->cV(k)->Flags();
								int b = fp->cV((k+1)%3)->Flags();
								if (a>b)
									std::swap(a,b);
								Edge e(a,b);
								Edge2Index::iterator it = edge2index.find(e);
								if (it!=edge2index.end())
								{
									stream << a+1 << " " << b+1 << "\n";
								}
							}
						}
					}

					// third pass to export the face to edge connectivity
					for (j=0,fi=m.face.begin(); fi!=m.face.end(); ++fi)
					{
						fp = &(*fi);
						if (!fp->IsD() )
						{
							for (int k=0; k<3; ++k)
							{
								int a = fp->cV(k)->Flags();
								int b = fp->cV((k+1)%3)->Flags();
								if (a>b)
									std::swap(a,b);
								Edge e(a,b);
								Edge2Index::iterator it = edge2index.find(e);
								if (it!=edge2index.end())
									stream << it->second+1 << " ";
								else
									return 2; // internal error
							}
							stream << "\n";
						}
					}

					// Recupera i flag originali
					for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
						(*vi).Flags()=FlagV[j++];


					int result = 0;
					if (stream.status() != QTextStream::Ok) result = 3;
					stream.flush();
					return result;
				}

        static const char *ErrorMsg(int error)
        {
			static std::vector<std::string> off_error_msg;
			if (off_error_msg.empty())
			{
				off_error_msg.resize(4);
				off_error_msg[0] = "No errors";
				off_error_msg[1] = "Can't open file";
				off_error_msg[2] = "Internal error";
				off_error_msg[3] = "Otput Stream Error";
			}

			if (error>3 || error<0) return "Unknown error";
			else return off_error_msg[error].c_str();
        }
        /*
	        returns mask of capability one define with what are the saveable information of the format.
        */
        static int GetExportMaskCapability()
        {
	        int capability = 0;
	        capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
          capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
	        return capability;
        }

			}; // end class
		} // end namespace tri
	} // end namespace io
} // end namespace vcg
//@}
#endif