rational_function.cpp 14.7 KB
Newer Older
1 2 3
#include "rational_function.h"

#include <string>
4
#include <sstream>
5 6 7 8 9 10
#include <iostream>
#include <fstream>
#include <limits>
#include <algorithm>
#include <cmath>

11
rational_function_1d::rational_function_1d()
12 13 14
{
}

15
rational_function_1d::rational_function_1d(int np, int nq) 
16
{
17 18
	a.resize(np);
	b.resize(nq);
19
}
20

21 22
rational_function_1d::rational_function_1d(const vec& a, 
                                           const vec& b) :
23 24 25
	a(a), b(b)
{
}
26

27
void rational_function_1d::load(std::istream& in)
28 29
{
}
30

31 32
void rational_function_1d::update(const vec& in_a,
                                  const vec& in_b)
33
{
34 35
	a.resize(in_a.size()) ;
	b.resize(in_b.size()) ;
36 37 38
	a = in_a ;
	b = in_b ;
}
39

40 41 42 43
void rational_function_1d::resize(int np, int nq)
{
	const int old_np = a.size();
	const int old_nq = b.size();
44

45 46 47 48 49 50 51 52 53 54 55 56 57
	// Resize the vector
	a.resize(np);
	b.resize(nq);

	// Set the new coeffs to zero
	for(int i=old_np; i<np; ++i) { a[i] = 0.0; }
	for(int i=old_nq; i<nq; ++i) { b[i] = 0.0; }
}



// Get the p_i and q_j function
vec rational_function_1d::p(const vec& x) const
58 59 60
{
	vec res(_nY) ;

61
	unsigned int const np = a.size() / _nY ;
62 63 64 65

	for(int k=0; k<_nY; ++k)
	{
		double p = 0.0f ;
66

67 68 69 70 71
		for(unsigned int i=0; i<np; ++i)
		{
			p += a[k*_nY + i]*this->p(x, i) ;
		}

72 73 74 75 76 77 78 79
		res[k] = p ;
	}
	return res ;
}
vec rational_function_1d::q(const vec& x) const 
{
	vec res(_nY) ;

80
	unsigned int const nq = b.size() / _nY ;
81 82 83 84 85

	for(int k=0; k<_nY; ++k)
	{
		double q = 0.0f ;

86 87 88 89 90
		for(unsigned int i=0; i<nq; ++i)
		{
			q += b[k*_nY + i]*this->q(x, i) ;
		}

91
		res[k] = q ;
92 93 94 95
	}
	return res ;
}

96 97 98 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 129 130 131 132 133 134 135 136 137 138 139 140 141
// Estimate the number of configuration for an indice
// vector of dimension d with maximum element value
// being k.
int rational_function_1d::estimate_dk(int k, int d)
{
	if(d == 1)
	{
		return 1;
	}
	else if(d ==2)
	{
		return k+1;
	}
	else
	{
		int res = 0;
		for(int i=0; i<=k; ++i)
		{
			res += estimate_dk(k-i, d-1);
		}
		return res;
	}
}

// Populate a vector of degrees of dimension N using a
// maximum degree of M. The index at the current level
// is j
void rational_function_1d::populate(std::vector<int>& vec, int N, int M, int j)
{
	// For each dimension, estimate the current level
	// based on the number of configurations in the
	// other dimensions
	int current_M = M ;
	int nb_conf = 0;
	for(int d=0; d<N-1; ++d)
	{
		int k;
		for(k=0; k<=current_M; ++k)
		{
			int oracle = estimate_dk(current_M-k, N-(d+1));
			if(nb_conf <= j && j < nb_conf+oracle)
			{
				break;
			}
			nb_conf += oracle;
		}
142

143 144 145 146 147 148 149 150 151 152 153 154
		vec[N-1 - d] = k ;	
		current_M -= k ;
	}
	vec[0] = current_M;
}

std::vector<int> rational_function_1d::index2degree(int i) const
{
	std::vector<int> deg ; deg.assign(dimX(), 0) ;

	if(i == 0)
		return deg ;
155

156 157
	if(dimX() == 1)
	{
158
		deg[0] = i;
159
	}
160
	else if(dimX() == 2)
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
	{
		int Nk = 1 ;
		int k  = 1 ;
		while(!(i >= Nk && i < Nk+k+1))
		{
			Nk += k+1 ;
			++k ;
		}

		int r = i-Nk ;
		deg[0] = k-r;
		deg[1] = r;
	}
	else
	{
		int Nk = 1 ;
		int k  = 1 ;
		int dk = estimate_dk(k, dimX()) ;
		while(!(i >= Nk && i < Nk+dk))
		{
			Nk += dk ;
			++k ;
			dk = estimate_dk(k, dimX()) ;
		}

		// Populate the vector from front to back
		int j = i-Nk ;
		populate(deg, dimX(), k, j) ;
	}

	return deg ;

}

