From: Lukas Jiriste Date: Fri, 22 Nov 2024 19:35:04 +0000 (+0100) Subject: Separate vector functions to separate file X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=8014340e9d00f99c3df772bf5b6bff65a21910c7;p=42%2FminiRT.git Separate vector functions to separate file --- diff --git a/Makefile b/Makefile index da0a728..f3cf1bb 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ SRCDIR := src SOURCES := main.c \ scene.c \ + vec3.c \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) diff --git a/inc/miniRT.h b/inc/miniRT.h index dfe55ec..b836d35 100644 --- a/inc/miniRT.h +++ b/inc/miniRT.h @@ -1,15 +1,9 @@ #ifndef MINIRT_H # define MINIRT_H +# include "vec3.h" # include "libft.h" -typedef struct s_vector_3d -{ - double x; - double y; - double z; -} t_vec3; - typedef t_vec3 t_color; typedef struct s_color_sRGB diff --git a/inc/vec3.h b/inc/vec3.h new file mode 100644 index 0000000..787b806 --- /dev/null +++ b/inc/vec3.h @@ -0,0 +1,19 @@ +#ifndef VEC3_H +# define VEC3_H + +typedef struct s_vector_3d +{ + double x; + double y; + double z; +} t_vec3; + +t_vec3 vec_add(t_vec3 v, t_vec3 u); +t_vec3 vec_diff(t_vec3 v, t_vec3 u); +t_vec3 vec_real_mul(t_vec3 vec, double real); +double vec_scalar_mul(t_vec3 v, t_vec3 u); +t_vec3 vec_elwise_mul(t_vec3 v, t_vec3 u); +t_vec3 vec_vec_mul(t_vec3 v, t_vec3 u); +double vec_norm(t_vec3 vec); + +#endif // VEC3_H diff --git a/src/scene.c b/src/scene.c index ffb58f9..e7aa5cc 100644 --- a/src/scene.c +++ b/src/scene.c @@ -1,3 +1,4 @@ +#include "vec3.h" #include "miniRT.h" #include "libft.h" #include @@ -30,56 +31,6 @@ double get_cylinder_circumsphere_radius(const t_cylinder *cylinder) return (sqrt(base_radius * base_radius + height * height / 4)); } -t_vec3 vec_add(t_vec3 v, t_vec3 u) -{ - t_vec3 res; - - res.x = v.x + u.x; - res.y = v.y + u.y; - res.z = v.z + u.z; - return (res); -} - -t_vec3 vec_diff(t_vec3 v, t_vec3 u) -{ - t_vec3 res; - - res.x = v.x - u.x; - res.y = v.y - u.y; - res.z = v.z - u.z; - return (res); -} - -t_vec3 vec_real_mul(t_vec3 vec, double real) -{ - t_vec3 res; - - res.x = real * vec.x; - res.y = real * vec.y; - res.z = real * vec.z; - return (res); -} - -double vec_scalar_mul(t_vec3 v, t_vec3 u) -{ - return (v.x * u.x + v.y * u.y + v.z * u.z); -} - -t_vec3 vec_elwise_mul(t_vec3 v, t_vec3 u) -{ - t_vec3 res; - - res.x = v.x * u.x; - res.y = v.y * u.y; - res.z = v.z * u.z; - return (res); -} - -double vec_norm(t_vec3 vec) -{ - return (sqrt(vec_scalar_mul(vec, vec))); -} - double dist_point_from_line(const t_ray *ray, t_vec3 point) { t_vec3 start; diff --git a/src/vec3.c b/src/vec3.c new file mode 100644 index 0000000..3a0697f --- /dev/null +++ b/src/vec3.c @@ -0,0 +1,62 @@ +#include "vec3.h" +#include + +t_vec3 vec_add(t_vec3 v, t_vec3 u) +{ + t_vec3 res; + + res.x = v.x + u.x; + res.y = v.y + u.y; + res.z = v.z + u.z; + return (res); +} + +t_vec3 vec_diff(t_vec3 v, t_vec3 u) +{ + t_vec3 res; + + res.x = v.x - u.x; + res.y = v.y - u.y; + res.z = v.z - u.z; + return (res); +} + +t_vec3 vec_real_mul(t_vec3 vec, double real) +{ + t_vec3 res; + + res.x = real * vec.x; + res.y = real * vec.y; + res.z = real * vec.z; + return (res); +} + +double vec_scalar_mul(t_vec3 v, t_vec3 u) +{ + return (v.x * u.x + v.y * u.y + v.z * u.z); +} + +t_vec3 vec_elwise_mul(t_vec3 v, t_vec3 u) +{ + t_vec3 res; + + res.x = v.x * u.x; + res.y = v.y * u.y; + res.z = v.z * u.z; + return (res); +} + +t_vec3 vec_vec_mul(t_vec3 v, t_vec3 u) +{ + t_vec3 res; + + res.x = v.y * u.z - v.z * u.y; + res.y = v.z * u.x - v.x * u.z; + res.z = v.x * u.y - v.y * u.x; + return (res); +} + +double vec_norm(t_vec3 vec) +{ + return (sqrt(vec_scalar_mul(vec, vec))); +}