quadricRGBfunctions.hpp 9.64 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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 42 43 44 45 46 47
/*
 * quadricLF.hpp
 *
 *  Created on: Nov 4, 2010
 *      Author: kenneth
 */

#ifndef QUADRICLF_HPP_
#define QUADRICLF_HPP_

template <typename REAL>
std::string QuadricRGBfunctions<REAL>::CGoGNnameOfType() {
	return std::string("QuadricColFuncs");
}

template <typename REAL>
QuadricRGBfunctions<REAL>::QuadricRGBfunctions() {
	for (unsigned col = RED; col < BLUE+1 ; ++col) {
		for (unsigned i = 0; i < 6; ++i) {
			b[col][i] = (REAL)0;
			for (unsigned j = 0; j < 6; ++j)
				A[col](i,j) = REAL(0);
		}

		c[col] = REAL(0);
	}
}

template <typename REAL>
QuadricRGBfunctions<REAL>::QuadricRGBfunctions(int i) {
	QuadricRGBfunctions();
}

template <typename REAL>
QuadricRGBfunctions<REAL>::QuadricRGBfunctions(const QuadricRGBfunctions& q) {
	for (unsigned col = RED; col < BLUE+1 ; ++col) {
		for (unsigned i = 0; i < 6; ++i) {
			b[col][i] = q.b[col][i];
			for (unsigned j = 0; j < 6; ++j)
				A[col](i,j) = q.A[col](i,j);
		}

		c[col] = q.c[col];
	}
}

template <typename REAL>
48
QuadricRGBfunctions<REAL>::QuadricRGBfunctions(const RGBFUNCTIONS& cf, REAL gamma, REAL alpha) {
Pierre Kraemer's avatar
Pierre Kraemer committed
49 50 51
	MATRIX66 R1,R2_A,R2_b,R2_c;
	// Matrice de rotation 1
	buildRotateMatrix(R1,gamma);
52 53
//	std::cout <<"R1 : " <<  R1 << std::endl ;
//	std::cout <<"alpha : " << alpha << std::endl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
54 55 56 57 58
	// Matrice de rotation 2 + intégrale
	buildIntegralMatrix_A(R2_A,alpha);
	buildIntegralMatrix_b(R2_b,alpha);
	buildIntegralMatrix_b(R2_c,alpha);

59 60 61 62
//	std::cout << "A : " << R2_A << std::endl ;
//	std::cout << "b : " << R2_b << std::endl ;
//	std::cout << "c : " << R2_c << std::endl ;

Pierre Kraemer's avatar
Pierre Kraemer committed
63 64 65 66 67 68

	// Quadrique (A,b,c) tel que L*A*Lt - 2*b*Lt + c = ERROR
	for (unsigned col = RED; col < BLUE+1; ++col) {
		// Rotation 1

		Geom::Vector<6,REAL> function;
69 70 71

		if (!cf.getSubVectorH(col,0,function))
			assert(!"QuadricRGBfunctions::constructor") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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

		VEC6 coefs = R1 * function ; // Multiply coefs

		// Calcul de A (intégrale)
		A[col] = R2_A ;
		// Calcul de b (intégrale+rotation sur 1 vec)
		b[col] = R2_b * function ;

		// Calcul de c (intégrale+rotation sur 2 vec)
		c[col] = function * (R2_c * function) ;
	}
}

template <typename REAL>
QuadricRGBfunctions<REAL>::~QuadricRGBfunctions() {

}

template <typename REAL>
REAL QuadricRGBfunctions<REAL>::operator() (const RGBFUNCTIONS& cf) const {
	REAL res = REAL(0);

	for (unsigned col = RED; col < BLUE+1; ++col) {
		Geom::Vector<6,REAL> function ;

97 98
		if (!cf.getSubVectorH(col,0,function))
			assert (!"QuadricRGBfunctions::getSubVectorH") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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

		VEC6 Al = A[col] * function;

		REAL res_local = REAL(0) ;
		res_local += (function * Al); // l*A*lt
		res_local -= 2 * (function * b[col]);
		res_local += c[col];

		res += res_local;
	}

	return res;
}

