1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2008-2009 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2006-2008 Benoit Jacob <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li
11*bf2c3715SXin Li #ifndef EIGEN_ORTHOMETHODS_H
12*bf2c3715SXin Li #define EIGEN_ORTHOMETHODS_H
13*bf2c3715SXin Li
14*bf2c3715SXin Li namespace Eigen {
15*bf2c3715SXin Li
16*bf2c3715SXin Li /** \geometry_module \ingroup Geometry_Module
17*bf2c3715SXin Li *
18*bf2c3715SXin Li * \returns the cross product of \c *this and \a other
19*bf2c3715SXin Li *
20*bf2c3715SXin Li * Here is a very good explanation of cross-product: http://xkcd.com/199/
21*bf2c3715SXin Li *
22*bf2c3715SXin Li * With complex numbers, the cross product is implemented as
23*bf2c3715SXin Li * \f$ (\mathbf{a}+i\mathbf{b}) \times (\mathbf{c}+i\mathbf{d}) = (\mathbf{a} \times \mathbf{c} - \mathbf{b} \times \mathbf{d}) - i(\mathbf{a} \times \mathbf{d} - \mathbf{b} \times \mathbf{c})\f$
24*bf2c3715SXin Li *
25*bf2c3715SXin Li * \sa MatrixBase::cross3()
26*bf2c3715SXin Li */
27*bf2c3715SXin Li template<typename Derived>
28*bf2c3715SXin Li template<typename OtherDerived>
29*bf2c3715SXin Li #ifndef EIGEN_PARSED_BY_DOXYGEN
30*bf2c3715SXin Li EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
31*bf2c3715SXin Li typename MatrixBase<Derived>::template cross_product_return_type<OtherDerived>::type
32*bf2c3715SXin Li #else
33*bf2c3715SXin Li typename MatrixBase<Derived>::PlainObject
34*bf2c3715SXin Li #endif
cross(const MatrixBase<OtherDerived> & other)35*bf2c3715SXin Li MatrixBase<Derived>::cross(const MatrixBase<OtherDerived>& other) const
36*bf2c3715SXin Li {
37*bf2c3715SXin Li EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived,3)
38*bf2c3715SXin Li EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,3)
39*bf2c3715SXin Li
40*bf2c3715SXin Li // Note that there is no need for an expression here since the compiler
41*bf2c3715SXin Li // optimize such a small temporary very well (even within a complex expression)
42*bf2c3715SXin Li typename internal::nested_eval<Derived,2>::type lhs(derived());
43*bf2c3715SXin Li typename internal::nested_eval<OtherDerived,2>::type rhs(other.derived());
44*bf2c3715SXin Li return typename cross_product_return_type<OtherDerived>::type(
45*bf2c3715SXin Li numext::conj(lhs.coeff(1) * rhs.coeff(2) - lhs.coeff(2) * rhs.coeff(1)),
46*bf2c3715SXin Li numext::conj(lhs.coeff(2) * rhs.coeff(0) - lhs.coeff(0) * rhs.coeff(2)),
47*bf2c3715SXin Li numext::conj(lhs.coeff(0) * rhs.coeff(1) - lhs.coeff(1) * rhs.coeff(0))
48*bf2c3715SXin Li );
49*bf2c3715SXin Li }
50*bf2c3715SXin Li
51*bf2c3715SXin Li namespace internal {
52*bf2c3715SXin Li
53*bf2c3715SXin Li template< int Arch,typename VectorLhs,typename VectorRhs,
54*bf2c3715SXin Li typename Scalar = typename VectorLhs::Scalar,
55*bf2c3715SXin Li bool Vectorizable = bool((VectorLhs::Flags&VectorRhs::Flags)&PacketAccessBit)>
56*bf2c3715SXin Li struct cross3_impl {
57*bf2c3715SXin Li EIGEN_DEVICE_FUNC static inline typename internal::plain_matrix_type<VectorLhs>::type
runcross3_impl58*bf2c3715SXin Li run(const VectorLhs& lhs, const VectorRhs& rhs)
59*bf2c3715SXin Li {
60*bf2c3715SXin Li return typename internal::plain_matrix_type<VectorLhs>::type(
61*bf2c3715SXin Li numext::conj(lhs.coeff(1) * rhs.coeff(2) - lhs.coeff(2) * rhs.coeff(1)),
62*bf2c3715SXin Li numext::conj(lhs.coeff(2) * rhs.coeff(0) - lhs.coeff(0) * rhs.coeff(2)),
63*bf2c3715SXin Li numext::conj(lhs.coeff(0) * rhs.coeff(1) - lhs.coeff(1) * rhs.coeff(0)),
64*bf2c3715SXin Li 0
65*bf2c3715SXin Li );
66*bf2c3715SXin Li }
67*bf2c3715SXin Li };
68*bf2c3715SXin Li
69*bf2c3715SXin Li }
70*bf2c3715SXin Li
71*bf2c3715SXin Li /** \geometry_module \ingroup Geometry_Module
72*bf2c3715SXin Li *
73*bf2c3715SXin Li * \returns the cross product of \c *this and \a other using only the x, y, and z coefficients
74*bf2c3715SXin Li *
75*bf2c3715SXin Li * The size of \c *this and \a other must be four. This function is especially useful
76*bf2c3715SXin Li * when using 4D vectors instead of 3D ones to get advantage of SSE/AltiVec vectorization.
77*bf2c3715SXin Li *
78*bf2c3715SXin Li * \sa MatrixBase::cross()
79*bf2c3715SXin Li */
80*bf2c3715SXin Li template<typename Derived>
81*bf2c3715SXin Li template<typename OtherDerived>
82*bf2c3715SXin Li EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::PlainObject
cross3(const MatrixBase<OtherDerived> & other)83*bf2c3715SXin Li MatrixBase<Derived>::cross3(const MatrixBase<OtherDerived>& other) const
84*bf2c3715SXin Li {
85*bf2c3715SXin Li EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived,4)
86*bf2c3715SXin Li EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,4)
87*bf2c3715SXin Li
88*bf2c3715SXin Li typedef typename internal::nested_eval<Derived,2>::type DerivedNested;
89*bf2c3715SXin Li typedef typename internal::nested_eval<OtherDerived,2>::type OtherDerivedNested;
90*bf2c3715SXin Li DerivedNested lhs(derived());
91*bf2c3715SXin Li OtherDerivedNested rhs(other.derived());
92*bf2c3715SXin Li
93*bf2c3715SXin Li return internal::cross3_impl<Architecture::Target,
94*bf2c3715SXin Li typename internal::remove_all<DerivedNested>::type,
95*bf2c3715SXin Li typename internal::remove_all<OtherDerivedNested>::type>::run(lhs,rhs);
96*bf2c3715SXin Li }
97*bf2c3715SXin Li
98*bf2c3715SXin Li /** \geometry_module \ingroup Geometry_Module
99*bf2c3715SXin Li *
100*bf2c3715SXin Li * \returns a matrix expression of the cross product of each column or row
101*bf2c3715SXin Li * of the referenced expression with the \a other vector.
102*bf2c3715SXin Li *
103*bf2c3715SXin Li * The referenced matrix must have one dimension equal to 3.
104*bf2c3715SXin Li * The result matrix has the same dimensions than the referenced one.
105*bf2c3715SXin Li *
106*bf2c3715SXin Li * \sa MatrixBase::cross() */
107*bf2c3715SXin Li template<typename ExpressionType, int Direction>
108*bf2c3715SXin Li template<typename OtherDerived>
109*bf2c3715SXin Li EIGEN_DEVICE_FUNC
110*bf2c3715SXin Li const typename VectorwiseOp<ExpressionType,Direction>::CrossReturnType
cross(const MatrixBase<OtherDerived> & other)111*bf2c3715SXin Li VectorwiseOp<ExpressionType,Direction>::cross(const MatrixBase<OtherDerived>& other) const
112*bf2c3715SXin Li {
113*bf2c3715SXin Li EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(OtherDerived,3)
114*bf2c3715SXin Li EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
115*bf2c3715SXin Li YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
116*bf2c3715SXin Li
117*bf2c3715SXin Li typename internal::nested_eval<ExpressionType,2>::type mat(_expression());
118*bf2c3715SXin Li typename internal::nested_eval<OtherDerived,2>::type vec(other.derived());
119*bf2c3715SXin Li
120*bf2c3715SXin Li CrossReturnType res(_expression().rows(),_expression().cols());
121*bf2c3715SXin Li if(Direction==Vertical)
122*bf2c3715SXin Li {
123*bf2c3715SXin Li eigen_assert(CrossReturnType::RowsAtCompileTime==3 && "the matrix must have exactly 3 rows");
124*bf2c3715SXin Li res.row(0) = (mat.row(1) * vec.coeff(2) - mat.row(2) * vec.coeff(1)).conjugate();
125*bf2c3715SXin Li res.row(1) = (mat.row(2) * vec.coeff(0) - mat.row(0) * vec.coeff(2)).conjugate();
126*bf2c3715SXin Li res.row(2) = (mat.row(0) * vec.coeff(1) - mat.row(1) * vec.coeff(0)).conjugate();
127*bf2c3715SXin Li }
128*bf2c3715SXin Li else
129*bf2c3715SXin Li {
130*bf2c3715SXin Li eigen_assert(CrossReturnType::ColsAtCompileTime==3 && "the matrix must have exactly 3 columns");
131*bf2c3715SXin Li res.col(0) = (mat.col(1) * vec.coeff(2) - mat.col(2) * vec.coeff(1)).conjugate();
132*bf2c3715SXin Li res.col(1) = (mat.col(2) * vec.coeff(0) - mat.col(0) * vec.coeff(2)).conjugate();
133*bf2c3715SXin Li res.col(2) = (mat.col(0) * vec.coeff(1) - mat.col(1) * vec.coeff(0)).conjugate();
134*bf2c3715SXin Li }
135*bf2c3715SXin Li return res;
136*bf2c3715SXin Li }
137*bf2c3715SXin Li
138*bf2c3715SXin Li namespace internal {
139*bf2c3715SXin Li
140*bf2c3715SXin Li template<typename Derived, int Size = Derived::SizeAtCompileTime>
141*bf2c3715SXin Li struct unitOrthogonal_selector
142*bf2c3715SXin Li {
143*bf2c3715SXin Li typedef typename plain_matrix_type<Derived>::type VectorType;
144*bf2c3715SXin Li typedef typename traits<Derived>::Scalar Scalar;
145*bf2c3715SXin Li typedef typename NumTraits<Scalar>::Real RealScalar;
146*bf2c3715SXin Li typedef Matrix<Scalar,2,1> Vector2;
147*bf2c3715SXin Li EIGEN_DEVICE_FUNC
rununitOrthogonal_selector148*bf2c3715SXin Li static inline VectorType run(const Derived& src)
149*bf2c3715SXin Li {
150*bf2c3715SXin Li VectorType perp = VectorType::Zero(src.size());
151*bf2c3715SXin Li Index maxi = 0;
152*bf2c3715SXin Li Index sndi = 0;
153*bf2c3715SXin Li src.cwiseAbs().maxCoeff(&maxi);
154*bf2c3715SXin Li if (maxi==0)
155*bf2c3715SXin Li sndi = 1;
156*bf2c3715SXin Li RealScalar invnm = RealScalar(1)/(Vector2() << src.coeff(sndi),src.coeff(maxi)).finished().norm();
157*bf2c3715SXin Li perp.coeffRef(maxi) = -numext::conj(src.coeff(sndi)) * invnm;
158*bf2c3715SXin Li perp.coeffRef(sndi) = numext::conj(src.coeff(maxi)) * invnm;
159*bf2c3715SXin Li
160*bf2c3715SXin Li return perp;
161*bf2c3715SXin Li }
162*bf2c3715SXin Li };
163*bf2c3715SXin Li
164*bf2c3715SXin Li template<typename Derived>
165*bf2c3715SXin Li struct unitOrthogonal_selector<Derived,3>
166*bf2c3715SXin Li {
167*bf2c3715SXin Li typedef typename plain_matrix_type<Derived>::type VectorType;
168*bf2c3715SXin Li typedef typename traits<Derived>::Scalar Scalar;
169*bf2c3715SXin Li typedef typename NumTraits<Scalar>::Real RealScalar;
170*bf2c3715SXin Li EIGEN_DEVICE_FUNC
171*bf2c3715SXin Li static inline VectorType run(const Derived& src)
172*bf2c3715SXin Li {
173*bf2c3715SXin Li VectorType perp;
174*bf2c3715SXin Li /* Let us compute the crossed product of *this with a vector
175*bf2c3715SXin Li * that is not too close to being colinear to *this.
176*bf2c3715SXin Li */
177*bf2c3715SXin Li
178*bf2c3715SXin Li /* unless the x and y coords are both close to zero, we can
179*bf2c3715SXin Li * simply take ( -y, x, 0 ) and normalize it.
180*bf2c3715SXin Li */
181*bf2c3715SXin Li if((!isMuchSmallerThan(src.x(), src.z()))
182*bf2c3715SXin Li || (!isMuchSmallerThan(src.y(), src.z())))
183*bf2c3715SXin Li {
184*bf2c3715SXin Li RealScalar invnm = RealScalar(1)/src.template head<2>().norm();
185*bf2c3715SXin Li perp.coeffRef(0) = -numext::conj(src.y())*invnm;
186*bf2c3715SXin Li perp.coeffRef(1) = numext::conj(src.x())*invnm;
187*bf2c3715SXin Li perp.coeffRef(2) = 0;
188*bf2c3715SXin Li }
189*bf2c3715SXin Li /* if both x and y are close to zero, then the vector is close
190*bf2c3715SXin Li * to the z-axis, so it's far from colinear to the x-axis for instance.
191*bf2c3715SXin Li * So we take the crossed product with (1,0,0) and normalize it.
192*bf2c3715SXin Li */
193*bf2c3715SXin Li else
194*bf2c3715SXin Li {
195*bf2c3715SXin Li RealScalar invnm = RealScalar(1)/src.template tail<2>().norm();
196*bf2c3715SXin Li perp.coeffRef(0) = 0;
197*bf2c3715SXin Li perp.coeffRef(1) = -numext::conj(src.z())*invnm;
198*bf2c3715SXin Li perp.coeffRef(2) = numext::conj(src.y())*invnm;
199*bf2c3715SXin Li }
200*bf2c3715SXin Li
201*bf2c3715SXin Li return perp;
202*bf2c3715SXin Li }
203*bf2c3715SXin Li };
204*bf2c3715SXin Li
205*bf2c3715SXin Li template<typename Derived>
206*bf2c3715SXin Li struct unitOrthogonal_selector<Derived,2>
207*bf2c3715SXin Li {
208*bf2c3715SXin Li typedef typename plain_matrix_type<Derived>::type VectorType;
209*bf2c3715SXin Li EIGEN_DEVICE_FUNC
210*bf2c3715SXin Li static inline VectorType run(const Derived& src)
211*bf2c3715SXin Li { return VectorType(-numext::conj(src.y()), numext::conj(src.x())).normalized(); }
212*bf2c3715SXin Li };
213*bf2c3715SXin Li
214*bf2c3715SXin Li } // end namespace internal
215*bf2c3715SXin Li
216*bf2c3715SXin Li /** \geometry_module \ingroup Geometry_Module
217*bf2c3715SXin Li *
218*bf2c3715SXin Li * \returns a unit vector which is orthogonal to \c *this
219*bf2c3715SXin Li *
220*bf2c3715SXin Li * The size of \c *this must be at least 2. If the size is exactly 2,
221*bf2c3715SXin Li * then the returned vector is a counter clock wise rotation of \c *this, i.e., (-y,x).normalized().
222*bf2c3715SXin Li *
223*bf2c3715SXin Li * \sa cross()
224*bf2c3715SXin Li */
225*bf2c3715SXin Li template<typename Derived>
226*bf2c3715SXin Li EIGEN_DEVICE_FUNC typename MatrixBase<Derived>::PlainObject
227*bf2c3715SXin Li MatrixBase<Derived>::unitOrthogonal() const
228*bf2c3715SXin Li {
229*bf2c3715SXin Li EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
230*bf2c3715SXin Li return internal::unitOrthogonal_selector<Derived>::run(derived());
231*bf2c3715SXin Li }
232*bf2c3715SXin Li
233*bf2c3715SXin Li } // end namespace Eigen
234*bf2c3715SXin Li
235*bf2c3715SXin Li #endif // EIGEN_ORTHOMETHODS_H
236