/src/Geometry_Eigen/Eigen/src/Core/PlainObjectBase.h

http://github.com/Akranar/daguerreo · C Header · 740 lines · 464 code · 79 blank · 197 comment · 97 complexity · 0d278da9df0715e02c7a7070009322ee MD5 · raw file

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  6. //
  7. // Eigen is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU Lesser General Public
  9. // License as published by the Free Software Foundation; either
  10. // version 3 of the License, or (at your option) any later version.
  11. //
  12. // Alternatively, you can redistribute it and/or
  13. // modify it under the terms of the GNU General Public License as
  14. // published by the Free Software Foundation; either version 2 of
  15. // the License, or (at your option) any later version.
  16. //
  17. // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
  18. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU Lesser General Public
  23. // License and a copy of the GNU General Public License along with
  24. // Eigen. If not, see <http://www.gnu.org/licenses/>.
  25. #ifndef EIGEN_DENSESTORAGEBASE_H
  26. #define EIGEN_DENSESTORAGEBASE_H
  27. #ifdef EIGEN_INITIALIZE_MATRICES_BY_ZERO
  28. # define EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED for(int i=0;i<base().size();++i) coeffRef(i)=Scalar(0);
  29. #else
  30. # define EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
  31. #endif
  32. namespace internal {
  33. template <typename Derived, typename OtherDerived = Derived, bool IsVector = static_cast<bool>(Derived::IsVectorAtCompileTime)> struct conservative_resize_like_impl;
  34. template<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers> struct matrix_swap_impl;
  35. } // end namespace internal
  36. /**
  37. * \brief %Dense storage base class for matrices and arrays.
  38. *
  39. * This class can be extended with the help of the plugin mechanism described on the page
  40. * \ref TopicCustomizingEigen by defining the preprocessor symbol \c EIGEN_PLAINOBJECTBASE_PLUGIN.
  41. *
  42. * \sa \ref TopicClassHierarchy
  43. */
  44. template<typename Derived>
  45. class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
  46. {
  47. public:
  48. enum { Options = internal::traits<Derived>::Options };
  49. typedef typename internal::dense_xpr_base<Derived>::type Base;
  50. typedef typename internal::traits<Derived>::StorageKind StorageKind;
  51. typedef typename internal::traits<Derived>::Index Index;
  52. typedef typename internal::traits<Derived>::Scalar Scalar;
  53. typedef typename internal::packet_traits<Scalar>::type PacketScalar;
  54. typedef typename NumTraits<Scalar>::Real RealScalar;
  55. typedef Derived DenseType;
  56. using Base::RowsAtCompileTime;
  57. using Base::ColsAtCompileTime;
  58. using Base::SizeAtCompileTime;
  59. using Base::MaxRowsAtCompileTime;
  60. using Base::MaxColsAtCompileTime;
  61. using Base::MaxSizeAtCompileTime;
  62. using Base::IsVectorAtCompileTime;
  63. using Base::Flags;
  64. template<typename PlainObjectType, int MapOptions, typename StrideType> friend class Eigen::Map;
  65. friend class Eigen::Map<Derived, Unaligned>;
  66. typedef Eigen::Map<Derived, Unaligned> MapType;
  67. friend class Eigen::Map<const Derived, Unaligned>;
  68. typedef const Eigen::Map<const Derived, Unaligned> ConstMapType;
  69. friend class Eigen::Map<Derived, Aligned>;
  70. typedef Eigen::Map<Derived, Aligned> AlignedMapType;
  71. friend class Eigen::Map<const Derived, Aligned>;
  72. typedef const Eigen::Map<const Derived, Aligned> ConstAlignedMapType;
  73. template<typename StrideType> struct StridedMapType { typedef Eigen::Map<Derived, Unaligned, StrideType> type; };
  74. template<typename StrideType> struct StridedConstMapType { typedef Eigen::Map<const Derived, Unaligned, StrideType> type; };
  75. template<typename StrideType> struct StridedAlignedMapType { typedef Eigen::Map<Derived, Aligned, StrideType> type; };
  76. template<typename StrideType> struct StridedConstAlignedMapType { typedef Eigen::Map<const Derived, Aligned, StrideType> type; };
  77. protected:
  78. DenseStorage<Scalar, Base::MaxSizeAtCompileTime, Base::RowsAtCompileTime, Base::ColsAtCompileTime, Options> m_storage;
  79. public:
  80. enum { NeedsToAlign = (!(Options&DontAlign))
  81. && SizeAtCompileTime!=Dynamic && ((static_cast<int>(sizeof(Scalar))*SizeAtCompileTime)%16)==0 };
  82. EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
  83. Base& base() { return *static_cast<Base*>(this); }
  84. const Base& base() const { return *static_cast<const Base*>(this); }
  85. EIGEN_STRONG_INLINE Index rows() const { return m_storage.rows(); }
  86. EIGEN_STRONG_INLINE Index cols() const { return m_storage.cols(); }
  87. EIGEN_STRONG_INLINE const Scalar& coeff(Index row, Index col) const
  88. {
  89. if(Flags & RowMajorBit)
  90. return m_storage.data()[col + row * m_storage.cols()];
  91. else // column-major
  92. return m_storage.data()[row + col * m_storage.rows()];
  93. }
  94. EIGEN_STRONG_INLINE const Scalar& coeff(Index index) const
  95. {
  96. return m_storage.data()[index];
  97. }
  98. EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col)
  99. {
  100. if(Flags & RowMajorBit)
  101. return m_storage.data()[col + row * m_storage.cols()];
  102. else // column-major
  103. return m_storage.data()[row + col * m_storage.rows()];
  104. }
  105. EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
  106. {
  107. return m_storage.data()[index];
  108. }
  109. EIGEN_STRONG_INLINE const Scalar& coeffRef(Index row, Index col) const
  110. {
  111. if(Flags & RowMajorBit)
  112. return m_storage.data()[col + row * m_storage.cols()];
  113. else // column-major
  114. return m_storage.data()[row + col * m_storage.rows()];
  115. }
  116. EIGEN_STRONG_INLINE const Scalar& coeffRef(Index index) const
  117. {
  118. return m_storage.data()[index];
  119. }
  120. /** \internal */
  121. template<int LoadMode>
  122. EIGEN_STRONG_INLINE PacketScalar packet(Index row, Index col) const
  123. {
  124. return internal::ploadt<PacketScalar, LoadMode>
  125. (m_storage.data() + (Flags & RowMajorBit
  126. ? col + row * m_storage.cols()
  127. : row + col * m_storage.rows()));
  128. }
  129. /** \internal */
  130. template<int LoadMode>
  131. EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
  132. {
  133. return internal::ploadt<PacketScalar, LoadMode>(m_storage.data() + index);
  134. }
  135. /** \internal */
  136. template<int StoreMode>
  137. EIGEN_STRONG_INLINE void writePacket(Index row, Index col, const PacketScalar& x)
  138. {
  139. internal::pstoret<Scalar, PacketScalar, StoreMode>
  140. (m_storage.data() + (Flags & RowMajorBit
  141. ? col + row * m_storage.cols()
  142. : row + col * m_storage.rows()), x);
  143. }
  144. /** \internal */
  145. template<int StoreMode>
  146. EIGEN_STRONG_INLINE void writePacket(Index index, const PacketScalar& x)
  147. {
  148. internal::pstoret<Scalar, PacketScalar, StoreMode>(m_storage.data() + index, x);
  149. }
  150. /** \returns a const pointer to the data array of this matrix */
  151. EIGEN_STRONG_INLINE const Scalar *data() const
  152. { return m_storage.data(); }
  153. /** \returns a pointer to the data array of this matrix */
  154. EIGEN_STRONG_INLINE Scalar *data()
  155. { return m_storage.data(); }
  156. /** Resizes \c *this to a \a rows x \a cols matrix.
  157. *
  158. * This method is intended for dynamic-size matrices, although it is legal to call it on any
  159. * matrix as long as fixed dimensions are left unchanged. If you only want to change the number
  160. * of rows and/or of columns, you can use resize(NoChange_t, Index), resize(Index, NoChange_t).
  161. *
  162. * If the current number of coefficients of \c *this exactly matches the
  163. * product \a rows * \a cols, then no memory allocation is performed and
  164. * the current values are left unchanged. In all other cases, including
  165. * shrinking, the data is reallocated and all previous values are lost.
  166. *
  167. * Example: \include Matrix_resize_int_int.cpp
  168. * Output: \verbinclude Matrix_resize_int_int.out
  169. *
  170. * \sa resize(Index) for vectors, resize(NoChange_t, Index), resize(Index, NoChange_t)
  171. */
  172. EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
  173. {
  174. #ifdef EIGEN_INITIALIZE_MATRICES_BY_ZERO
  175. Index size = rows*cols;
  176. bool size_changed = size != this->size();
  177. m_storage.resize(size, rows, cols);
  178. if(size_changed) EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
  179. #else
  180. m_storage.resize(rows*cols, rows, cols);
  181. #endif
  182. }
  183. /** Resizes \c *this to a vector of length \a size
  184. *
  185. * \only_for_vectors. This method does not work for
  186. * partially dynamic matrices when the static dimension is anything other
  187. * than 1. For example it will not work with Matrix<double, 2, Dynamic>.
  188. *
  189. * Example: \include Matrix_resize_int.cpp
  190. * Output: \verbinclude Matrix_resize_int.out
  191. *
  192. * \sa resize(Index,Index), resize(NoChange_t, Index), resize(Index, NoChange_t)
  193. */
  194. inline void resize(Index size)
  195. {
  196. EIGEN_STATIC_ASSERT_VECTOR_ONLY(PlainObjectBase)
  197. eigen_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == size);
  198. #ifdef EIGEN_INITIALIZE_MATRICES_BY_ZERO
  199. bool size_changed = size != this->size();
  200. #endif
  201. if(RowsAtCompileTime == 1)
  202. m_storage.resize(size, 1, size);
  203. else
  204. m_storage.resize(size, size, 1);
  205. #ifdef EIGEN_INITIALIZE_MATRICES_BY_ZERO
  206. if(size_changed) EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
  207. #endif
  208. }
  209. /** Resizes the matrix, changing only the number of columns. For the parameter of type NoChange_t, just pass the special value \c NoChange
  210. * as in the example below.
  211. *
  212. * Example: \include Matrix_resize_NoChange_int.cpp
  213. * Output: \verbinclude Matrix_resize_NoChange_int.out
  214. *
  215. * \sa resize(Index,Index)
  216. */
  217. inline void resize(NoChange_t, Index cols)
  218. {
  219. resize(rows(), cols);
  220. }
  221. /** Resizes the matrix, changing only the number of rows. For the parameter of type NoChange_t, just pass the special value \c NoChange
  222. * as in the example below.
  223. *
  224. * Example: \include Matrix_resize_int_NoChange.cpp
  225. * Output: \verbinclude Matrix_resize_int_NoChange.out
  226. *
  227. * \sa resize(Index,Index)
  228. */
  229. inline void resize(Index rows, NoChange_t)
  230. {
  231. resize(rows, cols());
  232. }
  233. /** Resizes \c *this to have the same dimensions as \a other.
  234. * Takes care of doing all the checking that's needed.
  235. *
  236. * Note that copying a row-vector into a vector (and conversely) is allowed.
  237. * The resizing, if any, is then done in the appropriate way so that row-vectors
  238. * remain row-vectors and vectors remain vectors.
  239. */
  240. template<typename OtherDerived>
  241. EIGEN_STRONG_INLINE void resizeLike(const EigenBase<OtherDerived>& _other)
  242. {
  243. const OtherDerived& other = _other.derived();
  244. const Index othersize = other.rows()*other.cols();
  245. if(RowsAtCompileTime == 1)
  246. {
  247. eigen_assert(other.rows() == 1 || other.cols() == 1);
  248. resize(1, othersize);
  249. }
  250. else if(ColsAtCompileTime == 1)
  251. {
  252. eigen_assert(other.rows() == 1 || other.cols() == 1);
  253. resize(othersize, 1);
  254. }
  255. else resize(other.rows(), other.cols());
  256. }
  257. /** Resizes the matrix to \a rows x \a cols while leaving old values untouched.
  258. *
  259. * The method is intended for matrices of dynamic size. If you only want to change the number
  260. * of rows and/or of columns, you can use conservativeResize(NoChange_t, Index) or
  261. * conservativeResize(Index, NoChange_t).
  262. *
  263. * Matrices are resized relative to the top-left element. In case values need to be
  264. * appended to the matrix they will be uninitialized.
  265. */
  266. EIGEN_STRONG_INLINE void conservativeResize(Index rows, Index cols)
  267. {
  268. internal::conservative_resize_like_impl<Derived>::run(*this, rows, cols);
  269. }
  270. /** Resizes the matrix to \a rows x \a cols while leaving old values untouched.
  271. *
  272. * As opposed to conservativeResize(Index rows, Index cols), this version leaves
  273. * the number of columns unchanged.
  274. *
  275. * In case the matrix is growing, new rows will be uninitialized.
  276. */
  277. EIGEN_STRONG_INLINE void conservativeResize(Index rows, NoChange_t)
  278. {
  279. // Note: see the comment in conservativeResize(Index,Index)
  280. conservativeResize(rows, cols());
  281. }
  282. /** Resizes the matrix to \a rows x \a cols while leaving old values untouched.
  283. *
  284. * As opposed to conservativeResize(Index rows, Index cols), this version leaves
  285. * the number of rows unchanged.
  286. *
  287. * In case the matrix is growing, new columns will be uninitialized.
  288. */
  289. EIGEN_STRONG_INLINE void conservativeResize(NoChange_t, Index cols)
  290. {
  291. // Note: see the comment in conservativeResize(Index,Index)
  292. conservativeResize(rows(), cols);
  293. }
  294. /** Resizes the vector to \a size while retaining old values.
  295. *
  296. * \only_for_vectors. This method does not work for
  297. * partially dynamic matrices when the static dimension is anything other
  298. * than 1. For example it will not work with Matrix<double, 2, Dynamic>.
  299. *
  300. * When values are appended, they will be uninitialized.
  301. */
  302. EIGEN_STRONG_INLINE void conservativeResize(Index size)
  303. {
  304. internal::conservative_resize_like_impl<Derived>::run(*this, size);
  305. }
  306. /** Resizes the matrix to \a rows x \a cols of \c other, while leaving old values untouched.
  307. *
  308. * The method is intended for matrices of dynamic size. If you only want to change the number
  309. * of rows and/or of columns, you can use conservativeResize(NoChange_t, Index) or
  310. * conservativeResize(Index, NoChange_t).
  311. *
  312. * Matrices are resized relative to the top-left element. In case values need to be
  313. * appended to the matrix they will copied from \c other.
  314. */
  315. template<typename OtherDerived>
  316. EIGEN_STRONG_INLINE void conservativeResizeLike(const DenseBase<OtherDerived>& other)
  317. {
  318. internal::conservative_resize_like_impl<Derived,OtherDerived>::run(*this, other);
  319. }
  320. /** This is a special case of the templated operator=. Its purpose is to
  321. * prevent a default operator= from hiding the templated operator=.
  322. */
  323. EIGEN_STRONG_INLINE Derived& operator=(const PlainObjectBase& other)
  324. {
  325. return _set(other);
  326. }
  327. /** \sa MatrixBase::lazyAssign() */
  328. template<typename OtherDerived>
  329. EIGEN_STRONG_INLINE Derived& lazyAssign(const DenseBase<OtherDerived>& other)
  330. {
  331. _resize_to_match(other);
  332. return Base::lazyAssign(other.derived());
  333. }
  334. template<typename OtherDerived>
  335. EIGEN_STRONG_INLINE Derived& operator=(const ReturnByValue<OtherDerived>& func)
  336. {
  337. resize(func.rows(), func.cols());
  338. return Base::operator=(func);
  339. }
  340. EIGEN_STRONG_INLINE explicit PlainObjectBase() : m_storage()
  341. {
  342. // _check_template_params();
  343. // EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
  344. }
  345. #ifndef EIGEN_PARSED_BY_DOXYGEN
  346. // FIXME is it still needed ?
  347. /** \internal */
  348. PlainObjectBase(internal::constructor_without_unaligned_array_assert)
  349. : m_storage(internal::constructor_without_unaligned_array_assert())
  350. {
  351. // _check_template_params(); EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
  352. }
  353. #endif
  354. EIGEN_STRONG_INLINE PlainObjectBase(Index size, Index rows, Index cols)
  355. : m_storage(size, rows, cols)
  356. {
  357. // _check_template_params();
  358. // EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
  359. }
  360. /** \copydoc MatrixBase::operator=(const EigenBase<OtherDerived>&)
  361. */
  362. template<typename OtherDerived>
  363. EIGEN_STRONG_INLINE Derived& operator=(const EigenBase<OtherDerived> &other)
  364. {
  365. _resize_to_match(other);
  366. Base::operator=(other.derived());
  367. return this->derived();
  368. }
  369. /** \sa MatrixBase::operator=(const EigenBase<OtherDerived>&) */
  370. template<typename OtherDerived>
  371. EIGEN_STRONG_INLINE PlainObjectBase(const EigenBase<OtherDerived> &other)
  372. : m_storage(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
  373. {
  374. _check_template_params();
  375. Base::operator=(other.derived());
  376. }
  377. /** \name Map
  378. * These are convenience functions returning Map objects. The Map() static functions return unaligned Map objects,
  379. * while the AlignedMap() functions return aligned Map objects and thus should be called only with 16-byte-aligned
  380. * \a data pointers.
  381. *
  382. * These methods do not allow to specify strides. If you need to specify strides, you have to
  383. * use the Map class directly.
  384. *
  385. * \see class Map
  386. */
  387. //@{
  388. inline static ConstMapType Map(const Scalar* data)
  389. { return ConstMapType(data); }
  390. inline static MapType Map(Scalar* data)
  391. { return MapType(data); }
  392. inline static ConstMapType Map(const Scalar* data, Index size)
  393. { return ConstMapType(data, size); }
  394. inline static MapType Map(Scalar* data, Index size)
  395. { return MapType(data, size); }
  396. inline static ConstMapType Map(const Scalar* data, Index rows, Index cols)
  397. { return ConstMapType(data, rows, cols); }
  398. inline static MapType Map(Scalar* data, Index rows, Index cols)
  399. { return MapType(data, rows, cols); }
  400. inline static ConstAlignedMapType MapAligned(const Scalar* data)
  401. { return ConstAlignedMapType(data); }
  402. inline static AlignedMapType MapAligned(Scalar* data)
  403. { return AlignedMapType(data); }
  404. inline static ConstAlignedMapType MapAligned(const Scalar* data, Index size)
  405. { return ConstAlignedMapType(data, size); }
  406. inline static AlignedMapType MapAligned(Scalar* data, Index size)
  407. { return AlignedMapType(data, size); }
  408. inline static ConstAlignedMapType MapAligned(const Scalar* data, Index rows, Index cols)
  409. { return ConstAlignedMapType(data, rows, cols); }
  410. inline static AlignedMapType MapAligned(Scalar* data, Index rows, Index cols)
  411. { return AlignedMapType(data, rows, cols); }
  412. template<int Outer, int Inner>
  413. inline static typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, const Stride<Outer, Inner>& stride)
  414. { return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, stride); }
  415. template<int Outer, int Inner>
  416. inline static typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, const Stride<Outer, Inner>& stride)
  417. { return typename StridedMapType<Stride<Outer, Inner> >::type(data, stride); }
  418. template<int Outer, int Inner>
  419. inline static typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, Index size, const Stride<Outer, Inner>& stride)
  420. { return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, size, stride); }
  421. template<int Outer, int Inner>
  422. inline static typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, Index size, const Stride<Outer, Inner>& stride)
  423. { return typename StridedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
  424. template<int Outer, int Inner>
  425. inline static typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
  426. { return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
  427. template<int Outer, int Inner>
  428. inline static typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
  429. { return typename StridedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
  430. template<int Outer, int Inner>
  431. inline static typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, const Stride<Outer, Inner>& stride)
  432. { return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, stride); }
  433. template<int Outer, int Inner>
  434. inline static typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, const Stride<Outer, Inner>& stride)
  435. { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, stride); }
  436. template<int Outer, int Inner>
  437. inline static typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, Index size, const Stride<Outer, Inner>& stride)
  438. { return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
  439. template<int Outer, int Inner>
  440. inline static typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index size, const Stride<Outer, Inner>& stride)
  441. { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
  442. template<int Outer, int Inner>
  443. inline static typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
  444. { return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
  445. template<int Outer, int Inner>
  446. inline static typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
  447. { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
  448. //@}
  449. using Base::setConstant;
  450. Derived& setConstant(Index size, const Scalar& value);
  451. Derived& setConstant(Index rows, Index cols, const Scalar& value);
  452. using Base::setZero;
  453. Derived& setZero(Index size);
  454. Derived& setZero(Index rows, Index cols);
  455. using Base::setOnes;
  456. Derived& setOnes(Index size);
  457. Derived& setOnes(Index rows, Index cols);
  458. using Base::setRandom;
  459. Derived& setRandom(Index size);
  460. Derived& setRandom(Index rows, Index cols);
  461. #ifdef EIGEN_PLAINOBJECTBASE_PLUGIN
  462. #include EIGEN_PLAINOBJECTBASE_PLUGIN
  463. #endif
  464. protected:
  465. /** \internal Resizes *this in preparation for assigning \a other to it.
  466. * Takes care of doing all the checking that's needed.
  467. *
  468. * Note that copying a row-vector into a vector (and conversely) is allowed.
  469. * The resizing, if any, is then done in the appropriate way so that row-vectors
  470. * remain row-vectors and vectors remain vectors.
  471. */
  472. template<typename OtherDerived>
  473. EIGEN_STRONG_INLINE void _resize_to_match(const EigenBase<OtherDerived>& other)
  474. {
  475. #ifdef EIGEN_NO_AUTOMATIC_RESIZING
  476. eigen_assert((this->size()==0 || (IsVectorAtCompileTime ? (this->size() == other.size())
  477. : (rows() == other.rows() && cols() == other.cols())))
  478. && "Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
  479. #else
  480. resizeLike(other);
  481. #endif
  482. }
  483. /**
  484. * \brief Copies the value of the expression \a other into \c *this with automatic resizing.
  485. *
  486. * *this might be resized to match the dimensions of \a other. If *this was a null matrix (not already initialized),
  487. * it will be initialized.
  488. *
  489. * Note that copying a row-vector into a vector (and conversely) is allowed.
  490. * The resizing, if any, is then done in the appropriate way so that row-vectors
  491. * remain row-vectors and vectors remain vectors.
  492. *
  493. * \sa operator=(const MatrixBase<OtherDerived>&), _set_noalias()
  494. *
  495. * \internal
  496. */
  497. template<typename OtherDerived>
  498. EIGEN_STRONG_INLINE Derived& _set(const DenseBase<OtherDerived>& other)
  499. {
  500. _set_selector(other.derived(), typename internal::conditional<static_cast<bool>(int(OtherDerived::Flags) & EvalBeforeAssigningBit), internal::true_type, internal::false_type>::type());
  501. return this->derived();
  502. }
  503. template<typename OtherDerived>
  504. EIGEN_STRONG_INLINE void _set_selector(const OtherDerived& other, const internal::true_type&) { _set_noalias(other.eval()); }
  505. template<typename OtherDerived>
  506. EIGEN_STRONG_INLINE void _set_selector(const OtherDerived& other, const internal::false_type&) { _set_noalias(other); }
  507. /** \internal Like _set() but additionally makes the assumption that no aliasing effect can happen (which
  508. * is the case when creating a new matrix) so one can enforce lazy evaluation.
  509. *
  510. * \sa operator=(const MatrixBase<OtherDerived>&), _set()
  511. */
  512. template<typename OtherDerived>
  513. EIGEN_STRONG_INLINE Derived& _set_noalias(const DenseBase<OtherDerived>& other)
  514. {
  515. // I don't think we need this resize call since the lazyAssign will anyways resize
  516. // and lazyAssign will be called by the assign selector.
  517. //_resize_to_match(other);
  518. // the 'false' below means to enforce lazy evaluation. We don't use lazyAssign() because
  519. // it wouldn't allow to copy a row-vector into a column-vector.
  520. return internal::assign_selector<Derived,OtherDerived,false>::run(this->derived(), other.derived());
  521. }
  522. template<typename T0, typename T1>
  523. EIGEN_STRONG_INLINE void _init2(Index rows, Index cols, typename internal::enable_if<Base::SizeAtCompileTime!=2,T0>::type* = 0)
  524. {
  525. eigen_assert(rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
  526. && cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
  527. m_storage.resize(rows*cols,rows,cols);
  528. EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
  529. }
  530. template<typename T0, typename T1>
  531. EIGEN_STRONG_INLINE void _init2(const Scalar& x, const Scalar& y, typename internal::enable_if<Base::SizeAtCompileTime==2,T0>::type* = 0)
  532. {
  533. EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(PlainObjectBase, 2)
  534. m_storage.data()[0] = x;
  535. m_storage.data()[1] = y;
  536. }
  537. template<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers>
  538. friend struct internal::matrix_swap_impl;
  539. /** \internal generic implementation of swap for dense storage since for dynamic-sized matrices of same type it is enough to swap the
  540. * data pointers.
  541. */
  542. template<typename OtherDerived>
  543. void _swap(DenseBase<OtherDerived> const & other)
  544. {
  545. enum { SwapPointers = internal::is_same<Derived, OtherDerived>::value && Base::SizeAtCompileTime==Dynamic };
  546. internal::matrix_swap_impl<Derived, OtherDerived, bool(SwapPointers)>::run(this->derived(), other.const_cast_derived());
  547. }
  548. public:
  549. #ifndef EIGEN_PARSED_BY_DOXYGEN
  550. EIGEN_STRONG_INLINE static void _check_template_params()
  551. {
  552. EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
  553. && EIGEN_IMPLIES(MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1, (Options&RowMajor)==0)
  554. && ((RowsAtCompileTime == Dynamic) || (RowsAtCompileTime >= 0))
  555. && ((ColsAtCompileTime == Dynamic) || (ColsAtCompileTime >= 0))
  556. && ((MaxRowsAtCompileTime == Dynamic) || (MaxRowsAtCompileTime >= 0))
  557. && ((MaxColsAtCompileTime == Dynamic) || (MaxColsAtCompileTime >= 0))
  558. && (MaxRowsAtCompileTime == RowsAtCompileTime || RowsAtCompileTime==Dynamic)
  559. && (MaxColsAtCompileTime == ColsAtCompileTime || ColsAtCompileTime==Dynamic)
  560. && (Options & (DontAlign|RowMajor)) == Options),
  561. INVALID_MATRIX_TEMPLATE_PARAMETERS)
  562. }
  563. #endif
  564. private:
  565. enum { ThisConstantIsPrivateInPlainObjectBase };
  566. };
  567. template <typename Derived, typename OtherDerived, bool IsVector>
  568. struct internal::conservative_resize_like_impl
  569. {
  570. typedef typename Derived::Index Index;
  571. static void run(DenseBase<Derived>& _this, Index rows, Index cols)
  572. {
  573. if (_this.rows() == rows && _this.cols() == cols) return;
  574. EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(Derived)
  575. if ( ( Derived::IsRowMajor && _this.cols() == cols) || // row-major and we change only the number of rows
  576. (!Derived::IsRowMajor && _this.rows() == rows) ) // column-major and we change only the number of columns
  577. {
  578. _this.derived().m_storage.conservativeResize(rows*cols,rows,cols);
  579. }
  580. else
  581. {
  582. // The storage order does not allow us to use reallocation.
  583. typename Derived::PlainObject tmp(rows,cols);
  584. const Index common_rows = (std::min)(rows, _this.rows());
  585. const Index common_cols = (std::min)(cols, _this.cols());
  586. tmp.block(0,0,common_rows,common_cols) = _this.block(0,0,common_rows,common_cols);
  587. _this.derived().swap(tmp);
  588. }
  589. }
  590. static void run(DenseBase<Derived>& _this, const DenseBase<OtherDerived>& other)
  591. {
  592. if (_this.rows() == other.rows() && _this.cols() == other.cols()) return;
  593. // Note: Here is space for improvement. Basically, for conservativeResize(Index,Index),
  594. // neither RowsAtCompileTime or ColsAtCompileTime must be Dynamic. If only one of the
  595. // dimensions is dynamic, one could use either conservativeResize(Index rows, NoChange_t) or
  596. // conservativeResize(NoChange_t, Index cols). For these methods new static asserts like
  597. // EIGEN_STATIC_ASSERT_DYNAMIC_ROWS and EIGEN_STATIC_ASSERT_DYNAMIC_COLS would be good.
  598. EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(Derived)
  599. EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(OtherDerived)
  600. if ( ( Derived::IsRowMajor && _this.cols() == other.cols()) || // row-major and we change only the number of rows
  601. (!Derived::IsRowMajor && _this.rows() == other.rows()) ) // column-major and we change only the number of columns
  602. {
  603. const Index new_rows = other.rows() - _this.rows();
  604. const Index new_cols = other.cols() - _this.cols();
  605. _this.derived().m_storage.conservativeResize(other.size(),other.rows(),other.cols());
  606. if (new_rows>0)
  607. _this.bottomRightCorner(new_rows, other.cols()) = other.bottomRows(new_rows);
  608. else if (new_cols>0)
  609. _this.bottomRightCorner(other.rows(), new_cols) = other.rightCols(new_cols);
  610. }
  611. else
  612. {
  613. // The storage order does not allow us to use reallocation.
  614. typename Derived::PlainObject tmp(other);
  615. const Index common_rows = (std::min)(tmp.rows(), _this.rows());
  616. const Index common_cols = (std::min)(tmp.cols(), _this.cols());
  617. tmp.block(0,0,common_rows,common_cols) = _this.block(0,0,common_rows,common_cols);
  618. _this.derived().swap(tmp);
  619. }
  620. }
  621. };
  622. namespace internal {
  623. template <typename Derived, typename OtherDerived>
  624. struct conservative_resize_like_impl<Derived,OtherDerived,true>
  625. {
  626. typedef typename Derived::Index Index;
  627. static void run(DenseBase<Derived>& _this, Index size)
  628. {
  629. const Index new_rows = Derived::RowsAtCompileTime==1 ? 1 : size;
  630. const Index new_cols = Derived::RowsAtCompileTime==1 ? size : 1;
  631. _this.derived().m_storage.conservativeResize(size,new_rows,new_cols);
  632. }
  633. static void run(DenseBase<Derived>& _this, const DenseBase<OtherDerived>& other)
  634. {
  635. if (_this.rows() == other.rows() && _this.cols() == other.cols()) return;
  636. const Index num_new_elements = other.size() - _this.size();
  637. const Index new_rows = Derived::RowsAtCompileTime==1 ? 1 : other.rows();
  638. const Index new_cols = Derived::RowsAtCompileTime==1 ? other.cols() : 1;
  639. _this.derived().m_storage.conservativeResize(other.size(),new_rows,new_cols);
  640. if (num_new_elements > 0)
  641. _this.tail(num_new_elements) = other.tail(num_new_elements);
  642. }
  643. };
  644. template<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers>
  645. struct matrix_swap_impl
  646. {
  647. static inline void run(MatrixTypeA& a, MatrixTypeB& b)
  648. {
  649. a.base().swap(b);
  650. }
  651. };
  652. template<typename MatrixTypeA, typename MatrixTypeB>
  653. struct matrix_swap_impl<MatrixTypeA, MatrixTypeB, true>
  654. {
  655. static inline void run(MatrixTypeA& a, MatrixTypeB& b)
  656. {
  657. static_cast<typename MatrixTypeA::Base&>(a).m_storage.swap(static_cast<typename MatrixTypeB::Base&>(b).m_storage);
  658. }
  659. };
  660. } // end namespace internal
  661. #endif // EIGEN_DENSESTORAGEBASE_H