Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 1 | /* |
| 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_MAT3_H_ |
| 18 | #define UI_MAT3_H_ |
| 19 | |
| 20 | #include <ui/quat.h> |
| 21 | #include <ui/TMatHelpers.h> |
| 22 | #include <ui/vec3.h> |
| 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | #define PURE __attribute__((pure)) |
| 27 | |
| 28 | namespace android { |
| 29 | // ------------------------------------------------------------------------------------- |
| 30 | namespace details { |
| 31 | |
| 32 | template<typename T> |
| 33 | class TQuaternion; |
| 34 | |
| 35 | /** |
| 36 | * A 3x3 column-major matrix class. |
| 37 | * |
| 38 | * Conceptually a 3x3 matrix is a an array of 3 column vec3: |
| 39 | * |
| 40 | * mat3 m = |
| 41 | * \f$ |
| 42 | * \left( |
| 43 | * \begin{array}{ccc} |
| 44 | * m[0] & m[1] & m[2] \\ |
| 45 | * \end{array} |
| 46 | * \right) |
| 47 | * \f$ |
| 48 | * = |
| 49 | * \f$ |
| 50 | * \left( |
| 51 | * \begin{array}{ccc} |
| 52 | * m[0][0] & m[1][0] & m[2][0] \\ |
| 53 | * m[0][1] & m[1][1] & m[2][1] \\ |
| 54 | * m[0][2] & m[1][2] & m[2][2] \\ |
| 55 | * \end{array} |
| 56 | * \right) |
| 57 | * \f$ |
| 58 | * = |
| 59 | * \f$ |
| 60 | * \left( |
| 61 | * \begin{array}{ccc} |
| 62 | * m(0,0) & m(0,1) & m(0,2) \\ |
| 63 | * m(1,0) & m(1,1) & m(1,2) \\ |
| 64 | * m(2,0) & m(2,1) & m(2,2) \\ |
| 65 | * \end{array} |
| 66 | * \right) |
| 67 | * \f$ |
| 68 | * |
| 69 | * m[n] is the \f$ n^{th} \f$ column of the matrix and is a vec3. |
| 70 | * |
| 71 | */ |
| 72 | template <typename T> |
| 73 | class TMat33 : public TVecUnaryOperators<TMat33, T>, |
| 74 | public TVecComparisonOperators<TMat33, T>, |
| 75 | public TVecAddOperators<TMat33, T>, |
| 76 | public TMatProductOperators<TMat33, T>, |
| 77 | public TMatSquareFunctions<TMat33, T>, |
| 78 | public TMatTransform<TMat33, T>, |
| 79 | public TMatHelpers<TMat33, T>, |
| 80 | public TMatDebug<TMat33, T> { |
| 81 | public: |
| 82 | enum no_init { NO_INIT }; |
| 83 | typedef T value_type; |
| 84 | typedef T& reference; |
| 85 | typedef T const& const_reference; |
| 86 | typedef size_t size_type; |
| 87 | typedef TVec3<T> col_type; |
| 88 | typedef TVec3<T> row_type; |
| 89 | |
| 90 | static constexpr size_t COL_SIZE = col_type::SIZE; // size of a column (i.e.: number of rows) |
| 91 | static constexpr size_t ROW_SIZE = row_type::SIZE; // size of a row (i.e.: number of columns) |
| 92 | static constexpr size_t NUM_ROWS = COL_SIZE; |
| 93 | static constexpr size_t NUM_COLS = ROW_SIZE; |
| 94 | |
| 95 | private: |
| 96 | /* |
| 97 | * <-- N columns --> |
| 98 | * |
| 99 | * a[0][0] a[1][0] a[2][0] ... a[N][0] ^ |
| 100 | * a[0][1] a[1][1] a[2][1] ... a[N][1] | |
| 101 | * a[0][2] a[1][2] a[2][2] ... a[N][2] M rows |
| 102 | * ... | |
| 103 | * a[0][M] a[1][M] a[2][M] ... a[N][M] v |
| 104 | * |
| 105 | * COL_SIZE = M |
| 106 | * ROW_SIZE = N |
| 107 | * m[0] = [ a[0][0] a[0][1] a[0][2] ... a[0][M] ] |
| 108 | */ |
| 109 | |
| 110 | col_type m_value[NUM_COLS]; |
| 111 | |
| 112 | public: |
| 113 | // array access |
| 114 | inline constexpr col_type const& operator[](size_t column) const { |
| 115 | #if __cplusplus >= 201402L |
| 116 | // only possible in C++0x14 with constexpr |
| 117 | assert(column < NUM_COLS); |
| 118 | #endif |
| 119 | return m_value[column]; |
| 120 | } |
| 121 | |
| 122 | inline col_type& operator[](size_t column) { |
| 123 | assert(column < NUM_COLS); |
| 124 | return m_value[column]; |
| 125 | } |
| 126 | |
| 127 | // ----------------------------------------------------------------------- |
| 128 | // we want the compiler generated versions for these... |
| 129 | TMat33(const TMat33&) = default; |
| 130 | ~TMat33() = default; |
| 131 | TMat33& operator = (const TMat33&) = default; |
| 132 | |
| 133 | /** |
| 134 | * constructors |
| 135 | */ |
| 136 | |
| 137 | /** |
| 138 | * leaves object uninitialized. use with caution. |
| 139 | */ |
| 140 | explicit |
| 141 | constexpr TMat33(no_init) |
| 142 | : m_value{ col_type(col_type::NO_INIT), |
| 143 | col_type(col_type::NO_INIT), |
| 144 | col_type(col_type::NO_INIT) } {} |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * initialize to identity. |
| 149 | * |
| 150 | * \f$ |
| 151 | * \left( |
| 152 | * \begin{array}{ccc} |
| 153 | * 1 & 0 & 0 \\ |
| 154 | * 0 & 1 & 0 \\ |
| 155 | * 0 & 0 & 1 \\ |
| 156 | * \end{array} |
| 157 | * \right) |
| 158 | * \f$ |
| 159 | */ |
| 160 | TMat33(); |
| 161 | |
| 162 | /** |
| 163 | * initialize to Identity*scalar. |
| 164 | * |
| 165 | * \f$ |
| 166 | * \left( |
| 167 | * \begin{array}{ccc} |
| 168 | * v & 0 & 0 \\ |
| 169 | * 0 & v & 0 \\ |
| 170 | * 0 & 0 & v \\ |
| 171 | * \end{array} |
| 172 | * \right) |
| 173 | * \f$ |
| 174 | */ |
| 175 | template<typename U> |
| 176 | explicit TMat33(U v); |
| 177 | |
| 178 | /** |
| 179 | * sets the diagonal to a vector. |
| 180 | * |
| 181 | * \f$ |
| 182 | * \left( |
| 183 | * \begin{array}{ccc} |
| 184 | * v[0] & 0 & 0 \\ |
| 185 | * 0 & v[1] & 0 \\ |
| 186 | * 0 & 0 & v[2] \\ |
| 187 | * \end{array} |
| 188 | * \right) |
| 189 | * \f$ |
| 190 | */ |
| 191 | template <typename U> |
| 192 | explicit TMat33(const TVec3<U>& v); |
| 193 | |
| 194 | /** |
| 195 | * construct from another matrix of the same size |
| 196 | */ |
| 197 | template <typename U> |
| 198 | explicit TMat33(const TMat33<U>& rhs); |
| 199 | |
| 200 | /** |
| 201 | * construct from 3 column vectors. |
| 202 | * |
| 203 | * \f$ |
| 204 | * \left( |
| 205 | * \begin{array}{ccc} |
| 206 | * v0 & v1 & v2 \\ |
| 207 | * \end{array} |
| 208 | * \right) |
| 209 | * \f$ |
| 210 | */ |
| 211 | template <typename A, typename B, typename C> |
| 212 | TMat33(const TVec3<A>& v0, const TVec3<B>& v1, const TVec3<C>& v2); |
| 213 | |
| 214 | /** construct from 9 elements in column-major form. |
| 215 | * |
| 216 | * \f$ |
| 217 | * \left( |
| 218 | * \begin{array}{ccc} |
| 219 | * m[0][0] & m[1][0] & m[2][0] \\ |
| 220 | * m[0][1] & m[1][1] & m[2][1] \\ |
| 221 | * m[0][2] & m[1][2] & m[2][2] \\ |
| 222 | * \end{array} |
| 223 | * \right) |
| 224 | * \f$ |
| 225 | */ |
| 226 | template < |
| 227 | typename A, typename B, typename C, |
| 228 | typename D, typename E, typename F, |
| 229 | typename G, typename H, typename I> |
| 230 | TMat33(A m00, B m01, C m02, |
| 231 | D m10, E m11, F m12, |
| 232 | G m20, H m21, I m22); |
| 233 | |
| 234 | /** |
| 235 | * construct from a quaternion |
| 236 | */ |
| 237 | template <typename U> |
| 238 | explicit TMat33(const TQuaternion<U>& q); |
| 239 | |
| 240 | /** |
| 241 | * construct from a C array in column major form. |
| 242 | */ |
| 243 | template <typename U> |
| 244 | explicit TMat33(U const* rawArray); |
| 245 | |
| 246 | /** |
| 247 | * orthogonalize only works on matrices of size 3x3 |
| 248 | */ |
| 249 | friend inline |
| 250 | TMat33 orthogonalize(const TMat33& m) { |
| 251 | TMat33 ret(TMat33::NO_INIT); |
| 252 | ret[0] = normalize(m[0]); |
| 253 | ret[2] = normalize(cross(ret[0], m[1])); |
| 254 | ret[1] = normalize(cross(ret[2], ret[0])); |
| 255 | return ret; |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | // ---------------------------------------------------------------------------------------- |
| 260 | // Constructors |
| 261 | // ---------------------------------------------------------------------------------------- |
| 262 | |
| 263 | // Since the matrix code could become pretty big quickly, we don't inline most |
| 264 | // operations. |
| 265 | |
| 266 | template <typename T> |
| 267 | TMat33<T>::TMat33() { |
| 268 | m_value[0] = col_type(1, 0, 0); |
| 269 | m_value[1] = col_type(0, 1, 0); |
| 270 | m_value[2] = col_type(0, 0, 1); |
| 271 | } |
| 272 | |
| 273 | template <typename T> |
| 274 | template <typename U> |
| 275 | TMat33<T>::TMat33(U v) { |
| 276 | m_value[0] = col_type(v, 0, 0); |
| 277 | m_value[1] = col_type(0, v, 0); |
| 278 | m_value[2] = col_type(0, 0, v); |
| 279 | } |
| 280 | |
| 281 | template<typename T> |
| 282 | template<typename U> |
| 283 | TMat33<T>::TMat33(const TVec3<U>& v) { |
| 284 | m_value[0] = col_type(v.x, 0, 0); |
| 285 | m_value[1] = col_type(0, v.y, 0); |
| 286 | m_value[2] = col_type(0, 0, v.z); |
| 287 | } |
| 288 | |
| 289 | // construct from 16 scalars. Note that the arrangement |
| 290 | // of values in the constructor is the transpose of the matrix |
| 291 | // notation. |
| 292 | template<typename T> |
| 293 | template < |
| 294 | typename A, typename B, typename C, |
| 295 | typename D, typename E, typename F, |
| 296 | typename G, typename H, typename I> |
| 297 | TMat33<T>::TMat33(A m00, B m01, C m02, |
| 298 | D m10, E m11, F m12, |
| 299 | G m20, H m21, I m22) { |
| 300 | m_value[0] = col_type(m00, m01, m02); |
| 301 | m_value[1] = col_type(m10, m11, m12); |
| 302 | m_value[2] = col_type(m20, m21, m22); |
| 303 | } |
| 304 | |
| 305 | template <typename T> |
| 306 | template <typename U> |
| 307 | TMat33<T>::TMat33(const TMat33<U>& rhs) { |
| 308 | for (size_t col = 0; col < NUM_COLS; ++col) { |
| 309 | m_value[col] = col_type(rhs[col]); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Construct from 3 column vectors. |
| 314 | template <typename T> |
| 315 | template <typename A, typename B, typename C> |
| 316 | TMat33<T>::TMat33(const TVec3<A>& v0, const TVec3<B>& v1, const TVec3<C>& v2) { |
| 317 | m_value[0] = v0; |
| 318 | m_value[1] = v1; |
| 319 | m_value[2] = v2; |
| 320 | } |
| 321 | |
| 322 | // Construct from raw array, in column-major form. |
| 323 | template <typename T> |
| 324 | template <typename U> |
| 325 | TMat33<T>::TMat33(U const* rawArray) { |
| 326 | for (size_t col = 0; col < NUM_COLS; ++col) { |
| 327 | for (size_t row = 0; row < NUM_ROWS; ++row) { |
| 328 | m_value[col][row] = *rawArray++; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | template <typename T> |
| 334 | template <typename U> |
| 335 | TMat33<T>::TMat33(const TQuaternion<U>& q) { |
| 336 | const U n = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w; |
| 337 | const U s = n > 0 ? 2/n : 0; |
| 338 | const U x = s*q.x; |
| 339 | const U y = s*q.y; |
| 340 | const U z = s*q.z; |
| 341 | const U xx = x*q.x; |
| 342 | const U xy = x*q.y; |
| 343 | const U xz = x*q.z; |
| 344 | const U xw = x*q.w; |
| 345 | const U yy = y*q.y; |
| 346 | const U yz = y*q.z; |
| 347 | const U yw = y*q.w; |
| 348 | const U zz = z*q.z; |
| 349 | const U zw = z*q.w; |
| 350 | m_value[0] = col_type(1-yy-zz, xy+zw, xz-yw); // NOLINT |
| 351 | m_value[1] = col_type( xy-zw, 1-xx-zz, yz+xw); // NOLINT |
| 352 | m_value[2] = col_type( xz+yw, yz-xw, 1-xx-yy); // NOLINT |
| 353 | } |
| 354 | |
| 355 | // ---------------------------------------------------------------------------------------- |
| 356 | // Arithmetic operators outside of class |
| 357 | // ---------------------------------------------------------------------------------------- |
| 358 | |
| 359 | /* We use non-friend functions here to prevent the compiler from using |
| 360 | * implicit conversions, for instance of a scalar to a vector. The result would |
| 361 | * not be what the caller expects. |
| 362 | * |
| 363 | * Also note that the order of the arguments in the inner loop is important since |
| 364 | * it determines the output type (only relevant when T != U). |
| 365 | */ |
| 366 | |
| 367 | // matrix * column-vector, result is a vector of the same type than the input vector |
| 368 | template <typename T, typename U> |
| 369 | typename TMat33<U>::col_type PURE operator *(const TMat33<T>& lhs, const TVec3<U>& rhs) { |
| 370 | // Result is initialized to zero. |
| 371 | typename TMat33<U>::col_type result; |
| 372 | for (size_t col = 0; col < TMat33<T>::NUM_COLS; ++col) { |
| 373 | result += lhs[col] * rhs[col]; |
| 374 | } |
| 375 | return result; |
| 376 | } |
| 377 | |
| 378 | // row-vector * matrix, result is a vector of the same type than the input vector |
| 379 | template <typename T, typename U> |
| 380 | typename TMat33<U>::row_type PURE operator *(const TVec3<U>& lhs, const TMat33<T>& rhs) { |
| 381 | typename TMat33<U>::row_type result(TMat33<U>::row_type::NO_INIT); |
| 382 | for (size_t col = 0; col < TMat33<T>::NUM_COLS; ++col) { |
| 383 | result[col] = dot(lhs, rhs[col]); |
| 384 | } |
| 385 | return result; |
| 386 | } |
| 387 | |
| 388 | // matrix * scalar, result is a matrix of the same type than the input matrix |
| 389 | template<typename T, typename U> |
| 390 | constexpr typename std::enable_if<std::is_arithmetic<U>::value, TMat33<T>>::type PURE |
| 391 | operator*(TMat33<T> lhs, U rhs) { |
| 392 | return lhs *= rhs; |
| 393 | } |
| 394 | |
| 395 | // scalar * matrix, result is a matrix of the same type than the input matrix |
| 396 | template<typename T, typename U> |
| 397 | constexpr typename std::enable_if<std::is_arithmetic<U>::value, TMat33<T>>::type PURE |
| 398 | operator*(U lhs, const TMat33<T>& rhs) { |
| 399 | return rhs * lhs; |
| 400 | } |
| 401 | |
| 402 | //------------------------------------------------------------------------------ |
| 403 | template <typename T> |
| 404 | TMat33<T> orthogonalize(const TMat33<T>& m) { |
| 405 | TMat33<T> ret(TMat33<T>::NO_INIT); |
| 406 | ret[0] = normalize(m[0]); |
| 407 | ret[2] = normalize(cross(ret[0], m[1])); |
| 408 | ret[1] = normalize(cross(ret[2], ret[0])); |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | // ---------------------------------------------------------------------------------------- |
| 413 | |
| 414 | /* FIXME: this should go into TMatSquareFunctions<> but for some reason |
| 415 | * BASE<T>::col_type is not accessible from there (???) |
| 416 | */ |
| 417 | template<typename T> |
| 418 | typename TMat33<T>::col_type PURE diag(const TMat33<T>& m) { |
| 419 | return matrix::diag(m); |
| 420 | } |
| 421 | |
| 422 | } // namespace details |
| 423 | |
| 424 | // ---------------------------------------------------------------------------------------- |
| 425 | |
| 426 | typedef details::TMat33<double> mat3d; |
| 427 | typedef details::TMat33<float> mat3; |
| 428 | typedef details::TMat33<float> mat3f; |
| 429 | |
| 430 | // ---------------------------------------------------------------------------------------- |
| 431 | } // namespace android |
| 432 | |
| 433 | #undef PURE |
| 434 | |
| 435 | #endif // UI_MAT3_H_ |