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.39 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)
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
120
121
	std::cout << "..done" << std::endl ;
	std::cout << "  Read triangles (build quadtree).." << std::flush ;

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

	nextNonEmptyLine(fp, line) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
128
	while(line.rfind("end") == std::string::npos)
Pierre Kraemer's avatar
Pierre Kraemer committed
129
130
131
132
133
134
135
136
137
138
139
140
141
	{
		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 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
142
143
144
145
146
		std::cout << num << " (" << currentLevel << ")" ;
		if(root == 1)
			std::cout << " / root" ;
		std::cout << std::endl ;

Pierre Kraemer's avatar
Pierre Kraemer committed
147
148
149
		if(root == 1)
		{
			assert(num == 0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
150
			QuadTreeNode<PFP>* n = new QuadTreeNode<PFP>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
151
152
153
			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
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
161
162
163
			prevNum = 0 ;
		}
		else
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
164
			if(num == prevNum + 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
					assert(prevNum == 3) ;
					assert(current->parent->parent != NULL) ;
					current = current->parent->parent->children[num] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
181
					--currentLevel ;
Pierre Kraemer's avatar
Pierre Kraemer committed
182
183
				}
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
184
185
186
			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
187
188
189
190
			current->indices[0] = idx0 ;
			current->indices[1] = idx1 ;
			current->indices[2] = idx2 ;
			prevNum = num ;
Pierre Kraemer's avatar
Pierre Kraemer committed
191
192
193
194
195
		}

		nextNonEmptyLine(fp, line) ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
196
197
	std::cout << "..done" << std::endl ;

Pierre Kraemer's avatar
Pierre Kraemer committed
198
199
	fp.close() ;

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

Pierre Kraemer's avatar
Pierre Kraemer committed
202
203
204
205
206
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
	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() ;
		CGoGNout << "Open mesh.. not managed for now.." << CGoGNendl ;
		return false ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
263
264
265
	std::cout << "..done" << std::endl ;
	std::cout << "  Create finer resolution levels.." << std::flush ;

Pierre Kraemer's avatar
Pierre Kraemer committed
266
267
268
	for(unsigned int i = 0; i < depth; ++i)
		map.addNewLevel(false) ;

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

272
	map.setCurrentLevel(0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
273
	qt.embed(map, verticesID) ;
274
	map.setCurrentLevel(map.getMaxLevel()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
275

276
277
	TraversorV<typename PFP::MAP> tv(map, allDarts, true) ;
	for(Dart d = tv.begin(); d != tv.end(); d = tv.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
278
	{
279
280
281
282
283
284
285
286
287
288
289
290
291
		unsigned int vertexLevel = map.getDartLevel(d) ;
		if(vertexLevel > 0 && vertexLevel < map.getMaxLevel())
		{
			map.pushLevel() ;
			map.setCurrentLevel(vertexLevel) ;
			unsigned int emb = map.getEmbedding(VERTEX, d) ;
			for(unsigned int i = vertexLevel + 1; i <= map.getMaxLevel(); ++i)
			{
				map.setCurrentLevel(i) ;
				map.embedOrbit(VERTEX, d, emb) ;
			}
			map.popLevel() ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
292
293
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
294
295
	std::cout << "..done" << std::endl ;

Pierre Kraemer's avatar
Pierre Kraemer committed
296
	return true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
297
298
299
300
301
302
303
}

} // namespace Import

} // namespace Algo

} // namespace CGoGN