aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Core/Map.h
blob: cbb1633adcd8de26c2017f08fbf27d3c8190c215 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// 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
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.

#ifndef EIGEN_MAP_H
#define EIGEN_MAP_H

/** \class Map
  *
  * \brief A matrix or vector expression mapping an existing array of data.
  *
  * 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,
  * such as plain C arrays or structures from other libraries.
  *
  * This class is the return type of Matrix::map() and most of the time this is the only
  * way it is used.
  *
  * \sa Matrix::map()
  */
template<typename MatrixType>
struct ei_traits<Map<MatrixType> >
{
  typedef typename MatrixType::Scalar Scalar;
  enum {
    RowsAtCompileTime = MatrixType::RowsAtCompileTime,
    ColsAtCompileTime = MatrixType::ColsAtCompileTime,
    MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
    MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
    Flags = MatrixType::Flags,
    CoeffReadCost = NumTraits<Scalar>::ReadCost
  };
};

template<typename MatrixType> class Map
  : public MatrixBase<Map<MatrixType> >
{
  public:

    EIGEN_GENERIC_PUBLIC_INTERFACE(Map)

  private:

    int _rows() const { return m_rows; }
    int _cols() const { return m_cols; }

    const Scalar& _coeff(int row, int col) const
    {
      if(Flags & RowMajorBit)
        return m_data[col + row * m_cols];
      else // column-major
        return m_data[row + col * m_rows];
    }

    Scalar& _coeffRef(int row, int col)
    {
      if(Flags & RowMajorBit)
        return const_cast<Scalar*>(m_data)[col + row * m_cols];
      else // column-major
        return const_cast<Scalar*>(m_data)[row + col * m_rows];
    }

  public:
    Map(const Scalar* data, int rows, int cols) : m_data(data), m_rows(rows), m_cols(cols)
    {
      ei_assert(rows > 0
          && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
          && cols > 0
          && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
    }

    EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)

  protected:
    const Scalar* m_data;
    const int m_rows, m_cols;
};

/** This is the const version of map(Scalar*,int,int). */
template<typename _Scalar, int _Rows, int _Cols, unsigned int _Flags, int _MaxRows, int _MaxCols>
const Map<Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols> >
Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols>::map(const Scalar* data, int rows, int cols)
{
  return Map<Matrix>(data, rows, cols);
}

/** This is the const version of map(Scalar*,int). */
template<typename _Scalar, int _Rows, int _Cols, unsigned int _Flags, int _MaxRows, int _MaxCols>
const Map<Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols> >
Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols>::map(const Scalar* data, int size)
{
  ei_assert(_Cols == 1 || _Rows ==1);
  if(_Cols == 1)
    return Map<Matrix>(data, size, 1);
  else
    return Map<Matrix>(data, 1, size);
}

/** This is the const version of map(Scalar*). */
template<typename _Scalar, int _Rows, int _Cols, unsigned int _Flags, int _MaxRows, int _MaxCols>
const Map<Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols> >
Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols>::map(const Scalar* data)
{
  return Map<Matrix>(data, _Rows, _Cols);
}

/** \returns a expression of a matrix or vector mapping the given data.
  *
  * \param data The array of data to map
  * \param rows The number of rows of the expression to construct
  * \param cols The number of columns of the expression to construct
  *
  * Example: \include MatrixBase_map_int_int.cpp
  * Output: \verbinclude MatrixBase_map_int_int.out
  *
  * \sa map(const Scalar*, int, int), map(Scalar*, int), map(Scalar*), class Map
  */
template<typename _Scalar, int _Rows, int _Cols, unsigned int _Flags, int _MaxRows, int _MaxCols>
Map<Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols> >
Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols>::map(Scalar* data, int rows, int cols)
{
  return Map<Matrix>(data, rows, cols);
}

/** \returns a expression of a vector mapping the given data.
  *
  * \param data The array of data to map
  * \param size The size (number of coefficients) of the expression to construct
  *
  * \only_for_vectors
  *
  * Example: \include MatrixBase_map_int.cpp
  * Output: \verbinclude MatrixBase_map_int.out
  *
  * \sa map(const Scalar*, int), map(Scalar*, int, int), map(Scalar*), class Map
  */
template<typename _Scalar, int _Rows, int _Cols, unsigned int _Flags, int _MaxRows, int _MaxCols>
Map<Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols> >
Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols>::map(Scalar* data, int size)
{
  ei_assert(_Cols == 1 || _Rows ==1);
  if(_Cols == 1)
    return Map<Matrix>(data, size, 1);
  else
    return Map<Matrix>(data, 1, size);
}

/** \returns a expression of a fixed-size matrix or vector mapping the given data.
  *
  * \param data The array of data to map
  *
  * Example: \include MatrixBase_map.cpp
  * Output: \verbinclude MatrixBase_map.out
  *
  * \sa map(const Scalar*), map(Scalar*, int), map(Scalar*, int, int), class Map
  */
template<typename _Scalar, int _Rows, int _Cols, unsigned int _Flags, int _MaxRows, int _MaxCols>
Map<Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols> >
Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols>::map(Scalar* data)
{
  return Map<Matrix>(data, _Rows, _Cols);
}

/** Constructor copying an existing array of data. Only useful for dynamic-size matrices:
  * for fixed-size matrices, it is redundant to pass the \a rows and \a cols parameters.
  * \param data The array of data to copy
  * \param rows The number of rows of the matrix to construct
  * \param cols The number of columns of the matrix to construct
  *
  * \sa Matrix(const Scalar *), Matrix::map(const Scalar *, int, int)
  */
template<typename _Scalar, int _Rows, int _Cols, unsigned int _Flags, int _MaxRows, int _MaxCols>
Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols>
  ::Matrix(const Scalar *data, int rows, int cols)
  : m_storage(rows*cols, rows, cols)
{
  *this = map(data, rows, cols);
}

/** Constructor copying an existing array of data. Only useful for dynamic-size vectors:
  * for fixed-size vectors, it is redundant to pass the \a size parameter.
  *
  * \only_for_vectors
  *
  * \param data The array of data to copy
  * \param size The size of the vector to construct
  *
  * \sa Matrix(const Scalar *), Matrix::map(const Scalar *, int)
  */
template<typename _Scalar, int _Rows, int _Cols, unsigned int _Flags, int _MaxRows, int _MaxCols>
Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols>
  ::Matrix(const Scalar *data, int size)
  : m_storage(size, RowsAtCompileTime == 1 ? 1 : size, ColsAtCompileTime == 1 ? 1 : size)
{
  *this = map(data, size);
}

/** Constructor copying an existing array of data.
  * Only for fixed-size matrices and vectors.
  * \param data The array of data to copy
  *
  * For dynamic-size matrices and vectors, see the variants taking additional int parameters
  * for the dimensions.
  *
  * \sa Matrix(const Scalar *, int), Matrix(const Scalar *, int, int),
  * Matrix::map(const Scalar *)
  */
template<typename _Scalar, int _Rows, int _Cols, unsigned int _Flags, int _MaxRows, int _MaxCols>
Matrix<_Scalar, _Rows, _Cols, _Flags, _MaxRows, _MaxCols>
  ::Matrix(const Scalar *data)
{
  *this = map(data);
}

#endif // EIGEN_MAP_H