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