-
Notifications
You must be signed in to change notification settings - Fork 74
/
math-toolkit.h
86 lines (73 loc) · 1.74 KB
/
math-toolkit.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef __RAY_MATH_TOOLKIT_H
#define __RAY_MATH_TOOLKIT_H
#include <math.h>
#include <stdio.h>
#include <assert.h>
static inline
void normalize(double *v)
{
double d = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
assert(d != 0.0 && "Error calculating normal");
v[0] /= d;
v[1] /= d;
v[2] /= d;
}
static inline
double length(const double *v)
{
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}
static inline
void add_vector(const double *a, const double *b, double *out)
{
for (int i = 0; i < 3; i++)
out[i] = a[i] + b[i];
}
static inline
void subtract_vector(const double *a, const double *b, double *out)
{
for (int i = 0; i < 3; i++)
out[i] = a[i] - b[i];
}
static inline
void multiply_vectors(const double *a, const double *b, double *out)
{
for (int i = 0; i < 3; i++)
out[i] = a[i] * b[i];
}
static inline
void multiply_vector(const double *a, double b, double *out)
{
for (int i = 0; i < 3; i++)
out[i] = a[i] * b;
}
static inline
void cross_product(const double *v1, const double *v2, double *out)
{
out[0] = v1[1] * v2[2] - v1[2] * v2[1];
out[1] = v1[2] * v2[0] - v1[0] * v2[2];
out[2] = v1[0] * v2[1] - v1[1] * v2[0];
}
static inline
double dot_product(const double *v1, const double *v2)
{
double dp = 0.0;
for (int i = 0; i < 3; i++)
dp += v1[i] * v2[i];
return dp;
}
static inline
void scalar_triple_product(const double *u, const double *v, const double *w,
double *out)
{
cross_product(v, w, out);
multiply_vectors(u, out, out);
}
static inline
double scalar_triple(const double *u, const double *v, const double *w)
{
double tmp[3];
cross_product(w, u, tmp);
return dot_product(v, tmp);
}
#endif