TinySpline
Spline Library for a Multitude of Programming Languages
v0.3.0
tinysplinecxx.h
1 #pragma once
2 
3 #include "tinyspline.h"
4 #include <vector>
5 #include <string>
6 
7 #ifdef SWIG
8 #define std_real_vector_in std::vector<tinyspline::real> *
9 #define std_real_vector_out std::vector<tinyspline::real> *
10 #else
11 #define std_real_vector_in std::vector<tinyspline::real> &
12 #define std_real_vector_out std::vector<tinyspline::real>
13 #endif
14 
15 namespace tinyspline {
16 
17 typedef tsReal real;
18 class BSpline;
19 
20 class DeBoorNet {
21 public:
22  /* Constructors & Destructors */
23  DeBoorNet(const DeBoorNet &other);
24  ~DeBoorNet();
25 
26  /* Operators */
27  DeBoorNet & operator=(const DeBoorNet &other);
28 
29  /* Accessors */
30  real knot() const;
31  size_t index() const;
32  size_t multiplicity() const;
33  size_t numInsertions() const;
34  size_t dimension() const;
35  std::vector<real> points() const;
36  std::vector<real> result() const;
37  tsDeBoorNet * data();
38 
39  /* Debug */
40  std::string toString() const;
41 
42 private:
43  tsDeBoorNet net;
44 
45  /* Constructors & Destructors */
46  DeBoorNet();
47 
48  friend class BSpline;
49 };
50 
51 class Domain {
52 public:
53  /* Constructors & Destructors */
54  Domain(tsReal min, tsReal max);
55  Domain(const Domain &other);
56 
57  /* Operators */
58  Domain &operator=(const Domain &other);
59 
60  /* Accessors */
61  real min() const;
62  real max() const;
63 
64  /* Debug */
65  std::string toString() const;
66 
67 private:
68  real _min;
69  real _max;
70 };
71 
72 class BSpline {
73 public:
74  typedef tsBSplineType type;
75 
76  /* Constructors & Destructors */
77  BSpline();
78  BSpline(const BSpline &other);
79  explicit BSpline(size_t numControlPoints, size_t dimension = 2,
80  size_t degree = 3,
81  tinyspline::BSpline::type type = TS_CLAMPED);
82  ~BSpline();
83 
84  /* Create from static method */
85  static BSpline interpolateCubicNatural(const std_real_vector_in points,
86  size_t dimension);
87  static BSpline interpolateCatmullRom(const std_real_vector_in points,
88  size_t dimension, tsReal alpha = (tsReal) 0.5f,
89  std::vector<tinyspline::real> *first = NULL,
90  std::vector<tinyspline::real> *last = NULL,
91  tsReal epsilon = TS_CONTROL_POINT_EPSILON);
92  static BSpline fromJson(std::string json);
93  static BSpline load(std::string path);
94 
95  /* Operators */
96  BSpline & operator=(const BSpline &other);
97  DeBoorNet operator()(real u) const;
98 
99  /* Accessors */
100  size_t degree() const;
101  size_t order() const;
102  size_t dimension() const;
103  std::vector<real> controlPoints() const;
104  std_real_vector_out controlPointAt(size_t index) const;
105  std::vector<real> knots() const;
106  real knotAt(size_t index) const;
107  tsBSpline * data();
108 
109  /* Query */
110  size_t numControlPoints() const;
111  DeBoorNet eval(real u) const;
112  std_real_vector_out evalAll(const std_real_vector_in us) const;
113  std_real_vector_out sample(size_t num = 0) const;
114  DeBoorNet bisect(real value, real epsilon = TS_CONTROL_POINT_EPSILON,
115  bool persnickety = false, size_t index = 0,
116  bool ascending = true, size_t maxIter = 30) const;
117  Domain domain() const;
118  bool isClosed(real epsilon = TS_CONTROL_POINT_EPSILON) const;
119 
120  /* Serialization */
121  std::string toJson() const;
122  void save(std::string path) const;
123 
124  /* Modifications */
125  void setControlPoints(const std::vector<real> &ctrlp);
126  void setControlPointAt(size_t index, const std_real_vector_in ctrlp);
127  void setKnots(const std::vector<real> &knots);
128  void setKnotAt(size_t index, real knot);
129 
130  /* Transformations */
131  BSpline insertKnot(real u, size_t n) const;
132  BSpline split(real u) const;
133  BSpline tension(real tension) const;
134  BSpline toBeziers() const;
135  BSpline derive(size_t n = 1,
136  real epsilon = TS_CONTROL_POINT_EPSILON) const;
137 
138  /* Debug */
139  std::string toString() const;
140 
141 private:
142  tsBSpline spline;
143 };
144 
145 class Utils {
146 public:
147  static bool knotsEqual(real x, real y);
148 
149 private:
150  Utils() {}
151 };
152 
153 }
Definition: tinysplinecxx.h:145
Definition: tinyspline.h:309
Definition: tinysplinecxx.h:20
Definition: tinysplinecxx.h:15
Definition: tinysplinecxx.h:51
Definition: tinyspline.h:372
#define TS_CONTROL_POINT_EPSILON
Definition: tinyspline.h:52
Definition: tinysplinecxx.h:72
tsBSplineType
Definition: tinyspline.h:271