-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathfunc.h
63 lines (53 loc) · 1.35 KB
/
mathfunc.h
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
#ifndef PIROVERD_MATHFUNC_H
#define PIROVERD_MATHFUNC_H
static inline float squared_length(float *v)
{
return (v[0]*v[0]) + (v[1]*v[1]) + (v[2]*v[2]);
}
static inline float length(float *v)
{
return sqrtf(squared_length(v));
}
static inline float dot_product(float *u, float *v)
{
return (u[0]*v[0]) + (u[1]*v[1]) + (u[2]*v[2]);
}
static inline void cross_product(float *u, float *v, float *r)
{
r[0] = u[1]*v[2] - u[2]*v[1];
r[1] = u[2]*v[0] - u[0]*v[2];
r[2] = u[0]*v[1] - u[1]*v[0];
}
static inline void projection(float *u, float *v, float *r)
{
float costheta = dot_product(u, v) / squared_length(v);
r[0] = v[0]*costheta;
r[1] = v[1]*costheta;
r[2] = v[2]*costheta;
}
static inline void subtract(float *u, float *v, float *r)
{
r[0] = u[0] - v[0];
r[1] = u[1] - v[1];
r[2] = u[2] - v[2];
}
static inline void normalise(float *v)
{
float scale = 1.0 / length(v);
v[0] *= scale;
v[1] *= scale;
v[2] *= scale;
}
static inline float unsigned_angle_between(float *u, float *v)
{
return acos(dot_product(u, v) / (length(u) * length(v)));
}
static inline float angle_between(float *u, float *v, float *n)
{
float angle = unsigned_angle_between(u, v);
float cross[3];
cross_product(u, v, cross);
if(dot_product(n, cross) > 0) angle *= -1;
return angle;
}
#endif // PIROVERD_MATHFUNC_H