cgmath initial commit
[gph-cmath] / src / cgmath.inl
1 static inline void cgm_vadd(cgm_vec3 *a, const cgm_vec3 *b)
2 {
3         a->x += b->x;
4         a->y += b->y;
5         a->z += b->z;
6 }
7
8 static inline void cgm_vsub(cgm_vec3 *a, const cgm_vec3 *b)
9 {
10         a->x -= b->x;
11         a->y -= b->y;
12         a->z -= b->z;
13 }
14
15 static inline void cgm_vmul(cgm_vec3 *a, const cgm_vec3 *b)
16 {
17         a->x *= b->x;
18         a->y *= b->y;
19         a->z *= b->z;
20 }
21
22 static inline void cgm_vscale(cgm_vec3 *v, float s)
23 {
24         v->x *= s;
25         v->y *= s;
26         v->z *= s;
27 }
28
29 static inline float cgm_vdot(const cgm_vec3 *a, const cgm_vec3 *b)
30 {
31         return a->x * b->x + a->y * b->y + a->z * b->z;
32 }
33
34 static inline void cgm_vcross(cgm_vec3 *res, const cgm_vec3 *a, const cgm_vec3 *b)
35 {
36         res->x = a->y * b->z - a->z * b->y;
37         res->y = a->z * b->x - a->x * b->z;
38         res->z = a->x * b->y - a->y * b->x;
39 }
40
41 static inline float cgm_vlength(const cgm_vec3 *v)
42 {
43         return sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
44 }
45
46 static inline float cgm_vlength_sq(const cgm_vec3 *v)
47 {
48         return v->x * v->x + v->y * v->y + v->z * v->z;
49 }
50
51 static inline float cgm_vdist(const cgm_vec3 *a, const cgm_vec3 *b)
52 {
53         float dx = a->x - b->x;
54         float dy = a->y - b->y;
55         float dz = a->z - b->z;
56         return sqrt(dx * dx + dy * dy + dz * dz);
57 }
58
59 static inline float cgm_vdist_sq(const cgm_vec3 *a, const cgm_vec3 *b)
60 {
61         float dx = a->x - b->x;
62         float dy = a->y - b->y;
63         float dz = a->z - b->z;
64         return dx * dx + dy * dy + dz * dz;
65 }
66
67 static inline void cgm_vnormalize(cgm_vec3 *v)
68 {
69         float len = cgm_vlength(v);
70         if(len != 0.0f) {
71                 float s = 1.0f / len;
72                 v->x *= s;
73                 v->y *= s;
74                 v->z *= s;
75         }
76 }