2007-10-14 14:45:31 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2007-10-14 14:45:31 +00:00
|
|
|
//
|
2010-02-18 20:42:38 -05:00
|
|
|
// Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2010-06-24 23:21:58 +02:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2007-10-14 14:45:31 +00:00
|
|
|
//
|
2008-02-28 15:44:45 +00:00
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
2008-03-04 12:34:58 +00:00
|
|
|
// License as published by the Free Software Foundation; either
|
2008-02-28 15:44:45 +00:00
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2008-03-04 12:34:58 +00:00
|
|
|
// published by the Free Software Foundation; either version 2 of
|
2008-02-28 15:44:45 +00:00
|
|
|
// the License, or (at your option) any later version.
|
2007-10-14 14:45:31 +00:00
|
|
|
//
|
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2008-02-28 15:44:45 +00:00
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
|
// GNU General Public License for more details.
|
2007-10-14 14:45:31 +00:00
|
|
|
//
|
2008-03-04 12:34:58 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
2008-02-28 15:44:45 +00:00
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-14 14:45:31 +00:00
|
|
|
|
2007-12-19 09:30:53 +00:00
|
|
|
#ifndef EIGEN_MAP_H
|
|
|
|
|
#define EIGEN_MAP_H
|
2007-10-14 14:45:31 +00:00
|
|
|
|
2008-01-03 19:36:32 +00:00
|
|
|
/** \class Map
|
2010-07-06 13:10:08 +01:00
|
|
|
* \ingroup Core_Module
|
2008-01-03 19:36:32 +00:00
|
|
|
*
|
|
|
|
|
* \brief A matrix or vector expression mapping an existing array of data.
|
|
|
|
|
*
|
2010-04-18 22:14:55 -04:00
|
|
|
* \param PlainObjectType the equivalent matrix type of the mapped data
|
2010-04-26 16:59:04 +02:00
|
|
|
* \param MapOptions specifies whether the pointer is \c Aligned, or \c Unaligned.
|
2009-11-20 16:30:14 +01:00
|
|
|
* The default is \c Unaligned.
|
2010-02-26 21:29:04 -05:00
|
|
|
* \param StrideType optionnally specifies strides. By default, Map assumes the memory layout
|
|
|
|
|
* of an ordinary, contiguous array. This can be overridden by specifying strides.
|
|
|
|
|
* The type passed here must be a specialization of the Stride template, see examples below.
|
2008-06-27 01:22:35 +00:00
|
|
|
*
|
2008-01-03 19:36:32 +00:00
|
|
|
* This class represents a matrix or vector expression mapping an existing array of data.
|
|
|
|
|
* It can be used to let Eigen interface without any overhead with non-Eigen data structures,
|
2010-02-26 21:29:04 -05:00
|
|
|
* such as plain C arrays or structures from other libraries. By default, it assumes that the
|
|
|
|
|
* data is laid out contiguously in memory. You can however override this by explicitly specifying
|
|
|
|
|
* inner and outer strides.
|
|
|
|
|
*
|
|
|
|
|
* Here's an example of simply mapping a contiguous array as a column-major matrix:
|
|
|
|
|
* \include Map_simple.cpp
|
|
|
|
|
* Output: \verbinclude Map_simple.out
|
|
|
|
|
*
|
|
|
|
|
* If you need to map non-contiguous arrays, you can do so by specifying strides:
|
|
|
|
|
*
|
|
|
|
|
* Here's an example of mapping an array as a vector, specifying an inner stride, that is, the pointer
|
|
|
|
|
* increment between two consecutive coefficients. Here, we're specifying the inner stride as a compile-time
|
|
|
|
|
* fixed value.
|
|
|
|
|
* \include Map_inner_stride.cpp
|
|
|
|
|
* Output: \verbinclude Map_inner_stride.out
|
|
|
|
|
*
|
|
|
|
|
* Here's an example of mapping an array while specifying an outer stride. Here, since we're mapping
|
|
|
|
|
* as a column-major matrix, 'outer stride' means the pointer increment between two consecutive columns.
|
2010-06-25 14:48:16 +02:00
|
|
|
* Here, we're specifying the outer stride as a runtime parameter. Note that here \c OuterStride<> is
|
|
|
|
|
* a short version of \c OuterStride<Dynamic> because the default template parameter of OuterStride
|
|
|
|
|
* is \c Dynamic
|
2010-02-26 21:29:04 -05:00
|
|
|
* \include Map_outer_stride.cpp
|
|
|
|
|
* Output: \verbinclude Map_outer_stride.out
|
|
|
|
|
*
|
|
|
|
|
* For more details and for an example of specifying both an inner and an outer stride, see class Stride.
|
2009-03-09 19:23:31 +00:00
|
|
|
*
|
2009-10-23 14:26:14 +02:00
|
|
|
* \b Tip: to change the array of data mapped by a Map object, you can use the C++
|
2009-02-09 09:54:48 +00:00
|
|
|
* placement new syntax:
|
|
|
|
|
*
|
|
|
|
|
* Example: \include Map_placement_new.cpp
|
|
|
|
|
* Output: \verbinclude Map_placement_new.out
|
2008-01-03 19:36:32 +00:00
|
|
|
*
|
2008-11-03 21:49:03 +00:00
|
|
|
* This class is the return type of Matrix::Map() but can also be used directly.
|
2008-01-03 19:36:32 +00:00
|
|
|
*
|
2008-11-03 21:49:03 +00:00
|
|
|
* \sa Matrix::Map()
|
2008-01-03 19:36:32 +00:00
|
|
|
*/
|
2010-10-25 10:15:22 -04:00
|
|
|
|
|
|
|
|
namespace internal {
|
2010-04-26 16:59:04 +02:00
|
|
|
template<typename PlainObjectType, int MapOptions, typename StrideType>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct traits<Map<PlainObjectType, MapOptions, StrideType> >
|
2010-12-22 17:45:37 -05:00
|
|
|
: public traits<PlainObjectType>
|
2008-03-12 17:17:36 +00:00
|
|
|
{
|
2010-12-22 17:45:37 -05:00
|
|
|
typedef traits<PlainObjectType> TraitsBase;
|
2010-06-21 11:36:00 +02:00
|
|
|
typedef typename PlainObjectType::Index Index;
|
2010-04-18 22:14:55 -04:00
|
|
|
typedef typename PlainObjectType::Scalar Scalar;
|
2008-03-12 17:17:36 +00:00
|
|
|
enum {
|
2010-06-11 07:56:50 -04:00
|
|
|
InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0
|
|
|
|
|
? int(PlainObjectType::InnerStrideAtCompileTime)
|
|
|
|
|
: int(StrideType::InnerStrideAtCompileTime),
|
|
|
|
|
OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
|
|
|
|
|
? int(PlainObjectType::OuterStrideAtCompileTime)
|
|
|
|
|
: int(StrideType::OuterStrideAtCompileTime),
|
|
|
|
|
HasNoInnerStride = InnerStrideAtCompileTime == 1,
|
|
|
|
|
HasNoOuterStride = StrideType::OuterStrideAtCompileTime == 0,
|
2010-02-26 20:12:51 -05:00
|
|
|
HasNoStride = HasNoInnerStride && HasNoOuterStride,
|
2010-04-26 16:59:04 +02:00
|
|
|
IsAligned = int(int(MapOptions)&Aligned)==Aligned,
|
2010-04-18 22:14:55 -04:00
|
|
|
IsDynamicSize = PlainObjectType::SizeAtCompileTime==Dynamic,
|
2010-02-26 20:12:51 -05:00
|
|
|
KeepsPacketAccess = bool(HasNoInnerStride)
|
|
|
|
|
&& ( bool(IsDynamicSize)
|
|
|
|
|
|| HasNoOuterStride
|
2010-04-16 10:13:32 -04:00
|
|
|
|| ( OuterStrideAtCompileTime!=Dynamic
|
2010-06-12 13:24:02 +02:00
|
|
|
&& ((static_cast<int>(sizeof(Scalar))*OuterStrideAtCompileTime)%16)==0 ) ),
|
2010-12-10 02:09:58 -05:00
|
|
|
Flags0 = TraitsBase::Flags,
|
2010-07-23 16:29:29 +02:00
|
|
|
Flags1 = IsAligned ? (int(Flags0) | AlignedBit) : (int(Flags0) & ~AlignedBit),
|
2010-12-10 02:09:58 -05:00
|
|
|
Flags2 = (bool(HasNoStride) || bool(PlainObjectType::IsVectorAtCompileTime))
|
|
|
|
|
? int(Flags1) : int(Flags1 & ~LinearAccessBit),
|
2010-12-22 17:45:37 -05:00
|
|
|
Flags3 = is_lvalue<PlainObjectType>::value ? int(Flags2) : (int(Flags2) & ~LvalueBit),
|
2010-12-10 02:09:58 -05:00
|
|
|
Flags = KeepsPacketAccess ? int(Flags3) : (int(Flags3) & ~PacketAccessBit)
|
2010-02-25 21:01:52 -05:00
|
|
|
};
|
2010-04-26 16:59:04 +02:00
|
|
|
private:
|
2010-12-10 02:09:58 -05:00
|
|
|
enum { Options }; // Expressions don't have Options
|
2008-03-12 17:17:36 +00:00
|
|
|
};
|
2010-10-25 10:15:22 -04:00
|
|
|
}
|
2008-03-10 17:23:11 +00:00
|
|
|
|
2010-04-26 16:59:04 +02:00
|
|
|
template<typename PlainObjectType, int MapOptions, typename StrideType> class Map
|
|
|
|
|
: public MapBase<Map<PlainObjectType, MapOptions, StrideType> >
|
2007-10-14 14:45:31 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2008-03-12 17:17:36 +00:00
|
|
|
|
2010-04-16 10:13:32 -04:00
|
|
|
typedef MapBase<Map> Base;
|
2010-02-18 20:42:38 -05:00
|
|
|
|
2010-01-22 10:15:41 +01:00
|
|
|
EIGEN_DENSE_PUBLIC_INTERFACE(Map)
|
2008-01-06 16:35:21 +00:00
|
|
|
|
2010-12-10 02:09:58 -05:00
|
|
|
typedef typename Base::PointerType PointerType;
|
2011-01-23 18:22:18 -05:00
|
|
|
#if EIGEN2_SUPPORT_STAGE <= STAGE30_FULL_EIGEN3_API
|
2011-01-21 10:42:19 -05:00
|
|
|
typedef const Scalar* PointerArgType;
|
|
|
|
|
inline PointerType cast_to_pointer_type(PointerArgType ptr) { return const_cast<PointerType>(ptr); }
|
|
|
|
|
#else
|
|
|
|
|
typedef PointerType PointerArgType;
|
|
|
|
|
inline PointerType cast_to_pointer_type(PointerArgType ptr) { return ptr; }
|
|
|
|
|
#endif
|
2010-12-10 02:09:58 -05:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
inline Index innerStride() const
|
2010-02-18 20:42:38 -05:00
|
|
|
{
|
|
|
|
|
return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
|
|
|
|
|
}
|
2008-07-19 00:09:01 +00:00
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
inline Index outerStride() const
|
2010-02-18 20:42:38 -05:00
|
|
|
{
|
|
|
|
|
return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
|
|
|
|
|
: IsVectorAtCompileTime ? this->size()
|
|
|
|
|
: int(Flags)&RowMajorBit ? this->cols()
|
|
|
|
|
: this->rows();
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-26 21:29:04 -05:00
|
|
|
/** Constructor in the fixed-size case.
|
|
|
|
|
*
|
|
|
|
|
* \param data pointer to the array to map
|
|
|
|
|
* \param stride optional Stride object, passing the strides.
|
|
|
|
|
*/
|
2011-01-21 10:42:19 -05:00
|
|
|
inline Map(PointerArgType data, const StrideType& stride = StrideType())
|
|
|
|
|
: Base(cast_to_pointer_type(data)), m_stride(stride)
|
2010-03-09 00:16:07 -05:00
|
|
|
{
|
2010-04-18 22:14:55 -04:00
|
|
|
PlainObjectType::Base::_check_template_params();
|
2010-03-09 00:16:07 -05:00
|
|
|
}
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2010-02-26 21:29:04 -05:00
|
|
|
/** Constructor in the dynamic-size vector case.
|
|
|
|
|
*
|
|
|
|
|
* \param data pointer to the array to map
|
|
|
|
|
* \param size the size of the vector expression
|
|
|
|
|
* \param stride optional Stride object, passing the strides.
|
|
|
|
|
*/
|
2011-01-21 10:42:19 -05:00
|
|
|
inline Map(PointerArgType data, Index size, const StrideType& stride = StrideType())
|
|
|
|
|
: Base(cast_to_pointer_type(data), size), m_stride(stride)
|
2010-03-09 00:16:07 -05:00
|
|
|
{
|
2010-04-18 22:14:55 -04:00
|
|
|
PlainObjectType::Base::_check_template_params();
|
2010-03-09 00:16:07 -05:00
|
|
|
}
|
2008-06-26 16:06:41 +00:00
|
|
|
|
2010-02-26 21:29:04 -05:00
|
|
|
/** Constructor in the dynamic-size matrix case.
|
|
|
|
|
*
|
|
|
|
|
* \param data pointer to the array to map
|
|
|
|
|
* \param rows the number of rows of the matrix expression
|
|
|
|
|
* \param cols the number of columns of the matrix expression
|
|
|
|
|
* \param stride optional Stride object, passing the strides.
|
|
|
|
|
*/
|
2011-01-21 10:42:19 -05:00
|
|
|
inline Map(PointerArgType data, Index rows, Index cols, const StrideType& stride = StrideType())
|
|
|
|
|
: Base(cast_to_pointer_type(data), rows, cols), m_stride(stride)
|
2010-03-09 00:16:07 -05:00
|
|
|
{
|
2010-04-18 22:14:55 -04:00
|
|
|
PlainObjectType::Base::_check_template_params();
|
2010-03-09 00:16:07 -05:00
|
|
|
}
|
|
|
|
|
|
2009-03-09 19:23:31 +00:00
|
|
|
|
2008-06-27 01:22:35 +00:00
|
|
|
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
|
2010-02-18 20:42:38 -05:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
StrideType m_stride;
|
2008-06-27 01:22:35 +00:00
|
|
|
};
|
2007-12-19 08:14:00 +00:00
|
|
|
|
2010-12-15 15:19:51 +01:00
|
|
|
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
|
|
|
|
|
inline Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
|
|
|
|
|
::Array(const Scalar *data)
|
|
|
|
|
{
|
|
|
|
|
_set_noalias(Eigen::Map<const Array>(data));
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-19 02:12:23 -04:00
|
|
|
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
|
|
|
|
|
inline Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
|
2007-12-18 15:29:28 +00:00
|
|
|
::Matrix(const Scalar *data)
|
|
|
|
|
{
|
2010-12-15 15:19:51 +01:00
|
|
|
_set_noalias(Eigen::Map<const Matrix>(data));
|
2007-10-14 14:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
2007-12-19 09:30:53 +00:00
|
|
|
#endif // EIGEN_MAP_H
|