Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
PointSequence.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_POINT_SEQUENCE_H
22 #define GF_POINT_SEQUENCE_H
23 
24 #include <vector>
25 
26 #include "CoreApi.h"
27 #include "Math.h"
28 #include "Matrix.h"
29 #include "SerializationFwd.h"
30 #include "Span.h"
31 #include "Vector.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
44  class GF_CORE_API PointSequence {
45  public:
49  PointSequence() = default;
50 
57 
64  template<typename Iterator>
65  PointSequence(Iterator first, Iterator last)
66  : m_points(first, last)
67  {
68  }
69 
77  bool isEmpty() const {
78  return m_points.empty();
79  }
80 
86  void addPoint(Vector2f point) {
87  m_points.push_back(point);
88  }
89 
95  std::size_t getPointCount() const {
96  return m_points.size();
97  }
98 
104  Vector2f getPoint(std::size_t index) const;
105 
113  Vector2f getCenter() const;
114 
122  const Vector2f *begin() const {
123  return m_points.data();
124  }
125 
134  return m_points.data();
135  }
136 
144  const Vector2f *end() const {
145  return m_points.data() + m_points.size();
146  }
147 
156  return m_points.data() + m_points.size();
157  }
158 
165  return m_points.front();
166  }
167 
174  return m_points.back();
175  }
176 
182  void applyTransform(const Matrix3f& mat);
183 
189  void simplify(float distance = Epsilon);
190 
191  protected:
195  std::vector<Vector2f>& getRawPoints() {
196  return m_points;
197  }
198 
202  const std::vector<Vector2f>& getRawPoints() const {
203  return m_points;
204  }
205 
206  private:
207  std::vector<Vector2f> m_points;
208  };
209 
214  GF_CORE_API Serializer& operator|(Serializer& ar, const PointSequence& sequence);
215 
220  GF_CORE_API Deserializer& operator|(Deserializer& ar, PointSequence& sequence);
221 
222 #ifndef DOXYGEN_SHOULD_SKIP_THIS
223 }
224 #endif
225 }
226 
227 #endif // GF_POINT_SEQUENCE_H
A deserializer from a binary file.
Definition: Serialization.h:151
constexpr float Epsilon
Machine epsilon.
Definition: Math.h:94
A span.
Definition: Span.h:36
Vector2f * begin()
Get an iterator to the first point.
Definition: PointSequence.h:133
Vector2f * end()
Get an iterator past the last point.
Definition: PointSequence.h:155
std::size_t getPointCount() const
Get the number of points of the sequence.
Definition: PointSequence.h:95
T data[N]
The internal representation of the vector.
Definition: Vector.h:270
const std::vector< Vector2f > & getRawPoints() const
Get the raw container of points.
Definition: PointSequence.h:202
const Vector2f * end() const
Get an iterator past the last point.
Definition: PointSequence.h:144
PointSequence(Iterator first, Iterator last)
Constructor from points.
Definition: PointSequence.h:65
A serializer to a binary file.
Definition: Serialization.h:43
bool isEmpty() const
Check if the sequence is empty.
Definition: PointSequence.h:77
const Vector2f * begin() const
Get an iterator to the first point.
Definition: PointSequence.h:122
The namespace for gf classes.
Definition: Action.h:35
std::vector< Vector2f > & getRawPoints()
Get the raw container of points.
Definition: PointSequence.h:195
Vector2f getFirstPoint() const
Get the first point of the sequence.
Definition: PointSequence.h:164
void addPoint(Vector2f point)
Add a point to the sequence.
Definition: PointSequence.h:86
General purpose math vector.
Definition: Vector.h:61
Vector2f getLastPoint() const
Get the last point of the sequence.
Definition: PointSequence.h:173
A sequence of points.
Definition: PointSequence.h:44