Euphoria
mat4.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "base/vec3.h"
4 #include "base/vec4.h"
5 #include "base/angle.h"
6 #include "base/axisangle.h"
7 #include "base/mat3.h"
8 
9 
10 
11 namespace eu
12 {
13  struct mat4f
14  {
15  [[nodiscard]] static mat4f from_col_major
16  (
17  float t00, float t01, float t02, float t03,
18  float t10, float t11, float t12, float t13,
19  float t20, float t21, float t22, float t23,
20  float t30, float t31, float t32, float t33
21  );
22 
23  [[nodiscard]] constexpr static mat4f from_row_major
24  (
25  float t00, float t10, float t20, float t30,
26  float t01, float t11, float t21, float t31,
27  float t02, float t12, float t22, float t32,
28  float t03, float t13, float t23, float t33
29  )
30  {
31  return
32  {
33  t00, t01, t02, t03,
34  t10, t11, t12, t13,
35  t20, t21, t22, t23,
36  t30, t31, t32, t33
37  };
38  }
39 
40  [[nodiscard]] static mat4f from_major(const vec4f& major);
41  [[nodiscard]] static mat4f from_scale(const Scale3f& scale);
42  [[nodiscard]] static mat4f from_translation(const vec3f& v);
43  [[nodiscard]] static mat4f from_rot_x(const Angle& a);
44  [[nodiscard]] static mat4f from_rot_y(const Angle& a);
45  [[nodiscard]] static mat4f from_rot_z(const Angle& a);
46  [[nodiscard]] static mat4f from_axis_angle(const AxisAngle& aa);
47  [[nodiscard]] static mat4f create_ortho(float l, float r, float b, float t, float n, float f);
48  [[nodiscard]] static mat4f create_perspective(const Angle& fov, float a, float near, float far);
49  [[nodiscard]] constexpr static mat4f from_scalar(float scalar)
50  {
51  const float z = 0;
52  return from_row_major
53  (
54  scalar, z, z, z,
55  z, scalar, z, z,
56  z, z, scalar, z,
57  z, z, z, scalar
58  );
59  }
60 
61  float* get_data_ptr();
62  float* get_column_major();
63 
64  bool invert();
65 
66  [[nodiscard]] float get(int row, int col) const;
67  [[nodiscard]] vec4f get_transform(const vec4f& p) const;
68  [[nodiscard]] vec3f get_transform(const vec3f& p, float w) const;
69  [[nodiscard]] vec3f get_transform_point(const vec3f& p) const;
70  [[nodiscard]] vec3f get_transform_vec(const vec3f& p) const;
71  [[nodiscard]] unit3f get_transform_vec(const unit3f& p) const;
72  [[nodiscard]] unit3f get_axis(int col) const;
73  [[nodiscard]] mat4f get_translated(const vec3f& t) const;
74  [[nodiscard]] mat4f get_rotated(const AxisAngle& aa) const;
75  [[nodiscard]] mat4f get_scaled(const Scale3f& scale) const;
76  [[nodiscard]] vec4f get_column(int c) const;
77  [[nodiscard]] vec4f get_row(int r) const;
78 
79  [[nodiscard]] const float* get_column_major() const;
80  [[nodiscard]] const float* get_data_ptr() const;
81 
82  [[nodiscard]] vec3f get_translation() const;
83  [[nodiscard]] vec4f get_major() const;
84  [[nodiscard]] unit3f get_x_axis() const;
85  [[nodiscard]] unit3f get_y_axis() const;
86  [[nodiscard]] unit3f get_z_axis() const;
87  [[nodiscard]] mat4f get_transposed() const;
88  [[nodiscard]] mat4f get_inverted() const;
89  [[nodiscard]] mat3f get_mat3() const;
90 
91  void operator+=(const mat4f& rhs);
92  void operator-=(const mat4f& rhs);
93 
94  private:
95  float data[16]; // col-major
96 
97  mat4f() = default;
98 
99  constexpr mat4f
100  (
101  float t00, float t01, float t02, float t03,
102  float t10, float t11, float t12, float t13,
103  float t20, float t21, float t22, float t23,
104  float t30, float t31, float t32, float t33
105  )
106  : data
107  {
108  t00, t01, t02, t03,
109  t10, t11, t12, t13,
110  t20, t21, t22, t23,
111  t30, t31, t32, t33
112  }
113  {
114  }
115  };
116 
117 
119 
120 
121  std::string to_string(const mat4f& m);
122 
123  mat4f operator+(const mat4f& lhs, const mat4f& rhs);
124  mat4f operator-(const mat4f& lhs, const mat4f& rhs);
125  mat4f operator*(const mat4f& lhs, const mat4f& rhs);
126  vec4f operator*(const mat4f& lhs, const vec4f& rhs);
127 }
128 
ADD_DEFAULT_FORMATTER(eu::mat4f, std::string, eu::to_string)
Definition: assert.h:90
constexpr mat4f m4_identity
Definition: mat4.h:118
Angle operator+(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:123
Angle operator-(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:132
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
Angle operator*(const Angle &lhs, float rhs)
Definition: angle.cc:149
Definition: mat3.h:12
Definition: mat4.h:14
static mat4f from_axis_angle(const AxisAngle &aa)
Definition: mat4.cc:146
mat4f get_translated(const vec3f &t) const
Definition: mat4.cc:421
float * get_data_ptr()
Definition: mat4.cc:445
constexpr static mat4f from_scalar(float scalar)
Definition: mat4.h:49
vec3f get_transform_point(const vec3f &p) const
Definition: mat4.cc:77
void operator+=(const mat4f &rhs)
Definition: mat4.cc:369
unit3f get_z_axis() const
Definition: mat4.cc:205
float * get_column_major()
Definition: mat4.cc:6
mat4f get_rotated(const AxisAngle &aa) const
Definition: mat4.cc:427
mat4f get_scaled(const Scale3f &scale) const
Definition: mat4.cc:433
unit3f get_axis(int col) const
Definition: mat4.cc:187
float get(int row, int col) const
Definition: mat4.cc:452
static mat4f from_major(const vec4f &major)
Definition: mat4.cc:30
static mat4f from_rot_y(const Angle &a)
Definition: mat4.cc:116
static mat4f from_rot_x(const Angle &a)
Definition: mat4.cc:102
static mat4f from_col_major(float t00, float t01, float t02, float t03, float t10, float t11, float t12, float t13, float t20, float t21, float t22, float t23, float t30, float t31, float t32, float t33)
Definition: mat4.cc:12
static mat4f from_scale(const Scale3f &scale)
Definition: mat4.cc:44
mat4f get_inverted() const
Definition: mat4.cc:349
void operator-=(const mat4f &rhs)
Definition: mat4.cc:380
mat3f get_mat3() const
Definition: mat4.cc:358
bool invert()
Definition: mat4.cc:224
vec3f get_transform_vec(const vec3f &p) const
Definition: mat4.cc:83
static mat4f create_perspective(const Angle &fov, float a, float near, float far)
Definition: mat4.cc:406
constexpr static mat4f from_row_major(float t00, float t10, float t20, float t30, float t01, float t11, float t21, float t31, float t02, float t12, float t22, float t32, float t03, float t13, float t23, float t33)
Definition: mat4.h:24
unit3f get_x_axis() const
Definition: mat4.cc:193
vec4f get_column(int c) const
Definition: mat4.cc:458
vec3f get_translation() const
Definition: mat4.cc:95
static mat4f create_ortho(float l, float r, float b, float t, float n, float f)
Definition: mat4.cc:392
unit3f get_y_axis() const
Definition: mat4.cc:199
vec4f get_major() const
Definition: mat4.cc:181
static mat4f from_translation(const vec3f &v)
Definition: mat4.cc:51
mat4f get_transposed() const
Definition: mat4.cc:211
static mat4f from_rot_z(const Angle &a)
Definition: mat4.cc:131
vec4f get_row(int r) const
Definition: mat4.cc:471
vec4f get_transform(const vec4f &p) const
Definition: mat4.cc:65
Definition: vec3.h:48
Definition: vec4.h:14