Michael Ernst Peter / Eigen
Committer:
ykuroda
Date:
Thu Oct 13 04:07:23 2016 +0000
Revision:
0:13a5d365ba16
First commint, Eigne Matrix Class Library

Who changed what in which revision?

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