-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.cpp
More file actions
66 lines (54 loc) · 1.94 KB
/
Copy pathmath.cpp
File metadata and controls
66 lines (54 loc) · 1.94 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
#include "core/math.hpp"
#include <cmath>
namespace droidcli::core::math {
float smooth_step01(float alpha)
{
const float clamped = clamp(alpha, 0.0f, 1.0f);
return clamped * clamped * (3.0f - 2.0f * clamped);
}
Vec3 lerp(const Vec3& a, const Vec3& b, float alpha)
{
return {
lerp(a.x, b.x, alpha),
lerp(a.y, b.y, alpha),
lerp(a.z, b.z, alpha)};
}
Vec3 rotate_around_axis(const Vec3& vector, const Vec3& axis, float angle_rad)
{
const Vec3 normalized_axis = axis.normalized();
const float cos_angle = std::cos(angle_rad);
const float sin_angle = std::sin(angle_rad);
const float one_minus_cos = 1.0f - cos_angle;
const float x = normalized_axis.x;
const float y = normalized_axis.y;
const float z = normalized_axis.z;
const Vec3 rotated = {
(cos_angle + x * x * one_minus_cos) * vector.x
+ (x * y * one_minus_cos - z * sin_angle) * vector.y
+ (x * z * one_minus_cos + y * sin_angle) * vector.z,
(y * x * one_minus_cos + z * sin_angle) * vector.x
+ (cos_angle + y * y * one_minus_cos) * vector.y
+ (y * z * one_minus_cos - x * sin_angle) * vector.z,
(z * x * one_minus_cos - y * sin_angle) * vector.x
+ (z * y * one_minus_cos + x * sin_angle) * vector.y
+ (cos_angle + z * z * one_minus_cos) * vector.z};
return rotated;
}
float evaluate_curve01(float normalized_time, const float* samples, int sample_count)
{
if (!samples || sample_count <= 0)
{
return clamp(normalized_time, 0.0f, 1.0f);
}
if (sample_count == 1)
{
return clamp(samples[0], 0.0f, 1.0f);
}
const float clamped_time = clamp(normalized_time, 0.0f, 1.0f);
const float scaled = clamped_time * static_cast<float>(sample_count - 1);
const int lower_index = static_cast<int>(std::floor(scaled));
const int upper_index = std::min(lower_index + 1, sample_count - 1);
const float fraction = scaled - static_cast<float>(lower_index);
return clamp(lerp(samples[lower_index], samples[upper_index], fraction), 0.0f, 1.0f);
}
} // namespace droidcli::core::math