Medusa  1.1
Coordinate Free Mehless Method implementation
mm::PolygonShape< vec_t > Class Template Reference

#include <PolygonShape_fwd.hpp>

Detailed Description

template<typename vec_t>
class mm::PolygonShape< vec_t >

Shape representing a simple (i.e. non self intersecting) nonempty polygon in 2D, which is given as a sequence of points.

Template Parameters
vec_tThis shape class is used only for 2D domains.

Usage example:

PolygonShape<Vec2d> poly({{0.0, -2.0}, {1.0, -1.0}, {2.0, 0.0}, {1.0, 1.0}, {0.0, 2.0},
{-1.0, 1.0}, {-2.0, 0.0}, {-1.0, -1.0}});
if (poly.contains({2.3, 4.5})) {
// do something
}
std::cout << poly << std::endl;
auto d = poly.discretizeBoundaryWithStep(0.1);

Definition at line 27 of file PolygonShape_fwd.hpp.

+ Inheritance diagram for mm::PolygonShape< vec_t >:
+ Collaboration diagram for mm::PolygonShape< vec_t >:

Public Member Functions

 PolygonShape (const std::vector< vec_t > &points)
 Create polygon given a sequence of points. More...
 
const std::vector< vec_t > & points () const
 Get points representing the polygon. More...
 
void setMargin (scalar_t margin) override
 Computes the polygon extended by margin as well. More...
 
bool contains (const vec_t &point) const override
 Winding number test for point in a polygon inclusion (paying respect to margin). More...
 
std::pair< vec_t, vec_t > bbox () const override
 Return the bounding box of the domain. More...
 
DomainDiscretization< vec_t > discretizeBoundaryWithStep (scalar_t step, int type) const override
 Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes. More...
 
DomainDiscretization< vec_t > discretizeBoundaryWithDensity (const std::function< scalar_t(vec_t)> &dr, int type) const override
 Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes. More...
 
PolygonShape< vec_t > * clone () const override
 Polymorphic clone pattern. More...
 
std::ostream & print (std::ostream &os) const override
 Output information about this shape to given output stream os. More...
 
virtual DomainDiscretization< vec_t > discretizeBoundaryWithDensity (const std::function< scalar_t(vec_t)> &dr, int type) const=0
 Discretizes boundary with given density and fill engine. More...
 
DomainDiscretization< vec_t > discretizeBoundaryWithDensity (const std::function< scalar_t(vec_t)> &dr) const
 Overload with default type. More...
 
virtual DomainDiscretization< vec_t > discretizeBoundaryWithStep (scalar_t step, int type) const
 Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes. More...
 
DomainDiscretization< vec_t > discretizeBoundaryWithStep (scalar_t step) const
 Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes. More...
 
scalar_t margin () const
 Returns current margin. More...
 
void toggleMargin ()
 Toggles the margin from positive to negative. More...
 
ShapeUnion< vec_t > add (const DomainShape &other) const
 Returns a shape representing a union of *this and other. More...
 
ShapeUnion< vec_t > operator+ (const DomainShape &other) const
 Operator form of DomainShape::add. More...
 
ShapeDifference< vec_t > subtract (const DomainShape &other) const
 Returns a shape representing a difference of *this and other. More...
 
ShapeDifference< vec_t > operator- (const DomainShape &other) const
 Operator form of DomainShape::subtract. More...
 
virtual std::pair< bool, vec_t > projectPointToBoundary (const vec_t &point, const vec_t &unit_normal) const
 Project point to boundary using bisection along the line define by unit_normal. More...
 
virtual bool hasContains () const
 Return true if shape has contains() method implemented. More...
 
DomainDiscretization< vec_t > discretizeBoundaryWithStep (scalar_t step) const
 Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes. More...
 
virtual DomainDiscretization< vec_t > discretizeWithStep (scalar_t step, int internal_type, int boundary_type) const
 Returns a discretization of this shape with approximately uniform distance step between nodes. More...
 
