aboutsummaryrefslogtreecommitdiff
path: root/include/math/spatial3.h
blob: f8caf5d37bb39da7ca9542d2d93940184f35b0d8 (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
#pragma once

#include "mat4.h"
#include "vec3.h"

/// An object in 3D space.
typedef struct Spatial3 {
  vec3 p; // Position.
  vec3 r; // Right vector.
  vec3 u; // Up vector.
  vec3 f; // Forward vector.
} Spatial3;

/// Construct a spatial with position 0 and canonical direction vectors.
static inline Spatial3 spatial3_make() {
  return (Spatial3){.p = zero3(), .r = right3(), .u = up3(), .f = forward3()};
}

/// Return the spatial's transformation matrix (from local to world
/// coordinates).
static inline mat4 spatial3_transform(const Spatial3* spatial) {
  const vec3 p = spatial->p;
  const vec3 r = spatial->r;
  const vec3 u = spatial->u;
  const vec3 f = spatial->f;
  return mat4_make(r.x, u.x, -f.x, p.x, r.y, u.y, -f.y, p.y, r.z, u.z, -f.z,
                   p.z, 0.0, 0.0, 0.0, 1.0f);
}

/// Return the spatial's inverse transformation matrix (from world to local
/// coordinates).
static inline mat4 spatial3_inverse_transform(const Spatial3* spatial) {
  return mat4_inverse_transform(spatial3_transform(spatial));
}

/// Move the spatial by the given vector.
static inline void spatial3_move(Spatial3* spatial, vec3 v) {
  spatial->p = vec3_add(spatial->p, v);
}

/// Move the spatial along its forward vector.
static inline void spatial3_move_forwards(Spatial3* spatial, R speed) {
  spatial->p = vec3_add(spatial->p, vec3_scale(spatial->f, speed));
}

/// Move the spatial along its backwards vector.
static inline void spatial3_move_backwards(Spatial3* spatial, R speed) {
  spatial->p = vec3_add(spatial->p, vec3_scale(spatial->f, -speed));
}

/// Move the spatial along its left vector.
static inline void spatial3_move_left(Spatial3* spatial, R speed) {
  spatial->p = vec3_add(spatial->p, vec3_scale(spatial->r, -speed));
}

/// Move the spatial along its right vector.
static inline void spatial3_move_right(Spatial3* spatial, R speed) {
  spatial->p = vec3_add(spatial->p, vec3_scale(spatial->r, speed));
}

/// Move the spatial along the global up vector.
static inline void spatial3_move_up(Spatial3* spatial, R speed) {
  spatial->p = vec3_add(spatial->p, vec3_scale(up3(), speed));
}

/// Move the spatial along the global down vector.
static inline void spatial3_move_down(Spatial3* spatial, R speed) {
  spatial->p = vec3_add(spatial->p, vec3_scale(up3(), -speed));
}

/// Rotate the spatial about the given axis by the given angle.
static inline void spatial3_rotate(Spatial3* spatial, vec3 axis, R angle) {
  mat4 transf = spatial3_transform(spatial);
  const vec3 local_axis =
      vec3_normalize(mat4_mul_vec3(mat4_inverse_transform(transf), axis, 0.0));
  transf = mat4_mul(transf, mat4_rot(local_axis, angle));
  spatial->r = vec3_normalize(mat4_v0(transf));
  spatial->u = vec3_normalize(mat4_v1(transf));
  spatial->f = vec3_normalize(vec3_neg(mat4_v2(transf)));
}

/// Rotate the spatial about its local y axis.
static inline void spatial3_yaw(Spatial3* spatial, const R angle) {
  const R sa = sin(angle);
  const R ca = cos(angle);
  spatial->f = vec3_normalize(
      vec3_sub(vec3_scale(spatial->f, ca), vec3_scale(spatial->r, sa)));
  spatial->r = vec3_normalize(vec3_cross(spatial->f, spatial->u));
}

/// Rotate the spatial about its local x axis.
static inline void spatial3_pitch(Spatial3* spatial, const R angle) {
  const R sa = sin(angle);
  const R ca = cos(angle);
  spatial->f = vec3_normalize(
      vec3_add(vec3_scale(spatial->f, ca), vec3_scale(spatial->u, sa)));
  spatial->u = vec3_normalize(vec3_cross(spatial->r, spatial->f));
}

/// Rotate the spatial about its local z axis.
static inline void spatial3_roll(Spatial3* spatial, const R angle) {
  const R sa = sin(angle);
  const R ca = cos(angle);
  spatial->u = vec3_normalize(
      vec3_sub(vec3_scale(spatial->u, ca), vec3_scale(spatial->r, sa)));
  spatial->r = vec3_normalize(vec3_cross(spatial->f, spatial->u));
}

/// Set the spatial's transformation matrix.
static inline void spatial3_set_transform(Spatial3* spatial, mat4 transform) {
  spatial->r = mat4_v0(transform);
  spatial->u = mat4_v1(transform);
  spatial->f = mat4_v2(transform);
  spatial->p = mat4_v3(transform);
}

static inline void spatial3_set_forward(Spatial3* spatial, vec3 forward) {
  spatial->f = vec3_normalize(forward);
  // Use aux vector to define right vector orthogonal to forward.
  if (vec3_eq(vec3_abs(spatial->f), up3())) {
    spatial->r = vec3_normalize(vec3_cross(spatial->f, forward3()));
  } else {
    spatial->r = vec3_normalize(vec3_cross(spatial->f, up3()));
  }
  spatial->u = vec3_normalize(vec3_cross(spatial->r, spatial->f));
}

/// Make the spatial look at the given target.
static inline void spatial3_lookat(Spatial3* spatial, vec3 target) {
  spatial3_set_forward(spatial, vec3_sub(target, spatial->p));
}

/// Make the spatial look at the given target.
static inline void spatial3_lookat_spatial(Spatial3* spatial,
                                           const Spatial3* target) {
  spatial3_set_forward(spatial, vec3_sub(target->p, spatial->p));
}

/// Make the spatial orbit around the given target.
/// \param target  Target position.
/// \param radius  Radial distance.
/// \param azimuth Azimuthal (horizontal) angle.
/// \param zenith  Polar (vertical) angle.
static inline void spatial3_orbit(Spatial3* spatial, vec3 target, R radius,
                                  R azimuth, R zenith) {
  const R sx = sin(azimuth);
  const R sy = sin(zenith);
  const R cx = cos(azimuth);
  const R cy = cos(zenith);
  spatial->p = (vec3){target.x + radius * cy * sx, target.y + radius * sy,
                      target.z + radius * cx * cy};
}

/// Make the spatial orbit around the given target.
/// \param target  Target spatial.
/// \param radius  Radial distance.
/// \param azimuth Azimuthal (horizontal) angle.
/// \param zenith  Polar (vertical) angle.
static inline void spatial3_orbit_spatial(Spatial3* spatial,
                                          const Spatial3* target, R radius,
                                          R azimuth, R zenith) {
  spatial3_orbit(spatial, target->p, radius, azimuth, zenith);
}

// The functions below are provided so that client code can work with a Spatial3
// with no assumptions on the data type's definition.

/// Return the spatial's position.
static inline vec3 spatial3_position(const Spatial3* spatial) {
  return spatial->p;
}

/// Return the spatial's right vector.
static inline vec3 spatial3_right(const Spatial3* spatial) {
  return spatial->r;
}

/// Return the spatial's up vector.
static inline vec3 spatial3_up(const Spatial3* spatial) { return spatial->u; }

/// Return the spatial's forward vector.
static inline vec3 spatial3_forward(const Spatial3* spatial) {
  return spatial->f;
}

static inline void spatial3_set_position(Spatial3* spatial, vec3 p) {
  spatial->p = p;
}

static inline void spatial3_setx(Spatial3* spatial, R x) { spatial->p.x = x; }

static inline void spatial3_sety(Spatial3* spatial, R y) { spatial->p.y = y; }

static inline void spatial3_setz(Spatial3* spatial, R z) { spatial->p.z = z; }