double legendre(double x, int i)
{
	if(i == 0)
	{
		return 1;
	}
	else if(i == 1)
	{
		return x;
	}
	else
	{
		return ((2*i-1)*x*legendre(x, i-1) - (i-1)*legendre(x, i-2)) / (double)i ;
	}
}

//#define POLYNOMIALS

// Get the p_i and q_j function
double rational_function_1d::p(const vec& x, int i) const
{
	std::vector<int> deg = index2degree(i);
	double res = 1.0;
	for(int k=0; k<dimX(); ++k)
	{
#ifdef POLYNOMIALS
		res *= pow(x[k], deg[k]) ;
222 223
//		res *= pow(2.0*((x[k] - _min[k]) / (_max[k]-_min[k]) - 0.5), deg[k]) ;
#else
224 225 226 227 228 229 230 231
		res *= legendre(2.0*((x[k] - _min[k]) / (_max[k]-_min[k]) - 0.5), deg[k]);
#endif
	}

	return res ;
}
double rational_function_1d::q(const vec& x, int i) const 
{
232
	return p(x, i);
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
}

// Overload the function operator
vec rational_function_1d::value(const vec& x) const 
{
	vec res(1) ;

	unsigned int const np = a.size() / _nY ;
	unsigned int const nq = b.size() / _nY ;

	double p = 0.0f ;
	double q = 0.0f ;

	for(unsigned int i=0; i<np; ++i)
	{
		p += a[i]*this->p(x, i) ;
	}

	for(unsigned int i=0; i<nq; ++i)
	{
		q += b[i]*this->q(x, i) ;
	}

	res[0] = p/q ;
	return res ;
}


