From 811b6a7177bd9cee562da68cbab92bd607bc1cc2 Mon Sep 17 00:00:00 2001 From: Anuj Verma Date: Tue, 30 Jun 2020 17:20:38 +0530 Subject: [PATCH] * src/sdf/ftsdf.c (cube_root, arc_cos): Added a few math functions. --- [GSoC]ChangeLog | 5 +++++ src/sdf/ftsdf.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/[GSoC]ChangeLog b/[GSoC]ChangeLog index 60258aff6..22c1fe1d8 100644 --- a/[GSoC]ChangeLog +++ b/[GSoC]ChangeLog @@ -1,3 +1,8 @@ +2020-06-30 Anuj Verma + + * src/sdf/ftsdf.c (cube_root, arc_cos): Added a few + essential math functions. + 2020-06-30 Anuj Verma [sdf] Fixed compilation under gnumake. diff --git a/src/sdf/ftsdf.c b/src/sdf/ftsdf.c index 2f676be6e..6ee6bcc97 100644 --- a/src/sdf/ftsdf.c +++ b/src/sdf/ftsdf.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "ftsdf.h" #include "ftsdferrs.h" @@ -624,6 +625,57 @@ return q; } + /* This function uses newton's iteration to find */ + /* cube root of a fixed point integer. */ + static FT_Fixed + cube_root( FT_Fixed val ) + { + /* [IMPORTANT]: This function is not good as it may */ + /* not break, so use a lookup table instead. */ + + FT_Int v, g, c; + + + if ( val == 0 || + val == -FT_INT_16D16( 1 ) || + val == FT_INT_16D16( 1 ) ) + return val; + + v = val < 0 ? -val : val; + g = square_root( v ); + c = 0; + + while ( 1 ) + { + c = FT_MulFix( FT_MulFix( g, g ), g ) - v; + c = FT_DivFix( c, 3 * FT_MulFix( g, g ) ); + + g -= c; + + if ( ( c < 0 ? -c : c ) < 30 ) + break; + } + + return val < 0 ? -g : g; + } + + /* returns cos inverse of a value */ + static FT_Fixed + arc_cos( FT_Fixed val ) + { + FT_Fixed p, b = val; + FT_Fixed one = FT_INT_16D16( 1 ); + + + if ( b > one ) b = one; + if ( b < -one ) b = -one; + + p = one - FT_MulFix( b, b ); + p = square_root( p ); + + return FT_Atan2( b, p ); + } + /*************************************************************************/ /*************************************************************************/ /** **/