CUGL
Cornell University Game Library
CUCubicSpline.h
1 //
2 // CUCubicSpline.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a class that represents a spline of cubic beziers. A
6 // bezier spline is just a sequence of beziers joined together, so that the end
7 // of one is the beginning of the other. Cubic beziers have four control points,
8 // two for the vertex anchors and two for their tangents.
9 //
10 // This class has been purposefully kept lightweight. If you want to draw a
11 // CubicSpline, you will need to allocate a Poly2 value for the spline using
12 // the factory CubicSplineApproximator. We have to turn shapes into polygons
13 // to draw them anyway, and this allows us to do all of the cool things we can
14 // already do with paths, like extrude them or create wireframes.
15 //
16 // Because math objects are intended to be on the stack, we do not provide
17 // any shared pointer support in this class.
18 //
19 // CUGL zlib License:
20 // This software is provided 'as-is', without any express or implied
21 // warranty. In no event will the authors be held liable for any damages
22 // arising from the use of this software.
23 //
24 // Permission is granted to anyone to use this software for any purpose,
25 // including commercial applications, and to alter it and redistribute it
26 // freely, subject to the following restrictions:
27 //
28 // 1. The origin of this software must not be misrepresented; you must not
29 // claim that you wrote the original software. If you use this software
30 // in a product, an acknowledgment in the product documentation would be
31 // appreciated but is not required.
32 //
33 // 2. Altered source versions must be plainly marked as such, and must not
34 // be misrepresented as being the original software.
35 //
36 // 3. This notice may not be removed or altered from any source distribution.
37 //
38 // Author: Walker White
39 // Version: 6/21/16
40 
41 #ifndef __CU_CUBIC_SPLINE_H__
42 #define __CU_CUBIC_SPLINE_H__
43 
44 #include "CUMathBase.h"
45 #include "CUVec2.h"
46 #include <vector>
47 
48 namespace cugl {
49 
50 class Polynomial;
51 
88 class CubicSpline {
89 #pragma mark Values
90 private:
92  int _size;
93 
102  std::vector<Vec2> _points;
103 
108  std::vector<bool> _smooth;
109 
114  bool _closed;
115 
116 
117 #pragma mark -
118 #pragma mark Constructors
119 public:
125  CubicSpline() : _size(0), _closed(false) { }
126 
135  CubicSpline(const Vec2& point) : CubicSpline(point,point) { }
136 
150  CubicSpline(const Vec2& start, const Vec2& end);
151 
169  CubicSpline(const float* points, int size, int offset=0);
170 
186  CubicSpline(const std::vector<float>& points);
187 
203  CubicSpline(const std::vector<Vec2>& points);
204 
210  CubicSpline(const CubicSpline& spline);
211 
216 
217 
218 #pragma mark -
219 #pragma mark Attribute Accessors
220 
228  int getSize() const { return _size; }
229 
244  bool isClosed() const { return _closed; }
245 
260  void setClosed(bool flag);
261 
275  Vec2 getPoint(float tp) const { return getPoint((int)tp,tp-(int)tp); }
276 
294  void setPoint(float tp, const Vec2& point);
295 
307  Vec2 getAnchor(int index) const;
308 
325  void setAnchor(int index, const Vec2& point);
326 
343  bool getSmooth(int index) const;
344 
365  void setSmooth(int index, bool flag);
366 
385  Vec2 getTangent(int index) const;
386 
413  void setTangent(int index, const Vec2& tang, bool symmetric=false);
414 
429  Polynomial getPolynomialX(int segment) const;
430 
445  Polynomial getPolynomialY(int segment) const;
446 
461  const std::vector<Vec2> getControlPoints() const { return _points; }
462 
463 
464 #pragma mark -
465 #pragma mark Anchor Editting Methods
466 
481  int addAnchor(const Vec2& point) { return addAnchor(point,point); }
482 
498  int addAnchor(const Vec2& point, const Vec2& tang);
499 
514  void deleteAnchor(int index);
515 
535  void insertAnchor(float param) { insertAnchor((int)param,param-(int)param); }
536 
540  void clear() {
541  _points.clear(); _smooth.clear();
542  _closed = false; _size = 0;
543  }
544 
545 #pragma mark -
546 #pragma mark Nearest Point Methods
547 
564  Vec2 nearestPoint(const Vec2& point) const { return getPoint(nearestParameter(point)); }
565 
579  float nearestParameter(const Vec2& point) const;
580 
592  int nearestAnchor(const Vec2& point, float threshold) const;
593 
605  int nearestTangent(const Vec2& point, float threshold) const;
606 
607 
608 #pragma mark -
609 #pragma mark Internal Helpers
610 protected:
624  Vec2 getPoint(int segment, float tp) const;
625 
645  void insertAnchor(int segment, float param);
646 
659  void subdivide(int segment, float tp, std::vector<Vec2>& left, std::vector<Vec2>& rght) const {
660  subdivide(_points,6*segment,tp,left,rght);
661  }
662 
680  static void subdivide(const std::vector<Vec2>& src, int soff, float tp,
681  std::vector<Vec2>& left, std::vector<Vec2>& rght);
696  Polynomial getProjectionPolynomial(const Vec2& point, int segment) const;
697 
719  Vec2 getProjectionSlow(const Vec2& point, int segment) const;
720 
747  Vec2 getProjectionFast(const Vec2& point, int segment) const;
748 
749  friend class CubicSplineApproximator;
750 };
751 
752 }
753 #endif /* __CU_CUBIC_SPLINE_H__ */
Vec2 getProjectionSlow(const Vec2 &point, int segment) const
void setClosed(bool flag)
Vec2 getPoint(float tp) const
Definition: CUCubicSpline.h:275
Definition: CUVec2.h:61
int getSize() const
Definition: CUCubicSpline.h:228
int addAnchor(const Vec2 &point)
Definition: CUCubicSpline.h:481
void deleteAnchor(int index)
bool getSmooth(int index) const
Vec2 nearestPoint(const Vec2 &point) const
Definition: CUCubicSpline.h:564
void setTangent(int index, const Vec2 &tang, bool symmetric=false)
const std::vector< Vec2 > getControlPoints() const
Definition: CUCubicSpline.h:461
void clear()
Definition: CUCubicSpline.h:540
void subdivide(int segment, float tp, std::vector< Vec2 > &left, std::vector< Vec2 > &rght) const
Definition: CUCubicSpline.h:659
Polynomial getPolynomialX(int segment) const
Definition: CUPolynomial.h:67
void setPoint(float tp, const Vec2 &point)
Vec2 getAnchor(int index) const
int nearestTangent(const Vec2 &point, float threshold) const
Vec2 getTangent(int index) const
int nearestAnchor(const Vec2 &point, float threshold) const
Vec2 getProjectionFast(const Vec2 &point, int segment) const
~CubicSpline()
Definition: CUCubicSpline.h:215
bool isClosed() const
Definition: CUCubicSpline.h:244
Polynomial getProjectionPolynomial(const Vec2 &point, int segment) const
Polynomial getPolynomialY(int segment) const
Definition: CUCubicSpline.h:88
CubicSpline(const Vec2 &point)
Definition: CUCubicSpline.h:135
float nearestParameter(const Vec2 &point) const
void setAnchor(int index, const Vec2 &point)
Definition: CUAnimationNode.h:52
void insertAnchor(float param)
Definition: CUCubicSpline.h:535
void setSmooth(int index, bool flag)
Definition: CUCubicSplineApproximator.h:70
CubicSpline()
Definition: CUCubicSpline.h:125