Mon, 10 Apr 2023 14:46:36 +0300
Move bezier curve to its own file
373
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
1 | #include "bezier_curve.h" |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
2 | |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
3 | /** |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
4 | * @brief computes the point on a Bezier curve |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
5 | * @param curve |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
6 | * @param t scalar between 0 and 1, with t=0 being P0 and t=1 being P3 |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
7 | * @return point on curve |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
8 | */ |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
9 | glm::vec3 point_on_bezier_curve(const struct bezier_curve &curve, float t) |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
10 | { |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
11 | // clamp t as rounding errors might make it slightly out of bounds |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
12 | t = std::clamp(t, 0.0f, 1.0f); |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
13 | const float t_squared = t * t; |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
14 | const float t_cubed = t * t * t; |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
15 | const float coefficients[3] = { |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
16 | -1*t_cubed +3*t_squared -3*t +1, |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
17 | +3*t_cubed -6*t_squared +3*t, |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
18 | -3*t_cubed +3*t_squared, |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
19 | }; |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
20 | return coefficients[0] * curve.points[0] |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
21 | + coefficients[1] * curve.points[1] |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
22 | + coefficients[2] * curve.points[2] |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
23 | + t_cubed * curve.points[3]; |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
24 | } |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
25 | |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
26 | /** |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
27 | * @brief computes the derivative of a point on a Bezier curve |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
28 | * @param curve |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
29 | * @param t scalar between 0 and 1, with t=0 being P0 and t=1 being P3 |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
30 | * @return point on curve |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
31 | */ |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
32 | glm::vec3 derivate_on_bezier_curve(const struct bezier_curve &curve, float t) |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
33 | { |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
34 | // clamp t as rounding errors might make it slightly out of bounds |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
35 | t = std::clamp(t, 0.0f, 1.0f); |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
36 | const float t_cubed = t * t; |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
37 | const float coefficients[4] = { |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
38 | -3*t_cubed + 6*t -3, |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
39 | +9*t_cubed -12*t +3, |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
40 | -9*t_cubed + 6*t, |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
41 | +3*t_cubed |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
42 | }; |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
43 | return coefficients[0] * curve.points[0] |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
44 | + coefficients[1] * curve.points[1] |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
45 | + coefficients[2] * curve.points[2] |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
46 | + coefficients[3] * curve.points[3]; |
e34d6a30b96d
Move bezier curve to its own file
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
47 | } |