DomainDiscretization< vec_t > discretizeWithStep (scalar_t step) const
 discretizeWithStep but with default types as assigned by the shape. More...
 
virtual DomainDiscretization< vec_t > discretizeWithDensity (const std::function< scalar_t(vec_t)> &dr, int internal_type, int boundary_type) const
 Returns a discretization of the domain with spatially variable step. More...
 
template<typename func_t , typename fill_t >
DomainDiscretization< vec_t > discretizeWithDensity (const func_t &dr, const fill_t &fill, int internal_type, int boundary_type) const
 Overload for fill engine. More...
 
DomainDiscretization< vec_t > discretizeWithDensity (const std::function< scalar_t(vec_t)> &dr) const
 Overload with default types. More...
 
template<typename func_t , typename fill_t >
DomainDiscretization< vec_t > discretizeWithDensity (const func_t &dr, const fill_t &fill) const
 Overload for fill engine with default types. More...
 
DomainDiscretization< vec_t > discretizeBoundaryWithDensity (const std::function< scalar_t(vec_t)> &dr) const
 Overload with default type. More...
 
TranslatedShape< vec_t > translate (const vec_t &a)
 Translate the shape by given vector a. More...
 
RotatedShape< vec_t > rotate (const Eigen::Matrix< scalar_t, dim, dim > &Q)
 Transform the shape by given orthogonal matrix Q. More...
 
RotatedShape< vec_t > rotate (scalar_t angle)
 2D version of rotate accepting an angle. More...
 

Public Types

typedef vec_t::Scalar scalar_t
 Scalar data type used in computation. More...
 
typedef vec_t vector_t
 Vector data type used in computations. More...
 
enum  { dim = vec_t::dim }
 Store dimension of the domain. More...
 

Protected Attributes

scalar_t margin_
 Tolerance for the geometric operation of the domain. More...
 

Private Types

using base_t = DomainShape< vec_t >
 Base class type. More...
 

Private Member Functions

void extendByMargin ()
 Save a version of points extended by margin_ for later contains checks. More...
 

Static Private Member Functions

static scalar_t isLeft (const vec_t &P0, const vec_t &P1, const vec_t &P2)
 Tests if a point is left, on, or right of an infinite line. More...
 

Private Attributes

std::vector< vec_t > points_
 The points that define the polygon, stored in CCW order. More...
 
std::vector< vec_t > points_with_margin_
 Polygon extended by margin_, for contains checks. More...
 

Member Enumeration Documentation

◆ anonymous enum

template<typename vec_t >
anonymous enum
inherited

Store dimension of the domain.

Enumerator
dim 

Dimensionality of the domain.

Definition at line 57 of file DomainShape_fwd.hpp.

Constructor & Destructor Documentation

◆ PolygonShape()

template<typename vec_t >
mm::PolygonShape< vec_t >::PolygonShape ( const std::vector< vec_t > &  points)
explicit

Create polygon given a sequence of points.

The points are stored in CCW order. It is the user's responsibility to ensure the polygon is non self intersecting. Polygon area is computed to ensure that polygon is nonempty and to possibly reverse the point ordering, so that points are stored in counter clockwise (CCW) order.

Definition at line 17 of file PolygonShape.hpp.

Member Function Documentation

◆ add()

template<typename vec_t >
ShapeUnion< vec_t > mm::DomainShape< vec_t >::add ( const DomainShape< vec_t > &  other) const
inherited

Returns a shape representing a union of *this and other.

Definition at line 66 of file DomainShape.hpp.

◆ bbox()

template<typename vec_t >
std::pair< vec_t, vec_t > mm::PolygonShape< vec_t >::bbox ( ) const
overridevirtual

Return the bounding box of the domain.

Bounding box is returned in format bbox() == {{mx, my, ...}, {MX, MY, ...}}, such that mx <= Mx and my <= My etc.\ and that the whole domain is contained in the cuboid [mx, my, ...] x [Mx, My, ...].

Implements mm::DomainShape< vec_t >.

