1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#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 |
* |
12 |
* External symbols declared in standard.h |
13 |
*/ |
14 |
|
15 |
/* ==================================================================== |
16 |
* The Radiance Software License, Version 1.0 |
17 |
* |
18 |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
19 |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
20 |
* |
21 |
* Redistribution and use in source and binary forms, with or without |
22 |
* modification, are permitted provided that the following conditions |
23 |
* are met: |
24 |
* |
25 |
* 1. Redistributions of source code must retain the above copyright |
26 |
* notice, this list of conditions and the following disclaimer. |
27 |
* |
28 |
* 2. Redistributions in binary form must reproduce the above copyright |
29 |
* notice, this list of conditions and the following disclaimer in |
30 |
* the documentation and/or other materials provided with the |
31 |
* distribution. |
32 |
* |
33 |
* 3. The end-user documentation included with the redistribution, |
34 |
* if any, must include the following acknowledgment: |
35 |
* "This product includes Radiance software |
36 |
* (http://radsite.lbl.gov/) |
37 |
* developed by the Lawrence Berkeley National Laboratory |
38 |
* (http://www.lbl.gov/)." |
39 |
* Alternately, this acknowledgment may appear in the software itself, |
40 |
* if and wherever such third-party acknowledgments normally appear. |
41 |
* |
42 |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
43 |
* and "The Regents of the University of California" must |
44 |
* not be used to endorse or promote products derived from this |
45 |
* software without prior written permission. For written |
46 |
* permission, please contact [email protected]. |
47 |
* |
48 |
* 5. Products derived from this software may not be called "Radiance", |
49 |
* nor may "Radiance" appear in their name, without prior written |
50 |
* permission of Lawrence Berkeley National Laboratory. |
51 |
* |
52 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
53 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
54 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
55 |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
56 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
57 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
58 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
59 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
60 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
61 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
62 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
63 |
* SUCH DAMAGE. |
64 |
* ==================================================================== |
65 |
* |
66 |
* This software consists of voluntary contributions made by many |
67 |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
68 |
* information on Lawrence Berkeley National Laboratory, please see |
69 |
* <http://www.lbl.gov/>. |
70 |
*/ |
71 |
|
72 |
#include <math.h> |
73 |
|
74 |
#ifndef NCOSENTRY |
75 |
#define NCOSENTRY 256 |
76 |
#endif |
77 |
|
78 |
#ifdef M_PI |
79 |
#define PI ((double)M_PI) |
80 |
#else |
81 |
#define PI 3.14159265358979323846 |
82 |
#endif |
83 |
|
84 |
|
85 |
double |
86 |
tcos(x) /* approximate cosine */ |
87 |
register double x; |
88 |
{ |
89 |
static double costab[NCOSENTRY+1]; |
90 |
register int i; |
91 |
|
92 |
if (costab[0] < 0.5) /* initialize table */ |
93 |
for (i = 0; i <= NCOSENTRY; i++) |
94 |
costab[i] = cos((PI/2./NCOSENTRY)*i); |
95 |
/* normalize angle */ |
96 |
if (x < 0.) |
97 |
x = -x; |
98 |
i = (NCOSENTRY*2./PI) * x + 0.5; |
99 |
if (i >= 4*NCOSENTRY) |
100 |
i %= 4*NCOSENTRY; |
101 |
switch (i / NCOSENTRY) { |
102 |
case 0: |
103 |
return(costab[i]); |
104 |
case 1: |
105 |
return(-costab[(2*NCOSENTRY)-i]); |
106 |
case 2: |
107 |
return(-costab[i-(2*NCOSENTRY)]); |
108 |
case 3: |
109 |
return(costab[(4*NCOSENTRY)-i]); |
110 |
} |
111 |
return(0.); /* should never be reached */ |
112 |
} |