/* * Copyright (C) 2011 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 ANDROID_MAT_H #define ANDROID_MAT_H #include "vec.h" #include "traits.h" // ----------------------------------------------------------------------- namespace android { template class mat; namespace helpers { template mat& doAssign( mat& lhs, typename TypeTraits::ParameterType rhs) { for (size_t i=0 ; i mat PURE doMul( const mat& lhs, const mat& rhs) { mat res; for (size_t c=0 ; c vec PURE doMul( const mat& lhs, const vec& rhs) { vec res; for (size_t r=0 ; r mat PURE doMul( const vec& lhs, const mat& rhs) { mat res; for (size_t c=0 ; c mat PURE doMul( const mat& rhs, typename TypeTraits::ParameterType v) { mat res; for (size_t c=0 ; c mat PURE doMul( typename TypeTraits::ParameterType v, const mat& rhs) { mat res; for (size_t c=0 ; c class mat : public vec< vec, C > { typedef typename TypeTraits::ParameterType pTYPE; typedef vec< vec, C > base; public: // STL-like interface. typedef TYPE value_type; typedef TYPE& reference; typedef TYPE const& const_reference; typedef size_t size_type; size_type size() const { return R*C; } enum { ROWS = R, COLS = C }; // ----------------------------------------------------------------------- // default constructors mat() { } mat(const mat& rhs) : base(rhs) { } mat(const base& rhs) : base(rhs) { } // ----------------------------------------------------------------------- // conversion constructors // sets the diagonal to the value, off-diagonal to zero mat(pTYPE rhs) { helpers::doAssign(*this, rhs); } // ----------------------------------------------------------------------- // Assignment mat& operator=(const mat& rhs) { base::operator=(rhs); return *this; } mat& operator=(const base& rhs) { base::operator=(rhs); return *this; } mat& operator=(pTYPE rhs) { return helpers::doAssign(*this, rhs); } // ----------------------------------------------------------------------- // non-member function declaration and definition friend inline mat PURE operator + (const mat& lhs, const mat& rhs) { return helpers::doAdd( static_cast(lhs), static_cast(rhs)); } friend inline mat PURE operator - (const mat& lhs, const mat& rhs) { return helpers::doSub( static_cast(lhs), static_cast(rhs)); } // matrix*matrix template friend mat PURE operator * ( const mat& lhs, const mat& rhs) { return helpers::doMul(lhs, rhs); } // matrix*vector friend vec PURE operator * ( const mat& lhs, const vec& rhs) { return helpers::doMul(lhs, rhs); } // vector*matrix friend mat PURE operator * ( const vec& lhs, const mat& rhs) { return helpers::doMul(lhs, rhs); } // matrix*scalar friend inline mat PURE operator * (const mat& lhs, pTYPE v) { return helpers::doMul(lhs, v); } // scalar*matrix friend inline mat PURE operator * (pTYPE v, const mat& rhs) { return helpers::doMul(v, rhs); } // ----------------------------------------------------------------------- // streaming operator to set the columns of the matrix: // example: // mat33_t m; // m << v0 << v1 << v2; // column_builder<> stores the matrix and knows which column to set template struct column_builder { mat& matrix; column_builder(mat& matrix) : matrix(matrix) { } }; // operator << is not a method of column_builder<> so we can // overload it for unauthorized values (partial specialization // not allowed in class-scope). // we just set the column and return the next column_builder<> template friend column_builder operator << ( const column_builder& lhs, const vec& rhs) { lhs.matrix[PREV_COLUMN+1] = rhs; return column_builder(lhs.matrix); } // we return void here so we get a compile-time error if the // user tries to set too many columns friend void operator << ( const column_builder& lhs, const vec& rhs) { lhs.matrix[C-1] = rhs; } // this is where the process starts. we set the first columns and // return the next column_builder<> column_builder<0> operator << (const vec& rhs) { (*this)[0] = rhs; return column_builder<0>(*this); } }; // Specialize column matrix so they're exactly equivalent to a vector template class mat : public vec { typedef vec base; public: // STL-like interface. typedef TYPE value_type; typedef TYPE& reference; typedef TYPE const& const_reference; typedef size_t size_type; size_type size() const { return R; } enum { ROWS = R, COLS = 1 }; mat() { } mat(const base& rhs) : base(rhs) { } mat(const mat& rhs) : base(rhs) { } mat(const TYPE& rhs) { helpers::doAssign(*this, rhs); } mat& operator=(const mat& rhs) { base::operator=(rhs); return *this; } mat& operator=(const base& rhs) { base::operator=(rhs); return *this; } mat& operator=(const TYPE& rhs) { return helpers::doAssign(*this, rhs); } // we only have one column, so ignore the index const base& operator[](size_t) const { return *this; } base& operator[](size_t) { return *this; } void operator << (const vec& rhs) { base::operator[](0) = rhs; } }; // ----------------------------------------------------------------------- // matrix functions // transpose. this handles matrices of matrices inline int PURE transpose(int v) { return v; } inline float PURE transpose(float v) { return v; } inline double PURE transpose(double v) { return v; } // Transpose a matrix template mat PURE transpose(const mat& m) { mat r; for (size_t i=0 ; i static TYPE trace(const mat& m) { TYPE t; for (size_t i=0 ; i static bool isPositiveSemidefinite(const mat& m, TYPE tolerance) { for (size_t i=0 ; i tolerance) return false; return true; } // Transpose a vector template < template class VEC, typename TYPE, size_t SIZE > mat PURE transpose(const VEC& v) { mat r; for (size_t i=0 ; i mat PURE invert(const mat& src) { T t; size_t swap; mat tmp(src); mat 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 mat22_t; typedef mat mat33_t; typedef mat mat44_t; // ----------------------------------------------------------------------- }; // namespace android #endif /* ANDROID_MAT_H */