Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,83 @@
package kha.math;
class FastMatrix2 {
static inline var width: Int = 2;
static inline var height: Int = 2;
public var _00: FastFloat;
public var _10: FastFloat;
public var _01: FastFloat;
public var _11: FastFloat;
public inline function new(_00: FastFloat, _10: FastFloat, _01: FastFloat, _11: FastFloat) {
this._00 = _00;
this._10 = _10;
this._01 = _01;
this._11 = _11;
}
/*@:arrayAccess public inline function get(index: Int): Float {
return this[index];
}
@:arrayAccess public inline function set(index: Int, value: Float): Float {
this[index] = value;
return value;
}
public static function index(x: Int, y: Int): Int {
return y * width + x;
}*/
extern public inline function setFrom(m: FastMatrix2): Void {
this._00 = m._00;
this._10 = m._10;
this._01 = m._01;
this._11 = m._11;
}
extern public static inline function empty(): FastMatrix2 {
return new FastMatrix2(0, 0, 0, 0);
}
extern public static inline function identity(): FastMatrix2 {
return new FastMatrix2(1, 0, 0, 1);
}
extern public static inline function scale(x: FastFloat, y: FastFloat): FastMatrix2 {
return new FastMatrix2(x, 0, 0, y);
}
extern public static inline function rotation(alpha: FastFloat): FastMatrix2 {
return new FastMatrix2(Math.cos(alpha), -Math.sin(alpha), Math.sin(alpha), Math.cos(alpha));
}
extern public inline function add(m: FastMatrix2): FastMatrix2 {
return new FastMatrix2(_00 + m._00, _10 + m._10, _01 + m._01, _11 + m._11);
}
extern public inline function sub(m: FastMatrix2): FastMatrix2 {
return new FastMatrix2(_00 - m._00, _10 - m._10, _01 - m._01, _11 - m._11);
}
extern public inline function mult(value: FastFloat): FastMatrix2 {
return new FastMatrix2(_00 * value, _10 * value, _01 * value, _11 * value);
}
extern public inline function transpose(): FastMatrix2 {
return new FastMatrix2(_00, _01, _10, _11);
}
extern public inline function trace(): FastFloat {
return _00 + _11;
}
extern public inline function multmat(m: FastMatrix2): FastMatrix2 {
return new FastMatrix2(_00 * m._00 + _10 * m._01, _00 * m._10 + _10 * m._11, _01 * m._00 + _11 * m._01, _01 * m._10 + _11 * m._11);
}
extern public inline function multvec(value: FastVector2): FastVector2 {
var x = _00 * value.x + _10 * value.y;
var y = _01 * value.x + _11 * value.y;
return new FastVector2(x, y);
}
}

View File

@ -0,0 +1,176 @@
package kha.math;
class FastMatrix3 {
static inline var width: Int = 3;
static inline var height: Int = 3;
/** Horizontal scaling. A value of 1 results in no scaling. */
public var _00: FastFloat;
/** Horizontal skewing. */
public var _10: FastFloat;
/** Horizontal translation (moving). */
public var _20: FastFloat;
/** Vertical skewing. */
public var _01: FastFloat;
/** Vertical scaling. A value of 1 results in no scaling. */
public var _11: FastFloat;
/** Vertical translation (moving). */
public var _21: FastFloat;
public var _02: FastFloat;
public var _12: FastFloat;
public var _22: FastFloat;
public inline function new(_00: FastFloat, _10: FastFloat, _20: FastFloat, _01: FastFloat, _11: FastFloat, _21: FastFloat, _02: FastFloat, _12: FastFloat,
_22: FastFloat) {
this._00 = _00;
this._10 = _10;
this._20 = _20;
this._01 = _01;
this._11 = _11;
this._21 = _21;
this._02 = _02;
this._12 = _12;
this._22 = _22;
}
public static inline function fromMatrix3(m: Matrix3): FastMatrix3 {
return new FastMatrix3(m._00, m._10, m._20, m._01, m._11, m._21, m._02, m._12, m._22);
}
/*@:arrayAccess public inline function get(index: Int): Float {
return this[index];
}
@:arrayAccess public inline function set(index: Int, value: Float): Float {
this[index] = value;
return value;
}
public static function index(x: Int, y: Int): Int {
return y * width + x;
}*/
extern public inline function setFrom(m: FastMatrix3): Void {
this._00 = m._00;
this._10 = m._10;
this._20 = m._20;
this._01 = m._01;
this._11 = m._11;
this._21 = m._21;
this._02 = m._02;
this._12 = m._12;
this._22 = m._22;
}
extern public static inline function translation(x: FastFloat, y: FastFloat): FastMatrix3 {
return new FastMatrix3(1, 0, x, 0, 1, y, 0, 0, 1);
}
extern public static inline function empty(): FastMatrix3 {
return new FastMatrix3(0, 0, 0, 0, 0, 0, 0, 0, 0);
}
extern public static inline function identity(): FastMatrix3 {
return new FastMatrix3(1, 0, 0, 0, 1, 0, 0, 0, 1);
}
extern public static inline function scale(x: FastFloat, y: FastFloat): FastMatrix3 {
return new FastMatrix3(x, 0, 0, 0, y, 0, 0, 0, 1);
}
extern public static inline function rotation(alpha: FastFloat): FastMatrix3 {
return new FastMatrix3(Math.cos(alpha), -Math.sin(alpha), 0, Math.sin(alpha), Math.cos(alpha), 0, 0, 0, 1);
}
extern public inline function add(m: FastMatrix3): FastMatrix3 {
return new FastMatrix3(_00 + m._00, _10 + m._10, _20 + m._20, _01 + m._01, _11 + m._11, _21 + m._21, _02 + m._02, _12 + m._12, _22 + m._22);
}
extern public inline function sub(m: FastMatrix3): FastMatrix3 {
return new FastMatrix3(_00 - m._00, _10 - m._10, _20 - m._20, _01 - m._01, _11 - m._11, _21 - m._21, _02 - m._02, _12 - m._12, _22 - m._22);
}
extern public inline function mult(value: FastFloat): FastMatrix3 {
return new FastMatrix3(_00 * value, _10 * value, _20 * value, _01 * value, _11 * value, _21 * value, _02 * value, _12 * value, _22 * value);
}
extern public inline function transpose(): FastMatrix3 {
return new FastMatrix3(_00, _01, _02, _10, _11, _12, _20, _21, _22);
}
extern public inline function trace(): FastFloat {
return _00 + _11 + _22;
}
extern public inline function multmat(m: FastMatrix3): FastMatrix3 {
return new FastMatrix3(_00 * m._00
+ _10 * m._01
+ _20 * m._02, _00 * m._10
+ _10 * m._11
+ _20 * m._12, _00 * m._20
+ _10 * m._21
+ _20 * m._22,
_01 * m._00
+ _11 * m._01
+ _21 * m._02, _01 * m._10
+ _11 * m._11
+ _21 * m._12, _01 * m._20
+ _11 * m._21
+ _21 * m._22,
_02 * m._00
+ _12 * m._01
+ _22 * m._02, _02 * m._10
+ _12 * m._11
+ _22 * m._12, _02 * m._20
+ _12 * m._21
+ _22 * m._22);
}
extern public inline function multvec(value: FastVector2): FastVector2 {
// var product = new Vector2(0, 0);
var w = _02 * value.x + _12 * value.y + _22 * 1;
var x = (_00 * value.x + _10 * value.y + _20 * 1) / w;
// type hint requred because of haxe bug #8220
var y: FastFloat = (_01 * value.x + _11 * value.y + _21 * 1) / w;
return new FastVector2(x, y);
}
extern public inline function cofactor(m0: FastFloat, m1: FastFloat, m2: FastFloat, m3: FastFloat): Float {
return m0 * m3 - m1 * m2;
}
extern public inline function determinant(): FastFloat {
var c00 = cofactor(_11, _21, _12, _22);
var c01 = cofactor(_10, _20, _12, _22);
var c02 = cofactor(_10, _20, _11, _21);
return _00 * c00 - _01 * c01 + _02 * c02;
}
extern public inline function inverse(): FastMatrix3 {
var c00 = cofactor(_11, _21, _12, _22);
var c01 = cofactor(_10, _20, _12, _22);
var c02 = cofactor(_10, _20, _11, _21);
var det: FastFloat = _00 * c00 - _01 * c01 + _02 * c02;
if (Math.abs(det) < 0.000001) {
throw "determinant is too small";
}
var c10 = cofactor(_01, _21, _02, _22);
var c11 = cofactor(_00, _20, _02, _22);
var c12 = cofactor(_00, _20, _01, _21);
var c20 = cofactor(_01, _11, _02, _12);
var c21 = cofactor(_00, _10, _02, _12);
var c22 = cofactor(_00, _10, _01, _11);
var invdet: FastFloat = 1.0 / det;
return new FastMatrix3(c00 * invdet, -c01 * invdet, c02 * invdet, -c10 * invdet, c11 * invdet, -c12 * invdet, c20 * invdet, -c21 * invdet,
c22 * invdet);
}
}

