Medial Code Documentation
Loading...
Searching...
No Matches
Hyperplane.h
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// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_HYPERPLANE_H
12#define EIGEN_HYPERPLANE_H
13
14namespace Eigen {
15
33template <typename _Scalar, int _AmbientDim, int _Options>
35{
36public:
37 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_AmbientDim==Dynamic ? Dynamic : _AmbientDim+1)
38 enum {
39 AmbientDimAtCompileTime = _AmbientDim,
40 Options = _Options
41 };
42 typedef _Scalar Scalar;
43 typedef typename NumTraits<Scalar>::Real RealScalar;
46 typedef Matrix<Scalar,Index(AmbientDimAtCompileTime)==Dynamic
47 ? Dynamic
48 : Index(AmbientDimAtCompileTime)+1,1,Options> Coefficients;
51
53 EIGEN_DEVICE_FUNC inline Hyperplane() {}
54
55 template<int OtherOptions>
57 : m_coeffs(other.coeffs())
58 {}
59
62 EIGEN_DEVICE_FUNC inline explicit Hyperplane(Index _dim) : m_coeffs(_dim+1) {}
63
67 EIGEN_DEVICE_FUNC inline Hyperplane(const VectorType& n, const VectorType& e)
68 : m_coeffs(n.size()+1)
69 {
70 normal() = n;
71 offset() = -n.dot(e);
72 }
73
78 EIGEN_DEVICE_FUNC inline Hyperplane(const VectorType& n, const Scalar& d)
79 : m_coeffs(n.size()+1)
80 {
81 normal() = n;
82 offset() = d;
83 }
84
88 EIGEN_DEVICE_FUNC static inline Hyperplane Through(const VectorType& p0, const VectorType& p1)
89 {
90 Hyperplane result(p0.size());
91 result.normal() = (p1 - p0).unitOrthogonal();
92 result.offset() = -p0.dot(result.normal());
93 return result;
94 }
95
99 EIGEN_DEVICE_FUNC static inline Hyperplane Through(const VectorType& p0, const VectorType& p1, const VectorType& p2)
100 {
101 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 3)
102 Hyperplane result(p0.size());
103 VectorType v0(p2 - p0), v1(p1 - p0);
104 result.normal() = v0.cross(v1);
105 RealScalar norm = result.normal().norm();
106 if(norm <= v0.norm() * v1.norm() * NumTraits<RealScalar>::epsilon())
107 {
110 result.normal() = svd.matrixV().col(2);
111 }
112 else
113 result.normal() /= norm;
114 result.offset() = -p0.dot(result.normal());
115 return result;
116 }
117
122 // FIXME to be consistent with the rest this could be implemented as a static Through function ??
124 {
125 normal() = parametrized.direction().unitOrthogonal();
126 offset() = -parametrized.origin().dot(normal());
127 }
128
129 EIGEN_DEVICE_FUNC ~Hyperplane() {}
130
132 EIGEN_DEVICE_FUNC inline Index dim() const { return AmbientDimAtCompileTime==Dynamic ? m_coeffs.size()-1 : Index(AmbientDimAtCompileTime); }
133
135 EIGEN_DEVICE_FUNC void normalize(void)
136 {
137 m_coeffs /= normal().norm();
138 }
139
143 EIGEN_DEVICE_FUNC inline Scalar signedDistance(const VectorType& p) const { return normal().dot(p) + offset(); }
144
148 EIGEN_DEVICE_FUNC inline Scalar absDistance(const VectorType& p) const { return numext::abs(signedDistance(p)); }
149
152 EIGEN_DEVICE_FUNC inline VectorType projection(const VectorType& p) const { return p - signedDistance(p) * normal(); }
153
157 EIGEN_DEVICE_FUNC inline ConstNormalReturnType normal() const { return ConstNormalReturnType(m_coeffs,0,0,dim(),1); }
158
162 EIGEN_DEVICE_FUNC inline NormalReturnType normal() { return NormalReturnType(m_coeffs,0,0,dim(),1); }
163
167 EIGEN_DEVICE_FUNC inline const Scalar& offset() const { return m_coeffs.coeff(dim()); }
168
171 EIGEN_DEVICE_FUNC inline Scalar& offset() { return m_coeffs(dim()); }
172
176 EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs; }
177
181 EIGEN_DEVICE_FUNC inline Coefficients& coeffs() { return m_coeffs; }
182
189 EIGEN_DEVICE_FUNC VectorType intersection(const Hyperplane& other) const
190 {
191 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(VectorType, 2)
192 Scalar det = coeffs().coeff(0) * other.coeffs().coeff(1) - coeffs().coeff(1) * other.coeffs().coeff(0);
193 // since the line equations ax+by=c are normalized with a^2+b^2=1, the following tests
194 // whether the two lines are approximately parallel.
195 if(internal::isMuchSmallerThan(det, Scalar(1)))
196 { // special case where the two lines are approximately parallel. Pick any point on the first line.
197 if(numext::abs(coeffs().coeff(1))>numext::abs(coeffs().coeff(0)))
198 return VectorType(coeffs().coeff(1), -coeffs().coeff(2)/coeffs().coeff(1)-coeffs().coeff(0));
199 else
200 return VectorType(-coeffs().coeff(2)/coeffs().coeff(0)-coeffs().coeff(1), coeffs().coeff(0));
201 }
202 else
203 { // general case
204 Scalar invdet = Scalar(1) / det;
205 return VectorType(invdet*(coeffs().coeff(1)*other.coeffs().coeff(2)-other.coeffs().coeff(1)*coeffs().coeff(2)),
206 invdet*(other.coeffs().coeff(0)*coeffs().coeff(2)-coeffs().coeff(0)*other.coeffs().coeff(2)));
207 }
208 }
209
216 template<typename XprType>
217 EIGEN_DEVICE_FUNC inline Hyperplane& transform(const MatrixBase<XprType>& mat, TransformTraits traits = Affine)
218 {
219 if (traits==Affine)
220 {
221 normal() = mat.inverse().transpose() * normal();
222 m_coeffs /= normal().norm();
223 }
224 else if (traits==Isometry)
225 normal() = mat * normal();
226 else
227 {
228 eigen_assert(0 && "invalid traits value in Hyperplane::transform()");
229 }
230 return *this;
231 }
232
240 template<int TrOptions>
242 TransformTraits traits = Affine)
243 {
244 transform(t.linear(), traits);
245 offset() -= normal().dot(t.translation());
246 return *this;
247 }
248
254 template<typename NewScalarType>
255 EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<Hyperplane,
261
263 template<typename OtherScalarType,int OtherOptions>
265 { m_coeffs = other.coeffs().template cast<Scalar>(); }
266
271 template<int OtherOptions>
273 { return m_coeffs.isApprox(other.m_coeffs, prec); }
274
275protected:
276
277 Coefficients m_coeffs;
278};
279
280} // end namespace Eigen
281
282#endif // EIGEN_HYPERPLANE_H
EIGEN_DEVICE_FUNC TransposeReturnType transpose()
Definition Transpose.h:182
\geometry_module
Definition Hyperplane.h:35
EIGEN_DEVICE_FUNC Hyperplane(const VectorType &n, const Scalar &d)
Constructs a plane from its normal n and distance to the origin d such that the algebraic equation of...
Definition Hyperplane.h:78
EIGEN_DEVICE_FUNC Hyperplane & transform(const Transform< Scalar, AmbientDimAtCompileTime, Affine, TrOptions > &t, TransformTraits traits=Affine)
Applies the transformation t to *this and returns a reference to *this.
Definition Hyperplane.h:241
EIGEN_DEVICE_FUNC internal::cast_return_type< Hyperplane, Hyperplane< NewScalarType, AmbientDimAtCompileTime, Options > >::type cast() const
Definition Hyperplane.h:256
EIGEN_DEVICE_FUNC Index dim() const
Definition Hyperplane.h:132
EIGEN_DEVICE_FUNC Hyperplane & transform(const MatrixBase< XprType > &mat, TransformTraits traits=Affine)
Applies the transformation matrix mat to *this and returns a reference to *this.
Definition Hyperplane.h:217
EIGEN_DEVICE_FUNC Hyperplane(const VectorType &n, const VectorType &e)
Construct a plane from its normal n and a point e onto the plane.
Definition Hyperplane.h:67
EIGEN_DEVICE_FUNC bool isApprox(const Hyperplane< Scalar, AmbientDimAtCompileTime, OtherOptions > &other, const typename NumTraits< Scalar >::Real &prec=NumTraits< Scalar >::dummy_precision()) const
Definition Hyperplane.h:272
Eigen::Index Index
Definition Hyperplane.h:44
EIGEN_DEVICE_FUNC Scalar & offset()
Definition Hyperplane.h:171
EIGEN_DEVICE_FUNC Scalar signedDistance(const VectorType &p) const
Definition Hyperplane.h:143
EIGEN_DEVICE_FUNC Scalar absDistance(const VectorType &p) const
Definition Hyperplane.h:148
EIGEN_DEVICE_FUNC const Coefficients & coeffs() const
Definition Hyperplane.h:176
EIGEN_DEVICE_FUNC Hyperplane()
Default constructor without initialization.
Definition Hyperplane.h:53
EIGEN_DEVICE_FUNC VectorType intersection(const Hyperplane &other) const
Definition Hyperplane.h:189
EIGEN_DEVICE_FUNC Hyperplane(Index _dim)
Constructs a dynamic-size hyperplane with _dim the dimension of the ambient space.
Definition Hyperplane.h:62
EIGEN_DEVICE_FUNC const Scalar & offset() const
Definition Hyperplane.h:167
EIGEN_DEVICE_FUNC NormalReturnType normal()
Definition Hyperplane.h:162
EIGEN_DEVICE_FUNC Hyperplane(const ParametrizedLine< Scalar, AmbientDimAtCompileTime > &parametrized)
Constructs a hyperplane passing through the parametrized line parametrized.
Definition Hyperplane.h:123
EIGEN_DEVICE_FUNC void normalize(void)
normalizes *this
Definition Hyperplane.h:135
EIGEN_DEVICE_FUNC Coefficients & coeffs()
Definition Hyperplane.h:181
EIGEN_DEVICE_FUNC ConstNormalReturnType normal() const
Definition Hyperplane.h:157
EIGEN_DEVICE_FUNC Hyperplane(const Hyperplane< OtherScalarType, AmbientDimAtCompileTime, OtherOptions > &other)
Copy constructor with scalar type conversion.
Definition Hyperplane.h:264
static EIGEN_DEVICE_FUNC Hyperplane Through(const VectorType &p0, const VectorType &p1)
Constructs a hyperplane passing through the two points.
Definition Hyperplane.h:88
static EIGEN_DEVICE_FUNC Hyperplane Through(const VectorType &p0, const VectorType &p1, const VectorType &p2)
Constructs a hyperplane passing through the three points.
Definition Hyperplane.h:99
EIGEN_DEVICE_FUNC VectorType projection(const VectorType &p) const
Definition Hyperplane.h:152
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
EIGEN_DEVICE_FUNC ScalarBinaryOpTraits< typenameinternal::traits< Derived >::Scalar, typenameinternal::traits< OtherDerived >::Scalar >::ReturnType dot(const MatrixBase< OtherDerived > &other) const
EIGEN_DEVICE_FUNC RealScalar norm() const
Definition Dot.h:103
EIGEN_DEVICE_FUNC const Inverse< Derived > inverse() const
\lu_module
Definition InverseImpl.h:348
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(Index rowId, Index colId) const
This is an overloaded version of DenseCoeffsBase<Derived,ReadOnlyAccessors>::coeff(Index,...
Definition PlainObjectBase.h:152
EIGEN_DEVICE_FUNC PlainObject unitOrthogonal(void) const
\geometry_module
Definition OrthoMethods.h:227
TransformTraits
Enum used to specify how a particular transformation is stored in a matrix.
Definition Constants.h:455
@ ComputeFullV
Used in JacobiSVD to indicate that the square matrix V is to be computed.
Definition Constants.h:397
@ Affine
Transformation is an affine transformation stored as a (Dim+1)^2 matrix whose last row is assumed to ...
Definition Constants.h:460
@ Isometry
Transformation is an isometry.
Definition Constants.h:457
Namespace containing all symbols from the Eigen library.
Definition LDLT.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time,...
Definition Constants.h:22
Holds information about the various numeric (i.e.
Definition NumTraits.h:236
Definition XprHelper.h:510