test_trig.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <ft2build.h>
  2. #include FT_FREETYPE_H
  3. #include FT_TRIGONOMETRY_H
  4. #include <math.h>
  5. #include <stdio.h>
  6. #define PI 3.14159265358979323846
  7. #define SPI (PI/FT_ANGLE_PI)
  8. /* the precision in 16.16 fixed float points of the checks. Expect */
  9. /* between 2 and 5 noise LSB bits during operations, due to */
  10. /* rounding errors.. */
  11. #define THRESHOLD 64
  12. static error = 0;
  13. static void
  14. test_cos( void )
  15. {
  16. FT_Fixed f1, f2;
  17. double d1, d2;
  18. int i;
  19. for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
  20. {
  21. f1 = FT_Cos(i);
  22. d1 = f1/65536.0;
  23. d2 = cos( i*SPI );
  24. f2 = (FT_Fixed)(d2*65536.0);
  25. if ( abs( f2-f1 ) > THRESHOLD )
  26. {
  27. error = 1;
  28. printf( "FT_Cos[%3d] = %.7f cos[%3d] = %.7f\n",
  29. (i >> 16), f1/65536.0, (i >> 16), d2 );
  30. }
  31. }
  32. }
  33. static void
  34. test_sin( void )
  35. {
  36. FT_Fixed f1, f2;
  37. double d1, d2;
  38. int i;
  39. for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
  40. {
  41. f1 = FT_Sin(i);
  42. d1 = f1/65536.0;
  43. d2 = sin( i*SPI );
  44. f2 = (FT_Fixed)(d2*65536.0);
  45. if ( abs( f2-f1 ) > THRESHOLD )
  46. {
  47. error = 1;
  48. printf( "FT_Sin[%3d] = %.7f sin[%3d] = %.7f\n",
  49. (i >> 16), f1/65536.0, (i >> 16), d2 );
  50. }
  51. }
  52. }
  53. static void
  54. test_tan( void )
  55. {
  56. FT_Fixed f1, f2;
  57. double d1, d2;
  58. int i;
  59. for ( i = 0; i < FT_ANGLE_PI2-0x2000000; i += 0x10000 )
  60. {
  61. f1 = FT_Tan(i);
  62. d1 = f1/65536.0;
  63. d2 = tan( i*SPI );
  64. f2 = (FT_Fixed)(d2*65536.0);
  65. if ( abs( f2-f1 ) > THRESHOLD )
  66. {
  67. error = 1;
  68. printf( "FT_Tan[%3d] = %.7f tan[%3d] = %.7f\n",
  69. (i >> 16), f1/65536.0, (i >> 16), d2 );
  70. }
  71. }
  72. }
  73. static void
  74. test_atan2( void )
  75. {
  76. FT_Fixed c2, s2;
  77. double l, a, c1, s1;
  78. int i, j;
  79. for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
  80. {
  81. l = 5.0;
  82. a = i*SPI;
  83. c1 = l * cos(a);
  84. s1 = l * sin(a);
  85. c2 = (FT_Fixed)(c1*65536.0);
  86. s2 = (FT_Fixed)(s1*65536.0);
  87. j = FT_Atan2( c2, s2 );
  88. if ( j < 0 )
  89. j += FT_ANGLE_2PI;
  90. if ( abs( i - j ) > 1 )
  91. {
  92. printf( "FT_Atan2( %.7f, %.7f ) = %.5f, atan = %.5f\n",
  93. c2/65536.0, s2/65536.0, j/65536.0, i/65536.0 );
  94. }
  95. }
  96. }
  97. static void
  98. test_unit( void )
  99. {
  100. FT_Vector v;
  101. double a, c1, s1;
  102. FT_Fixed c2, s2;
  103. int i;
  104. for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
  105. {
  106. FT_Vector_Unit( &v, i );
  107. a = ( i*SPI );
  108. c1 = cos(a);
  109. s1 = sin(a);
  110. c2 = (FT_Fixed)(c1*65536.0);
  111. s2 = (FT_Fixed)(s1*65536.0);
  112. if ( abs( v.x-c2 ) > THRESHOLD ||
  113. abs( v.y-s2 ) > THRESHOLD )
  114. {
  115. error = 1;
  116. printf( "FT_Vector_Unit[%3d] = ( %.7f, %.7f ) vec = ( %.7f, %.7f )\n",
  117. (i >> 16),
  118. v.x/65536.0, v.y/65536.0,
  119. c1, s1 );
  120. }
  121. }
  122. }
  123. static void
  124. test_length( void )
  125. {
  126. FT_Vector v;
  127. FT_Fixed l, l2;
  128. int i;
  129. for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
  130. {
  131. l = (FT_Fixed)(500.0*65536.0);
  132. v.x = (FT_Fixed)( l * cos( i*SPI ) );
  133. v.y = (FT_Fixed)( l * sin( i*SPI ) );
  134. l2 = FT_Vector_Length( &v );
  135. if ( abs( l2-l ) > THRESHOLD )
  136. {
  137. error = 1;
  138. printf( "FT_Length( %.7f, %.7f ) = %.5f, length = %.5f\n",
  139. v.x/65536.0, v.y/65536.0, l2/65536.0, l/65536.0 );
  140. }
  141. }
  142. }
  143. static void
  144. test_rotate( void )
  145. {
  146. FT_Fixed c2, s2, c4, s4;
  147. FT_Vector v;
  148. double l, ra, a, c1, s1, cra, sra, c3, s3;
  149. int i, j, rotate;
  150. for ( rotate = 0; rotate < FT_ANGLE_2PI; rotate += 0x10000 )
  151. {
  152. ra = rotate*SPI;
  153. cra = cos( ra );
  154. sra = sin( ra );
  155. for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
  156. {
  157. l = 500.0;
  158. a = i*SPI;
  159. c1 = l * cos(a);
  160. s1 = l * sin(a);
  161. v.x = c2 = (FT_Fixed)(c1*65536.0);
  162. v.y = s2 = (FT_Fixed)(s1*65536.0);
  163. FT_Vector_Rotate( &v, rotate );
  164. c3 = c1 * cra - s1 * sra;
  165. s3 = c1 * sra + s1 * cra;
  166. c4 = (FT_Fixed)(c3*65536.0);
  167. s4 = (FT_Fixed)(s3*65536.0);
  168. if ( abs( c4 - v.x ) > THRESHOLD ||
  169. abs( s4 - v.y ) > THRESHOLD )
  170. {
  171. error = 1;
  172. printf( "FT_Rotate( (%.7f,%.7f), %.5f ) = ( %.7f, %.7f ), rot = ( %.7f, %.7f )\n",
  173. c1, s1, ra,
  174. c2/65536.0, s2/65536.0,
  175. c4/65536.0, s4/65536.0 );
  176. }
  177. }
  178. }
  179. }
  180. int main( void )
  181. {
  182. test_cos();
  183. test_sin();
  184. test_tan();
  185. test_atan2();
  186. test_unit();
  187. test_length();
  188. test_rotate();
  189. if (!error)
  190. printf( "trigonometry test ok !\n" );
  191. return !error;
  192. }