Skip to content

Commit

Permalink
Rename the fast trig functions
Browse files Browse the repository at this point in the history
  • Loading branch information
leonmavr committed Sep 26, 2024
1 parent b9435e0 commit d9cfe32
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion demos/01_spinning_cube.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main(int argc, char** argv) {
signal(SIGINT, interrupt_handler);
// initialise objects to render
mesh_t* shape = obj_mesh_from_file(g_mesh_file, g_cx, g_cy, g_cz, g_width, g_height, g_depth);
init_lookup_tables();
ftrig_init_lut();
// do the actual rendering
render_init();
for (size_t t = 0; t < g_max_iterations; ++t) {
Expand Down
2 changes: 1 addition & 1 deletion demos/02_3_diamonds.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main(int argc, char** argv) {
sprintf(mesh_filepath, "%s/%s", mesh_dir, mesh_filename);
assert(access(mesh_filepath, F_OK) == 0);

init_lookup_tables();
ftrig_init_lut();
mesh_t* obj1 = obj_mesh_from_file(mesh_filepath, -40, 0, 150, 60, 80, 60);
mesh_t* obj2 = obj_mesh_from_file(mesh_filepath, 0, 0, 250, 60, 80, 60);
mesh_t* obj3 = obj_mesh_from_file(mesh_filepath, 40, 0, 350, 60, 80, 60);
Expand Down
2 changes: 1 addition & 1 deletion demos/03_3_diamonds.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main(int argc, char** argv) {
const char* mesh_filename = "rhombus.scl";
sprintf(mesh_filepath, "%s/%s", mesh_dir, mesh_filename);
assert(access(mesh_filepath, F_OK) == 0);
init_lookup_tables();
ftrig_init_lut();

// draw the same object in 3 different depths to showcase perspective
mesh_t* obj1 = obj_mesh_from_file(mesh_filepath, -80, 0, 600, w, h, d);
Expand Down
8 changes: 4 additions & 4 deletions demos/04_overlapping_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main(int argc, char** argv) {
// make sure we end gracefully if the user hits Ctr+C
signal(SIGINT, interrupt_handler);

init_lookup_tables();
ftrig_init_lut();
render_init();

// path to directory where meshes are stored - dir stored in CFG_DIR prep. constant
Expand All @@ -55,9 +55,9 @@ int main(int argc, char** argv) {
#endif
for (size_t t = 0; t < UINT_MAX; ++t) {
obj_mesh_rotate_to(obj1, 1.0/80*t, 1.0/40*t, 1.0/60*t);
obj_mesh_rotate_to(obj2, amplitude_x*xsin(random_rot_speed_x*xsin(random_rot_speed_x*t) + 2*random_bias_x),
amplitude_y*xsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
amplitude_z*xsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
obj_mesh_rotate_to(obj2, amplitude_x*fsin(random_rot_speed_x*fsin(random_rot_speed_x*t) + 2*random_bias_x),
amplitude_y*fsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
amplitude_z*fsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
render_write_shape(obj1);
render_write_shape(obj2);
render_flush();
Expand Down
8 changes: 4 additions & 4 deletions demos/05_moving_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(int argc, char** argv) {
// make sure we end gracefully if the user hits Ctr+C
signal(SIGINT, interrupt_handler);

init_lookup_tables();
ftrig_init_lut();
render_init();

mesh_t* shape = obj_mesh_from_file(g_mesh_file, g_cx, g_cy, g_cz, g_width, g_height, g_depth);
Expand All @@ -40,9 +40,9 @@ int main(int argc, char** argv) {
#endif
for (size_t t = 0; t < g_max_iterations; ++t) {
if (g_use_random_rotation)
obj_mesh_rotate_to(shape, amplitude_x*xsin(random_rot_speed_x*xsin(random_rot_speed_x*t) + 2*random_bias_x),
amplitude_y*xsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
amplitude_z*xsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
obj_mesh_rotate_to(shape, amplitude_x*fsin(random_rot_speed_x*fsin(random_rot_speed_x*t) + 2*random_bias_x),
amplitude_y*fsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
amplitude_z*fsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
else
obj_mesh_rotate_to(shape, g_rot_speed_x/20*t, g_rot_speed_y/20*t, g_rot_speed_z/20*t);
if ((t % 100) >= 50)
Expand Down
10 changes: 5 additions & 5 deletions include/xtrig.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#define LUT_SIZE 901 // (int)(HALF_PI/ LUT_BIN_SIZE + 1)

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

/** Initialize lookup tables - must be called before xsin or xcos */
void init_lookup_tables();
/** Initialize lookup tables - must be called before fsin or fcos */
void ftrig_init_lut();
/** fast sine */
double xsin(double angle);
double fsin(double angle);
/** fast cosine */
double xcos(double angle);
double fcos(double angle);

#endif // XTRIG_H
8 changes: 4 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main(int argc, char** argv) {
signal(SIGINT, interrupt_handler);

render_init();
init_lookup_tables();
ftrig_init_lut();

mesh_t* shape = obj_mesh_from_file(g_mesh_file, g_cx, g_cy, g_cz, g_width, g_height, g_depth);
// spinning parameters in case random rotation was selected
Expand All @@ -38,9 +38,9 @@ int main(int argc, char** argv) {
#endif
for (size_t t = 0; t < g_max_iterations; ++t) {
if (g_use_random_rotation)
obj_mesh_rotate_to(shape, amplitude_x*xsin(random_rot_speed_x*xsin(random_rot_speed_x*t) + 2*random_bias_x),
amplitude_y*xsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
amplitude_z*xsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
obj_mesh_rotate_to(shape, amplitude_x*fsin(random_rot_speed_x*fsin(random_rot_speed_x*t) + 2*random_bias_x),
amplitude_y*fsin(random_rot_speed_y*random_bias_y*t + 2*random_bias_y),
amplitude_z*fsin(random_rot_speed_z*random_bias_z*t + 2*random_bias_z));
else
obj_mesh_rotate_to(shape, g_rot_speed_x/20*t, g_rot_speed_y/20*t, g_rot_speed_z/20*t);
if (g_bounce_every != 0) {
Expand Down
8 changes: 4 additions & 4 deletions src/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void vec_vec3_rotate(vec3_t* src, float angle_x_rad, float angle_y_rad, float an
src->z -= z0;

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

const float a = angle_x_rad, b = angle_y_rad, c = angle_z_rad;
const float ca = xcos(a), cb = xcos(b), cc = xcos(c);
const float sa = xsin(a), sb = xsin(b), sc = xsin(c);
const float ca = fcos(a), cb = fcos(b), cc = fcos(c);
const float sa = fsin(a), sb = fsin(b), sc = fsin(c);
const float matrix_rotx[3][3] = {
{1, 0, 0 },
{0, ca, -sa},
Expand Down
20 changes: 10 additions & 10 deletions src/xtrig.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#include "xtrig.h"

double sine_lookup[LUT_SIZE];
double sine_lut[LUT_SIZE];

void init_lookup_tables() {
void ftrig_init_lut() {
for (int i = 0; i < LUT_SIZE; i++)
sine_lookup[i] = sin(i * LUT_BIN_SIZE);
sine_lut[i] = sin(i * LUT_BIN_SIZE);
}

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

double xsin(double rad) {
double fsin(double rad) {
// use the period and symmetry to compute based off [0, pi/2]
rad = fmod(rad, 2*M_PI);
if (rad < 0) rad += 2*M_PI;
if (rad <= HALF_PI) {
return sine_lookup[get_lookup_index(rad)];
return sine_lut[get_lookup_index(rad)];
} else if (rad <= M_PI) {
return sine_lookup[get_lookup_index(M_PI - rad)];
return sine_lut[get_lookup_index(M_PI - rad)];
} else if (rad <= 3*HALF_PI) {
return -sine_lookup[get_lookup_index(rad - M_PI)];
return -sine_lut[get_lookup_index(rad - M_PI)];
} else {
return -sine_lookup[get_lookup_index(2*M_PI - rad)];
return -sine_lut[get_lookup_index(2*M_PI - rad)];
}
}

double xcos(double rad) {
return xsin(HALF_PI - rad);
double fcos(double rad) {
return fsin(HALF_PI - rad);
}

0 comments on commit d9cfe32

Please sign in to comment.