Virtual Testbed
Ship dynamics simulator for extreme conditions
polygon.hh
1 #ifndef VTESTBED_GEOMETRY_POLYGON_HH
2 #define VTESTBED_GEOMETRY_POLYGON_HH
3 
4 #include <algorithm>
5 #include <iosfwd>
6 #include <vector>
7 
8 #include <vtestbed/geometry/line_segment.hh>
9 #include <vtestbed/geometry/math.hh>
10 #include <vtestbed/geometry/plane.hh>
11 #include <vtestbed/geometry/types.hh>
12 
13 namespace vtb {
14 
15  namespace geometry {
16 
17  template <class T, int N>
18  class Polygon: public std::vector<Vertex<T,N>> {
19 
20  private:
22 
23  public:
24  using scalar_type = T;
25  static constexpr const int dimensions = N;
26  using value_type = typename base_type::value_type;
27  using pointer = typename base_type::pointer;
28  using const_pointer = typename base_type::const_pointer;
29  using reference = typename base_type::reference;
30  using const_reference = typename base_type::const_reference;
31  using iterator = typename base_type::iterator;
32  using const_iterator = typename base_type::const_iterator;
33  using size_type = typename base_type::size_type;
34  using difference_type = typename base_type::difference_type;
35  using reverse_iterator = typename base_type::reverse_iterator;
36  using const_reverse_iterator = typename base_type::const_reverse_iterator;
38 
39  public:
40 
41  using base_type::base_type;
42  using base_type::operator=;
43  using base_type::operator[];
44 
45  inline bool
46  closed() const {
47  return !this->empty() && all(this->front() == this->back());
48  }
49 
50  inline void
51  make_closed() {
52  if (this->empty()) { return; }
53  if (all(this->front() == this->back())) { return; }
54  this->emplace_back(this->front());
55  }
56 
57  inline size_t
58  find(const value_type& vertex) const {
59  const auto& vertices = *this;
60  const auto nvertices = vertices.size();
61  for (size_t i=0; i<nvertices; ++i) {
62  const auto& v = vertices[i];
63  if (all(v == vertex)) {
64  return i;
65  }
66  }
68  }
69 
70  inline void flip() { std::reverse(this->begin(), this->end()); }
71 
72  Plane<T,N> plane() const;
73  Polyline<T,N> polyline() const;
74  bool degenerate(scalar_type eps) const;
75  void triangle_fan(triangle_array& triangles) const;
76  triangle_array triangle_fan() const;
77 
84  triangle_array triangles() const;
85 
86  void simplify(T eps);
87  void gift_wrap(scalar_type eps, size_type count);
88  void gnuplot(std::ostream& out) const;
89 
90  };
91 
92  template <class T, int N>
93  struct is_figure<Polygon<T,N>>: public std::true_type {};
94 
95  template <class T> Vertex<T,2> centroid(const Polygon<T,2>& p);
96 
97  }
98 
99 }
100 
101 #endif // vim:filetype=cpp
A polygon composed of line segments.
Definition: polyline.hh:20
triangle_array triangles() const
Uses ear-clipping triangulation.
T max(T... args)
Main namespace.
Definition: convert.hh:9
Three- and two-dimensional plane.
Definition: plane.hh:28