forked from Rhoban/Metabot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcubic.cpp
More file actions
107 lines (86 loc) · 1.78 KB
/
cubic.cpp
File metadata and controls
107 lines (86 loc) · 1.78 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdlib.h>
#include <stdio.h>
#include "cubic.h"
Cubic::Cubic() : nbPoints(0)
{
size = CUBIC_DEFAULT_POINTS;
points = (Point *)malloc(size*sizeof(Point));
polynoms = (Polynom *)malloc(size*sizeof(Polynom));
}
Cubic::~Cubic()
{
free(points);
free(polynoms);
}
void Cubic::checkSize()
{
if (nbPoints+1 >= size) {
size *= 2;
points = (Point *)realloc(points, size*sizeof(Point));
polynoms = (Polynom *)realloc(polynoms, size*sizeof(Polynom));
}
}
void Cubic::clear()
{
nbPoints = 0;
}
float Cubic::getXMax()
{
if (nbPoints == 0) {
return 0.0;
}
return points[nbPoints-1].x;
}
void Cubic::addPoint(float x, float y, float yp)
{
checkSize();
Point point;
point.x = x;
point.y = y;
point.yp = yp;
points[nbPoints] = point;
if (nbPoints > 0) {
Point previous = points[nbPoints-1];
Polynom p;
p.a = 2*previous.y + previous.yp - 2*y + yp;
p.b = -3*previous.y - 2*previous.yp + 3*y - yp;
p.c = previous.yp;
p.d = previous.y;
polynoms[nbPoints-1] = p;
}
nbPoints++;
}
float Cubic::get(float x)
{
int i;
for (i=0; i<nbPoints; i++) {
if (points[i].x >= x) {
break;
}
}
if (i == 0 || i == nbPoints) {
return 0.0;
}
Point A = points[i-1];
Point B = points[i];
Polynom f = polynoms[i-1];
x = (x-A.x)/(B.x-A.x);
float tx = x;
float result = f.d;
result += f.c * tx;
tx *= x;
result += f.b * tx;
tx *= x;
result += f.a * tx;
return result;
}
float Cubic::getMod(float x)
{
float maxX = getXMax();
if (x < 0.0 || x > maxX) {
x -= maxX*(int)(x/maxX);
}
return get(x);
}
#undef POINT_X
#undef POINT_Y