Public Types | Public Member Functions | Protected Attributes | Related Functions | List of all members
vgl_polygon< T > Class Template Reference

Store a polygon. More...

#include <vgl_area.h>

Public Types

typedef vgl_point_2d< T > point_t
 
typedef std::vector< point_tsheet_t
 

Public Member Functions

 vgl_polygon ()=default
 Default constructor - constructs an empty polygon with no sheets. More...
 
 vgl_polygon (unsigned int nr_sheets)
 Construct an empty polygon, setting the number of (empty) sheets. More...
 
 vgl_polygon (point_t const p[], int n)
 Construct a single-sheet polygon from a list of n points. More...
 
 vgl_polygon (T const *x, T const *y, int n)
 Construct a single-sheet polygon from a list of n points. More...
 
 vgl_polygon (T const x_y[], int n)
 Construct a single-sheet polygon from a list of n points, given in (x,y) pairs. More...
 
 vgl_polygon (sheet_t const &points, unsigned n_sheets=1)
 Construct a single-sheet polygon from a sheet, i.e., a vector of 2D points. More...
 
 vgl_polygon (std::vector< sheet_t > sheets)
 Construct by specifying all of its sheets. More...
 
 vgl_polygon (vgl_polygon const &a)
 
 ~vgl_polygon ()=default
 
bool contains (point_t const &p) const
 Returns true if p(x,y) is inside the polygon, else false. More...
 
bool contains (T x, T y) const
 
void add_contour (point_t const p[], int n)
 Add a single sheet to this polygon, specified by the given list of n points. More...
 
void add_contour (T const *x, T const *y, int n)
 Add a single sheet to this polygon, specified by the given list of n points. More...
 
void add_contour (T const x_y[], int n)
 Add a single sheet to this polygon, specified by the given list of n (x,y) pairs. More...
 
void clear ()
 Set the number of sheets to zero, so the polygon becomes empty. More...
 
void new_sheet ()
 Add a new (empty) sheet to the polygon. More...
 
void push_back (T x, T y)
 Add a new point to the last sheet. More...
 
void push_back (point_t const &)
 Add a new point to the last sheet. More...
 
void push_back (sheet_t const &s)
 Add a pre-existing sheet to the polygon. More...
 
unsigned int num_sheets () const
 
unsigned int num_vertices () const
 
sheet_toperator[] (int i)
 Get the ith sheet. More...
 
sheet_t const & operator[] (int i) const
 Get the ith sheet. More...
 
std::ostream & print (std::ostream &) const
 Pretty print. More...
 
std::istream & read (std::istream &)
 read this polygon from ascii stream. More...
 

Protected Attributes

std::vector< sheet_tsheets_
 

Related Functions

(Note that these are not member functions.)

template<class T >
vgl_area_signed (vgl_polygon< T > const &poly)
 Computes the signed area of a polygon. More...
 
template<class T >
vgl_area (vgl_polygon< T > const &poly)
 The area of a polygon. More...
 
template<class T >
vgl_area_enforce_orientation (vgl_polygon< T > const &poly)
 The orientation enforced area of a polygon. More...
 
template<class T >
vgl_point_2d< T > vgl_centroid (vgl_polygon< T > const &poly)
 The area weighted center of a polygon. More...
 
template<class T >
vgl_polygon< T > vgl_clip (const vgl_polygon< T > &poly1, const vgl_polygon< T > &poly2, vgl_clip_type op=vgl_clip_type_intersect)
 Clip a polygon against another polygon. More...
 
template<class T >
vgl_polygon< T > vgl_clip (vgl_polygon< T > const &poly1, vgl_polygon< T > const &poly2, vgl_clip_type op, int *p_retval)
 Clip a polygon against another polygon. More...
 
template<class T >
vgl_point_2d< T > vgl_closest_point (vgl_polygon< T > const &poly, vgl_point_2d< T > const &point, bool closed=true)
 Return the point on the given polygon closest to the given point. More...
 
template<class T >
vgl_polygon< T > vgl_convex_hull (std::vector< vgl_point_2d< T > > const &points)
 Return a single-sheet polygon which is the smallest one containing all given points. More...
 
template<class T >
double vgl_distance (vgl_polygon< T > const &poly, vgl_point_2d< T > const &point, bool closed=true)
 distance between a point and the closest point on the polygon. More...
 
template<class T >
bool vgl_polygon_test_inside (T const *xs, T const *ys, unsigned n, T x, T y)
 return true iff (x, y) is inside (or on boundary of) the given n-gon. More...
 

Detailed Description

template<class T>
class vgl_polygon< T >

Store a polygon.

May have holes or multiple sections. The polygon is stored as a list of "sheets", each sheet is a list of 2d points. Iterate through all points using

for (unsigned int s = 0; s < polygon.num_sheets(); ++s) for (unsigned int p = 0; p < polygon[s].size(); ++p) do_something(polygon[s][p].x(), polygon[s][p].y());

Note: area is not defined on the polygon class to keep a clean interface see vgl_area<T>

Definition at line 6 of file vgl_area.h.

Member Typedef Documentation

◆ point_t

