/* * Copyright 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef TMAT_IMPLEMENTATION #error "Don't include TMatHelpers.h directly. use ui/mat*.h instead" #else #undef TMAT_IMPLEMENTATION #endif #ifndef UI_TMAT_HELPERS_H #define UI_TMAT_HELPERS_H #include #include #include #include #include #define PURE __attribute__((pure)) namespace android { // ------------------------------------------------------------------------------------- /* * No user serviceable parts here. * * Don't use this file directly, instead include ui/mat*.h */ /* * Matrix utilities */ namespace matrix { inline int PURE transpose(int v) { return v; } inline float PURE transpose(float v) { return v; } inline double PURE transpose(double v) { return v; } inline int PURE trace(int v) { return v; } inline float PURE trace(float v) { return v; } inline double PURE trace(double v) { return v; } template MATRIX PURE inverse(const MATRIX& src) { COMPILE_TIME_ASSERT_FUNCTION_SCOPE( MATRIX::COL_SIZE == MATRIX::ROW_SIZE ); typename MATRIX::value_type t; const size_t N = MATRIX::col_size(); size_t swap; MATRIX tmp(src); MATRIX inverse(1); for (size_t i=0 ; i fabs(tmp[i][i])) { swap = j; } } if (swap != i) { /* swap rows. */ for (size_t k=0 ; k MATRIX_R PURE multiply(const MATRIX_A& lhs, const MATRIX_B& rhs) { // pre-requisite: // lhs : D columns, R rows // rhs : C columns, D rows // res : C columns, R rows COMPILE_TIME_ASSERT_FUNCTION_SCOPE( MATRIX_A::ROW_SIZE == MATRIX_B::COL_SIZE ); COMPILE_TIME_ASSERT_FUNCTION_SCOPE( MATRIX_R::ROW_SIZE == MATRIX_B::ROW_SIZE ); COMPILE_TIME_ASSERT_FUNCTION_SCOPE( MATRIX_R::COL_SIZE == MATRIX_A::COL_SIZE ); MATRIX_R res(MATRIX_R::NO_INIT); for (size_t r=0 ; r MATRIX PURE transpose(const MATRIX& m) { // for now we only handle square matrix transpose COMPILE_TIME_ASSERT_FUNCTION_SCOPE( MATRIX::ROW_SIZE == MATRIX::COL_SIZE ); MATRIX result(MATRIX::NO_INIT); for (size_t r=0 ; r typename MATRIX::value_type PURE trace(const MATRIX& m) { COMPILE_TIME_ASSERT_FUNCTION_SCOPE( MATRIX::ROW_SIZE == MATRIX::COL_SIZE ); typename MATRIX::value_type result(0); for (size_t r=0 ; r typename MATRIX::col_type PURE diag(const MATRIX& m) { COMPILE_TIME_ASSERT_FUNCTION_SCOPE( MATRIX::ROW_SIZE == MATRIX::COL_SIZE ); typename MATRIX::col_type result(MATRIX::col_type::NO_INIT); for (size_t r=0 ; r String8 asString(const MATRIX& m) { String8 s; for (size_t c=0 ; c. * * BASE only needs to implement operator[] and size(). * By simply inheriting from TMatProductOperators BASE will automatically * get all the functionality here. */ template class BASE, typename T> class TMatProductOperators { public: // multiply by a scalar BASE& operator *= (T v) { BASE& lhs(static_cast< BASE& >(*this)); for (size_t r=0 ; r& operator /= (T v) { BASE& lhs(static_cast< BASE& >(*this)); for (size_t r=0 ; r friend BASE PURE operator *(const BASE& lhs, const BASE& rhs) { return matrix::multiply >(lhs, rhs); } }; /* * TMatSquareFunctions implements functions on a matrix of type BASE. * * BASE only needs to implement: * - operator[] * - col_type * - row_type * - COL_SIZE * - ROW_SIZE * * By simply inheriting from TMatSquareFunctions BASE will automatically * get all the functionality here. */ template class BASE, typename T> class TMatSquareFunctions { public: /* * NOTE: the functions below ARE NOT member methods. They are friend functions * with they definition inlined with their declaration. This makes these * template functions available to the compiler when (and only when) this class * is instantiated, at which point they're only templated on the 2nd parameter * (the first one, BASE being known). */ friend BASE PURE inverse(const BASE& m) { return matrix::inverse(m); } friend BASE PURE transpose(const BASE& m) { return matrix::transpose(m); } friend T PURE trace(const BASE& m) { return matrix::trace(m); } }; template class BASE, typename T> class TMatDebug { public: String8 asString() const { return matrix::asString(*this); } }; // ------------------------------------------------------------------------------------- }; // namespace android #undef PURE #endif /* UI_TMAT_HELPERS_H */