CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 MIT 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 
217  CubicSpline(CubicSpline&& spline) : _size(spline._size), _closed(spline._closed),
218  _points(std::move(spline._points)), _smooth(std::move(spline._smooth)) {}
219 
224 
225 #pragma mark -
226 #pragma mark Assignment Operators
227 
234  CubicSpline& operator=(const CubicSpline& spline) { return set(spline); }
235 
244  _size = spline._size; _closed = spline._closed;
245  _points = std::move(spline._points);
246  _smooth = std::move(spline._smooth);
247  return *this;
248  }
249 
260  CubicSpline& set(const Vec2& point) { return set(point,point); }
261 
277  CubicSpline& set(const Vec2& start, const Vec2& end);
278 
298  CubicSpline& set(const float* points, int size, int offset=0);
299 
317  CubicSpline& set(const std::vector<float>& points);
318 
336  CubicSpline& set(const std::vector<Vec2>& points);
337 
345  CubicSpline& set(const CubicSpline& spline);
346 
347 
348 #pragma mark -
349 #pragma mark Attribute Accessors
350 
358  int getSize() const { return _size; }
359 
374  bool isClosed() const { return _closed; }
375 
390  void setClosed(bool flag);
391 
405  Vec2 getPoint(float tp) const { return getPoint((int)tp,tp-(int)tp); }
406 
424  void setPoint(float tp, const Vec2& point);
425 
437  Vec2 getAnchor(int index) const;
438 
455  void setAnchor(int index, const Vec2& point);
456 
473  bool getSmooth(int index) const;
474 
495  void setSmooth(int index, bool flag);
496 
515  Vec2 getTangent(int index) const;
516 
543  void setTangent(int index, const Vec2& tang, bool symmetric=false);
544 
559  Polynomial getPolynomialX(int segment) const;
560 
575  Polynomial getPolynomialY(int segment) const;
576 
591  const std::vector<Vec2> getControlPoints() const { return _points; }
592 
593 
594 #pragma mark -
595 #pragma mark Anchor Editting Methods
596 
611  int addAnchor(const Vec2& point) { return addAnchor(point,point); }
612 
628  int addAnchor(const Vec2& point, const Vec2& tang);
629 
644  void deleteAnchor(int index);
645 
665  void insertAnchor(float param) { insertAnchor((int)param,param-(int)param); }
666 
670  void clear() {
671  _points.clear(); _smooth.clear();
672  _closed = false; _size = 0;
673  }
674 
675 #pragma mark -
676 #pragma mark Nearest Point Methods
677 
694  Vec2 nearestPoint(const Vec2& point) const { return getPoint(nearestParameter(point)); }
695 
709  float nearestParameter(const Vec2& point) const;
710 
722  int nearestAnchor(const Vec2& point, float threshold) const;
723 
735  int nearestTangent(const Vec2& point, float threshold) const;
736 
737 
738 #pragma mark -
739 #pragma mark Internal Helpers
740 protected:
754  Vec2 getPoint(int segment, float tp) const;
755 
775  void insertAnchor(int segment, float param);
776 
789  void subdivide(int segment, float tp, std::vector<Vec2>& left, std::vector<Vec2>& rght) const {
790  subdivide(_points,6*segment,tp,left,rght);
791  }
792 
810  static void subdivide(const std::vector<Vec2>& src, int soff, float tp,
811  std::vector<Vec2>& left, std::vector<Vec2>& rght);
826  Polynomial getProjectionPolynomial(const Vec2& point, int segment) const;
827 
849  Vec2 getProjectionSlow(const Vec2& point, int segment) const;
850 
877  Vec2 getProjectionFast(const Vec2& point, int segment) const;
878 
879  friend class CubicSplineApproximator;
880 };
881 
882 }
883 #endif /* __CU_CUBIC_SPLINE_H__ */
cugl::CubicSpline::addAnchor
int addAnchor(const Vec2 &point)
Definition: CUCubicSpline.h:611
cugl::CubicSpline::getProjectionSlow
Vec2 getProjectionSlow(const Vec2 &point, int segment) const
cugl::CubicSpline::nearestParameter
float nearestParameter(const Vec2 &point) const
cugl::CubicSpline::getProjectionFast
Vec2 getProjectionFast(const Vec2 &point, int segment) const
cugl::CubicSplineApproximator
Definition: CUCubicSplineApproximator.h:70
cugl::CubicSpline::CubicSpline
CubicSpline(CubicSpline &&spline)
Definition: CUCubicSpline.h:217
cugl::CubicSpline::setTangent
void setTangent(int index, const Vec2 &tang, bool symmetric=false)
cugl::CubicSpline::CubicSpline
CubicSpline()
Definition: CUCubicSpline.h:125
cugl::CubicSpline::setPoint
void setPoint(float tp, const Vec2 &point)
cugl::CubicSpline::getPoint
Vec2 getPoint(float tp) const
Definition: CUCubicSpline.h:405
cugl::CubicSpline::getControlPoints
const std::vector< Vec2 > getControlPoints() const
Definition: CUCubicSpline.h:591
cugl::CubicSpline::deleteAnchor
void deleteAnchor(int index)
cugl::CubicSpline::set
CubicSpline & set(const Vec2 &point)
Definition: CUCubicSpline.h:260
cugl::CubicSpline::getSize
int getSize() const
Definition: CUCubicSpline.h:358
cugl::CubicSpline::operator=
CubicSpline & operator=(const CubicSpline &spline)
Definition: CUCubicSpline.h:234
cugl::CubicSpline::getPolynomialX
Polynomial getPolynomialX(int segment) const
cugl::CubicSpline::~CubicSpline
~CubicSpline()
Definition: CUCubicSpline.h:223
cugl::CubicSpline::isClosed
bool isClosed() const
Definition: CUCubicSpline.h:374
cugl::Polynomial
Definition: CUPolynomial.h:67
cugl::CubicSpline::getProjectionPolynomial
Polynomial getProjectionPolynomial(const Vec2 &point, int segment) const
cugl::CubicSpline
Definition: CUCubicSpline.h:88
cugl::CubicSpline::getAnchor
Vec2 getAnchor(int index) const
cugl::CubicSpline::setClosed
void setClosed(bool flag)
cugl::CubicSpline::subdivide
void subdivide(int segment, float tp, std::vector< Vec2 > &left, std::vector< Vec2 > &rght) const
Definition: CUCubicSpline.h:789
cugl::CubicSpline::nearestTangent
int nearestTangent(const Vec2 &point, float threshold) const
cugl::CubicSpline::getSmooth
bool getSmooth(int index) const
cugl::CubicSpline::CubicSpline
CubicSpline(const Vec2 &point)
Definition: CUCubicSpline.h:135
cugl::CubicSpline::setAnchor
void setAnchor(int index, const Vec2 &point)
cugl::Vec2
Definition: CUVec2.h:61
cugl::CubicSpline::getPolynomialY
Polynomial getPolynomialY(int segment) const
cugl::CubicSpline::setSmooth
void setSmooth(int index, bool flag)
cugl::CubicSpline::operator=
CubicSpline & operator=(CubicSpline &&spline)
Definition: CUCubicSpline.h:243
cugl::CubicSpline::getTangent
Vec2 getTangent(int index) const
cugl::CubicSpline::insertAnchor
void insertAnchor(float param)
Definition: CUCubicSpline.h:665
cugl::CubicSpline::nearestAnchor
int nearestAnchor(const Vec2 &point, float threshold) const
cugl::CubicSpline::clear
void clear()
Definition: CUCubicSpline.h:670
cugl::CubicSpline::nearestPoint
Vec2 nearestPoint(const Vec2 &point) const
Definition: CUCubicSpline.h:694