JugiMap Framework
jmVectorShapes.h
1 #ifndef JUGIMAP_VECTOR_SHAPES_H
2 #define JUGIMAP_VECTOR_SHAPES_H
3 
4 #include <vector>
5 
6 #include "jmCommonFunctions.h"
7 #include "jmGlobal.h"
8 
9 
10 
11 namespace jugimap{
12 
13 
14 class JugiMapBinaryLoader;
15 
16 
17 
18 
19 struct TShapePoint : public Vec2f
20 {
21  float distance = 0.0;
22  float angle = 0.0;
23  bool smoothCorner = false;
24 
25  TShapePoint():Vec2f(){}
26  TShapePoint(Vec2f _p): Vec2f(_p){}
27  TShapePoint(Vec2f _p, float _distance) : Vec2f(_p), distance(_distance) {}
28  TShapePoint(Vec2f _p, float _distance, bool _smoothCorner) : Vec2f(_p), distance(_distance), smoothCorner(_smoothCorner){}
29 };
30 
31 
32 
39 {
40 
41  friend class JugiMapBinaryLoader;
42 
43 
44  virtual ~GeometricShape(){}
45 
47  static GeometricShape *Copy(GeometricShape *_geomShape);
48 
49 
53  static void Assign(GeometricShape *sSrc, GeometricShape *sDst);
54 
55 
57  ShapeKind GetKind(){ return kind; }
58 
59 
63  virtual bool IsValid() = 0;
64 
65 
69  bool IsClosed(){ return closed; }
70 
71 
73  void _SetClosed(bool _closed) { closed = _closed; }
74 
75 
79  std::vector<TShapePoint>& GetPath(){ return path; }
80 
81 
85  virtual void RebuildPath() = 0;
86 
87 
91  float GetPathLength(){ return pathLength; }
92 
93 
98  virtual Vec2f GetPathPoint(float p);
99 
100 
106  virtual Vec2f GetPathPoint(float p, float &directionAngle);
107 
108 
109 
110 
111 protected:
113 
114  std::vector<TShapePoint>path;
115  float pathLength = 0.0f;
116  bool closed = false;
117 
118 };
119 
120 
121 
125 {
126 
127  std::vector<Vec2f>vertices; // closed curves have added extra point at the end as duplicate of the first point
128  bool convex = true;
129  bool rectangle = false;
130  Vec2f boundingBoxMin;
131  Vec2f boundingBoxMax;
132 
133 
135  bool IsValid() override { return !vertices.empty(); }
136  void RebuildPath() override;
137 
138  void UpdateBoundingBox();
139 };
140 
141 
142 struct BezierVertex
143 {
144  Vec2f P;
145  Vec2f CPprevious; // used for bezier curves
146  Vec2f CPnext;
147 
148  BezierVertex(Vec2f _P, Vec2f _BPprevious, Vec2f _BPnext) : P(_P),CPprevious(_BPprevious),CPnext(_BPnext){}
149 };
150 
151 
155 {
156  std::vector<BezierVertex>vertices;
157  float toPolyThreshold = 1.0f;
158 
160  bool IsValid() override { return !vertices.empty(); }
161  void RebuildPath() override;
162 
163 private:
164 
165  void _GetShapePointsFromBezierSegment(std::vector<TShapePoint> &path, Vec2f BP1, Vec2f BP2, Vec2f BP3, Vec2f BP4, int i);
166 
167 };
168 
169 
173 {
174  Vec2f center;
175  float a = 0;
176  float b = 0;
177 
178 
179  EllipseShape(){ kind = ShapeKind::ELLIPSE; closed = true; }
180  bool IsValid() override {return a>0 && b>0;}
181  void RebuildPath() override;
182 };
183 
184 
188 {
189  Vec2f point;
190 
192  bool IsValid() override { return true;}
193  void RebuildPath() override;
194 
195 
196  Vec2f GetPathPoint(float p) override { return point;}
197  Vec2f GetPathPoint(float p, float &directionAngle) override { return point;}
198 
199 };
200 
201 
207 {
208 public:
209  friend class JugiMapBinaryLoader;
210 
211 
214 
215 
219  VectorShape(bool _extra, int _dataFlags, const std::vector<jugimap::Parameter> &_parameters) : extra(_extra), dataFlags(_dataFlags), parameters(_parameters){}
220 
221 
223  VectorShape(GeometricShape *_geomShape) : shape(_geomShape){}
224 
225 
227  VectorShape(const VectorShape &vs);
228 
229 
231  ~VectorShape(){if(shape) delete shape;}
232 
233 
234  std::string GetName(){ return name; }
235 
236 
238  GeometricShape * GetGeometricShape() {return shape;}
239 
240 
242  void SetGeometricShape(GeometricShape *_geomShape){ shape = _geomShape;}
243 
244 
246  ShapeKind GetKind() {return shape->GetKind();}
247 
248  /*
250  bool IsProbe() {return extra;}
251  */
252 
254  int GetDataFlags() {return dataFlags;}
255 
256 
258  std::vector<jugimap::Parameter>& GetParameters() {return parameters;}
259 
260 
261  void RebuildPath(){ shape->RebuildPath(); }
262 
263 
265  float GetPathLength() { return shape->GetPathLength();}
266 
267 
269  Vec2f GetPathPoint(float r) { return shape->GetPathPoint(r); }
270 
271 
273  Vec2f GetPathPoint(float r, float &directionAngle) { return shape->GetPathPoint(r,directionAngle); }
274 
275 
276  std::vector<TShapePoint>& GetPath(){ return shape->GetPath(); }
277 
278 
280  bool IsValid() {return shape->IsValid();}
281 
282 
284  bool IsClosed() {return shape->IsClosed();}
285 
286 
287 private:
288  std::string name;
289  int id = 0;
290  GeometricShape * shape = nullptr; // OWNED
291  bool extra = false; // custom identifier
292  int dataFlags = 0; // custom data
293  std::vector<jugimap::Parameter> parameters; // custom parameters
294  float pathLength = 0.0;
295 };
296 
297 
298 
299 
300 
301 }
302 #endif // VECTORSHAPE_H
jugimap::VectorShape::~VectorShape
~VectorShape()
Destructor.
Definition: jmVectorShapes.h:231
jugimap::VectorShape::VectorShape
VectorShape()
Base constructor.
Definition: jmVectorShapes.h:213
jugimap::VectorShape::GetKind
ShapeKind GetKind()
Returns the kind of this shape.
Definition: jmVectorShapes.h:246
jugimap::ShapeKind::BEZIER_POLYCURVE
Bezier polycurve - BezierShape object.
jugimap::SinglePointShape
The single point shape.
Definition: jmVectorShapes.h:187
jugimap::PolyLineShape::RebuildPath
void RebuildPath() override
Rebuild the path from the current shape geometry.
Definition: jmVectorShapes.cpp:288
jugimap::SinglePointShape::IsValid
bool IsValid() override
Returns true if this shape is a valid geometric object; otherwise returns false.
Definition: jmVectorShapes.h:192
jugimap::VectorShape::GetGeometricShape
GeometricShape * GetGeometricShape()
Returns the geometric shape of this vector shape.
Definition: jmVectorShapes.h:238
jugimap::GeometricShape::GetPath
std::vector< TShapePoint > & GetPath()
Returns a reference to the path vector of this geometric shape.
Definition: jmVectorShapes.h:79
jugimap::EllipseShape
The ellipse shape.
Definition: jmVectorShapes.h:172
jugimap::EllipseShape::IsValid
bool IsValid() override
Returns true if this shape is a valid geometric object; otherwise returns false.
Definition: jmVectorShapes.h:180
jugimap::ShapeKind::POLYLINE
Polyline - PolyLineShape object.
jugimap::PolyLineShape
The polyline shape. Defines also a rectangle shape.
Definition: jmVectorShapes.h:124
jugimap::VectorShape::SetGeometricShape
void SetGeometricShape(GeometricShape *_geomShape)
Set the geometric shape of this vector shape to *_geomShape*.
Definition: jmVectorShapes.h:242
jugimap::GeometricShape::GetKind
ShapeKind GetKind()
Returns the kind of this shape.
Definition: jmVectorShapes.h:57
jugimap::VectorShape::GetPathPoint
Vec2f GetPathPoint(float r)
Returns a point along the path at the given parametric position r.
Definition: jmVectorShapes.h:269
jugimap::ShapeKind::ELLIPSE
Ellipse - EllipseShape object.
jugimap::GeometricShape::Copy
static GeometricShape * Copy(GeometricShape *_geomShape)
Create a new geometric object which is a copy of the given *_geomShape*.
Definition: jmVectorShapes.cpp:43
jugimap::KeyCode::P
P.
jugimap::GeometricShape::RebuildPath
virtual void RebuildPath()=0
Rebuild the path from the current shape geometry.
jugimap::PolyLineShape::IsValid
bool IsValid() override
Returns true if this shape is a valid geometric object; otherwise returns false.
Definition: jmVectorShapes.h:135
jugimap::ShapeKind::NOT_DEFINED
The shape kind not defined.
jugimap::VectorShape::VectorShape
VectorShape(GeometricShape *_geomShape)
Create a vector shape with the given *_geomShape*.
Definition: jmVectorShapes.h:223
jugimap::VectorShape::IsValid
bool IsValid()
Returns true if this vector shape has a geometric object which is valid; otherwise returns false.
Definition: jmVectorShapes.h:280
jugimap::BezierShape::IsValid
bool IsValid() override
Returns true if this shape is a valid geometric object; otherwise returns false.
Definition: jmVectorShapes.h:160
jugimap::GeometricShape
The GeometricShape is the base class for geometric shapes.
Definition: jmVectorShapes.h:38
jugimap::VectorShape::GetParameters
std::vector< jugimap::Parameter > & GetParameters()
Returns a reference to the vector of data parameters in this vector shape.
Definition: jmVectorShapes.h:258
jugimap::SinglePointShape::RebuildPath
void RebuildPath() override
Rebuild the path from the current shape geometry.
Definition: jmVectorShapes.cpp:593
jugimap::BezierShape::RebuildPath
void RebuildPath() override
Rebuild the path from the current shape geometry.
Definition: jmVectorShapes.cpp:415
jugimap::SinglePointShape::GetPathPoint
Vec2f GetPathPoint(float p, float &directionAngle) override
Returns a point along the path at the given parametric position p.
Definition: jmVectorShapes.h:197
jugimap::VectorShape::GetDataFlags
int GetDataFlags()
Returns the data flags of this vector shape.
Definition: jmVectorShapes.h:254
jugimap::EllipseShape::RebuildPath
void RebuildPath() override
Rebuild the path from the current shape geometry.
Definition: jmVectorShapes.cpp:537
jugimap::VectorShape::GetPathPoint
Vec2f GetPathPoint(float r, float &directionAngle)
Returns a point along the path at the given parametric position r.
Definition: jmVectorShapes.h:273
jugimap::Vec2f
Vec2< float > Vec2f
Vec2 struct in float precision.
Definition: jmCommon.h:167
jugimap::ShapeKind
ShapeKind
The kinds of geometric shape. Used as identifier for GeometricShape and VectorShape objects.
Definition: jmGlobal.h:133
jugimap::GeometricShape::GetPathPoint
virtual Vec2f GetPathPoint(float p)
Returns a point along the path at the given parametric position p.
Definition: jmVectorShapes.cpp:105
jugimap::GeometricShape::_SetClosed
void _SetClosed(bool _closed)
Set the closed parameter when you create a geometric shape by hand. Do not change it for existing sha...
Definition: jmVectorShapes.h:73
jugimap::VectorShape::IsClosed
bool IsClosed()
Returns true if this vector shape has a geometric object which is closed; otherwise returns false.
Definition: jmVectorShapes.h:284
jugimap::GeometricShape::GetPathLength
float GetPathLength()
Returns the length of the path.
Definition: jmVectorShapes.h:91
jugimap::VectorShape::GetPathLength
float GetPathLength()
Returns the length of this vector shape.
Definition: jmVectorShapes.h:265
jugimap::ShapeKind::SINGLE_POINT
Single point - SinglePointShape object.
jugimap::Vec2
2d vector.
Definition: jmCommon.h:23
jugimap::GeometricShape::IsValid
virtual bool IsValid()=0
Returns true if this shape is a valid geometric object; otherwise returns false.
jugimap::BezierShape
The bezier shape.
Definition: jmVectorShapes.h:154
jugimap::VectorShape
The VectorShape class defines the vector shape from JugiMap Editor.
Definition: jmVectorShapes.h:206
jugimap::VectorShape::VectorShape
VectorShape(bool _extra, int _dataFlags, const std::vector< jugimap::Parameter > &_parameters)
Create a vector shape with the given data parameters *_probe*, *_dataFlags* and *_parameters*.
Definition: jmVectorShapes.h:219
jugimap::JugiMapBinaryLoader
The JugiMapBinaryLoader class loads data from jugimap binary files (.jmb).
Definition: jmMapBinaryLoader.h:37
jugimap::GeometricShape::IsClosed
bool IsClosed()
Returns true if this shape is a closed geometric object; otherwise returns false.
Definition: jmVectorShapes.h:69
jugimap::SinglePointShape::GetPathPoint
Vec2f GetPathPoint(float p) override
Returns a point along the path at the given parametric position p.
Definition: jmVectorShapes.h:196
jugimap::GeometricShape::Assign
static void Assign(GeometricShape *sSrc, GeometricShape *sDst)
Assign the content of the given sSrc to the sDst.
Definition: jmVectorShapes.cpp:74