bivariatePolynomials.hpp 2.98 KB
Newer Older
Sauvage's avatar
Sauvage committed
1
/*
Sauvage's avatar
Sauvage committed
2
 * bivariatePolynomials.hpp
Sauvage's avatar
Sauvage committed
3
 *
Sauvage's avatar
Sauvage committed
4
 *  Created on: July, 2015
Sauvage's avatar
Sauvage committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *      Author: sauvage
 */

namespace CGoGN
{

namespace Utils
{



/*************************************************************************
construction, destruction and initialization
**************************************************************************/

template <typename Tscalar,typename Tcoef>
Sauvage's avatar
Sauvage committed
21
BivariatePolynomials<Tscalar,Tcoef>::BivariatePolynomials() : degree(4), nb_coefs(15)
Sauvage's avatar
Sauvage committed
22
{
Sauvage's avatar
Sauvage committed
23
	assert ( (nb_coefs > 0) || !" negative # of coefs");
Sauvage's avatar
Sauvage committed
24 25 26 27 28 29
	coefs = new Tcoef[nb_coefs];
	for (int i = 0; i < nb_coefs; i++)
		coefs[i] = Tcoef (0);
}

template <typename Tscalar,typename Tcoef>
Sauvage's avatar
Sauvage committed
30
BivariatePolynomials<Tscalar,Tcoef>::BivariatePolynomials(BivariatePolynomials const & r) : degree(r.degree), nb_coefs (r.nb_coefs)
Sauvage's avatar
Sauvage committed
31
{
Sauvage's avatar
Sauvage committed
32
	assert ( (nb_coefs > 0) || !" negative # of coefs");
Sauvage's avatar
Sauvage committed
33 34 35 36 37 38
	coefs = new Tcoef[nb_coefs];
	for (int i = 0; i < nb_coefs; i++)
		coefs[i] = r.coefs[i];
}

template <typename Tscalar,typename Tcoef>
Sauvage's avatar
Sauvage committed
39
BivariatePolynomials<Tscalar,Tcoef>::~BivariatePolynomials()
Sauvage's avatar
Sauvage committed
40 41 42 43 44 45 46 47 48
{
	delete[] coefs;
}

/*************************************************************************
evaluation
**************************************************************************/

template <typename Tscalar,typename Tcoef>
Sauvage's avatar
Sauvage committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
Tcoef BivariatePolynomials<Tscalar,Tcoef>::evaluate_at (Tscalar u, Tscalar v) const
{
	Tcoef r (0);
	Tscalar u2 = u*u;
	Tscalar u3 = u2*u;
	Tscalar u4 = u3*u;
	Tscalar v2 = v*v;
	Tscalar v3 = v2*v;
	Tscalar v4 = v3*v;

	r += coefs[0];
	r += coefs[1] * u;
	r += coefs[2] * v;
	r += coefs[3] * u2;
	r += coefs[4] * u * v;
	r += coefs[5] * v2;
	r += coefs[6] * u3;
	r += coefs[7] * u2 * v;
	r += coefs[8] * u * v2;
	r += coefs[9] * v3;
	r += coefs[10] * u4;
	r += coefs[11] * u3 * v;
	r += coefs[12] * u2 * v2;
	r += coefs[13] * u * v3;
	r += coefs[14] * v4;
Sauvage's avatar
Sauvage committed
74 75 76 77
	return r;
}

template <typename Tscalar,typename Tcoef>
78
Tcoef BivariatePolynomials<Tscalar,Tcoef>::evaluate_at (const Geom::Vector<3,Tscalar>& tu, const Geom::Vector<3,Tscalar>& tv, const Geom::Vector<3,Tscalar>& n, const Geom::Vector<3,Tscalar>& eval_dir) const
Sauvage's avatar
Sauvage committed
79
{
Sauvage's avatar
Sauvage committed
80 81 82
	Tscalar u = tu*eval_dir;
	Tscalar v = tv*eval_dir;
	if (n*eval_dir < 0)
Sauvage's avatar
Sauvage committed
83
	{
Sauvage's avatar
Sauvage committed
84 85 86
		Tscalar d = sqrt(u*u + v*v);
		u/=d;
		v/=d;
Sauvage's avatar
Sauvage committed
87
	}
Sauvage's avatar
Sauvage committed
88
	return evaluate_at(u,v);
Sauvage's avatar
Sauvage committed
89 90 91 92 93 94 95
}

/*************************************************************************
I/O
**************************************************************************/

template <typename Tscalar,typename Tcoef>
Sauvage's avatar
Sauvage committed
96
std::ostream & operator << (std::ostream & os, const BivariatePolynomials<Tscalar,Tcoef> & p)
Sauvage's avatar
Sauvage committed
97
{
98
	for (int d = 0; d <= p.degree; d++)
Sauvage's avatar
Sauvage committed
99
	{
Sauvage's avatar
Sauvage committed
100 101
		for (int du = d; du >=0 ; du--)
			os << p.get_coef(du,d-du) << "\t";
Sauvage's avatar
Sauvage committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		os << std::endl;
	}
	return os;
}

/*************************************************************************
operators
**************************************************************************/


/*************************************************************************
fitting
**************************************************************************/


} // namespace Utils

} // namespace CGoGN