Gorgon Game Engine
PointList.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <exception>
5 
6 #include "Point.h"
7 #include "Line.h"
8 
9 namespace Gorgon { namespace Geometry {
10 
16  template<class P_ = Pointf>
17  class PointList {
18  public:
19 
21  PointList() = default;
22 
24  PointList(std::vector<P_> points) : Points(std::move(points)) { }
25 
27  PointList(std::initializer_list<P_> points) : Points(std::move(points)) { }
28 
30  PointList(const PointList &) = delete;
31 
33  PointList(PointList &&other) {
34  Swap(other);
35  }
36 
38  PointList Duplicate() const {
39  return {Points};
40  }
41 
42 
44  PointList &operator =(const PointList &) = delete;
45 
48  this->Destroy();
49 
50  Swap(right);
51 
52  return *this;
53  }
54 
56  void Destroy() {
57  std::vector<P_> n;
58 
59  using std::swap;
60 
61  swap(n, Points);
62  }
63 
65  void Clear() {
66  Points.clear();
67  }
68 
70  auto GetSize() const {
71  return Points.size();
72  }
73 
75  auto begin() {
76  return Points.begin();
77  }
78 
80  auto end() {
81  return Points.end();
82  }
83 
85  auto begin() const {
86  return Points.begin();
87  }
88 
90  auto end() const {
91  return Points.end();
92  }
93 
95  auto rbegin() {
96  return Points.rbegin();
97  }
98 
100  auto rend() {
101  return Points.rend();
102  }
103 
105  auto rbegin() const {
106  return Points.rbegin();
107  }
108 
110  auto rend() const {
111  return Points.rend();
112  }
113 
115  P_ &operator [](std::size_t index) {
116  return Points[index];
117  }
118 
120  const P_ &operator [](std::size_t index) const {
121  return Points[index];
122  }
123 
125  P_ &Front() {
126  return Points.front();
127  }
128 
130  P_ &Back() {
131  return Points.back();
132  }
133 
135  const P_ &Front() const {
136  return Points.front();
137  }
138 
140  const P_ &Back() const {
141  return Points.back();
142  }
143 
146  P_ &Get(long index) {
147  if(Points.size() == 0)
148  throw std::out_of_range("List is empty");
149 
150  return Points[PositiveMod(index, (long)Points.size())];
151  }
152 
155  const P_ &Get(long index) const {
156  if(Points.size() == 0)
157  throw std::out_of_range("List is empty");
158 
159  return Points[PositiveMod(index, (long)Points.size())];
160  }
161 
165  Line<P_> GetLine(long index) const {
166  if(Points.size() == 0)
167  throw std::out_of_range("List is empty");
168 
169  return {Points[PositiveMod(index, (long)Points.size())], Points[PositiveMod(index+1, (long)Points.size())]};
170  }
171 
173  void Push(P_ point) {
174  Points.push_back(point);
175  }
176 
178  template<class ...T_>
179  void Push(T_&&... params) {
180  Points.emplace_back(std::forward<T_>(params)...);
181  }
182 
184  void Pop() {
185  Points.pop_back();
186  }
187 
191  PointList operator +(const PointList &right) const {
192  PointList ret;
193 
194  long ind = 0;
195  for(const auto &p : *this) {
196  ret.Push(p + right.Get(ind));
197 
198  ind++;
199  }
200 
201  return ret;
202  }
203 
207  PointList &operator +=(const PointList &right) {
208  PointList ret;
209 
210  long ind = 0;
211  for(auto &p : *this) {
212  p += right.Get(ind);
213 
214  ind++;
215  }
216 
217  return *this;
218  }
219 
223  PointList operator -(const PointList &right) const {
224  PointList ret;
225 
226  long ind = 0;
227  for(const auto &p : *this) {
228  ret.Push(p - right.Get(ind));
229 
230  ind++;
231  }
232 
233  return ret;
234  }
235 
239  PointList &operator -=(const PointList &right) {
240  PointList ret;
241 
242  long ind = 0;
243  for(auto &p : *this) {
244  p -= right.Get(ind);
245 
246  ind++;
247  }
248 
249  return *this;
250  }
251 
253  template<class O_>
254  PointList operator +(const O_ &right) const {
255  PointList ret;
256 
257  for(const auto &p : *this) {
258  ret.Push(p + right);
259  }
260 
261  return ret;
262  }
263 
265  template<class O_>
266  PointList &operator +=(const O_ &right) {
267  PointList ret;
268 
269  for(auto &p : *this) {
270  p += right;
271  }
272 
273  return *this;
274  }
275 
277  template<class O_>
278  PointList operator -(const O_ &right) const {
279  PointList ret;
280 
281  for(const auto &p : *this) {
282  ret.Push(p - right);
283  }
284 
285  return ret;
286  }
287 
289  template<class O_>
290  PointList &operator -=(const O_ &right) {
291  PointList ret;
292 
293  for(auto &p : *this) {
294  p -= right;
295  }
296 
297  return *this;
298  }
299 
301  template<class O_>
302  PointList operator *(const O_ &right) const {
303  PointList ret;
304 
305  for(const auto &p : *this) {
306  ret.Push(p * right);
307  }
308 
309  return ret;
310  }
311 
313  template<class O_>
314  PointList &operator *=(const O_ &right) {
315  PointList ret;
316 
317  for(auto &p : *this) {
318  p *= right;
319  }
320 
321  return *this;
322  }
323 
325  template<class O_>
326  PointList operator /(const O_ &right) const {
327  PointList ret;
328 
329  for(const auto &p : *this) {
330  ret.Push(p / right);
331  }
332 
333  return ret;
334  }
335 
337  template<class O_>
338  PointList &operator /=(const O_ &right) {
339  PointList ret;
340 
341  for(auto &p : *this) {
342  p /= right;
343  }
344 
345  return *this;
346  }
347 
349  void Swap(PointList<P_> &right) {
350  using std::swap;
351 
352  swap(this->Points, right.Points);
353  }
354 
356  std::vector<P_> Points;
357  };
358 
360  template<class P_>
361  bool operator <(PointList<P_> &left, const PointList<P_> &right) {
362  return left.Points < right.Points;
363  }
364 
366  template<class P_>
367  bool operator >(PointList<P_> &left, const PointList<P_> &right) {
368  return left.Points > right.Points;
369  }
370 
372  template<class P_>
373  bool operator <=(PointList<P_> &left, const PointList<P_> &right) {
374  return left.Points <= right.Points;
375  }
376 
378  template<class P_>
379  bool operator >=(PointList<P_> &left, const PointList<P_> &right) {
380  return left.Points >= right.Points;
381  }
382 
384  template<class P_>
385  bool operator ==(PointList<P_> &left, const PointList<P_> &right) {
386  return left.Points == right.Points;
387  }
388 
390  template<class P_>
391  bool operator !=(PointList<P_> &left, const PointList<P_> &right) {
392  return left.Points != right.Points;
393  }
394 
395  template<class P_>
396  void swap(PointList<P_> &left, PointList<P_> &right) {
397  left.Swap(right);
398  }
399 
400  //non-member operations: translate, scale, rotate, etc...
401 
402 } }
Gorgon::Geometry::PointList::operator+
PointList operator+(const PointList &right) const
Adds the coordinates of the points on the right list to the left.
Definition: PointList.h:191
Gorgon::Geometry::PointList::end
auto end() const
End iterator to underlying points vector.
Definition: PointList.h:90
Gorgon::Geometry::PointList::operator*
PointList operator*(const O_ &right) const
Adds a point to each element of the list.
Definition: PointList.h:302
Gorgon::Geometry::PointList::operator+=
PointList & operator+=(const PointList &right)
Adds the coordinates of the points on the right list to the left.
Definition: PointList.h:207
Gorgon::Geometry::PointList::Front
P_ & Front()
Accesses the first element in the list.
Definition: PointList.h:125
Gorgon::Geometry::PointList::operator-
PointList operator-(const PointList &right) const
Subtracts the coordinates of the points on the right list to the left.
Definition: PointList.h:223
Gorgon::Geometry::PointList::GetLine
Line< P_ > GetLine(long index) const
Returns the line at the given index.
Definition: PointList.h:165
Gorgon::Geometry::PointList::rbegin
auto rbegin()
Begin iterator to underlying points vector.
Definition: PointList.h:95
Gorgon::Geometry::PointList::Points
std::vector< P_ > Points
Stored points. You may directly use this class as if it is a point vector.
Definition: PointList.h:356
Gorgon::Geometry::PointList::Front
const P_ & Front() const
Accesses the first element in the list.
Definition: PointList.h:135
Gorgon::Geometry::PointList::Pop
void Pop()
Removes the last point from the list.
Definition: PointList.h:184
Gorgon::Geometry::PointList::Push
void Push(P_ point)
Adds a new point to the end of the point list.
Definition: PointList.h:173
Gorgon::Geometry::PointList::Destroy
void Destroy()
Destroys the storage used by this list.
Definition: PointList.h:56
Point.h
contains point class.
Gorgon::Geometry::PointList::operator*=
PointList & operator*=(const O_ &right)
Adds a point to each element of the list.
Definition: PointList.h:314
Gorgon::Geometry::Line
This class represents a set of points.
Definition: Line.h:16
Gorgon::Geometry::PointList::PointList
PointList(const PointList &)=delete
Due to relatively high cost, copying is disabled. Use Duplicate instead.
Gorgon::Geometry::PointList::rend
auto rend() const
End iterator to underlying points vector.
Definition: PointList.h:110
Gorgon::Geometry::PointList::operator=
PointList & operator=(const PointList &)=delete
Due to relatively high cost, copying is disabled. Use Duplicate instead.
Gorgon::Geometry::PointList::operator/=
PointList & operator/=(const O_ &right)
Subtracts a point to each element of the list.
Definition: PointList.h:338
Gorgon::Geometry::PointList::operator/
PointList operator/(const O_ &right) const
Subtracts a point to each element of the list.
Definition: PointList.h:326
Gorgon::Geometry::PointList::PointList
PointList(std::vector< P_ > points)
Implicit vector to PointList casting. You may move in the data using std::move.
Definition: PointList.h:24
Gorgon::Geometry::PointList::rend
auto rend()
End iterator to underlying points vector.
Definition: PointList.h:100
Gorgon::Geometry::operator>=
bool operator>=(PointList< P_ > &left, const PointList< P_ > &right)
Comparison: this operation is expensive: O(n).
Definition: PointList.h:379
Gorgon::Geometry::swap
void swap(PointList< P_ > &left, PointList< P_ > &right)
Definition: PointList.h:396
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::PointList::PointList
PointList(PointList &&other)
Move constructor.
Definition: PointList.h:33
Gorgon::Geometry::PointList::operator[]
P_ & operator[](std::size_t index)
Accesses the elements in the list.
Definition: PointList.h:115
Gorgon::Geometry::PointList::Clear
void Clear()
Clears the elements in this list.
Definition: PointList.h:65
Gorgon::Geometry::PointList::Back
const P_ & Back() const
Accesses the last element in the list.
Definition: PointList.h:140
Gorgon::Geometry::PointList::GetSize
auto GetSize() const
Returns the number of elements in the list.
Definition: PointList.h:70
Gorgon::Geometry::operator!=
bool operator!=(PointList< P_ > &left, const PointList< P_ > &right)
Comparison: this operation is expensive: O(n).
Definition: PointList.h:391
Gorgon::Geometry::PointList::Get
const P_ & Get(long index) const
Returns the element at the given index.
Definition: PointList.h:155
Gorgon::Geometry::PointList::begin
auto begin() const
Begin iterator to underlying points vector.
Definition: PointList.h:85
Gorgon::Geometry::operator==
bool operator==(PointList< P_ > &left, const PointList< P_ > &right)
Comparison: this operation is expensive: O(n).
Definition: PointList.h:385
Gorgon::Geometry::PointList::PointList
PointList()=default
Empty constructor.
Gorgon::Geometry::PointList::Back
P_ & Back()
Accesses the last element in the list.
Definition: PointList.h:130
Gorgon::Geometry::PointList::PointList
PointList(std::initializer_list< P_ > points)
Initializer list.
Definition: PointList.h:27
Gorgon::Geometry::PointList::Push
void Push(T_ &&... params)
Adds a new point to the end of the point list.
Definition: PointList.h:179
Gorgon::Geometry::PointList::Swap
void Swap(PointList< P_ > &right)
Swaps two lists, mainly for move operations.
Definition: PointList.h:349
Gorgon::Geometry::PointList::Duplicate
PointList Duplicate() const
Duplicates this PointList.
Definition: PointList.h:38
Gorgon::Geometry::PointList::rbegin
auto rbegin() const
Begin iterator to underlying points vector.
Definition: PointList.h:105
Gorgon::Geometry::PointList::Get
P_ & Get(long index)
Returns the element at the given index.
Definition: PointList.h:146
Gorgon::Geometry::PointList::end
auto end()
End iterator to underlying points vector.
Definition: PointList.h:80
Gorgon::PositiveMod
T_ PositiveMod(T_ value, T_ mod)
Definition: Types.h:131
Gorgon::Geometry::operator<=
bool operator<=(PointList< P_ > &left, const PointList< P_ > &right)
Comparison: this operation is expensive: O(n).
Definition: PointList.h:373
Line.h
Gorgon::Geometry::PointList::begin
auto begin()
Begin iterator to underlying points vector.
Definition: PointList.h:75
Gorgon::Geometry::operator>
bool operator>(PointList< P_ > &left, const PointList< P_ > &right)
Comparison: this operation is expensive: O(n).
Definition: PointList.h:367
Gorgon::Geometry::PointList::operator-=
PointList & operator-=(const PointList &right)
Subtracts the coordinates of the points on the right list to the left.
Definition: PointList.h:239
Gorgon::Geometry::PointList
This class represents a set of points.
Definition: PointList.h:17
Gorgon::Geometry::operator<
bool operator<(PointList< P_ > &left, const PointList< P_ > &right)
Comparison: this operation is expensive: O(n).
Definition: PointList.h:361