Hyperplane.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_HYPERPLANE_H
27 #define EIGEN_HYPERPLANE_H
28 
29 namespace Eigen {
30 
48 template <typename _Scalar, int _AmbientDim, int _Options>
50 {
51 public:
52  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim==Dynamic ? Dynamic : _AmbientDim+1)
53  enum {
54  AmbientDimAtCompileTime = _AmbientDim,
55  Options = _Options
56  };
57  typedef _Scalar Scalar;
59  typedef DenseIndex Index;
62  ? Dynamic
66 
68  inline explicit Hyperplane() {}
69 
70  template<int OtherOptions>
72  : m_coeffs(other.coeffs())
73  {}
74 
77  inline explicit Hyperplane(Index _dim) : m_coeffs(_dim+1) {}
78 
82  inline Hyperplane(const VectorType& n, const VectorType& e)
83  : m_coeffs(n.size()+1)
84  {
85  normal() = n;
86  offset() = -n.dot(e);
87  }
88 
93  inline Hyperplane(const VectorType& n, Scalar d)
94  : m_coeffs(n.size()+1)
95  {
96  normal() = n;
97  offset() = d;
98  }
99 
103  static inline Hyperplane Through(const VectorType& p0, const VectorType& p1)
104  {
105  Hyperplane result(p0.size());
106  result.normal() = (p1 - p0).unitOrthogonal();
107  result.offset() = -p0.dot(result.normal());
108  return result;
109  }
110 
114  static inline Hyperplane Through(const VectorType& p0, const VectorType& p1, const VectorType& p2)
115  {
117  Hyperplane result(p0.size());
118  result.normal() = (p2 - p0).cross(p1 - p0).normalized();
119  result.offset() = -p0.dot(result.normal());
120  return result;
121  }
122 
127  // FIXME to be consitent with the rest this could be implemented as a static Through function ??
129  {
130  normal() = parametrized.direction().unitOrthogonal();
131  offset() = -parametrized.origin().dot(normal());
132  }
133 
135 
138 
140  void normalize(void)
141  {
142  m_coeffs /= normal().norm();
143  }
144 
148  inline Scalar signedDistance(const VectorType& p) const { return normal().dot(p) + offset(); }
149 
153  inline Scalar absDistance(const VectorType& p) const { return internal::abs(signedDistance(p)); }
154 
157  inline VectorType projection(const VectorType& p) const { return p - signedDistance(p) * normal(); }
158 
162  inline ConstNormalReturnType normal() const { return ConstNormalReturnType(m_coeffs,0,0,dim(),1); }
163 
167  inline NormalReturnType normal() { return NormalReturnType(m_coeffs,0,0,dim(),1); }
168 
172  inline const Scalar& offset() const { return m_coeffs.coeff(dim()); }
173 
176  inline Scalar& offset() { return m_coeffs(dim()); }
177 
181  inline const Coefficients& coeffs() const { return m_coeffs; }
182 
186  inline Coefficients& coeffs() { return m_coeffs; }
187 
194  VectorType intersection(const Hyperplane& other) const
195  {
197  Scalar det = coeffs().coeff(0) * other.coeffs().coeff(1) - coeffs().coeff(1) * other.coeffs().coeff(0);
198  // since the line equations ax+by=c are normalized with a^2+b^2=1, the following tests
199  // whether the two lines are approximately parallel.
201  { // special case where the two lines are approximately parallel. Pick any point on the first line.
202  if(internal::abs(coeffs().coeff(1))>internal::abs(coeffs().coeff(0)))
203  return VectorType(coeffs().coeff(1), -coeffs().coeff(2)/coeffs().coeff(1)-coeffs().coeff(0));
204  else
205  return VectorType(-coeffs().coeff(2)/coeffs().coeff(0)-coeffs().coeff(1), coeffs().coeff(0));
206  }
207  else
208  { // general case
209  Scalar invdet = Scalar(1) / det;
210  return VectorType(invdet*(coeffs().coeff(1)*other.coeffs().coeff(2)-other.coeffs().coeff(1)*coeffs().coeff(2)),
211  invdet*(other.coeffs().coeff(0)*coeffs().coeff(2)-coeffs().coeff(0)*other.coeffs().coeff(2)));
212  }
213  }
214 
221  template<typename XprType>
223  {
224  if (traits==Affine)
225  normal() = mat.inverse().transpose() * normal();
226  else if (traits==Isometry)
227  normal() = mat * normal();
228  else
229  {
230  eigen_assert(0 && "invalid traits value in Hyperplane::transform()");
231  }
232  return *this;
233  }
234 
242  template<int TrOptions>
245  {
246  transform(t.linear(), traits);
247  offset() -= normal().dot(t.translation());
248  return *this;
249  }
250 
256  template<typename NewScalarType>
257  inline typename internal::cast_return_type<Hyperplane,
259  {
260  return typename internal::cast_return_type<Hyperplane,
262  }
263 
265  template<typename OtherScalarType,int OtherOptions>
267  { m_coeffs = other.coeffs().template cast<Scalar>(); }
268 
273  template<int OtherOptions>
275  { return m_coeffs.isApprox(other.m_coeffs, prec); }
276 
277 protected:
278 
280 };
281 
282 } // end namespace Eigen
283 
284 #endif // EIGEN_HYPERPLANE_H