From edabdd157e9216fc5c1262324e886d51cf9fe880 Mon Sep 17 00:00:00 2001 From: Garux Date: Thu, 25 Mar 2021 18:03:46 +0300 Subject: [PATCH] shorten BernsteinPolynomial --- libs/math/curve.h | 106 ++++++++-------------------------------------- radiant/patch.cpp | 6 +-- 2 files changed, 20 insertions(+), 92 deletions(-) diff --git a/libs/math/curve.h b/libs/math/curve.h index ac21c743..6d76a90e 100644 --- a/libs/math/curve.h +++ b/libs/math/curve.h @@ -31,92 +31,20 @@ template -struct BernsteinPolynomial -{ - static double apply( double t ){ - return 1; // general case not implemented - } -}; +double BernsteinPolynomial( double t ){ + if constexpr( I == 0 && Degree == 0 ) return 1; + else if constexpr( I == 0 && Degree == 1 ) return 1 - t; + 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 ControlPoints; @@ -125,22 +53,22 @@ inline Vector3 CubicBezier_evaluate( const Vector3* firstPoint, double t ){ double denominator = 0; { - double weight = BernsteinPolynomial<0, 3>::apply( t ); + double weight = BernsteinPolynomial<0, 3>( t ); result += vector3_scaled( *firstPoint++, weight ); denominator += weight; } { - double weight = BernsteinPolynomial<1, 3>::apply( t ); + double weight = BernsteinPolynomial<1, 3>( t ); result += vector3_scaled( *firstPoint++, weight ); denominator += weight; } { - double weight = BernsteinPolynomial<2, 3>::apply( t ); + double weight = BernsteinPolynomial<2, 3>( t ); result += vector3_scaled( *firstPoint++, weight ); denominator += weight; } { - double weight = BernsteinPolynomial<3, 3>::apply( t ); + double weight = BernsteinPolynomial<3, 3>( t ); result += vector3_scaled( *firstPoint++, weight ); denominator += weight; } diff --git a/radiant/patch.cpp b/radiant/patch.cpp index e9b347bc..9fb2d337 100644 --- a/radiant/patch.cpp +++ b/radiant/patch.cpp @@ -1789,19 +1789,19 @@ inline PatchControl QuadraticBezier_evaluate( const PatchControl* firstPoint, do 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 ) ); vector2_add( result.m_texcoord, vector2_scaled( firstPoint[0].m_texcoord, 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 ) ); vector2_add( result.m_texcoord, vector2_scaled( firstPoint[1].m_texcoord, 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 ) ); vector2_add( result.m_texcoord, vector2_scaled( firstPoint[2].m_texcoord, weight ) ); denominator += weight;