Euphoria
mat2.cc
Go to the documentation of this file.
1 #include "base/mat2.h"
2 
3 
4 
5 namespace eu
6 {
7  [[nodiscard]] mat2f
9  (
10  float t00, float t01,
11  float t10, float t11
12  )
13  {
14  return {t00, t01, t10, t11};
15  }
16 
17 
18  [[nodiscard]] mat2f
20  {
21  return from_row_major
22  (
23  k, 0,
24  0, 1
25  );
26  }
27 
28 
29  [[nodiscard]] mat2f
31  {
32  return from_row_major
33  (
34  1, 0,
35  0, k
36  );
37  }
38 
39  [[nodiscard]] mat2f
41  {
42  const auto s = sin(a);
43  const auto c = cos(a);
44  return from_row_major
45  (
46  c, s,
47  -s, c
48  );
49  }
50 
51 
52  void
54  {
55 #define OP(i) data[i] += rhs.data[i]
56  OP(0); OP(1);
57  OP(2); OP(3);
58 #undef OP
59  }
60 
61 
62  void
64  {
65 #define OP(i) data[i] -= rhs.data[i]
66  OP(0); OP(1);
67  OP(2); OP(3);
68 #undef OP
69  }
70 
71 
72  const float*
74  {
75  return data;
76  }
77 
78 
79  float*
81  {
82  return data;
83  }
84 
85 
86  float
87  mat2f::get(int row, int col) const
88  {
89  return data[col * 2 + row];
90  }
91 
92 
93  vec2f
94  mat2f::get_column(int c) const
95  {
96  return {get(0, c), get(1, c)};
97  }
98 
99 
100  vec2f
101  mat2f::get_row(int r) const
102  {
103  return {get(r, 0), get(r, 1)};
104  }
105 
106 
107  std::string to_string(const mat2f& m)
108  {
109  return fmt::format("({}, {})", m.get_row(0), m.get_row(1));
110  }
111 
112 
113  mat2f
114  operator+(const mat2f& lhs, const mat2f& rhs)
115  {
116  mat2f t = lhs;
117  t += rhs;
118  return t;
119  }
120 
121 
122  mat2f
123  operator-(const mat2f& lhs, const mat2f& rhs)
124  {
125  mat2f t = lhs;
126  t -= rhs;
127  return t;
128  }
129 
130  float
131  get_component_multiply_sum(const vec2f& lhs, const vec2f& rhs)
132  {
133  const auto x = lhs.x * rhs.x;
134  const auto y = lhs.y * rhs.y;
135  return x + y;
136  }
137 
138  mat2f operator*(const mat2f& lhs, const mat2f& rhs)
139  {
140 #define OP(r, c) \
141  get_component_multiply_sum(lhs.get_row(r), rhs.get_column(c))
142  return mat2f::from_row_major
143  (
144  OP(0, 0), OP(0, 1),
145  OP(1, 0), OP(1, 1)
146  );
147 #undef OP
148  }
149 
150 
151  vec2f operator*(const mat2f& lhs, const vec2f& rhs)
152  {
153 #define OP(r) get_component_multiply_sum(lhs.get_row(r), rhs)
154  return {OP(0), OP(1)};
155 #undef OP
156  }
157 
158 
159 }
160 
161 
#define OP(i)
Definition: assert.h:90
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
float cos(const Angle &ang)
Definition: angle.cc:68
float sin(const Angle &ang)
Definition: angle.cc:61
Angle operator*(const Angle &lhs, float rhs)
Definition: angle.cc:149
float get_component_multiply_sum(const vec2f &lhs, const vec2f &rhs)
Definition: mat2.cc:131
Definition: mat2.h:10
static mat2f from_stretch_y(float k)
Definition: mat2.cc:30
static mat2f from_stretch_x(float k)
Definition: mat2.cc:19
void operator-=(const mat2f &rhs)
Definition: mat2.cc:63
static mat2f from_rotation(const Angle &a)
Definition: mat2.cc:40
vec2f get_column(int c) const
Definition: mat2.cc:94
vec2f get_row(int r) const
Definition: mat2.cc:101
float get(int row, int col) const
Definition: mat2.cc:87
void operator+=(const mat2f &rhs)
Definition: mat2.cc:53
static mat2f from_col_major(float t00, float t01, float t10, float t11)
Definition: mat2.cc:9
constexpr static mat2f from_row_major(float t00, float t10, float t01, float t11)
Definition: mat2.h:20
float * get_data_ptr()
Definition: mat2.cc:80
Definition: vec2.h:33
float x
Definition: vec2.h:34
float y
Definition: vec2.h:35