-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymath.c
377 lines (324 loc) · 11.5 KB
/
mymath.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <SL_DEF.H>
#include "def.h"
#include "mymath.h"
/*
Deflection
//d - 2 * DotProduct(d,n) * n
d = direction vector
n = normal vector
*/
#define MATH_TOLERANCE (16384)
static int realNormal[XYZ] = {0, 0, 0};
static int realpt[XYZ] = {0, 0, 0};
static FIXED pFNn[XYZ] = {0, 0, 0};
inline FIXED fxm(FIXED d1, FIXED d2) //Fixed Point Multiplication
{
register volatile FIXED rtval;
asm(
"dmuls.l %[d1],%[d2];"
"sts MACH,r1;" // Store system register [sts] , high of 64-bit register MAC to r1
"sts MACL,%[out];" // Low of 64-bit register MAC to the register of output param "out"
"xtrct r1,%[out];" //This whole procress gets the middle 32-bits of 32 * 32 -> (2x32 bit registers)
: [out] "=r" (rtval) //OUT
: [d1] "r" (d1), [d2] "r" (d2) //IN
: "r1", "mach", "macl" //CLOBBERS
);
return rtval;
}
inline FIXED fxdot(VECTOR ptA, VECTOR ptB) //Fixed-point dot product
{
register volatile FIXED rtval;
asm(
"clrmac;"
"mac.l @%[ptr1]+,@%[ptr2]+;"
"mac.l @%[ptr1]+,@%[ptr2]+;"
"mac.l @%[ptr1]+,@%[ptr2]+;"
"sts MACH,r1;"
"sts MACL,%[ox];"
"xtrct r1,%[ox];"
: [ox] "=r" (rtval), [ptr1] "+p" (ptA) , [ptr2] "+p" (ptB) //OUT
: //IN
: "r1", "mach", "macl" //CLOBBERS
);
return rtval;
}
inline FIXED fxdiv(FIXED dividend, FIXED divisor) //Fixed-point division
{
volatile int * DVSR = ( int*)0xFFFFFF00;
volatile int * DVDNTH = ( int*)0xFFFFFF10;
volatile int * DVDNTL = ( int*)0xFFFFFF14;
*DVSR = divisor;
*DVDNTH = (dividend>>16);
*DVDNTL = (dividend<<16);
return *DVDNTL;
// register volatile FIXED quotient;
// asm(
// "mov.l %[dvs], @%[dvsr];"
// "mov %[dvd], r1;" //Move the dividend to a general-purpose register, to prevent weird misreading of data.
// "shlr16 r1;"
// "exts.w r1, r1;" //Sign extension in case value is negative
// "mov.l r1, @%[nth];" //Expresses "*DVDNTH = dividend>>16"
// "mov %[dvd], r1;"
// "shll16 r1;"
// "mov.l r1, @%[ntl];" //Expresses *DVDNTL = dividend<<16";
// "mov.l @%[ntl], %[out];" //Get result.
// : [out] "=r" (quotient) //OUT
// : [dvs] "r" (divisor) , [dvd] "r" (dividend) , //IN
// [dvsr] "r" (DVSR) , [nth] "r" (DVDNTH) , [ntl] "r" (DVDNTL) //IN
// : "r1" //CLOBBERS
// );
// return quotient;
}
//////////////////////////////////
// Shorthand to turn two points (to represent a segment) into a vector
//////////////////////////////////
void segment_to_vector(FIXED * start, FIXED * end, FIXED * out)
{
out[X] = (start[X] - end[X]);
out[Y] = (start[Y] - end[Y]);
out[Z] = (start[Z] - end[Z]);
}
//////////////////////////////////
// Manhattan
//
// Cube root scalar.
// 1.25992104989ish / 3 = 0.2467ish * 65536 = 16168
//
//////////////////////////////////
/* FIXED approximate_distance(FIXED * p0, FIXED * p1)
{
// POINT difference;
// segment_to_vector(p0, p1, difference);
// int max = JO_MAX(JO_ABS(difference[X]), JO_MAX(JO_ABS(difference[Y]), JO_ABS(difference[Z])));
// if(max == JO_ABS(difference[X]))
// {
// return JO_ABS(p0[X] - p1[X]) + fxm(JO_ABS(p0[Y] - p1[Y]), 16168) + fxm(JO_ABS(p0[Z] - p1[Z]), 16168);
// } else if(max == JO_ABS(difference[Y]))
// {
// return JO_ABS(p0[Y] - p1[Y]) + fxm(JO_ABS(p0[X] - p1[X]), 16168) + fxm(JO_ABS(p0[Z] - p1[Z]), 16168);
// } else {
// return JO_ABS(p0[Z] - p1[Z]) + fxm(JO_ABS(p0[Y] - p1[Y]), 16168) + fxm(JO_ABS(p0[X] - p1[X]), 16168);
// }
return (JO_ABS(p0[X] - p1[X]) + JO_ABS(p0[Y] - p1[Y]) + JO_ABS(p0[Z] - p1[Z]));
}
*/
//////////////////////////////////
// "fast inverse square root", but fixed-point
//////////////////////////////////
FIXED fxisqrt(FIXED input){
FIXED xSR = 0;
FIXED pushrsamp = 0;
FIXED msb = 0;
FIXED shoffset = 0;
FIXED yIsqr = 0;
if(input <= 65536){
return 65536;
}
xSR = input>>1;
pushrsamp = input;
while(pushrsamp >= 65536){
pushrsamp >>=1;
msb++;
}
shoffset = (16 - ((msb)>>1));
yIsqr = 1<<shoffset;
//y = (y * (98304 - ( ( (x>>1) * ((y * y)>>16 ) )>>16 ) ) )>>16; x2
return (fxm(yIsqr, (98304 - fxm(xSR, fxm(yIsqr, yIsqr)))));
}
//////////////////////////////////
// "fast inverse square root x2", but fixed-point
//////////////////////////////////
FIXED double_fxisqrt(FIXED input){
FIXED xSR = 0;
FIXED pushrsamp = 0;
FIXED msb = 0;
FIXED shoffset = 0;
FIXED yIsqr = 0;
if(input <= 65536){
return 65536;
}
xSR = input>>1;
pushrsamp = input;
while(pushrsamp >= 65536){
pushrsamp >>=1;
msb++;
}
shoffset = (16 - ((msb)>>1));
yIsqr = 1<<shoffset;
//y = (y * (98304 - ( ( (x>>1) * ((y * y)>>16 ) )>>16 ) ) )>>16; x2
yIsqr = (fxm(yIsqr, (98304 - fxm(xSR, fxm(yIsqr, yIsqr)))));
return (fxm(yIsqr, (98304 - fxm(xSR, fxm(yIsqr, yIsqr)))));
}
void normalize(FIXED vector_in[XYZ], FIXED vector_out[XYZ])
{
//Shift inputs rsamp by 8, to prevent overflow.
static FIXED vmag = 0;
vmag = fxisqrt(fxm(vector_in[X],vector_in[X]) + fxm(vector_in[Y],vector_in[Y]) + fxm(vector_in[Z],vector_in[Z]));
vector_out[X] = fxm(vmag, vector_in[X]);
vector_out[Y] = fxm(vmag, vector_in[Y]);
vector_out[Z] = fxm(vmag, vector_in[Z]);
}
void double_normalize(FIXED vector_in[XYZ], FIXED vector_out[XYZ])
{
//Shift inputs rsamp by 8, to prevent overflow.
static FIXED vmag = 0;
vmag = double_fxisqrt(fxm(vector_in[X],vector_in[X]) + fxm(vector_in[Y],vector_in[Y]) + fxm(vector_in[Z],vector_in[Z]));
vector_out[X] = fxm(vmag, vector_in[X]);
vector_out[Y] = fxm(vmag, vector_in[Y]);
vector_out[Z] = fxm(vmag, vector_in[Z]);
}
//////////////////////////////////
// Checks if "point" is between "start" and "end".
//////////////////////////////////
Bool isPointonSegment(FIXED point[XYZ], FIXED start[XYZ], FIXED end[XYZ])
{
FIXED max[XYZ];
FIXED min[XYZ];
max[X] = JO_MAX(start[X], end[X]);
max[Y] = JO_MAX(start[Y], end[Y]);
max[Z] = JO_MAX(start[Z], end[Z]);
min[X] = JO_MIN(start[X], end[X]);
min[Y] = JO_MIN(start[Y], end[Y]);
min[Z] = JO_MIN(start[Z], end[Z]);
if(point[X] >= (min[X] - MATH_TOLERANCE) && point[X] <= (max[X] + MATH_TOLERANCE) &&
point[Y] >= (min[Y] - MATH_TOLERANCE) && point[Y] <= (max[Y] + MATH_TOLERANCE) &&
point[Z] >= (min[Z] - MATH_TOLERANCE) && point[Z] <= (max[Z] + MATH_TOLERANCE)){
return 1;
} else {
return 0;
}
}
//////////////////////////////////
// Given a segment represented by two points "p1" and "p2,
// project a third point "tgt" onto the the same segment.
// Method is: Unit vector "p1->p2", dot product that unit with tgt, multiply dot result with unit.
//////////////////////////////////
void project_to_segment(POINT tgt, POINT p1, POINT p2, POINT outPt, VECTOR outV)
{
VECTOR vectorized_pt;
//Following makes a vector from the left/right centers.
outV[X] = (p1[X] - p2[X]);
outV[Y] = (p1[Y] - p2[Y]);
outV[Z] = (p1[Z] - p2[Z]);
if(JO_ABS(outV[X]) >= (SQUARE_MAX) || JO_ABS(outV[Y]) >= (SQUARE_MAX) || JO_ABS(outV[Z]) >= (SQUARE_MAX))
{
//Normalization that covers values >SQUARE_MAX
int vmag = slSquart( ((outV[X]>>16) * (outV[X]>>16)) + ((outV[Y]>>16) * (outV[Y]>>16)) + ((outV[Z]>>16) * (outV[Z]>>16)) )<<16;
vmag = slDivFX(vmag, 65536);
//
vectorized_pt[X] = fxm(outV[X], vmag);
vectorized_pt[Y] = fxm(outV[Y], vmag);
vectorized_pt[Z] = fxm(outV[Z], vmag);
} else {
normalize(outV, vectorized_pt);
}
//
//Generatr a scalar for the source vector
int scaler = slInnerProduct(vectorized_pt, tgt);
//Scale the vector to project onto ; using scalar as a represenation of how far along the vector it is
outPt[X] = fxm(scaler, vectorized_pt[X]);
outPt[Y] = fxm(scaler, vectorized_pt[Y]);
outPt[Z] = fxm(scaler, vectorized_pt[Z]);
}
void cross_fixed(FIXED vector1[XYZ], FIXED vector2[XYZ], FIXED output[XYZ])
{
output[X] = fxm(vector1[Y], vector2[Z]) - fxm(vector1[Z], vector2[Y]);
output[Y] = fxm(vector1[Z], vector2[X]) - fxm(vector1[X], vector2[Z]);
output[Z] = fxm(vector1[X], vector2[Y]) - fxm(vector1[Y], vector2[X]);
}
//////////////////////////////////
//A helper function which checks the X and Z signs of a vector to find its domain.
//////////////////////////////////
Uint8 solve_domain(FIXED normal[XYZ]){
if(normal[X] >= 0 && normal[Z] >= 0){
//PP
return 1;
} else if(normal[X] >= 0 && normal[Z] < 0){
//PN
return 2;
} else if(normal[X] < 0 && normal[Z] >= 0){
//NP
return 3;
} else if(normal[X] < 0 && normal[Z] < 0){
//NN
return 4;
};
/*
3 - 1
4 - 2
*/
return 0;
}
FIXED pt_col_plane(FIXED planept[XYZ], FIXED ptoffset[XYZ], FIXED normal[XYZ], FIXED unitNormal[XYZ], FIXED offset[XYZ])
{
//Using a NORMAL OF A PLANE which is also a POINT ON THE PLANE and checking IF A POINT IS ON THAT PLANE
//the REAL POSITION of the normal, which is also a POINT ON THE PLANE, needs an actual position. WE FIND IT HERE.
realNormal[X] = normal[X] + (offset[X]);
realNormal[Y] = normal[Y] + (offset[Y]);
realNormal[Z] = normal[Z] + (offset[Z]);
realpt[X] = planept[X] + (ptoffset[X]);
realpt[Y] = planept[Y] + (ptoffset[Y]);
realpt[Z] = planept[Z] + (ptoffset[Z]);
//the DIFFERENCE between a POSSIBLE POINT ON THE PLANE, and a KNOWN POINT ON THE PLANE, must use the REAL POSITION of the NORMAL POINT.
pFNn[X] = realNormal[X] - realpt[X];
pFNn[Y] = realNormal[Y] - realpt[Y];
pFNn[Z] = realNormal[Z] - realpt[Z];
//The NORMAL of the plane has NO REAL POSITION. it is FROM ORIGIN. We use the normal here.
//If the dot product here is zero, the point lies on the plane.
return fxdot(pFNn, unitNormal);
}
int ptalt_plane(FIXED ptreal[XYZ], FIXED normal[XYZ], FIXED offset[XYZ]) //Shifts down the pFNn to suppress overflows
{
realNormal[X] = normal[X] + (offset[X]);
realNormal[Y] = normal[Y] + (offset[Y]);
realNormal[Z] = normal[Z] + (offset[Z]);
pFNn[X] = (realNormal[X] - ptreal[X])>>8;
pFNn[Y] = (realNormal[Y] - ptreal[Y])>>8;
pFNn[Z] = (realNormal[Z] - ptreal[Z])>>8;
return fxdot(pFNn, normal);
}
FIXED realpt_to_plane(FIXED ptreal[XYZ], FIXED normal[XYZ], FIXED offset[XYZ])
{
realNormal[X] = normal[X] + (offset[X]);
realNormal[Y] = normal[Y] + (offset[Y]);
realNormal[Z] = normal[Z] + (offset[Z]);
pFNn[X] = realNormal[X] - ptreal[X];
pFNn[Y] = realNormal[Y] - ptreal[Y];
pFNn[Z] = realNormal[Z] - ptreal[Z];
return fxdot(pFNn, normal);
}
//////////////////////////////////
// Line-to-plane projection function
// Line: p0->p1
// point_on_plane : a point on the plane
// unitNormal : the unit vector normal of the plane
// offset : world-space position of the point_on_plane. If point_on_plane is already, substitute with "zPt". (do not leave blank)
// output : the point at which the line intersects the plane
// return value : whether or not the output point is between p0 and p1
//////////////////////////////////
Bool line_hit_plane_here(FIXED p0[XYZ], FIXED p1[XYZ], FIXED point_on_plane[XYZ], FIXED unitNormal[XYZ], FIXED offset[XYZ], FIXED output[XYZ])
{
FIXED line_scalar;
FIXED vector_of_line[XYZ];
FIXED vector_to_plane[XYZ];
vector_of_line[X] = p0[X] - p1[X];
vector_of_line[Y] = p0[Y] - p1[Y];
vector_of_line[Z] = p0[Z] - p1[Z];
vector_to_plane[X] = (point_on_plane[X] + (offset[X])) - p0[X];
vector_to_plane[Y] = (point_on_plane[Y] + (offset[Y])) - p0[Y];
vector_to_plane[Z] = (point_on_plane[Z] + (offset[Z])) - p0[Z];
line_scalar = slDivFX(slInnerProduct(vector_of_line, unitNormal), slInnerProduct(vector_to_plane, unitNormal));
if(line_scalar > (1000<<16) || line_scalar < -(1000<<16)){
return 0;
}
output[X] = (p0[X] + fxm(vector_of_line[X], line_scalar));
output[Y] = (p0[Y] + fxm(vector_of_line[Y], line_scalar));
output[Z] = (p0[Z] + fxm(vector_of_line[Z], line_scalar));
return isPointonSegment(output, p0, p1);
}
void * align_4(void * ptr)
{
ptr += (((unsigned int)ptr) & 1) ? 1 : 0;
ptr += (((unsigned int)ptr) & 2) ? 1 : 0;
return ptr;
}