curvedraw

view src/curve.cc @ 0:8e524989c904

getting there
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 15 Dec 2015 07:15:53 +0200
parents
children ce7aa9a0594c
line source
1 #include <float.h>
2 #include "curve.h"
4 Curve::Curve(CurveType type)
5 {
6 this->type = type;
7 }
9 void Curve::set_type(CurveType type)
10 {
11 this->type = type;
12 }
14 CurveType Curve::get_type() const
15 {
16 return type;
17 }
19 void Curve::add_point(const Vector2 &p, float weight)
20 {
21 cp.push_back(Vector3(p.x, p.y, weight));
22 }
24 bool Curve::remove_point(int idx)
25 {
26 if(idx < 0 || idx >= (int)cp.size()) {
27 return false;
28 }
29 cp.erase(cp.begin() + idx);
30 return true;
31 }
33 int Curve::nearest_point(const Vector2 &p)
34 {
35 int res = -1;
36 float bestsq = FLT_MAX;
38 for(size_t i=0; i<cp.size(); i++) {
39 float d = (get_point(i) - p).length_sq();
40 if(d < bestsq) {
41 bestsq = d;
42 res = i;
43 }
44 }
46 return res;
47 }
49 int Curve::get_point_count() const
50 {
51 return (int)cp.size();
52 }
54 const Vector3 &Curve::get_homo_point(int idx) const
55 {
56 return cp[idx];
57 }
59 Vector2 Curve::get_point(int idx) const
60 {
61 return Vector2(cp[idx].x, cp[idx].y);
62 }
64 float Curve::get_weight(int idx) const
65 {
66 return cp[idx].z;
67 }
69 bool Curve::set_point(int idx, const Vector2 &p, float weight)
70 {
71 if(idx < 0 || idx >= (int)cp.size()) {
72 return false;
73 }
74 cp[idx] = Vector3(p.x, p.y, weight);
75 return true;
76 }
78 bool Curve::set_weight(int idx, float weight)
79 {
80 if(idx < 0 || idx >= (int)cp.size()) {
81 return false;
82 }
83 cp[idx].z = weight;
84 return true;
85 }
87 Vector2 Curve::interpolate(float t, CurveType type) const
88 {
89 int num_cp = (int)cp.size();
90 if(!num_cp) {
91 return Vector2(0, 0);
92 }
93 if(num_cp == 1) {
94 return Vector2(cp[0].x, cp[0].y);
95 }
97 Vector3 res;
98 int idx0 = (int)floor(t * (num_cp - 1));
99 int idx1 = idx0 + 1;
101 float dt = 1.0 / (float)(num_cp - 1);
102 float t0 = (float)idx0 * dt;
103 float t1 = (float)idx1 * dt;
105 t = (t - t0) / (t1 - t0);
106 if(t < 0.0) t = 0.0;
107 if(t > 1.0) t = 1.0;
109 if(type == CURVE_LINEAR || num_cp <= 2) {
110 res = lerp(cp[idx0], cp[idx1], t);
111 } else {
112 int idx_prev = idx0 <= 0 ? idx0 : idx0 - 1;
113 int idx_next = idx1 >= num_cp - 1 ? idx1 : idx1 + 1;
115 if(type == CURVE_HERMITE) {
116 res = catmull_rom_spline(cp[idx_prev], cp[idx0], cp[idx1], cp[idx_next], t);
117 } else {
118 res = bspline(cp[idx_prev], cp[idx0], cp[idx1], cp[idx_next], t);
119 if(res.z != 0.0f) {
120 res.x /= res.z;
121 res.y /= res.z;
122 }
123 }
124 }
126 return Vector2(res.x, res.y);
127 }
129 Vector2 Curve::interpolate(float t) const
130 {
131 return interpolate(t, type);
132 }
134 Vector2 Curve::operator ()(float t) const
135 {
136 return interpolate(t);
137 }
139 float Curve::map_param(float x) const
140 {
141 if(x < 0.0f) x = 0.0f;
142 if(x >= 1.0f) x = 1.0f;
143 return x * ((float)cp.size() - 1);
144 }