/src/Geometry_Eigen/Eigen/src/Core/Reverse.h

http://github.com/Akranar/daguerreo · C Header · 230 lines · 136 code · 34 blank · 60 comment · 20 complexity · d558fa0acdb9d059178b263a5b103ada MD5 · raw file

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2009 Ricard Marxer <email@ricardmarxer.com>
  6. // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  7. //
  8. // Eigen is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Lesser General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 3 of the License, or (at your option) any later version.
  12. //
  13. // Alternatively, you can redistribute it and/or
  14. // modify it under the terms of the GNU General Public License as
  15. // published by the Free Software Foundation; either version 2 of
  16. // the License, or (at your option) any later version.
  17. //
  18. // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
  19. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
  21. // GNU General Public License for more details.
  22. //
  23. // You should have received a copy of the GNU Lesser General Public
  24. // License and a copy of the GNU General Public License along with
  25. // Eigen. If not, see <http://www.gnu.org/licenses/>.
  26. #ifndef EIGEN_REVERSE_H
  27. #define EIGEN_REVERSE_H
  28. /** \class Reverse
  29. * \ingroup Core_Module
  30. *
  31. * \brief Expression of the reverse of a vector or matrix
  32. *
  33. * \param MatrixType the type of the object of which we are taking the reverse
  34. *
  35. * This class represents an expression of the reverse of a vector.
  36. * It is the return type of MatrixBase::reverse() and VectorwiseOp::reverse()
  37. * and most of the time this is the only way it is used.
  38. *
  39. * \sa MatrixBase::reverse(), VectorwiseOp::reverse()
  40. */
  41. namespace internal {
  42. template<typename MatrixType, int Direction>
  43. struct traits<Reverse<MatrixType, Direction> >
  44. : traits<MatrixType>
  45. {
  46. typedef typename MatrixType::Scalar Scalar;
  47. typedef typename traits<MatrixType>::StorageKind StorageKind;
  48. typedef typename traits<MatrixType>::XprKind XprKind;
  49. typedef typename nested<MatrixType>::type MatrixTypeNested;
  50. typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
  51. enum {
  52. RowsAtCompileTime = MatrixType::RowsAtCompileTime,
  53. ColsAtCompileTime = MatrixType::ColsAtCompileTime,
  54. MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
  55. MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
  56. // let's enable LinearAccess only with vectorization because of the product overhead
  57. LinearAccess = ( (Direction==BothDirections) && (int(_MatrixTypeNested::Flags)&PacketAccessBit) )
  58. ? LinearAccessBit : 0,
  59. Flags = int(_MatrixTypeNested::Flags) & (HereditaryBits | LvalueBit | PacketAccessBit | LinearAccess),
  60. CoeffReadCost = _MatrixTypeNested::CoeffReadCost
  61. };
  62. };
  63. template<typename PacketScalar, bool ReversePacket> struct reverse_packet_cond
  64. {
  65. static inline PacketScalar run(const PacketScalar& x) { return preverse(x); }
  66. };
  67. template<typename PacketScalar> struct reverse_packet_cond<PacketScalar,false>
  68. {
  69. static inline PacketScalar run(const PacketScalar& x) { return x; }
  70. };
  71. } // end namespace internal
  72. template<typename MatrixType, int Direction> class Reverse
  73. : public internal::dense_xpr_base< Reverse<MatrixType, Direction> >::type
  74. {
  75. public:
  76. typedef typename internal::dense_xpr_base<Reverse>::type Base;
  77. EIGEN_DENSE_PUBLIC_INTERFACE(Reverse)
  78. using Base::IsRowMajor;
  79. // next line is necessary because otherwise const version of operator()
  80. // is hidden by non-const version defined in this file
  81. using Base::operator();
  82. protected:
  83. enum {
  84. PacketSize = internal::packet_traits<Scalar>::size,
  85. IsColMajor = !IsRowMajor,
  86. ReverseRow = (Direction == Vertical) || (Direction == BothDirections),
  87. ReverseCol = (Direction == Horizontal) || (Direction == BothDirections),
  88. OffsetRow = ReverseRow && IsColMajor ? PacketSize : 1,
  89. OffsetCol = ReverseCol && IsRowMajor ? PacketSize : 1,
  90. ReversePacket = (Direction == BothDirections)
  91. || ((Direction == Vertical) && IsColMajor)
  92. || ((Direction == Horizontal) && IsRowMajor)
  93. };
  94. typedef internal::reverse_packet_cond<PacketScalar,ReversePacket> reverse_packet;
  95. public:
  96. inline Reverse(const MatrixType& matrix) : m_matrix(matrix) { }
  97. EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Reverse)
  98. inline Index rows() const { return m_matrix.rows(); }
  99. inline Index cols() const { return m_matrix.cols(); }
  100. inline Index innerStride() const
  101. {
  102. return -m_matrix.innerStride();
  103. }
  104. inline Scalar& operator()(Index row, Index col)
  105. {
  106. eigen_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
  107. return coeffRef(row, col);
  108. }
  109. inline Scalar& coeffRef(Index row, Index col)
  110. {
  111. return m_matrix.const_cast_derived().coeffRef(ReverseRow ? m_matrix.rows() - row - 1 : row,
  112. ReverseCol ? m_matrix.cols() - col - 1 : col);
  113. }
  114. inline CoeffReturnType coeff(Index row, Index col) const
  115. {
  116. return m_matrix.coeff(ReverseRow ? m_matrix.rows() - row - 1 : row,
  117. ReverseCol ? m_matrix.cols() - col - 1 : col);
  118. }
  119. inline CoeffReturnType coeff(Index index) const
  120. {
  121. return m_matrix.coeff(m_matrix.size() - index - 1);
  122. }
  123. inline Scalar& coeffRef(Index index)
  124. {
  125. return m_matrix.const_cast_derived().coeffRef(m_matrix.size() - index - 1);
  126. }
  127. inline Scalar& operator()(Index index)
  128. {
  129. eigen_assert(index >= 0 && index < m_matrix.size());
  130. return coeffRef(index);
  131. }
  132. template<int LoadMode>
  133. inline const PacketScalar packet(Index row, Index col) const
  134. {
  135. return reverse_packet::run(m_matrix.template packet<LoadMode>(
  136. ReverseRow ? m_matrix.rows() - row - OffsetRow : row,
  137. ReverseCol ? m_matrix.cols() - col - OffsetCol : col));
  138. }
  139. template<int LoadMode>
  140. inline void writePacket(Index row, Index col, const PacketScalar& x)
  141. {
  142. m_matrix.const_cast_derived().template writePacket<LoadMode>(
  143. ReverseRow ? m_matrix.rows() - row - OffsetRow : row,
  144. ReverseCol ? m_matrix.cols() - col - OffsetCol : col,
  145. reverse_packet::run(x));
  146. }
  147. template<int LoadMode>
  148. inline const PacketScalar packet(Index index) const
  149. {
  150. return internal::preverse(m_matrix.template packet<LoadMode>( m_matrix.size() - index - PacketSize ));
  151. }
  152. template<int LoadMode>
  153. inline void writePacket(Index index, const PacketScalar& x)
  154. {
  155. m_matrix.const_cast_derived().template writePacket<LoadMode>(m_matrix.size() - index - PacketSize, internal::preverse(x));
  156. }
  157. protected:
  158. const typename MatrixType::Nested m_matrix;
  159. };
  160. /** \returns an expression of the reverse of *this.
  161. *
  162. * Example: \include MatrixBase_reverse.cpp
  163. * Output: \verbinclude MatrixBase_reverse.out
  164. *
  165. */
  166. template<typename Derived>
  167. inline typename DenseBase<Derived>::ReverseReturnType
  168. DenseBase<Derived>::reverse()
  169. {
  170. return derived();
  171. }
  172. /** This is the const version of reverse(). */
  173. template<typename Derived>
  174. inline const typename DenseBase<Derived>::ConstReverseReturnType
  175. DenseBase<Derived>::reverse() const
  176. {
  177. return derived();
  178. }
  179. /** This is the "in place" version of reverse: it reverses \c *this.
  180. *
  181. * In most cases it is probably better to simply use the reversed expression
  182. * of a matrix. However, when reversing the matrix data itself is really needed,
  183. * then this "in-place" version is probably the right choice because it provides
  184. * the following additional features:
  185. * - less error prone: doing the same operation with .reverse() requires special care:
  186. * \code m = m.reverse().eval(); \endcode
  187. * - this API allows to avoid creating a temporary (the current implementation creates a temporary, but that could be avoided using swap)
  188. * - it allows future optimizations (cache friendliness, etc.)
  189. *
  190. * \sa reverse() */
  191. template<typename Derived>
  192. inline void DenseBase<Derived>::reverseInPlace()
  193. {
  194. derived() = derived().reverse().eval();
  195. }
  196. #endif // EIGEN_REVERSE_H