1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra. Eigen itself is part of the KDE project.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// no include guard, we'll include this twice from All.h from Eigen2Support, and it's internal anyway
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \geometry_module \ingroup Geometry_Module
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \class Hyperplane
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \brief A hyperplane
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * A hyperplane is an affine subspace of dimension n-1 in a space of dimension n.
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * For example, a hyperplane in a plane is a line; a hyperplane in 3-space is a plane.
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \param _Scalar the scalar type, i.e., the type of the coefficients
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \param _AmbientDim the dimension of the ambient space, can be a compile time value or Dynamic.
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *             Notice that the dimension of the hyperplane is _AmbientDim-1.
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * This class represents an hyperplane as the zero set of the implicit equation
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \f$ n \cdot x + d = 0 \f$ where \f$ n \f$ is a unit normal vector of the plane (linear part)
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * and \f$ d \f$ is the distance (offset) to the origin.
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate <typename _Scalar, int _AmbientDim>
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathclass Hyperplane
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathpublic:
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim==Dynamic ? Dynamic : _AmbientDim+1)
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  enum { AmbientDimAtCompileTime = _AmbientDim };
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef _Scalar Scalar;
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Matrix<Scalar,AmbientDimAtCompileTime,1> VectorType;
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Matrix<Scalar,int(AmbientDimAtCompileTime)==Dynamic
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        ? Dynamic
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        : int(AmbientDimAtCompileTime)+1,1> Coefficients;
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Block<Coefficients,AmbientDimAtCompileTime,1> NormalReturnType;
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Default constructor without initialization */
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline explicit Hyperplane() {}
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Constructs a dynamic-size hyperplane with \a _dim the dimension
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * of the ambient space */
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline explicit Hyperplane(int _dim) : m_coeffs(_dim+1) {}
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Construct a plane from its normal \a n and a point \a e onto the plane.
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \warning the vector normal is assumed to be normalized.
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline Hyperplane(const VectorType& n, const VectorType& e)
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    : m_coeffs(n.size()+1)
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    normal() = n;
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    offset() = -e.eigen2_dot(n);
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Constructs a plane from its normal \a n and distance to the origin \a d
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * such that the algebraic equation of the plane is \f$ n \cdot x + d = 0 \f$.
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \warning the vector normal is assumed to be normalized.
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline Hyperplane(const VectorType& n, Scalar d)
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    : m_coeffs(n.size()+1)
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    normal() = n;
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    offset() = d;
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Constructs a hyperplane passing through the two points. If the dimension of the ambient space
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * is greater than 2, then there isn't uniqueness, so an arbitrary choice is made.
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Hyperplane Through(const VectorType& p0, const VectorType& p1)
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Hyperplane result(p0.size());
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    result.normal() = (p1 - p0).unitOrthogonal();
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    result.offset() = -result.normal().eigen2_dot(p0);
82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return result;
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Constructs a hyperplane passing through the three points. The dimension of the ambient space
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * is required to be exactly 3.
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline Hyperplane Through(const VectorType& p0, const VectorType& p1, const VectorType& p2)
89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 3)
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Hyperplane result(p0.size());
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    result.normal() = (p2 - p0).cross(p1 - p0).normalized();
93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    result.offset() = -result.normal().eigen2_dot(p0);
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return result;
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Constructs a hyperplane passing through the parametrized line \a parametrized.
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * If the dimension of the ambient space is greater than 2, then there isn't uniqueness,
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * so an arbitrary choice is made.
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // FIXME to be consitent with the rest this could be implemented as a static Through function ??
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  explicit Hyperplane(const ParametrizedLine<Scalar, AmbientDimAtCompileTime>& parametrized)
103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    normal() = parametrized.direction().unitOrthogonal();
105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    offset() = -normal().eigen2_dot(parametrized.origin());
106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  ~Hyperplane() {}
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
110c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns the dimension in which the plane holds */
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline int dim() const { return int(AmbientDimAtCompileTime)==Dynamic ? m_coeffs.size()-1 : int(AmbientDimAtCompileTime); }
112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** normalizes \c *this */
114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  void normalize(void)
115c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
116c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    m_coeffs /= normal().norm();
117c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
118c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns the signed distance between the plane \c *this and a point \a p.
120c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \sa absDistance()
121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline Scalar signedDistance(const VectorType& p) const { return p.eigen2_dot(normal()) + offset(); }
123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
124c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns the absolute distance between the plane \c *this and a point \a p.
125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \sa signedDistance()
126c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline Scalar absDistance(const VectorType& p) const { return ei_abs(signedDistance(p)); }
128c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns the projection of a point \a p onto the plane \c *this.
130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline VectorType projection(const VectorType& p) const { return p - signedDistance(p) * normal(); }
132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns a constant reference to the unit normal vector of the plane, which corresponds
134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * to the linear part of the implicit equation.
135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
136c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline const NormalReturnType normal() const { return NormalReturnType(*const_cast<Coefficients*>(&m_coeffs),0,0,dim(),1); }
137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
138c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns a non-constant reference to the unit normal vector of the plane, which corresponds
139c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * to the linear part of the implicit equation.
140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
141c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline NormalReturnType normal() { return NormalReturnType(m_coeffs,0,0,dim(),1); }
142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
143c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns the distance to the origin, which is also the "constant term" of the implicit equation
144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \warning the vector normal is assumed to be normalized.
145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline const Scalar& offset() const { return m_coeffs.coeff(dim()); }
147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
148c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns a non-constant reference to the distance to the origin, which is also the constant part
149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * of the implicit equation */
150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline Scalar& offset() { return m_coeffs(dim()); }
151c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns a constant reference to the coefficients c_i of the plane equation:
153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \f$ c_0*x_0 + ... + c_{d-1}*x_{d-1} + c_d = 0 \f$
154c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline const Coefficients& coeffs() const { return m_coeffs; }
156c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns a non-constant reference to the coefficients c_i of the plane equation:
158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \f$ c_0*x_0 + ... + c_{d-1}*x_{d-1} + c_d = 0 \f$
159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
160c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline Coefficients& coeffs() { return m_coeffs; }
161c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
162c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns the intersection of *this with \a other.
163c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *
164c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \warning The ambient space must be a plane, i.e. have dimension 2, so that \c *this and \a other are lines.
165c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *
166c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \note If \a other is approximately parallel to *this, this method will return any point on *this.
167c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
168c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VectorType intersection(const Hyperplane& other)
169c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
170c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 2)
171c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Scalar det = coeffs().coeff(0) * other.coeffs().coeff(1) - coeffs().coeff(1) * other.coeffs().coeff(0);
172c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // since the line equations ax+by=c are normalized with a^2+b^2=1, the following tests
173c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // whether the two lines are approximately parallel.
174c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if(ei_isMuchSmallerThan(det, Scalar(1)))
175c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {   // special case where the two lines are approximately parallel. Pick any point on the first line.
176c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        if(ei_abs(coeffs().coeff(1))>ei_abs(coeffs().coeff(0)))
177c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            return VectorType(coeffs().coeff(1), -coeffs().coeff(2)/coeffs().coeff(1)-coeffs().coeff(0));
178c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        else
179c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            return VectorType(-coeffs().coeff(2)/coeffs().coeff(0)-coeffs().coeff(1), coeffs().coeff(0));
180c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
181c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    else
182c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {   // general case
183c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Scalar invdet = Scalar(1) / det;
184c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        return VectorType(invdet*(coeffs().coeff(1)*other.coeffs().coeff(2)-other.coeffs().coeff(1)*coeffs().coeff(2)),
185c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                          invdet*(other.coeffs().coeff(0)*coeffs().coeff(2)-coeffs().coeff(0)*other.coeffs().coeff(2)));
186c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
187c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
188c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
189c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Applies the transformation matrix \a mat to \c *this and returns a reference to \c *this.
190c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *
191c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \param mat the Dim x Dim transformation matrix
192c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \param traits specifies whether the matrix \a mat represents an Isometry
193c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *               or a more generic Affine transformation. The default is Affine.
194c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
195c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename XprType>
196c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline Hyperplane& transform(const MatrixBase<XprType>& mat, TransformTraits traits = Affine)
197c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
198c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if (traits==Affine)
199c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      normal() = mat.inverse().transpose() * normal();
200c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    else if (traits==Isometry)
201c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      normal() = mat * normal();
202c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    else
203c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
204c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      ei_assert("invalid traits value in Hyperplane::transform()");
205c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
206c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return *this;
207c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
208c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
209c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Applies the transformation \a t to \c *this and returns a reference to \c *this.
210c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *
211c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \param t the transformation of dimension Dim
212c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \param traits specifies whether the transformation \a t represents an Isometry
213c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *               or a more generic Affine transformation. The default is Affine.
214c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *               Other kind of transformations are not supported.
215c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
216c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline Hyperplane& transform(const Transform<Scalar,AmbientDimAtCompileTime>& t,
217c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                                TransformTraits traits = Affine)
218c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
219c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    transform(t.linear(), traits);
220c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    offset() -= t.translation().eigen2_dot(normal());
221c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return *this;
222c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
223c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
224c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns \c *this with scalar type casted to \a NewScalarType
225c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *
226c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * Note that if \a NewScalarType is equal to the current scalar type of \c *this
227c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * then this function smartly returns a const reference to \c *this.
228c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    */
229c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename NewScalarType>
230c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline typename internal::cast_return_type<Hyperplane,
231c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath           Hyperplane<NewScalarType,AmbientDimAtCompileTime> >::type cast() const
232c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
233c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return typename internal::cast_return_type<Hyperplane,
234c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                    Hyperplane<NewScalarType,AmbientDimAtCompileTime> >::type(*this);
235c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
236c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
237c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** Copy constructor with scalar type conversion */
238c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  template<typename OtherScalarType>
239c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  inline explicit Hyperplane(const Hyperplane<OtherScalarType,AmbientDimAtCompileTime>& other)
240c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  { m_coeffs = other.coeffs().template cast<Scalar>(); }
241c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
242c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns \c true if \c *this is approximately equal to \a other, within the precision
243c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * determined by \a prec.
244c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *
245c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    * \sa MatrixBase::isApprox() */
246c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  bool isApprox(const Hyperplane& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
247c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  { return m_coeffs.isApprox(other.m_coeffs, prec); }
248c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
249c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathprotected:
250c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
251c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Coefficients m_coeffs;
252c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
253c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
254c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace Eigen
255