pliant.hpp 6.32 KB
Newer Older
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
21 22 23 24 25
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Geometry/basic.h"
26
#include "Algo/Geometry/feature.h"
27 28 29 30 31 32 33 34 35 36 37

namespace CGoGN
{

namespace Algo
{

namespace Remeshing
{

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

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

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

62 63
	// split long edges
	DartMarker m2(map) ;
64 65
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
66
		if(!m2.isMarked(d))
67
		{
68
			m2.markOrbit(EDGE, d) ;
69 70 71 72 73 74 75 76
			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)) ;
77 78
				if(dd != d)
					map.splitFace(map.phi1(dd), map.phi_1(dd)) ;
79 80 81 82
			}
		}
	}

83
	// compute feature edges
Pierre Kraemer's avatar
Pierre Kraemer committed
84
	CellMarker featureEdge(map, EDGE) ;
85 86 87
	Algo::Geometry::featureEdgeDetection<PFP>(map, position, featureEdge) ;

	// compute feature vertices
88 89
	CellMarker featureVertex(map, VERTEX) ;
	CellMarker cornerVertex(map, VERTEX) ;
90
	DartMarker m3(map) ;
91 92
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
93
		if(!m3.isMarked(d))
94
		{
95
			m3.markOrbit(VERTEX, d) ;
96 97 98
			unsigned int nbFeatureEdges = 0 ;
			Dart vit = d ;
			do
99
			{
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
				if(featureEdge.isMarked(vit))
					++nbFeatureEdges ;
				vit = map.alpha1(vit) ;
			} while(vit != d) ;
			if(nbFeatureEdges > 0)
			{
				if(nbFeatureEdges == 2)
					featureVertex.mark(d) ;
				else
					cornerVertex.mark(d) ;
			}
		}
	}

	// collapse short
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(m3.isMarked(d))
		{
119
			m3.unmarkOrbit(EDGE, d) ;
120 121 122 123 124 125
			Dart d1 = map.phi1(d) ;
			if(!cornerVertex.isMarked(d) && !cornerVertex.isMarked(d1) &&
				( (featureVertex.isMarked(d) && featureVertex.isMarked(d1)) || (!featureVertex.isMarked(d) && !featureVertex.isMarked(d1)) ))
			{
				REAL length = Algo::Geometry::edgeLength<PFP>(map, d, position) ;
				if(length < edgeLengthInf && map.edgeCanCollapse(d))
126
				{
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
					bool collapse = true ;
					VEC3 p = position[d1] ;
					Dart vit = map.alpha1(d) ;
					do
					{
						VEC3 vec = position[d1] - 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 ;
					}
142 143 144 145
				}
			}
		}
	}
146 147 148 149

	// equalize valences with edge flips
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
150
		if(!m3.isMarked(d))
151
		{
152
			m3.markOrbit(EDGE, d) ;
153
			Dart e = map.phi2(d) ;
154
			if(!featureEdge.isMarked(d) && e != d)
155
			{
156 157 158 159 160 161 162 163 164 165 166 167
				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) ;
				if(flip > 1)
				{
					map.flipEdge(d) ;
168 169 170 171
					m3.markOrbit(EDGE, map.phi1(d)) ;
					m3.markOrbit(EDGE, map.phi_1(d)) ;
					m3.markOrbit(EDGE, map.phi1(e)) ;
					m3.markOrbit(EDGE, map.phi_1(e)) ;
172
				}
173 174 175 176 177 178 179 180
			}
		}
	}

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

	// tangential relaxation
181
	AttributeHandler<VEC3> centroid = map.template addAttribute<VEC3>(VERTEX, "centroid") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
182
	Algo::Geometry::computeNeighborhoodCentroidVertices<PFP>(map, position, centroid) ;
183

184
	CellMarker vm(map, VERTEX) ;
185 186 187 188 189
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(!vm.isMarked(d))
		{
			vm.mark(d) ;
190 191 192 193 194 195 196
			if(!cornerVertex.isMarked(d) && !featureVertex.isMarked(d) && !map.isBoundaryVertex(d))
			{
				VEC3 l = position[d] - centroid[d] ;
				REAL e = l * normal[d] ;
				VEC3 displ = e * normal[d] ;
				position[d] = centroid[d] + displ ;
			}
197 198
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
199 200

	map.template removeAttribute<VEC3>(centroid) ;
201 202 203 204 205 206 207
}

} // namespace Remeshing

} // namespace Algo

} // namespace CGoGN