average_normals.h 14.7 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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           *
Pierre Kraemer's avatar
Pierre Kraemer committed
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/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25 26
#include "Topology/generic/traversorCell.h"
#include "Topology/generic/traversor2.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
27 28 29 30 31 32 33

namespace CGoGN
{

namespace Algo
{

Pierre Kraemer's avatar
Pierre Kraemer committed
34
namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
{

/**
 * compute new position of vertices from normals (normalAverage & MMSE filters)
 * @param map the map
 */
template <typename PFP>
void computeNewPositionsFromFaceNormals(
	typename PFP::MAP& map,
	const typename PFP::TVEC3& position,
	typename PFP::TVEC3& position2,
	const typename PFP::TREAL& faceArea,
	const typename PFP::TVEC3& faceCentroid,
	const typename PFP::TVEC3& faceNormal,
	const typename PFP::TVEC3& faceNewNormal,
	const FunctorSelect& select)
{
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

55 56
	TraversorV<typename PFP::MAP> t(map) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
57
	{
58
		if(select(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
59 60 61 62 63
		{
			const VEC3& pos_d = position[d] ;

			VEC3 displ(0) ;
			REAL sumAreas = 0 ;
64 65 66

			Traversor2VF<typename PFP::MAP> tvf(map, d) ;
			for(Dart it = tvf.begin(); it != tvf.end(); it = tvf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
67
			{
68 69 70 71 72 73
				sumAreas += faceArea[it] ;
				VEC3 vT = faceCentroid[it] - pos_d ;
				vT = (vT * faceNewNormal[it]) * faceNormal[it] ;
				displ += faceArea[it] * vT ;
			}

Pierre Kraemer's avatar
Pierre Kraemer committed
74 75 76 77 78 79 80
			displ /= sumAreas ;
			position2[d] = pos_d + displ ;
		}
	}
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
81
void filterAverageNormals(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TVEC3& position2, const FunctorSelect& select = SelectorTrue())
Pierre Kraemer's avatar
Pierre Kraemer committed
82 83 84 85
{
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

86 87 88
	AutoAttributeHandler<REAL> faceArea(map, FACE, "faceArea") ;
	AutoAttributeHandler<VEC3> faceNormal(map, FACE, "faceNormal") ;
	AutoAttributeHandler<VEC3> faceCentroid(map, FACE, "faceCentroid") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
89 90 91 92 93

	Algo::Geometry::computeAreaFaces<PFP>(map, position, faceArea, select) ;
	Algo::Geometry::computeNormalFaces<PFP>(map, position, faceNormal, select) ;
	Algo::Geometry::computeCentroidFaces<PFP>(map, position, faceCentroid, select) ;

94
	AutoAttributeHandler<VEC3> faceNewNormal(map, FACE, "faceNewNormal") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96

	// Compute new normals
97 98
	TraversorF<typename PFP::MAP> tf(map) ;
	for(Dart d = tf.begin(); d != tf.end(); d = tf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
99
	{
100
		if(select(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
101 102 103 104 105
		{
			REAL sumArea = 0 ;
			VEC3 meanFilter(0) ;

			// traversal of adjacent faces (by edges and vertices)
106 107
			Traversor2FFaV<typename PFP::MAP> taf(map, d) ;
			for(Dart it = taf.begin(); it != taf.end(); it = taf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
108
			{
109 110 111
				sumArea += faceArea[it] ;
				meanFilter += faceArea[it] * faceNormal[it] ;
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

			// finalize the computation of meanFilter normal
			meanFilter /= sumArea ;
			meanFilter.normalize() ;
			// and store it
			faceNewNormal[d] = meanFilter ;
		}
	}

	// Compute new vertices position
	computeNewPositionsFromFaceNormals<PFP>(
		map, position, position2, faceArea, faceCentroid, faceNormal, faceNewNormal, select
	) ;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
128
void filterMMSE(typename PFP::MAP& map, float sigmaN2, const typename PFP::TVEC3& position, typename PFP::TVEC3& position2, const FunctorSelect& select = SelectorTrue())
Pierre Kraemer's avatar
Pierre Kraemer committed
129 130 131 132
{
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

133 134 135
	AutoAttributeHandler<REAL> faceArea(map, FACE, "faceArea") ;
	AutoAttributeHandler<VEC3> faceNormal(map, FACE, "faceNormal") ;
	AutoAttributeHandler<VEC3> faceCentroid(map, FACE, "faceCentroid") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
136 137 138 139 140

	Algo::Geometry::computeAreaFaces<PFP>(map, position, faceArea, select) ;
	Algo::Geometry::computeNormalFaces<PFP>(map, position, faceNormal, select) ;
	Algo::Geometry::computeCentroidFaces<PFP>(map, position, faceCentroid, select) ;

141
	AutoAttributeHandler<VEC3> faceNewNormal(map, FACE, "faceNewNormal") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
142 143

	// Compute new normals
144 145
	TraversorF<typename PFP::MAP> tf(map) ;
	for(Dart d = tf.begin(); d != tf.end(); d = tf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
146
	{
147
		if( select(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
148 149 150 151 152 153 154 155 156 157
		{
			// traversal of neighbour vertices
			REAL sumArea = 0 ;
			REAL sigmaX2 = 0 ;
			REAL sigmaY2 = 0 ;
			REAL sigmaZ2 = 0 ;

			VEC3 meanFilter(0) ;

			// traversal of adjacent faces (by edges and vertices)
158 159
			Traversor2FFaV<typename PFP::MAP> taf(map, d) ;
			for(Dart it = taf.begin(); it != taf.end(); it = taf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
160 161
			{
				// get info from face embedding and sum
162
				REAL area = faceArea[it] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
163
				sumArea += area ;
164
				VEC3 normal = faceNormal[it] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166 167 168
				meanFilter += area * normal ;
				sigmaX2 += area * normal[0] * normal[0] ;
				sigmaY2 += area * normal[1] * normal[1] ;
				sigmaZ2 += area * normal[2] * normal[2] ;
169
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
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

			meanFilter /= sumArea ;
			sigmaX2 /= sumArea ;
			sigmaX2 -= meanFilter[0] * meanFilter[0] ;
			sigmaY2 /= sumArea ;
			sigmaY2 -= meanFilter[1] * meanFilter[1] ;
			sigmaZ2 /= sumArea ;
			sigmaZ2 -= meanFilter[2] * meanFilter[2] ;

			VEC3& oldNormal = faceNormal[d] ;
			VEC3 newNormal ;

			if(sigmaX2 < sigmaN2)
				newNormal[0] = meanFilter[0] ;
			else
			{
				newNormal[0] = (1 - (sigmaN2 / sigmaX2)) * oldNormal[0] ;
				newNormal[0] += (sigmaN2 / sigmaX2) * meanFilter[0] ;
			}
			if(sigmaY2 < sigmaN2)
				newNormal[1] = meanFilter[1] ;
			else
			{
				newNormal[1] = (1 - (sigmaN2 / sigmaY2)) * oldNormal[1] ;
				newNormal[1] += (sigmaN2 / sigmaY2) * meanFilter[1] ;
			}
			if(sigmaZ2 < sigmaN2)
				newNormal[2] = meanFilter[2] ;
			else
			{
				newNormal[2] = (1 - (sigmaN2 / sigmaZ2)) * oldNormal[2] ;
				newNormal[2] += (sigmaN2 / sigmaZ2) * meanFilter[2] ;
			}

			newNormal.normalize() ;
			faceNewNormal[d] = newNormal ;
		}
	}

	// Compute new vertices position
	computeNewPositionsFromFaceNormals<PFP>(
		map, position, position2, faceArea, faceCentroid, faceNormal, faceNewNormal, select
	) ;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
216
void filterTNBA(typename PFP::MAP& map, float sigmaN2, float SUSANthreshold, const typename PFP::TVEC3& position, typename PFP::TVEC3& position2, const FunctorSelect& select = SelectorTrue())
Pierre Kraemer's avatar
Pierre Kraemer committed
217 218 219 220
{
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

221 222 223
	AutoAttributeHandler<REAL> faceArea(map, FACE, "faceArea") ;
	AutoAttributeHandler<VEC3> faceNormal(map, FACE, "faceNormal") ;
	AutoAttributeHandler<VEC3> faceCentroid(map, FACE, "faceCentroid") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
224 225 226 227 228

	Algo::Geometry::computeAreaFaces<PFP>(map, position, faceArea, select) ;
	Algo::Geometry::computeNormalFaces<PFP>(map, position, faceNormal, select) ;
	Algo::Geometry::computeCentroidFaces<PFP>(map, position, faceCentroid, select) ;

229
	AutoAttributeHandler<VEC3> faceNewNormal(map, FACE, "faceNewNormal") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231 232 233 234 235

	// Compute new normals
	long nbTot = 0 ;
	long nbAdapt = 0 ;
	long nbSusan = 0 ;

236 237
	TraversorF<typename PFP::MAP> tf(map) ;
	for(Dart d = tf.begin(); d != tf.end(); d = tf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
238
	{
239
		if( select(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
240 241 242 243 244 245 246 247 248 249 250 251 252
		{
			const VEC3& normF = faceNormal[d] ;

			// traversal of neighbour vertices
			REAL sumArea = 0 ;
			REAL sigmaX2 = 0 ;
			REAL sigmaY2 = 0 ;
			REAL sigmaZ2 = 0 ;

			VEC3 meanFilter(0) ;
			bool SUSANregion = false ;

			// traversal of adjacent faces (by edges and vertices)
253 254
			Traversor2FFaV<typename PFP::MAP> taf(map, d) ;
			for(Dart it = taf.begin(); it != taf.end(); it = taf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
255 256
			{
				// get info from face embedding and sum
257
				const VEC3& normal = faceNormal[it] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
258 259 260 261

				float angle = Geom::angle(normF, normal) ;
				if(angle <= SUSANthreshold)
				{
262
					REAL area = faceArea[it] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
263 264 265 266 267 268 269
					sumArea += area ;
					meanFilter += area * normal ;
					sigmaX2 += area * normal[0] * normal[0] ;
					sigmaY2 += area * normal[1] * normal[1] ;
					sigmaZ2 += area * normal[2] * normal[2] ;
				}
				else SUSANregion = true ;
270
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
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

			if(SUSANregion)
				++nbSusan ;

			++nbTot ;

			if(sumArea > 0.0f)
			{
				meanFilter /= sumArea ;
				sigmaX2 /= sumArea ;
				sigmaX2 -= meanFilter[0] * meanFilter[0] ;
				sigmaY2 /= sumArea ;
				sigmaY2 -= meanFilter[1] * meanFilter[1] ;
				sigmaZ2 /= sumArea ;
				sigmaZ2 -= meanFilter[2] * meanFilter[2] ;

				VEC3& oldNormal = faceNormal[d] ;
				VEC3 newNormal ;

				bool adapt = false ;
				if(sigmaX2 < sigmaN2)
					newNormal[0] = meanFilter[0] ;
				else
				{
					adapt = true ;
					newNormal[0] = (1 - (sigmaN2 / sigmaX2)) * oldNormal[0] ;
					newNormal[0] += (sigmaN2 / sigmaX2) * meanFilter[0] ;
				}
				if(sigmaY2 < sigmaN2)
					newNormal[1] = meanFilter[1] ;
				else
				{
					adapt = true ;
					newNormal[1] = (1 - (sigmaN2 / sigmaY2)) * oldNormal[1] ;
					newNormal[1] += (sigmaN2 / sigmaY2) * meanFilter[1] ;
				}
				if(sigmaZ2 < sigmaN2)
					newNormal[2] = meanFilter[2] ;
				else
				{
					adapt = true ;
					newNormal[2] = (1 - (sigmaN2 / sigmaZ2)) * oldNormal[2] ;
					newNormal[2] += (sigmaN2 / sigmaZ2) * meanFilter[2] ;
				}
				if(adapt)
					++nbAdapt ;

				newNormal.normalize() ;
				faceNewNormal[d] = newNormal;
			}
			else
			{
				faceNewNormal[d] = normF ;
			}
		}
	}

	// Compute new vertices position
	computeNewPositionsFromFaceNormals<PFP>(
		map, position, position2, faceArea, faceCentroid, faceNormal, faceNewNormal, select
	) ;

333 334
//	CGoGNout <<" susan rate = "<< float(nbSusan)/float(nbTot)<<CGoGNendl;
//	CGoGNout <<" adaptive rate = "<< float(nbAdapt)/float(nbTot)<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
335 336 337
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
338
void filterVNBA(typename PFP::MAP& map, float sigmaN2, float SUSANthreshold, const typename PFP::TVEC3& position, typename PFP::TVEC3& position2, const typename PFP::TVEC3& normal, const FunctorSelect& select = SelectorTrue())
Pierre Kraemer's avatar
Pierre Kraemer committed
339 340 341 342
{
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

343 344 345
	AutoAttributeHandler<REAL> faceArea(map, FACE, "faceArea") ;
	AutoAttributeHandler<VEC3> faceNormal(map, FACE, "faceNormal") ;
	AutoAttributeHandler<VEC3> faceCentroid(map, FACE, "faceCentroid") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
346 347 348 349 350

	Algo::Geometry::computeAreaFaces<PFP>(map, position, faceArea, select) ;
	Algo::Geometry::computeNormalFaces<PFP>(map, position, faceNormal, select) ;
	Algo::Geometry::computeCentroidFaces<PFP>(map, position, faceCentroid, select) ;

351 352 353
	AutoAttributeHandler<REAL> vertexArea(map, VERTEX, "vertexArea") ;
	AutoAttributeHandler<VEC3> faceNewNormal(map, FACE, "faceNewNormal") ;
	AutoAttributeHandler<VEC3> vertexNewNormal(map, VERTEX, "vertexNewNormal") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
354 355 356 357 358

	long nbTot = 0 ;
	long nbAdapt = 0 ;
	long nbSusan = 0 ;

359 360
	TraversorV<typename PFP::MAP> tv(map) ;
	for(Dart d = tv.begin(); d != tv.end(); d = tv.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
361
	{
362
		if( select(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
363 364 365 366 367 368 369 370 371 372 373 374 375
		{
			const VEC3& normV = normal[d] ;

			// traversal of neighbour vertices
			REAL sumArea = 0 ;
			REAL sigmaX2 = 0 ;
			REAL sigmaY2 = 0 ;
			REAL sigmaZ2 = 0 ;

			VEC3 meanFilter(0) ;

			bool SUSANregion = false ;

376 377
			Traversor2VVaE<typename PFP::MAP> tav(map, d) ;
			for(Dart it = tav.begin(); it != tav.end(); it = tav.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
378
			{
379
				const VEC3& neighborNormal = normal[it] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
380 381 382
				float angle = Geom::angle(normV, neighborNormal) ;
				if( angle <= SUSANthreshold )
				{
383 384
					REAL umbArea = Algo::Geometry::vertexOneRingArea<PFP>(map, it, position) ;
			        vertexArea[it] = umbArea ;
Pierre Kraemer's avatar
Pierre Kraemer committed
385 386 387 388 389 390 391 392

			        sumArea += umbArea ;
					sigmaX2 += umbArea * neighborNormal[0] * neighborNormal[0] ;
					sigmaY2 += umbArea * neighborNormal[1] * neighborNormal[1] ;
					sigmaZ2 += umbArea * neighborNormal[2] * neighborNormal[2] ;
					meanFilter += neighborNormal * umbArea ;
				}
				else SUSANregion = true ;
393
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

			if(SUSANregion)
				++nbSusan ;

			++nbTot ;

			if(sumArea > 0.0f)
			{
				meanFilter /= sumArea ;
				sigmaX2 /= sumArea ;
				sigmaX2 -= meanFilter[0] * meanFilter[0] ;
				sigmaY2 /= sumArea ;
				sigmaY2 -= meanFilter[1] * meanFilter[1] ;
				sigmaZ2 /= sumArea ;
				sigmaZ2 -= meanFilter[2] * meanFilter[2] ;

				VEC3 newNormal ;
				bool adapt = false ;
				if(sigmaX2 < sigmaN2)
					newNormal[0] = meanFilter[0] ;
				else
				{
					adapt = true ;
					newNormal[0] = (1 - (sigmaN2 / sigmaX2)) * normV[0] ;
					newNormal[0] += (sigmaN2 / sigmaX2) * meanFilter[0] ;
				}
				if(sigmaY2 < sigmaN2)
					newNormal[1] = meanFilter[1] ;
				else
				{
					adapt = true ;
					newNormal[1] = (1 - (sigmaN2 / sigmaY2)) * normV[1] ;
					newNormal[1] += (sigmaN2 / sigmaY2) * meanFilter[1] ;
				}
				if(sigmaZ2 < sigmaN2)
					newNormal[2] = meanFilter[2] ;
				else
				{
					adapt = true ;
					newNormal[2] = (1 - (sigmaN2 / sigmaZ2)) * normV[2] ;
					newNormal[2] += (sigmaN2 / sigmaZ2) * meanFilter[2] ;
				}

				if(adapt)
					++nbAdapt ;

				newNormal.normalize() ;
				vertexNewNormal[d] = newNormal ;
			}
			else
			{
				vertexNewNormal[d] = normV ;
			}
		}
	}

	// Compute face normals from vertex normals
451 452
	TraversorF<typename PFP::MAP> tf(map) ;
	for(Dart d = tf.begin(); d != tf.end(); d = tf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
453
	{
454
		if (select(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
455 456 457
		{
            VEC3 newNormal(0) ;
            REAL totArea = 0 ;
458 459 460 461 462
            Traversor2FV<typename PFP::MAP> tav(map, d) ;
			for(Dart it = tav.begin(); it != tav.end(); it = tav.next())
			{
            	VEC3 vNorm = vertexNewNormal[it] ;
            	REAL area = vertexArea[it] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
463 464 465
            	totArea += area ;
            	vNorm *= area ;
            	newNormal += vNorm ;
466
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
467 468 469 470 471 472 473 474 475 476
            newNormal.normalize() ;
            faceNewNormal[d] = newNormal ;
		}
	}

	// Compute new vertices position
	computeNewPositionsFromFaceNormals<PFP>(
		map, position, position2, faceArea, faceCentroid, faceNormal, faceNewNormal, select
	) ;

477 478
//	CGoGNout <<" susan rate = "<< float(nbSusan)/float(nbTot)<<CGoGNendl;
//	CGoGNout <<" adaptive rate = "<< float(nbAdapt)/float(nbTot)<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
479 480
}

Pierre Kraemer's avatar
Pierre Kraemer committed
481
} //namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
482 483 484 485

} //namespace Algo

} //namespace CGoGN