iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nrt::Polygon< T > Class Template Reference

#include <nrt/Core/Geometry/Polygon.H>

template<class T>
class nrt::Polygon< T >

A 2D polygon representation.

Examples:
tests/test-Geometry.C, and tests/test-UKF.C.

Definition at line 52 of file Polygon.H.

Public Member Functions

 Polygon ()
 Default constructor - creates an empty polygon.
 
 Polygon (std::vector< Point2D< T > > const &vertices)
 Create a polygon with the given vertices.
 
 Polygon (std::initializer_list< Point2D< T > > list)
 Create a polygon from a list of vertices.
 
template<class U >
 Polygon (std::vector< Point2D< U > > const &vertices)
 Create a polygon with the given vertices of a different datatype. More...
 
 Polygon (Polygon< T > const &other)
 Copy constructor.
 
 Polygon (Polygon< T > &&other)
 Move constructor.
 
Polygon< T > & operator= (Polygon< T > const &other)
 
template<class U >
 Polygon (Polygon< U > const &other)
 Create a polygon with the given vertices of a different datatype. More...
 
std::vector< Point2D< T > > const & vertices () const
 Get a copy the vertices.
 
size_t size () const
 Get the size (number of vertices)
 
Point2D< T > & vertex (size_t index)
 Access one vertex, read/write version.
 
Point2D< T > const & vertex (size_t index) const
 Access one vertex, read-only version.
 
std::vector< Point2D< T >
>::iterator 
begin ()
 Access the beginning of the underlying vector of vertices.
 
std::vector< Point2D< T >
>::iterator 
end ()
 Access the end of the underlying vector of vertices.
 
std::vector< Point2D< T >
>::const_iterator 
begin () const
 Access the beginning of the underlying vector of vertices - const version.
 
std::vector< Point2D< T >
>::const_iterator 
end () const
 Access the end of the underlying vector of vertices - const version.
 
template<typename promo >
Point2D< typename promote< T,
promo >::type > 
center () const
 Get the location of the center of the polygon's vertices. More...
 
template<typename promo >
Point2D< typename promote< T,
promo >::type > 
centroid () const
 Get the location of the centroid of the polygon. More...
 
double area (const bool getsigned=false) const
 Get the area of the polygon. More...
 
template<class U >
bool contains (Point2D< U > const &point) const
 Detect whether the given point lies inside the polygon.
 
Rectangle< T > boundingRect () const
 Compute the bounding Rectangle around this Polygon.
 
Polygon< T > toConvex () const
 Compute convex hull of this polygon. More...
 
template<class T>
 Polygon (std::initializer_list< nrt::Point2D< T > > list)
 
template<typename promo >
nrt::Point2D< typename
nrt::promote< T, promo >::type > 
center () const
 
template<typename promo >
nrt::Point2D< typename
nrt::promote< T, promo >::type > 
centroid () const
 

Static Public Member Functions

static Polygon< T > convexHull (std::vector< Point2D< T > > const &points)
 Compute the convex hull around an unsorted collection of points. More...
 
static Polygon< T > approximateConvexHull (std::vector< Point2D< T > > const &points, int accuracy=30)
 Compute the approximate convex hull around an unsorted collection of points. More...
 

Protected Member Functions

template<class Archive >
void serialize (Archive &ar)
 Serialization.
 

Friends

class cereal::access
 

Related Functions

(Note that these are not member functions.)

template<class T >
std::ostream & operator<< (std::ostream &out, Polygon< T > const &poly)
 Human-readable output to a stream: outputs {(x1,y1)(x2,y2)...(xN,yN)}. More...
 
template<class T >
Polygon< T > scale (Polygon< T > const &src, double const factor)
 Return a scaled version of the source object. More...
 
template<class T >
Polygon< T > rotate (Polygon< T > const &src, double const angle)
 Return a rotated version of the source object, about its center by a given angle in radians. More...
 
template<class T >
Polygon< T > rotateAbout (Polygon< T > const &src, Point2D< T > const &p, double const angle)
 Return a rotated version of the source object, about the given point and by a given angle in radians. More...
 
template<class T >
bool operator== (Polygon< T > const &lhs, Polygon< T > const &rhs)
 Operator overload for Polygon<T> == Polygon<T> More...
 
template<class T >
bool operator!= (Polygon< T > const &lhs, Polygon< T > const &rhs)
 Operator overload for Polygon<T> != Polygon<T> More...
 
template<class T >
Polygon< T > operator+ (Polygon< T > const &lhs, Point2D< T > const &rhs)
 Operator overload for Polygon<T> + Point2D<T> More...
 
