Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
Polyline.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
141  bool hasPrevPoint(std::size_t i) const;
142 
151  Vector2f getPrevPoint(std::size_t i) const;
152 
161  bool hasNextPoint(std::size_t i) const;
162 
171  Vector2f getNextPoint(std::size_t i) const;
172 
178  void setType(Type type) {
179  m_type = type;
180  }
181 
187  Type getType() const {
188  return m_type;
189  }
190 
198  bool isLoop() const {
199  return m_type == Loop;
200  }
201 
209  bool isChain() const {
210  return m_type == Chain;
211  }
212 
218  void simplify(float distance = Epsilon);
219 
220  private:
221  std::vector<Vector2f> m_points;
222  Type m_type;
223  };
224 
229  GF_API Serializer& operator|(Serializer& ar, const Polyline& polyline);
230 
235  GF_API Deserializer& operator|(Deserializer& ar, Polyline& polyline);
236 
237 #ifndef DOXYGEN_SHOULD_SKIP_THIS
238 }
239 #endif
240 }
241 
242 #endif // GF_POLYLINE_H
void setType(Type type)
Set the type of the polyline.
Definition: Polyline.h:178
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:187
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:198
bool isChain() const
Check is the polyline is a chain.
Definition: Polyline.h:209
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