CUGL
Cornell University Game Library
CUCubicSplineApproximator.h
1 //
2 // CUCubicSplineApproximator.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module is a factory for producing Poly2 objects from CubicSpline.
6 // In previous interations, this functionality was embedded in the CubicSpline
7 // class. That made that class much more heavyweight than we wanted for a
8 // a simple math class. By separating this out as a factory, we allow ourselves
9 // the option of moving these calculations to a worker thread if necessary.
10 //
11 // Because math objects are intended to be on the stack, we do not provide
12 // any shared pointer support in this class.
13 //
14 // CUGL zlib License:
15 // This software is provided 'as-is', without any express or implied
16 // warranty. In no event will the authors be held liable for any damages
17 // arising from the use of this software.
18 //
19 // Permission is granted to anyone to use this software for any purpose,
20 // including commercial applications, and to alter it and redistribute it
21 // freely, subject to the following restrictions:
22 //
23 // 1. The origin of this software must not be misrepresented; you must not
24 // claim that you wrote the original software. If you use this software
25 // in a product, an acknowledgment in the product documentation would be
26 // appreciated but is not required.
27 //
28 // 2. Altered source versions must be plainly marked as such, and must not
29 // be misrepresented as being the original software.
30 //
31 // 3. This notice may not be removed or altered from any source distribution.
32 //
33 // Author: Walker White
34 // Version: 6/22/16
35 
36 #ifndef __CU_CUBIC_SPLINE_APPROXIMATOR_H__
37 #define __CU_CUBIC_SPLINE_APPROXIMATOR_H__
38 
39 #include "../CUPoly2.h"
40 #include "../CUCubicSpline.h"
41 #include "../CUVec2.h"
42 #include <vector>
43 
45 #define DEFAULT_TOLERANCE 0.25
46 
47 namespace cugl {
48 
71 #pragma mark Values
72 private:
74  const CubicSpline* _spline;
76  std::vector<Vec2> _pointbuff;
78  std::vector<float> _parambuff;
80  bool _closed;
82  bool _calculated;
83 
84 public:
93  enum Criterion {
101 
109 
117  };
118 
119 #pragma mark -
120 #pragma mark Constructors
121 
124  CubicSplineApproximator() : _spline(nullptr), _calculated(false) { }
125 
131  CubicSplineApproximator(const CubicSpline* spline) : _spline(spline), _calculated(false) { }
132 
137 
138 
139 #pragma mark -
140 #pragma mark Initialization
141 
149  void set(const CubicSpline* spline) {
150  reset();
151  _spline = spline;
152  }
153 
160  void reset() {
161  _calculated = false;
162  _pointbuff.clear(); _parambuff.clear();
163  }
164 
171  void clear() {
172  _calculated = false;
173  _spline = nullptr;
174  _pointbuff.clear(); _parambuff.clear();
175  }
176 
177 
178 #pragma mark -
179 #pragma mark Calculation
180 
198  void calculate(Criterion criterion=Criterion::DISTANCE, float tolerance=DEFAULT_TOLERANCE);
199 
200 
201 #pragma mark -
202 #pragma mark Materialization
203 
218  Poly2 getPath() const;
219 
241  Poly2* getPath(Poly2* buffer) const;
242 
258  std::vector<float> getParameters() const;
259 
280  size_t getParameters(std::vector<float> buffer);
281 
298  std::vector<Vec2> getTangents() const;
299 
319  size_t getTangents(std::vector<Vec2> buffer);
320 
340  Poly2* getTangents(Poly2* buffer);
341 
358  std::vector<Vec2> getNormals() const;
359 
379  size_t getNormals(std::vector<Vec2> buffer);
380 
401  Poly2* getNormals(Poly2* buffer);
402 
422  Poly2 getAnchors(float radius, int segments=4) const;
423 
448  Poly2* getAnchors(Poly2* buffer, float radius, int segments=4) const;
449 
469  Poly2 getHandles(float radius, int segments=4) const;
470 
495  Poly2* getHandles(Poly2* buffer, float radius, int segments=4) const;
496 
511  CubicSpline getRefinement() const;
512 
533  CubicSpline* getRefinement(CubicSpline* buffer) const;
534 
535 #pragma mark -
536 #pragma mark Internal Data Generation
537 private:
554  int generate(const std::vector<Vec2>& src, int soff, float tp,
555  float tolerance, Criterion criterion, int depth);
556 
563  const std::vector<Vec2>* getActivePoints() const {
564  return (_calculated ? &_pointbuff : (_spline ? &(_spline->_points) : nullptr));
565  }
566 
572  bool isClosed() const {
573  return (_calculated ? _closed : (_spline ? _spline->_closed : false));
574  }
575 };
576 
577 }
578 #endif /* __CU_CUBIC_SPLINE_APPROXIMATOR_H__ */
CubicSplineApproximator()
Definition: CUCubicSplineApproximator.h:124
Definition: CUPoly2.h:115
std::vector< float > getParameters() const
std::vector< Vec2 > getTangents() const
void clear()
Definition: CUCubicSpline.h:540
Definition: CUCubicSplineApproximator.h:100
std::vector< Vec2 > getNormals() const
Poly2 getHandles(float radius, int segments=4) const
CubicSplineApproximator(const CubicSpline *spline)
Definition: CUCubicSplineApproximator.h:131
void set(const CubicSpline *spline)
Definition: CUCubicSplineApproximator.h:149
Criterion
Definition: CUCubicSplineApproximator.h:93
Definition: CUCubicSpline.h:88
Poly2 getAnchors(float radius, int segments=4) const
CubicSpline getRefinement() const
void clear()
Definition: CUCubicSplineApproximator.h:171
void reset()
Definition: CUCubicSplineApproximator.h:160
Definition: CUCubicSplineApproximator.h:116
Definition: CUAnimationNode.h:52
Definition: CUCubicSplineApproximator.h:108
void calculate(Criterion criterion=Criterion::DISTANCE, float tolerance=DEFAULT_TOLERANCE)
Definition: CUCubicSplineApproximator.h:70