plane.h 3.84 KB
Newer Older
1 2
/*
 Copyright (C) 2014 Nicolas Mellado <nmellado0@gmail.com>
Nicolas Mellado's avatar
Nicolas Mellado committed
3

4 5 6 7 8 9 10 11 12
 This Source Code Form is subject to the terms of the Mozilla Public
 License, v. 2.0. If a copy of the MPL was not distributed with this
 file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/


#ifndef _GRENAILLE_PLANE_
#define _GRENAILLE_PLANE_

Patrick's avatar
Patrick committed
13
#include "primitive.h" // PrimitiveBase
14
#include <Eigen/Geometry>
Patrick's avatar
Patrick committed
15

16 17 18 19
namespace Grenaille
{

/*!
Nicolas Mellado's avatar
Nicolas Mellado committed
20 21
    \brief Implicit hyperplane defined by an homogeneous vector \f$\mathbf{p}\f$.

22 23
    In n-dimensionnal space, the plane is defined as
    the \f$0\f$-isosurface of the scalar field
Nicolas Mellado's avatar
Nicolas Mellado committed
24 25

    \f$ s_\mathbf{u}(\mathbf{x}) =
26
    \left[ \mathbf{x}^T \; 1 \;\right]^T \cdot \mathbf{p} \f$.
Nicolas Mellado's avatar
Nicolas Mellado committed
27

28
    This class inherits Eigen::Hyperplane.
Nicolas Mellado's avatar
Nicolas Mellado committed
29 30

    This primitive requires the definition of n-dimensionnal vectors
31
    (VectorType) in Concept::PointConcept.
Nicolas Mellado's avatar
Nicolas Mellado committed
32 33

    This primitive provides:
34
    \verbatim PROVIDES_PLANE \endverbatim
Nicolas Mellado's avatar
Nicolas Mellado committed
35

36 37
*/
template < class DataPoint, class _WFunctor, typename T = void  >
38 39
class CompactPlane : public PrimitiveBase<DataPoint, _WFunctor>,
                     public Eigen::Hyperplane<typename DataPoint::Scalar, DataPoint::Dim >
40
{
Patrick's avatar
Patrick committed
41 42
private:

43 44 45 46 47 48
    using Base      = PrimitiveBase<DataPoint, _WFunctor>;

public:
    /// \brief Specialization of Eigen::Hyperplane inherited by Grenaille::CompactPlane
    using EigenBase = Eigen::Hyperplane<typename DataPoint::Scalar, DataPoint::Dim >;

Nicolas Mellado's avatar
Nicolas Mellado committed
49

50 51 52 53 54 55 56 57 58 59
protected:

    enum
    {
        PROVIDES_PLANE /*!< \brief Provides a Plane primitive */
    };

public:

    /*! \brief Scalar type inherited from DataPoint */
Nicolas Mellado's avatar
Nicolas Mellado committed
60
    typedef typename DataPoint::Scalar      Scalar;
61 62
    /*! \brief Vector type inherited from DataPoint */
    typedef typename DataPoint::VectorType  VectorType;
Nicolas Mellado's avatar
Nicolas Mellado committed
63 64
    /*! \brief Matrix type inherited from DataPoint */
    typedef typename DataPoint::MatrixType  MatrixType;
65
    /*! \brief Weight Function */
Patrick's avatar
Patrick committed
66
    typedef _WFunctor                       WFunctor;
67 68 69 70

public:

    /*! \brief Default constructor */
Patrick's avatar
Patrick committed
71
    MULTIARCH inline CompactPlane()
72
        : Base(), EigenBase()
73 74
    {
        resetPrimitive();
75
    }
Nicolas Mellado's avatar
Nicolas Mellado committed
76 77

    /*! \brief Set the scalar field values to 0 and reset the isNormalized()
78 79 80
         status */
    MULTIARCH inline void resetPrimitive()
    {
Patrick's avatar
Patrick committed
81
        Base::resetPrimitive();
82 83
        EigenBase* cc = static_cast<EigenBase*>(this);
        *cc = EigenBase();
84
    }
85 86

    MULTIARCH inline bool operator==(const CompactPlane<DataPoint, WFunctor, T>& other) const{
87
        return EigenBase::isApprox(other);
88 89 90 91 92 93 94
    }

    /*! \brief Comparison operator, convenience function */
    MULTIARCH inline bool operator!=(const CompactPlane<DataPoint, WFunctor, T>& other) const{
        return ! ((*this) == other);
    }

95
    /* \brief Init the plane from a direction and a position
96
       \param _dir Orientation of the plane, does not need to be normalized
97 98
       \param _pos Position of the plane
    */
Nicolas Mellado's avatar
Nicolas Mellado committed
99
    MULTIARCH inline void setPlane (const VectorType& _dir,
100 101
                                    const VectorType& _pos)
    {
102 103
        EigenBase* cc = static_cast<EigenBase*>(this);
        *cc = EigenBase(_dir.normalized(), _pos);
104
    }
Nicolas Mellado's avatar
Nicolas Mellado committed
105

106 107 108
    //! \brief Value of the scalar field at the location \f$ \mathbf{q} \f$
    MULTIARCH inline Scalar potential (const VectorType& _q) const
    {
109 110
        // The potential is the distance from the point to the plane
        return EigenBase::signedDistance(_q);
111
    }
Nicolas Mellado's avatar
Nicolas Mellado committed
112

113 114 115
    //! \brief Project a point on the plane
    MULTIARCH inline VectorType project (const VectorType& _q) const
    {
116 117
        // Project on the normal vector and add the offset value
        return EigenBase::projection(_q);
118
    }
Nicolas Mellado's avatar
Nicolas Mellado committed
119

120 121 122 123
    //! \brief Scalar field gradient direction at \f$ \mathbf{q}\f$
    MULTIARCH inline VectorType primitiveGradient (const VectorType&) const
    {
        // Uniform gradient defined only by the orientation of the plane
124
        return EigenBase::normal();
125 126 127 128 129 130 131
    }


}; //class Plane

}
#endif  // _GRENAILLE_PLANE_