template<class T>
typedef vgl_point_2d<T> vgl_polygon< T >::point_t

Definition at line 41 of file vgl_polygon.h.

◆ sheet_t

template<class T>
typedef std::vector<point_t> vgl_polygon< T >::sheet_t

Definition at line 43 of file vgl_polygon.h.

Constructor & Destructor Documentation

◆ vgl_polygon() [1/8]

template<class T>
vgl_polygon< T >::vgl_polygon ( )
default

Default constructor - constructs an empty polygon with no sheets.

◆ vgl_polygon() [2/8]

template<class T>
vgl_polygon< T >::vgl_polygon ( unsigned int  nr_sheets)
inlineexplicit

Construct an empty polygon, setting the number of (empty) sheets.

Definition at line 51 of file vgl_polygon.h.

◆ vgl_polygon() [3/8]

template<class T >
vgl_polygon< T >::vgl_polygon ( point_t const  p[],
int  n 
)

Construct a single-sheet polygon from a list of n points.

More sheets can be added later with the add_contour method.

Definition at line 28 of file vgl_polygon.hxx.

◆ vgl_polygon() [4/8]

template<class T >
vgl_polygon< T >::vgl_polygon ( T const *  x,
T const *  y,
int  n 
)

Construct a single-sheet polygon from a list of n points.

More sheets can be added later with the add_contour method.

Definition at line 36 of file vgl_polygon.hxx.

◆ vgl_polygon() [5/8]

template<class T >
vgl_polygon< T >::vgl_polygon ( T const  x_y[],
int  n 
)

Construct a single-sheet polygon from a list of n points, given in (x,y) pairs.

The x_y array should thus be of size 2*n ! More sheets can be added later with the add_contour method.

Definition at line 44 of file vgl_polygon.hxx.

◆ vgl_polygon() [6/8]

template<class T>
vgl_polygon< T >::vgl_polygon ( sheet_t const &  points,
unsigned  n_sheets = 1 
)
inlineexplicit

Construct a single-sheet polygon from a sheet, i.e., a vector of 2D points.

Note: n_sheets is only there to distinguish this from the next constructor for VC6 which seems to have a problem.

Definition at line 69 of file vgl_polygon.h.

◆ vgl_polygon() [7/8]

template<class T>
vgl_polygon< T >::vgl_polygon ( std::vector< sheet_t sheets)
inlineexplicit

Construct by specifying all of its sheets.

Definition at line 72 of file vgl_polygon.h.

◆ vgl_polygon() [8/8]

template<class T>
vgl_polygon< T >::vgl_polygon ( vgl_polygon< T > const &  a)
inline

Definition at line 75 of file vgl_polygon.h.

◆ ~vgl_polygon()

template<class T>
vgl_polygon< T >::~vgl_polygon ( )
default

Member Function Documentation

◆ add_contour() [1/3]

template<class T >
void vgl_polygon< T >::add_contour ( point_t const  p[],
int  n 
)

Add a single sheet to this polygon, specified by the given list of n points.

This increments the number of sheets by one.

Definition at line 52 of file vgl_polygon.hxx.

◆ add_contour() [2/3]

template<class T >
void vgl_polygon< T >::add_contour ( T const *  x,
T const *  y,
int  n 
)

Add a single sheet to this polygon, specified by the given list of n points.

This increments the number of sheets by one.

Definition at line 61 of file vgl_polygon.hxx.

◆ add_contour() [3/3]

template<class T >
void vgl_polygon< T >::add_contour ( T const  x_y[],
int  n 
)

Add a single sheet to this polygon, specified by the given list of n (x,y) pairs.

The x_y array should thus be of size 2*n ! This increments the number of sheets by one.

Definition at line 70 of file vgl_polygon.hxx.

◆ clear()

template<class T>
void vgl_polygon< T >::clear ( )
inline

Set the number of sheets to zero, so the polygon becomes empty.

Definition at line 101 of file vgl_polygon.h.

◆ contains() [1/2]

template<class T>
bool vgl_polygon< T >::contains ( point_t const &  p) const
inline

Returns true if p(x,y) is inside the polygon, else false.

Definition at line 81 of file vgl_polygon.h.

◆ contains() [2/2]

template<class T >
bool vgl_polygon< T >::contains ( x,
y 
) const

Definition at line 93 of file vgl_polygon.hxx.

◆ new_sheet()

template<class T>
void vgl_polygon< T >::new_sheet ( )
inline

Add a new (empty) sheet to the polygon.

This increments the number of sheets by one.

Definition at line 105 of file vgl_polygon.h.

◆ num_sheets()

template<class T>
unsigned int vgl_polygon< T >::num_sheets ( ) const
inline

Definition at line 116 of file vgl_polygon.h.

◆ num_vertices()

template<class T>
unsigned int vgl_polygon< T >::num_vertices ( ) const
inline

Definition at line 118 of file vgl_polygon.h.

◆ operator[]() [1/2]

template<class T>
sheet_t& vgl_polygon< T >::operator[] ( int  i)
inline

Get the ith sheet.

Definition at line 124 of file vgl_polygon.h.

◆ operator[]() [2/2]

