Skip to content

Commit d9cfe32

Browse files
committed
Rename the fast trig functions
1 parent b9435e0 commit d9cfe32

File tree

9 files changed

+34
-34
lines changed

9 files changed

+34
-34
lines changed

demos/01_spinning_cube.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main(int argc, char** argv) {
2424
signal(SIGINT, interrupt_handler);
2525
// initialise objects to render
2626
mesh_t* shape = obj_mesh_from_file(g_mesh_file, g_cx, g_cy, g_cz, g_width, g_height, g_depth);
27-
init_lookup_tables();
27+
ftrig_init_lut();
2828
// do the actual rendering
2929
render_init();
3030
for (size_t t = 0; t < g_max_iterations; ++t) {

demos/02_3_diamonds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int main(int argc, char** argv) {
3434
sprintf(mesh_filepath, "%s/%s", mesh_dir, mesh_filename);
3535
assert(access(mesh_filepath, F_OK) == 0);
3636

37-
init_lookup_tables();
37+
ftrig_init_lut();
3838
mesh_t* obj1 = obj_mesh_from_file(mesh_filepath, -40, 0, 150, 60, 80, 60);
3939
mesh_t* obj2 = obj_mesh_from_file(mesh_filepath, 0, 0, 250, 60, 80, 60);
4040
mesh_t* obj3 = obj_mesh_from_file(mesh_filepath, 40, 0, 350, 60, 80, 60);

demos/03_3_diamonds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int main(int argc, char** argv) {
4040
const char* mesh_filename = "rhombus.scl";
4141
sprintf(mesh_filepath, "%s/%s", mesh_dir, mesh_filename);
4242
assert(access(mesh_filepath, F_OK) == 0);
43-
init_lookup_tables();
43+
ftrig_init_lut();
4444

4545
// draw the same object in 3 different depths to showcase perspective
4646
mesh_t* obj1 = obj_mesh_from_file(mesh_filepath, -80, 0, 600, w, h, d);

demos/04_overlapping_objects.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int main(int argc, char** argv) {
2828
// make sure we end gracefully if the user hits Ctr+C
2929
signal(SIGINT, interrupt_handler);
3030

31-
init_lookup_tables();
31+
ftrig_init_lut();
3232
render_init();
3333

3434
// path to directory where meshes are stored - dir stored in CFG_DIR prep. constant
@@ -55,9 +55,9 @@ int main(int argc, char** argv) {
5555
#endif
5656
for (size_t t = 0; t < UINT_MAX; ++t) {
5757
obj_mesh_rotate_to(obj1, 1.0/80*t, 1.0/40*t, 1.0/60*t);
58-
obj_mesh_rotate_to(obj2, amplitude_x*xsin(random_rot_speed_x*xsin(random_rot_speed_x*t) + 2*random_bias_x),
59-
amplitude_y*xsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
60-
amplitude_z*xsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
58+
obj_mesh_rotate_to(obj2, amplitude_x*fsin(random_rot_speed_x*fsin(random_rot_speed_x*t) + 2*random_bias_x),
59+
amplitude_y*fsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
60+
amplitude_z*fsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
6161
render_write_shape(obj1);
6262
render_write_shape(obj2);
6363
render_flush();

demos/05_moving_object.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int main(int argc, char** argv) {
2525
// make sure we end gracefully if the user hits Ctr+C
2626
signal(SIGINT, interrupt_handler);
2727

28-
init_lookup_tables();
28+
ftrig_init_lut();
2929
render_init();
3030

3131
mesh_t* shape = obj_mesh_from_file(g_mesh_file, g_cx, g_cy, g_cz, g_width, g_height, g_depth);
@@ -40,9 +40,9 @@ int main(int argc, char** argv) {
4040
#endif
4141
for (size_t t = 0; t < g_max_iterations; ++t) {
4242
if (g_use_random_rotation)
43-
obj_mesh_rotate_to(shape, amplitude_x*xsin(random_rot_speed_x*xsin(random_rot_speed_x*t) + 2*random_bias_x),
44-
amplitude_y*xsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
45-
amplitude_z*xsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
43+
obj_mesh_rotate_to(shape, amplitude_x*fsin(random_rot_speed_x*fsin(random_rot_speed_x*t) + 2*random_bias_x),
44+
amplitude_y*fsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
45+
amplitude_z*fsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
4646
else
4747
obj_mesh_rotate_to(shape, g_rot_speed_x/20*t, g_rot_speed_y/20*t, g_rot_speed_z/20*t);
4848
if ((t % 100) >= 50)

include/xtrig.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
#define LUT_SIZE 901 // (int)(HALF_PI/ LUT_BIN_SIZE + 1)
1010

1111
/** sine lookup table (LUT) with sampled values from 0 to pi/2 */
12-
extern double sine_lookup[LUT_SIZE];
12+
extern double sine_lut[LUT_SIZE];
1313

14-
/** Initialize lookup tables - must be called before xsin or xcos */
15-
void init_lookup_tables();
14+
/** Initialize lookup tables - must be called before fsin or fcos */
15+
void ftrig_init_lut();
1616
/** fast sine */
17-
double xsin(double angle);
17+
double fsin(double angle);
1818
/** fast cosine */
19-
double xcos(double angle);
19+
double fcos(double angle);
2020

2121
#endif // XTRIG_H

main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main(int argc, char** argv) {
2424
signal(SIGINT, interrupt_handler);
2525

2626
render_init();
27-
init_lookup_tables();
27+
ftrig_init_lut();
2828

2929
mesh_t* shape = obj_mesh_from_file(g_mesh_file, g_cx, g_cy, g_cz, g_width, g_height, g_depth);
3030
// spinning parameters in case random rotation was selected
@@ -38,9 +38,9 @@ int main(int argc, char** argv) {
3838
#endif
3939
for (size_t t = 0; t < g_max_iterations; ++t) {
4040
if (g_use_random_rotation)
41-
obj_mesh_rotate_to(shape, amplitude_x*xsin(random_rot_speed_x*xsin(random_rot_speed_x*t) + 2*random_bias_x),
42-
amplitude_y*xsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
43-
amplitude_z*xsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
41+
obj_mesh_rotate_to(shape, amplitude_x*fsin(random_rot_speed_x*fsin(random_rot_speed_x*t) + 2*random_bias_x),
42+
amplitude_y*fsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
43+
amplitude_z*fsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
4444
else
4545
obj_mesh_rotate_to(shape, g_rot_speed_x/20*t, g_rot_speed_y/20*t, g_rot_speed_z/20*t);
4646
if (g_bounce_every != 0) {

src/vector.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ void vec_vec3_rotate(vec3_t* src, float angle_x_rad, float angle_y_rad, float an
6262
src->z -= z0;
6363

6464
float a = angle_x_rad, b = angle_y_rad, c = angle_z_rad;
65-
float ca = xcos(a), cb = xcos(b), cc = xcos(c);
66-
float sa = xsin(a), sb = xsin(b), sc = xsin(c);
65+
float ca = fcos(a), cb = fcos(b), cc = fcos(c);
66+
float sa = fsin(a), sb = fsin(b), sc = fsin(c);
6767
float matrix_rotx[3][3] = {
6868
{1, 0, 0 },
6969
{0, ca, -sa},
@@ -165,8 +165,8 @@ void vec_vec3i_rotate(vec3i_t* src, float angle_x_rad, float angle_y_rad, float
165165
rotated.z -= z0;
166166

167167
const float a = angle_x_rad, b = angle_y_rad, c = angle_z_rad;
168-
const float ca = xcos(a), cb = xcos(b), cc = xcos(c);
169-
const float sa = xsin(a), sb = xsin(b), sc = xsin(c);
168+
const float ca = fcos(a), cb = fcos(b), cc = fcos(c);
169+
const float sa = fsin(a), sb = fsin(b), sc = fsin(c);
170170
const float matrix_rotx[3][3] = {
171171
{1, 0, 0 },
172172
{0, ca, -sa},

src/xtrig.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
#include "xtrig.h"
22

3-
double sine_lookup[LUT_SIZE];
3+
double sine_lut[LUT_SIZE];
44

5-
void init_lookup_tables() {
5+
void ftrig_init_lut() {
66
for (int i = 0; i < LUT_SIZE; i++)
7-
sine_lookup[i] = sin(i * LUT_BIN_SIZE);
7+
sine_lut[i] = sin(i * LUT_BIN_SIZE);
88
}
99

1010
/** Get the index of an angle in the LUT */
1111
static int get_lookup_index(double rad) {
1212
return (int) round(rad / LUT_BIN_SIZE);
1313
}
1414

15-
double xsin(double rad) {
15+
double fsin(double rad) {
1616
// use the period and symmetry to compute based off [0, pi/2]
1717
rad = fmod(rad, 2*M_PI);
1818
if (rad < 0) rad += 2*M_PI;
1919
if (rad <= HALF_PI) {
20-
return sine_lookup[get_lookup_index(rad)];
20+
return sine_lut[get_lookup_index(rad)];
2121
} else if (rad <= M_PI) {
22-
return sine_lookup[get_lookup_index(M_PI - rad)];
22+
return sine_lut[get_lookup_index(M_PI - rad)];
2323
} else if (rad <= 3*HALF_PI) {
24-
return -sine_lookup[get_lookup_index(rad - M_PI)];
24+
return -sine_lut[get_lookup_index(rad - M_PI)];
2525
} else {
26-
return -sine_lookup[get_lookup_index(2*M_PI - rad)];
26+
return -sine_lut[get_lookup_index(2*M_PI - rad)];
2727
}
2828
}
2929

30-
double xcos(double rad) {
31-
return xsin(HALF_PI - rad);
30+
double fcos(double rad) {
31+
return fsin(HALF_PI - rad);
3232
}
3333

0 commit comments

Comments
 (0)