Public Types | Public Member Functions | Protected Attributes | Related Functions
vgl_polygon< T > Class Template Reference

Store a polygon. More...

#include <vgl_polygon.h>

List of all members.

Public Types

typedef vgl_point_2d< T > point_t
typedef vcl_vector< point_tsheet_t

Public Member Functions

 vgl_polygon ()
 Default constructor - constructs an empty polygon with no sheets.
 vgl_polygon (unsigned int nr_sheets)
 Construct an empty polygon, setting the number of (empty) sheets.
 vgl_polygon (point_t const p[], int n)
 Construct a single-sheet polygon from a list of n points.
 vgl_polygon (T const *x, T const *y, int n)
 Construct a single-sheet polygon from a list of n points.
 vgl_polygon (T const x_y[], int n)
 Construct a single-sheet polygon from a list of n points, given in (x,y) pairs.
 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.
 vgl_polygon (vcl_vector< sheet_t > const &sheets)
 Construct by specifying all of its sheets.
 vgl_polygon (vgl_polygon const &a)
 ~vgl_polygon ()
bool contains (point_t const &p) const
 Returns true if p(x,y) is inside the polygon, else false.
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.
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.
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.
void clear ()
 Set the number of sheets to zero, so the polygon becomes empty.
void new_sheet ()
 Add a new (empty) sheet to the polygon.
void push_back (T x, T y)
 Add a new point to the last sheet.
void push_back (point_t const &)
 Add a new point to the last sheet.
void push_back (sheet_t const &s)
 Add a pre-existing sheet to the polygon.
unsigned int num_sheets () const
unsigned int num_vertices () const
sheet_toperator[] (int i)
 Get the ith sheet.
sheet_t const & operator[] (int i) const
 Get the ith sheet.
vcl_ostream & print (vcl_ostream &) const
 Pretty print.

Protected Attributes

vcl_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.
template<class T >
vgl_area (vgl_polygon< T > const &poly)
 The area of a polygon.
template<class T >
vgl_area_enforce_orientation (vgl_polygon< T > const &poly)
 The orientation enforced area of a polygon.
template<class T >
vgl_point_2d< T > vgl_centroid (vgl_polygon< T > const &poly)
 The area weighted center of a polygon.
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.
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.
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.
template<class T >
vgl_polygon< T > vgl_convex_hull (vcl_vector< vgl_point_2d< T > > const &points)
 Return a single-sheet polygon which is the smallest one containing all given points.
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.
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.

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());

Definition at line 33 of file vgl_polygon.h.


Member Typedef Documentation

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

Definition at line 36 of file vgl_polygon.h.

template<class T>
typedef vcl_vector<point_t> vgl_polygon< T >::sheet_t

Definition at line 38 of file vgl_polygon.h.


Constructor & Destructor Documentation

template<class T>
vgl_polygon< T >::vgl_polygon ( ) [inline]

Default constructor - constructs an empty polygon with no sheets.

Definition at line 43 of file vgl_polygon.h.

template<class T>
vgl_polygon< T >::vgl_polygon ( unsigned int  nr_sheets) [inline, explicit]

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

Definition at line 46 of file vgl_polygon.h.

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.

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 30 of file vgl_polygon.txx.

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 38 of file vgl_polygon.txx.

template<class T>
vgl_polygon< T >::vgl_polygon ( sheet_t const &  points,
unsigned  n_sheets = 1 
) [inline, explicit]

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 64 of file vgl_polygon.h.

template<class T>
vgl_polygon< T >::vgl_polygon ( vcl_vector< sheet_t > const &  sheets) [inline, explicit]

Construct by specifying all of its sheets.

Definition at line 67 of file vgl_polygon.h.

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

Definition at line 70 of file vgl_polygon.h.

template<class T>
vgl_polygon< T >::~vgl_polygon ( ) [inline]

Definition at line 73 of file vgl_polygon.h.


Member Function Documentation

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 46 of file vgl_polygon.txx.

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 55 of file vgl_polygon.txx.

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 64 of file vgl_polygon.txx.

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

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

Definition at line 96 of file vgl_polygon.h.

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 76 of file vgl_polygon.h.

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

Definition at line 87 of file vgl_polygon.txx.

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 100 of file vgl_polygon.h.

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

Definition at line 111 of file vgl_polygon.h.

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

Definition at line 113 of file vgl_polygon.h.

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

Get the ith sheet.

Definition at line 120 of file vgl_polygon.h.

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

Get the ith sheet.

Definition at line 123 of file vgl_polygon.h.

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

Pretty print.

Definition at line 114 of file vgl_polygon.txx.

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

Add a new point to the last sheet.

Definition at line 75 of file vgl_polygon.txx.

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

Add a new point to the last sheet.

Definition at line 81 of file vgl_polygon.txx.

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 109 of file vgl_polygon.h.


Friends And Related Function Documentation

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

The area of a polygon.

See also:
vgl_area_signed
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 59 of file vgl_area.txx.

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
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

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.

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.

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.

template<class T >
vgl_polygon< T > vgl_convex_hull ( vcl_vector< vgl_point_2d< T > > const &  points) [related]

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

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.

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

template<class T>
vcl_vector<sheet_t> vgl_polygon< T >::sheets_ [protected]

Definition at line 131 of file vgl_polygon.h.


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