template <typename REAL>
bool QuadricRGBfunctions<REAL>::findOptimizedRGBfunctions(RGBFUNCTIONS& cf) const {

	for (unsigned  col = RED; col < BLUE+1 ; ++col) {
		MATRIX66 Ainv,M;
		M = A[col];

		REAL det = M.invert(Ainv);

		if(det > -1e-8 && det < 1e-8)
			return false ;

		VEC6 coefs;

		coefs = Ainv * b[col];

129 130
		if (!cf.setSubVectorH(col,0,coefs))
			assert (!"QuadricRGBfunctions::findOptimizedRGBfunctions(cf) setSubVector failed") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
131 132 133 134 135 136 137 138 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 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 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 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 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 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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
	}

	return true;
}

template <typename REAL>
void QuadricRGBfunctions<REAL>::buildIntegralMatrix_A(MATRIX66 &M, REAL alpha) const {
	REAL alphaAbs = alpha > 0 ? alpha : -alpha;

	const REAL pi = 3.141592 ;

	const REAL cosAbs = cos(alphaAbs); // = - cos(alpha) si alpha < 0
	const REAL cosinus = cos(alpha);
	const REAL sinus = sin(alpha);

	// Line 1
	M(0,0) = 2.0*(pi-alphaAbs)/5.0f;

	M(0,1) = 2.0 * (pi - alphaAbs - cosAbs*sinus) / 15.0 ;

	M(0,2) = 0;

	M(0,3) = 0;

	M(0,4) = - sinus*pi / 8.0;

	M(0,5) = 2.0*(pi-alphaAbs)/3.0;

	// Line 2
	M(1,0) = M(0,1);

	M(1,1) = -4.0 * sinus*cosAbs*cosAbs*cosAbs / 15.0 + 2.0 * (pi - cosAbs*sinus - alphaAbs) / 5.0 ;

	M(1,2) = 0;

	M(1,3) = 0;

	M(1,4) = -(sinus*cosinus*cosinus*pi + 2*sinus*pi) / 8.0;

	M(1,5) = 2.0*(pi-cosAbs*sinus-alphaAbs)/3.0;

	// LINE 3
	M(2,0) = REAL(0);

	M(2,1) = REAL(0);

	M(2,2) = 2.0*(pi - alphaAbs - cosAbs*sinus)/15.0;

	M(2,3) = -pi*sinus/8.0f;

	M(2,4) = REAL(0) ;

	M(2,5) = REAL(0) ;

	// Line 4
	M(3,0) = REAL(0) ;

	M(3,1) = REAL(0) ;

	M(3,2) = M(2,3);

	M(3,3) = 2.0*(pi-alphaAbs)/3.0 ;

	M(3,4) = REAL(0) ;

	M(3,5) = REAL(0) ;

	// Line 5
	M(4,0) = - M(0,4) ;

	M(4,1) = - (2*pi*sinus + sinus*cosinus*cosinus*pi ) / 8.0 ;

	M(4,2) = REAL(0) ;

	M(4,3) = REAL(0) ;

	M(4,4) = 2.0 * (pi - cosAbs*sinus - alphaAbs ) / 3.0 ;

	M(4,5) = - pi*sinus / 2.0 ;

	// Line 6
	M(5,0) = M(0,5) ;

	M(5,1) = M(1,5) ;

	M(5,2) = REAL(0) ;

	M(5,3) = REAL(0) ;

	M(5,4) = M(4,5) ;

	M(5,5) = 2.0*(pi-alphaAbs);
}


