subdivision3.hpp 10 KB
Newer Older
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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Geometry/centroid.h"
#include "Algo/Modelisation/subdivision.h"

namespace CGoGN
{

namespace Algo
{

namespace IHM3
{

//OK
template <typename PFP>
void subdivideEdge(typename PFP::MAP& map, Dart d, typename PFP::TVEC3& position)
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(!map.edgeIsSubdivided(d) || !"Trying to subdivide an already subdivided edge") ;

	unsigned int eLevel = map.edgeLevel(d) ;

	unsigned int cur = map.getCurrentLevel() ;
	map.setCurrentLevel(eLevel) ;

	std::cout << "eLevel=" << eLevel << std::endl;

	Dart dd = map.phi2(d) ;
	typename PFP::VEC3 p1 = position[d] ;
	typename PFP::VEC3 p2 = position[map.phi1(d)] ;

	map.setCurrentLevel(eLevel + 1) ;

	map.cutEdge(d) ;
	unsigned int eId = map.getEdgeId(d) ;
	map.setEdgeId(map.phi1(d), eId, EDGE_ORBIT) ; //sur chaque moitie d'arete
	map.setEdgeId(map.phi1(dd), eId, EDGE_ORBIT) ; //TODO peut etre a optimiser

	unsigned int fId = map.getFaceId(d);
	map.setFaceId(map.phi1(d), fId, EDGE_ORBIT) ;
	map.setFaceId(map.phi1(dd), fId, EDGE_ORBIT) ;

	position[map.phi1(d)] = (p1 + p2) * typename PFP::REAL(0.5) ;

	map.setCurrentLevel(cur) ;
}

//OK
template <typename PFP>
Dart subdivideFace(typename PFP::MAP& map, Dart d, typename PFP::TVEC3& position)
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(!map.faceIsSubdivided(d) || !"Trying to subdivide an already subdivided face") ;

	unsigned int fLevel = map.faceLevel(d) ;
	Dart old = map.faceOldestDart(d) ;

	unsigned int cur = map.getCurrentLevel() ;
	map.setCurrentLevel(fLevel) ;		// go to the level of the face to subdivide its edges

	std::cout << "fLevel=" << fLevel << std::endl;

	unsigned int degree = 0 ;
	typename PFP::VEC3 p ;
	Dart it = old ;
	do
	{
		++degree;
		p += position[it] ;
		if(!map.edgeIsSubdivided(it))							// first cut the edges (if they are not already)
			Algo::IHM3::subdivideEdge<PFP>(map, it, position) ;	// and compute the degree of the face
		it = map.phi1(it) ;
	} while(it != old) ;
	p /= typename PFP::REAL(degree) ;

	map.setCurrentLevel(fLevel + 1) ;			// go to the next level to perform face subdivision

	Dart dd = map.phi1(old) ;
	map.splitFace(dd, map.phi1(map.phi1(dd))) ;
	unsigned int idface = map.getFaceId(d);
	map.setFaceId(map.phi1(map.phi1(dd)), idface, FACE_ORBIT) ;
	map.setFaceId(map.phi2(map.phi1(map.phi1(dd))), idface, FACE_ORBIT) ;

	Dart ne = map.phi2(map.phi_1(dd));
	Dart ne2 = map.phi2(ne);

	map.cutEdge(ne) ;
	unsigned int id = map.getNewEdgeId() ;
	map.setEdgeId(ne, id, EDGE_ORBIT) ;
	id = map.getNewEdgeId() ;
	map.setEdgeId(ne2, id, EDGE_ORBIT) ;

	Dart res = map.phi2(ne);

	dd = map.phi1(map.phi1(map.phi1(map.phi1(ne)))) ;
	while(dd != ne)
	{
		Dart next = map.phi1(map.phi1(dd)) ;
		map.splitFace(map.phi1(ne), dd) ;
		Dart nne = map.phi2(map.phi_1(dd)) ;

		id = map.getNewEdgeId() ;
		map.setEdgeId(nne, id, EDGE_ORBIT) ;

		map.setFaceId(nne, idface, FACE_ORBIT) ;
		dd = next ;
	}

	position[res] = p ;


	map.setCurrentLevel(cur) ;

	return res;
}

template <typename PFP>
void subdivideVolume(typename PFP::MAP& map, Dart d, typename PFP::TVEC3& position)
{
	assert(map.getDartLevel(d) <= map.getCurrentLevel() || !"Access to a dart introduced after current level") ;
	assert(!map.volumeIsSubdivided(d) || !"Trying to subdivide an already subdivided volume") ;

	unsigned int vLevel = map.volumeLevel(d) ;
	Dart old = map.volumeOldestDart(d) ;


	unsigned int cur = map.getCurrentLevel() ;
	map.setCurrentLevel(vLevel) ;		// go to the level of the face to subdivide its edges

	// compute volume centroid
	typename PFP::VEC3 volCenter;// = Algo::Geometry::volumeCentroidGen<PFP, typename PFP::TVEC3, typename PFP::VEC3>(map, d, position);


	std::cout << "vLevel=" << vLevel << std::endl;

	/*
	 * ou niveau du volume courant i
	 * stockage d'un brin de chaque face de celui-ci
	 */

	DartMarkerStore mf(map);		// Lock a face marker
	DartMarkerStore mv(map);		// Lock a vertex marker

	//Store faces that are traversed and start with the face of d
	std::vector<Dart> visitedFaces;
	visitedFaces.reserve(20);
	visitedFaces.push_back(old);

	//Store the edges before the cutEdge
	std::vector<Dart> oldEdges;
	oldEdges.reserve(20);

	//Store the darts from quadrangulated faces
	std::vector<Dart> quadfaces;
	quadfaces.reserve(100);


	mf.markOrbit(FACE_ORBIT, old) ;
	for(std::vector<Dart>::iterator face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
	{
		Dart e = *face ;

		do	// add all face neighbours to the table
		{
			//if(!mv.isMarked(e))
			//{
				oldEdges.push_back(e);
			//	mv.markOrbit(VERTEX_ORBIT, e);
			//}
			Dart ee = map.phi2(e) ;
			if(!mf.isMarked(ee)) // not already marked
			{
				visitedFaces.push_back(ee) ;
				mf.markOrbit(FACE_ORBIT, ee) ;
			}
			e = map.phi1(e) ;
		} while(e != *face) ;
	}



	DartMarker me(map) ; //mark edge

	unsigned int degree = 0 ;
	DartMarker mvolcent(map); //mark vertices vol centroid computing

	//First step : subdivide edges and faces
	//creates a i+1 edge level and i+1 face level
	for (std::vector<Dart>::iterator face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
	{
		Dart d = *face;


			Dart it = d ;
			do
			{
				if(!mvolcent.isMarked(it))
				{
					++degree;
					volCenter += position[it] ;
					mvolcent.markOrbit(VERTEX_ORBIT, it);
				}
				it = map.phi1(it) ;
			} while(it != d) ;



		std::cout << "subdiv de toutes les faces" << std::endl;
		if(!map.faceIsSubdivided(d))
		{

			Dart cf = Algo::IHM3::subdivideFace<PFP>(map, *face, position);
			std::cout << "face faite" << std::endl;


			unsigned int fLevel = map.getDartLevel(cf) ;

			map.setCurrentLevel(fLevel) ;

			Dart e = cf;
			do
			{
				quadfaces.push_back(e);
				quadfaces.push_back(map.phi2(e));
				e = map.phi2(map.phi_1(e));
			}while (e != cf);
		}
	}

	volCenter /= typename PFP::REAL(degree) ;

	map.setCurrentLevel(vLevel + 1) ; // go to the next level to perform volume subdivision

	std::cout << "vLevel+1=" << vLevel + 1 << std::endl;

	DartMarker moe(map) ; //mark edge

	//Third pass : deconnect the corners
	for (std::vector<Dart>::iterator edge = oldEdges.begin(); edge != oldEdges.end(); ++edge)
	{

//					Dart e = *edge;
//					do
//					{
//						map.unsewFaces(map.phi1(map.phi1(*edge)));
//						map.unsewFaces(map.phi1(*edge));
//						e = map.phi2(map.phi_1(e));
//					}while (e != *edge);
		map.unsewFaces(map.phi1(*edge));
		moe.markOrbit(DART_ORBIT,map.phi1(*edge));
	}

	//Thourth pass : close the hole
	for (std::vector<Dart>::iterator edge = oldEdges.begin(); edge != oldEdges.end(); ++edge)
	{
		if(moe.isMarked(map.phi1(*edge)))
		{
			map.closeHole(map.phi1(*edge));

			Dart h = map.phi2(map.phi1(*edge));
			do
			{
				map.setEdgeId(h,map.getEdgeId(map.phi2(h)), EDGE_ORBIT) ;
				h = map.phi1(h);
			}while(h != map.phi2(map.phi1(*edge)));

			moe.unmark(map.phi1(*edge));
			moe.unmark(map.phi1(map.phi2(map.phi_1(*edge))));
			moe.unmark(map.phi1(map.phi1(map.phi2(*edge))));

			Dart d = map.phi1(map.phi2(map.phi1(*edge)));
			Dart old = d;
			Dart dd = map.phi1(old) ;
			map.splitFace(dd, map.phi1(map.phi1(dd))) ;

			Dart ne = map.phi2(map.phi_1(dd));
			Dart ne2 = map.phi2(ne);

			map.cutEdge(ne) ;
			unsigned int id = map.getNewEdgeId() ;
			map.setEdgeId(ne, id, EDGE_ORBIT) ;

			id = map.getNewEdgeId() ;
			map.setEdgeId(ne2, id, EDGE_ORBIT) ;

			position[map.phi2(ne)] = volCenter;

			dd = map.phi1(map.phi1(map.phi1(map.phi1(ne)))) ;
			while(dd != ne)
			{
				Dart next = map.phi1(map.phi1(dd)) ;
				map.splitFace(map.phi1(ne), dd) ;
				Dart nne = map.phi2(map.phi_1(dd)) ;

				id = map.getNewEdgeId() ;
				map.setEdgeId(nne, id, EDGE_ORBIT) ;

				dd = next ;
			}
		}
	}

	moe.unmarkAll();


	for (std::vector<Dart>::iterator nvol = quadfaces.begin(); nvol != quadfaces.end(); nvol = nvol + 2)
	{
		if(map.phi3(map.phi2(*nvol)) == map.phi2(*nvol) && map.phi3(map.phi2(*(nvol+1))) == map.phi2(*(nvol+1)))
		{
			map.sewVolumes(map.phi2(*nvol), map.phi2(*(nvol+1)));
			unsigned int id = map.getNewFaceId();
			map.setFaceId(map.phi2(*nvol),id, FACE_ORBIT);
		}
	}



	map.setCurrentLevel(cur) ;

	std::cout << std::endl << std::endl;
}

template <typename PFP>
void catmullClarck(typename PFP::MAP& map, Dart d, typename PFP::TVEC3& position)
{


}

} //namespace IHM

} //namespace Algo

} //namespace CGoGN