Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

importMRDAT.hpp 8.27 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library 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 Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

namespace Import
{

inline void nextNonEmptyLine(std::ifstream& fp, std::string& line)
{
	do {
		std::getline(fp, line) ;
	} while (line.size() == 0) ;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
42
bool importMRDAT(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames, QuadTree& qt)
Pierre Kraemer's avatar
Pierre Kraemer committed
43
{
Pierre Kraemer's avatar
Pierre Kraemer committed
44
45
46
47
48
49
50
	AttributeHandler<typename PFP::VEC3> position = map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;

	if (!position.isValid())
		position = map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;

	attrNames.push_back(position.name()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
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
	AttributeContainer& container = map.getAttributeContainer(VERTEX) ;

	// open file
	std::ifstream fp(filename.c_str(), std::ios::in) ;
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
		return false ;
	}

	std::string line ;

	nextNonEmptyLine(fp, line) ;
	if (line.rfind("Multires data file") == std::string::npos)
	{
		CGoGNerr << "Problem reading MRDAT file" << CGoGNendl ;
		CGoGNerr << line << CGoGNendl ;
		return false ;
	}

	// read the depth
	unsigned int depth ;
	{
		nextNonEmptyLine(fp, line) ;
		std::stringstream oss(line) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
76
77
		std::string s ;
		oss >> s ;
Pierre Kraemer's avatar
Pierre Kraemer committed
78
79
		oss >> depth ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
80
81

	std::cout << "  MR depth -> " << depth << std::endl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
82
83
84
85
86
87
88
89
90
91

	// read vertices
	nextNonEmptyLine(fp, line) ;
	if (line.rfind("Vertices") == std::string::npos)
	{
		CGoGNerr << "Problem reading MRDAT file" << CGoGNendl ;
		CGoGNerr << line << CGoGNendl ;
		return false ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
92
93
	std::cout << "  Read vertices.." << std::flush ;

Pierre Kraemer's avatar
Pierre Kraemer committed
94
	std::vector<unsigned int> verticesID ;
Pierre Kraemer's avatar
Pierre Kraemer committed
95
	std::vector<unsigned int> verticesLevel ;
Pierre Kraemer's avatar
Pierre Kraemer committed
96
97

	nextNonEmptyLine(fp, line) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
98
	while(line.rfind("Triangles") == std::string::npos)
Pierre Kraemer's avatar
Pierre Kraemer committed
99
100
101
102
103
104
105
106
107
108
	{
		std::stringstream oss(line) ;

		unsigned int level ;
		oss >> level ;

		float x, y, z ;
		oss >> x ;
		oss >> y ;
		oss >> z ;
Pierre Kraemer's avatar
Pierre Kraemer committed
109
110
111
112
113
		typename PFP::VEC3 pos(x, y, z) ;

		unsigned int id = container.insertLine() ;
		position[id] = pos ;
		verticesID.push_back(id) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
114
		verticesLevel.push_back(level) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
115
116
117
118

		nextNonEmptyLine(fp, line) ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
119
	std::cout << "..done (nb vertices -> " << verticesID.size() << ")" << std::endl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
120
121
	std::cout << "  Read triangles (build quadtree).." << std::flush ;

Pierre Kraemer's avatar
Pierre Kraemer committed
122
123
//	QuadTree qt ;
	QuadTreeNode* current = NULL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
124
	unsigned int currentLevel = -1 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
125
126
127

	std::vector<unsigned int> lastNum ;
	lastNum.resize(depth + 1) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
128
129

	nextNonEmptyLine(fp, line) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
130
	while(line.rfind("end") == std::string::npos)
Pierre Kraemer's avatar
Pierre Kraemer committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	{
		std::stringstream oss(line) ;

		std::string name ;
		oss >> name ;

		unsigned int num, root, idx0, idx1, idx2 ;
		oss >> num ;
		oss >> root ;
		oss >> idx0 ;
		oss >> idx1 ;
		oss >> idx2 ;

		if(root == 1)
		{
			assert(num == 0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
147
148
149
150
			QuadTreeNode* n = new QuadTreeNode() ;
//			assert(depth + 1 - verticesLevel[idx0] == 0) ;
//			assert(depth + 1 - verticesLevel[idx1] == 0) ;
//			assert(depth + 1 - verticesLevel[idx2] == 0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
151
152
153
//			assert(verticesLevel[idx0] == 1) ;
//			assert(verticesLevel[idx1] == 1) ;	// pour les exports de triReme
//			assert(verticesLevel[idx2] == 1) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
154
155
156
157
158
			n->indices[0] = idx0 ;
			n->indices[1] = idx1 ;
			n->indices[2] = idx2 ;
			qt.roots.push_back(n) ;
			current = n ;
Pierre Kraemer's avatar
Pierre Kraemer committed
159
			currentLevel = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
160
			lastNum[0] = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
161
162
163
		}
		else
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
164
			if(num == lastNum[currentLevel] + 1) // on lit un autre triangle du même niveau
Pierre Kraemer's avatar
Pierre Kraemer committed
165
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
166
				current = current->parent->children[num] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
167
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
168
			else // on monte ou on descend d'un niveau
Pierre Kraemer's avatar
Pierre Kraemer committed
169
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
170
				if(num == 0) // on subdivise le triangle courant
Pierre Kraemer's avatar
Pierre Kraemer committed
171
172
				{
					current->subdivide() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
173
					current = current->children[0] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
174
					++currentLevel ;
Pierre Kraemer's avatar
Pierre Kraemer committed
175
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
176
				else // on remonte d'un niveau
Pierre Kraemer's avatar
Pierre Kraemer committed
177
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
178
179
180
181
182
183
					assert(lastNum[currentLevel] == 3) ;
					do
					{
						current = current->parent->parent->children[num] ;
						--currentLevel ;
					} while(lastNum[currentLevel] == 3) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
184
185
				}
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
186
187
188
//			assert(depth + 1 - verticesLevel[idx0] <= currentLevel) ;
//			assert(depth + 1 - verticesLevel[idx1] <= currentLevel) ;
//			assert(depth + 1 - verticesLevel[idx2] <= currentLevel) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
189
190
191
//			assert(verticesLevel[idx0] <= currentLevel + 1) ;
//			assert(verticesLevel[idx1] <= currentLevel + 1) ;	// pour les exports de triReme
//			assert(verticesLevel[idx2] <= currentLevel + 1) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
192
193
194
			current->indices[0] = idx0 ;
			current->indices[1] = idx1 ;
			current->indices[2] = idx2 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
195
			lastNum[currentLevel] = num ;
Pierre Kraemer's avatar
Pierre Kraemer committed
196
197
198
199
200
		}

		nextNonEmptyLine(fp, line) ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
201
202
	std::cout << "..done" << std::endl ;

Pierre Kraemer's avatar
Pierre Kraemer committed
203
204
	fp.close() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
205
206
	std::cout << "  Create base level mesh.." << std::flush ;

Pierre Kraemer's avatar
Pierre Kraemer committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	AutoAttributeHandler< NoMathIONameAttribute< std::vector<Dart> > > vecDartsPerVertex(map, VERTEX, "incidents") ;
	DartMarkerNoUnmark m(map) ;

	unsigned nbf = qt.roots.size() ;

	// for each root face
	for(unsigned int i = 0; i < nbf; ++i)
	{
		Dart d = map.newFace(3, false) ;
		qt.darts.push_back(d) ;
		for (unsigned int j = 0; j < 3; ++j)
		{
			unsigned int idx = qt.roots[i]->indices[j] ;
			unsigned int emb = verticesID[idx] ;

			FunctorSetEmb<typename PFP::MAP> fsetemb(map, VERTEX, emb) ;
			map.foreach_dart_of_orbit(PFP::MAP::ORBIT_IN_PARENT(VERTEX), d, fsetemb) ;

			m.mark(d) ;								// mark on the fly to unmark on second loop
			vecDartsPerVertex[emb].push_back(d) ;	// store incident darts for fast adjacency reconstruction
			d = map.phi1(d) ;
		}
	}

	// reconstruct neighbourhood between root faces
	unsigned int nbBoundaryEdges = 0 ;
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (m.isMarked(d))
		{
			// darts incident to end vertex of edge
			std::vector<Dart>& vec = vecDartsPerVertex[map.phi1(d)] ;

			unsigned int embd = map.getEmbedding(VERTEX, d) ;
			Dart good_dart = NIL ;
			for (typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
			{
				if (map.getEmbedding(VERTEX, map.phi1(*it)) == embd)
					good_dart = *it ;
			}

			if (good_dart != NIL)
			{
				map.sewFaces(d, good_dart, false) ;
				m.unmarkOrbit(EDGE, d) ;
			}
			else
			{
				m.unmark(d) ;
				++nbBoundaryEdges ;
			}
		}
	}

	if (nbBoundaryEdges > 0)
	{
//		map.closeMap() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
264
		CGoGNout << "Open mesh.. not managed yet.." << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
265
266
267
		return false ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
268
269
270
	std::cout << "..done" << std::endl ;
	std::cout << "  Create finer resolution levels.." << std::flush ;

Pierre Kraemer's avatar
Pierre Kraemer committed
271
272
273
	for(unsigned int i = 0; i < depth; ++i)
		map.addNewLevel(false) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
274
275
276
	std::cout << "..done" << std::endl ;
	std::cout << "  Embed finer resolution levels.." << std::flush ;

277
	map.setCurrentLevel(0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
278
	qt.embed<PFP>(map, verticesID, verticesLevel) ;
279
	map.setCurrentLevel(map.getMaxLevel()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
280

Pierre Kraemer's avatar
Pierre Kraemer committed
281
282
	std::cout << "..done" << std::endl ;

Pierre Kraemer's avatar
Pierre Kraemer committed
283
	return true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
284
285
286
287
288
289
290
}

} // namespace Import

} // namespace Algo

} // namespace CGoGN