-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmath_stuff.cpp
More file actions
319 lines (258 loc) · 6.8 KB
/
math_stuff.cpp
File metadata and controls
319 lines (258 loc) · 6.8 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
PROJECT: mod_sa
LICENSE: See LICENSE in the top level directory
COPYRIGHT: Copyright we_sux, BlastHack
mod_sa is available from https://github.com/BlastHackNet/mod_s0beit_sa/
mod_sa is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mod_sa is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mod_sa. If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
#define FLOAT_EPSILON 0.0001f
/*
a lot of these are only for float[] so it would be nice to consolidate
some more functions for actual VECTOR/D3DVECTOR types, or better yet,
to just finally move to the CVector and CMatrix classes.
*/
int near_zero ( float v )
{
if ( !isfinite(v) )
return 1;
return fabs( v ) < FLOAT_EPSILON;
}
void vect2_normalize ( const float in[2], float out[2] )
{
float m;
int i;
m = sqrtf( in[0] * in[0] + in[1] * in[1] );
for ( i = 0; i < 2; i++ )
out[i] = in[i] / m;
}
float vect2_length ( const float in[2] )
{
return sqrtf( in[0] * in[0] + in[1] * in[1] );
}
int vect2_near_zero ( const float in[2] )
{
//if ( !isfinite(in[0]) || !isfinite(in[1]) )
// return 1;
return near_zero( in[0] ) && near_zero( in[1] );
}
void vect3_zero ( float out[3] )
{
int i;
for ( i = 0; i < 3; i++ )
out[i] = 0.0f;
}
void vect2_copy ( const uint8_t in[2], uint8_t out[2] )
{
memcpy( out, in, sizeof(uint8_t) * 2 );
}
void vect3_normalize ( const float in[3], float out[3] )
{
float m;
int i;
m = sqrtf( in[0] * in[0] + in[1] * in[1] + in[2] * in[2] );
for ( i = 0; i < 3; i++ )
out[i] = in[i] / m;
}
float vect3_length ( const float in[3] )
{
return sqrtf( in[0] * in[0] + in[1] * in[1] + in[2] * in[2] );
}
void vect3_div ( const float in[3], float m, float out[3] )
{
int i;
for ( i = 0; i < 3; i++ )
out[i] = in[i] / m;
}
void vect3_mult ( const float in[3], float m, float out[3] )
{
int i;
for ( i = 0; i < 3; i++ )
out[i] = in[i] * m;
}
void vect3_vect3_mult ( const float in1[3], const float in2[3], float out[3] )
{
int i;
for ( i = 0; i < 3; i++ )
out[i] = in1[i] * in2[i];
}
void vect3_vect3_add ( const float in1[3], const float in2[3], float out[3] )
{
int i;
for ( i = 0; i < 3; i++ )
out[i] = in1[i] + in2[i];
}
void vect3_vect3_sub ( const float in1[3], const float in2[3], float out[3] )
{
int i;
for ( i = 0; i < 3; i++ )
out[i] = in1[i] - in2[i];
}
void vect3_invert ( const float in[3], float out[3] )
{
int i;
for ( i = 0; i < 3; i++ )
out[i] = -in[i];
}
int vect3_near_zero ( const float in[3] )
{
//if ( !isfinite(in[0]) || !isfinite(in[1]) || !isfinite(in[2]) )
// return 1;
return near_zero( in[0] ) && near_zero( in[1] ) && near_zero( in[2] );
}
void vect3_copy ( const float in[3], float out[3] )
{
memcpy( out, in, sizeof(float) * 3 );
}
float vect3_dist ( const float in1[3], const float in2[3] )
{
float dist[3];
vect3_vect3_sub( in1, in2, dist );
return vect3_length( dist );
}
int vect4_near_zero ( const float in[4] )
{
//if ( !isfinite(in[0]) || !isfinite(in[1]) || !isfinite(in[2]) || !isfinite(in[3]) )
// return 1;
return near_zero( in[0] ) && near_zero( in[1] ) && near_zero( in[2] ) && near_zero( in[3] );
}
void vect4_copy ( const uint8_t in[4], uint8_t out[4] )
{
memcpy( out, in, sizeof(uint8_t) * 4 );
}
void vect4_copy ( const float in[4], float out[4] )
{
memcpy( out, in, sizeof(float) * 4 );
}
void matrix_copy ( const float in[16], float out[16] )
{
memcpy( out, in, sizeof(float) * 16 );
}
float vect3_dot_product ( const float in1[3], const float in2[3] )
{
return in1[0] * in2[0] + in1[1] * in2[1] + in1[2] * in2[2];
}
void vect3_cross_product ( const float in1[3], const float in2[3], float out[3] )
{
out[0] = ( in1[1] * in2[2] ) - ( in2[1] * in1[2] );
out[1] = ( in1[2] * in2[0] ) - ( in2[2] * in1[0] );
out[2] = ( in1[0] * in2[1] ) - ( in2[0] * in1[1] );
}
void matrix_vect3_mult ( const float matrix[16], const float in[3], float out[3] )
{
const float in4[4] = { in[0], in[1], in[2], 1.0f };
float out4[4];
matrix_vect4_mult( matrix, in4, out4 );
vect3_copy( out4, out );
}
void matrix_vect4_mult ( const float matrix[16], const float in[4], float out[4] )
{
float res[4];
int i;
for ( i = 0; i < 4; i++ )
{
res[i] = in[0] * matrix[0 + i] + in[1] * matrix[4 + i] + in[2] * matrix[8 + i] + in[3] * matrix[12 + i];
}
vect4_copy( res, out );
}
void matrix_matrix_mult ( const float in1[16], const float in2[16], float out[16] )
{
float res[16];
int i, j;
for ( i = 0; i < 4; i++ )
{
for ( j = 0; j < 4; j++ )
{
res[i * 4 + j] = in1[i * 4 + 0] *
in2[0 + j] +
in1[i * 4 + 1] *
in2[4 + j] +
in1[i * 4 + 2] *
in2[8 + j] +
in1[i * 4 + 3] *
in2[12 + j];
}
}
matrix_copy( res, out );
}
void matrix_vect3_rotate ( const float in[16], const float vect[3], float t, float out[16] )
{
const float x = vect[0], y = vect[1], z = vect[2];
const float sin_t = sinf( t ), cos_t = cosf( t );
float res[16];
const float matrix[16] =
{
cos_t + ( 1.0f - cos_t ) * x * x,
( 1.0f - cos_t ) * x * y - sin_t * z,
( 1.0f - cos_t ) * x * z + sin_t * y,
0.0f,
( 1.0f - cos_t ) * y * x + sin_t * z,
cos_t + ( 1.0f - cos_t ) * y * y,
( 1.0f - cos_t ) * y * z - sin_t * x,
0.0f,
( 1.0f - cos_t ) * z * x - sin_t * y,
( 1.0f - cos_t ) * z * y + sin_t * x,
cos_t + ( 1.0f - cos_t ) * z * z,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f
};
matrix_matrix_mult( in, matrix, res );
vect4_copy( &in[4 * 3], &res[4 * 3] );
matrix_copy( res, out );
}
void matrix_vect3_switchXY ( const float in[16], float out[16] )
{
float res[16];
vect4_copy( &in[4 * 0], &res[4 * 0] );
vect4_copy( &in[4 * 1], &res[4 * 1] );
vect4_copy( &in[4 * 2], &res[4 * 2] );
vect4_copy( &in[4 * 3], &res[4 * 3] );
matrix_copy( res, out );
}
void matrix_identity ( float out[16] )
{
static const float id[16] =
{
1.0f,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f
};
matrix_copy( id, out );
}
float roundf ( float v )
{
return floorf( v + 0.5f );
}
uint32_t encode_panels(unsigned char flp, unsigned char frp, unsigned char rlp, unsigned char rrp, unsigned char windshield, unsigned char front_bumper, unsigned char rear_bumper)
{
return flp | (frp << 4) | (rlp << 8) | (rrp << 12) | (windshield << 16) | (front_bumper << 20) | (rear_bumper << 24);
}
uint32_t encode_doors(unsigned char bonnet, unsigned char boot, unsigned char driver_door, unsigned char passenger_door)
{
return bonnet | (boot << 8) | (driver_door << 16) | (passenger_door << 24);
}