blob: 37c7221b14a5b2bf74d69939e1c35fbab4390873 [file] [log] [blame]
Romain Guy5d4bae72016-11-08 09:49:25 -08001/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef UI_MAT2_H_
18#define UI_MAT2_H_
19
20#include <ui/TMatHelpers.h>
21#include <ui/vec2.h>
22#include <stdint.h>
23#include <sys/types.h>
24
25#define PURE __attribute__((pure))
26
Romain Guycaf2ca42016-11-10 11:45:58 -080027#if __cplusplus >= 201402L
28#define CONSTEXPR constexpr
29#else
30#define CONSTEXPR
31#endif
32
Romain Guy5d4bae72016-11-08 09:49:25 -080033namespace android {
34// -------------------------------------------------------------------------------------
35namespace details {
36
37/**
38 * A 2x2 column-major matrix class.
39 *
40 * Conceptually a 2x2 matrix is a an array of 2 column vec2:
41 *
42 * mat2 m =
43 * \f$
44 * \left(
45 * \begin{array}{cc}
46 * m[0] & m[1] \\
47 * \end{array}
48 * \right)
49 * \f$
50 * =
51 * \f$
52 * \left(
53 * \begin{array}{cc}
54 * m[0][0] & m[1][0] \\
55 * m[0][1] & m[1][1] \\
56 * \end{array}
57 * \right)
58 * \f$
59 * =
60 * \f$
61 * \left(
62 * \begin{array}{cc}
63 * m(0,0) & m(0,1) \\
64 * m(1,0) & m(1,1) \\
65 * \end{array}
66 * \right)
67 * \f$
68 *
69 * m[n] is the \f$ n^{th} \f$ column of the matrix and is a vec2.
70 *
71 */
72template <typename T>
73class TMat22 : public TVecUnaryOperators<TMat22, T>,
74 public TVecComparisonOperators<TMat22, T>,
75 public TVecAddOperators<TMat22, T>,
76 public TMatProductOperators<TMat22, T>,
77 public TMatSquareFunctions<TMat22, T>,
78 public TMatHelpers<TMat22, T>,
79 public TMatDebug<TMat22, T> {
80public:
81 enum no_init { NO_INIT };
82 typedef T value_type;
83 typedef T& reference;
84 typedef T const& const_reference;
85 typedef size_t size_type;
86 typedef TVec2<T> col_type;
87 typedef TVec2<T> row_type;
88
89 static constexpr size_t COL_SIZE = col_type::SIZE; // size of a column (i.e.: number of rows)
90 static constexpr size_t ROW_SIZE = row_type::SIZE; // size of a row (i.e.: number of columns)
91 static constexpr size_t NUM_ROWS = COL_SIZE;
92 static constexpr size_t NUM_COLS = ROW_SIZE;
93
94private:
95 /*
96 * <-- N columns -->
97 *
98 * a[0][0] a[1][0] a[2][0] ... a[N][0] ^
99 * a[0][1] a[1][1] a[2][1] ... a[N][1] |
100 * a[0][2] a[1][2] a[2][2] ... a[N][2] M rows
101 * ... |
102 * a[0][M] a[1][M] a[2][M] ... a[N][M] v
103 *
104 * COL_SIZE = M
105 * ROW_SIZE = N
106 * m[0] = [ a[0][0] a[0][1] a[0][2] ... a[0][M] ]
107 */
108
109 col_type m_value[NUM_COLS];
110
111public:
112 // array access
113 inline constexpr col_type const& operator[](size_t column) const {
114#if __cplusplus >= 201402L
115 // only possible in C++0x14 with constexpr
116 assert(column < NUM_COLS);
117#endif
118 return m_value[column];
119 }
120
121 inline col_type& operator[](size_t column) {
122 assert(column < NUM_COLS);
123 return m_value[column];
124 }
125
126 // -----------------------------------------------------------------------
127 // we want the compiler generated versions for these...
128 TMat22(const TMat22&) = default;
129 ~TMat22() = default;
130 TMat22& operator = (const TMat22&) = default;
131
132 /**
133 * constructors
134 */
135
136 /**
137 * leaves object uninitialized. use with caution.
138 */
Romain Guycaf2ca42016-11-10 11:45:58 -0800139 explicit constexpr TMat22(no_init)
Romain Guy5d4bae72016-11-08 09:49:25 -0800140 : m_value{ col_type(col_type::NO_INIT),
141 col_type(col_type::NO_INIT) } {}
142
143
144 /**
145 * initialize to identity.
146 *
147 * \f$
148 * \left(
149 * \begin{array}{cc}
150 * 1 & 0 \\
151 * 0 & 1 \\
152 * \end{array}
153 * \right)
154 * \f$
155 */
Romain Guycaf2ca42016-11-10 11:45:58 -0800156 CONSTEXPR TMat22();
Romain Guy5d4bae72016-11-08 09:49:25 -0800157
158 /**
159 * initialize to Identity*scalar.
160 *
161 * \f$
162 * \left(
163 * \begin{array}{cc}
164 * v & 0 \\
165 * 0 & v \\
166 * \end{array}
167 * \right)
168 * \f$
169 */
170 template<typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800171 explicit CONSTEXPR TMat22(U v);
Romain Guy5d4bae72016-11-08 09:49:25 -0800172
173 /**
174 * sets the diagonal to a vector.
175 *
176 * \f$
177 * \left(
178 * \begin{array}{cc}
179 * v[0] & 0 \\
180 * 0 & v[1] \\
181 * \end{array}
182 * \right)
183 * \f$
184 */
185 template <typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800186 explicit CONSTEXPR TMat22(const TVec2<U>& v);
Romain Guy5d4bae72016-11-08 09:49:25 -0800187
188 /**
189 * construct from another matrix of the same size
190 */
191 template <typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800192 explicit CONSTEXPR TMat22(const TMat22<U>& rhs);
Romain Guy5d4bae72016-11-08 09:49:25 -0800193
194 /**
195 * construct from 2 column vectors.
196 *
197 * \f$
198 * \left(
199 * \begin{array}{cc}
200 * v0 & v1 \\
201 * \end{array}
202 * \right)
203 * \f$
204 */
205 template <typename A, typename B>
Romain Guycaf2ca42016-11-10 11:45:58 -0800206 CONSTEXPR TMat22(const TVec2<A>& v0, const TVec2<B>& v1);
Romain Guy5d4bae72016-11-08 09:49:25 -0800207
208 /** construct from 4 elements in column-major form.
209 *
210 * \f$
211 * \left(
212 * \begin{array}{cc}
213 * m[0][0] & m[1][0] \\
214 * m[0][1] & m[1][1] \\
215 * \end{array}
216 * \right)
217 * \f$
218 */
219 template <
220 typename A, typename B,
221 typename C, typename D>
Romain Guycaf2ca42016-11-10 11:45:58 -0800222 CONSTEXPR TMat22(A m00, B m01, C m10, D m11);
Romain Guy5d4bae72016-11-08 09:49:25 -0800223
224 /**
225 * construct from a C array in column major form.
226 */
227 template <typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800228 explicit CONSTEXPR TMat22(U const* rawArray);
Romain Guy5d4bae72016-11-08 09:49:25 -0800229
230 /**
231 * Rotate by radians in the 2D plane
232 */
Romain Guycaf2ca42016-11-10 11:45:58 -0800233 static CONSTEXPR TMat22<T> rotate(T radian) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800234 TMat22<T> r(TMat22<T>::NO_INIT);
235 T c = std::cos(radian);
236 T s = std::sin(radian);
237 r[0][0] = c; r[1][1] = c;
238 r[0][1] = s; r[1][0] = -s;
239 return r;
240 }
241};
242
243// ----------------------------------------------------------------------------------------
244// Constructors
245// ----------------------------------------------------------------------------------------
246
247// Since the matrix code could become pretty big quickly, we don't inline most
248// operations.
249
250template <typename T>
Romain Guycaf2ca42016-11-10 11:45:58 -0800251CONSTEXPR TMat22<T>::TMat22() {
Romain Guy5d4bae72016-11-08 09:49:25 -0800252 m_value[0] = col_type(1, 0);
253 m_value[1] = col_type(0, 1);
254}
255
256template <typename T>
257template <typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800258CONSTEXPR TMat22<T>::TMat22(U v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800259 m_value[0] = col_type(v, 0);
260 m_value[1] = col_type(0, v);
261}
262
263template<typename T>
264template<typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800265CONSTEXPR TMat22<T>::TMat22(const TVec2<U>& v) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800266 m_value[0] = col_type(v.x, 0);
267 m_value[1] = col_type(0, v.y);
268}
269
270// construct from 4 scalars. Note that the arrangement
271// of values in the constructor is the transpose of the matrix
272// notation.
273template<typename T>
274template <
275 typename A, typename B,
276 typename C, typename D>
Romain Guycaf2ca42016-11-10 11:45:58 -0800277CONSTEXPR TMat22<T>::TMat22( A m00, B m01, C m10, D m11) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800278 m_value[0] = col_type(m00, m01);
279 m_value[1] = col_type(m10, m11);
280}
281
282template <typename T>
283template <typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800284CONSTEXPR TMat22<T>::TMat22(const TMat22<U>& rhs) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800285 for (size_t col = 0; col < NUM_COLS; ++col) {
286 m_value[col] = col_type(rhs[col]);
287 }
288}
289
290// Construct from 2 column vectors.
291template <typename T>
292template <typename A, typename B>
Romain Guycaf2ca42016-11-10 11:45:58 -0800293CONSTEXPR TMat22<T>::TMat22(const TVec2<A>& v0, const TVec2<B>& v1) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800294 m_value[0] = v0;
295 m_value[1] = v1;
296}
297
298// Construct from raw array, in column-major form.
299template <typename T>
300template <typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800301CONSTEXPR TMat22<T>::TMat22(U const* rawArray) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800302 for (size_t col = 0; col < NUM_COLS; ++col) {
303 for (size_t row = 0; row < NUM_ROWS; ++row) {
304 m_value[col][row] = *rawArray++;
305 }
306 }
307}
308
309// ----------------------------------------------------------------------------------------
310// Arithmetic operators outside of class
311// ----------------------------------------------------------------------------------------
312
313/* We use non-friend functions here to prevent the compiler from using
314 * implicit conversions, for instance of a scalar to a vector. The result would
315 * not be what the caller expects.
316 *
317 * Also note that the order of the arguments in the inner loop is important since
318 * it determines the output type (only relevant when T != U).
319 */
320
321// matrix * column-vector, result is a vector of the same type than the input vector
322template <typename T, typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800323CONSTEXPR typename TMat22<U>::col_type PURE operator *(const TMat22<T>& lhs, const TVec2<U>& rhs) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800324 // Result is initialized to zero.
325 typename TMat22<U>::col_type result;
326 for (size_t col = 0; col < TMat22<T>::NUM_COLS; ++col) {
327 result += lhs[col] * rhs[col];
328 }
329 return result;
330}
331
332// row-vector * matrix, result is a vector of the same type than the input vector
333template <typename T, typename U>
Romain Guycaf2ca42016-11-10 11:45:58 -0800334CONSTEXPR typename TMat22<U>::row_type PURE operator *(const TVec2<U>& lhs, const TMat22<T>& rhs) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800335 typename TMat22<U>::row_type result(TMat22<U>::row_type::NO_INIT);
336 for (size_t col = 0; col < TMat22<T>::NUM_COLS; ++col) {
337 result[col] = dot(lhs, rhs[col]);
338 }
339 return result;
340}
341
342// matrix * scalar, result is a matrix of the same type than the input matrix
343template<typename T, typename U>
344constexpr typename std::enable_if<std::is_arithmetic<U>::value, TMat22<T>>::type PURE
345operator*(TMat22<T> lhs, U rhs) {
346 return lhs *= rhs;
347}
348
349// scalar * matrix, result is a matrix of the same type than the input matrix
350template<typename T, typename U>
351constexpr typename std::enable_if<std::is_arithmetic<U>::value, TMat22<T>>::type PURE
352operator*(U lhs, const TMat22<T>& rhs) {
353 return rhs * lhs;
354}
355
356// ----------------------------------------------------------------------------------------
357
358/* FIXME: this should go into TMatSquareFunctions<> but for some reason
359 * BASE<T>::col_type is not accessible from there (???)
360 */
361template<typename T>
Romain Guycaf2ca42016-11-10 11:45:58 -0800362CONSTEXPR typename TMat22<T>::col_type PURE diag(const TMat22<T>& m) {
Romain Guy5d4bae72016-11-08 09:49:25 -0800363 return matrix::diag(m);
364}
365
366} // namespace details
367
368// ----------------------------------------------------------------------------------------
369
370typedef details::TMat22<double> mat2d;
371typedef details::TMat22<float> mat2;
372typedef details::TMat22<float> mat2f;
373
374// ----------------------------------------------------------------------------------------
375} // namespace android
376
377#undef PURE
Romain Guycaf2ca42016-11-10 11:45:58 -0800378#undef CONSTEXPR
Romain Guy5d4bae72016-11-08 09:49:25 -0800379
380#endif // UI_MAT2_H_