Definition at line 76 of file PolygonShape.hpp.

◆ clone()

template<typename vec_t >
PolygonShape<vec_t>* mm::PolygonShape< vec_t >::clone ( ) const
inlineoverridevirtual

Polymorphic clone pattern.

Implements mm::DomainShape< vec_t >.

Definition at line 81 of file PolygonShape_fwd.hpp.

◆ contains()

template<typename vec_t >
bool mm::PolygonShape< vec_t >::contains ( const vec_t &  point) const
overridevirtual

Winding number test for point in a polygon inclusion (paying respect to margin).

Loosely based on: http://geomalgorithms.com/a03-_inclusion.html

Implements mm::DomainShape< vec_t >.

Definition at line 54 of file PolygonShape.hpp.

◆ discretizeBoundaryWithDensity() [1/4]

template<typename vec_t >
DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeBoundaryWithDensity
inline

Overload with default type.

Definition at line 189 of file DomainShape_fwd.hpp.

◆ discretizeBoundaryWithDensity() [2/4]

template<typename vec_t >
DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeBoundaryWithDensity ( const std::function< scalar_t(vec_t)> &  dr) const
inlineinherited

Overload with default type.

Definition at line 189 of file DomainShape_fwd.hpp.

◆ discretizeBoundaryWithDensity() [3/4]

template<typename vec_t >
DomainDiscretization< vec_t > mm::PolygonShape< vec_t >::discretizeBoundaryWithDensity ( const std::function< scalar_t(vec_t)> &  dr,
int  type 
) const
overridevirtual

Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes.

step must be positive. Added nodes are of type type, which must be non-positive. Value 0 indicates that types are dependant on the implementation of concrete shape.

The boundary points are guaranteed to be added in counter clock-wise order.

Implements mm::DomainShape< vec_t >.

Definition at line 118 of file PolygonShape.hpp.

◆ discretizeBoundaryWithDensity() [4/4]

template<typename vec_t >
virtual DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeBoundaryWithDensity

Discretizes boundary with given density and fill engine.

If type is 0, the underlying shape provides the default.

◆ discretizeBoundaryWithStep() [1/4]

template<typename vec_t >
DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeBoundaryWithStep
inline

Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes.

Node type are decided by the underlying shape.

Definition at line 126 of file DomainShape_fwd.hpp.

◆ discretizeBoundaryWithStep() [2/4]

template<typename vec_t >
DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeBoundaryWithStep ( scalar_t  step) const
inlineinherited

Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes.

Node type are decided by the underlying shape.

Definition at line 126 of file DomainShape_fwd.hpp.

◆ discretizeBoundaryWithStep() [3/4]

template<typename vec_t >
virtual DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeBoundaryWithStep
inline

Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes.

step must be positive. Added nodes are of type type, which must be non-positive. Value 0 indicates that types are dependant on the implementation of concrete shape.

Definition at line 118 of file DomainShape_fwd.hpp.

◆ discretizeBoundaryWithStep() [4/4]

template<typename vec_t >
DomainDiscretization< vec_t > mm::PolygonShape< vec_t >::discretizeBoundaryWithStep ( scalar_t  step,
int  type 
) const
overridevirtual

Returns a discretization of the boundary of this shape with approximately uniform distance step between nodes.

step must be positive. Added nodes are of type type, which must be non-positive. Value 0 indicates that types are dependant on the implementation of concrete shape.

The boundary points are guaranteed to be added in counter clock-wise order.

Reimplemented from mm::DomainShape< vec_t >.

Definition at line 89 of file PolygonShape.hpp.

◆ discretizeWithDensity() [1/4]

template<typename vec_t >
template<typename func_t , typename fill_t >
DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeWithDensity ( const func_t &  dr,
const fill_t &  fill 
) const
inlineinherited

Overload for fill engine with default types.

Definition at line 177 of file DomainShape_fwd.hpp.

◆ discretizeWithDensity() [2/4]

