Main MRPT website > C++ reference
MRPT logo
CMatrixTemplateNumeric.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (c) 2005-2013, Individual contributors, see AUTHORS file |
7  | Copyright (c) 2005-2013, MAPIR group, University of Malaga |
8  | Copyright (c) 2012-2013, University of Almeria |
9  | All rights reserved. |
10  | |
11  | Redistribution and use in source and binary forms, with or without |
12  | modification, are permitted provided that the following conditions are |
13  | met: |
14  | * Redistributions of source code must retain the above copyright |
15  | notice, this list of conditions and the following disclaimer. |
16  | * Redistributions in binary form must reproduce the above copyright |
17  | notice, this list of conditions and the following disclaimer in the |
18  | documentation and/or other materials provided with the distribution. |
19  | * Neither the name of the copyright holders nor the |
20  | names of its contributors may be used to endorse or promote products |
21  | derived from this software without specific prior written permission.|
22  | |
23  | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24  | 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
25  | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR|
26  | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
27  | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL|
28  | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR|
29  | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30  | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
31  | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
32  | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
33  | POSSIBILITY OF SUCH DAMAGE. |
34  +---------------------------------------------------------------------------+ */
35 #ifndef CMatrixTemplateNumeric_H
36 #define CMatrixTemplateNumeric_H
37 
40 
41 #include <mrpt/system/os.h>
42 #include <cmath>
43 #include <limits>
44 
45 namespace mrpt
46 {
47  namespace poses
48  {
49  class CPose2D;
50  class CPose3D;
51  class CPoint2D;
52  class CPoint3D;
53  }
54 
55  namespace math
56  {
57  using namespace mrpt::system;
58 
59  /** A matrix of dynamic size.
60  * Basically, this class is a wrapper on Eigen::Matrix<T,Dynamic,Dynamic>, but
61  * with a RowMajor element memory layout (except for column vectors).
62  *
63  * \note This class exists for backward compatibility of ancient times when MRPT didn't rely on Eigen, feel free to directly use Eigen::Matrix<> types instead.
64  *
65  * \sa CMatrixTemplate (a non Eigen lib-based class, which can hold arbitrary objects, not only numerical types).
66  * \note For a complete introduction to Matrices and vectors in MRPT, see: http://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes
67  * \ingroup mrpt_base_grp
68  */
69  template <class T>
71  public Eigen::Matrix<
72  T,
73  Eigen::Dynamic,
74  Eigen::Dynamic,
75  // Use row major storage for backward compatibility with MRPT matrices in all cases (even in column vectors!)
76  Eigen::AutoAlign | Eigen::RowMajor
77  >
78  {
79  public:
80  typedef Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic,Eigen::AutoAlign|Eigen::RowMajor> Base;
82 
84 
85  /** Default constructor, builds a 1x1 matrix */
86  inline CMatrixTemplateNumeric() : Base(1,1) { Base::setZero(); }
87 
88  /** Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency is a priority.
89  * Use as:
90  * \code
91  * CMatrixTemplateNumeric<double> M( UNINITIALIZED_MATRIX);
92  * \endcode
93  */
94  inline CMatrixTemplateNumeric(TConstructorFlags_Matrices constructor_flag) : Base( 0,0 ) { }
95 
96  /** Constructor, creates a matrix of the given size, filled with zeros. */
97  inline CMatrixTemplateNumeric(size_t row, size_t col) : Base(row,col) { Base::setZero(); }
98 
99  MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixTemplateNumeric) // Implements ctor and "operator =" for any other Eigen class
100 
101  /** Assignment operator of other types
102  */
103  template <class R>
104  inline CMatrixTemplateNumeric<T>& operator = (const CMatrixTemplate<R>& m)
105  {
106  Base::resize( m.getRowCount(), m.getColCount() );
107 
108  for (size_t i=0; i < CMatrixTemplate<T>::getRowCount(); i++)
109  for (size_t j=0; j < CMatrixTemplate<T>::getColCount(); j++)
110  Base::coeffRef(i,j) = static_cast<T>(m.get_unsafe(i,j));
111  return *this;
112  }
113 
114  /** Constructor from a given size and a C array. The array length must match cols x row.
115  * \code
116  * const double numbers[] = {
117  * 1,2,3,
118  * 4,5,6 };
119  * CMatrixDouble M(3,2, numbers);
120  * \endcode
121  */
122  template <typename V, size_t N>
123  inline CMatrixTemplateNumeric(size_t row, size_t col, V (&theArray)[N] ) : Base(row,col)
124  {
125  ASSERT_EQUAL_(row*col,N)
126  ASSERT_EQUAL_(sizeof(theArray[0]),sizeof(T))
127  ::memcpy(Base::data(),&theArray[0],sizeof(T)*N); // Remember, row-major order!
128  }
129 
130  /** Destructor
131  */
132  inline ~CMatrixTemplateNumeric() { }
133 
134  /** == comparison of two matrices; it differs from default Eigen operator in that returns false if matrices are of different sizes instead of raising an assert. */
135  template <typename Derived>
136  inline bool operator ==(const Eigen::MatrixBase<Derived>& m2) const
137  {
138  return Base::cols()==m2.cols() &&
139  Base::rows()==m2.rows() &&
140  Base::cwiseEqual(m2).all();
141  }
142  /** != comparison of two matrices; it differs from default Eigen operator in that returns true if matrices are of different sizes instead of raising an assert. */
143  template <typename Derived>
144  inline bool operator !=(const Eigen::MatrixBase<Derived>& m2) const{ return !((*this)==m2); }
145 
146  }; // end of class definition
147 
148 
149  /** Declares a matrix of float numbers (non serializable).
150  * For a serializable version, use math::CMatrix
151  * \sa CMatrixDouble, CMatrix, CMatrixD
152  */
154 
155  /** Declares a matrix of double numbers (non serializable).
156  * For a serializable version, use math::CMatrixD
157  * \sa CMatrixFloat, CMatrix, CMatrixD
158  */
160 
161  /** Declares a matrix of unsigned ints (non serializable).
162  * \sa CMatrixDouble, CMatrixFloat
163  */
165 
166  /** Declares a matrix of booleans (non serializable).
167  * \sa CMatrixDouble, CMatrixFloat, CMatrixB
168  */
170 
171 #ifdef HAVE_LONG_DOUBLE
172  /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
173  * \sa CMatrixDouble, CMatrixFloat
174  */
176 #else
177  /** Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not support "long double".
178  * \sa CMatrixDouble, CMatrixFloat
179  */
181 #endif
182 
183 
184  namespace detail
185  {
186  /**
187  * Vicinity traits class specialization for fixed size matrices.
188  */
189  template<typename T> class VicinityTraits<CMatrixTemplateNumeric<T> > {
190  public:
191  inline static void initialize(CMatrixTemplateNumeric<T> &mat,size_t N) {
192  mat.setSize(N,N);
193  mat.fill(0);
194  }
195  inline static void insertInContainer(CMatrixTemplateNumeric<T> &mat,size_t r,size_t c,const T &t) {
196  mat.get_unsafe(r,c)=t;
197  }
198  };
199  } //End of detail namespace.
200 
201  } // End of namespace
202 
203 
204  namespace utils
205  {
206  // Extensions to mrpt::utils::TTypeName for matrices:
207  template<typename T> struct TTypeName <mrpt::math::CMatrixTemplateNumeric<T> > {
208  static std::string get() { return std::string("CMatrixTemplateNumeric<")+ std::string( TTypeName<T>::get() )+std::string(">"); }
209  };
210  }
211 
212 } // End of namespace
213 
214 
215 #endif
bool operator!=(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:306
#define ASSERT_EQUAL_(__A, __B)
const T & get_unsafe(size_t row, size_t col) const
Fast but unsafe method to read a value from the matrix.
TConstructorFlags_Matrices
For usage in one of the constructors of CMatrixFixedNumeric or CMatrixTemplate (and derived classes)...
Definition: math_frwds.h:107
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:58
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:72
static void insertInContainer(CMatrixTemplateNumeric< T > &mat, size_t r, size_t c, const T &t)
EIGEN_STRONG_INLINE const AdjointReturnType t() const
Transpose.
#define MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(_CLASS_)
CMatrixTemplateNumeric< T > mrpt_autotype
EIGEN_STRONG_INLINE size_t getColCount() const
Get number of columns.
Definition: eigen_plugins.h:74
CMatrixTemplate< bool > CMatrixBool
Declares a matrix of booleans (non serializable).
The purpose of this class is to model traits for containers, so that they can be used as return value...
Definition: math_frwds.h:179
bool operator==(const CArray< T, N > &x, const CArray< T, N > &y)
Definition: CArray.h:298
#define MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(_CLASS_)
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign|Eigen::RowMajor > Base
A class used to store a 2D point.
Definition: CPoint2D.h:61
A class used to store a 3D point.
Definition: CPoint3D.h:59
This template class provides the basic functionality for a general 2D any-size, resizable container o...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CMatrixTemplateNumeric< unsigned int > CMatrixUInt
Declares a matrix of unsigned ints (non serializable).
A class used to store a 2D pose.
Definition: CPose2D.h:61
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:97
size_t getColCount() const
Number of columns in the matrix.
size_t getRowCount() const
Number of rows in the matrix.
CMatrixTemplateNumeric< double > CMatrixLongDouble
Declares a matrix of "long doubles" (non serializable), or of "doubles" if the compiler does not supp...
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
EIGEN_STRONG_INLINE size_t getRowCount() const
Get number of rows.
Definition: eigen_plugins.h:72
CMatrixTemplateNumeric(TConstructorFlags_Matrices constructor_flag)
Constructor that builds a 0x0 matrix (that is, uninitialized), for usage in places where efficiency i...
CMatrixTemplateNumeric(size_t row, size_t col)
Constructor, creates a matrix of the given size, filled with zeros.
static void initialize(CMatrixTemplateNumeric< T > &mat, size_t N)
CMatrixTemplateNumeric< float > CMatrixFloat
Declares a matrix of float numbers (non serializable).



Page generated by Doxygen 1.8.14 for MRPT 1.0.2 SVN: at lun oct 28 00:52:41 CET 2019 Hosted on:
SourceForge.net Logo