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