pliant.hpp 5.31 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
/*******************************************************************************
* 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/basic.h"

namespace CGoGN
{

namespace Algo
{

namespace Remeshing
{

template <typename PFP>
37
void pliantRemeshing(typename PFP::MAP& map, typename PFP::TVEC3& position, typename PFP::TVEC3& normal)
38 39 40 41
{
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

42 43
	// compute the mean edge length
	DartMarker m1(map) ;
44 45 46 47
	REAL meanEdgeLength = 0 ;
	unsigned int nbEdges = 0 ;
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
48
		if(!m1.isMarked(d))
49
		{
50
			m1.markOrbit(EDGE_ORBIT, d) ;
51 52 53 54 55 56
			meanEdgeLength += Algo::Geometry::edgeLength<PFP>(map, d, position) ;
			++nbEdges ;
		}
	}
	meanEdgeLength /= REAL(nbEdges) ;

57 58
	// compute the min and max edge lengths
	REAL edgeLengthInf = REAL(3) / REAL(4) * meanEdgeLength ;
59 60
	REAL edgeLengthSup = REAL(4) / REAL(3) * meanEdgeLength ;

61 62
	// split long edges
	DartMarker m2(map) ;
63 64
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
65
		if(!m2.isMarked(d))
66
		{
67
			m2.markOrbit(EDGE_ORBIT, d) ;
68 69 70 71 72 73 74 75 76 77 78 79 80
			REAL length = Algo::Geometry::edgeLength<PFP>(map, d, position) ;
			if(length > edgeLengthSup)
			{
				Dart dd = map.phi2(d) ;
				VEC3 p = REAL(0.5) * (position[d] + position[dd]) ;
				map.cutEdge(d) ;
				position[map.phi1(d)] = p ;
				map.splitFace(map.phi1(d), map.phi_1(d)) ;
				map.splitFace(map.phi1(dd), map.phi_1(dd)) ;
			}
		}
	}

81 82
	// collapse short edges
	DartMarker m3(map) ;
83 84
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
85
		if(!m3.isMarked(d))
86
		{
87
			m3.markOrbit(EDGE_ORBIT, d) ;
88
			REAL length = Algo::Geometry::edgeLength<PFP>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
89
			if(length < edgeLengthInf && map.edgeCanCollapse(d))
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
			{
				bool collapse = true ;
				Dart dd = map.phi2(d) ;
				VEC3 p = position[dd] ;
				Dart vit = map.alpha1(d) ;
				do
				{
					VEC3 vec = position[dd] - position[map.phi1(vit)] ;
					if(vec.norm() > edgeLengthSup)
						collapse = false ;
					vit = map.alpha1(vit) ;
				} while(vit != d && collapse) ;
				if(collapse)
				{
					Dart v = map.collapseEdge(d) ;
					position[v] = p ;
				}
			}
		}
	}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

	// equalize valences with edge flips
	DartMarker m4(map) ;
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(!m4.isMarked(d))
		{
			m4.markOrbit(EDGE_ORBIT, d) ;
			Dart e = map.phi2(d) ;
			unsigned int w = map.vertexDegree(d) ;
			unsigned int x = map.vertexDegree(e) ;
			unsigned int y = map.vertexDegree(map.phi1(map.phi1(d))) ;
			unsigned int z = map.vertexDegree(map.phi1(map.phi1(e))) ;
			int flip = 0 ;
			flip += w > 6 ? 1 : (w < 6 ? -1 : 0) ;
			flip += x > 6 ? 1 : (x < 6 ? -1 : 0) ;
			flip += y < 6 ? 1 : (y > 6 ? -1 : 0) ;
			flip += z < 6 ? 1 : (z > 6 ? -1 : 0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
128
			if(flip > 1)
129 130 131 132 133 134 135 136 137 138 139 140 141 142
			{
				map.flipEdge(d) ;
				m4.markOrbit(EDGE_ORBIT, map.phi1(d)) ;
				m4.markOrbit(EDGE_ORBIT, map.phi_1(d)) ;
				m4.markOrbit(EDGE_ORBIT, map.phi1(e)) ;
				m4.markOrbit(EDGE_ORBIT, map.phi_1(e)) ;
			}
		}
	}

	// update vertices normals
	Algo::Geometry::computeNormalVertices<PFP>(map, position, normal) ;

	// tangential relaxation
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144
	AttributeHandler<VEC3> centroid = map.template addAttribute<VEC3>(VERTEX_ORBIT, "centroid") ;
	Algo::Geometry::computeNeighborhoodCentroidVertices<PFP>(map, position, centroid) ;
145 146 147 148 149 150 151

	CellMarker vm(map, VERTEX_CELL) ;
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(!vm.isMarked(d))
		{
			vm.mark(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
152 153 154 155
			VEC3 l = position[d] - centroid[d] ;
			REAL e = l * normal[d] ;
			VEC3 displ = e * normal[d] ;
			position[d] = centroid[d] + displ ;
156 157
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
158 159

	map.template removeAttribute<VEC3>(centroid) ;
160 161 162 163 164 165 166
}

} // namespace Remeshing

} // namespace Algo

} // namespace CGoGN