hermespy-rt

Minimalistic signal processing ray-tracer in C
git clone https://git.ea.contact/hermespy-rt
Log | Files | Refs

vec3.h (1023B)


      1 #ifndef VEC3_H
      2 #define VEC3_H
      3 
      4 #include <math.h> /* for sqrtf */
      5 
      6 typedef struct {
      7   float x, y, z;
      8 } Vec3;
      9 
     10 static inline Vec3 vec3_sub(const Vec3 *a, const Vec3 *b)
     11 {
     12   Vec3 result = {a->x - b->x, a->y - b->y, a->z - b->z};
     13   return result;
     14 }
     15 static inline Vec3 vec3_add(const Vec3 *a, const Vec3 *b)
     16 {
     17   Vec3 result = {a->x + b->x, a->y + b->y, a->z + b->z};
     18   return result;
     19 }
     20 static inline Vec3 vec3_cross(const Vec3 *a, const Vec3 *b)
     21 {
     22   Vec3 result = {
     23     a->y * b->z - a->z * b->y,
     24     a->z * b->x - a->x * b->z,
     25     a->x * b->y - a->y * b->x
     26   };
     27   return result;
     28 }
     29 static inline float vec3_dot(const Vec3 *a, const Vec3 *b)
     30 {
     31   return a->x * b->x + a->y * b->y + a->z * b->z;
     32 }
     33 static inline Vec3 vec3_scale(const Vec3 *a, float s)
     34 {
     35   Vec3 result = {a->x * s, a->y * s, a->z * s};
     36   return result;
     37 }
     38 static inline Vec3 vec3_normalize(const Vec3 *a)
     39 {
     40   float norm = sqrtf(a->x * a->x + a->y * a->y + a->z * a->z);
     41   Vec3 result = {a->x / norm, a->y / norm, a->z / norm};
     42   return result;
     43 }
     44 
     45 #endif /* VEC3_H */