CUGL
Cornell University Game Library
CUPathExtruder.h
1 //
2 // CUPathExtruder.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module is a factory for extruding a path polgyon into a stroke with
6 // width. It has support for joints and end caps.
7 //
8 // The code in this factory is ported from the Kivy implementation of Line in
9 // package kivy.vertex_instructions. We believe that this port is acceptable
10 // within the scope of the Kivy license. There are no specific credits in that
11 // file, so there is no one specific to credit. However, thanks to the Kivy
12 // team for doing the heavy lifting on this method.
13 //
14 // Because they did all the hard work, we will recommend their picture of how
15 // joints and end caps work:
16 //
17 // http://kivy.org/docs/_images/line-instruction.png
18 //
19 // Since math objects are intended to be on the stack, we do not provide
20 // any shared pointer support in this class.
21 //
22 // CUGL zlib License:
23 // This software is provided 'as-is', without any express or implied
24 // warranty. In no event will the authors be held liable for any damages
25 // arising from the use of this software.
26 //
27 // Permission is granted to anyone to use this software for any purpose,
28 // including commercial applications, and to alter it and redistribute it
29 // freely, subject to the following restrictions:
30 //
31 // 1. The origin of this software must not be misrepresented; you must not
32 // claim that you wrote the original software. If you use this software
33 // in a product, an acknowledgment in the product documentation would be
34 // appreciated but is not required.
35 //
36 // 2. Altered source versions must be plainly marked as such, and must not
37 // be misrepresented as being the original software.
38 //
39 // 3. This notice may not be removed or altered from any source distribution.
40 //
41 // Author: Walker White
42 // Version: 6/22/16
43 
44 #ifndef __CU_PATH_EXTRUDER_H__
45 #define __CU_PATH_EXTRUDER_H__
46 
47 #include "../CUPoly2.h"
48 #include "../CUVec2.h"
49 #include <vector>
50 
51 namespace cugl {
52 
53 // Forward reference to the opaque data class.
54 class KivyData;
55 
65 enum class PathJoint : int {
67  NONE = 0,
69  MITRE = 1,
71  BEVEL = 2,
73  ROUND = 3
74 };
75 
85 enum class PathCap :int {
87  NONE = 0,
89  SQUARE = 1,
91  ROUND = 2
92 };
93 
130 #pragma mark Values
131 private:
133  std::vector<Vec2> _input;
135  bool _closed;
136 
138  std::vector<Vec2> _outverts;
140  std::vector<unsigned short> _outindx;
142  bool _calculated;
143 
144 #pragma mark -
145 #pragma mark Constructors
146 public:
151 
161  PathExtruder(const std::vector<Vec2>& points, bool closed) : _calculated(false) {
162  _input = points; _closed = closed;
163  }
164 
177  PathExtruder(const Poly2& poly) : _calculated(false) { _input = poly._vertices; }
178 
183 
184 #pragma mark -
185 #pragma mark Initialization
186 
201  void set(const Poly2& poly);
202 
215  void set(const std::vector<Vec2>& points, bool closed) {
216  reset();
217  _input = points;
218  _closed = closed;
219  }
220 
224  void reset() {
225  _calculated = false;
226  _outverts.clear(); _outindx.clear();
227  }
228 
235  void clear() {
236  _calculated = false;
237  _input.clear(); _outverts.clear(); _outindx.clear();
238  }
239 
240 #pragma mark -
241 #pragma mark Calculation
242 
266  void calculate(float stroke, PathJoint joint=PathJoint::ROUND, PathCap cap = PathCap::ROUND);
267 
268 #pragma mark -
269 #pragma mark Materialization
270 
282  Poly2 getPolygon();
283 
298  Poly2* getPolygon(Poly2* buffer);
299 
300 #pragma mark -
301 #pragma mark Internal Data Generation
302 private:
318  unsigned int computeSize(PathJoint joint, PathCap cap, unsigned int* vcount, unsigned int* icount);
319 
330  void makeSegment(const Vec2& a, const Vec2& b, KivyData* data);
331 
343  bool makeJoint(const Vec2& a, KivyData* data);
344 
357  bool makeMitreJoint(const Vec2& a, float jangle, KivyData* data);
358 
371  bool makeBevelJoint(const Vec2& a, float jangle, KivyData* data);
372 
385  bool makeRoundJoint(const Vec2& a, float jangle, KivyData* data);
386 
396  void makeCaps(int count, KivyData* data);
397 
407  void makeSquareCaps(int count, KivyData* data);
408 
418  void makeRoundCaps(int count, KivyData* data);
419 
430  bool makeLastJoint(KivyData* data);
431 };
432 }
433 
434 #endif /* __CU_PATH_EXTRUDER_H__ */
PathExtruder(const std::vector< Vec2 > &points, bool closed)
Definition: CUPathExtruder.h:161
void set(const std::vector< Vec2 > &points, bool closed)
Definition: CUPathExtruder.h:215
PathJoint
Definition: CUPathExtruder.h:65
Definition: CUPoly2.h:115
void reset()
Definition: CUPathExtruder.h:224
Definition: CUVec2.h:61
~PathExtruder()
Definition: CUPathExtruder.h:182
void calculate(float stroke, PathJoint joint=PathJoint::ROUND, PathCap cap=PathCap::ROUND)
PathExtruder()
Definition: CUPathExtruder.h:150
void clear()
Definition: CUPathExtruder.h:235
PathCap
Definition: CUPathExtruder.h:85
Definition: CUPathExtruder.h:129
void set(const Poly2 &poly)
PathExtruder(const Poly2 &poly)
Definition: CUPathExtruder.h:177
Definition: CUAnimationNode.h:52