aboutsummaryrefslogtreecommitdiff
path: root/include/math/vec3.h
blob: 641c02f3b92869e946ce4aa8fb3dcebfbabb7327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once

#include "defs.h"

#include <assert.h>
#include <stdbool.h>

/// A 3D vector.
typedef struct vec3 {
  R x, y, z;
} vec3;

/// Construct a vector from 3 coordinates.
static inline vec3 vec3_make(R x, R y, R z) { return (vec3){x, y, z}; }

/// Construct a vector from an array.
static inline vec3 vec3_from_array(const R xyz[3]) {
  return (vec3){xyz[0], xyz[1], xyz[2]};
}

/// Construct a vector from a single scalar value.
/// x = y = z = val.
static inline vec3 vec3_from_scalar(R val) { return (vec3){val, val, val}; }

/// Return the vector's ith coordinate.
static inline R vec3_ith(vec3 v, int i) {
  assert(i >= 0 && i < 3);
  return ((const R*)&v)[i];
}

/// Negate the given vector.
static inline vec3 vec3_neg(vec3 v) { return (vec3){-v.x, -v.y, -v.z}; }

/// Add two vectors.
static inline vec3 vec3_add(vec3 a, vec3 b) {
  return (vec3){a.x + b.x, a.y + b.y, a.z + b.z};
}

/// Subtract two vectors.
static inline vec3 vec3_sub(vec3 a, vec3 b) {
  return (vec3){a.x - b.x, a.y - b.y, a.z - b.z};
}

/// Modulate two vectors (component-wise multiplication).
static inline vec3 vec3_mul(vec3 a, vec3 b) {
  return (vec3){a.x * b.x, a.y * b.y, a.z * b.z};
}

/// Divide two vectors component-wise.
static inline vec3 vec3_div(vec3 a, vec3 b) {
  return (vec3){a.x / b.x, a.y / b.y, a.z / b.z};
}

/// Scale a vector by a scalar value.
static inline vec3 vec3_scale(vec3 v, R s) {
  return (vec3){v.x * s, v.y * s, v.z * s};
}

/// Compare two vectors for equality.
static inline bool vec3_eq(vec3 a, vec3 b) {
  return a.x == b.x && a.y == b.y && a.z == b.z;
}

/// Return the absolute value of the vector.
static inline vec3 vec3_abs(vec3 v) {
  return (vec3){rabs(v.x), rabs(v.y), rabs(v.z)};
}

/// Compare two vectors for inequality.
static inline bool vec3_ne(vec3 a, vec3 b) { return !(vec3_eq(a, b)); }

/// Return the vector's squared magnitude.
static inline R vec3_norm2(vec3 v) { return v.x * v.x + v.y * v.y + v.z * v.z; }

/// Return the vector's magnitude.
static inline R vec3_norm(vec3 v) { return sqrt(vec3_norm2(v)); }

/// Return the squared distance between two points.
static inline R vec3_dist2(vec3 a, vec3 b) {
  const vec3 v = vec3_sub(b, a);
  return vec3_norm2(v);
}

/// Return the distance between two points.
static inline R vec3_dist(vec3 a, vec3 b) { return sqrt(vec3_dist2(a, b)); }

/// Return the given vector divided by its magnitude.
static inline vec3 vec3_normalize(vec3 v) {
  const R n = vec3_norm(v);
  assert(n > 0);
  return (vec3){v.x / n, v.y / n, v.z / n};
}

/// Return the dot product of two vectors.
static inline R vec3_dot(vec3 a, vec3 b) {
  return a.x * b.x + a.y * b.y + a.z * b.z;
}

/// Return the cross product of two vectors.
static inline vec3 vec3_cross(vec3 a, vec3 b) {
  return (vec3){
      a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
}

/// Reflect the vector about the normal.
static inline vec3 vec3_reflect(vec3 v, vec3 n) {
  // r = v - 2 * dot(v, n) * n
  return vec3_sub(v, vec3_scale(n, 2 * vec3_dot(v, n)));
}

/// Refract the vector about the normal.
static inline vec3 vec3_refract(vec3 v, vec3 n, R e) {
  // k = 1 - e^2(1 - dot(n,v) * dot(n,v))
  const R k = 1.0 - e * e * (1.0 - vec3_dot(n, v) * vec3_dot(n, v));
  assert(k >= 0);
  // r = e*v - (e * dot(n,v) + sqrt(k)) * n
  return vec3_sub(
      vec3_scale(v, e), vec3_scale(n, e * vec3_dot(n, v) * sqrt(k)));
}

/// Elevate the vector to a power.
static inline vec3 vec3_pow(vec3 v, R p) {
  return (vec3){pow(v.x, p), pow(v.y, p), pow(v.z, p)};
}

/// The (1, 0, 0) vector.
static inline vec3 right3() { return (vec3){1.0, 0.0, 0.0}; }

/// The (0, 1, 0) vector.
static inline vec3 up3() { return (const vec3){0.0, 1.0, 0.0}; }

/// The (0, 0, -1) vector.
static inline vec3 forward3() { return (const vec3){0.0, 0.0, -1.0}; }

/// The (0, 0, 0) vector.
static inline vec3 zero3() { return (const vec3){0.0, 0.0, 0.0}; }