Separate vector functions to separate file
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 22 Nov 2024 19:35:04 +0000 (20:35 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 23 Nov 2024 11:05:51 +0000 (12:05 +0100)
Makefile
inc/miniRT.h
inc/vec3.h [new file with mode: 0644]
src/scene.c
src/vec3.c [new file with mode: 0644]

index da0a728669acb41b562b510ddacd2c475a7eb593..f3cf1bba71d2ba855ed49b28e64525e5dce4780e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -22,6 +22,7 @@ SRCDIR := src
 
 SOURCES :=     main.c                          \
                        scene.c                         \
+                       vec3.c                          \
 
 SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
 
index dfe55ec4d037f0992c76b50061a3f18b809e1dc1..b836d35ecf6965ae8dad14e626236c7fd7101722 100644 (file)
@@ -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 (file)
index 0000000..787b806
--- /dev/null
@@ -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
index ffb58f9378fdc5e148e67ba3b264578bb1bef9a9..e7aa5cc6d045d3e4165d037953c679bb1017eda9 100644 (file)
@@ -1,3 +1,4 @@
+#include "vec3.h"
 #include "miniRT.h"
 #include "libft.h"
 #include <math.h>
@@ -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 (file)
index 0000000..3a0697f
--- /dev/null
@@ -0,0 +1,62 @@
+#include "vec3.h"
+#include <math.h>
+
+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)));
+}