MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/util/matrix.hpp
00001 /*******************************************************************************
00002  * Copyright (c) 2009-2016, MAV'RIC Development Team
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  *
00015  * 3. Neither the name of the copyright holder nor the names of its contributors
00016  * may be used to endorse or promote products derived from this software without
00017  * specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  ******************************************************************************/
00031 
00032 /*******************************************************************************
00033  * \file    matrix.hpp
00034  *
00035  * \author  MAV'RIC Team
00036  * \author  Julien Lecoeur
00037  *
00038  * \brief   Templated matrix library
00039  *
00040  ******************************************************************************/
00041 
00042 
00043 #ifndef MATRIX_HPP__
00044 #define MATRIX_HPP__
00045 
00046 
00047 #include <cstdint>
00048 #include <array>
00049 #include <initializer_list>
00050 
00051 
00055 template<uint32_t N, uint32_t P, typename T>
00056 class Mat;
00057 
00058 namespace mat
00059 {
00060 
00068 class op
00069 {
00070 public:
00087     template<uint32_t N, uint32_t P, typename T>
00088     static void add(const Mat<N,P,T>& m1, const Mat<N,P,T>& m2, Mat<N,P,T>& res);
00089 
00090 
00107     template<uint32_t N, uint32_t P, typename T>
00108     static void add(const Mat<N,P,T>& m1, const T value, Mat<N,P,T>& res);
00109 
00110 
00127     template<uint32_t N, uint32_t P, typename T>
00128     static void subtract(const Mat<N,P,T>& m1, const Mat<N,P,T>& m2, Mat<N,P,T>& res);
00129 
00130 
00147     template<uint32_t N, uint32_t P, typename T>
00148     static void subtract(const Mat<N,P,T>& m1, const T value, Mat<N,P,T>& res);
00149 
00150 
00167     template<uint32_t N, uint32_t P, typename T>
00168     static void multiply(const Mat<N,P,T>& m1, const Mat<N,P,T>& m2, Mat<N,P,T>& res);
00169 
00170 
00187     template<uint32_t N, uint32_t P, typename T>
00188     static void multiply(const Mat<N,P,T>& m1, const T value, Mat<N,P,T>& res);
00189 
00190 
00205     template<uint32_t N, uint32_t P, typename T>
00206     static void transpose(const Mat<N,P,T>& m, Mat<P,N,T>& res);
00207 
00208 
00225     template<uint32_t N, uint32_t P, uint32_t Q, typename T>
00226     static void dot(const Mat<N,P,T>& m1, const Mat<P,Q,T>& m2, Mat<N,Q,T>& res);
00227 
00228 
00244     template<uint32_t N, typename T>
00245     static bool inverse(const Mat<N,N,T>& m, Mat<N,N,T>& res);
00246 
00247 
00285     template<uint32_t N, uint32_t P, uint32_t I, uint32_t J, uint32_t Q, uint32_t R, typename T>
00286     static bool insert(const Mat<N,P,T>& m1, const Mat<Q,R,T>& m2, Mat<N,P,T>& res);
00287 
00325     template<uint32_t N, uint32_t P, uint32_t I, uint32_t J, uint32_t Q, uint32_t R, typename T>
00326     static bool insert_inplace(Mat<N,P,T>& m1, const Mat<Q,R,T>& m2);
00327 
00328 
00336     template<uint32_t N, uint32_t P, typename T>
00337     static void clip(Mat<N,P,T>& m, const Mat<N,P,T>& min, const Mat<N,P,T>& max);
00338 
00339 
00347     template<uint32_t N, uint32_t P, typename T>
00348     static void clip(Mat<N,P,T>& m, float min, float max);
00349 };
00350 
00351 }
00352 
00353 
00361 template<uint32_t N, uint32_t P, typename T=float>
00362 class Mat
00363 {
00364 friend class mat::op;   
00365 
00366 
00367 public:
00374     Mat(T value=0.0f, bool diag=false);
00375 
00381     Mat(const Mat<N,P>& mat);
00382 
00383 
00391     Mat(const std::array<T,N*P> arr);
00392 
00393 
00401     Mat(const std::initializer_list<T> list);
00402 
00403 
00409     uint32_t rows(void);
00410 
00411 
00417     uint32_t cols(void);
00418 
00419 
00425     uint32_t index(uint32_t i, uint32_t j);
00426 
00435     const T& operator()(const uint32_t& i, const uint32_t& j) const;
00436     T& operator()(const uint32_t& i, const uint32_t& j);
00437 
00438 
00446     const T& operator[](const uint32_t& index) const;
00447     T& operator[](const uint32_t& index);
00448 
00449 
00457     Mat add(const Mat& m) const;
00458 
00459 
00467     Mat add(const T value) const;
00468 
00469 
00477     Mat operator+(const Mat& m) const;
00478 
00479 
00487     Mat operator+(T value) const;
00488 
00489 
00498     // template<uint32_t NN, uint32_t PP, typename TT>
00499     template<typename TT>
00500     friend Mat operator+(const T value, const Mat& m);
00501 
00502 
00510     void add_inplace(const Mat& m);
00511 
00512 
00520     void add_inplace(const T value);
00521 
00522 
00530     void operator+=(const Mat& m);
00531 
00532 
00540     void operator+=(const T value);
00541 
00542 
00550     Mat subtract(const Mat& m) const;
00551 
00552 
00560     Mat subtract(const T value) const;
00561 
00562 
00570     Mat operator-(const Mat& m) const;
00571 
00572 
00580     Mat operator-(const T value) const;
00581 
00582 
00590     // template<uint32_t NN, uint32_t PP, typename TT>
00591     template<typename TT>
00592     friend Mat operator-(const T value, const Mat& m);
00593 
00594 
00602     void subtract_inplace(const Mat& m);
00603 
00604 
00612     void subtract_inplace(const T value);
00613 
00614 
00622     void operator-=(const Mat& m);
00623 
00624 
00632     void operator-=(const T value);
00633 
00641     Mat multiply(const Mat& m) const;
00642 
00643 
00651     Mat multiply(const T value) const;
00652 
00653 
00661     Mat operator*(const Mat& m) const;
00662 
00663 
00671     Mat operator*(const T value) const;
00672 
00673 
00682     template<typename TT>
00683     friend Mat operator*(const T value, const Mat& m);
00684 
00685 
00693     void multiply_inplace(const Mat& m);
00694 
00695 
00703     void multiply_inplace(const T value);
00704 
00705 
00713     void operator*=(const Mat& m);
00714 
00715 
00723     void operator*=(const T value);
00724 
00725 
00738     template<uint32_t Q>
00739     Mat<N,Q,T> dot(const Mat<P,Q,T>& m) const
00740     {
00741         Mat<N,Q,T> res;
00742         mat::op::dot(*this, m, res);
00743         return res;
00744     }
00745 
00746 
00759     template<uint32_t Q>
00760     Mat<N,Q,T> operator%(const Mat<P,Q,T>& m) const
00761     {
00762         return dot(m);
00763     }
00764 
00765 
00771     Mat<P,N,T> transpose(void) const;
00772 
00773 
00779     Mat<P,N,T> operator~(void) const;
00780 
00781 
00789     Mat inverse(bool& success) const;
00790 
00791 
00799     Mat inv(bool& success) const;
00800 
00801 
00812     template<uint32_t I, uint32_t J, uint32_t Q, uint32_t R>
00813     Mat insert(const Mat<Q,R,T>& m) const;
00814 
00815 
00828     template<uint32_t I, uint32_t J, uint32_t Q, uint32_t R>
00829     bool insert_inplace(const Mat<Q,R,T>& m);
00830 
00831 
00838     void clip(const Mat& min, const Mat& max);
00839 
00840 
00847     void clip(float min, float max);
00848 
00849 private:
00850     std::array<T,N*P> d;   
00851 };
00852 
00853 
00857 typedef Mat<3, 1, float> Vector3f;
00858 
00859 
00860 #include "matrix.hxx"
00861 
00862 
00863 #endif /* MATRIX_HPP__ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines