ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tcos.c
Revision: 3.6
Committed: Thu Jan 17 22:48:21 2013 UTC (11 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.5: +2 -2 lines
Log Message:
Increased table size from 256 to 1024

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 greg 3.6 static const char RCSid[] = "$Id: tcos.c,v 3.5 2011/02/11 00:35:14 greg Exp $";
3 gwlarson 3.1 #endif
4     /*
5     * Table-based cosine approximation.
6     *
7     * Use doubles in table even though we're not nearly that accurate just
8     * to avoid conversion and guarantee that tsin(x)^2 + tcos(x)^2 == 1.
9     *
10     * No interpolation in this version.
11 greg 3.2 *
12 greg 3.5 * External symbols declared in rtmath.h
13 greg 3.2 */
14    
15 greg 3.3 #include "copyright.h"
16 gwlarson 3.1
17     #include <math.h>
18 schorsch 3.4
19     #include "rtmath.h"
20 gwlarson 3.1
21     #ifndef NCOSENTRY
22 greg 3.6 #define NCOSENTRY 1024
23 gwlarson 3.1 #endif
24    
25    
26     double
27 greg 3.5 tcos(double x) /* approximate cosine */
28 gwlarson 3.1 {
29     static double costab[NCOSENTRY+1];
30     register int i;
31    
32     if (costab[0] < 0.5) /* initialize table */
33     for (i = 0; i <= NCOSENTRY; i++)
34     costab[i] = cos((PI/2./NCOSENTRY)*i);
35     /* normalize angle */
36     if (x < 0.)
37     x = -x;
38     i = (NCOSENTRY*2./PI) * x + 0.5;
39 greg 3.5 while (i >= 4*NCOSENTRY)
40     i -= 4*NCOSENTRY;
41 gwlarson 3.1 switch (i / NCOSENTRY) {
42     case 0:
43     return(costab[i]);
44     case 1:
45     return(-costab[(2*NCOSENTRY)-i]);
46     case 2:
47     return(-costab[i-(2*NCOSENTRY)]);
48     case 3:
49     return(costab[(4*NCOSENTRY)-i]);
50     }
51     return(0.); /* should never be reached */
52     }