template <typename REAL>
void QuadricRGBfunctions<REAL>::buildIntegralMatrix_b(MATRIX66 &M, REAL alpha) const {
	REAL alphaAbs = alpha > 0 ? alpha : -alpha;

	const REAL pi = 3.141592 ;

	const REAL cosAbs = cos(alphaAbs); // = - cos(alpha) si alpha < 0
	const REAL cosinus = cos(alpha);
	const REAL sinus = sin(alpha);

	// Line 1
	M(0,0) = 2.0*(pi-alphaAbs)/5.0f;

	M(0,1) = 2.0 * (pi - alphaAbs - cosAbs*sinus) / 15.0 ;

	M(0,2) = 0;

	M(0,3) = 0;

	M(0,4) = sinus*pi / 8.0;

	M(0,5) = 2.0*(pi-alphaAbs)/3.0;

	// Line 2
	M(1,0) = M(0,1);

	M(1,1) = 2 * (pi - alphaAbs + 2*(cosAbs*cosAbs*pi-alphaAbs*cosAbs*cosAbs))/15.0 - 2.0*cosAbs*sinus/3.0 ;

	M(1,2) = 0;

	M(1,3) = 0;

	M(1,4) = (sinus*pi - 2*sinus*cosinus*pi) / 8.0;

	M(1,5) = 2.0*(pi-cosAbs*sinus-alphaAbs)/3.0;

	// LINE 3
	M(2,0) = REAL(0);

	M(2,1) = REAL(0);

	M(2,2) = 2.0*(cosinus*pi - cosinus*alphaAbs + (alpha < 0 ? sinus : -sinus))/15.0;

	M(2,3) = -pi*sinus/8.0f;

	M(2,4) = REAL(0) ;

	M(2,5) = REAL(0) ;

	// Line 4
	M(3,0) = REAL(0) ;

	M(3,1) = REAL(0) ;

	M(3,2) = - M(2,3);

	M(3,3) = 2.0*(pi-alphaAbs)/3.0 ;

	M(3,4) = REAL(0) ;

	M(3,5) = REAL(0) ;

	// Line 5
	M(4,0) = - M(0,4) ;

	M(4,1) = (2*pi*cosinus*sinus - sinus*pi ) / 8.0 ;

	M(4,2) = REAL(0) ;

	M(4,3) = REAL(0) ;

	M(4,4) = 2.0 * (cosinus*pi - cosinus*alphaAbs + (alpha < 0 ? sinus : -sinus) ) / 3.0 ;

	M(4,5) = pi*sinus / 2.0 ;

	// Line 6
	M(5,0) = M(0,5) ;

	M(5,1) = M(1,5) ;

	M(5,2) = REAL(0) ;

	M(5,3) = REAL(0) ;

	M(5,4) = M(4,5) ;

	M(5,5) = 2.0*(pi-alphaAbs);
}

template <typename REAL>
void QuadricRGBfunctions<REAL>::buildIntegralMatrix_c(MATRIX66 &M, REAL alpha) const {
	REAL alphaAbs = alpha > 0 ? alpha : -alpha;

	const REAL pi = 3.141592 ;

	const REAL cosAbs = cos(alphaAbs); // = - cos(alpha) si alpha < 0
	const REAL cos = cos(alpha);
	const REAL sin = sin(alpha);

	// Line 1
	M(0,0) = 2.0*(pi-alphaAbs)/5.0f;

	M(0,1) = 2.0 * (pi - alphaAbs - cosAbs*sin) / 15.0 ;

	M(0,2) = 0;

	M(0,3) = 0;

	M(0,4) = sin*pi / 8.0;

	M(0,5) = 2.0*(pi-alphaAbs)/3.0;

	// Line 2
	M(1.0) = M(0,1);

	M(1,1) = -4*cosAbs*cosAbs*cosAbs*sin/15.0f + 2*(pi-cosAbs*sin-alphaAbs)/5.0f;

	M(1,2) = 0;

	M(1,3) = 0;

	M(1,4) = (sin*cos*cos*pi + 2*sin*pi)/8.0f ;

	M(1,5) = 2.0*(pi-cosAbs*sin-alphaAbs)/3.0;

	// LINE 3
	M(2,0) = REAL(0);

	M(2,1) = REAL(0);

	M(2,2) = 2.0*(pi - alphaAbs -cosAbs*sin)/15.0;

	M(2,3) = pi*sin/8.0f;

	M(2,4) = REAL(0) ;

	M(2,5) = REAL(0) ;

	// Line 4
	M(3,0) = REAL(0) ;

	M(3,1) = REAL(0) ;

	M(3,2) = M(2,3);

	M(3,3) = 2.0*(pi-alphaAbs)/3.0 ;

	M(3,4) = REAL(0) ;

	M(3,5) = REAL(0) ;

	// Line 5
	M(4,0) = M(0,4) ;

	M(4,1) = (2*sin*pi + sin*cos*cos*pi ) / 8.0 ;

	M(4,2) = REAL(0) ;

	M(4,3) = REAL(0) ;

	M(4,4) = 2.0 * (pi - alphaAbs - cosAbs*sin ) / 3.0 ;

	M(4,5) = pi*sin / 2.0 ;

	// Line 6
	M(5,0) = M(0,5) ;

	M(5,1) = M(1,5) ;

	M(5,2) = REAL(0) ;

	M(5,3) = REAL(0) ;

	M(5,4) = M(4,5) ;

	M(5,5) = 2.0*(pi-alphaAbs);
}


