shorten BernsteinPolynomial

This commit is contained in:
Garux 2021-03-25 18:03:46 +03:00
parent f8ab9a3b59
commit edabdd157e
2 changed files with 20 additions and 92 deletions

View File

@ -31,92 +31,20 @@
template<int I, int Degree> template<int I, int Degree>
struct BernsteinPolynomial double BernsteinPolynomial( double t ){
{ if constexpr( I == 0 && Degree == 0 ) return 1;
static double apply( double t ){ else if constexpr( I == 0 && Degree == 1 ) return 1 - t;
return 1; // general case not implemented else if constexpr( I == 1 && Degree == 1 ) return t;
} else if constexpr( I == 0 && Degree == 2 ) return ( 1 - t ) * ( 1 - t );
}; else if constexpr( I == 1 && Degree == 2 ) return 2 * ( 1 - t ) * t;
else if constexpr( I == 2 && Degree == 2 ) return t * t;
else if constexpr( I == 0 && Degree == 3 ) return ( 1 - t ) * ( 1 - t ) * ( 1 - t );
else if constexpr( I == 1 && Degree == 3 ) return 3 * ( 1 - t ) * ( 1 - t ) * t;
else if constexpr( I == 2 && Degree == 3 ) return 3 * ( 1 - t ) * t * t;
else if constexpr( I == 3 && Degree == 3 ) return t * t * t;
else 0 = 1; // general case not implemented
}
template<>
struct BernsteinPolynomial<0, 0>
{
static double apply( double t ){
return 1;
}
};
template<>
struct BernsteinPolynomial<0, 1>
{
static double apply( double t ){
return 1 - t;
}
};
template<>
struct BernsteinPolynomial<1, 1>
{
static double apply( double t ){
return t;
}
};
template<>
struct BernsteinPolynomial<0, 2>
{
static double apply( double t ){
return ( 1 - t ) * ( 1 - t );
}
};
template<>
struct BernsteinPolynomial<1, 2>
{
static double apply( double t ){
return 2 * ( 1 - t ) * t;
}
};
template<>
struct BernsteinPolynomial<2, 2>
{
static double apply( double t ){
return t * t;
}
};
template<>
struct BernsteinPolynomial<0, 3>
{
static double apply( double t ){
return ( 1 - t ) * ( 1 - t ) * ( 1 - t );
}
};
template<>
struct BernsteinPolynomial<1, 3>
{
static double apply( double t ){
return 3 * ( 1 - t ) * ( 1 - t ) * t;
}
};
template<>
struct BernsteinPolynomial<2, 3>
{
static double apply( double t ){
return 3 * ( 1 - t ) * t * t;
}
};
template<>
struct BernsteinPolynomial<3, 3>
{
static double apply( double t ){
return t * t * t;
}
};
typedef Array<Vector3> ControlPoints; typedef Array<Vector3> ControlPoints;
@ -125,22 +53,22 @@ inline Vector3 CubicBezier_evaluate( const Vector3* firstPoint, double t ){
double denominator = 0; double denominator = 0;
{ {
double weight = BernsteinPolynomial<0, 3>::apply( t ); double weight = BernsteinPolynomial<0, 3>( t );
result += vector3_scaled( *firstPoint++, weight ); result += vector3_scaled( *firstPoint++, weight );
denominator += weight; denominator += weight;
} }
{ {
double weight = BernsteinPolynomial<1, 3>::apply( t ); double weight = BernsteinPolynomial<1, 3>( t );
result += vector3_scaled( *firstPoint++, weight ); result += vector3_scaled( *firstPoint++, weight );
denominator += weight; denominator += weight;
} }
{ {
double weight = BernsteinPolynomial<2, 3>::apply( t ); double weight = BernsteinPolynomial<2, 3>( t );
result += vector3_scaled( *firstPoint++, weight ); result += vector3_scaled( *firstPoint++, weight );
denominator += weight; denominator += weight;
} }
{ {
double weight = BernsteinPolynomial<3, 3>::apply( t ); double weight = BernsteinPolynomial<3, 3>( t );
result += vector3_scaled( *firstPoint++, weight ); result += vector3_scaled( *firstPoint++, weight );
denominator += weight; denominator += weight;
} }

View File

@ -1789,19 +1789,19 @@ inline PatchControl QuadraticBezier_evaluate( const PatchControl* firstPoint, do
double denominator = 0; double denominator = 0;
{ {
double weight = BernsteinPolynomial<0, 2>::apply( t ); double weight = BernsteinPolynomial<0, 2>( t );
vector3_add( result.m_vertex, vector3_scaled( firstPoint[0].m_vertex, weight ) ); vector3_add( result.m_vertex, vector3_scaled( firstPoint[0].m_vertex, weight ) );
vector2_add( result.m_texcoord, vector2_scaled( firstPoint[0].m_texcoord, weight ) ); vector2_add( result.m_texcoord, vector2_scaled( firstPoint[0].m_texcoord, weight ) );
denominator += weight; denominator += weight;
} }
{ {
double weight = BernsteinPolynomial<1, 2>::apply( t ); double weight = BernsteinPolynomial<1, 2>( t );
vector3_add( result.m_vertex, vector3_scaled( firstPoint[1].m_vertex, weight ) ); vector3_add( result.m_vertex, vector3_scaled( firstPoint[1].m_vertex, weight ) );
vector2_add( result.m_texcoord, vector2_scaled( firstPoint[1].m_texcoord, weight ) ); vector2_add( result.m_texcoord, vector2_scaled( firstPoint[1].m_texcoord, weight ) );
denominator += weight; denominator += weight;
} }
{ {
double weight = BernsteinPolynomial<2, 2>::apply( t ); double weight = BernsteinPolynomial<2, 2>( t );
vector3_add( result.m_vertex, vector3_scaled( firstPoint[2].m_vertex, weight ) ); vector3_add( result.m_vertex, vector3_scaled( firstPoint[2].m_vertex, weight ) );
vector2_add( result.m_texcoord, vector2_scaled( firstPoint[2].m_texcoord, weight ) ); vector2_add( result.m_texcoord, vector2_scaled( firstPoint[2].m_texcoord, weight ) );
denominator += weight; denominator += weight;