template<class T >
Polygon< T > operator+ (Point2D< T > const &lhs, Polygon< T > const &rhs)
 Operator overload for Point2D<T> + Polygon<T> More...
 
template<class T >
Polygon< T > operator- (Polygon< T > const &lhs, nrt::Point2D< T > const &rhs)
 Operator overload for Polygon<T> - Point2D<T> More...
 
template<class T >
Polygon< T > operator- (Point2D< T > const &lhs, Polygon< T > const &rhs)
 Operator overload for Point2D<T> - Polygon<T> More...
 
template<class T >
Polygon< T > operator* (Polygon< T > const &lhs, double const rhs)
 Operator overload for Polygon<T> * double. More...
 
template<class T >
Polygon< T > operator* (double const lhs, Polygon< T > const &rhs)
 Operator overload for double * Polygon<T> More...
 
template<class T >
Polygon< T > operator/ (Polygon< T > const &lhs, double const rhs)
 Operator overload for Polygon<T> / double. More...
 
template<class T >
Polygon< T > & operator*= (Polygon< T > &lhs, double const rhs)
 Operator overload for Polygon<T> *= double. More...
 
template<class T >
Polygon< T > & operator/= (Polygon< T > &lhs, double const rhs)
 Operator overload for Polygon<T> /= double. More...
 
template<class T >
Polygon< T > & operator+= (Polygon< T > &lhs, Point2D< T > const &rhs)
 Operator overload for Polygon<T> += Point2D<T> More...
 
template<class T >
Polygon< T > & operator-= (Polygon< T > &lhs, Point2D< T > const &rhs)
 Operator overload for Polygon<T> -= Point2D<T> More...
 

Constructor & Destructor Documentation

template<class T >
template<class U >
nrt::Polygon< T >::Polygon ( std::vector< Point2D< U > > const &  vertices)
inline

Create a polygon with the given vertices of a different datatype.

This internally will use nrt::clamped_convert<T,U> on each vertex

Definition at line 60 of file PolygonImpl.H.

template<class T >
template<class U >
nrt::Polygon< T >::Polygon ( Polygon< U > const &  other)
inlineexplicit

Create a polygon with the given vertices of a different datatype.

This internally will use nrt::clamped_convert<T,U> on each vertex

Definition at line 67 of file PolygonImpl.H.

References nrt::Polygon< T >::vertices().

Member Function Documentation

template<class T>
template<typename promo >
Point2D<typename promote<T, promo>::type> nrt::Polygon< T >::center ( ) const

Get the location of the center of the polygon's vertices.

Template Parameters
promodefines how the coordinates get promoted over type T internally during our computations. If T is a floating point type, then promo = void is fine, unless you need more precision (e.g., promo is double if T is float). If T is an integer type, you should typically choose float or double for promo, like this:
nrt::Polygon<int> poly { ...initialization values... };
nrt::Point2D<double> c = poly.center<double>();
nrt::Point2D<int> ci(c); // convert coordinates back to integer and round (if needed)
template<class T>
template<typename promo >
Point2D<typename promote<T, promo>::type> nrt::Polygon< T >::centroid ( ) const

Get the location of the centroid of the polygon.

Template Parameters
promodefines how the coordinates get promoted over type T internally during our computations. If T is a floating point type, then promo = void is fine, unless you need more precision (e.g., promo is double if T is float). If T is an integer type, you should typically choose float or double for promo, like this:
nrt::Polygon<int> poly { ...initialization values... };
nrt::Point2D<double> c = poly.centroid<double>();
nrt::Point2D<int> ci(c); // convert coordinates back to integer and round (if needed)
template<class T >
double nrt::Polygon< T >::area ( const bool  getsigned = false) const

Get the area of the polygon.

Precondition
The polygon must be simple (non-self-intersecting)

Definition at line 159 of file PolygonImpl.H.

References nrt::Point2D< T >::x(), and nrt::Point2D< T >::y().

template<class T >
nrt::Polygon< T > nrt::Polygon< T >::toConvex ( ) const

Compute convex hull of this polygon.

This algorithm is taken from Melkman, et al. 1985. Online Construction of the convex hull of a simple polyline

Precondition
The polygon must be non-intersecting for this method to work correctly. If you do not know whether or not your polygon is non-intersecting or not, you should use Polygon<T>::convexHull instead.

Definition at line 228 of file PolygonImpl.H.

template<class T>
nrt::Polygon< T > nrt::Polygon< T >::convexHull ( std::vector< Point2D< T > > const &  points)
static

