Visualization Library 2.0.0-b5

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
test_trig.c
Go to the documentation of this file.
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
18  test_cos( void )
19  {
20  FT_Fixed f1, f2;
21  double d1, d2;
22  int i;
23 
24  for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
25  {
26  f1 = FT_Cos(i);
27  d1 = f1/65536.0;
28  d2 = cos( i*SPI );
29  f2 = (FT_Fixed)(d2*65536.0);
30 
31  if ( abs( f2-f1 ) > THRESHOLD )
32  {
33  error = 1;
34  printf( "FT_Cos[%3d] = %.7f cos[%3d] = %.7f\n",
35  (i >> 16), f1/65536.0, (i >> 16), d2 );
36  }
37  }
38  }
39 
40 
41 
42  static void
43  test_sin( void )
44  {
45  FT_Fixed f1, f2;
46  double d1, d2;
47  int i;
48 
49  for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
50  {
51  f1 = FT_Sin(i);
52  d1 = f1/65536.0;
53  d2 = sin( i*SPI );
54  f2 = (FT_Fixed)(d2*65536.0);
55 
56  if ( abs( f2-f1 ) > THRESHOLD )
57  {
58  error = 1;
59  printf( "FT_Sin[%3d] = %.7f sin[%3d] = %.7f\n",
60  (i >> 16), f1/65536.0, (i >> 16), d2 );
61  }
62  }
63  }
64 
65 
66  static void
67  test_tan( void )
68  {
69  FT_Fixed f1, f2;
70  double d1, d2;
71  int i;
72 
73  for ( i = 0; i < FT_ANGLE_PI2-0x2000000; i += 0x10000 )
74  {
75  f1 = FT_Tan(i);
76  d1 = f1/65536.0;
77  d2 = tan( i*SPI );
78  f2 = (FT_Fixed)(d2*65536.0);
79 
80  if ( abs( f2-f1 ) > THRESHOLD )
81  {
82  error = 1;
83  printf( "FT_Tan[%3d] = %.7f tan[%3d] = %.7f\n",
84  (i >> 16), f1/65536.0, (i >> 16), d2 );
85  }
86  }
87  }
88 
89 
90  static void
91  test_atan2( void )
92  {
93  FT_Fixed c2, s2;
94  double l, a, c1, s1;
95  int i, j;
96 
97  for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
98  {
99  l = 5.0;
100  a = i*SPI;
101 
102  c1 = l * cos(a);
103  s1 = l * sin(a);
104 
105  c2 = (FT_Fixed)(c1*65536.0);
106  s2 = (FT_Fixed)(s1*65536.0);
107 
108  j = FT_Atan2( c2, s2 );
109  if ( j < 0 )
110  j += FT_ANGLE_2PI;
111 
112  if ( abs( i - j ) > 1 )
113  {
114  printf( "FT_Atan2( %.7f, %.7f ) = %.5f, atan = %.5f\n",
115  c2/65536.0, s2/65536.0, j/65536.0, i/65536.0 );
116  }
117  }
118  }
119 
120  static void
121  test_unit( void )
122  {
123  FT_Vector v;
124  double a, c1, s1;
125  FT_Fixed c2, s2;
126  int i;
127 
128  for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
129  {
130  FT_Vector_Unit( &v, i );
131  a = ( i*SPI );
132  c1 = cos(a);
133  s1 = sin(a);
134  c2 = (FT_Fixed)(c1*65536.0);
135  s2 = (FT_Fixed)(s1*65536.0);
136 
137  if ( abs( v.x-c2 ) > THRESHOLD ||
138  abs( v.y-s2 ) > THRESHOLD )
139  {
140  error = 1;
141  printf( "FT_Vector_Unit[%3d] = ( %.7f, %.7f ) vec = ( %.7f, %.7f )\n",
142  (i >> 16),
143  v.x/65536.0, v.y/65536.0,
144  c1, s1 );
145  }
146  }
147  }
148 
149 
150  static void
151  test_length( void )
152  {
153  FT_Vector v;
154  FT_Fixed l, l2;
155  int i;
156 
157  for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
158  {
159  l = (FT_Fixed)(500.0*65536.0);
160  v.x = (FT_Fixed)( l * cos( i*SPI ) );
161  v.y = (FT_Fixed)( l * sin( i*SPI ) );
162  l2 = FT_Vector_Length( &v );
163 
164  if ( abs( l2-l ) > THRESHOLD )
165  {
166  error = 1;
167  printf( "FT_Length( %.7f, %.7f ) = %.5f, length = %.5f\n",
168  v.x/65536.0, v.y/65536.0, l2/65536.0, l/65536.0 );
169  }
170  }
171  }
172 
173 
174  static void
175  test_rotate( void )
176  {
177  FT_Fixed c2, s2, c4, s4;
178  FT_Vector v;
179  double l, ra, a, c1, s1, cra, sra, c3, s3;
180  int i, j, rotate;
181 
182  for ( rotate = 0; rotate < FT_ANGLE_2PI; rotate += 0x10000 )
183  {
184  ra = rotate*SPI;
185  cra = cos( ra );
186  sra = sin( ra );
187 
188  for ( i = 0; i < FT_ANGLE_2PI; i += 0x10000 )
189  {
190  l = 500.0;
191  a = i*SPI;
192 
193  c1 = l * cos(a);
194  s1 = l * sin(a);
195 
196  v.x = c2 = (FT_Fixed)(c1*65536.0);
197  v.y = s2 = (FT_Fixed)(s1*65536.0);
198 
199  FT_Vector_Rotate( &v, rotate );
200 
201  c3 = c1 * cra - s1 * sra;
202  s3 = c1 * sra + s1 * cra;
203 
204  c4 = (FT_Fixed)(c3*65536.0);
205  s4 = (FT_Fixed)(s3*65536.0);
206 
207  if ( abs( c4 - v.x ) > THRESHOLD ||
208  abs( s4 - v.y ) > THRESHOLD )
209  {
210  error = 1;
211  printf( "FT_Rotate( (%.7f,%.7f), %.5f ) = ( %.7f, %.7f ), rot = ( %.7f, %.7f )\n",
212  c1, s1, ra,
213  c2/65536.0, s2/65536.0,
214  c4/65536.0, s4/65536.0 );
215  }
216  }
217  }
218  }
219 
220 
221  int main( void )
222  {
223  test_cos();
224  test_sin();
225  test_tan();
226  test_atan2();
227  test_unit();
228  test_length();
229  test_rotate();
230 
231  if (!error)
232  printf( "trigonometry test ok !\n" );
233 
234  return !error;
235  }
#define FT_ANGLE_PI2
Definition: fttrigon.h:88
GLboolean GLboolean GLboolean GLboolean a
png_voidp s1
Definition: png.h:1956
T sin(T a)
Definition: glsl_math.hpp:199
#define SPI
Definition: test_trig.c:9
FT_Tan(FT_Angle angle)
Definition: fttrigon.c:303
local void rotate(unsigned char *list, unsigned len, unsigned rot)
Definition: gzappend.c:123
png_uint_32 i
Definition: png.h:2640
FT_Atan2(FT_Fixed x, FT_Fixed y)
Definition: fttrigon.c:319
FT_Error error
Definition: cffdrivr.c:411
const GLdouble * v
FT_Pos x
Definition: ftimage.h:77
FT_Pos y
Definition: ftimage.h:78
FT_Sin(FT_Angle angle)
Definition: fttrigon.c:294
#define FT_ANGLE_2PI
Definition: fttrigon.h:76
T abs(T a)
Definition: glsl_math.hpp:646
FT_Cos(FT_Angle angle)
Definition: fttrigon.c:278
FT_Vector_Unit(FT_Vector *vec, FT_Angle angle)
Definition: fttrigon.c:340
png_voidp png_voidp s2
Definition: png.h:1956
int main(void)
Definition: test_trig.c:221
signed long FT_Fixed
Definition: fttypes.h:284
T cos(T a)
Definition: glsl_math.hpp:225
FT_Vector_Rotate(FT_Vector *vec, FT_Angle angle)
Definition: fttrigon.c:362
FT_Vector_Length(FT_Vector *vec)
Definition: fttrigon.c:400
T tan(T a)
Definition: glsl_math.hpp:251
#define THRESHOLD
Definition: test_trig.c:13