-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathext.c
executable file
·136 lines (119 loc) · 3.23 KB
/
mathext.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* mathext.c
*
* Created on: 12/apr/2012
* Author: enrico zamagni
*/
#include "mathext.h"
// prodotto scalare
float dprod(Vect3 a, Vect3 b) {
return a.x * b.x + a.y * b.y;
}
// prodotto vettoriale
Vect3 cprod(Vect3 a, Vect3 b) {
Vect3 result;
result.x = a.y * b.z - a.z * b.y;
result.y = a.z * b.x - a.x * b.z;
result.z = a.x * b.y - a.y * b.x;
return result;
}
// inverso
Vect3 inv(Vect3 v) {
Vect3 result;
result.x = -v.x;
result.y = -v.y;
result.z = -v.z;
return result;
}
// modulo
float mod(Vect3 v) {
return sqrt(sqr(v.x) + sqr(v.y) + sqr(v.z));
}
// normalizzazione
Vect3 normalize(Vect3 v) {
Vect3 result;
float vmod = mod(v);
if(vmod == 0) {
// salviamoci da una divisione per zero:
// se qualche furbone tenta di normalizzare un vettore "nullo"
// restituiamo per convenzione il versore y
result.x = result.z = 0;
result.y = 1;
return result;
}
result.x = v.x / vmod;
result.y = v.y / vmod;
result.z = v.z / vmod;
return result;
}
// differenza (a - b)
Vect3 subtr(Vect3 a, Vect3 b) {
Vect3 result;
result.x = a.x - b.x;
result.y = a.y - b.y;
result.z = a.z - b.z;
return result;
}
// addizione
Vect3 sum(Vect3 a, Vect3 b) {
Vect3 result;
result.x = a.x + b.x;
result.y = a.y + b.y;
result.z = a.z + b.z;
return result;
}
// moltiplicazione per scalare
Vect3 scale(Vect3 v, float s) {
Vect3 result;
result.x = v.x * s;
result.y = v.y * s;
result.z = v.z * s;
return result;
}
// vettore riflesso alla normale
Vect3 reflect(Vect3 ray, Vect3 normal) {
Vect3 result;
// Res = 2(R*N)N - R
result = scale(normal, 2 * dprod(ray, normal));
result = subtr(result, ray);
return result;
}
// rotazione antioraria rispetto ad asse
Vect3 rotate(Vect3 v, Vect3 axis, float tht) {
Vect3 result;
float rmatrix[3][3];
rmatrix[0][0] = sqr(axis.x) + (1 - sqr(axis.x)) * cos(tht);
rmatrix[0][1] = (1 - cos(tht)) * axis.x * axis.y - axis.z * sin(tht);
rmatrix[0][2] = (1 - cos(tht)) * axis.x * axis.z + sin(tht) * axis.y;
rmatrix[1][0] = (1 - cos(tht)) * axis.y * axis.x + sin(tht) * axis.z;
rmatrix[1][1] = sqr(axis.y) + (1 - sqr(axis.y)) * cos(tht);
rmatrix[1][2] = (1 - cos(tht)) * axis.y * axis.z - sin(tht) * axis.x;
rmatrix[2][0] = (1 - cos(tht)) * axis.z * axis.x - sin(tht) * axis.y;
rmatrix[2][1] = (1 - cos(tht)) * axis.z * axis.y + sin(tht) * axis.x;
rmatrix[2][2] = sqr(axis.z) + (1 - sqr(axis.z)) * cos(tht);
result.x = v.x * rmatrix[0][0] + v.y * rmatrix[0][1] + v.z * rmatrix[0][2];
result.y = v.x * rmatrix[1][0] + v.y * rmatrix[1][1] + v.z * rmatrix[1][2];
result.z = v.x * rmatrix[2][0] + v.y * rmatrix[2][1] + v.z * rmatrix[2][2];
return result;
}
// rotazione antioraria attorno ad asse X
Vect3 rotateX(Vect3 v, float tht) {
Vect3 result = v;
result.y = v.y * cos(tht) - v.z * sin(tht);
result.z = v.y * sin(tht) + v.z * cos(tht);
return result;
}
// rotazione antioraria attorno ad asse Y
Vect3 rotateY(Vect3 v, float tht) {
Vect3 result = v;
result.z = v.z * cos(tht) - v.x * sin(tht);
result.x = v.z * sin(tht) + v.x * cos(tht);
return result;
}
// rotazione antioraria attorno ad asse Z
Vect3 rotateZ(Vect3 v, float tht) {
Vect3 result = v;
result.x = v.x * cos(tht) - v.y * sin(tht);
result.y = v.x * sin(tht) + v.y * cos(tht);
return result;
}