-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurve.c
More file actions
72 lines (56 loc) · 1.59 KB
/
Copy pathcurve.c
File metadata and controls
72 lines (56 loc) · 1.59 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
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "curve.h"
static int clamp(int v, int lo, int hi) { return v < lo ? lo : v > hi ? hi : v; }
int curve_get_value (struct curve *c, float in)
{
int i;
float slope;
// min speed
if (in <= c->inputs[0])
return clamp(c->outputs[0], 0, 255);
// on the curve
for (i = 0; i < c->length - 1; ++i)
{
// on a curve point
if (in == c->inputs[i])
return clamp(c->outputs[i], 0, 255);
// inbetween curve points
if (in > c->inputs[i] && in < c->inputs[i+1])
{
slope = (float)(c->outputs[i+1] - c->outputs[i]) / (c->inputs[i+1] - c->inputs[i]);
return clamp((int)(slope * (in - c->inputs[i]) + c->outputs[i]), 0, 255);
}
}
// max speed
return clamp(c->outputs[c->length - 1], 0, 255);
}
struct curve *curve_create (int *inputs, int *outputs, int length)
{
int i;
struct curve *c;
if (length < 1)
return NULL;
for (i = 0; i < length - 1; ++i) {
if (inputs[i] >= inputs[i+1]) {
DBG("curve: temperatures must be strictly increasing\n");
return NULL;
}
}
c = malloc(sizeof(struct curve));
if (!c)
return NULL;
c->length = length;
c->inputs = malloc(length * sizeof(int));
c->outputs = malloc(length * sizeof(int));
memcpy(c->inputs, inputs, length * sizeof(int));
memcpy(c->outputs, outputs, length * sizeof(int));
return c;
}
void curve_destroy (struct curve *c)
{
free (c->inputs);
free (c->outputs);
free (c);
}