2008-06-02 22:58:36 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra. Eigen itself is part of the KDE project.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
|
2008-07-22 10:54:42 +00:00
|
|
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
|
2008-06-02 22:58:36 +00:00
|
|
|
//
|
|
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2 of
|
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
#ifndef EIGEN_CROSS_H
|
|
|
|
|
#define EIGEN_CROSS_H
|
|
|
|
|
|
2008-07-19 20:36:41 +00:00
|
|
|
/** \geometry_module
|
|
|
|
|
* \returns the cross product of \c *this and \a other */
|
2008-06-02 22:58:36 +00:00
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
2008-06-19 23:00:51 +00:00
|
|
|
inline typename ei_eval<Derived>::type
|
|
|
|
|
MatrixBase<Derived>::cross(const MatrixBase<OtherDerived>& other) const
|
2008-06-02 22:58:36 +00:00
|
|
|
{
|
2008-07-22 10:54:42 +00:00
|
|
|
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived,3);
|
|
|
|
|
|
2008-06-07 13:18:29 +00:00
|
|
|
// Note that there is no need for an expression here since the compiler
|
|
|
|
|
// optimize such a small temporary very well (even within a complex expression)
|
|
|
|
|
const typename ei_nested<Derived,2>::type lhs(derived());
|
|
|
|
|
const typename ei_nested<OtherDerived,2>::type rhs(other.derived());
|
|
|
|
|
return typename ei_eval<Derived>::type(
|
|
|
|
|
lhs.coeff(1) * rhs.coeff(2) - lhs.coeff(2) * rhs.coeff(1),
|
|
|
|
|
lhs.coeff(2) * rhs.coeff(0) - lhs.coeff(0) * rhs.coeff(2),
|
|
|
|
|
lhs.coeff(0) * rhs.coeff(1) - lhs.coeff(1) * rhs.coeff(0)
|
|
|
|
|
);
|
2008-06-02 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-22 10:54:42 +00:00
|
|
|
template<typename Derived, int Size = Derived::SizeAtCompileTime>
|
|
|
|
|
struct ei_perpendicular_selector
|
|
|
|
|
{
|
|
|
|
|
typedef typename ei_eval<Derived>::type VectorType;
|
|
|
|
|
typedef typename ei_traits<Derived>::Scalar Scalar;
|
|
|
|
|
inline static VectorType run(const Derived& src)
|
|
|
|
|
{
|
|
|
|
|
VectorType perp;
|
|
|
|
|
/* Let us compute the crossed product of *this with a vector
|
|
|
|
|
* that is not too close to being colinear to *this.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* unless the x and y coords are both close to zero, we can
|
|
|
|
|
* simply take ( -y, x, 0 ) and normalize it.
|
|
|
|
|
*/
|
|
|
|
|
if((!ei_isMuchSmallerThan(src.x(), src.z()))
|
|
|
|
|
|| (!ei_isMuchSmallerThan(src.y(), src.z())))
|
|
|
|
|
{
|
|
|
|
|
Scalar invnm = Scalar(1)/src.template start<2>().norm();
|
|
|
|
|
perp.template start<3>() << -ei_conj(src.y())*invnm, ei_conj(src.x())*invnm, 0;
|
|
|
|
|
}
|
|
|
|
|
/* if both x and y are close to zero, then the vector is close
|
|
|
|
|
* to the z-axis, so it's far from colinear to the x-axis for instance.
|
|
|
|
|
* So we take the crossed product with (1,0,0) and normalize it.
|
|
|
|
|
*/
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Scalar invnm = Scalar(1)/src.template end<2>().norm();
|
|
|
|
|
perp.template start<3>() << 0, -ei_conj(src.z())*invnm, ei_conj(src.y())*invnm;
|
|
|
|
|
}
|
|
|
|
|
if (Derived::SizeAtCompileTime>3
|
|
|
|
|
|| (Derived::SizeAtCompileTime==Dynamic && src.size()>3))
|
|
|
|
|
perp.end(src.size()-3).setZero();
|
|
|
|
|
|
|
|
|
|
return perp;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
struct ei_perpendicular_selector<Derived,2>
|
|
|
|
|
{
|
|
|
|
|
typedef typename ei_eval<Derived>::type VectorType;
|
|
|
|
|
inline static VectorType run(const Derived& src)
|
|
|
|
|
{ return VectorType(-ei_conj(src.y()), ei_conj(src.x())).normalized(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \Returns an orthogonal vector of \c *this
|
|
|
|
|
*
|
|
|
|
|
* The size of \c *this must be at least 2. If the size is exactly 2,
|
|
|
|
|
* then the returned vector is a counter clock wise rotation of \c *this, \ie (-y,x).
|
|
|
|
|
*
|
|
|
|
|
* \sa cross()
|
|
|
|
|
*/
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
typename ei_eval<Derived>::type
|
2008-07-26 20:40:29 +00:00
|
|
|
MatrixBase<Derived>::someOrthogonal() const
|
2008-07-22 10:54:42 +00:00
|
|
|
{
|
|
|
|
|
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
|
|
|
|
|
return ei_perpendicular_selector<Derived>::run(derived());
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-02 22:58:36 +00:00
|
|
|
#endif // EIGEN_CROSS_H
|