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