115
115
// saturateFast(v: F32xN) F32xN
116
116
// lerp(v0: F32xN, v1: F32xN, t: f32) F32xN
117
117
// lerpV(v0: F32xN, v1: F32xN, t: F32xN) F32xN
118
- // lerp_inverse (v0: F32xN, v1: F32xN, t: f32) F32xN
119
- // lerp_inverseV (v0: F32xN, v1: F32xN, t: F32xN) F32xN
120
- // map_linear (v: F32xN, min1: f32, max1: f32, min2: f32, max2: f32) F32xN
121
- // map_linearV (v: F32xN, min1: F32xN, max1: F32xN, min2: F32xN, max2: F32xN) F32xN
118
+ // lerpInverse (v0: F32xN, v1: F32xN, t: f32) F32xN
119
+ // lerpInverseV (v0: F32xN, v1: F32xN, t: F32xN) F32xN
120
+ // mapLinear (v: F32xN, min1: f32, max1: f32, min2: f32, max2: f32) F32xN
121
+ // mapLinearV (v: F32xN, min1: F32xN, max1: F32xN, min2: F32xN, max2: F32xN) F32xN
122
122
// sqrt(v: F32xN) F32xN
123
123
// abs(v: F32xN) F32xN
124
124
// mod(v0: F32xN, v1: F32xN) F32xN
@@ -1361,18 +1361,24 @@ pub inline fn lerpV(v0: anytype, v1: anytype, t: anytype) @TypeOf(v0, v1, t) {
1361
1361
return v0 + (v1 - v0 ) * t ; // subps, addps, mulps
1362
1362
}
1363
1363
1364
- pub inline fn lerp_inverse (v0 : anytype , v1 : anytype , t : anytype ) @TypeOf (v0 , v1 , t ) {
1364
+ pub inline fn lerpInverse (v0 : anytype , v1 : anytype , t : anytype ) @TypeOf (v0 , v1 ) {
1365
1365
const T = @TypeOf (v0 , v1 );
1366
1366
return (splat (T , t ) - v0 ) / (v1 - v0 );
1367
1367
}
1368
1368
1369
- pub inline fn lerp_inverseV (v0 : anytype , v1 : anytype , t : anytype ) @TypeOf (v0 , v1 , t ) {
1369
+ pub inline fn lerpInverseV (v0 : anytype , v1 : anytype , t : anytype ) @TypeOf (v0 , v1 , t ) {
1370
1370
return (t - v0 ) / (v1 - v0 );
1371
1371
}
1372
+ test "zmath.lerpInverse" {
1373
+ try expect (math .approxEqAbs (f32 , lerpInverseV (10.0 , 100.0 , 10.0 ), 0 , 0.0005 ));
1374
+ try expect (math .approxEqAbs (f32 , lerpInverseV (10.0 , 100.0 , 100.0 ), 1 , 0.0005 ));
1375
+ try expect (math .approxEqAbs (f32 , lerpInverseV (10.0 , 100.0 , 55.0 ), 0.5 , 0.05 ));
1376
+ try expect (approxEqAbs (lerpInverse (f32x4 (0 , 0 , 10 , 10 ), f32x4 (100 , 200 , 100 , 100 ), 10.0 ), f32x4 (0.1 , 0.05 , 0 , 0 ), 0.0005 ));
1377
+ }
1372
1378
1373
1379
/// To transform a vector of values from one range to another.
1374
- pub inline fn map_linear (v : anytype , min1 : anytype , max1 : anytype , min2 : anytype , max2 : anytype ) @TypeOf (v ) {
1375
- const T = @TypeOf (min1 , max1 , min2 , max2 );
1380
+ pub inline fn mapLinear (v : anytype , min1 : anytype , max1 : anytype , min2 : anytype , max2 : anytype ) @TypeOf (v ) {
1381
+ const T = @TypeOf (v );
1376
1382
const min1V = splat (T , min1 );
1377
1383
const max1V = splat (T , max1 );
1378
1384
const min2V = splat (T , min2 );
@@ -1381,10 +1387,17 @@ pub inline fn map_linear(v: anytype, min1: anytype, max1: anytype, min2: anytype
1381
1387
return min2V + (v - min1V ) * (max2V - min2V ) / dV ;
1382
1388
}
1383
1389
1384
- pub inline fn map_linearV (v : anytype , min1 : anytype , max1 : anytype , min2 : anytype , max2 : anytype ) @TypeOf (v , min1 , max1 , min2 , max2 ) {
1390
+ pub inline fn mapLinearV (v : anytype , min1 : anytype , max1 : anytype , min2 : anytype , max2 : anytype ) @TypeOf (v , min1 , max1 , min2 , max2 ) {
1385
1391
const d = max1 - min1 ;
1386
1392
return min2 + (v - min1 ) * (max2 - min2 ) / d ;
1387
1393
}
1394
+ test "zmath.mapLinear" {
1395
+ try expect (math .approxEqAbs (f32 , mapLinearV (0 , 0 , 1.2 , 10 , 100 ), 10 , 0.0005 ));
1396
+ try expect (math .approxEqAbs (f32 , mapLinearV (1.2 , 0 , 1.2 , 10 , 100 ), 100 , 0.0005 ));
1397
+ try expect (math .approxEqAbs (f32 , mapLinearV (0.6 , 0 , 1.2 , 10 , 100 ), 55 , 0.0005 ));
1398
+ try expect (approxEqAbs (mapLinearV (splat (F32x4 , 0 ), splat (F32x4 , 0 ), splat (F32x4 , 1.2 ), splat (F32x4 , 10 ), splat (F32x4 , 100 )), splat (F32x4 , 10 ), 0.0005 ));
1399
+ try expect (approxEqAbs (mapLinear (f32x4 (0 , 0 , 0.6 , 1.2 ), 0 , 1.2 , 10 , 100 ), f32x4 (10 , 10 , 55 , 100 ), 0.0005 ));
1400
+ }
1388
1401
1389
1402
pub const F32x4Component = enum { x , y , z , w };
1390
1403
0 commit comments