summaryrefslogtreecommitdiff
path: root/src/util/mini_math.h
blob: 3024737c4e91f7a3c742f7b1b4a95b78e194cf77 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// This file is part of the 64k demo project.
// It provides shared vector, matrix and animation utilities, templatized and
// inlined.

#pragma once
#include <cmath>

// --- Configuration ---
#define USE_VEC2
#define USE_VEC3
#define USE_VEC4
#define USE_QUAT
#define USE_MAT4
#define USE_EASING
#define USE_SPRING

// --- Operator Macro ---
// T: Class Name (e.g., vec3)
// N: Number of active components for math (e.g., 3)
#define VEC_OPERATORS(T, N)                                                    \
  float &operator[](int i) {                                                   \
    return v[i];                                                               \
  }                                                                            \
  const float &operator[](int i) const {                                       \
    return v[i];                                                               \
  }                                                                            \
  T &operator+=(const T &r) {                                                  \
    for (int i = 0; i < N; ++i)                                                \
      v[i] += r.v[i];                                                          \
    return *this;                                                              \
  }                                                                            \
  T &operator-=(const T &r) {                                                  \
    for (int i = 0; i < N; ++i)                                                \
      v[i] -= r.v[i];                                                          \
    return *this;                                                              \
  }                                                                            \
  T &operator*=(float s) {                                                     \
    for (int i = 0; i < N; ++i)                                                \
      v[i] *= s;                                                               \
    return *this;                                                              \
  }                                                                            \
  T operator+(const T &r) const {                                              \
    T res(*this);                                                              \
    res += r;                                                                  \
    return res;                                                                \
  }                                                                            \
  T operator-(const T &r) const {                                              \
    T res(*this);                                                              \
    res -= r;                                                                  \
    return res;                                                                \
  }                                                                            \
  T operator*(float s) const {                                                 \
    T res(*this);                                                              \
    res *= s;                                                                  \
    return res;                                                                \
  }                                                                            \
  T operator-() const {                                                        \
    T res;                                                                     \
    for (int i = 0; i < N; ++i)                                                \
      res.v[i] = -v[i];                                                        \
    return res;                                                                \
  }                                                                            \
  static float dot(const T &a, const T &b) {                                   \
    float s = 0;                                                               \
    for (int i = 0; i < N; ++i)                                                \
      s += a.v[i] * b.v[i];                                                    \
    return s;                                                                  \
  }                                                                            \
  float dot(const T &a) const {                                                \
    return dot(*this, a);                                                      \
  }                                                                            \
  float norm() const {                                                         \
    return std::sqrt(dot(*this, *this));                                       \
  }                                                                            \
  float len() const {                                                          \
    return norm();                                                             \
  }                                                                            \
  float inv_norm() const {                                                     \
    float l2 = dot(*this, *this);                                              \
    return l2 > 0 ? 1.0f / std::sqrt(l2) : 0;                                  \
  }                                                                            \
  T normalize() const {                                                        \
    return (*this) * inv_norm();                                               \
  }

#ifdef USE_VEC2
struct vec2 {
  union {
    struct {
      float x, y;
    };
    float v[2];
  };
  vec2(float x = 0, float y = 0) : x(x), y(y) {
  }
  VEC_OPERATORS(vec2, 2)
};
#endif

#ifdef USE_VEC3
struct vec3 {
  union {
    struct {
      float x, y, z;
      float _;
    }; // _ is padding for 16-byte alignment
    float v[4]; // Size 4 to match alignment
  };
  vec3(float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z), _(0) {
  }
  VEC_OPERATORS(vec3, 3) // Operators only touch x,y,z (indices 0,1,2)

  static vec3 cross(vec3 a, vec3 b) {
    return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
            a.x * b.y - a.y * b.x};
  }
};
#endif

#ifdef USE_VEC4
struct vec4 {
  union {
    struct {
      float x, y, z, w;
    };
    float v[4];
  };
  vec4(float x = 0, float y = 0, float z = 0, float w = 0)
      : x(x), y(y), z(z), w(w) {
  }
  VEC_OPERATORS(vec4, 4)
};
#endif

#ifdef USE_MAT4
struct mat4 {
  float m[16] = {1, 0, 0, 0, 0, 1, 0, 0,
                 0, 0, 1, 0, 0, 0, 0, 1}; // Identity (Column-Major)

  static mat4 perspective(float fov, float asp, float n, float f) {
    mat4 r = {};
    float t = 1.0f / std::tan(fov * 0.5f);
    r.m[0] = t / asp;
    r.m[5] = t;
    r.m[10] = f / (n - f);
    r.m[11] = -1;
    r.m[14] = (n * f) / (n - f);
    return r;
  }

  static mat4 look_at(vec3 eye, vec3 center, vec3 up) {
    vec3 f = (center - eye).normalize();
    vec3 s = vec3::cross(f, up).normalize();
    vec3 u = vec3::cross(s, f);
    mat4 res;
    res.m[0] = s.x;
    res.m[4] = s.y;
    res.m[8] = s.z;
    res.m[1] = u.x;
    res.m[5] = u.y;
    res.m[9] = u.z;
    res.m[2] = -f.x;
    res.m[6] = -f.y;
    res.m[10] = -f.z;
    res.m[12] = -vec3::dot(s, eye);
    res.m[13] = -vec3::dot(u, eye);
    res.m[14] = vec3::dot(f, eye);
    return res;
  }
};
#endif

