forked from mogonen/shady
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curve.h
76 lines (49 loc) · 1.54 KB
/
curve.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef CURVE_H
#define CURVE_H
#include <vector>
#include "Base.h"
typedef std::vector<Point_p> Point_pVec;
class Curve: public Selectable {
int _size;
Point_p _pts;
bool _isFlipped;
bool _isClosed;
inline int I(int i) const { return _isClosed ? ((i+_size)%_size) : CLAMP(i, 0, _size);}
protected:
void onUpdate();
public:
Curve(int sz = 100);
~Curve();
virtual Point evalP(double) const = 0;
virtual Vec2 evalT(double) const;
virtual Vec2 evalN(double) const;
Point P(int i) const { return _pts[I(i)];}
Vec2 T(int i) const { return (_pts[I(i+1)] - _pts[I(i-1)] ).normalize();}
Vec2 N(int i) const;
double computeDistance(const Point&, double &);
void flip(){_isFlipped = !_isFlipped;}
bool isClosed() const {return _isClosed;}
void resize(int);
virtual void resample();
void render() const;
};
class Spline:public Curve {
Point_pVec _cvs;
public:
Spline(int sz):Curve(sz){}
Point CV(int i) const{ return *_cvs[i];}
Point_p pCV(int i)const { return _cvs[i];}
void insert(Point_p p){ _cvs.push_back(p);}
void set(Point_p pP, int i){_cvs[i] = pP;}
};
class Bezier:public Spline{
public:
Bezier(int sz):Spline(sz){}
Point evalP(double) const;
void calculateDivideCV(double t, Point[]);
};
static const int FTABLE[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600};
inline double cubicBernstein(int i, double t){
return 6.0 / (FTABLE[3-i] * FTABLE[i]) * pow(1-t, 3-i) * pow(t,i);
}
#endif // CURVE_H