test_trig.c

Go to the documentation of this file.
00001 #include <ft2build.h>
00002 #include FT_FREETYPE_H
00003 #include FT_TRIGONOMETRY_H
00004 
00005 #include <math.h>
00006 #include <stdio.h>
00007 
00008 #define  PI   3.14159265358979323846
00009 #define  SPI  (PI/FT_ANGLE_PI)
00010 
00011 /* the precision in 16.16 fixed float points of the checks. Expect */
00012 /* between 2 and 5 noise LSB bits during operations, due to        */
00013 /* rounding errors..                                               */
00014 #define  THRESHOLD  64
00015 
00016   static  error = 0;
00017 
00018   static void
00019   test_cos( void )
00020   {
00021     FT_Fixed  f1, f2;
00022     double    d1, d2;
00023     int       i;
00024 
00025     for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
00026     {
00027       f1 = FT_Cos(i);
00028       d1 = f1/65536.0;
00029       d2 = cos( i*SPI );
00030       f2 = (FT_Fixed)(d2*65536.0);
00031 
00032       if ( abs( f2-f1 ) > THRESHOLD )
00033       {
00034         error = 1;
00035         printf( "FT_Cos[%3d] = %.7f  cos[%3d] = %.7f\n",
00036                 (i >> 16), f1/65536.0, (i >> 16), d2 );
00037       }
00038     }
00039   }
00040 
00041 
00042 
00043   static void
00044   test_sin( void )
00045   {
00046     FT_Fixed  f1, f2;
00047     double    d1, d2;
00048     int       i;
00049 
00050     for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
00051     {
00052       f1 = FT_Sin(i);
00053       d1 = f1/65536.0;
00054       d2 = sin( i*SPI );
00055       f2 = (FT_Fixed)(d2*65536.0);
00056 
00057       if ( abs( f2-f1 ) > THRESHOLD )
00058       {
00059         error = 1;
00060         printf( "FT_Sin[%3d] = %.7f  sin[%3d] = %.7f\n",
00061                 (i >> 16), f1/65536.0, (i >> 16), d2 );
00062       }
00063     }
00064   }
00065 
00066 
00067   static void
00068   test_tan( void )
00069   {
00070     FT_Fixed  f1, f2;
00071     double    d1, d2;
00072     int       i;
00073 
00074     for ( i = 0; i < FT_ANGLE_PI2-0x2000000; i += 0x10000 )
00075     {
00076       f1 = FT_Tan(i);
00077       d1 = f1/65536.0;
00078       d2 = tan( i*SPI );
00079       f2 = (FT_Fixed)(d2*65536.0);
00080 
00081       if ( abs( f2-f1 ) > THRESHOLD )
00082       {
00083         error = 1;
00084         printf( "FT_Tan[%3d] = %.7f  tan[%3d] = %.7f\n",
00085                 (i >> 16), f1/65536.0, (i >> 16), d2 );
00086       }
00087     }
00088   }
00089 
00090 
00091   static void
00092   test_atan2( void )
00093   {
00094     FT_Fixed  c2, s2;
00095     double    l, a, c1, s1;
00096     int       i, j;
00097 
00098     for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
00099     {
00100       l  = 5.0;
00101       a  = i*SPI;
00102 
00103       c1 = l * cos(a);
00104       s1 = l * sin(a);
00105 
00106       c2 = (FT_Fixed)(c1*65536.0);
00107       s2 = (FT_Fixed)(s1*65536.0);
00108 
00109       j  = FT_Atan2( c2, s2 );
00110       if ( j < 0 )
00111         j += FT_ANGLE_2PI;
00112 
00113       if ( abs( i - j ) > 1 )
00114       {
00115         printf( "FT_Atan2( %.7f, %.7f ) = %.5f, atan = %.5f\n",
00116                 c2/65536.0, s2/65536.0, j/65536.0, i/65536.0 );
00117       }
00118     }
00119   }
00120 
00121   static void
00122   test_unit( void )
00123   {
00124     FT_Vector  v;
00125     double     a, c1, s1;
00126     FT_Fixed   c2, s2;
00127     int        i;
00128 
00129     for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
00130     {
00131       FT_Vector_Unit( &v, i );
00132       a  = ( i*SPI );
00133       c1 = cos(a);
00134       s1 = sin(a);
00135       c2 = (FT_Fixed)(c1*65536.0);
00136       s2 = (FT_Fixed)(s1*65536.0);
00137 
00138       if ( abs( v.x-c2 ) > THRESHOLD ||
00139            abs( v.y-s2 ) > THRESHOLD )
00140       {
00141         error = 1;
00142         printf( "FT_Vector_Unit[%3d] = ( %.7f, %.7f )  vec = ( %.7f, %.7f )\n",
00143                 (i >> 16),
00144                 v.x/65536.0, v.y/65536.0,
00145                 c1, s1 );
00146       }
00147     }
00148   }
00149 
00150 
00151   static void
00152   test_length( void )
00153   {
00154     FT_Vector  v;
00155     FT_Fixed   l, l2;
00156     int        i;
00157 
00158     for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
00159     {
00160       l   = (FT_Fixed)(500.0*65536.0);
00161       v.x = (FT_Fixed)( l * cos( i*SPI ) );
00162       v.y = (FT_Fixed)( l * sin( i*SPI ) );
00163       l2  = FT_Vector_Length( &v );
00164 
00165       if ( abs( l2-l ) > THRESHOLD )
00166       {
00167         error = 1;
00168         printf( "FT_Length( %.7f, %.7f ) = %.5f, length = %.5f\n",
00169                 v.x/65536.0, v.y/65536.0, l2/65536.0, l/65536.0 );
00170       }
00171     }
00172   }
00173 
00174 
00175   static void
00176   test_rotate( void )
00177   {
00178     FT_Fixed  c2, s2, c4, s4;
00179     FT_Vector v;
00180     double    l, ra, a, c1, s1, cra, sra, c3, s3;
00181     int       i, j, rotate;
00182 
00183     for ( rotate = 0; rotate < FT_ANGLE_2PI; rotate += 0x10000 )
00184     {
00185       ra  = rotate*SPI;
00186       cra = cos( ra );
00187       sra = sin( ra );
00188 
00189       for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
00190       {
00191         l  = 500.0;
00192         a  = i*SPI;
00193 
00194         c1 = l * cos(a);
00195         s1 = l * sin(a);
00196 
00197         v.x = c2 = (FT_Fixed)(c1*65536.0);
00198         v.y = s2 = (FT_Fixed)(s1*65536.0);
00199 
00200         FT_Vector_Rotate( &v, rotate );
00201 
00202         c3 = c1 * cra - s1 * sra;
00203         s3 = c1 * sra + s1 * cra;
00204 
00205         c4 = (FT_Fixed)(c3*65536.0);
00206         s4 = (FT_Fixed)(s3*65536.0);
00207 
00208         if ( abs( c4 - v.x ) > THRESHOLD ||
00209              abs( s4 - v.y ) > THRESHOLD )
00210         {
00211           error = 1;
00212           printf( "FT_Rotate( (%.7f,%.7f), %.5f ) = ( %.7f, %.7f ), rot = ( %.7f, %.7f )\n",
00213                   c1, s1, ra,
00214                   c2/65536.0, s2/65536.0,
00215                   c4/65536.0, s4/65536.0 );
00216         }
00217       }
00218     }
00219   }
00220 
00221 
00222   int main( void )
00223   {
00224     test_cos();
00225     test_sin();
00226     test_tan();
00227     test_atan2();
00228     test_unit();
00229     test_length();
00230     test_rotate();
00231 
00232     if (!error)
00233       printf( "trigonometry test ok !\n" );
00234 
00235     return !error;
00236   }

Generated on Tue Jul 5 14:13:53 2011 for ROOT_528-00b_version by  doxygen 1.5.1