Compute the convex hull around an unsorted collection of points.

This is an implementation of the method described in "Another Efficient Algorithm for Convex Hulls in Two Dimensions" Andrew, 1979.

This method is fairly fast, as it's runtime is generally bounded by the time needed to sort the given points in increasing x then y coordinates, which is O(n logn).

Parameters
pointsAn unsorted collection of points around which to find an exact convex hull
Examples:
tests/test-Geometry.C.

Definition at line 322 of file PolygonImpl.H.

References nrt::H, nrt::Point2D< T >::x(), and nrt::Point2D< T >::y().

template<class T>
nrt::Polygon< T > nrt::Polygon< T >::approximateConvexHull ( std::vector< Point2D< T > > const &  points,
int  accuracy = 30 
)
static

Compute the approximate convex hull around an unsorted collection of points.

This is an implementation of the method described in "Approximation Algorithms for Convex Hulls" Bently, Faust & Preparata 1982.

This method is very fast, as it's runtime is O(n). The algorithm works by breaking the point group into vertical strips, computing the extrema points in each strip, and then finding the convex hull around those extrema points.

Parameters
pointsAn unsorted collection of points around which to find an approximate convex hull
accuracyThe number of vertical strips to break the point group into. The more strips, the closer the approximation is to a true convex hull.
Examples:
tests/test-Geometry.C.

Definition at line 433 of file PolygonImpl.H.

Friends And Related Function Documentation

template<class T >
std::ostream & operator<< ( std::ostream &  out,
Polygon< T > const &  poly 
)
related

Human-readable output to a stream: outputs {(x1,y1)(x2,y2)...(xN,yN)}.

template<class T >
Polygon< T > scale ( Polygon< T > const &  src,
double const  factor 
)
related

Return a scaled version of the source object.

Note
This does not have the same effect as the multiplication or division operators, which scale everything, while here we only scale the object size without scaling its center coordinates.
template<class T >
Polygon< T > rotate ( Polygon< T > const &  src,
double const  angle 
)
related

Return a rotated version of the source object, about its center by a given angle in radians.

template<class T >
Polygon< T > rotateAbout ( Polygon< T > const &  src,
Point2D< T > const &  p,
double const  angle 
)
related

Return a rotated version of the source object, about the given point and by a given angle in radians.

template<class T >
bool operator== ( Polygon< T > const &  lhs,
Polygon< T > const &  rhs 
)
related

Operator overload for Polygon<T> == Polygon<T>

template<class T >
bool operator!= ( Polygon< T > const &  lhs,
Polygon< T > const &  rhs 
)
related

Operator overload for Polygon<T> != Polygon<T>

template<class T >
Polygon< T > operator+ ( Polygon< T > const &  lhs,
Point2D< T > const &  rhs 
)
related

Operator overload for Polygon<T> + Point2D<T>

template<class T >
Polygon< T > operator+ ( Point2D< T > const &  lhs,
Polygon< T > const &  rhs 
)
related

Operator overload for Point2D<T> + Polygon<T>

template<class T >
Polygon< T > operator- ( Polygon< T > const &  lhs,
nrt::Point2D< T > const &  rhs 
)
related

Operator overload for Polygon<T> - Point2D<T>

template<class T >
Polygon< T > operator- ( Point2D< T > const &  lhs,
Polygon< T > const &  rhs 
)
related

Operator overload for Point2D<T> - Polygon<T>

template<class T >
Polygon< T > operator* ( Polygon< T > const &  lhs,
double const  rhs 
)
related

Operator overload for Polygon<T> * double.

template<class T >
Polygon< T > operator* ( double const  lhs,
Polygon< T > const &  rhs 
)
related

Operator overload for double * Polygon<T>

template<class T >
Polygon< T > operator/ ( Polygon< T > const &  lhs,
double const  rhs 
)
related

Operator overload for Polygon<T> / double.

template<class T >
Polygon< T > & operator*= ( Polygon< T > &  lhs,
double const  rhs 
)
related

Operator overload for Polygon<T> *= double.

template<class T >
Polygon< T > & operator/= ( Polygon< T > &  lhs,
double const  rhs 
)
related

Operator overload for Polygon<T> /= double.

template<class T >
Polygon< T > & operator+= ( Polygon< T > &  lhs,
Point2D< T > const &  rhs 
)
related

Operator overload for Polygon<T> += Point2D<T>

template<class T >
Polygon< T > & operator-= ( Polygon< T > &  lhs,
Point2D< T > const &  rhs 
)
related

Operator overload for Polygon<T> -= Point2D<T>


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