tomwalters@0
|
1 /*
|
tomwalters@0
|
2 Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989
|
tomwalters@0
|
3 ===========================================================================
|
tomwalters@0
|
4
|
tomwalters@0
|
5 Permission to use, copy, modify, and distribute this software without fee
|
tomwalters@0
|
6 is hereby granted for research purposes, provided that this copyright
|
tomwalters@0
|
7 notice appears in all copies and in all supporting documentation, and that
|
tomwalters@0
|
8 the software is not redistributed for any fee (except for a nominal shipping
|
tomwalters@0
|
9 charge). Anyone wanting to incorporate all or part of this software in a
|
tomwalters@0
|
10 commercial product must obtain a license from the Medical Research Council.
|
tomwalters@0
|
11
|
tomwalters@0
|
12 The MRC makes no representations about the suitability of this
|
tomwalters@0
|
13 software for any purpose. It is provided "as is" without express or implied
|
tomwalters@0
|
14 warranty.
|
tomwalters@0
|
15
|
tomwalters@0
|
16 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
|
tomwalters@0
|
17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
|
tomwalters@0
|
18 A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
|
tomwalters@0
|
19 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
|
tomwalters@0
|
20 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
tomwalters@0
|
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
tomwalters@0
|
22 */
|
tomwalters@0
|
23
|
tomwalters@0
|
24 #include <stdio.h>
|
tomwalters@0
|
25 #include <math.h>
|
tomwalters@0
|
26
|
tomwalters@0
|
27 #include "stitch.h"
|
tomwalters@0
|
28
|
tomwalters@0
|
29 #define OUTPUT_SCALE 360
|
tomwalters@0
|
30
|
tomwalters@0
|
31 #ifdef TEST
|
tomwalters@0
|
32
|
tomwalters@0
|
33 #define TwoPi (3.1415926535*2)
|
tomwalters@0
|
34
|
tomwalters@0
|
35
|
tomwalters@0
|
36 main( argc, argv )
|
tomwalters@0
|
37 int argc ;
|
tomwalters@0
|
38 char *argv[] ;
|
tomwalters@0
|
39 {
|
tomwalters@0
|
40 double deg ;
|
tomwalters@0
|
41
|
tomwalters@0
|
42 printf( "hello\n" );
|
tomwalters@0
|
43
|
tomwalters@0
|
44 for( deg = 0 ; deg < TwoPi/2 ; deg += TwoPi/90001. )
|
tomwalters@0
|
45 if( (int) ( atan2( sin( deg ), cos( deg ) )/TwoPi*OUTPUT_SCALE + 0.5 ) != iatan2( (int) ( sin(deg)*1000 ), (int) ( cos(deg)*1000 ) ) )
|
tomwalters@0
|
46 printf( " %f %d\n", atan2( sin( deg ), cos( deg ) )/TwoPi*OUTPUT_SCALE, iatan2( (int) ( sin(deg)*1000 ), (int) ( cos(deg)*1000 ) ) ) ;
|
tomwalters@0
|
47
|
tomwalters@0
|
48 for( ; deg < TwoPi ; deg += TwoPi/90001. )
|
tomwalters@0
|
49 if( (int) ( atan2( sin( deg ), cos( deg ) )/TwoPi*OUTPUT_SCALE - 0.5 ) != iatan2( (int) ( sin(deg)*1000 ), (int) ( cos(deg)*1000 ) ) )
|
tomwalters@0
|
50 printf( " %f %d\n", atan2( sin( deg ), cos( deg ) )/TwoPi*OUTPUT_SCALE, iatan2( (int) ( sin(deg)*1000 ), (int) ( cos(deg)*1000 ) ) ) ;
|
tomwalters@0
|
51
|
tomwalters@0
|
52
|
tomwalters@0
|
53 }
|
tomwalters@0
|
54
|
tomwalters@0
|
55 #endif
|
tomwalters@0
|
56
|
tomwalters@0
|
57 iatan2( y, x )
|
tomwalters@0
|
58 int y, x ;
|
tomwalters@0
|
59 {
|
tomwalters@0
|
60 static int table_bits = 14 ;
|
tomwalters@0
|
61 static unsigned table_size ;
|
tomwalters@0
|
62 static short *atan_table = ( short * ) 0 ;
|
tomwalters@0
|
63 double two_phi ;
|
tomwalters@0
|
64 int i ;
|
tomwalters@0
|
65
|
tomwalters@0
|
66 if( atan_table == ( short * ) 0 ) {
|
tomwalters@0
|
67
|
tomwalters@0
|
68 table_size = 1 << table_bits ;
|
tomwalters@0
|
69
|
tomwalters@0
|
70 atan_table = ( short * ) stitch_malloc( sizeof ( *atan_table ) * ( table_size + 1 ), "Making atan lookup table" ) ;
|
tomwalters@0
|
71
|
tomwalters@0
|
72 two_phi = atan( 1. ) * 8. ;
|
tomwalters@0
|
73
|
tomwalters@0
|
74 for( i=0 ; i <= table_size ; i++ )
|
tomwalters@0
|
75 atan_table[ i ] = atan( ( double ) i / ( table_size ) ) / two_phi * OUTPUT_SCALE + 0.5 ;
|
tomwalters@0
|
76 }
|
tomwalters@0
|
77
|
tomwalters@0
|
78 if( x != 0 || y != 0 )
|
tomwalters@0
|
79 if( y > 0 )
|
tomwalters@0
|
80 if( x > 0 )
|
tomwalters@0
|
81 if( x > y )
|
tomwalters@0
|
82 return( atan_table[ ( ( y << table_bits + ( table_size >> 1 ) ) ) / x ] ) ;
|
tomwalters@0
|
83 else
|
tomwalters@0
|
84 return( ( OUTPUT_SCALE >> 2 ) - atan_table[ ( ( x << table_bits + ( table_size >> 1 ) ) ) / y ] ) ;
|
tomwalters@0
|
85 else
|
tomwalters@0
|
86 if( -x < y )
|
tomwalters@0
|
87 return( ( OUTPUT_SCALE >> 2 ) + atan_table[ ( ( -x << table_bits + ( table_size >> 1 ) ) ) / y ] ) ;
|
tomwalters@0
|
88 else
|
tomwalters@0
|
89 return( ( OUTPUT_SCALE >> 1 ) - atan_table[ ( ( y << table_bits + ( table_size >> 1 ) ) ) / -x ] ) ;
|
tomwalters@0
|
90 else
|
tomwalters@0
|
91 if( x < 0 )
|
tomwalters@0
|
92 if( -x > -y )
|
tomwalters@0
|
93 return( -( OUTPUT_SCALE >> 1 ) + atan_table[ ( ( -y << table_bits + ( table_size >> 1 ) ) ) / -x ] ) ;
|
tomwalters@0
|
94 else
|
tomwalters@0
|
95 return( -( OUTPUT_SCALE >> 2 ) - atan_table[ ( ( -x << table_bits + ( table_size >> 1 ) ) ) / -y ] ) ;
|
tomwalters@0
|
96 else
|
tomwalters@0
|
97 if( x < -y )
|
tomwalters@0
|
98 return( -( OUTPUT_SCALE >> 2 ) + atan_table[ ( ( x << table_bits + ( table_size >> 1 ) ) ) / -y ] ) ;
|
tomwalters@0
|
99 else
|
tomwalters@0
|
100 return( - atan_table[ ( ( -y << table_bits + ( table_size >> 1 ) ) ) / x ] ) ;
|
tomwalters@0
|
101 else
|
tomwalters@0
|
102 return( 99 ) ;
|
tomwalters@0
|
103 }
|