template<typename vec_t >
template<typename func_t , typename fill_t >
DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeWithDensity ( const func_t &  dr,
const fill_t &  fill,
int  internal_type,
int  boundary_type 
) const
inlineinherited

Overload for fill engine.

Definition at line 162 of file DomainShape_fwd.hpp.

◆ discretizeWithDensity() [3/4]

template<typename vec_t >
DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeWithDensity ( const std::function< scalar_t(vec_t)> &  dr) const
inlineinherited

Overload with default types.

Definition at line 170 of file DomainShape_fwd.hpp.

◆ discretizeWithDensity() [4/4]

template<typename vec_t >
DomainDiscretization< vec_t > mm::DomainShape< vec_t >::discretizeWithDensity ( const std::function< scalar_t(vec_t)> &  dr,
int  internal_type,
int  boundary_type 
) const
virtualinherited

Returns a discretization of the domain with spatially variable step.

Parameters
drFunction giving desired internodal distance at each point.
internal_typeUser supplied type of internal nodes. Must be non-negative.
boundary_typeUser supplied type of boundary nodes. Must be non-positive. If any of the types is 0, the underlying shape provides the default.
Returns
Discretization with nodes distributed according to dr.

Reimplemented in mm::TranslatedShape< vec_t >, mm::ShapeUnion< vec_t >, and mm::ShapeDifference< vec_t >.

Definition at line 77 of file DomainShape.hpp.

◆ discretizeWithStep() [1/2]

template<typename vec_t >
DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeWithStep ( scalar_t  step) const
inlineinherited

discretizeWithStep but with default types as assigned by the shape.

Definition at line 144 of file DomainShape_fwd.hpp.

◆ discretizeWithStep() [2/2]

template<typename vec_t >
virtual DomainDiscretization<vec_t> mm::DomainShape< vec_t >::discretizeWithStep ( scalar_t  step,
int  internal_type,
int  boundary_type 
) const
inlinevirtualinherited

Returns a discretization of this shape with approximately uniform distance step between nodes.

step must be positive.

Parameters
stepDesired internodal distance.
internal_typeUser supplied type of internal nodes. Must be non-negative.
boundary_typeUser supplied type of boundary nodes. Must be non-positive. If any of the types is 0, the underlying shape provides the default.

Reimplemented in mm::TranslatedShape< vec_t >, mm::ShapeUnion< vec_t >, mm::BoxShape< vec_t >, mm::ShapeDifference< vec_t >, and mm::BallShape< vec_t >.

Definition at line 137 of file DomainShape_fwd.hpp.

◆ extendByMargin()

template<typename vec_t >
void mm::PolygonShape< vec_t >::extendByMargin
private

Save a version of points extended by margin_ for later contains checks.

Definition at line 39 of file PolygonShape.hpp.

◆ hasContains()

template<typename vec_t >
virtual bool mm::DomainShape< vec_t >::hasContains ( ) const
inlinevirtualinherited

◆ isLeft()

template<typename vec_t >
static scalar_t mm::PolygonShape< vec_t >::isLeft ( const vec_t &  P0,
const vec_t &  P1,
const vec_t &  P2 
)
inlinestaticprivate

Tests if a point is left, on, or right of an infinite line.

Input: three points P0, P1, and P2

Returns
a number > 0 if P2 is left of the line through P0 and P1 (looking from P0 to P1), 0 for P2 on the line P0P1 or a number < 0 for P2 right of the line P0P1.

Definition at line 94 of file PolygonShape_fwd.hpp.

◆ margin()

template<typename vec_t >
scalar_t mm::DomainShape< vec_t >::margin ( ) const
inlineinherited

Returns current margin.

Definition at line 72 of file DomainShape_fwd.hpp.

◆ operator+()

template<typename vec_t >
ShapeUnion<vec_t> mm::DomainShape< vec_t >::operator+ ( const DomainShape< vec_t > &  other) const
inlineinherited

Operator form of DomainShape::add.

See also
add

Definition at line 81 of file DomainShape_fwd.hpp.

