Main MRPT website > C++ reference
MRPT logo

MapBase.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_MAPBASE_H
00027 #define EIGEN_MAPBASE_H
00028 
00029 #define EIGEN_STATIC_ASSERT_LINEAR_ACCESS(Derived) \
00030       EIGEN_STATIC_ASSERT(int(internal::traits<Derived>::Flags) & LinearAccessBit, \
00031                           YOU_ARE_TRYING_TO_USE_AN_INDEX_BASED_ACCESSOR_ON_AN_EXPRESSION_THAT_DOES_NOT_SUPPORT_THAT)
00032 
00033 
00034 /** \class MapBase
00035   * \ingroup Core_Module
00036   *
00037   * \brief Base class for Map and Block expression with direct access
00038   *
00039   * \sa class Map, class Block
00040   */
00041 template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
00042   : public internal::dense_xpr_base<Derived>::type
00043 {
00044   public:
00045 
00046     typedef typename internal::dense_xpr_base<Derived>::type Base;
00047     enum {
00048       RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
00049       ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
00050       SizeAtCompileTime = Base::SizeAtCompileTime
00051     };
00052 
00053     typedef typename internal::traits<Derived>::StorageKind StorageKind;
00054     typedef typename internal::traits<Derived>::Index Index;
00055     typedef typename internal::traits<Derived>::Scalar Scalar;
00056     typedef typename internal::packet_traits<Scalar>::type PacketScalar;
00057     typedef typename NumTraits<Scalar>::Real RealScalar;
00058     typedef typename internal::conditional<
00059                          bool(internal::is_lvalue<Derived>::value),
00060                          Scalar *,
00061                          const Scalar *>::type
00062                      PointerType;
00063 
00064     using Base::derived;
00065 //    using Base::RowsAtCompileTime;
00066 //    using Base::ColsAtCompileTime;
00067 //    using Base::SizeAtCompileTime;
00068     using Base::MaxRowsAtCompileTime;
00069     using Base::MaxColsAtCompileTime;
00070     using Base::MaxSizeAtCompileTime;
00071     using Base::IsVectorAtCompileTime;
00072     using Base::Flags;
00073     using Base::IsRowMajor;
00074 
00075     using Base::rows;
00076     using Base::cols;
00077     using Base::size;
00078     using Base::coeff;
00079     using Base::coeffRef;
00080     using Base::lazyAssign;
00081     using Base::eval;
00082 
00083     using Base::innerStride;
00084     using Base::outerStride;
00085     using Base::rowStride;
00086     using Base::colStride;
00087 
00088 
00089     typedef typename Base::CoeffReturnType CoeffReturnType;
00090 
00091     inline Index rows() const { return m_rows.value(); }
00092     inline Index cols() const { return m_cols.value(); }
00093 
00094     /** Returns a pointer to the first coefficient of the matrix or vector.
00095       *
00096       * \note When addressing this data, make sure to honor the strides returned by innerStride() and outerStride().
00097       *
00098       * \sa innerStride(), outerStride()
00099       */
00100     inline const Scalar* data() const { return m_data; }
00101 
00102     inline const Scalar& coeff(Index row, Index col) const
00103     {
00104       return m_data[col * colStride() + row * rowStride()];
00105     }
00106 
00107     inline const Scalar& coeff(Index index) const
00108     {
00109       EIGEN_STATIC_ASSERT_LINEAR_ACCESS(Derived)
00110       return m_data[index * innerStride()];
00111     }
00112 
00113     inline const Scalar& coeffRef(Index row, Index col) const
00114     {
00115       return this->m_data[col * colStride() + row * rowStride()];
00116     }
00117 
00118     inline const Scalar& coeffRef(Index index) const
00119     {
00120       EIGEN_STATIC_ASSERT_LINEAR_ACCESS(Derived)
00121       return this->m_data[index * innerStride()];
00122     }
00123 
00124     template<int LoadMode>
00125     inline PacketScalar packet(Index row, Index col) const
00126     {
00127       return internal::ploadt<PacketScalar, LoadMode>
00128                (m_data + (col * colStride() + row * rowStride()));
00129     }
00130 
00131     template<int LoadMode>
00132     inline PacketScalar packet(Index index) const
00133     {
00134       EIGEN_STATIC_ASSERT_LINEAR_ACCESS(Derived)
00135       return internal::ploadt<PacketScalar, LoadMode>(m_data + index * innerStride());
00136     }
00137 
00138     inline MapBase(PointerType data) : m_data(data), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime)
00139     {
00140       EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00141       checkSanity();
00142     }
00143 
00144     inline MapBase(PointerType data, Index size)
00145             : m_data(data),
00146               m_rows(RowsAtCompileTime == Dynamic ? size : Index(RowsAtCompileTime)),
00147               m_cols(ColsAtCompileTime == Dynamic ? size : Index(ColsAtCompileTime))
00148     {
00149       EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00150       eigen_assert(size >= 0);
00151       eigen_assert(data == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == size);
00152       checkSanity();
00153     }
00154 
00155     inline MapBase(PointerType data, Index rows, Index cols)
00156             : m_data(data), m_rows(rows), m_cols(cols)
00157     {
00158       eigen_assert( (data == 0)
00159               || (   rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
00160                   && cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
00161       checkSanity();
00162     }
00163 
00164   protected:
00165 
00166     void checkSanity() const
00167     {
00168       EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(internal::traits<Derived>::Flags&PacketAccessBit,
00169                                         internal::inner_stride_at_compile_time<Derived>::ret==1),
00170                           PACKET_ACCESS_REQUIRES_TO_HAVE_INNER_STRIDE_FIXED_TO_1);
00171       eigen_assert(EIGEN_IMPLIES(internal::traits<Derived>::Flags&AlignedBit, (size_t(m_data) % (sizeof(Scalar)*internal::packet_traits<Scalar>::size)) == 0)
00172         && "data is not aligned");
00173     }
00174 
00175     PointerType m_data;
00176     const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
00177     const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
00178 };
00179 
00180 template<typename Derived> class MapBase<Derived, WriteAccessors>
00181   : public MapBase<Derived, ReadOnlyAccessors>
00182 {
00183   public:
00184 
00185     typedef MapBase<Derived, ReadOnlyAccessors> Base;
00186 
00187     typedef typename Base::Scalar Scalar;
00188     typedef typename Base::PacketScalar PacketScalar;
00189     typedef typename Base::Index Index;
00190     typedef typename Base::PointerType PointerType;
00191 
00192     using Base::derived;
00193     using Base::rows;
00194     using Base::cols;
00195     using Base::size;
00196     using Base::coeff;
00197     using Base::coeffRef;
00198 
00199     using Base::innerStride;
00200     using Base::outerStride;
00201     using Base::rowStride;
00202     using Base::colStride;
00203 
00204     typedef typename internal::conditional<
00205                     internal::is_lvalue<Derived>::value,
00206                     Scalar,
00207                     const Scalar
00208                   >::type ScalarWithConstIfNotLvalue;
00209 
00210     inline const Scalar* data() const { return this->m_data; }
00211     inline ScalarWithConstIfNotLvalue* data() { return this->m_data; } // no const-cast here so non-const-correct code will give a compile error
00212 
00213     inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col)
00214     {
00215       return this->m_data[col * colStride() + row * rowStride()];
00216     }
00217 
00218     inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
00219     {
00220       EIGEN_STATIC_ASSERT_LINEAR_ACCESS(Derived)
00221       return this->m_data[index * innerStride()];
00222     }
00223 
00224     template<int StoreMode>
00225     inline void writePacket(Index row, Index col, const PacketScalar& x)
00226     {
00227       internal::pstoret<Scalar, PacketScalar, StoreMode>
00228                (this->m_data + (col * colStride() + row * rowStride()), x);
00229     }
00230 
00231     template<int StoreMode>
00232     inline void writePacket(Index index, const PacketScalar& x)
00233     {
00234       EIGEN_STATIC_ASSERT_LINEAR_ACCESS(Derived)
00235       internal::pstoret<Scalar, PacketScalar, StoreMode>
00236                 (this->m_data + index * innerStride(), x);
00237     }
00238 
00239     inline MapBase(PointerType data) : Base(data) {}
00240     inline MapBase(PointerType data, Index size) : Base(data, size) {}
00241     inline MapBase(PointerType data, Index rows, Index cols) : Base(data, rows, cols) {}
00242 
00243     Derived& operator=(const MapBase& other)
00244     {
00245       Base::Base::operator=(other);
00246       return derived();
00247     }
00248 
00249     using Base::Base::operator=;
00250 };
00251 
00252 
00253 #endif // EIGEN_MAPBASE_H



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