-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
258 lines (200 loc) · 7.27 KB
/
Vector.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//
// Created by Marrony Neris on 12/1/15.
//
#ifndef VECTOR_H
#define VECTOR_H
#include <math.h>
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector2Add(const float in0[2], const float in1[2], float out[2]) {
out[0] = in0[0] + in1[0];
out[1] = in0[1] + in1[1];
}
static inline void mnVector3Add(const float in0[3], const float in1[3], float out[3]) {
out[0] = in0[0] + in1[0];
out[1] = in0[1] + in1[1];
out[2] = in0[2] + in1[2];
}
static inline void mnVector4Add(const float in0[4], const float in1[4], float out[4]) {
out[0] = in0[0] + in1[0];
out[1] = in0[1] + in1[1];
out[2] = in0[2] + in1[2];
out[3] = in0[3] + in1[3];
}
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector2Sub(const float in0[2], const float in1[2], float out[2]) {
out[0] = in0[0] - in1[0];
out[1] = in0[1] - in1[1];
}
static inline void mnVector3Sub(const float in0[3], const float in1[3], float out[3]) {
out[0] = in0[0] - in1[0];
out[1] = in0[1] - in1[1];
out[2] = in0[2] - in1[2];
}
static inline void mnVector4Sub(const float in0[4], const float in1[4], float out[4]) {
out[0] = in0[0] - in1[0];
out[1] = in0[1] - in1[1];
out[2] = in0[2] - in1[2];
out[3] = in0[3] - in1[3];
}
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector2Mul(const float in0[2], const float in1[2], float out[2]) {
out[0] = in0[0] * in1[0];
out[1] = in0[1] * in1[1];
}
static inline void mnVector3Mul(const float in0[3], const float in1[3], float out[3]) {
out[0] = in0[0] * in1[0];
out[1] = in0[1] * in1[1];
out[2] = in0[2] * in1[2];
}
static inline void mnVector4Mul(const float in0[4], const float in1[4], float out[4]) {
out[0] = in0[0] * in1[0];
out[1] = in0[1] * in1[1];
out[2] = in0[2] * in1[2];
out[3] = in0[3] * in1[3];
}
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector2MulScalar(const float in0[2], const float in1, float out[2]) {
out[0] = in0[0] * in1;
out[1] = in0[1] * in1;
}
static inline void mnVector3MulScalar(const float in0[3], const float in1, float out[3]) {
out[0] = in0[0] * in1;
out[1] = in0[1] * in1;
out[2] = in0[2] * in1;
}
static inline void mnVector4MulScalar(const float in0[4], const float in1, float out[4]) {
out[0] = in0[0] * in1;
out[1] = in0[1] * in1;
out[2] = in0[2] * in1;
out[3] = in0[3] * in1;
}
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector2AddScalar(const float in0[2], const float in1, float out[2]) {
out[0] = in0[0] + in1;
out[1] = in0[1] + in1;
}
static inline void mnVector3AddScalar(const float in0[3], const float in1, float out[3]) {
out[0] = in0[0] + in1;
out[1] = in0[1] + in1;
out[2] = in0[2] + in1;
}
static inline void mnVector4AddScalar(const float in0[4], const float in1, float out[4]) {
out[0] = in0[0] + in1;
out[1] = in0[1] + in1;
out[2] = in0[2] + in1;
out[3] = in0[3] + in1;
}
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector2SubScalar(const float in0[2], const float in1, float out[2]) {
out[0] = in0[0] - in1;
out[1] = in0[1] - in1;
}
static inline void mnVector3SubScalar(const float in0[3], const float in1, float out[3]) {
out[0] = in0[0] - in1;
out[1] = in0[1] - in1;
out[2] = in0[2] - in1;
}
static inline void mnVector4SubScalar(const float in0[4], const float in1, float out[4]) {
out[0] = in0[0] - in1;
out[1] = in0[1] - in1;
out[2] = in0[2] - in1;
out[3] = in0[3] - in1;
}
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector2MulAddScalar(const float in0[2], const float in1, float out[2]) {
out[0] += in0[0] * in1;
out[1] += in0[1] * in1;
}
static inline void mnVector3MulAddScalar(const float in0[3], const float in1, float out[3]) {
out[0] += in0[0] * in1;
out[1] += in0[1] * in1;
out[2] += in0[2] * in1;
}
static inline void mnVector4MulAddScalar(const float in0[4], const float in1, float out[4]) {
out[0] += in0[0] * in1;
out[1] += in0[1] * in1;
out[2] += in0[2] * in1;
out[3] += in0[3] * in1;
}
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector2Div(const float in0[2], const float in1[2], float out[2]) {
out[0] = in0[0] / in1[0];
out[1] = in0[1] / in1[1];
}
static inline void mnVector3Div(const float in0[3], const float in1[3], float out[3]) {
out[0] = in0[0] / in1[0];
out[1] = in0[1] / in1[1];
out[2] = in0[2] / in1[2];
}
static inline void mnVector4Div(const float in0[4], const float in1[4], float out[4]) {
out[0] = in0[0] / in1[0];
out[1] = in0[1] / in1[1];
out[2] = in0[2] / in1[2];
out[3] = in0[3] / in1[3];
}
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector3Cross(const float in0[3], const float in1[3], float out[3]) {
float temp[3];
temp[0] = in0[1]*in1[2] - in0[2]*in1[1];
temp[1] = in0[2]*in1[0] - in0[0]*in1[2];
temp[2] = in0[0]*in1[1] - in0[1]*in1[0];
out[0] = temp[0];
out[1] = temp[1];
out[2] = temp[2];
}
//////////////////////////////////////////////////////////////////////////////
static inline float mnVector2Dot(const float in0[2], const float in1[2]) {
return in0[0]*in1[0] + in0[1]*in1[1];
}
static inline float mnVector3Dot(const float in0[3], const float in1[3]) {
return in0[0]*in1[0] + in0[1]*in1[1] + in0[2]*in1[2];
}
static inline float mnVector4Dot(const float in0[4], const float in1[4]) {
return in0[0]*in1[0] + in0[1]*in1[1] + in0[2]*in1[2] + in0[3]*in1[3];
}
//////////////////////////////////////////////////////////////////////////////
static inline float mnVector2Length(const float in[2]) {
return sqrtf(mnVector2Dot(in, in));
}
static inline float mnVector3Length(const float in[3]) {
return sqrtf(mnVector3Dot(in, in));
}
static inline float mnVector4Length(const float in[4]) {
return sqrtf(mnVector4Dot(in, in));
}
//////////////////////////////////////////////////////////////////////////////
static inline void mnVector2Normalize(const float in[2], float out[2]) {
const float length = mnVector2Length(in);
const float invLength[2] = {1.0f / length, 1.0f / length};
mnVector2Mul(in, invLength, out);
}
static inline void mnVector3Normalize(const float in[3], float out[3]) {
const float length = mnVector3Length(in);
const float invLength[3] = {1.0f / length, 1.0f / length, 1.0f / length};
mnVector3Mul(in, invLength, out);
}
static inline void mnVector4Normalize(const float in[4], float out[4]) {
const float length = mnVector4Length(in);
const float invLength[4] = {1.0f / length, 1.0f / length, 1.0f / length, 1.0f / length};
mnVector4Mul(in, invLength, out);
}
//////////////////////////////////////////////////////////////////////////////
union Vector2 {
struct {
float x, y;
};
float values[2];
};
union Vector3 {
struct {
float x, y, z;
};
float values[3];
};
union Vector4 {
struct {
float x, y, z, w;
};
float values[4];
};
#endif //VECTOR_H