This repository has been archived by the owner on Nov 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
math.h
428 lines (362 loc) · 10.3 KB
/
math.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#ifndef __MATH3D_H__
#define __MATH3D_H__
typedef float matrix[9];
typedef float3 vector;
//=============================Basic Math=============================
static float
lerp(const float a,const float b,float value)
{
//return linear interpolation between the value a and b
private float result = a*(1-value)+b*value;
return result;
}
static float3
lerpv(const float3 a,const float3 b,float value)
{
//return linear interpolation between the vector a and b
private float3 result = a*(1-value)+b*value;
return result;
}
static float
fit(float a,const float smin,const float smax,const float min,const float max)
{
//takes the value in the range (smin.smax) and shifts it to the corresponding value in the new range (min,max)
private float slen = smax-smin;
private float len = max-min;
a -= smin;
a /= slen;
a *= len;
a += min;
return a;
}
static float
fit01(float a,const float min,const float max)
{
//takes the value in the range (0,1) and shifts it to the corresponding value in the new range (min,max)
private float len = max-min;
a *= len;
a += min;
return a;
}
static float3
fitv(float3 a,const float3 smin,const float3 smax,const float3 min,const float3 max)
{
//takes the vector in range (smin.smax) and shifts it to the corresponding value in the new range (min,max)
a.x = fit(a.x,smin.x,smax.x,min.x,max.x);
a.y = fit(a.y,smin.y,smax.y,min.y,max.y);
a.z = fit(a.z,smin.z,smax.z,min.z,max.z);
return a;
}
static float
angle(float3 v1,float3 v2)
{
//computes the angle between vector v1 and v2
v1 = normalize(v1);
v2 = normalize(v2);
private float angle = acos(dot(v1,v2));
angle = degrees(angle);
return angle;
}
//=============================Quaternion=============================
static float4
qnormalize(float4 q)
{
//normalizes quaternion
private const float mag2 = q.x*q.x+q.y*q.y+q.z*q.z+q.w*q.w;
if(mag2 == 0.0f) return q;
private const float mag = sqrt(mag2);
q.x /= mag;
q.y /= mag;
q.z /= mag;
q.w /= mag;
return q;
}
static float3
qrotate(const float3 v,const float4 q)
{
//rotates vector by quaternion
private const float3 quat_vector = {q.x, q.y, q.z};
private const float quat_scalar = q.w;
private const float3 t = 2.0f * cross(quat_vector, v);
private float3 rotated_vector = v + (quat_scalar * t) + cross(quat_vector, t);
return rotated_vector;
}
static float4
dihedral(const float3 v1,const float3 v2)
{
//computes the rotation quaternion which rotates the vector v1 onto the vector v2
private float3 v = cross(v1, v2);
private float v1_len = length(v1);
private float v2_len = length(v2);
private float q_w = sqrt(v1_len * v1_len * v2_len * v2_len) + dot(v1, v2);
private float4 q = {v.x, v.y, v.z, q_w};
q = normalize(q);
return q;
}
static float4
eulertoq(const float3 v)
{
//creates a quaternion from euler angles
private float4 q;
private const float x = v.x*0.5f;
private const float y = v.y*0.5f;
private const float z = v.z*0.5f;
private const float sx = sin(x);
private const float sy = sin(y);
private const float sz = sin(z);
private const float cx = cos(x);
private const float cy = cos(y);
private const float cz = cos(z);
q.x = sx*cy*cz - cx*sy*sz;
q.y = cx*sy*cz + sx*cy*sz;
q.z = cx*cy*sz - sx*sy*cz;
q.w = cx*cy*cz + sx*sy*sz;
return q;
}
static float3
qtoeuler(const float4 q)
{
//creates euler angle representing quaternion
private float3 v;
v.x = atan2(2.0f*(q.x*q.w),1.0f-2.0f*(q.x*q.x+q.y*q.y));
v.y = asin(2.0f*(q.y*q.w-q.x*q.z));
v.z = atan2(2.0f*(q.z*q.w+q.x*q.y),1.0f-2.0f*(q.y*q.y+q.z*q.z));
return v;
}
static float4
quaternion(const float ang,float3 axi)
{
//generates a quaternion using angle and axi
axi = normalize(axi);
private float4 q;
const float sinAng = sin(ang*0.5f);
q.x = axi.x*sinAng;
q.y = axi.y*sinAng;
q.z = axi.z*sinAng;
q.w = cos(ang*0.5f);
return q;
}
static void
qconvert(matrix a,const float4 q)
{
//converts a quaternion to a 3x3matrix
private const float x2 = q.x*q.x;
private const float y2 = q.y*q.y;
private const float z2 = q.z*q.z;
private const float xy = q.x*q.y;
private const float xz = q.x*q.z;
private const float yz = q.y*q.z;
private const float wx = q.w*q.x;
private const float wy = q.w*q.y;
private const float wz = q.w*q.z;
a[0] = 1.0f - 2.0f * (y2 + z2);
a[1] = 2.0f * (xy + wz);
a[2] = 2.0f * (xz - wy);
a[3] = 2.0f * (xy - wz);
a[4] = 1.0f - 2.0f * (x2 + z2);
a[5] = 2.0f * (yz + wx);
a[6] = 2.0f * (xz + wy);
a[7] = 2.0f * (yz - wx);
a[8] = 1.0f - 2.0f * (x2 + y2);
}
static float4
qMul(const float4 q1,const float4 q2)
{
//multiplies two quaternions and returns the result
private float4 q;
q.x = q1.w*q2.x + q1.x*q2.w + q1.y*q2.z - q1.z*q2.y;
q.y = q1.w*q2.y + q1.y*q2.w + q1.z*q2.x - q1.x*q2.z;
q.z = q1.w*q2.z + q1.z*q2.w + q1.x*q2.y - q1.y*q2.x;
q.w = q1.w*q2.w - q1.x*q2.x - q1.y*q2.y - q1.z*q2.z;
return q;
}
static float4
qinvert(float4 q)
{
//inverts a quaternion rotation
q.x *= -1.0;
q.y *= -1.0;
q.z *= -1.0;
return q;
}
static float4
qlerp(const float4 q1,const float4 q2,const float value)
{
//return linear interpolation between the q1 and q2
private float4 q;
q.x = lerp(q1.x,q2.x,value);
q.y = lerp(q1.y,q2.y,value);
q.z = lerp(q1.z,q2.z,value);
q.w = lerp(q1.w,q2.w,value);
q = qnormalize(q);
return q;
}
static float4
qslerp(const float4 q1,const float4 q2,const float value)
{
//return a quaternion blend between q1 and q2 based on the bias
private float4 q;
private float cosa = q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w;
// If the dot product is negative, the quaternions have opposite handed-ness and slerp won't take
// the shorter path. Fix by reversing one quaternion.
if ( cosa < 0.0f )
{
q2.x = -q2.x;
q2.y = -q2.y;
q2.z = -q2.z;
q2.w = -q2.w;
cosa = -cosa;
}
private float k0, k1;
// If the inputs are too close for comfort, linearly interpolate
if ( cosa > 0.9995f )
{
k0 = 1.0f - value;
k1 = value;
}
else
{
float sina = sqrt( 1.0f - cosa*cosa );
float a = atan2( sina, cosa );
k0 = sin((1.0f - value)*a) / sina;
k1 = sin(value*a) / sina;
}
q.x = q1.x*k0 + q2.x*k1;
q.y = q1.y*k0 + q2.y*k1;
q.z = q1.z*k0 + q2.z*k1;
q.w = q1.w*k0 + q2.w*k1;
return q;
}
static void
qconvertToaa(float3 *axi,float *angle,const float4 q)
{
//converts a quaternion to axi and angle
//example: qconvertToaa(&axi,&angle,q)
private const float scale = sqrt(q.x*q.x+q.y*q.y+q.z*q.z);
axi->x = q.x/scale;
axi->y = q.y/scale;
axi->z = q.z/scale;
*angle = acos(q.w)*2.0f;
}
//=============================Random=============================
static float
srand(const float x, const float y){
//core random function
private float a = cos(x * 12.9898f + y * 4.1414f) * 43758.5453f;
return a-floor(a);
}
static float
rand(const float3 seed){
//return random float
private float a = srand(seed.x, seed.y);
private float b = srand(seed.z, seed.y);
private float c = srand(seed.x, seed.z);
private float d = srand(a, b);
private float e = srand(b, c);
private float f = srand(d, e);
return f;
}
static float3
randv(const float3 seed){
//return random float3
private float a = srand(seed.x, seed.y);
private float b = srand(seed.z, seed.y);
private float c = srand(seed.x, seed.z);
private float d = srand(a, b);
private float e = srand(b, c);
private float f = srand(d, e);
private float3 r = {d,e,f};
return r;
}
//=============================Matrix=============================
static void
ident(matrix a)
{
//makes matrix a become identity matrix
for(int i=0;i<9;i++){
a[i] = 0;
}
a[0] = 1;
a[4] = 1;
a[8] = 1;
}
static void
mCopy(matrix a,const matrix b)
{
//copies matrix b to a
for(int i=0;i<9;i++){
a[i] = b[i];
}
}
static void
mstore(const matrix a,const int idx, global float * data)
{
//stores matrix to attribute
private const int start = idx*9;
private int id = 0;
for(int i = start;i<start+9;i++){
data[i] = a[id];
id++;
}
}
static void
mload(matrix a,const int idx, global float * data)
{
//loads matix from attribute
private const int start = idx*9;
private int id = 0;
for(int i = start;i<start+9;i++){
a[id] = data[i];
id++;
}
}
static void
mMul(matrix a,matrix b,const matrix c)
{
//multiplies matrix b,c and apply the result to matrix a
private matrix tmp;
private int n = 0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
n = i*3;
tmp[n+j] = b[n]*c[j] + b[n+1]*c[j+3] + b[n+2]*c[j+6];
}
}
mCopy(a,tmp);
}
static float3
vmMul(float3 a,const matrix b)
{
//vector multiply matrix
private float3 tmp;
tmp.x = a.x*b[0] + a.y*b[3] + a.z*b[6];
tmp.y = a.x*b[1] + a.y*b[4] + a.z*b[7];
tmp.z = a.x*b[2] + a.y*b[5] + a.z*b[8];
return tmp;
}
static void
mScale(matrix a,const float3 b)
{
//scales the matrix a in three directions simultaneously (X, Y, Z - given by the components of the scale_vector).
a[0] *= b.x;
a[4] *= b.y;
a[8] *= b.z;
}
static void
mDirScale(matrix a,const float3 d,float k)
{
//scales the mateix a ,d is scaled direction,k is strength
k -= 1.0f;
private matrix rot;
rot[0] = 1 + k * d.x*d.x;
rot[1] = k * d.x*d.y;
rot[2] = k * d.x*d.z;
rot[3] = k * d.x*d.y;
rot[4] = 1 + k * d.y*d.y;
rot[5] = k * d.y*d.z;
rot[6] = k * d.x*d.z;
rot[7] = k * d.y*d.z;
rot[8] = 1 + k * d.z*d.z;
mMul(a,a,rot);
}
#endif