ihm2_PrimalRegular.hpp 9.58 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
/*******************************************************************************
* 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 MR
{

namespace Primal
{

namespace Regular
{

template <typename PFP>
IHM2<PFP>::IHM2(typename PFP::MAP& map) :
42 43
	m_map(map),
	shareVertexEmbeddings(true)
44 45 46 47 48 49 50 51
{

}

//if true : tri and quad else quad
template <typename PFP>
void IHM2<PFP>::addNewLevel(bool triQuad)
{
52
	unsigned int cur = m_map.getCurrentLevel() ; //pushLevel
53

54
	m_map.setCurrentLevel(m_map.getMaxLevel() + 1) ;
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
	// cut edges
	TraversorE<typename PFP::MAP> travE(m_map) ;
	for (Dart d = travE.begin(); d != travE.end(); d = travE.next())
	{
		Dart dd = m_map.phi2(d) ;

		m_map.cutEdge(d) ;
		unsigned int eId = m_map.getEdgeId(d) ;
		m_map.setEdgeId(m_map.phi1(d), eId) ;
		m_map.setEdgeId(m_map.phi1(dd), eId) ;

		travE.skip(d) ;
		travE.skip(m_map.phi1(d)) ;
	}

	// split faces
	TraversorF<typename PFP::MAP> travF(m_map) ;
	for (Dart d = travF.begin(); d != travF.end(); d = travF.next())
	{
		Dart old = d ;

		if(m_map.getDartLevel(old) == m_map.getMaxLevel())
			old = m_map.phi1(old) ;

		m_map.decCurrentLevel();
		unsigned int degree = m_map.faceDegree(old) ;
		m_map.incCurrentLevel();

		if((degree == 3) && triQuad)								// if subdividing a triangle
		{
			Dart dd = m_map.phi1(old) ;
			Dart e = m_map.phi1(m_map.phi1(dd)) ;
			m_map.splitFace(dd, e) ;					// insert a new edge
			travF.skip(dd) ;
			//unsigned int id = m_map.getNewEdgeId() ;
			unsigned int id = m_map.getTriRefinementEdgeId(m_map.phi_1(dd));
			m_map.setEdgeId(m_map.phi_1(dd), id) ;		// set the edge id of the inserted
			m_map.setEdgeId(m_map.phi_1(e), id) ;		// edge to the next available id

			dd = e ;
			e = m_map.phi1(m_map.phi1(dd)) ;
			m_map.splitFace(dd, e) ;
			travF.skip(dd) ;
			//id = m_map.getNewEdgeId() ;
			id = m_map.getTriRefinementEdgeId(m_map.phi_1(dd));
			m_map.setEdgeId(m_map.phi_1(dd), id) ;
			m_map.setEdgeId(m_map.phi_1(e), id) ;

			dd = e ;
			e = m_map.phi1(m_map.phi1(dd)) ;
			m_map.splitFace(dd, e) ;
			travF.skip(dd) ;
			//id = m_map.getNewEdgeId() ;
			id = m_map.getTriRefinementEdgeId(m_map.phi_1(dd));
			m_map.setEdgeId(m_map.phi_1(dd), id) ;
			m_map.setEdgeId(m_map.phi_1(e), id) ;

			travF.skip(e) ;
		}
		else											// if subdividing a polygonal face
		{
			Dart dd = m_map.phi1(old) ;
			Dart next = m_map.phi1(m_map.phi1(dd)) ;
			m_map.splitFace(dd, next) ;		// insert a first edge

			Dart ne = m_map.phi2(m_map.phi_1(dd)) ;
			Dart ne2 = m_map.phi2(ne) ;
			m_map.cutEdge(ne) ;				// cut the new edge to insert the central vertex
			travF.skip(dd) ;
			//unsigned int id = m_map.getNewEdgeId() ;
			unsigned int id = m_map.getQuadRefinementEdgeId(m_map.phi2(ne));
			m_map.setEdgeId(ne, id) ;
			m_map.setEdgeId(m_map.phi2(ne), id) ;			// set the edge id of the inserted
			//id = m_map.getNewEdgeId() ;
			id = m_map.getQuadRefinementEdgeId(ne2);
			m_map.setEdgeId(ne2, id) ;					// edges to the next available ids
			m_map.setEdgeId(m_map.phi2(ne2), id) ;

			dd = m_map.phi1(m_map.phi1(next)) ;
			while(dd != ne)				// turn around the face and insert new edges
			{							// linked to the central vertex
				Dart tmp = m_map.phi1(ne) ;
				m_map.splitFace(tmp, dd) ;
				travF.skip(tmp) ;
				Dart nne = m_map.phi2(m_map.phi_1(dd)) ;
				//id = m_map.getNewEdgeId() ;
				id = m_map.getQuadRefinementEdgeId(m_map.phi2(nne));
				m_map.setEdgeId(nne, id) ;
				m_map.setEdgeId(m_map.phi2(nne), id) ;
				dd = m_map.phi1(m_map.phi1(dd)) ;
			}
			travF.skip(ne) ;
		}
	}

	m_map.setCurrentLevel(cur) ;
152 153 154 155 156
}

template <typename PFP>
void IHM2<PFP>::addLevelFront()
{
157 158
	std::vector<Dart> irregVertices;
	irregVertices.reserve(1024);
159 160 161 162 163 164 165 166 167 168

	//look for an irregular vertex

	TraversorV<typename PFP::MAP> tv(m_map);
	bool found = false;
	for(Dart d = tv.begin() ; !found && d != tv.end() ; d = tv.next())
	{
		if(m_map.vertexDegree(d) != 6)
		{
			found = true;
169
			irregVertices.push_back(d);
170 171 172
		}
	}

173 174 175 176 177 178 179 180 181 182 183 184
	//found the number of levels
	bool finished = false;
	Dart dit = irregVertices[0];
	unsigned int nbSteps = 0;
	do
	{
		dit = m_map.phi1(m_map.phi2(m_map.phi1(m_map.phi2(m_map.phi1(dit)))));
		++nbSteps;

		if(m_map.vertexDegree(m_map.phi1(dit)) != 6)
			finished = true;
	}while(!finished);
185

186 187 188 189
	++nbSteps;

	unsigned int nbLevel = 0;
	while(nbSteps > 1)
190
	{
191 192 193 194 195 196
		nbSteps /= 2 ;
		++nbLevel ;
	}
	m_map.setMaxLevel(nbLevel);

	std::cout << "nb levels = " << nbLevel+1 << std::endl;
197

198 199 200 201 202 203 204 205 206 207 208 209
	unsigned int curLevel = nbLevel;

	do
	{
		m_map.setCurrentLevel(curLevel);

		DartMarker md(m_map);
		std::vector<Dart> visitedVertices(irregVertices);

		std::cout << "getCurrentLevel = " << m_map.getCurrentLevel() << std::endl;

		for(unsigned int i = 0 ; i < visitedVertices.size() ; ++i)
210
		{
211
			Dart d = visitedVertices[i];
212

213 214 215 216 217
			Traversor2VE<typename PFP::MAP> tve(m_map, d);
			for(Dart eit = tve.begin() ; eit != tve.end() ; eit = tve.next())
			{
				//coarse all faces around the vertex
				if(!md.isMarked(eit))
218 219 220 221 222 223 224 225
				{
					Dart fit1 = m_map.phi2(m_map.phi1(eit));
					Dart fit2 = m_map.phi1(fit1);
					Dart fit3 = m_map.phi1(fit2);

					md.markOrbit<FACE>(fit1);
					md.markOrbit<FACE>(m_map.phi2(fit2));
					md.markOrbit<FACE>(m_map.phi2(fit3));
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
					md.markOrbit<FACE>(eit);

					visitedVertices.push_back(m_map.phi_1(m_map.phi2(fit2)));
					visitedVertices.push_back(m_map.phi_1(m_map.phi2(fit3)));

					m_map.setDartLevel(fit1, curLevel);
					m_map.setDartLevel(m_map.phi2(fit1), curLevel);
					m_map.setDartLevel(m_map.phi1(m_map.phi2(fit1)), curLevel);

					m_map.setDartLevel(fit2, curLevel);
					m_map.setDartLevel(m_map.phi2(fit2), curLevel);
					m_map.setDartLevel(m_map.phi1(m_map.phi2(fit2)), curLevel);

					m_map.setDartLevel(fit3, curLevel);
					m_map.setDartLevel(m_map.phi2(fit3), curLevel);
					m_map.setDartLevel(m_map.phi1(m_map.phi2(fit3)), curLevel);

					if(curLevel == maxLevel)
					{
						unsigned int id = m_map.getTriRefinementEdgeId(m_map.phi2(fit1));
						m_map.setEdgeId(m_map.phi2(fit1), id);
						m_map.setEdgeId(fit1, id);

						id = m_map.getTriRefinementEdgeId(m_map.phi2(fit2));
						m_map.setEdgeId(m_map.phi2(fit2), id);
						m_map.setEdgeId(fit2, id);

						id = m_map.getTriRefinementEdgeId(m_map.phi2(fit3));
						m_map.setEdgeId(m_map.phi2(fit3), id);
						m_map.setEdgeId(fit3, id);
					}
					else
					{

					}
//					if(curLevel == 2)
//					{
//						unsigned int id = m_map.getTriRefinementEdgeId(m_map.phi2(fit1));
//						m_map.setEdgeId(m_map.phi2(fit1), id);
//						m_map.setEdgeId(fit1, id);
						//m_map.setEdgeId(fit1, id);
//						/std::cout << "fit1 = " << fit1 << std::endl;
						//std::cout << "m_map.phi2(fit1) = " << m_map.phi2(fit1) << std::endl;

//						id = m_map.getTriRefinementEdgeId(m_map.phi2(fit2));
//						m_map.setEdgeId(m_map.phi2(fit2), id);
//						m_map.setEdgeId(fit2, id);
						//m_map.setEdgeId(fit2, id);
						//std::cout << "fit2 = " << fit2 << std::endl;
						//std::cout << "m_map.phi2(fit2) = " << m_map.phi2(fit2) << std::endl;

//						id = m_map.getTriRefinementEdgeId(m_map.phi2(fit3));
//						m_map.setEdgeId(m_map.phi2(fit3), id);
//						m_map.setEdgeId(fit3, id);
						//m_map.setEdgeId(fit3, id);
						//std::cout << "fit3 = " << fit3 << std::endl;
						//std::cout << "m_map.phi2(fit3) = " << m_map.phi2(fit3) << std::endl;
//					}
284
				}
285 286
			}
		}
287

288
		curLevel = curLevel - 1;
289

290
	}while(curLevel > 1);
291

292
	m_map.setCurrentLevel(nbLevel); //m_maxLevel
293 294 295 296 297 298
}


template <typename PFP>
void IHM2<PFP>::analysis()
{
299
	assert(m_map.getCurrentLevel() > 0 || !"analysis : called on level 0") ;
300

301
	m_map.decCurrentLevel() ;
302

303 304
	for(unsigned int i = 0; i < analysisFilters.size(); ++i)
		(*analysisFilters[i])() ;
305 306 307 308 309 310

}

template <typename PFP>
void IHM2<PFP>::synthesis()
{
311
	assert(m_map.getCurrentLevel() < m_map.getMaxLevel() || !"synthesis : called on max level") ;
312

313 314
	for(unsigned int i = 0; i < synthesisFilters.size(); ++i)
		(*synthesisFilters[i])() ;
315

316
	m_map.incCurrentLevel() ;
317 318 319 320 321 322 323 324 325 326 327
}

} // namespace Regular

} // namespace Primal

} // namespace MR

} // namespace Algo

} // namespace CGoGN