AlignedVector3 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifndef EIGEN_ALIGNED_VECTOR3
  10. #define EIGEN_ALIGNED_VECTOR3
  11. #include "../../Eigen/Geometry"
  12. #include "../../Eigen/src/Core/util/DisableStupidWarnings.h"
  13. namespace Eigen {
  14. /**
  15. * \defgroup AlignedVector3_Module Aligned vector3 module
  16. *
  17. * \code
  18. * #include <unsupported/Eigen/AlignedVector3>
  19. * \endcode
  20. */
  21. //@{
  22. /** \class AlignedVector3
  23. *
  24. * \brief A vectorization friendly 3D vector
  25. *
  26. * This class represents a 3D vector internally using a 4D vector
  27. * such that vectorization can be seamlessly enabled. Of course,
  28. * the same result can be achieved by directly using a 4D vector.
  29. * This class makes this process simpler.
  30. *
  31. */
  32. // TODO specialize Cwise
  33. template<typename _Scalar> class AlignedVector3;
  34. namespace internal {
  35. template<typename _Scalar> struct traits<AlignedVector3<_Scalar> >
  36. : traits<Matrix<_Scalar,3,1,0,4,1> >
  37. {
  38. };
  39. }
  40. template<typename _Scalar> class AlignedVector3
  41. : public MatrixBase<AlignedVector3<_Scalar> >
  42. {
  43. typedef Matrix<_Scalar,4,1> CoeffType;
  44. CoeffType m_coeffs;
  45. public:
  46. typedef MatrixBase<AlignedVector3<_Scalar> > Base;
  47. EIGEN_DENSE_PUBLIC_INTERFACE(AlignedVector3)
  48. using Base::operator*;
  49. inline Index rows() const { return 3; }
  50. inline Index cols() const { return 1; }
  51. Scalar* data() { return m_coeffs.data(); }
  52. const Scalar* data() const { return m_coeffs.data(); }
  53. Index innerStride() const { return 1; }
  54. Index outerStride() const { return 3; }
  55. inline const Scalar& coeff(Index row, Index col) const
  56. { return m_coeffs.coeff(row, col); }
  57. inline Scalar& coeffRef(Index row, Index col)
  58. { return m_coeffs.coeffRef(row, col); }
  59. inline const Scalar& coeff(Index index) const
  60. { return m_coeffs.coeff(index); }
  61. inline Scalar& coeffRef(Index index)
  62. { return m_coeffs.coeffRef(index);}
  63. inline AlignedVector3()
  64. {}
  65. inline AlignedVector3(const Scalar& x, const Scalar& y, const Scalar& z)
  66. : m_coeffs(x, y, z, Scalar(0))
  67. {}
  68. inline AlignedVector3(const AlignedVector3& other)
  69. : Base(), m_coeffs(other.m_coeffs)
  70. {}
  71. template<typename XprType, int Size=XprType::SizeAtCompileTime>
  72. struct generic_assign_selector {};
  73. template<typename XprType> struct generic_assign_selector<XprType,4>
  74. {
  75. inline static void run(AlignedVector3& dest, const XprType& src)
  76. {
  77. dest.m_coeffs = src;
  78. }
  79. };
  80. template<typename XprType> struct generic_assign_selector<XprType,3>
  81. {
  82. inline static void run(AlignedVector3& dest, const XprType& src)
  83. {
  84. dest.m_coeffs.template head<3>() = src;
  85. dest.m_coeffs.w() = Scalar(0);
  86. }
  87. };
  88. template<typename Derived>
  89. inline AlignedVector3(const MatrixBase<Derived>& other)
  90. {
  91. generic_assign_selector<Derived>::run(*this,other.derived());
  92. }
  93. inline AlignedVector3& operator=(const AlignedVector3& other)
  94. { m_coeffs = other.m_coeffs; return *this; }
  95. template <typename Derived>
  96. inline AlignedVector3& operator=(const MatrixBase<Derived>& other)
  97. {
  98. generic_assign_selector<Derived>::run(*this,other.derived());
  99. return *this;
  100. }
  101. inline AlignedVector3 operator+(const AlignedVector3& other) const
  102. { return AlignedVector3(m_coeffs + other.m_coeffs); }
  103. inline AlignedVector3& operator+=(const AlignedVector3& other)
  104. { m_coeffs += other.m_coeffs; return *this; }
  105. inline AlignedVector3 operator-(const AlignedVector3& other) const
  106. { return AlignedVector3(m_coeffs - other.m_coeffs); }
  107. inline AlignedVector3 operator-() const
  108. { return AlignedVector3(-m_coeffs); }
  109. inline AlignedVector3 operator-=(const AlignedVector3& other)
  110. { m_coeffs -= other.m_coeffs; return *this; }
  111. inline AlignedVector3 operator*(const Scalar& s) const
  112. { return AlignedVector3(m_coeffs * s); }
  113. inline friend AlignedVector3 operator*(const Scalar& s,const AlignedVector3& vec)
  114. { return AlignedVector3(s * vec.m_coeffs); }
  115. inline AlignedVector3& operator*=(const Scalar& s)
  116. { m_coeffs *= s; return *this; }
  117. inline AlignedVector3 operator/(const Scalar& s) const
  118. { return AlignedVector3(m_coeffs / s); }
  119. inline AlignedVector3& operator/=(const Scalar& s)
  120. { m_coeffs /= s; return *this; }
  121. inline Scalar dot(const AlignedVector3& other) const
  122. {
  123. eigen_assert(m_coeffs.w()==Scalar(0));
  124. eigen_assert(other.m_coeffs.w()==Scalar(0));
  125. return m_coeffs.dot(other.m_coeffs);
  126. }
  127. inline void normalize()
  128. {
  129. m_coeffs /= norm();
  130. }
  131. inline AlignedVector3 normalized() const
  132. {
  133. return AlignedVector3(m_coeffs / norm());
  134. }
  135. inline Scalar sum() const
  136. {
  137. eigen_assert(m_coeffs.w()==Scalar(0));
  138. return m_coeffs.sum();
  139. }
  140. inline Scalar squaredNorm() const
  141. {
  142. eigen_assert(m_coeffs.w()==Scalar(0));
  143. return m_coeffs.squaredNorm();
  144. }
  145. inline Scalar norm() const
  146. {
  147. using std::sqrt;
  148. return sqrt(squaredNorm());
  149. }
  150. inline AlignedVector3 cross(const AlignedVector3& other) const
  151. {
  152. return AlignedVector3(m_coeffs.cross3(other.m_coeffs));
  153. }
  154. template<typename Derived>
  155. inline bool isApprox(const MatrixBase<Derived>& other, const RealScalar& eps=NumTraits<Scalar>::dummy_precision()) const
  156. {
  157. return m_coeffs.template head<3>().isApprox(other,eps);
  158. }
  159. CoeffType& coeffs() { return m_coeffs; }
  160. const CoeffType& coeffs() const { return m_coeffs; }
  161. };
  162. namespace internal {
  163. template<typename _Scalar>
  164. struct eval<AlignedVector3<_Scalar>, Dense>
  165. {
  166. typedef const AlignedVector3<_Scalar>& type;
  167. };
  168. template<typename Scalar>
  169. struct evaluator<AlignedVector3<Scalar> >
  170. : evaluator<Matrix<Scalar,4,1> >
  171. {
  172. typedef AlignedVector3<Scalar> XprType;
  173. typedef evaluator<Matrix<Scalar,4,1> > Base;
  174. evaluator(const XprType &m) : Base(m.coeffs()) {}
  175. };
  176. }
  177. //@}
  178. }
  179. #include "../../Eigen/src/Core/util/ReenableStupidWarnings.h"
  180. #endif // EIGEN_ALIGNED_VECTOR3