tools.h 11.1 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-2012, 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.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Geometry/distances.h"

#include <time.h>
#include <stdlib.h>
#include <limits>

namespace CGoGN
{

namespace Algo
{

37 38 39
namespace Surface
{

40
namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
41 42 43
{

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
44
float computeHaussdorf(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& originalPosition, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position2)
Pierre Kraemer's avatar
Pierre Kraemer committed
45 46 47 48 49 50
{
	typedef typename PFP::VEC3 VEC3 ;

	float dist_o = 0.0f ;
	float dist_f = 0.0f ;

51
	TraversorV<typename PFP::MAP> t(map) ;
52
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
53
	{
54 55 56 57 58 59 60 61 62
		const VEC3& posO = originalPosition[d] ;
		const VEC3& posF = position2[d] ;

		float dv_o = std::numeric_limits<float>::max();
		float dv_f = std::numeric_limits<float>::max();

		// just test the faces around the vertex => warning not real haussdorff distance!
		Traversor2VF<typename PFP::MAP> tf(map, d) ;
		for(Dart it = tf.begin(); it != tf.end(); it = tf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
63
		{
64 65 66 67 68 69 70 71 72 73 74 75 76
			Dart e = map.phi1(it) ;
			const VEC3& Bo = originalPosition[e] ;
			const VEC3& Bf = position2[e] ;
			e = map.phi1(e) ;
			const VEC3& Co = originalPosition[e] ;
			const VEC3& Cf = position2[e] ;

			float d = Geom::squaredDistancePoint2Triangle(posO, posF, Bf, Cf) ;
			if(d < dv_o)
				dv_o = d ;
			d = Geom::squaredDistancePoint2Triangle(posF, posO, Bo, Co) ;
			if(d < dv_f)
				dv_f = d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
77
		}
78 79 80 81 82

		if(dv_o > dist_o)
			dist_o = dv_o ;
		if(dv_f > dist_f)
			dist_f = dv_f ;
Pierre Kraemer's avatar
Pierre Kraemer committed
83 84 85 86 87 88 89 90
	}

	if (dist_f > dist_o)
		return sqrtf(dist_f) ;
	return sqrtf(dist_o) ;
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
91
void computeNoise(typename PFP::MAP& map, long amount, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position2, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& normal)
Pierre Kraemer's avatar
Pierre Kraemer committed
92 93 94 95 96 97 98
{
	typedef typename PFP::VEC3 VEC3 ;

	// init the seed for random
	srand(time(NULL)) ;

	// apply noise on each vertex
99
	TraversorV<typename PFP::MAP> t(map) ;
100
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
101
	{
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		const VEC3& pos = position[d] ;
		VEC3 norm = normal[d] ;

		float r1 = float(rand() % amount) / 100.0f ;
		float r2 = 0 ;
		if (amount >= 5)
			r2 = float(rand() % (amount/5)) / 100.0f ;

		long sign = rand() % 2 ;
		if (sign == 1) norm *= -1.0f ;
		float avEL = 0.0f ;
		VEC3 td(0) ;

		long nbE = 0 ;
		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
118
		{
119 120 121 122 123 124 125
			const VEC3& p = position[it] ;
			VEC3 vec = p - pos ;
			float el = vec.norm() ;
			vec *= r2 ;
			td += vec ;
			avEL += el ;
			nbE++ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
126
		}
127 128 129 130 131

		avEL /= float(nbE) ;
		norm *= avEL * r1 ;
		norm += td ;
		position2[d] = pos + norm ;
Pierre Kraemer's avatar
Pierre Kraemer committed
132 133 134
	}
}

untereiner's avatar
untereiner committed
135 136 137
//Uniform-distributed additive noise
//TODO do not touch to boundary vertices
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
138
void computeUnfirmAdditiveNoise(typename PFP::MAP& map, float noiseIntensity, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position2)
untereiner's avatar
untereiner committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
{
	typedef typename PFP::VEC3 VEC3 ;

	// Compute mesh center
	unsigned int count = 0;
	VEC3 centroid(0.0);

	TraversorV<typename PFP::MAP> tv(map) ;
	for(Dart dit = tv.begin(); dit != tv.end(); dit = tv.next())
	{
		centroid += position[dit];
		++count;
	}
	centroid /= count;

	double distanceCentroid = 0.0;

	// Compute the average distance from vertices to mesh center
	for(Dart dit = tv.begin(); dit != tv.end(); dit = tv.next())
	{
		VEC3 dist = position[dit];
		dist -= centroid;
		distanceCentroid += float(dist.norm());
	}
	distanceCentroid /= count;

	// add random uniform-distributed (between [-noiseLevel, +noiseLevel])
	srand((unsigned)time(NULL));
    float noisex, noisey, noisez;
    float noiseLevel = distanceCentroid * noiseIntensity;


    for(Dart dit = tv.begin(); dit != tv.end(); dit = tv.next())
	{
    	noisex = noiseLevel * (1.0*rand()/RAND_MAX-0.5)*2;
    	noisey = noiseLevel * (1.0*rand()/RAND_MAX-0.5)*2;
    	noisez = noiseLevel * (1.0*rand()/RAND_MAX-0.5)*2;

    	position2[dit] = position[dit] + VEC3(noisex,noisey,noisez);
	}
}


//Gaussian-distributed additive noise
//TODO do not touch to boundary vertices
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
185
void computeGaussianAdditiveNoise(typename PFP::MAP& map, float noiseIntensity, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position2)
untereiner's avatar
untereiner committed
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
{
	typedef typename PFP::VEC3 VEC3 ;

	// Compute mesh center
	unsigned int count = 0;
	VEC3 centroid(0.0);

	TraversorV<typename PFP::MAP> tv(map) ;
	for(Dart dit = tv.begin(); dit != tv.end(); dit = tv.next())
	{
		centroid += position[dit];
		++count;
	}
	centroid /= count;

	double distanceCentroid = 0.0;

	// Compute the average distance from vertices to mesh center
	for(Dart dit = tv.begin(); dit != tv.end(); dit = tv.next())
	{
		VEC3 dist = position[dit];
		dist -= centroid;
		distanceCentroid += float(dist.norm());
	}
	distanceCentroid /= count;

	// add random gaussian-distributed
	srand((unsigned)time(NULL));
    float noisex, noisey, noisez;
    float gaussNumbers[3];
    float noiseLevel = distanceCentroid * noiseIntensity;

    for(Dart dit = tv.begin(); dit != tv.end(); dit = tv.next())
	{

    	// pseudo-random Gaussian-distributed numbers generation from uniformly-distributed pseudo-random numbers
    	float x, y, r2;
    	for (int i=0; i<3; i++)
    	{
    		do
    		{
    			x = -1.0 + 2.0 * 1.0*rand()/RAND_MAX;
    			y = -1.0 + 2.0 * 1.0*rand()/RAND_MAX;
    			r2 = x * x + y * y;
    		} while ((r2>1.0)||(r2==0.0));
    		gaussNumbers[i] = y * sqrt(-2.0 * log(r2) / r2);
    	}

    	noisex = noiseLevel * gaussNumbers[0];
    	noisey = noiseLevel * gaussNumbers[1];
    	noisez = noiseLevel * gaussNumbers[2];

    	position2[dit] = position[dit] + VEC3(noisex,noisey,noisez);
	}

}

243
} //namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
244

untereiner's avatar
untereiner committed
245
} //namespace Surface
246

untereiner's avatar
untereiner committed
247 248 249 250 251 252 253 254

namespace Volume
{

namespace Filtering
{

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
255
void computeNoise(typename PFP::MAP& map, long amount, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position2)
untereiner's avatar
untereiner committed
256 257 258 259 260 261 262 263 264
{
	typedef typename PFP::VEC3 VEC3 ;

	// add random gaussian-distributed
	srand((unsigned)time(NULL));
    float noisex, noisey, noisez;
    float gaussNumbers[3];
    float noiseLevel = 0.1f;

265 266 267
//    TraversorV<typename PFP::MAP> tv(map) ;
//    for(Dart dit = tv.begin(); dit != tv.end(); dit = tv.next())
//	{
untereiner's avatar
untereiner committed
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
//    	// pseudo-random Gaussian-distributed numbers generation from uniformly-distributed pseudo-random numbers
//    	float x, y, r2;
//    	for (int i=0; i<3; i++)
//    	{
//    		do
//    		{
//    			x = -1.0 + 2.0 * 1.0*rand()/RAND_MAX;
//    			y = -1.0 + 2.0 * 1.0*rand()/RAND_MAX;
//    			r2 = x * x + y * y;
//    		} while ((r2>1.0)||(r2==0.0));
//    		gaussNumbers[i] = y * sqrt(-2.0 * log(r2) / r2);
//    	}

//    	noisex = noiseLevel * gaussNumbers[0];
//    	noisey = noiseLevel * gaussNumbers[1];
//    	noisez = noiseLevel * gaussNumbers[2];

//    	position2[dit] = position[dit] + VEC3(noisex,noisey,noisez);
//	}
untereiner's avatar
untereiner committed
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
    // init the seed for random
    srand(time(NULL)) ;

    // apply noise on each vertex
    TraversorV<typename PFP::MAP> t(map) ;
    for(Dart d = t.begin(); d != t.end(); d = t.next())
    {
        const VEC3& pos = position[d] ;
        VEC3 norm = position[d] ;

        float r1 = float(rand() % amount) / 100.0f ;
        float r2 = 0 ;
        if (amount >= 5)
            r2 = float(rand() % (amount/5)) / 100.0f ;

        long sign = rand() % 2 ;
        if (sign == 1) norm *= -1.0f ;
        float avEL = 0.0f ;
        VEC3 td(0) ;

        long nbE = 0 ;
        Traversor3VVaE<typename PFP::MAP> tav(map, d) ;
        for(Dart it = tav.begin(); it != tav.end(); it = tav.next())
        {
            const VEC3& p = position[it] ;
            VEC3 vec = p - pos ;
            float el = vec.norm() ;
            vec *= r2 ;
            td += vec ;
            avEL += el ;
            nbE++ ;
        }

        avEL /= float(nbE) ;
        norm *= avEL * r1 ;
        norm += td ;
        position2[d] = pos + norm ;
    }
}
untereiner's avatar
untereiner committed
329 330


331
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
332
void computeNoiseGaussian(typename PFP::MAP& map, long amount, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position2)
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
{
    typedef typename PFP::VEC3 VEC3 ;

    // add random gaussian-distributed
    srand((unsigned)time(NULL));
    float noisex, noisey, noisez;
    float gaussNumbers[3];
    float noiseLevel = 0.08f;

    TraversorV<typename PFP::MAP> tv(map) ;
    for(Dart dit = tv.begin(); dit != tv.end(); dit = tv.next())
    {

        // pseudo-random Gaussian-distributed numbers generation from uniformly-distributed pseudo-random numbers
        float x, y, r2;
        for (int i=0; i<3; i++)
        {
            do
            {
                x = -1.0 + 2.0 * 1.0*rand()/RAND_MAX;
                y = -1.0 + 2.0 * 1.0*rand()/RAND_MAX;
                r2 = x * x + y * y;
            } while ((r2>1.0)||(r2==0.0));
            gaussNumbers[i] = y * sqrt(-2.0 * log(r2) / r2);
        }

        noisex = noiseLevel * gaussNumbers[0];
        noisey = noiseLevel * gaussNumbers[1];
        noisez = noiseLevel * gaussNumbers[2];

        position2[dit] = position[dit] + VEC3(noisex,noisey,noisez);
    }

untereiner's avatar
untereiner committed
366 367 368 369 370 371
}

} //namespace Filtering

} //namespace Volume

Pierre Kraemer's avatar
Pierre Kraemer committed
372 373 374
} //namespace Algo

} //namespace CGoGN