TinySpline
Spline Library for a Multitude of Programming Languages
v0.3.0
tinysplinecpp.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 nCtrlp, size_t dim = 2, size_t deg = 3,
80  tinyspline::BSpline::type type = TS_CLAMPED);
81  ~BSpline();
82 
83  /* Operators */
84  BSpline & operator=(const BSpline &other);
85  DeBoorNet operator()(real u) const;
86 
87  /* Accessors */
88  size_t degree() const;
89  size_t order() const;
90  size_t dimension() const;
91  std::vector<real> controlPoints() const;
92  std_real_vector_out controlPointAt(size_t index) const;
93  std::vector<real> knots() const;
94  real knotAt(size_t index) const;
95  tsBSpline * data();
96 
97  /* Query */
98  size_t numControlPoints() const;
99  DeBoorNet eval(real u) const;
100  std_real_vector_out evalAll(const std_real_vector_in us) const;
101  std_real_vector_out sample(size_t num = 0) const;
102  DeBoorNet bisect(real value, real epsilon = TS_CONTROL_POINT_EPSILON,
103  bool persnickety = false, size_t index = 0,
104  bool ascending = true, size_t maxIter = 30) const;
105  Domain domain() const;
106  bool isClosed(real epsilon = TS_CONTROL_POINT_EPSILON) const;
107 
108  /* Serialization */
109  std::string toJSON();
110  void fromJSON(std::string json);
111  void save(std::string path);
112  void load(std::string path);
113 
114  /* Modifications */
115  void setControlPoints(const std::vector<real> &ctrlp);
116  void setControlPointAt(size_t index, const std_real_vector_in ctrlp);
117  void setKnots(const std::vector<real> &knots);
118  void setKnotAt(size_t index, real knot);
119 
120  /* Transformations */
121  BSpline insertKnot(real u, size_t n) const;
122  BSpline split(real u) const;
123  BSpline tension(real tension) const;
124  BSpline toBeziers() const;
125  BSpline derive(size_t n = 1,
126  real epsilon = TS_CONTROL_POINT_EPSILON) const;
127 
128  /* Debug */
129  std::string toString() const;
130 
131 private:
132  tsBSpline spline;
133 };
134 
135 class Utils {
136 public:
137  static BSpline interpolateCubic(
138  const std::vector<real> *points, size_t dim);
139  static bool knotsEqual(real x, real y);
140 
141 private:
142  Utils() {}
143 };
144 
145 }
Definition: tinysplinecpp.h:135
Definition: tinyspline.h:302
Definition: tinysplinecpp.h:20
Definition: tinysplinecpp.h:15
Definition: tinysplinecpp.h:51
Definition: tinyspline.h:365
#define TS_CONTROL_POINT_EPSILON
Definition: tinyspline.h:47
Definition: tinysplinecpp.h:72
tsBSplineType
Definition: tinyspline.h:264