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 7.77 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
96

	nextNonEmptyLine(fp, line) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
97
	while(line.rfind("Triangles") == std::string::npos)
Pierre Kraemer's avatar
Pierre Kraemer committed
98
99
100
101
102
103
104
105
106
107
	{
		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
108
109
110
111
112
		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
113
114
115
116

		nextNonEmptyLine(fp, line) ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
117
118
119
	std::cout << "..done" << std::endl ;
	std::cout << "  Read triangles (build quadtree).." << std::flush ;

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

	nextNonEmptyLine(fp, line) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
125
	while(line.rfind("end") == std::string::npos)
Pierre Kraemer's avatar
Pierre Kraemer committed
126
127
128
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 ;

		if(root == 1)
		{
			assert(num == 0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
142
			QuadTreeNode<PFP>* n = new QuadTreeNode<PFP>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
143
144
145
146
147
148
149
150
151
			n->indices[0] = idx0 ;
			n->indices[1] = idx1 ;
			n->indices[2] = idx2 ;
			qt.roots.push_back(n) ;
			current = n ;
			prevNum = 0 ;
		}
		else
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
152
			if(num == prevNum + 1) // on lit un autre triangle du même niveau
Pierre Kraemer's avatar
Pierre Kraemer committed
153
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
154
				current = current->parent->children[num] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
155
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
156
			else // on monte ou on descend d'un niveau
Pierre Kraemer's avatar
Pierre Kraemer committed
157
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
158
				if(num == 0) // on subdivise le triangle courant
Pierre Kraemer's avatar
Pierre Kraemer committed
159
160
				{
					current->subdivide() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
161
					current = current->children[0] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
162
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
163
				else // on remonte d'un niveau
Pierre Kraemer's avatar
Pierre Kraemer committed
164
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
165
166
167
					assert(prevNum == 3) ;
					assert(current->parent->parent != NULL) ;
					current = current->parent->parent->children[num] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
168
169
				}
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
170
171
172
173
			current->indices[0] = idx0 ;
			current->indices[1] = idx1 ;
			current->indices[2] = idx2 ;
			prevNum = num ;
Pierre Kraemer's avatar
Pierre Kraemer committed
174
175
176
177
178
		}

		nextNonEmptyLine(fp, line) ;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
179
180
	std::cout << "..done" << std::endl ;

Pierre Kraemer's avatar
Pierre Kraemer committed
181
182
	fp.close() ;

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

Pierre Kraemer's avatar
Pierre Kraemer committed
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
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
	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
246
247
248
	std::cout << "..done" << std::endl ;
	std::cout << "  Create finer resolution levels.." << std::flush ;

Pierre Kraemer's avatar
Pierre Kraemer committed
249
250
251
	for(unsigned int i = 0; i < depth; ++i)
		map.addNewLevel(false) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
252
253
254
	std::cout << "..done" << std::endl ;
	std::cout << "  Embed finer resolution levels.." << std::flush ;

255
	map.setCurrentLevel(0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
256
	qt.embed(map, verticesID) ;
257
	map.setCurrentLevel(map.getMaxLevel()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
258

259
260
	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
261
	{
262
263
264
265
266
267
268
269
270
271
272
273
274
		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
275
276
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
277
278
	std::cout << "..done" << std::endl ;

Pierre Kraemer's avatar
Pierre Kraemer committed
279
	return true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
280
281
282
283
284
285
286
}

} // namespace Import

} // namespace Algo

} // namespace CGoGN