template<class T>
sheet_t const& vgl_polygon< T >::operator[] ( int  i) const
inline

Get the ith sheet.

Definition at line 127 of file vgl_polygon.h.

◆ print()

template<class T >
std::ostream & vgl_polygon< T >::print ( std::ostream &  os) const

Pretty print.

Definition at line 120 of file vgl_polygon.hxx.

◆ push_back() [1/3]

template<class T >
void vgl_polygon< T >::push_back ( x,
y 
)

Add a new point to the last sheet.

Definition at line 81 of file vgl_polygon.hxx.

◆ push_back() [2/3]

template<class T >
void vgl_polygon< T >::push_back ( point_t const &  p)

Add a new point to the last sheet.

Definition at line 87 of file vgl_polygon.hxx.

◆ push_back() [3/3]

template<class T>
void vgl_polygon< T >::push_back ( sheet_t const &  s)
inline

Add a pre-existing sheet to the polygon.

Definition at line 114 of file vgl_polygon.h.

◆ read()

template<class T >
std::istream & vgl_polygon< T >::read ( std::istream &  is)

read this polygon from ascii stream.

Definition at line 142 of file vgl_polygon.hxx.

Friends And Related Function Documentation

◆ vgl_area()

template<class T >
T vgl_area ( vgl_polygon< T > const &  poly)
related

The area of a polygon.

See also
vgl_area_signed

◆ vgl_area_enforce_orientation()

template<class T >
T vgl_area_enforce_orientation ( vgl_polygon< T > const &  poly)
related

The orientation enforced area of a polygon.

Note
This method assumes that the polygon is simple (i.e. no crossings) and the correct orientation is 'enforced' on the polygon (i.e. holes are given negative area) to ensure that the resultant area is correct
See also
vgl_area

Definition at line 61 of file vgl_area.hxx.

◆ vgl_area_signed()

template<class T >
T vgl_area_signed ( vgl_polygon< T > const &  poly)
related

Computes the signed area of a polygon.

The sign is positive if the polygon is oriented counter-clockwise (in RH coordinate system) and negative otherwise. For "polygons" consisting of more than one contour, the result is valid only if the contours are oriented consistently. That is, an enclosed contour must have opposite orientation to the enclosing contour.

See also
vgl_area

◆ vgl_centroid()

template<class T >
vgl_point_2d< T > vgl_centroid ( vgl_polygon< T > const &  poly)
related

The area weighted center of a polygon.

In general this is different than the mean of the polygon's vertices

◆ vgl_clip() [1/2]

template<class T >
vgl_polygon< T > vgl_clip ( const vgl_polygon< T > &  poly1,
const vgl_polygon< T > &  poly2,
vgl_clip_type  op = vgl_clip_type_intersect 
)
related

Clip a polygon against another polygon.

The two polygons poly1 and poly2 are combined with each other. The operation (intersection, union, etc) is given by parameter op.

Note
The implementation of this code is based on Alan Murta's GPC library (http://www.cs.man.ac.uk/aig/staff/alan/software/gpc.html) which is free for non-commercial use.

In order to be able to use it, make sure to satisfy the copyright notice, then activate the "BUILD_NONCOMMERCIAL" compiler option.

◆ vgl_clip() [2/2]

template<class T >
vgl_polygon< T > vgl_clip ( vgl_polygon< T > const &  poly1,
vgl_polygon< T > const &  poly2,
vgl_clip_type  op,
int *  p_retval 
)
related

Clip a polygon against another polygon.

Same as vgl_clip( const vgl_polygon<T>& poly1, const vgl_polygon<T>& poly2, vgl_clip_type op = vgl_clip_type_intersect ); but where the fourth parameter is a return flag which is 1 if success, or 0 if the operation faced a geometric degeneracy which could not be handled. In this case, it might be necessary to perturb the input with a tiny amount of random noise and try again.

◆ vgl_closest_point()

template<class T >
vgl_point_2d< T > vgl_closest_point ( vgl_polygon< T > const &  poly,
vgl_point_2d< T > const &  point,
bool  closed = true 
)
related

Return the point on the given polygon closest to the given point.

If the third argument is "false", the edge from last to first point of each polygon sheet is not considered part of the polygon.

◆ vgl_convex_hull()

template<class T >
vgl_polygon< T > vgl_convex_hull ( std::vector< vgl_point_2d< T > > const &  points)
related

Return a single-sheet polygon which is the smallest one containing all given points.

◆ vgl_distance()

template<class T >
double vgl_distance ( vgl_polygon< T > const &  poly,
vgl_point_2d< T > const &  point,
bool  closed = true 
)
related

distance between a point and the closest point on the polygon.

If the third argument is "false", the edge from last to first point of each polygon sheet is not considered part of the polygon.

◆ vgl_polygon_test_inside()

template<class T >
bool vgl_polygon_test_inside ( T const *  xs,
T const *  ys,
unsigned  n,
x,
y 
)
related

return true iff (x, y) is inside (or on boundary of) the given n-gon.

Member Data Documentation

◆ sheets_

template<class T>
std::vector<sheet_t> vgl_polygon< T >::sheets_
protected

Definition at line 137 of file vgl_polygon.h.


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