template <typename REAL>
void QuadricRGBfunctions<REAL>::buildRotateMatrix(MATRIX66 &N, REAL gamma) const {
	REAL cosinus = cos(gamma), cos2 = cosinus*cosinus;
	REAL sinus = sin(gamma), sin2 = sinus*sinus;
	REAL sincos = sinus*cosinus;

Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
411 412
	// Inverted matrix for left-hand side application

Pierre Kraemer's avatar
Pierre Kraemer committed
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 451 452
	N(0,0) = cos2;
	N(0,1) = sin2;
	N(0,2) = -sincos;
	N(0,3) = REAL(0);
	N(0,4) = REAL(0);
	N(0,5) = REAL(0);

	N(1,0) = sin2;
	N(1,1) = cos2;
	N(1,2) = sincos;
	N(1,3) = REAL(0);
	N(1,4) = REAL(0);
	N(1,5) = REAL(0);

	N(2,0) = 2*sincos;
	N(2,1) = -2*sincos;
	N(2,2) = cos2-sin2;
	N(2,3) = REAL(0);
	N(2,4) = REAL(0);
	N(2,5) = REAL(0);

	N(3,0) = REAL(0);
	N(3,1) = REAL(0);
	N(3,2) = REAL(0);
	N(3,3) = cosinus;
	N(3,4) = -sinus;
	N(3,5) = REAL(0);

	N(4,0) = REAL(0);
	N(4,1) = REAL(0);
	N(4,2) = REAL(0);
	N(4,3) = sinus;
	N(4,4) = cosinus;
	N(4,5) = REAL(0);

	N(5,0) = REAL(0);
	N(5,1) = REAL(0);
	N(5,2) = REAL(0);
	N(5,3) = REAL(0);
	N(5,4) = REAL(0);
Kenneth Vanhoey's avatar
LF  
Kenneth Vanhoey committed
453 454
	N(5,5) = REAL(1.0);

Pierre Kraemer's avatar
Pierre Kraemer committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
}

template <typename REAL>
void QuadricRGBfunctions<REAL>::operator += (const QuadricRGBfunctions& q) {
	for (unsigned col = RED; col < BLUE+1 ; ++col) {
		for (unsigned i = 0; i < 6; ++i) {
			b[col][i] += q.b[col][i];
			for (unsigned j = 0; j < 6; ++j)
				A[col](i,j) += q.A[col](i,j);
		}

		c[col] += q.c[col];
	}
}

template <typename REAL>
void QuadricRGBfunctions<REAL>::operator -= (const QuadricRGBfunctions& q) {
	for (unsigned col = RED; col < BLUE+1 ; ++col) {
		for (unsigned i = 0; i < 6; ++i) {
			b[col][i] -= q.b[col][i];
			for (unsigned j = 0; j < 6; ++j)
				A[col](i,j) -= q.A[col](i,j);
		}

		c[col] -= q.c[col];
	}
}

template <typename REAL>
void QuadricRGBfunctions<REAL>::operator *= (REAL v) {
	for (unsigned col = RED; col < BLUE+1 ; ++col) {
		for (unsigned i = 0; i < 6; ++i) {
			b[col][i] *= v;
			for (unsigned j = 0; j < 6; ++j)
				A[col](i,j) *= v;
		}

		c[col] *= v;
	}
}

template <typename REAL>
void QuadricRGBfunctions<REAL>::operator /= (REAL v) {
	for (unsigned col = RED; col < BLUE+1 ; ++col) {
		for (unsigned i = 0; i < 6; ++i) {
			b[col][i] /= v;
			for (unsigned j = 0; j < 6; ++j)
				A[col](i,j) /= v;
		}

		c[col] /= v;
	}
}

template <typename REAL>
void QuadricRGBfunctions<REAL>::zero () {
	for (unsigned int i = 0 ; i < COLCHANNELS ; ++i) {
		A[i].zero();
		b[i].zero();
		c[i] = REAL(0) ;
	}
}

#endif /* QUADRICLF_HPP_ */