Main MRPT website > C++ reference
MRPT logo

Array.h

Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_ARRAY_H
00026 #define EIGEN_ARRAY_H
00027 
00028 /** \class Array 
00029   * \ingroup Core_Module
00030   *
00031   * \brief General-purpose arrays with easy API for coefficient-wise operations
00032   *
00033   * The %Array class is very similar to the Matrix class. It provides
00034   * general-purpose one- and two-dimensional arrays. The difference between the
00035   * %Array and the %Matrix class is primarily in the API: the API for the
00036   * %Array class provides easy access to coefficient-wise operations, while the
00037   * API for the %Matrix class provides easy access to linear-algebra
00038   * operations.
00039   *
00040   * \sa \ref TutorialArrayClass, \ref TopicClassHierarchy
00041   */
00042 namespace internal {
00043 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00044 struct traits<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > : traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
00045 {
00046   typedef ArrayXpr XprKind;
00047   typedef ArrayBase<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > XprBase;
00048 };
00049 }
00050 
00051 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00052 class Array
00053   : public PlainObjectBase<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
00054 {
00055   public:
00056 
00057     typedef PlainObjectBase<Array> Base;
00058     EIGEN_DENSE_PUBLIC_INTERFACE(Array)
00059 
00060     enum { Options = _Options };
00061     typedef typename Base::PlainObject PlainObject;
00062 
00063   protected:
00064     template <typename Derived, typename OtherDerived, bool IsVector>
00065     friend struct internal::conservative_resize_like_impl;
00066 
00067     using Base::m_storage;
00068   public:
00069     enum { NeedsToAlign = (!(Options&DontAlign))
00070                           && SizeAtCompileTime!=Dynamic && ((static_cast<int>(sizeof(Scalar))*SizeAtCompileTime)%16)==0 };
00071     EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
00072 
00073     using Base::base;
00074     using Base::coeff;
00075     using Base::coeffRef;
00076 
00077     /**
00078       * The usage of
00079       *   using Base::operator=;
00080       * fails on MSVC. Since the code below is working with GCC and MSVC, we skipped
00081       * the usage of 'using'. This should be done only for operator=.
00082       */
00083     template<typename OtherDerived>
00084     EIGEN_STRONG_INLINE Array& operator=(const EigenBase<OtherDerived> &other)
00085     {
00086       return Base::operator=(other);
00087     }
00088 
00089     /** Copies the value of the expression \a other into \c *this with automatic resizing.
00090       *
00091       * *this might be resized to match the dimensions of \a other. If *this was a null matrix (not already initialized),
00092       * it will be initialized.
00093       *
00094       * Note that copying a row-vector into a vector (and conversely) is allowed.
00095       * The resizing, if any, is then done in the appropriate way so that row-vectors
00096       * remain row-vectors and vectors remain vectors.
00097       */
00098     template<typename OtherDerived>
00099     EIGEN_STRONG_INLINE Array& operator=(const ArrayBase<OtherDerived>& other)
00100     {
00101       return Base::_set(other);
00102     }
00103 
00104     /** This is a special case of the templated operator=. Its purpose is to
00105       * prevent a default operator= from hiding the templated operator=.
00106       */
00107     EIGEN_STRONG_INLINE Array& operator=(const Array& other)
00108     {
00109       return Base::_set(other);
00110     }
00111 
00112     /** Default constructor.
00113       *
00114       * For fixed-size matrices, does nothing.
00115       *
00116       * For dynamic-size matrices, creates an empty matrix of size 0. Does not allocate any array. Such a matrix
00117       * is called a null matrix. This constructor is the unique way to create null matrices: resizing
00118       * a matrix to 0 is not supported.
00119       *
00120       * \sa resize(Index,Index)
00121       */
00122     EIGEN_STRONG_INLINE explicit Array() : Base()
00123     {
00124       Base::_check_template_params();
00125       EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
00126     }
00127 
00128 #ifndef EIGEN_PARSED_BY_DOXYGEN
00129     // FIXME is it still needed ??
00130     /** \internal */
00131     Array(internal::constructor_without_unaligned_array_assert)
00132       : Base(internal::constructor_without_unaligned_array_assert())
00133     {
00134       Base::_check_template_params();
00135       EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
00136     }
00137 #endif
00138 
00139     /** Constructs a vector or row-vector with given dimension. \only_for_vectors
00140       *
00141       * Note that this is only useful for dynamic-size vectors. For fixed-size vectors,
00142       * it is redundant to pass the dimension here, so it makes more sense to use the default
00143       * constructor Matrix() instead.
00144       */
00145     EIGEN_STRONG_INLINE explicit Array(Index dim)
00146       : Base(dim, RowsAtCompileTime == 1 ? 1 : dim, ColsAtCompileTime == 1 ? 1 : dim)
00147     {
00148       Base::_check_template_params();
00149       EIGEN_STATIC_ASSERT_VECTOR_ONLY(Array)
00150       eigen_assert(dim > 0);
00151       eigen_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == dim);
00152       EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
00153     }
00154 
00155     #ifndef EIGEN_PARSED_BY_DOXYGEN
00156     template<typename T0, typename T1>
00157     EIGEN_STRONG_INLINE Array(const T0& x, const T1& y)
00158     {
00159       Base::_check_template_params();
00160       this->template _init2<T0,T1>(x, y);
00161     }
00162     #else
00163     /** constructs an uninitialized matrix with \a rows rows and \a cols columns.
00164       *
00165       * This is useful for dynamic-size matrices. For fixed-size matrices,
00166       * it is redundant to pass these parameters, so one should use the default constructor
00167       * Matrix() instead. */
00168     Array(Index rows, Index cols);
00169     /** constructs an initialized 2D vector with given coefficients */
00170     Array(const Scalar& x, const Scalar& y);
00171     #endif
00172 
00173     /** constructs an initialized 3D vector with given coefficients */
00174     EIGEN_STRONG_INLINE Array(const Scalar& x, const Scalar& y, const Scalar& z)
00175     {
00176       Base::_check_template_params();
00177       EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Array, 3)
00178       m_storage.data()[0] = x;
00179       m_storage.data()[1] = y;
00180       m_storage.data()[2] = z;
00181     }
00182     /** constructs an initialized 4D vector with given coefficients */
00183     EIGEN_STRONG_INLINE Array(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
00184     {
00185       Base::_check_template_params();
00186       EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Array, 4)
00187       m_storage.data()[0] = x;
00188       m_storage.data()[1] = y;
00189       m_storage.data()[2] = z;
00190       m_storage.data()[3] = w;
00191     }
00192 
00193     explicit Array(const Scalar *data);
00194 
00195     /** Constructor copying the value of the expression \a other */
00196     template<typename OtherDerived>
00197     EIGEN_STRONG_INLINE Array(const ArrayBase<OtherDerived>& other)
00198              : Base(other.rows() * other.cols(), other.rows(), other.cols())
00199     {
00200       Base::_check_template_params();
00201       Base::_set_noalias(other);
00202     }
00203     /** Copy constructor */
00204     EIGEN_STRONG_INLINE Array(const Array& other)
00205             : Base(other.rows() * other.cols(), other.rows(), other.cols())
00206     {
00207       Base::_check_template_params();
00208       Base::_set_noalias(other);
00209     }
00210     /** Copy constructor with in-place evaluation */
00211     template<typename OtherDerived>
00212     EIGEN_STRONG_INLINE Array(const ReturnByValue<OtherDerived>& other)
00213     {
00214       Base::_check_template_params();
00215       Base::resize(other.rows(), other.cols());
00216       other.evalTo(*this);
00217     }
00218 
00219     /** \sa MatrixBase::operator=(const EigenBase<OtherDerived>&) */
00220     template<typename OtherDerived>
00221     EIGEN_STRONG_INLINE Array(const EigenBase<OtherDerived> &other)
00222       : Base(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
00223     {
00224       Base::_check_template_params();
00225       Base::resize(other.rows(), other.cols());
00226       *this = other;
00227     }
00228 
00229     /** Override MatrixBase::swap() since for dynamic-sized matrices of same type it is enough to swap the
00230       * data pointers.
00231       */
00232     template<typename OtherDerived>
00233     void swap(ArrayBase<OtherDerived> const & other)
00234     { this->_swap(other.derived()); }
00235 
00236     inline Index innerStride() const { return 1; }
00237     inline Index outerStride() const { return this->innerSize(); }
00238 
00239     #ifdef EIGEN_ARRAY_PLUGIN
00240     #include EIGEN_ARRAY_PLUGIN
00241     #endif
00242 
00243   private:
00244 
00245     template<typename MatrixType, typename OtherDerived, bool SwapPointers>
00246     friend struct internal::matrix_swap_impl;
00247 };
00248 
00249 /** \defgroup arraytypedefs Global array typedefs
00250   * \ingroup Core_Module
00251   *
00252   * Eigen defines several typedef shortcuts for most common 1D and 2D array types.
00253   *
00254   * The general patterns are the following:
00255   *
00256   * \c ArrayRowsColsType where \c Rows and \c Cols can be \c 2,\c 3,\c 4 for fixed size square matrices or \c X for dynamic size,
00257   * and where \c Type can be \c i for integer, \c f for float, \c d for double, \c cf for complex float, \c cd
00258   * for complex double.
00259   *
00260   * For example, \c Array33d is a fixed-size 3x3 array type of doubles, and \c ArrayXXf is a dynamic-size matrix of floats.
00261   *
00262   * There are also \c ArraySizeType which are self-explanatory. For example, \c Array4cf is
00263   * a fixed-size 1D array of 4 complex floats.
00264   *
00265   * \sa class Array
00266   */
00267 
00268 #define EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix)   \
00269 /** \ingroup arraytypedefs */                                    \
00270 typedef Array<Type, Size, Size> Array##SizeSuffix##SizeSuffix##TypeSuffix;  \
00271 /** \ingroup arraytypedefs */                                    \
00272 typedef Array<Type, Size, 1>    Array##SizeSuffix##TypeSuffix;
00273 
00274 #define EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, Size)         \
00275 /** \ingroup arraytypedefs */                                    \
00276 typedef Array<Type, Size, Dynamic> Array##Size##X##TypeSuffix;  \
00277 /** \ingroup arraytypedefs */                                    \
00278 typedef Array<Type, Dynamic, Size> Array##X##Size##TypeSuffix;
00279 
00280 #define EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
00281 EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 2, 2) \
00282 EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 3, 3) \
00283 EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, 4, 4) \
00284 EIGEN_MAKE_ARRAY_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
00285 EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
00286 EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
00287 EIGEN_MAKE_ARRAY_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
00288 
00289 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(int,                  i)
00290 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(float,                f)
00291 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(double,               d)
00292 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(std::complex<float>,  cf)
00293 EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
00294 
00295 #undef EIGEN_MAKE_ARRAY_TYPEDEFS_ALL_SIZES
00296 #undef EIGEN_MAKE_ARRAY_TYPEDEFS
00297 
00298 #undef EIGEN_MAKE_ARRAY_TYPEDEFS_LARGE
00299 
00300 #define EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, SizeSuffix) \
00301 using Eigen::Matrix##SizeSuffix##TypeSuffix; \
00302 using Eigen::Vector##SizeSuffix##TypeSuffix; \
00303 using Eigen::RowVector##SizeSuffix##TypeSuffix;
00304 
00305 #define EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(TypeSuffix) \
00306 EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 2) \
00307 EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 3) \
00308 EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, 4) \
00309 EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE_AND_SIZE(TypeSuffix, X) \
00310 
00311 #define EIGEN_USING_ARRAY_TYPEDEFS \
00312 EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(i) \
00313 EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(f) \
00314 EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(d) \
00315 EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(cf) \
00316 EIGEN_USING_ARRAY_TYPEDEFS_FOR_TYPE(cd)
00317 
00318 
00319 #endif // EIGEN_ARRAY_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011