std::ostream& operator<< (std::ostream& out, const rational_function_1d& r) 
{
	std::cout << "p = [" ;
	for(unsigned int i=0; i<r.a.size(); ++i)
	{
		if(i != 0)
		{
			std::cout << ", " ;
		}
		std::cout << r.a[i] ;
	}
	std::cout << "]" << std::endl ;

	std::cout << "q = [" ;
	for(unsigned int i=0; i<r.b.size(); ++i)
	{
		if(i != 0)
		{
			std::cout << ", " ;
		}
		std::cout << r.b[i] ;
	}
	std::cout << "]" << std::endl ;

	return out ;
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
rational_function::rational_function() : np(0), nq(0)
{
}


rational_function::rational_function(int np, int nq) : np(np), nq(nq)
{
}

//! \todo clean memory here
rational_function::~rational_function()
{
}


void rational_function::update(int i, rational_function_1d* r)
{
305
	rs[i] = r;
306 307 308 309 310
}


rational_function_1d* rational_function::get(int i)
{
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	// Check for consistency in the index of color channel
	if(i < _nY)
	{
		if(rs[i] == NULL)
		{
			rs[i] = new rational_function_1d(np, nq);
			rs[i]->setDimX(dimX());
			rs[i]->setDimY(dimY());
			rs[i]->setMin(getMin()) ;
			rs[i]->setMax(getMax()) ;
		}
		return rs[i];
	}
	else
	{
		std::cout << "<<ERROR>> tried to access out of bound 1D RF" << std::endl;
		return NULL;
	}
329 330 331 332
}

rational_function_1d* rational_function::get(int i) const
{
333 334 335 336 337 338 339 340 341 342
	// Check for consistency in the index of color channel
	if(i < _nY)
	{
		return rs[i];
	}
	else
	{
		std::cout << "<<ERROR>> tried to access out of bound 1D RF" << std::endl;
		return NULL;
	}
343 344 345 346 347 348
}

// Overload the function operator

vec rational_function::value(const vec& x) const
{
349
	vec res(_nY) ;
350

351 352 353 354 355
	for(int k=0; k<_nY; ++k)
	{
		res[k] = rs[k]->value(x)[0] ;
	}
	return res ;
356 357 358
}

// IO function to text files
359
void rational_function::load(std::istream& in)
360
{
361

362 363 364 365 366 367
	// Parse line until the next comment
	while(in.peek() != '#')
	{
		char line[256];
		in.getline(line, 256);
	}
368

369 370 371 372 373 374 375
    // Checking for the comment line #FUNC nonlinear_function_lafortune
	std::string token;
	in >> token;
	if(token.compare("#FUNC") != 0) 
	{ 
		std::cerr << "<<ERROR>> parsing the stream. The #FUNC is not the next line defined." << std::endl; 
	}
376

377 378 379 380 381
	in >> token;
   if(token.compare("rational_function") != 0) 
	{
		std::cerr << "<<ERROR>> parsing the stream. function name is not the next token." << std::endl; 
	}
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	int _np, _nq;
	// Shoudl have the #NP [int]
	in >> token >> _np;
	
	// Shoudl have the #NQ [int]
	in >> token >> _nq;
	setSize(_np, _nq);

	// Check for the MIN and MAX vector
	vec min(dimX()), max(dimX());
	in >> token;
   if(token.compare("#MIN") != 0) 
	{
		std::cerr << "<<ERROR>> the min value for the input space is not defined." << std::endl; 
	}
	for(int k=0; k<dimX(); ++k) {in >> min[k];}
	setMin(min);
400

401 402 403 404 405 406 407
	in >> token;
   if(token.compare("#MAX") != 0) 
	{
		std::cerr << "<<ERROR>> the max value for the input space is not defined." << std::endl; 
	}
	for(int k=0; k<dimX(); ++k) {in >> max[k]; }
	setMax(max);
408 409


410 411 412 413 414 415 416 417 418 419 420
	// Check for the polynomial basis type
	in >> token;
   if(token.compare("#BASIS") != 0) 
	{
		std::cerr << "<<ERROR>> the file is not specifying the polynomial basis." << std::endl; 
	}
	in >> token;
   if(token.compare("LEGENDRE") != 0) 
	{
		std::cerr << "<<ERROR>> the basis is different than LEGENDRE." << std::endl; 
	}
421

422 423 424 425 426 427 428 429 430 431 432 433 434 435
	vec a(_np), b(_nq);
	for(int i=0; i<_nY; ++i)
	{
		// Parse the p_i coefficients
		for(int j=0; j<_np; ++j)
		{
			in >> token >> a[j];
		}
		
		// Parse the q_i coefficients
		for(int j=0; j<_nq; ++j)
		{
			in >> token >> b[j];
		}
436

437 438
		std::cout << a << std::endl;
		std::cout << b << std::endl;
439

440 441
		// Update the i_th color channel
		get(i)->update(a, b);
442
	}
443 444 445 446 447
}

//! \todo it should handle parametrization
void rational_function::save_matlab(const std::string& filename, const arguments& args) const
{
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	std::ofstream file(filename.c_str(), std::ios_base::trunc);

	file << "function y = brdf(x)" << std::endl;
	file << std::endl;
	file << "\ts = [";
	for(int i=0; i<dimX(); ++i)
	{
		file << 1.0 / (_max[i]-_min[i]);
		if(i < dimX()-1)
		{
			file << ", ";
		}
	}
	file << "];" << std::endl;
	file << "\tc = [";
	for(int i=0; i<dimX(); ++i)
	{
		file << _min[i];
		if(i < dimX()-1)
		{
			file << ", ";
		}
	}
	file << "];" << std::endl;
	file << std::endl ;

	// Export each color channel independantly
	for(int j=0; j<dimY(); ++j)
	{
		rational_function_1d* rf = get(j);
478 479
		vec a = rf->getP();
		vec b = rf->getQ();
480 481 482

		// Export the numerator of the jth color channel
		file << "\tp(" << j+1 << ",:) = ";
483
		for(int i=0; i<np; ++i)
484
		{
Laurent Belcour's avatar
Laurent Belcour committed
485 486
			if(i > 0 && a[i] >= 0.0)
			{
487
				file << " + ";
Laurent Belcour's avatar
Laurent Belcour committed
488 489 490
			}
			else if(a[i] < 0.0)
			{
491
				file << " " ;
Laurent Belcour's avatar
Laurent Belcour committed
492 493
			}
			file << a[i];
494 495 496 497 498 499 500 501 502 503 504 505

			std::vector<int> degree = rf->index2degree(i);
			for(unsigned int k=0; k<degree.size(); ++k)
			{
				file << ".*legendrepoly(" << degree[k] << ", 2.0*((x(" << k+1 << ",:)"
					<< "-c(" << k+1 << "))*s(" << k+1 << ") - 0.5))" ;
			}
		}
		file << ";" << std::endl;

		// Export the denominator of the jth color channel
		file << "\tq(" << j+1 << ",:) = ";
506
		for(int i=0; i<nq; ++i)
507
		{
Laurent Belcour's avatar
Laurent Belcour committed
508 509
			if(i > 0 && b[i] >= 0.0)
			{
510
				file << " + ";
Laurent Belcour's avatar
Laurent Belcour committed
511 512 513
			}
			else if(b[i] < 0.0)
			{
514
				file << " " ;
Laurent Belcour's avatar
Laurent Belcour committed
515 516
			}
			file << b[i] ;
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

			std::vector<int> degree = rf->index2degree(i);
			for(unsigned int k=0; k<degree.size(); ++k)
			{
				file << ".*legendrepoly(" << degree[k] << ", 2.0*((x(" << k+1 << ",:)"
					<< "-c(" << k+1 << "))*s(" << k+1 << ") - 0.5))" ;
			}
		}
		file << ";" << std::endl;

		file << "\ty(" << j+1 << ",:) = p./q;" << std::endl;
		if(j < dimY()-1)
		{
			file << std::endl;
		}
	}


	file << "endfunction" << std::endl;

	file.close() ;
538 539 540 541 542
}

//! \todo it should handle parametrization
void rational_function::save_cpp(const std::string& filename, const arguments& args) const
{
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
	std::ofstream file(filename.c_str(), std::ios_base::trunc);

	file << "double s[" << dimX() << "] = {";
	for(int i=0; i<dimX(); ++i)
	{
		file << 1.0 / (_max[i]-_min[i]);
		if(i < dimX()-1)
		{
			file << ", ";
		}
	}
	file << "};" << std::endl;
	file << "double c[" << dimX() << "] = {";
	for(int i=0; i<dimX(); ++i)
	{
		file << _min[i];
		if(i < dimX()-1)
		{
			file << ", ";
		}
	}
	file << "};" << std::endl;
	file << std::endl ;

	file << "// The Legendre polynomial of order i evaluated in x" << std::endl;
	file << "double l(double x, int i)" << std::endl;
	file << "{" << std::endl;
	file << "    if(i == 0)" << std::endl;
	file << "    {" << std::endl;
	file << "        return 1;" << std::endl;
	file << "    }" << std::endl;
	file << "    else if(i == 1)" << std::endl;
	file << "    {" << std::endl;
	file << "        return x;" << std::endl;
	file << "    }" << std::endl;
	file << "    else" << std::endl;
	file << "    {" << std::endl;
	file << "        return ((2*i-1)*x*l(x, i-1) - (i-1)*l(x, i-2)) / (double)i ;" << std::endl;
	file << "    }" << std::endl;
	file << "}" << std::endl;
	file << std::endl;

	file << "void brdf(double* x, double* y)" << std::endl;
	file << "{" << std::endl;
	file << "\tdouble p, q;" << std::endl;

	// Export each color channel independantly
	for(int j=0; j<dimY(); ++j)
	{
		rational_function_1d* rf = get(j);
593 594
		vec a = rf->getP();
		vec b = rf->getQ();
595 596 597

		// Export the numerator of the jth color channel
		file << "\tp = ";
598
		for(int i=0; i<np; ++i)
599
		{
Laurent Belcour's avatar
Laurent Belcour committed
600
			if(i > 0 && a[i] >= 0.0)
601 602 603
			{
				file << " + ";
			}
Laurent Belcour's avatar
Laurent Belcour committed
604 605 606 607 608
			else if(a[i] < 0.0)
			{
				file << " " ;
			}
			file << a[i];
609 610 611 612 613 614 615 616 617 618 619

			std::vector<int> degree = rf->index2degree(i);
			for(unsigned int k=0; k<degree.size(); ++k)
			{
				file << "*l(2.0*((x[" << k << "]-c[" << k << "])*s[" << k << "] - 0.5), " << degree[k] << ")" ;
			}
		}
		file << ";" << std::endl;

		// Export the denominator of the jth color channel
		file << "\tq = ";
620
		for(int i=0; i<nq; ++i)
621
		{
Laurent Belcour's avatar
Laurent Belcour committed
622 623
			if(i > 0 && b[i] >= 0.0)
			{
624
				file << " + ";
Laurent Belcour's avatar
Laurent Belcour committed
625 626 627
			}
			else if(b[i] < 0.0)
			{
628
				file << " " ;
Laurent Belcour's avatar
Laurent Belcour committed
629 630
			}
			file << b[i] ;
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

			std::vector<int> degree = rf->index2degree(i);
			for(unsigned int k=0; k<degree.size(); ++k)
			{
				file << "*l(2.0*((x[" << k << "]-c[" << k << "])*s[" << k << "] - 0.5), " << degree[k] << ")" ;
			}
		}
		file << ";" << std::endl;

		file << "\ty[" << j << "] = p/q;" << std::endl;
		if(j < dimY()-1)
		{
			file << std::endl;
		}
	}


	file << "}" << std::endl;

	file.close() ;
651 652 653 654 655
}


void rational_function::save_gnuplot(const std::string& filename, const data* d, const arguments& args) const
{
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
	std::ofstream file(filename.c_str(), std::ios_base::trunc);
	for(int i=0; i<d->size(); ++i)
	{
		vec v = d->get(i) ;
		//		vec y1 ; y1.assign(d->dimY(), 0.0) ;
		//		for(int k=0; k<d->dimY(); ++k) { y1[k] = v[d->dimX() + k] ; }

		vec y2 = value(v) ;
		for(int u=0; u<d->dimX(); ++u)
			file << v[u] << "\t" ;

		for(int u=0; u<d->dimY(); ++u)
			file << y2[u] << "\t" ;

		file << std::endl ;
	}
	file.close();
673 674
}

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
void rational_function::save_call(std::ostream& out, const arguments& args) const
{
	out << "#FUNC rational_function" << std::endl;
	out << "#NP " << np << std::endl ;
	out << "#NQ " << nq << std::endl ;
	out << "#MIN "; for(int k=0; k<_nX; ++k) { out << _min[k] << " "; } out << std::endl;
	out << "#MAX "; for(int k=0; k<_nX; ++k) { out << _max[k] << " "; } out << std::endl; 
	out << "#BASIS LEGENDRE" << std::endl ;

	for(int k=0; k<_nY; ++k)
	{
		rational_function_1d* rf = get(k);
		vec a = rf->getP();
		vec b = rf->getQ();

690
		for(int i=0; i<np; ++i)
691 692 693 694 695 696 697 698 699
		{
			std::vector<int> index = rf->index2degree(i) ;
			for(unsigned int j=0; j<index.size(); ++j)
			{
				out << index[j] << "\t" ;
			}
			out << a[i] << std::endl ;
		}

700
		for(int i=0; i<nq; ++i)
701 702 703 704 705 706 707 708 709 710
		{
			std::vector<int> index = rf->index2degree(i) ;
			for(unsigned int j=0; j<index.size(); ++j)
			{
				out << index[j] << "\t" ;
			}
			out << b[i] << std::endl ;
		}
	}
}
711 712 713

void rational_function::save(const std::string& filename) const
{
714 715 716 717 718 719 720 721 722 723
	std::ofstream file(filename.c_str(), std::ios_base::trunc);
	file << "#DIM " << _nX << " " << _nY << std::endl ;
	file << "#NP " << np << std::endl ;
	file << "#NQ " << nq << std::endl ;
	file << "#BASIS LEGENDRE" << std::endl ;
	file << "#INPUT_PARAM " << params::get_name(this->parametrization()) << std::endl;

	for(int k=0; k<_nY; ++k)
	{
		rational_function_1d* rf = get(k);
724 725
		vec a = rf->getP();
		vec b = rf->getQ();
726

727
		for(int i=0; i<np; ++i)
728 729 730 731 732 733 734 735 736
		{
			std::vector<int> index = rf->index2degree(i) ;
			for(unsigned int j=0; j<index.size(); ++j)
			{
				file << index[j] << "\t" ;
			}
			file << a[i] << std::endl ;
		}

737
		for(int i=0; i<nq; ++i)
738 739 740 741 742 743 744 745 746
		{
			std::vector<int> index = rf->index2degree(i) ;
			for(unsigned int j=0; j<index.size(); ++j)
			{
				file << index[j] << "\t" ;
			}
			file << b[i] << std::endl ;
		}
	}
747 748 749 750 751 752

}


std::ostream& operator<< (std::ostream& out, rational_function& r)
{
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	for(int i=0; i<r.dimY(); ++i)
	{
		rational_function_1d* rf = r.get(i);
		out << "dimension " << i << ": ";
		if(rf != NULL)
		{
			out << *rf << std::endl;
		}
		else
		{
			out << "[NULL]" << std::endl;
		}
	}

	return out ;
768 769 770
}


771