Main MRPT website > C++ reference
MRPT logo

Map.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) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_MAP_H
00027 #define EIGEN_MAP_H
00028 
00029 /** \class Map
00030   * \ingroup Core_Module
00031   *
00032   * \brief A matrix or vector expression mapping an existing array of data.
00033   *
00034   * \param PlainObjectType the equivalent matrix type of the mapped data
00035   * \param MapOptions specifies whether the pointer is \c Aligned, or \c Unaligned.
00036   *                The default is \c Unaligned.
00037   * \param StrideType optionnally specifies strides. By default, Map assumes the memory layout
00038   *                   of an ordinary, contiguous array. This can be overridden by specifying strides.
00039   *                   The type passed here must be a specialization of the Stride template, see examples below.
00040   *
00041   * This class represents a matrix or vector expression mapping an existing array of data.
00042   * It can be used to let Eigen interface without any overhead with non-Eigen data structures,
00043   * such as plain C arrays or structures from other libraries. By default, it assumes that the
00044   * data is laid out contiguously in memory. You can however override this by explicitly specifying
00045   * inner and outer strides.
00046   *
00047   * Here's an example of simply mapping a contiguous array as a column-major matrix:
00048   * \include Map_simple.cpp
00049   * Output: \verbinclude Map_simple.out
00050   *
00051   * If you need to map non-contiguous arrays, you can do so by specifying strides:
00052   *
00053   * Here's an example of mapping an array as a vector, specifying an inner stride, that is, the pointer
00054   * increment between two consecutive coefficients. Here, we're specifying the inner stride as a compile-time
00055   * fixed value.
00056   * \include Map_inner_stride.cpp
00057   * Output: \verbinclude Map_inner_stride.out
00058   *
00059   * Here's an example of mapping an array while specifying an outer stride. Here, since we're mapping
00060   * as a column-major matrix, 'outer stride' means the pointer increment between two consecutive columns.
00061   * Here, we're specifying the outer stride as a runtime parameter. Note that here \c OuterStride<> is
00062   * a short version of \c OuterStride<Dynamic> because the default template parameter of OuterStride
00063   * is  \c Dynamic
00064   * \include Map_outer_stride.cpp
00065   * Output: \verbinclude Map_outer_stride.out
00066   *
00067   * For more details and for an example of specifying both an inner and an outer stride, see class Stride.
00068   *
00069   * \b Tip: to change the array of data mapped by a Map object, you can use the C++
00070   * placement new syntax:
00071   *
00072   * Example: \include Map_placement_new.cpp
00073   * Output: \verbinclude Map_placement_new.out
00074   *
00075   * This class is the return type of Matrix::Map() but can also be used directly.
00076   *
00077   * \sa Matrix::Map()
00078   */
00079 
00080 namespace internal {
00081 template<typename PlainObjectType, int MapOptions, typename StrideType>
00082 struct traits<Map<PlainObjectType, MapOptions, StrideType> >
00083   : public traits<PlainObjectType>
00084 {
00085   typedef traits<PlainObjectType> TraitsBase;
00086   typedef typename PlainObjectType::Index Index;
00087   typedef typename PlainObjectType::Scalar Scalar;
00088   enum {
00089     InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0
00090                              ? int(PlainObjectType::InnerStrideAtCompileTime)
00091                              : int(StrideType::InnerStrideAtCompileTime),
00092     OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
00093                              ? int(PlainObjectType::OuterStrideAtCompileTime)
00094                              : int(StrideType::OuterStrideAtCompileTime),
00095     HasNoInnerStride = InnerStrideAtCompileTime == 1,
00096     HasNoOuterStride = StrideType::OuterStrideAtCompileTime == 0,
00097     HasNoStride = HasNoInnerStride && HasNoOuterStride,
00098     IsAligned = int(int(MapOptions)&Aligned)==Aligned,
00099     IsDynamicSize = PlainObjectType::SizeAtCompileTime==Dynamic,
00100     KeepsPacketAccess = bool(HasNoInnerStride)
00101                         && ( bool(IsDynamicSize)
00102                            || HasNoOuterStride
00103                            || ( OuterStrideAtCompileTime!=Dynamic
00104                            && ((static_cast<int>(sizeof(Scalar))*OuterStrideAtCompileTime)%16)==0 ) ),
00105     Flags0 = TraitsBase::Flags,
00106     Flags1 = IsAligned ? (int(Flags0) | AlignedBit) : (int(Flags0) & ~AlignedBit),
00107     Flags2 = (bool(HasNoStride) || bool(PlainObjectType::IsVectorAtCompileTime))
00108            ? int(Flags1) : int(Flags1 & ~LinearAccessBit),
00109     Flags3 = is_lvalue<PlainObjectType>::value ? int(Flags2) : (int(Flags2) & ~LvalueBit),
00110     Flags = KeepsPacketAccess ? int(Flags3) : (int(Flags3) & ~PacketAccessBit)
00111   };
00112 private:
00113   enum { Options }; // Expressions don't have Options
00114 };
00115 }
00116 
00117 template<typename PlainObjectType, int MapOptions, typename StrideType> class Map
00118   : public MapBase<Map<PlainObjectType, MapOptions, StrideType> >
00119 {
00120   public:
00121 
00122     typedef MapBase<Map> Base;
00123 
00124     EIGEN_DENSE_PUBLIC_INTERFACE(Map)
00125 
00126     typedef typename Base::PointerType PointerType;
00127 
00128 
00129     inline Index innerStride() const
00130     {
00131       return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
00132     }
00133 
00134     inline Index outerStride() const
00135     {
00136       return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
00137            : IsVectorAtCompileTime ? this->size()
00138            : int(Flags)&RowMajorBit ? this->cols()
00139            : this->rows();
00140     }
00141 
00142     /** Constructor in the fixed-size case.
00143       *
00144       * \param data pointer to the array to map
00145       * \param stride optional Stride object, passing the strides.
00146       */
00147     inline Map(PointerType data, const StrideType& stride = StrideType())
00148       : Base(data), m_stride(stride)
00149     {
00150       PlainObjectType::Base::_check_template_params();
00151     }
00152 
00153     /** Constructor in the dynamic-size vector case.
00154       *
00155       * \param data pointer to the array to map
00156       * \param size the size of the vector expression
00157       * \param stride optional Stride object, passing the strides.
00158       */
00159     inline Map(PointerType data, Index size, const StrideType& stride = StrideType())
00160       : Base(data, size), m_stride(stride)
00161     {
00162       PlainObjectType::Base::_check_template_params();
00163     }
00164 
00165     /** Constructor in the dynamic-size matrix case.
00166       *
00167       * \param data pointer to the array to map
00168       * \param rows the number of rows of the matrix expression
00169       * \param cols the number of columns of the matrix expression
00170       * \param stride optional Stride object, passing the strides.
00171       */
00172     inline Map(PointerType data, Index rows, Index cols, const StrideType& stride = StrideType())
00173       : Base(data, rows, cols), m_stride(stride)
00174     {
00175       PlainObjectType::Base::_check_template_params();
00176     }
00177 
00178 
00179     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
00180 
00181   protected:
00182     StrideType m_stride;
00183 };
00184 
00185 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00186 inline Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
00187   ::Array(const Scalar *data)
00188 {
00189   _set_noalias(Eigen::Map<const Array>(data));
00190 }
00191 
00192 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00193 inline Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
00194   ::Matrix(const Scalar *data)
00195 {
00196   _set_noalias(Eigen::Map<const Matrix>(data));
00197 }
00198 
00199 #endif // EIGEN_MAP_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011