Gamedev Framework (gf)  0.12.0
A C++14 framework for 2D games
Polyline.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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_POLYLINE_H
22 #define GF_POLYLINE_H
23 
24 #include <vector>
25 
26 #include "ArrayRef.h"
27 #include "Math.h"
28 #include "Matrix.h"
29 #include "Portability.h"
30 #include "SerializationFwd.h"
31 #include "Vector.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
45  class GF_API Polyline {
46  public:
50  enum Type {
52  Loop,
53  };
54 
60  Polyline(Type type = Chain)
61  : m_type(type)
62  {
63 
64  }
65 
72  Polyline(ArrayRef<Vector2f> points, Type type = Chain)
73  : m_points(points.begin(), points.end())
74  , m_type(type)
75  {
76 
77  }
78 
86  template<typename Iterator>
87  Polyline(Iterator first, Iterator last, Type type = Chain)
88  : m_points(first, last)
89  , m_type(type)
90  {
91 
92  }
93 
99  void addPoint(Vector2f point);
100 
106  std::size_t getPointCount() const;
107 
113  Vector2f getPoint(std::size_t index) const;
114 
122  const Vector2f *begin() const;
123 
131  const Vector2f *end() const;
132 
140  Vector2f *begin();
141 
149  Vector2f *end();
150 
159  bool hasPrevPoint(std::size_t i) const;
160 
169  Vector2f getPrevPoint(std::size_t i) const;
170 
179  bool hasNextPoint(std::size_t i) const;
180 
189  Vector2f getNextPoint(std::size_t i) const;
190 
196  void setType(Type type) {
197  m_type = type;
198  }
199 
205  Type getType() const {
206  return m_type;
207  }
208 
216  bool isLoop() const {
217  return m_type == Loop;
218  }
219 
227  bool isChain() const {
228  return m_type == Chain;
229  }
230 
236  void simplify(float distance = Epsilon);
237 
238  private:
239  std::vector<Vector2f> m_points;
240  Type m_type;
241  };
242 
247  GF_API Serializer& operator|(Serializer& ar, const Polyline& polyline);
248 
253  GF_API Deserializer& operator|(Deserializer& ar, Polyline& polyline);
254 
255 #ifndef DOXYGEN_SHOULD_SKIP_THIS
256 }
257 #endif
258 }
259 
260 #endif // GF_POLYLINE_H
void setType(Type type)
Set the type of the polyline.
Definition: Polyline.h:196
A deserializer from a binary file.
Definition: Serialization.h:153
Polyline(ArrayRef< Vector2f > points, Type type=Chain)
Constructor from an array.
Definition: Polyline.h:72
The polyline is open.
Definition: Polyline.h:51
Type getType() const
Get the type of the polyline.
Definition: Polyline.h:205
constexpr float Epsilon
Machine epsilon.
Definition: Math.h:74
Polyline(Iterator first, Iterator last, Type type=Chain)
Constructor from points.
Definition: Polyline.h:87
A polyline.
Definition: Polyline.h:45
bool isLoop() const
Check is the polyline is a loop.
Definition: Polyline.h:216
bool isChain() const
Check is the polyline is a chain.
Definition: Polyline.h:227
Polyline(Type type=Chain)
Default constructor.
Definition: Polyline.h:60
A serializer to a binary file.
Definition: Serialization.h:45
The namespace for gf classes.
Definition: Action.h:35
A constant reference to an array and its size.
Definition: ArrayRef.h:42
Type
The type of polyline.
Definition: Polyline.h:50
Deserializer & operator|(Deserializer &ar, T &data)
Definition: SerializationOps.h:315
The polyline is closed.
Definition: Polyline.h:52