◆ operator-()

template<typename vec_t >
ShapeDifference<vec_t> mm::DomainShape< vec_t >::operator- ( const DomainShape< vec_t > &  other) const
inlineinherited

Operator form of DomainShape::subtract.

See also
subtract

Definition at line 86 of file DomainShape_fwd.hpp.

◆ points()

template<typename vec_t >
const std::vector<vec_t>& mm::PolygonShape< vec_t >::points ( ) const
inline

Get points representing the polygon.

Definition at line 53 of file PolygonShape_fwd.hpp.

◆ print()

template<typename vec_t >
std::ostream& mm::PolygonShape< vec_t >::print ( std::ostream &  os) const
inlineoverridevirtual

Output information about this shape to given output stream os.

Implements mm::DomainShape< vec_t >.

Definition at line 83 of file PolygonShape_fwd.hpp.

◆ projectPointToBoundary()

template<typename vec_t >
std::pair< bool, vec_t > mm::DomainShape< vec_t >::projectPointToBoundary ( const vec_t &  point,
const vec_t &  unit_normal 
) const
virtualinherited

Project point to boundary using bisection along the line define by unit_normal.

Definition at line 21 of file DomainShape.hpp.

◆ rotate() [1/2]

template<typename vec_t >
RotatedShape< vec_t > mm::DomainShape< vec_t >::rotate ( const Eigen::Matrix< scalar_t, dim, dim > &  Q)
inherited

Transform the shape by given orthogonal matrix Q.

Note
It is usually faster to first discretize the domain and than transform the whole discretization using DomainDiscretization::rotate.

Definition at line 98 of file DomainShape.hpp.

◆ rotate() [2/2]

template<typename vec_t >
RotatedShape< vec_t > mm::DomainShape< vec_t >::rotate ( scalar_t  angle)
inherited

2D version of rotate accepting an angle.

Definition at line 89 of file DomainShape.hpp.

◆ setMargin()

template<typename vec_t >
void mm::PolygonShape< vec_t >::setMargin ( scalar_t  margin)
overridevirtual

Computes the polygon extended by margin as well.

Reimplemented from mm::DomainShape< vec_t >.

Definition at line 33 of file PolygonShape.hpp.

◆ subtract()

template<typename vec_t >
ShapeDifference< vec_t > mm::DomainShape< vec_t >::subtract ( const DomainShape< vec_t > &  other) const
inherited

Returns a shape representing a difference of *this and other.

Definition at line 71 of file DomainShape.hpp.

◆ toggleMargin()

template<typename vec_t >
void mm::DomainShape< vec_t >::toggleMargin ( )
inlineinherited

Toggles the margin from positive to negative.

Definition at line 77 of file DomainShape_fwd.hpp.

◆ translate()

template<typename vec_t >
TranslatedShape< vec_t > mm::DomainShape< vec_t >::translate ( const vec_t &  a)
inherited

Translate the shape by given vector a.

Note
It is usually faster to first discretize the domain and than translate the whole discretization using DomainDiscretization::translate.

Definition at line 84 of file DomainShape.hpp.

Member Data Documentation

◆ margin_

template<typename vec_t >
scalar_t mm::DomainShape< vec_t >::margin_
protectedinherited

Tolerance for the geometric operation of the domain.

The domain should behave as if it was margin_ thicker. Default margin is 1e-10.

Definition at line 64 of file DomainShape_fwd.hpp.

◆ points_

template<typename vec_t >
std::vector<vec_t> mm::PolygonShape< vec_t >::points_
private

The points that define the polygon, stored in CCW order.

Definition at line 31 of file PolygonShape_fwd.hpp.

◆ points_with_margin_

template<typename vec_t >
std::vector<vec_t> mm::PolygonShape< vec_t >::points_with_margin_
private

Polygon extended by margin_, for contains checks.

It updates along with margin and has the first point repeated at the end.

Definition at line 34 of file PolygonShape_fwd.hpp.


The documentation for this class was generated from the following files: