subdivision3.hpp 11.2 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
/*******************************************************************************
* 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
{

untereiner's avatar
untereiner committed
34
namespace IHM
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
{

//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)
untereiner's avatar
untereiner committed
94
			Algo::IHM::subdivideEdge<PFP>(map, it, position) ;	// and compute the degree of the face
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
		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


untereiner's avatar
untereiner committed
154
	std::cout << "vLevel to subdivide volume=" << vLevel << std::endl;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

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

	DartMarkerStore mf(map);		// Lock a face 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);

	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
		{
untereiner's avatar
untereiner committed
179 180 181

			oldEdges.push_back(e);

182 183 184 185 186 187 188 189 190 191
			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) ;
	}

untereiner's avatar
untereiner committed
192 193 194
	/*
	 *
	 */
195

untereiner's avatar
untereiner committed
196 197
	// compute volume centroid
	typename PFP::VEC3 volCenter = Algo::Geometry::volumeCentroidGen<PFP, typename PFP::TVEC3, typename PFP::VEC3>(map, d, position);
198 199


untereiner's avatar
untereiner committed
200 201 202
	//Store the darts from quadrangulated faces
	std::vector<Dart> quadfaces;
	quadfaces.reserve(100);
203 204 205 206 207 208 209

	//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;

untereiner's avatar
untereiner committed
210 211 212
		std::cout << "subdiv de toutes les faces" << std::endl;
		if(!map.faceIsSubdivided(d))
		{
213

untereiner's avatar
untereiner committed
214 215 216 217 218 219 220 221 222 223 224 225

			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 ;
226 227
			do
			{
untereiner's avatar
untereiner committed
228 229 230 231
				++degree;
				p += position[it] ;
				if(!map.edgeIsSubdivided(it))							// first cut the edges (if they are not already)
					Algo::IHM::subdivideEdge<PFP>(map, it, position) ;	// and compute the degree of the face
232
				it = map.phi1(it) ;
untereiner's avatar
untereiner committed
233 234
			} while(it != old) ;
			p /= typename PFP::REAL(degree) ;
235

untereiner's avatar
untereiner committed
236
			map.setCurrentLevel(fLevel + 1) ;			// go to the next level to perform face subdivision
237

untereiner's avatar
untereiner committed
238 239 240 241 242
			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) ;
243

untereiner's avatar
untereiner committed
244 245
			Dart ne = map.phi2(map.phi_1(dd));
			Dart ne2 = map.phi2(ne);
246

untereiner's avatar
untereiner committed
247 248 249 250 251
			map.cutEdge(ne) ;
			unsigned int id = map.getNewEdgeId() ;
			map.setEdgeId(ne, id, EDGE_ORBIT) ;
			id = map.getNewEdgeId() ;
			map.setEdgeId(ne2, id, EDGE_ORBIT) ;
252

untereiner's avatar
untereiner committed
253
			Dart res = map.phi2(ne);
254

untereiner's avatar
untereiner committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
			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 ;
270

untereiner's avatar
untereiner committed
271
			Dart cf = res;
272 273 274 275 276 277 278 279

			Dart e = cf;
			do
			{
				quadfaces.push_back(e);
				quadfaces.push_back(map.phi2(e));
				e = map.phi2(map.phi_1(e));
			}while (e != cf);
untereiner's avatar
untereiner committed
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


			map.setCurrentLevel(cur) ;













			//Dart cf = Algo::IHM::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);
312 313 314
		}
	}

untereiner's avatar
untereiner committed
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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	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)
	{
		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