CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUSimpleTriangulator.h
1 //
2 // CUSimpleTriangulator.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module is a factory for a very lightweight triangulator. While we
6 // do have access to the very powerful Pol2Tri, that API has a lot of overhead
7 // with it. If you polygon is simple (no holes or self-intersections), this
8 // is good enough for you.
9 //
10 // Because math objects are intended to be on the stack, we do not provide
11 // any shared pointer support in this class.
12 //
13 // This implementation is largely inspired by the LibGDX implementation from
14 // Nicolas Gramlich, Eric Spits, Thomas Cate, and Nathan Sweet.
15 //
16 // CUGL MIT License:
17 // This software is provided 'as-is', without any express or implied
18 // warranty. In no event will the authors be held liable for any damages
19 // arising from the use of this software.
20 //
21 // Permission is granted to anyone to use this software for any purpose,
22 // including commercial applications, and to alter it and redistribute it
23 // freely, subject to the following restrictions:
24 //
25 // 1. The origin of this software must not be misrepresented; you must not
26 // claim that you wrote the original software. If you use this software
27 // in a product, an acknowledgment in the product documentation would be
28 // appreciated but is not required.
29 //
30 // 2. Altered source versions must be plainly marked as such, and must not
31 // be misrepresented as being the original software.
32 //
33 // 3. This notice may not be removed or altered from any source distribution.
34 //
35 // Author: Walker White
36 // Version: 6/22/16
37 
38 #ifndef __CU_SIMPLE_TRIANGULATOR_H__
39 #define __CU_SIMPLE_TRIANGULATOR_H__
40 
41 #include <cugl/math/CUPoly2.h>
42 #include <cugl/math/CUVec2.h>
43 #include <vector>
44 
45 namespace cugl {
46 
67 #pragma mark Values
68 private:
76  enum VertexType {
78  CONCAVE = -1,
80  TANGENTIAL = 0,
82  CONVEX = 1
83  };
84 
86  std::vector<Vec2> _input;
88  std::vector<VertexType> _types;
90  std::vector<unsigned short> _naive;
92  std::vector<unsigned short> _output;
94  bool _calculated;
95 
96 
97 #pragma mark -
98 #pragma mark Constructors
99 public:
104 
113  SimpleTriangulator(const std::vector<Vec2>& points) : _calculated(false) { _input = points; }
114 
126  SimpleTriangulator(const Poly2& poly) : _calculated(false) { _input = poly._vertices; }
127 
132 
133 #pragma mark -
134 #pragma mark Initialization
135 
149  void set(const Poly2& poly) {
150  reset();
151  _input = poly._vertices;
152  }
153 
165  void set(const std::vector<Vec2>& points) {
166  reset();
167  _input = points;
168  }
172  void reset() {
173  _calculated = false;
174  _output.clear(); _naive.clear(); _types.clear();
175  }
176 
183  void clear() {
184  _calculated = false;
185  _input.clear(); _output.clear(); _naive.clear(); _types.clear();
186  }
187 
188 #pragma mark -
189 #pragma mark Calculation
190 
193  void calculate();
194 
195 #pragma mark -
196 #pragma mark Materialization
197 
211  std::vector<unsigned short> getTriangulation() const;
212 
226  size_t getTriangulation(std::vector<unsigned short>& buffer) const;
227 
240  Poly2 getPolygon() const;
241 
256  Poly2* getPolygon(Poly2* buffer) const;
257 
258 #pragma mark -
259 #pragma mark Internal Data Generation
260 private:
273  VertexType computeSpannedAreaType(const Vec2& p1, const Vec2& p2, const Vec2& p3);
274 
282  bool areVerticesClockwise (const std::vector<Vec2>& vertices);
283 
293  void cutEarTip(int earTipIndex);
294 
305  bool isEarTip (int earTipIndex);
306 
316  int findEarTip();
317 
327  VertexType classifyVertex (int index);
328 
339  void computeTriangulation();
340 
348  void trimColinear();
349 
350 };
351 
352 }
353 
354 #endif /* __CU_SIMPLE_TRIANGULATOR_H__ */
355 
cugl::SimpleTriangulator::~SimpleTriangulator
~SimpleTriangulator()
Definition: CUSimpleTriangulator.h:131
cugl::SimpleTriangulator::reset
void reset()
Definition: CUSimpleTriangulator.h:172
cugl::SimpleTriangulator::clear
void clear()
Definition: CUSimpleTriangulator.h:183
cugl::SimpleTriangulator::calculate
void calculate()
cugl::SimpleTriangulator::set
void set(const std::vector< Vec2 > &points)
Definition: CUSimpleTriangulator.h:165
cugl::Poly2
Definition: CUPoly2.h:109
cugl::Vec2
Definition: CUVec2.h:61
cugl::SimpleTriangulator
Definition: CUSimpleTriangulator.h:66
cugl::SimpleTriangulator::SimpleTriangulator
SimpleTriangulator(const Poly2 &poly)
Definition: CUSimpleTriangulator.h:126
cugl::SimpleTriangulator::SimpleTriangulator
SimpleTriangulator()
Definition: CUSimpleTriangulator.h:103
cugl::SimpleTriangulator::set
void set(const Poly2 &poly)
Definition: CUSimpleTriangulator.h:149
cugl::SimpleTriangulator::getPolygon
Poly2 getPolygon() const
cugl::SimpleTriangulator::SimpleTriangulator
SimpleTriangulator(const std::vector< Vec2 > &points)
Definition: CUSimpleTriangulator.h:113
cugl::SimpleTriangulator::getTriangulation
std::vector< unsigned short > getTriangulation() const