vgl_polygon.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_polygon.h
2 #ifndef vgl_polygon_h_
3 #define vgl_polygon_h_
4 //:
5 // \file
6 // \author awf@robots.ox.ac.uk
7 // \date 02 Apr 2000
8 //
9 // \verbatim
10 // Modifications
11 // Binary IO added and documentation tidied up NPC, 20/03/01
12 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
13 // Nov.2003 - Peter Vanroose - made vgl_polygon a templated class and added lost of documentation
14 // Nov.2003 - Peter Vanroose - added constructor (to replace new_polygon from test_driver)
15 // May.2009 - Matt Leotta - added a function to find self-intersections
16 // \endverbatim
17 
18 #include <iosfwd>
19 #include <utility>
20 #include <vector>
21 #ifdef _MSC_VER
22 # include <vcl_msvc_warnings.h>
23 #endif
24 #include <vgl/vgl_point_2d.h> // needed for std::vector instantiations
25 
26 //: Store a polygon.
27 // May have holes or multiple sections. The polygon is stored as a list
28 // of "sheets", each sheet is a list of 2d points.
29 // Iterate through all points using
30 //
31 // for (unsigned int s = 0; s < polygon.num_sheets(); ++s)
32 // for (unsigned int p = 0; p < polygon[s].size(); ++p)
33 // do_something(polygon[s][p].x(), polygon[s][p].y());
34 //
35 // Note: area is not defined on the polygon class to keep a clean interface
36 // see vgl_area<T>
37 template <class T>
38 class vgl_polygon
39 {
40  public:
42 
43  typedef std::vector<point_t> sheet_t;
44 
45  // Constructors/Destructor---------------------------------------------------
46 
47  //: Default constructor - constructs an empty polygon with no sheets
48  vgl_polygon() = default;
49 
50  //: Construct an empty polygon, setting the number of (empty) sheets
51  explicit vgl_polygon(unsigned int nr_sheets) : sheets_(nr_sheets) {}
52 
53  //: Construct a single-sheet polygon from a list of n points.
54  // More sheets can be added later with the add_contour method.
55  vgl_polygon(point_t const p[], int n);
56 
57  //: Construct a single-sheet polygon from a list of n points.
58  // More sheets can be added later with the add_contour method.
59  vgl_polygon(T const* x, T const* y, int n);
60 
61  //: Construct a single-sheet polygon from a list of n points, given in (x,y) pairs.
62  // The x_y array should thus be of size 2*n !
63  // More sheets can be added later with the add_contour method.
64  vgl_polygon(T const x_y[], int n);
65 
66  //: Construct a single-sheet polygon from a sheet, i.e., a vector of 2D points.
67  // Note: n_sheets is only there to distinguish this from the next constructor for VC6
68  // which seems to have a problem.
69  explicit vgl_polygon(sheet_t const& points, unsigned n_sheets=1) : sheets_(n_sheets,points) {}
70 
71  //: Construct by specifying all of its sheets
72  explicit vgl_polygon(std::vector<sheet_t> sheets) : sheets_(std::move(sheets)) {}
73 
74  // Copy constructor
76 
77  // Destructor
78  ~vgl_polygon() = default;
79 
80  //: Returns true if \a p(x,y) is inside the polygon, else false
81  bool contains(point_t const& p) const { return contains(p.x(),p.y()); }
82 
83  bool contains(T x, T y) const;
84 
85  // creation
86 
87  //: Add a single sheet to this polygon, specified by the given list of n points.
88  // This increments the number of sheets by one.
89  void add_contour(point_t const p[], int n);
90 
91  //: Add a single sheet to this polygon, specified by the given list of n points.
92  // This increments the number of sheets by one.
93  void add_contour(T const* x, T const* y, int n);
94 
95  //: Add a single sheet to this polygon, specified by the given list of n (x,y) pairs.
96  // The x_y array should thus be of size 2*n !
97  // This increments the number of sheets by one.
98  void add_contour(T const x_y[], int n);
99 
100  //: Set the number of sheets to zero, so the polygon becomes empty
101  void clear() { sheets_.resize(0); }
102 
103  //: Add a new (empty) sheet to the polygon.
104  // This increments the number of sheets by one.
105  void new_sheet() { sheets_.push_back(sheet_t()); }
106 
107  //: Add a new point to the last sheet
108  void push_back(T x, T y);
109 
110  //: Add a new point to the last sheet
111  void push_back(point_t const&);
112 
113  //: Add a pre-existing sheet to the polygon
114  void push_back(sheet_t const& s) { sheets_.push_back(s); }
115 
116  inline unsigned int num_sheets() const { return (unsigned int)(sheets_.size()); }
117 
118  inline unsigned int num_vertices() const {
119  unsigned int c=0;
120  for (unsigned int i=0;i<num_sheets();++i) c += (unsigned int)(sheets_[i].size());
121  return c;
122  }
123  //: Get the ith sheet
124  inline sheet_t& operator[](int i) { return sheets_[i]; }
125 
126  //: Get the ith sheet
127  inline sheet_t const& operator[](int i) const { return sheets_[i]; }
128 
129  //: Pretty print
130  std::ostream& print(std::ostream&) const;
131 
132  //: read this polygon from ascii stream
133  std::istream& read(std::istream&);
134  protected:
135 
136  // Data Members--------------------------------------------------------------
137  std::vector<sheet_t> sheets_;
138 };
139 
140 //: A commonly required (single-sheet) polygon representation.
141 template <class T>
143 {
144  int n;
145  T* x;
146  T* y;
147 
148  //: Automatic constructor from a single-sheet polygon
150 
151  //: Automatic constructor from a polygon sheet
153 
154  //: Destructor
156 };
157 
158 //: Compute all self-intersections between all edges on all sheets.
159 // \returns three arrays \a e1, \a e2, and \a ip of equal size.
160 // Corresponding elements from these arrays describe an intersection.
161 // e1[k].first is the sheet index containing edge (e1[k].second, e1[k].second+1)
162 // involved in the k-th intersection. Similarly, e2[k] indexes the other
163 // edge involved in the k-th intersection. The corresponding intersection
164 // point is returned in ip[k].
165 template <class T>
167  std::vector<std::pair<unsigned,unsigned> >& e1,
168  std::vector<std::pair<unsigned,unsigned> >& e2,
169  std::vector<vgl_point_2d<T> >& ip);
170 
171 //these function is used to determine if a polygon is oriented counter clockwise it does this by comparing
172 //the dot product of vertices in the ordered list
173 template <class T>
175 
176 template <class T>
178 
179 
180 // \relatesalso vgl_polygon
181 template <class T>
182 std::ostream& operator<< (std::ostream& os, vgl_polygon<T> const& p) { return p.print(os); }
183 
184 template <class T>
185 std::istream& operator>> (std::istream& is, vgl_polygon<T>& p) { return p.read(is); }
186 
187 #define VGL_POLYGON_INSTANTIATE(T) extern "please include vgl/vgl_polygon.hxx instead"
188 
189 #endif // vgl_polygon_h_
vgl_polygon()=default
Default constructor - constructs an empty polygon with no sheets.
a point in 2D nonhomogeneous space
bool contains(point_t const &p) const
Returns true if p(x,y) is inside the polygon, else false.
Definition: vgl_polygon.h:81
~vgl_polygon()=default
void vgl_selfintersections(vgl_polygon< T > const &p, std::vector< std::pair< unsigned, unsigned > > &e1, std::vector< std::pair< unsigned, unsigned > > &e2, std::vector< vgl_point_2d< T > > &ip)
Compute all self-intersections between all edges on all sheets.
vgl_polygon(vgl_polygon const &a)
Definition: vgl_polygon.h:75
void new_sheet()
Add a new (empty) sheet to the polygon.
Definition: vgl_polygon.h:105
std::ostream & print(std::ostream &) const
Pretty print.
vgl_polygon< T > vgl_reorient_polygon(vgl_polygon< T > const &p)
void push_back(sheet_t const &s)
Add a pre-existing sheet to the polygon.
Definition: vgl_polygon.h:114
void add_contour(point_t const p[], int n)
Add a single sheet to this polygon, specified by the given list of n points.
Definition: vgl_polygon.hxx:52
vgl_polygon(std::vector< sheet_t > sheets)
Construct by specifying all of its sheets.
Definition: vgl_polygon.h:72
bool vgl_polygon_sheet_is_counter_clockwise(std::vector< vgl_point_2d< T > > verts)
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
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.
Definition: vgl_polygon.h:69
A commonly required (single-sheet) polygon representation.
Definition: vgl_polygon.h:142
vgl_point_2d< T > point_t
Definition: vgl_polygon.h:41
vgl_polygon_sheet_as_array(vgl_polygon< T > const &p)
Automatic constructor from a single-sheet polygon.
Type & y()
Definition: vgl_point_2d.h:72
void push_back(T x, T y)
Add a new point to the last sheet.
Definition: vgl_polygon.hxx:81
sheet_t const & operator[](int i) const
Get the ith sheet.
Definition: vgl_polygon.h:127
unsigned int num_vertices() const
Definition: vgl_polygon.h:118
vgl_polygon(unsigned int nr_sheets)
Construct an empty polygon, setting the number of (empty) sheets.
Definition: vgl_polygon.h:51
sheet_t & operator[](int i)
Get the ith sheet.
Definition: vgl_polygon.h:124
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
~vgl_polygon_sheet_as_array()
Destructor.
std::vector< point_t > sheet_t
Definition: vgl_polygon.h:43
unsigned int num_sheets() const
Definition: vgl_polygon.h:116
Store a polygon.
Definition: vgl_area.h:6
std::istream & read(std::istream &)
read this polygon from ascii stream.
std::vector< sheet_t > sheets_
Definition: vgl_polygon.h:137
Type & x()
Definition: vgl_point_2d.h:71
void clear()
Set the number of sheets to zero, so the polygon becomes empty.
Definition: vgl_polygon.h:101