-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcos-sin.cc
144 lines (100 loc) · 3.39 KB
/
cos-sin.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* coords/cos-sin.cc
*
* Copyright 2006 by The University of York
* Author: Paul Emsley
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copies of the GNU General Public License and
* the GNU Lesser General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 51 Franklin Street,
* Fifth Floor, Boston, MA, 02110-1301, USA.
* See http://www.gnu.org/licenses/
*
*/
/* given the sine of an angle, return the cosine,
e.g sqrt(3)/2 returns 1/2.
We are only interested in values of angles between 0-90 degrees (pi/2)
so that (for now) we don't consider negative sine/cosines.
We create a table before this function is used which contains a
number of reference points, which will be used for simple
interpolation here.
*/
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include "cos-sin.h"
// initialize the statics in class cos-sin. They don't matter because
// the object is reinitiallised in init() [globjects.cc].
int cos_sin::cos_to_sine_table_steps = 1000;
int cos_sin::is_table_filled = 0;
float *cos_sin::cos_to_sine_table = NULL;
float
cos_sin::operator()(float v) const {
int whole_part;
float frac_part;
float tmp;
float a1, a2, a_interp;
/* Recall that the sin is symmetric about pi/2 and that
we only have cos values between 0 and pi. */
if (v < 0.0) {
v = -v;
}
if (v > 1.0) {
std::cout << "Impossible cosine: " << v << std::endl;
v = 1.0;
}
/* Check that there are values in the table */
if (is_table_filled == FALSE) {
std::cout << "ERROR:: Need to call construct_cos_to_sin_table() first"
<< std::endl;
return 0;
}
tmp = (v*cos_to_sine_table_steps);
whole_part = (int) tmp;
frac_part = tmp - (float) whole_part;
if (frac_part != 0.0) {
/* The usual case - interpolation needed */
a1 = cos_to_sine_table[whole_part];
a2 = cos_to_sine_table[whole_part+1];
a_interp = a1 + frac_part*(a2-a1);
return a_interp;
} else {
/* simply lookup and return the whole_part */
return cos_to_sine_table[whole_part];
}
}
/* cos_steps typically 10000 */
void
cos_sin::fillTable (int cos_steps) {
int i;
float val, table_val;
/* assign the class's holder */
cos_to_sine_table_steps = cos_steps;
cos_to_sine_table = new float[cos_steps + 1];
for(i=0; i<= cos_steps; i++) {
val = (float) (i/(float)cos_steps);
table_val = (float) sin(acos(val));
cos_to_sine_table[i] = table_val;
}
is_table_filled = TRUE;
}
cos_sin::cos_sin(int isteps) {
fillTable(isteps);
}
cos_sin::cos_sin() {
// do nothing because this has already been inited in init (globjects).
//
}
cos_sin::~cos_sin() {
//std::cout << "deleting cos_sin table (disabled)" << std::endl;
// delete [] cos_to_sine_table;
}