#ifdef USE_QUAT
struct quat {
  union {
    struct {
      float x, y, z, w;
    };
    float v[4];
  };
  quat(float x = 0, float y = 0, float z = 0, float w = 1)
      : x(x), y(y), z(z), w(w) {
  }
  VEC_OPERATORS(quat, 4)

  quat operator*(const quat &q) const {
    return {w * q.x + x * q.w + y * q.z - z * q.y,
            w * q.y - x * q.z + y * q.w + z * q.x,
            w * q.z + x * q.y - y * q.x + z * q.w,
            w * q.w - x * q.x - y * q.y - z * q.z};
  }

  static quat from_axis(vec3 a, float ang) {
    float s = std::sin(ang * 0.5f);
    return {a.x * s, a.y * s, a.z * s, std::cos(ang * 0.5f)};
  }

  static quat from_to(vec3 a, vec3 b) {
    float d = vec3::dot(a, b);
    vec3 axis = vec3::cross(a, b);
    if (d < -0.9999f)
      return {0, 1, 0, 0};
    float s = std::sqrt((1.0f + d) * 2.0f), inv_s = 1.0f / s;
    return {axis.x * inv_s, axis.y * inv_s, axis.z * inv_s, s * 0.5f};
  }

  static quat look_at(vec3 eye, vec3 target, vec3 up) {
    vec3 f = (target - eye).normalize();
    vec3 r = vec3::cross(f, up).normalize();
    vec3 u = vec3::cross(r, f);
    float m00 = r.x, m11 = u.y, m22 = -f.z, tr = m00 + m11 + m22;
    if (tr > 0) {
      float s = std::sqrt(tr + 1.0f) * 2.0f;
      return {(u.z - (-f.y)) / s, ((-f.x) - r.z) / s, (r.y - u.x) / s,
              0.25f * s};
    } else if ((m00 > m11) && (m00 > m22)) {
      float s = std::sqrt(1.0f + m00 - m11 - m22) * 2.0f;
      return {0.25f * s, (r.y + u.x) / s, ((-f.x) + r.z) / s,
              (u.z - (-f.y)) / s};
    } else if (m11 > m22) {
      float s = std::sqrt(1.0f + m11 - m00 - m22) * 2.0f;
      return {(r.y + u.x) / s, 0.25f * s, (u.z + (-f.y)) / s,
              ((-f.x) - r.z) / s};
    } else {
      float s = std::sqrt(1.0f + m22 - m00 - m11) * 2.0f;
      return {((-f.x) + r.z) / s, (u.z + (-f.y)) / s, 0.25f * s,
              (r.y - u.x) / s};
    }
  }

  vec3 rotate(vec3 v_in) const {
    vec3 qv(x, y, z), t = vec3::cross(qv, v_in) * 2.0f;
    return v_in + t * w + vec3::cross(qv, t);
  }

  mat4 to_mat() const {
    mat4 r;
    float x2 = x + x, y2 = y + y, z2 = z + z, xx = x * x2, xy = x * y2,
          xz = x * z2, yy = y * y2, yz = y * z2, zz = z * z2, wx = w * x2,
          wy = w * y2, wz = w * z2;
    r.m[0] = 1 - (yy + zz);
    r.m[4] = xy - wz;
    r.m[8] = xz + wy;
    r.m[1] = xy + wz;
    r.m[5] = 1 - (xx + zz);
    r.m[9] = yz - wx;
    r.m[2] = xz - wy;
    r.m[6] = yz + wx;
    r.m[10] = 1 - (xx + yy);
    return r;
  }
};

inline quat slerp(quat a, quat b, float t) {
  float d = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  if (d < 0) {
    b = b * -1.0f;
    d = -d;
  }
  if (d > 0.9995f) { // Linear fall-back
    quat r;
    for (int i = 0; i < 4; ++i)
      r.v[i] = a.v[i] + (b.v[i] - a.v[i]) * t;
    return r;
  }
  float th0 = std::acos(d), th = th0 * t, s0 = std::sin(th0),
        s1 = std::sin(th) / s0, s2 = std::sin(th0 - th) / s0;
  return a * s2 + b * s1;
}
#endif

template <typename T> inline T lerp(const T &a, const T &b, float t) {
  return a + (b - a) * t;
}

#ifdef USE_EASING
namespace ease {
inline float out_cubic(float t) {
  return 1.0f - std::pow(1.0f - t, 3.0f);
}
inline float in_out_quad(float t) {
  return t < 0.5f ? 2.0f * t * t
                  : 1.0f - std::pow(-2.0f * t + 2.0f, 2.0f) / 2.0f;
}
inline float out_expo(float t) {
  return t == 1.0f ? 1.0f : 1.0f - std::pow(2.0f, -10.0f * t);
}
}
#endif

#ifdef USE_SPRING
namespace spring {
template <typename T>
void solve(T &current, T &velocity, const T &target, float smooth_time,
           float dt) {
  float omega = 2.0f / smooth_time;
  float x = omega * dt;
  float exp = 1.0f / (1.0f + x + 0.48f * x * x + 0.235f * x * x * x);
  T change = current - target;
  T temp = (velocity + change * omega) * dt;
  velocity = (velocity - temp * omega) * exp;
  current = target + (change + temp) * exp;
}
}
#endif