aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Geometry/Cross.h
blob: debe43ca47b091688de7d5bc621785ea5f4ec903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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>
//
// 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

/** \class Cross
  *
  * \brief Expression of the cross product of two vectors
  *
  * \param Lhs the type of the left-hand side
  * \param Rhs the type of the right-hand side
  *
  * This class represents an expression of the cross product of two 3D vectors.
  * It is the return type of MatrixBase::cross(), and most
  * of the time this is the only way it is used.
  */
template<typename Lhs, typename Rhs>
struct ei_traits<Cross<Lhs, Rhs> >
{
  typedef typename Lhs::Scalar Scalar;
  typedef typename ei_nested<Lhs,2>::type LhsNested;
  typedef typename ei_nested<Rhs,2>::type RhsNested;
  typedef typename ei_unref<LhsNested>::type _LhsNested;
  typedef typename ei_unref<RhsNested>::type _RhsNested;
  enum {
    RowsAtCompileTime = 3,
    ColsAtCompileTime = 1,
    MaxRowsAtCompileTime = 3,
    MaxColsAtCompileTime = 1,
    Flags = ((_RhsNested::Flags | _LhsNested::Flags) & HereditaryBits)
          | EvalBeforeAssigningBit,
    CoeffReadCost = NumTraits<Scalar>::AddCost + 2 * NumTraits<Scalar>::MulCost
  };
};

template<typename Lhs, typename Rhs> class Cross : ei_no_assignment_operator,
    public MatrixBase<Cross<Lhs, Rhs> >
{
  public:

    EIGEN_GENERIC_PUBLIC_INTERFACE(Cross)
    typedef typename ei_traits<Cross>::LhsNested LhsNested;
    typedef typename ei_traits<Cross>::RhsNested RhsNested;

    Cross(const Lhs& lhs, const Rhs& rhs)
      : m_lhs(lhs), m_rhs(rhs)
    {
      assert(lhs.isVector());
      assert(rhs.isVector());
      assert(lhs.size() == 3 && rhs.size() == 3);
    }

  private:

    int _rows() const { return 3; }
    int _cols() const { return 1; }

    Scalar _coeff(int i, int) const
    {
      return m_lhs[(i+1)%3]*m_rhs[(i+2)%3] - m_lhs[(i+2)%3]*m_rhs[(i+1)%3];
    }

  protected:
    const LhsNested m_lhs;
    const RhsNested m_rhs;
};

/** \returns an expression of the cross product of \c *this and \a other
  *
  * \sa class Cross
  */
template<typename Derived>
template<typename OtherDerived>
const Cross<Derived,OtherDerived>
MatrixBase<Derived>::cross(const MatrixBase<OtherDerived>& other) const
{
    return Cross<Derived,OtherDerived>(derived(),other.derived());
}

#endif // EIGEN_CROSS_H