View File

@ -0,0 +1,319 @@
package kha.math;
class FastMatrix4 {
static inline var width: Int = 4;
static inline var height: Int = 4;
public var _00: FastFloat;
public var _10: FastFloat;
public var _20: FastFloat;
public var _30: FastFloat;
public var _01: FastFloat;
public var _11: FastFloat;
public var _21: FastFloat;
public var _31: FastFloat;
public var _02: FastFloat;
public var _12: FastFloat;
public var _22: FastFloat;
public var _32: FastFloat;
public var _03: FastFloat;
public var _13: FastFloat;
public var _23: FastFloat;
public var _33: FastFloat;
public inline function new(_00: FastFloat, _10: FastFloat, _20: FastFloat, _30: FastFloat, _01: FastFloat, _11: FastFloat, _21: FastFloat, _31: FastFloat,
_02: FastFloat, _12: FastFloat, _22: FastFloat, _32: FastFloat, _03: FastFloat, _13: FastFloat, _23: FastFloat, _33: FastFloat) {
this._00 = _00;
this._10 = _10;
this._20 = _20;
this._30 = _30;
this._01 = _01;
this._11 = _11;
this._21 = _21;
this._31 = _31;
this._02 = _02;
this._12 = _12;
this._22 = _22;
this._32 = _32;
this._03 = _03;
this._13 = _13;
this._23 = _23;
this._33 = _33;
}
public static inline function fromMatrix4(m: Matrix4): FastMatrix4 {
return new FastMatrix4(m._00, m._10, m._20, m._30, m._01, m._11, m._21, m._31, m._02, m._12, m._22, m._32, m._03, m._13, m._23, m._33);
}
extern public inline function setFrom(m: FastMatrix4): Void {
this._00 = m._00;
this._10 = m._10;
this._20 = m._20;
this._30 = m._30;
this._01 = m._01;
this._11 = m._11;
this._21 = m._21;
this._31 = m._31;
this._02 = m._02;
this._12 = m._12;
this._22 = m._22;
this._32 = m._32;
this._03 = m._03;
this._13 = m._13;
this._23 = m._23;
this._33 = m._33;
}
extern public static inline function translation(x: FastFloat, y: FastFloat, z: FastFloat): FastMatrix4 {
return new FastMatrix4(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1);
}
extern public static inline function empty(): FastMatrix4 {
return new FastMatrix4(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
extern public static inline function identity(): FastMatrix4 {
return new FastMatrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
}
extern public static inline function scale(x: FastFloat, y: FastFloat, z: FastFloat): FastMatrix4 {
return new FastMatrix4(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
}
extern public static inline function rotationX(alpha: FastFloat): FastMatrix4 {
var ca = Math.cos(alpha);
var sa = Math.sin(alpha);
return new FastMatrix4(1, 0, 0, 0, 0, ca, -sa, 0, 0, sa, ca, 0, 0, 0, 0, 1);
}
extern public static inline function rotationY(alpha: FastFloat): FastMatrix4 {
var ca = Math.cos(alpha);
var sa = Math.sin(alpha);
return new FastMatrix4(ca, 0, sa, 0, 0, 1, 0, 0, -sa, 0, ca, 0, 0, 0, 0, 1);
}
extern public static inline function rotationZ(alpha: FastFloat): FastMatrix4 {
var ca = Math.cos(alpha);
var sa = Math.sin(alpha);
return new FastMatrix4(ca, -sa, 0, 0, sa, ca, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
}
extern public static inline function rotation(yaw: FastFloat, pitch: FastFloat, roll: FastFloat): FastMatrix4 {
var sy = Math.sin(yaw);
var cy = Math.cos(yaw);
var sx = Math.sin(pitch);
var cx = Math.cos(pitch);
var sz = Math.sin(roll);
var cz = Math.cos(roll);
return new FastMatrix4(cx * cy, cx * sy * sz
- sx * cz, cx * sy * cz
+ sx * sz, 0, sx * cy, sx * sy * sz
+ cx * cz, sx * sy * cz
- cx * sz, 0,
-sy,
cy * sz, cy * cz, 0, 0, 0, 0, 1);
}
// Inlining with `2` instead of `2.0` leads to wrong temp var type in Java/C#
public static inline function orthogonalProjection(left: FastFloat, right: FastFloat, bottom: FastFloat, top: FastFloat, zn: FastFloat,
zf: FastFloat): FastMatrix4 {
var tx: FastFloat = -(right + left) / (right - left);
var ty: FastFloat = -(top + bottom) / (top - bottom);
var tz: FastFloat = -(zf + zn) / (zf - zn);
return new FastMatrix4(2 / (right - left), 0, 0, tx, 0, 2.0 / (top - bottom), 0, ty, 0, 0, -2 / (zf - zn), tz, 0, 0, 0, 1);
}
public static inline function perspectiveProjection(fovY: FastFloat, aspect: FastFloat, zn: FastFloat, zf: FastFloat): FastMatrix4 {
var uh = 1.0 / Math.tan(fovY / 2);
var uw = uh / aspect;
return new FastMatrix4(uw, 0, 0, 0, 0, uh, 0, 0, 0, 0, (zf + zn) / (zn - zf), 2 * zf * zn / (zn - zf), 0, 0, -1, 0);
}
public static inline function lookAt(eye: FastVector3, at: FastVector3, up: FastVector3): FastMatrix4 {
var zaxis = at.sub(eye).normalized();
var xaxis = zaxis.cross(up).normalized();
var yaxis = xaxis.cross(zaxis);
return new FastMatrix4(xaxis.x, xaxis.y, xaxis.z, -xaxis.dot(eye), yaxis.x, yaxis.y, yaxis.z, -yaxis.dot(eye), -zaxis.x, -zaxis.y, -zaxis.z,
zaxis.dot(eye), 0, 0, 0, 1);
}
extern public inline function add(m: FastMatrix4): FastMatrix4 {
return new FastMatrix4(_00
+ m._00, _10
+ m._10, _20
+ m._20, _30
+ m._30, _01
+ m._01, _11
+ m._11, _21
+ m._21, _31
+ m._31, _02
+ m._02,
_12
+ m._12, _22
+ m._22, _32
+ m._32, _03
+ m._03, _13
+ m._13, _23
+ m._23, _33
+ m._33);
}
extern public inline function sub(m: FastMatrix4): FastMatrix4 {
return new FastMatrix4(_00
- m._00, _10
- m._10, _20
- m._20, _30
- m._30, _01
- m._01, _11
- m._11, _21
- m._21, _31
- m._31, _02
- m._02,
_12
- m._12, _22
- m._22, _32
- m._32, _03
- m._03, _13
- m._13, _23
- m._23, _33
- m._33);
}
extern public inline function mult(value: FastFloat): FastMatrix4 {
return new FastMatrix4(_00 * value, _10 * value, _20 * value, _30 * value, _01 * value, _11 * value, _21 * value, _31 * value, _02 * value,
_12 * value, _22 * value, _32 * value, _03 * value, _13 * value, _23 * value, _33 * value);
}
extern public inline function transpose(): FastMatrix4 {
return new FastMatrix4(_00, _01, _02, _03, _10, _11, _12, _13, _20, _21, _22, _23, _30, _31, _32, _33);
}
extern public inline function transpose3x3(): FastMatrix4 {
return new FastMatrix4(_00, _01, _02, _30, _10, _11, _12, _31, _20, _21, _22, _32, _03, _13, _23, _33);
}
extern public inline function trace(): FastFloat {
return _00 + _11 + _22 + _33;
}
extern public inline function multmat(m: FastMatrix4): FastMatrix4 {
return new FastMatrix4(_00 * m._00
+ _10 * m._01
+ _20 * m._02
+ _30 * m._03, _00 * m._10
+ _10 * m._11
+ _20 * m._12
+ _30 * m._13,
_00 * m._20
+ _10 * m._21
+ _20 * m._22
+ _30 * m._23, _00 * m._30
+ _10 * m._31
+ _20 * m._32
+ _30 * m._33,
_01 * m._00
+ _11 * m._01
+ _21 * m._02
+ _31 * m._03, _01 * m._10
+ _11 * m._11
+ _21 * m._12
+ _31 * m._13,
_01 * m._20
+ _11 * m._21
+ _21 * m._22
+ _31 * m._23, _01 * m._30
+ _11 * m._31
+ _21 * m._32
+ _31 * m._33,
_02 * m._00
+ _12 * m._01
+ _22 * m._02
+ _32 * m._03, _02 * m._10
+ _12 * m._11
+ _22 * m._12
+ _32 * m._13,
_02 * m._20
+ _12 * m._21
+ _22 * m._22
+ _32 * m._23, _02 * m._30
+ _12 * m._31
+ _22 * m._32
+ _32 * m._33,
_03 * m._00
+ _13 * m._01
+ _23 * m._02
+ _33 * m._03, _03 * m._10
+ _13 * m._11
+ _23 * m._12
+ _33 * m._13,
_03 * m._20
+ _13 * m._21
+ _23 * m._22
+ _33 * m._23, _03 * m._30
+ _13 * m._31
+ _23 * m._32
+ _33 * m._33);
}
extern public inline function multvec(value: FastVector4): FastVector4 {
var product = new FastVector4();
product.x = _00 * value.x + _10 * value.y + _20 * value.z + _30 * value.w;
product.y = _01 * value.x + _11 * value.y + _21 * value.z + _31 * value.w;
product.z = _02 * value.x + _12 * value.y + _22 * value.z + _32 * value.w;
product.w = _03 * value.x + _13 * value.y + _23 * value.z + _33 * value.w;
return product;
}
extern public inline function cofactor(m0: FastFloat, m1: FastFloat, m2: FastFloat, m3: FastFloat, m4: FastFloat, m5: FastFloat, m6: FastFloat,
m7: FastFloat, m8: FastFloat): FastFloat {
return m0 * (m4 * m8 - m5 * m7) - m1 * (m3 * m8 - m5 * m6) + m2 * (m3 * m7 - m4 * m6);
}
extern public inline function determinant(): FastFloat {
var c00 = cofactor(_11, _21, _31, _12, _22, _32, _13, _23, _33);
var c01 = cofactor(_10, _20, _30, _12, _22, _32, _13, _23, _33);
var c02 = cofactor(_10, _20, _30, _11, _21, _31, _13, _23, _33);
var c03 = cofactor(_10, _20, _30, _11, _21, _31, _12, _22, _32);
return _00 * c00 - _01 * c01 + _02 * c02 - _03 * c03;
}
extern public inline function inverse(): FastMatrix4 {
var c00 = cofactor(_11, _21, _31, _12, _22, _32, _13, _23, _33);
var c01 = cofactor(_10, _20, _30, _12, _22, _32, _13, _23, _33);
var c02 = cofactor(_10, _20, _30, _11, _21, _31, _13, _23, _33);
var c03 = cofactor(_10, _20, _30, _11, _21, _31, _12, _22, _32);
var det: FastFloat = _00 * c00 - _01 * c01 + _02 * c02 - _03 * c03;
if (Math.abs(det) < 0.000001) {
throw "determinant is too small";
}
var c10 = cofactor(_01, _21, _31, _02, _22, _32, _03, _23, _33);
var c11 = cofactor(_00, _20, _30, _02, _22, _32, _03, _23, _33);
var c12 = cofactor(_00, _20, _30, _01, _21, _31, _03, _23, _33);
var c13 = cofactor(_00, _20, _30, _01, _21, _31, _02, _22, _32);
var c20 = cofactor(_01, _11, _31, _02, _12, _32, _03, _13, _33);
var c21 = cofactor(_00, _10, _30, _02, _12, _32, _03, _13, _33);
var c22 = cofactor(_00, _10, _30, _01, _11, _31, _03, _13, _33);
var c23 = cofactor(_00, _10, _30, _01, _11, _31, _02, _12, _32);
var c30 = cofactor(_01, _11, _21, _02, _12, _22, _03, _13, _23);
var c31 = cofactor(_00, _10, _20, _02, _12, _22, _03, _13, _23);
var c32 = cofactor(_00, _10, _20, _01, _11, _21, _03, _13, _23);
var c33 = cofactor(_00, _10, _20, _01, _11, _21, _02, _12, _22);
var invdet: FastFloat = 1.0 / det;
return new FastMatrix4(c00 * invdet,
-c01 * invdet, c02 * invdet,
-c03 * invdet,
-c10 * invdet, c11 * invdet,
-c12 * invdet, c13 * invdet,
c20 * invdet,
-c21 * invdet, c22 * invdet,
-c23 * invdet,
-c30 * invdet, c31 * invdet,
-c32 * invdet, c33 * invdet);
}
}

View File

@ -0,0 +1,75 @@
package kha.math;
@:structInit
class FastVector2 {
public inline function new(x: FastFloat = 0, y: FastFloat = 0): Void {
this.x = x;
this.y = y;
}
public static function fromVector2(v: Vector2): FastVector2 {
return new FastVector2(v.x, v.y);
}
public var x: FastFloat;
public var y: FastFloat;
public var length(get, set): FastFloat;
extern public inline function setFrom(v: FastVector2): Void {
this.x = v.x;
this.y = v.y;
}
inline function get_length(): FastFloat {
return Math.sqrt(x * x + y * y);
}
inline function set_length(length: FastFloat): FastFloat {
var currentLength = get_length();
if (currentLength == 0)
return 0;
var mul = length / currentLength;
x *= mul;
y *= mul;
return length;
}
extern public inline function add(vec: FastVector2): FastVector2 {
return new FastVector2(x + vec.x, y + vec.y);
}
extern public inline function sub(vec: FastVector2): FastVector2 {
return new FastVector2(x - vec.x, y - vec.y);
}
extern public inline function mult(value: FastFloat): FastVector2 {
return new FastVector2(x * value, y * value);
}
extern public inline function div(value: FastFloat): FastVector2 {
return mult(1 / value);
}
extern public inline function dot(v: FastVector2): FastFloat {
return x * v.x + y * v.y;
}
@:deprecated("normalize() will be deprecated soon, use the immutable normalized() instead")
extern public inline function normalize(): Void {
#if haxe4 inline #end set_length(1);
}
extern public inline function normalized(): FastVector2 {
var v = new FastVector2(x, y);
#if haxe4 inline #end v.set_length(1);
return v;
}
extern public inline function angle(v: FastVector2): FastFloat {
return Math.atan2(x * v.y - y * v.x, x * v.x + y * v.y);
}
public function toString() {
return 'FastVector2($x, $y)';
}
}

View File

@ -0,0 +1,78 @@
package kha.math;
@:structInit
class FastVector3 {
public inline function new(x: FastFloat = 0, y: FastFloat = 0, z: FastFloat = 0): Void {
this.x = x;
this.y = y;
this.z = z;
}
public static function fromVector3(v: Vector3): FastVector3 {
return new FastVector3(v.x, v.y, v.z);
}
public var x: FastFloat;
public var y: FastFloat;
public var z: FastFloat;
public var length(get, set): FastFloat;
extern public inline function setFrom(v: FastVector3): Void {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
inline function get_length(): FastFloat {
return Math.sqrt(x * x + y * y + z * z);
}
function set_length(length: FastFloat): FastFloat {
var currentLength = get_length();
if (currentLength == 0)
return 0;
var mul = length / currentLength;
x *= mul;
y *= mul;
z *= mul;
return length;
}
extern public inline function add(vec: FastVector3): FastVector3 {
return new FastVector3(x + vec.x, y + vec.y, z + vec.z);
}
extern public inline function sub(vec: FastVector3): FastVector3 {
return new FastVector3(x - vec.x, y - vec.y, z - vec.z);
}
extern public inline function mult(value: FastFloat): FastVector3 {
return new FastVector3(x * value, y * value, z * value);
}
extern public inline function dot(v: FastVector3): FastFloat {
return x * v.x + y * v.y + z * v.z;
}
extern public inline function cross(v: FastVector3): FastVector3 {
var _x = y * v.z - z * v.y;
var _y = z * v.x - x * v.z;
var _z = x * v.y - y * v.x;
return new FastVector3(_x, _y, _z);
}
@:deprecated("normalize() will be deprecated soon, use the immutable normalized() instead")
extern public inline function normalize(): Void {
#if haxe4 inline #end set_length(1);
}
extern public inline function normalized(): FastVector3 {
var v = new FastVector3(x, y, z);
#if haxe4 inline #end v.set_length(1);
return v;
}
public function toString() {
return 'FastVector3($x, $y, $z)';
}
}

View File

@ -0,0 +1,71 @@
package kha.math;
@:structInit
class FastVector4 {
public inline function new(x: FastFloat = 0, y: FastFloat = 0, z: FastFloat = 0, w: FastFloat = 1): Void {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public static function fromVector4(v: Vector4): FastVector4 {
return new FastVector4(v.x, v.y, v.z, v.w);
}
public var x: FastFloat;
public var y: FastFloat;
public var z: FastFloat;
public var w: FastFloat;
public var length(get, set): FastFloat;
extern public inline function setFrom(v: FastVector4): Void {
this.x = v.x;
this.y = v.y;
this.z = v.z;
this.w = v.w;
}
inline function get_length(): FastFloat {
return Math.sqrt(x * x + y * y + z * z + w * w);
}
function set_length(length: FastFloat): FastFloat {
var currentLength = get_length();
if (currentLength == 0)
return 0;
var mul = length / currentLength;
x *= mul;
y *= mul;
z *= mul;
w *= mul;
return length;
}
extern public inline function add(vec: FastVector4): FastVector4 {
return new FastVector4(x + vec.x, y + vec.y, z + vec.z, w + vec.w);
}
extern public inline function sub(vec: FastVector4): FastVector4 {
return new FastVector4(x - vec.x, y - vec.y, z - vec.z, w - vec.w);
}
extern public inline function mult(value: FastFloat): FastVector4 {
return new FastVector4(x * value, y * value, z * value, w * value);
}
@:deprecated("normalize() will be deprecated soon, use the immutable normalized() instead")
extern public inline function normalize(): Void {
#if haxe4 inline #end set_length(1);
}
extern public inline function normalized(): FastVector4 {
var v = new FastVector4(x, y, z, w);
#if haxe4 inline #end v.set_length(1);
return v;
}
public function toString() {
return 'FastVector4($x, $y, $z, $w)';
}
}

View File

@ -0,0 +1,83 @@
package kha.math;
class Matrix2 {
static inline var width: Int = 2;
static inline var height: Int = 2;
public var _00: Float;
public var _10: Float;
public var _01: Float;
public var _11: Float;
public inline function new(_00: Float, _10: Float, _01: Float, _11: Float) {
this._00 = _00;
this._10 = _10;
this._01 = _01;
this._11 = _11;
}
/*@:arrayAccess public inline function get(index: Int): Float {
return this[index];
}
@:arrayAccess public inline function set(index: Int, value: Float): Float {
this[index] = value;
return value;
}
public static function index(x: Int, y: Int): Int {
return y * width + x;
}*/
extern public inline function setFrom(m: Matrix2): Void {
this._00 = m._00;
this._10 = m._10;
this._01 = m._01;
this._11 = m._11;
}
extern public static inline function empty(): Matrix2 {
return new Matrix2(0, 0, 0, 0);
}
extern public static inline function identity(): Matrix2 {
return new Matrix2(1, 0, 0, 1);
}
extern public static inline function scale(x: Float, y: Float): Matrix2 {
return new Matrix2(x, 0, 0, y);
}
extern public static inline function rotation(alpha: Float): Matrix2 {
return new Matrix2(Math.cos(alpha), -Math.sin(alpha), Math.sin(alpha), Math.cos(alpha));
}
extern public inline function add(m: Matrix2): Matrix2 {
return new Matrix2(_00 + m._00, _10 + m._10, _01 + m._01, _11 + m._11);
}
extern public inline function sub(m: Matrix2): Matrix2 {
return new Matrix2(_00 - m._00, _10 - m._10, _01 - m._01, _11 - m._11);
}
extern public inline function mult(value: Float): Matrix2 {
return new Matrix2(_00 * value, _10 * value, _01 * value, _11 * value);
}
extern public inline function transpose(): Matrix2 {
return new Matrix2(_00, _01, _10, _11);
}
extern public inline function trace(): Float {
return _00 + _11;
}
extern public inline function multmat(m: Matrix2): Matrix2 {
return new Matrix2(_00 * m._00 + _10 * m._01, _00 * m._10 + _10 * m._11, _01 * m._00 + _11 * m._01, _01 * m._10 + _11 * m._11);
}
extern public inline function multvec(value: Vector2): Vector2 {
var x = _00 * value.x + _10 * value.y;
var y = _01 * value.x + _11 * value.y;
return new Vector2(x, y);
}
}

View File

@ -0,0 +1,173 @@
package kha.math;
class Matrix3 {
static inline var width: Int = 3;
static inline var height: Int = 3;
/** Horizontal scaling. A value of 1 results in no scaling. */
public var _00: FastFloat;
/** Horizontal skewing. */
public var _10: FastFloat;
/** Horizontal translation (moving). */
public var _20: FastFloat;
/** Vertical skewing. */
public var _01: FastFloat;
/** Vertical scaling. A value of 1 results in no scaling. */
public var _11: FastFloat;
/** Vertical translation (moving). */
public var _21: FastFloat;
public var _02: FastFloat;
public var _12: FastFloat;
public var _22: FastFloat;
public inline function new(_00: Float, _10: Float, _20: Float, _01: Float, _11: Float, _21: Float, _02: Float, _12: Float, _22: Float) {
this._00 = _00;
this._10 = _10;
this._20 = _20;
this._01 = _01;
this._11 = _11;
this._21 = _21;
this._02 = _02;
this._12 = _12;
this._22 = _22;
}
public static inline function fromFastMatrix3(m: FastMatrix3): Matrix3 {
return new Matrix3(m._00, m._10, m._20, m._01, m._11, m._21, m._02, m._12, m._22);
}
/*@:arrayAccess public inline function get(index: Int): Float {
return this[index];
}
@:arrayAccess public inline function set(index: Int, value: Float): Float {
this[index] = value;
return value;
}
public static function index(x: Int, y: Int): Int {
return y * width + x;
}*/
extern public inline function setFrom(m: Matrix3): Void {
this._00 = m._00;
this._10 = m._10;
this._20 = m._20;
this._01 = m._01;
this._11 = m._11;
this._21 = m._21;
this._02 = m._02;
this._12 = m._12;
this._22 = m._22;
}
extern public static inline function translation(x: Float, y: Float): Matrix3 {
return new Matrix3(1, 0, x, 0, 1, y, 0, 0, 1);
}
extern public static inline function empty(): Matrix3 {
return new Matrix3(0, 0, 0, 0, 0, 0, 0, 0, 0);
}
extern public static inline function identity(): Matrix3 {
return new Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1);
}
extern public static inline function scale(x: Float, y: Float): Matrix3 {
return new Matrix3(x, 0, 0, 0, y, 0, 0, 0, 1);
}
extern public static inline function rotation(alpha: Float): Matrix3 {
return new Matrix3(Math.cos(alpha), -Math.sin(alpha), 0, Math.sin(alpha), Math.cos(alpha), 0, 0, 0, 1);
}
extern public inline function add(m: Matrix3): Matrix3 {
return new Matrix3(_00 + m._00, _10 + m._10, _20 + m._20, _01 + m._01, _11 + m._11, _21 + m._21, _02 + m._02, _12 + m._12, _22 + m._22);
}
extern public inline function sub(m: Matrix3): Matrix3 {
return new Matrix3(_00 - m._00, _10 - m._10, _20 - m._20, _01 - m._01, _11 - m._11, _21 - m._21, _02 - m._02, _12 - m._12, _22 - m._22);
}
extern public inline function mult(value: Float): Matrix3 {
return new Matrix3(_00 * value, _10 * value, _20 * value, _01 * value, _11 * value, _21 * value, _02 * value, _12 * value, _22 * value);
}
extern public inline function transpose(): Matrix3 {
return new Matrix3(_00, _01, _02, _10, _11, _12, _20, _21, _22);
}
extern public inline function trace(): Float {
return _00 + _11 + _22;
}
extern public inline function multmat(m: Matrix3): Matrix3 {
return new Matrix3(_00 * m._00
+ _10 * m._01
+ _20 * m._02, _00 * m._10
+ _10 * m._11
+ _20 * m._12, _00 * m._20
+ _10 * m._21
+ _20 * m._22,
_01 * m._00
+ _11 * m._01
+ _21 * m._02, _01 * m._10
+ _11 * m._11
+ _21 * m._12, _01 * m._20
+ _11 * m._21
+ _21 * m._22,
_02 * m._00
+ _12 * m._01
+ _22 * m._02, _02 * m._10
+ _12 * m._11
+ _22 * m._12, _02 * m._20
+ _12 * m._21
+ _22 * m._22);
}
extern public inline function multvec(value: Vector2): Vector2 {
// var product = new Vector2(0, 0);
var w = _02 * value.x + _12 * value.y + _22 * 1;
var x = (_00 * value.x + _10 * value.y + _20 * 1) / w;
var y = (_01 * value.x + _11 * value.y + _21 * 1) / w;
return new Vector2(x, y);
}
extern public inline function cofactor(m0: Float, m1: Float, m2: Float, m3: Float): Float {
return m0 * m3 - m1 * m2;
}
extern public inline function determinant(): Float {
var c00 = cofactor(_11, _21, _12, _22);
var c01 = cofactor(_10, _20, _12, _22);
var c02 = cofactor(_10, _20, _11, _21);
return _00 * c00 - _01 * c01 + _02 * c02;
}
extern public inline function inverse(): Matrix3 {
var c00 = cofactor(_11, _21, _12, _22);
var c01 = cofactor(_10, _20, _12, _22);
var c02 = cofactor(_10, _20, _11, _21);
var det: Float = _00 * c00 - _01 * c01 + _02 * c02;
if (Math.abs(det) < 0.000001) {
throw "determinant is too small";
}
var c10 = cofactor(_01, _21, _02, _22);
var c11 = cofactor(_00, _20, _02, _22);
var c12 = cofactor(_00, _20, _01, _21);
var c20 = cofactor(_01, _11, _02, _12);
var c21 = cofactor(_00, _10, _02, _12);
var c22 = cofactor(_00, _10, _01, _11);
var invdet: Float = 1.0 / det;
return new Matrix3(c00 * invdet, -c01 * invdet, c02 * invdet, -c10 * invdet, c11 * invdet, -c12 * invdet, c20 * invdet, -c21 * invdet, c22 * invdet);
}
}

View File

@ -0,0 +1,316 @@
package kha.math;
class Matrix4 {
static inline var width: Int = 4;
static inline var height: Int = 4;
public var _00: Float;
public var _10: Float;
public var _20: Float;
public var _30: Float;
public var _01: Float;
public var _11: Float;
public var _21: Float;
public var _31: Float;
public var _02: Float;
public var _12: Float;
public var _22: Float;
public var _32: Float;
public var _03: Float;
public var _13: Float;
public var _23: Float;
public var _33: Float;
public inline function new(_00: Float, _10: Float, _20: Float, _30: Float, _01: Float, _11: Float, _21: Float, _31: Float, _02: Float, _12: Float,
_22: Float, _32: Float, _03: Float, _13: Float, _23: Float, _33: Float) {
this._00 = _00;
this._10 = _10;
this._20 = _20;
this._30 = _30;
this._01 = _01;
this._11 = _11;
this._21 = _21;
this._31 = _31;
this._02 = _02;
this._12 = _12;
this._22 = _22;
this._32 = _32;
this._03 = _03;
this._13 = _13;
this._23 = _23;
this._33 = _33;
}
public static inline function fromFastMatrix4(m: FastMatrix4): Matrix4 {
return new Matrix4(m._00, m._10, m._20, m._30, m._01, m._11, m._21, m._31, m._02, m._12, m._22, m._32, m._03, m._13, m._23, m._33);
}
extern public inline function setFrom(m: Matrix4): Void {
this._00 = m._00;
this._10 = m._10;
this._20 = m._20;
this._30 = m._30;
this._01 = m._01;
this._11 = m._11;
this._21 = m._21;
this._31 = m._31;
this._02 = m._02;
this._12 = m._12;
this._22 = m._22;
this._32 = m._32;
this._03 = m._03;
this._13 = m._13;
this._23 = m._23;
this._33 = m._33;
}
extern public static inline function translation(x: Float, y: Float, z: Float): Matrix4 {
return new Matrix4(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1);
}
extern public static inline function empty(): Matrix4 {
return new Matrix4(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
extern public static inline function identity(): Matrix4 {
return new Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
}
extern public static inline function scale(x: Float, y: Float, z: Float): Matrix4 {
return new Matrix4(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
}
extern public static inline function rotationX(alpha: Float): Matrix4 {
var ca = Math.cos(alpha);
var sa = Math.sin(alpha);
return new Matrix4(1, 0, 0, 0, 0, ca, -sa, 0, 0, sa, ca, 0, 0, 0, 0, 1);
}
extern public static inline function rotationY(alpha: Float): Matrix4 {
var ca = Math.cos(alpha);
var sa = Math.sin(alpha);
return new Matrix4(ca, 0, sa, 0, 0, 1, 0, 0, -sa, 0, ca, 0, 0, 0, 0, 1);
}
extern public static inline function rotationZ(alpha: Float): Matrix4 {
var ca = Math.cos(alpha);
var sa = Math.sin(alpha);
return new Matrix4(ca, -sa, 0, 0, sa, ca, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
}
extern public static inline function rotation(yaw: Float, pitch: Float, roll: Float): Matrix4 {
var sy = Math.sin(yaw);
var cy = Math.cos(yaw);
var sx = Math.sin(pitch);
var cx = Math.cos(pitch);
var sz = Math.sin(roll);
var cz = Math.cos(roll);
return new Matrix4(cx * cy, cx * sy * sz
- sx * cz, cx * sy * cz
+ sx * sz, 0, sx * cy, sx * sy * sz
+ cx * cz, sx * sy * cz
- cx * sz, 0,
-sy,
cy * sz, cy * cz, 0, 0, 0, 0, 1);
}
// Inlining this leads to weird error in C#, please investigate
public static function orthogonalProjection(left: Float, right: Float, bottom: Float, top: Float, zn: Float, zf: Float): Matrix4 {
var tx: Float = -(right + left) / (right - left);
var ty: Float = -(top + bottom) / (top - bottom);
var tz: Float = -(zf + zn) / (zf - zn);
return new Matrix4(2 / (right - left), 0, 0, tx, 0, 2 / (top - bottom), 0, ty, 0, 0, -2 / (zf - zn), tz, 0, 0, 0, 1);
}
public static function perspectiveProjection(fovY: Float, aspect: Float, zn: Float, zf: Float): Matrix4 {
var uh = 1.0 / Math.tan(fovY / 2);
var uw = uh / aspect;
return new Matrix4(uw, 0, 0, 0, 0, uh, 0, 0, 0, 0, (zf + zn) / (zn - zf), 2 * zf * zn / (zn - zf), 0, 0, -1, 0);
}
public static function lookAt(eye: Vector3, at: Vector3, up: Vector3): Matrix4 {
var zaxis = at.sub(eye).normalized();
var xaxis = zaxis.cross(up).normalized();
var yaxis = xaxis.cross(zaxis);
return new Matrix4(xaxis.x, xaxis.y, xaxis.z, -xaxis.dot(eye), yaxis.x, yaxis.y, yaxis.z, -yaxis.dot(eye), -zaxis.x, -zaxis.y, -zaxis.z,
zaxis.dot(eye), 0, 0, 0, 1);
}
extern public inline function add(m: Matrix4): Matrix4 {
return new Matrix4(_00
+ m._00, _10
+ m._10, _20
+ m._20, _30
+ m._30, _01
+ m._01, _11
+ m._11, _21
+ m._21, _31
+ m._31, _02
+ m._02, _12
+ m._12,
_22
+ m._22, _32
+ m._32, _03
+ m._03, _13
+ m._13, _23
+ m._23, _33
+ m._33);
}
extern public inline function sub(m: Matrix4): Matrix4 {
return new Matrix4(_00
- m._00, _10
- m._10, _20
- m._20, _30
- m._30, _01
- m._01, _11
- m._11, _21
- m._21, _31
- m._31, _02
- m._02, _12
- m._12,
_22
- m._22, _32
- m._32, _03
- m._03, _13
- m._13, _23
- m._23, _33
- m._33);
}
extern public inline function mult(value: Float): Matrix4 {
return new Matrix4(_00 * value, _10 * value, _20 * value, _30 * value, _01 * value, _11 * value, _21 * value, _31 * value, _02 * value, _12 * value,
_22 * value, _32 * value, _03 * value, _13 * value, _23 * value, _33 * value);
}
extern public inline function transpose(): Matrix4 {
return new Matrix4(_00, _01, _02, _03, _10, _11, _12, _13, _20, _21, _22, _23, _30, _31, _32, _33);
}
extern public inline function transpose3x3(): Matrix4 {
return new Matrix4(_00, _01, _02, _30, _10, _11, _12, _31, _20, _21, _22, _32, _03, _13, _23, _33);
}
extern public inline function trace(): Float {
return _00 + _11 + _22 + _33;
}
extern public inline function multmat(m: Matrix4): Matrix4 {
return new Matrix4(_00 * m._00
+ _10 * m._01
+ _20 * m._02
+ _30 * m._03, _00 * m._10
+ _10 * m._11
+ _20 * m._12
+ _30 * m._13,
_00 * m._20
+ _10 * m._21
+ _20 * m._22
+ _30 * m._23, _00 * m._30
+ _10 * m._31
+ _20 * m._32
+ _30 * m._33,
_01 * m._00
+ _11 * m._01
+ _21 * m._02
+ _31 * m._03, _01 * m._10
+ _11 * m._11
+ _21 * m._12
+ _31 * m._13,
_01 * m._20
+ _11 * m._21
+ _21 * m._22
+ _31 * m._23, _01 * m._30
+ _11 * m._31
+ _21 * m._32
+ _31 * m._33,
_02 * m._00
+ _12 * m._01
+ _22 * m._02
+ _32 * m._03, _02 * m._10
+ _12 * m._11
+ _22 * m._12
+ _32 * m._13,
_02 * m._20
+ _12 * m._21
+ _22 * m._22
+ _32 * m._23, _02 * m._30
+ _12 * m._31
+ _22 * m._32
+ _32 * m._33,
_03 * m._00
+ _13 * m._01
+ _23 * m._02
+ _33 * m._03, _03 * m._10
+ _13 * m._11
+ _23 * m._12
+ _33 * m._13,
_03 * m._20
+ _13 * m._21
+ _23 * m._22
+ _33 * m._23, _03 * m._30
+ _13 * m._31
+ _23 * m._32
+ _33 * m._33);
}
extern public inline function multvec(value: Vector4): Vector4 {
var product = new Vector4();
product.x = _00 * value.x + _10 * value.y + _20 * value.z + _30 * value.w;
product.y = _01 * value.x + _11 * value.y + _21 * value.z + _31 * value.w;
product.z = _02 * value.x + _12 * value.y + _22 * value.z + _32 * value.w;
product.w = _03 * value.x + _13 * value.y + _23 * value.z + _33 * value.w;
return product;
}
extern public inline function cofactor(m0: Float, m1: Float, m2: Float, m3: Float, m4: Float, m5: Float, m6: Float, m7: Float, m8: Float): Float {
return m0 * (m4 * m8 - m5 * m7) - m1 * (m3 * m8 - m5 * m6) + m2 * (m3 * m7 - m4 * m6);
}
extern public inline function determinant(): Float {
var c00 = cofactor(_11, _21, _31, _12, _22, _32, _13, _23, _33);
var c01 = cofactor(_10, _20, _30, _12, _22, _32, _13, _23, _33);
var c02 = cofactor(_10, _20, _30, _11, _21, _31, _13, _23, _33);
var c03 = cofactor(_10, _20, _30, _11, _21, _31, _12, _22, _32);
return _00 * c00 - _01 * c01 + _02 * c02 - _03 * c03;
}
extern public inline function inverse(): Matrix4 {
var c00 = cofactor(_11, _21, _31, _12, _22, _32, _13, _23, _33);
var c01 = cofactor(_10, _20, _30, _12, _22, _32, _13, _23, _33);
var c02 = cofactor(_10, _20, _30, _11, _21, _31, _13, _23, _33);
var c03 = cofactor(_10, _20, _30, _11, _21, _31, _12, _22, _32);
var det: Float = _00 * c00 - _01 * c01 + _02 * c02 - _03 * c03;
if (Math.abs(det) < 0.000001) {
throw "determinant is too small";
}
var c10 = cofactor(_01, _21, _31, _02, _22, _32, _03, _23, _33);
var c11 = cofactor(_00, _20, _30, _02, _22, _32, _03, _23, _33);
var c12 = cofactor(_00, _20, _30, _01, _21, _31, _03, _23, _33);
var c13 = cofactor(_00, _20, _30, _01, _21, _31, _02, _22, _32);
var c20 = cofactor(_01, _11, _31, _02, _12, _32, _03, _13, _33);
var c21 = cofactor(_00, _10, _30, _02, _12, _32, _03, _13, _33);
var c22 = cofactor(_00, _10, _30, _01, _11, _31, _03, _13, _33);
var c23 = cofactor(_00, _10, _30, _01, _11, _31, _02, _12, _32);
var c30 = cofactor(_01, _11, _21, _02, _12, _22, _03, _13, _23);
var c31 = cofactor(_00, _10, _20, _02, _12, _22, _03, _13, _23);
var c32 = cofactor(_00, _10, _20, _01, _11, _21, _03, _13, _23);
var c33 = cofactor(_00, _10, _20, _01, _11, _21, _02, _12, _22);
var invdet: Float = 1.0 / det;
return new Matrix4(c00 * invdet,
-c01 * invdet, c02 * invdet,
-c03 * invdet,
-c10 * invdet, c11 * invdet,
-c12 * invdet, c13 * invdet, c20 * invdet,
-c21 * invdet, c22 * invdet,
-c23 * invdet,
-c30 * invdet, c31 * invdet,
-c32 * invdet, c33 * invdet);
}
}

View File

@ -0,0 +1,255 @@
package kha.math;
import kha.math.Vector3;
import kha.math.Matrix4;
@:structInit
class Quaternion {
var values: Array<Float>;
public inline function new(x: Float = 0, y: Float = 0, z: Float = 0, w: Float = 1): Void {
values = new Array<Float>();
values.push(x);
values.push(y);
values.push(z);
values.push(w);
}
// Axis has to be normalized
public inline static function fromAxisAngle(axis: Vector3, radians: Float): Quaternion {
var q: Quaternion = new Quaternion();
q.w = Math.cos(radians / 2.0);
q.x = q.y = q.z = Math.sin(radians / 2.0);
q.x *= axis.x;
q.y *= axis.y;
q.z *= axis.z;
return q;
}
public function slerp(t: Float, q: Quaternion) {
var epsilon: Float = 0.0005;
var dot = dot(q);
if (dot > 1 - epsilon) {
var result: Quaternion = q.add((this.sub(q)).scaled(t));
result.normalize();
return result;
}
if (dot < 0)
dot = 0;
if (dot > 1)
dot = 1;
var theta0: Float = Math.acos(dot);
var theta: Float = theta0 * t;
var q2: Quaternion = q.sub(scaled(dot));
q2.normalize();
var result: Quaternion = scaled(Math.cos(theta)).add(q2.scaled(Math.sin(theta)));
result.normalize();
return result;
}
// TODO: This should be multiplication
public inline function rotated(b: Quaternion): Quaternion {
var q: Quaternion = new Quaternion();
q.w = w * b.w - x * b.x - y * b.y - z * b.z;
q.x = w * b.x + x * b.w + y * b.z - z * b.y;
q.y = w * b.y + y * b.w + z * b.x - x * b.z;
q.z = w * b.z + z * b.w + x * b.y - y * b.x;
q.normalize();
return q;
}
public inline function scaled(scale: Float): Quaternion {
return new Quaternion(x * scale, y * scale, z * scale, w * scale);
}
public inline function scale(scale: Float) {
x = x * scale;
y = y * scale;
z = z * scale;
w = w * scale;
}
public inline function matrix(): Matrix4 {
var s: Float = 2.0;
var xs: Float = x * s;
var ys: Float = y * s;
var zs: Float = z * s;
var wx: Float = w * xs;
var wy: Float = w * ys;
var wz: Float = w * zs;
var xx: Float = x * xs;
var xy: Float = x * ys;
var xz: Float = x * zs;
var yy: Float = y * ys;
var yz: Float = y * zs;
var zz: Float = z * zs;
return new Matrix4(1 - (yy + zz), xy - wz, xz + wy, 0, xy + wz, 1 - (xx + zz), yz - wx, 0, xz - wy, yz + wx, 1 - (xx + yy), 0, 0, 0, 0, 1);
}
public inline function get(index: Int): Float {
return values[index];
}
public inline function set(index: Int, value: Float): Void {
values[index] = value;
}
public var x(get, set): Float;
public var y(get, set): Float;
public var z(get, set): Float;
public var w(get, set): Float;
public var length(get, set): Float;
function get_x(): Float {
return values[0];
}
function set_x(value: Float): Float {
return values[0] = value;
}
function get_y(): Float {
return values[1];
}
function set_y(value: Float): Float {
return values[1] = value;
}
function get_z(): Float {
return values[2];
}
function set_z(value: Float): Float {
return values[2] = value;
}
function get_w(): Float {
return values[3];
}
function set_w(value: Float): Float {
return values[3] = value;
}
// TODO: Isn't this code wrong? Is wrong in Vector4 for sure! (Missing w in the length)
function get_length(): Float {
return Math.sqrt(x * x + y * y + z * z + w * w);
}
function set_length(length: Float): Float {
if (get_length() == 0)
return 0;
var mul = length / get_length();
x *= mul;
y *= mul;
z *= mul;
return length;
}
// For adding a (scaled) axis-angle representation of a quaternion
public inline function addVector(vec: Vector3): Quaternion {
var result: Quaternion = new Quaternion(x, y, z, w);
var q1: Quaternion = new Quaternion(0, vec.x, vec.y, vec.z);
q1 = q1.mult(result);
result.x += q1.x * 0.5;
result.y += q1.y * 0.5;
result.z += q1.z * 0.5;
result.w += q1.w * 0.5;
return result;
}
public inline function add(q: Quaternion): Quaternion {
return new Quaternion(x + q.x, y + q.y, z + q.z, w + q.w);
}
public inline function sub(q: Quaternion): Quaternion {
return new Quaternion(x - q.x, y - q.y, z - q.z, w - q.w);
}
// TODO: Check again, but I think the code in Kore is wrong
public inline function mult(r: Quaternion): Quaternion {
var q: Quaternion = new Quaternion();
q.x = w * r.x + x * r.w + y * r.z - z * r.y;
q.y = w * r.y - x * r.z + y * r.w + z * r.x;
q.z = w * r.z + x * r.y - y * r.x + z * r.w;
q.w = w * r.w - x * r.x - y * r.y - z * r.z;
return q;
}
public inline function normalize() {
scale(1.0 / length);
}
public inline function dot(q: Quaternion) {
return x * q.x + y * q.y + z * q.z + w * q.w;
}
// GetEulerAngles extracts Euler angles from the quaternion, in the specified order of
// axis rotations and the specified coordinate system. Right-handed coordinate system
// is the default, with CCW rotations while looking in the negative axis direction.
// Here a,b,c, are the Yaw/Pitch/Roll angles to be returned.
// rotation a around axis A1
// is followed by rotation b around axis A2
// is followed by rotation c around axis A3
// rotations are CCW or CW (D) in LH or RH coordinate system (S)
public static inline var AXIS_X: Int = 0;
public static inline var AXIS_Y: Int = 1;
public static inline var AXIS_Z: Int = 2;
public function getEulerAngles(A1: Int, A2: Int, A3: Int, S: Int = 1, D: Int = 1): Vector3 {
var result: Vector3 = new Vector3();
var Q: Array<Float> = new Array<Float>();
Q[0] = x;
Q[1] = y;
Q[2] = z;
var ww: Float = w * w;
var Q11: Float = Q[A1] * Q[A1];
var Q22: Float = Q[A2] * Q[A2];
var Q33: Float = Q[A3] * Q[A3];
var psign: Float = -1;
var SingularityRadius: Float = 0.0000001;
var PiOver2: Float = Math.PI / 2.0;
// Determine whether even permutation
if (((A1 + 1) % 3 == A2) && ((A2 + 1) % 3 == A3)) {
psign = 1;
}
var s2: Float = psign * 2.0 * (psign * w * Q[A2] + Q[A1] * Q[A3]);
if (s2 < -1 + SingularityRadius) { // South pole singularity
result.x = 0;
result.y = -S * D * PiOver2;
result.z = S * D * Math.atan2(2 * (psign * Q[A1] * Q[A2] + w * Q[A3]), ww + Q22 - Q11 - Q33);
}
else if (s2 > 1 - SingularityRadius) { // North pole singularity
result.x = 0;
result.y = S * D * PiOver2;
result.z = S * D * Math.atan2(2 * (psign * Q[A1] * Q[A2] + w * Q[A3]), ww + Q22 - Q11 - Q33);
}
else {
result.x = -S * D * Math.atan2(-2 * (w * Q[A1] - psign * Q[A2] * Q[A3]), ww + Q33 - Q11 - Q22);
result.y = S * D * Math.asin(s2);
result.z = S * D * Math.atan2(2 * (w * Q[A3] - psign * Q[A1] * Q[A2]), ww + Q11 - Q22 - Q33);
}
return result;
}
}

View File

@ -0,0 +1,82 @@
package kha.math;
// Small Fast Counter 32 (sfc32) PRNG implementation
// sfc32 is public domain: http://pracrand.sourceforge.net/license.txt
// Implementation derived from: https://github.com/bryc/code/blob/master/jshash/PRNGs.md
// which is also public domain
/**
Random number generator
Please use this one instead of the native Haxe one to
keep consistency between different platforms.
**/
class Random {
var a: Int;
var b: Int;
var c: Int;
var d: Int;
public function new(seed: Int): Void {
d = seed;
a = 0x36aef51a;
b = 0x21d4b3eb;
c = 0xf2517abf;
// Immediately skip a few possibly poor results the easy way
for (i in 0...15) {
this.Get();
}
}
public function Get(): Int {
var t = (a + b | 0) + d | 0;
d = d + 1 | 0;
a = b ^ b >>> 9;
b = c + (c << 3) | 0;
c = c << 21 | c >>> 11;
c = c + t | 0;
return t & 0x7fffffff;
}
public function GetFloat(): Float {
return Get() / 0x7fffffff;
}
public function GetUpTo(max: Int): Int {
return Get() % (max + 1);
}
public function GetIn(min: Int, max: Int): Int {
return Get() % (max + 1 - min) + min;
}
public function GetFloatIn(min: Float, max: Float): Float {
return min + GetFloat() * (max - min);
}
public static var Default: Random;
public static function init(seed: Int): Void {
Default = new Random(seed);
}
public static function get(): Int {
return Default.Get();
}
public static function getFloat(): Float {
return Default.GetFloat();
}
public static function getUpTo(max: Int): Int {
return Default.GetUpTo(max);
}
public static function getIn(min: Int, max: Int): Int {
return Default.GetIn(min, max);
}
public static function getFloatIn(min: Float, max: Float): Float {
return min + Default.GetFloat() * (max - min);
}
}

View File

@ -0,0 +1,71 @@
package kha.math;
@:structInit
class Vector2 {
public inline function new(x: Float = 0, y: Float = 0): Void {
this.x = x;
this.y = y;
}
public var x: Float;
public var y: Float;
public var length(get, set): Float;
extern public inline function setFrom(v: Vector2): Void {
this.x = v.x;
this.y = v.y;
}
inline function get_length(): Float {
return Math.sqrt(x * x + y * y);
}
function set_length(length: Float): Float {
var currentLength = get_length();
if (currentLength == 0)
return 0;
var mul = length / currentLength;
x *= mul;
y *= mul;
return length;
}
extern public inline function add(vec: Vector2): Vector2 {
return new Vector2(x + vec.x, y + vec.y);
}
extern public inline function sub(vec: Vector2): Vector2 {
return new Vector2(x - vec.x, y - vec.y);
}
extern public inline function mult(value: Float): Vector2 {
return new Vector2(x * value, y * value);
}
extern public inline function div(value: Float): Vector2 {
return mult(1 / value);
}
extern public inline function dot(v: Vector2): Float {
return x * v.x + y * v.y;
}
@:deprecated("normalize() will be deprecated soon, use the immutable normalized() instead")
extern public inline function normalize(): Void {
#if haxe4 inline #end set_length(1);
}
extern public inline function normalized(): Vector2 {
var v = new Vector2(x, y);
#if haxe4 inline #end v.set_length(1);
return v;
}
extern public inline function angle(v: Vector2): Float {
return Math.atan2(x * v.y - y * v.x, x * v.x + y * v.y);
}
extern public inline function fast(): FastVector2 {
return new FastVector2(x, y);
}
}

View File

@ -0,0 +1,37 @@
package kha.math;
@:structInit
class Vector2i {
public inline function new(x: Int = 0, y: Int = 0): Void {
this.x = x;
this.y = y;
}
public var x: Int;
public var y: Int;
extern public inline function setFrom(v: Vector2i): Void {
this.x = v.x;
this.y = v.y;
}
extern public inline function add(vec: Vector2i): Vector2i {
return new Vector2i(x + vec.x, y + vec.y);
}
extern public inline function sub(vec: Vector2i): Vector2i {
return new Vector2i(x - vec.x, y - vec.y);
}
extern public inline function mult(value: Int): Vector2i {
return new Vector2i(x * value, y * value);
}
extern public inline function div(value: Int): Vector2i {
return new Vector2i(Std.int(x / value), Std.int(y / value));
}
extern public inline function dot(v: Vector2i): Float {
return x * v.x + y * v.y;
}
}

View File

@ -0,0 +1,74 @@
package kha.math;
@:structInit
class Vector3 {
public inline function new(x: Float = 0, y: Float = 0, z: Float = 0): Void {
this.x = x;
this.y = y;
this.z = z;
}
public var x: Float;
public var y: Float;
public var z: Float;
public var length(get, set): Float;
extern public inline function setFrom(v: Vector3): Void {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
inline function get_length(): Float {
return Math.sqrt(x * x + y * y + z * z);
}
function set_length(length: Float): Float {
var currentLength = get_length();
if (currentLength == 0)
return 0;
var mul = length / currentLength;
x *= mul;
y *= mul;
z *= mul;
return length;
}
extern public inline function add(vec: Vector3): Vector3 {
return new Vector3(x + vec.x, y + vec.y, z + vec.z);
}
extern public inline function sub(vec: Vector3): Vector3 {
return new Vector3(x - vec.x, y - vec.y, z - vec.z);
}
extern public inline function mult(value: Float): Vector3 {
return new Vector3(x * value, y * value, z * value);
}
extern public inline function dot(v: Vector3): Float {
return x * v.x + y * v.y + z * v.z;
}
extern public inline function cross(v: Vector3): Vector3 {
var _x = y * v.z - z * v.y;
var _y = z * v.x - x * v.z;
var _z = x * v.y - y * v.x;
return new Vector3(_x, _y, _z);
}
@:deprecated("normalize() will be deprecated soon, use the immutable normalized() instead")
extern public inline function normalize(): Void {
#if haxe4 inline #end set_length(1);
}
extern public inline function normalized(): Vector3 {
var v = new Vector3(x, y, z);
#if haxe4 inline #end v.set_length(1);
return v;
}
extern public inline function fast(): FastVector3 {
return new FastVector3(x, y, z);
}
}

View File

@ -0,0 +1,67 @@
package kha.math;
@:structInit
class Vector4 {
public inline function new(x: Float = 0, y: Float = 0, z: Float = 0, w: Float = 1): Void {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public var x: Float;
public var y: Float;
public var z: Float;
public var w: Float;
public var length(get, set): Float;
extern public inline function setFrom(v: Vector4): Void {
this.x = v.x;
this.y = v.y;
this.z = v.z;
this.w = v.w;
}
inline function get_length(): Float {
return Math.sqrt(x * x + y * y + z * z + w * w);
}
function set_length(length: Float): Float {
var currentLength = get_length();
if (currentLength == 0)
return 0;
var mul = length / currentLength;
x *= mul;
y *= mul;
z *= mul;
w *= mul;
return length;
}
extern public inline function add(vec: Vector4): Vector4 {
return new Vector4(x + vec.x, y + vec.y, z + vec.z, w + vec.w);
}
extern public inline function sub(vec: Vector4): Vector4 {
return new Vector4(x - vec.x, y - vec.y, z - vec.z, w - vec.w);
}
extern public inline function mult(value: Float): Vector4 {
return new Vector4(x * value, y * value, z * value, w * value);
}
@:deprecated("Use the immutable normalized() instead")
extern public inline function normalize(): Void {
#if haxe4 inline #end set_length(1);
}
extern public inline function normalized(): Vector4 {
var v = new Vector4(x, y, z, w);
#if haxe4 inline #end v.set_length(1);
return v;
}
extern public inline function fast(): FastVector4 {
return new FastVector4(x, y, z, w);
}
}