From 9a23f6fad9820554c47aeaf8e9c415facfa09fa1 Mon Sep 17 00:00:00 2001 From: fabmax Date: Tue, 26 Dec 2023 11:14:33 +0100 Subject: [PATCH] added destructuring and swizzle functions --- .../kotlin/de/fabmax/kool/math/Mat3.kt | 8 +++ .../kotlin/de/fabmax/kool/math/Mat4.kt | 10 ++++ .../kotlin/de/fabmax/kool/math/Quat.kt | 10 ++++ .../kotlin/de/fabmax/kool/math/Vec2.kt | 9 +++ .../kotlin/de/fabmax/kool/math/Vec3.kt | 30 ++++++++++ .../kotlin/de/fabmax/kool/math/Vec4.kt | 60 +++++++++++++++---- 6 files changed, 115 insertions(+), 12 deletions(-) diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat3.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat3.kt index 1ff59e830..c2ae13df5 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat3.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat3.kt @@ -82,6 +82,10 @@ open class Mat3f( col0.z, col1.z, col2.z ) + operator fun component1(): Vec3f = Vec3f(m00, m10, m20) + operator fun component2(): Vec3f = Vec3f(m01, m11, m21) + operator fun component3(): Vec3f = Vec3f(m02, m12, m22) + operator fun times(that: Mat3f): MutableMat3f = mul(that, MutableMat3f()) operator fun plus(that: Mat3f): MutableMat3f = add(that, MutableMat3f()) @@ -960,6 +964,10 @@ open class Mat3d( col0.z, col1.z, col2.z ) + operator fun component1(): Vec3d = Vec3d(m00, m10, m20) + operator fun component2(): Vec3d = Vec3d(m01, m11, m21) + operator fun component3(): Vec3d = Vec3d(m02, m12, m22) + operator fun times(that: Mat3d): MutableMat3d = mul(that, MutableMat3d()) operator fun plus(that: Mat3d): MutableMat3d = add(that, MutableMat3d()) diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat4.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat4.kt index cf57346e7..13f1d38ba 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat4.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat4.kt @@ -120,6 +120,11 @@ open class Mat4f( col0.w, col1.w, col2.w, col3.w ) + operator fun component1(): Vec4f = Vec4f(m00, m10, m20, m30) + operator fun component2(): Vec4f = Vec4f(m01, m11, m21, m31) + operator fun component3(): Vec4f = Vec4f(m02, m12, m22, m32) + operator fun component4(): Vec4f = Vec4f(m03, m13, m23, m33) + operator fun times(that: Mat4f): MutableMat4f = mul(that, MutableMat4f()) operator fun plus(that: Mat4f): MutableMat4f = add(that, MutableMat4f()) @@ -1402,6 +1407,11 @@ open class Mat4d( col0.w, col1.w, col2.w, col3.w ) + operator fun component1(): Vec4d = Vec4d(m00, m10, m20, m30) + operator fun component2(): Vec4d = Vec4d(m01, m11, m21, m31) + operator fun component3(): Vec4d = Vec4d(m02, m12, m22, m32) + operator fun component4(): Vec4d = Vec4d(m03, m13, m23, m33) + operator fun times(that: Mat4d): MutableMat4d = mul(that, MutableMat4d()) operator fun plus(that: Mat4d): MutableMat4d = add(that, MutableMat4d()) diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Quat.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Quat.kt index 0ba96d821..d9f92d4db 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Quat.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Quat.kt @@ -22,6 +22,11 @@ open class QuatF(open val x: Float, open val y: Float, open val z: Float, open v constructor(q: QuatF) : this(q.x, q.y, q.z, q.w) + operator fun component1(): Float = x + operator fun component2(): Float = y + operator fun component3(): Float = z + operator fun component4(): Float = w + /** * Component-wise addition with the given [QuatF]. Returns the result as a new [QuatF]. Consider using [add] with * a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations. @@ -313,6 +318,11 @@ open class QuatD(open val x: Double, open val y: Double, open val z: Double, ope constructor(q: QuatD) : this(q.x, q.y, q.z, q.w) + operator fun component1(): Double = x + operator fun component2(): Double = y + operator fun component3(): Double = z + operator fun component4(): Double = w + /** * Component-wise addition with the given [QuatD]. Returns the result as a new [QuatD]. Consider using [add] with * a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations. diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec2.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec2.kt index a00cc96b8..b7ee60117 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec2.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec2.kt @@ -33,6 +33,9 @@ open class Vec2f(open val x: Float, open val y: Float) { constructor(f: Float): this(f, f) constructor(v: Vec2f): this(v.x, v.y) + operator fun component1(): Float = x + operator fun component2(): Float = y + /** * Component-wise addition with the given [Vec2f]. Returns the result as a new [Vec2f]. Consider using [add] with * a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations. @@ -372,6 +375,9 @@ open class Vec2d(open val x: Double, open val y: Double) { constructor(f: Double): this(f, f) constructor(v: Vec2d): this(v.x, v.y) + operator fun component1(): Double = x + operator fun component2(): Double = y + /** * Component-wise addition with the given [Vec2d]. Returns the result as a new [Vec2d]. Consider using [add] with * a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations. @@ -702,6 +708,9 @@ open class Vec2i(open val x: Int, open val y: Int) { constructor(f: Int): this(f, f) constructor(v: Vec2i): this(v.x, v.y) + operator fun component1(): Int = x + operator fun component2(): Int = y + /** * Component-wise addition with the given [Vec2i]. Returns the result as a new [Vec2i]. Consider using [add] with * a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations. diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec3.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec3.kt index c6e218fb5..48a34ea25 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec3.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec3.kt @@ -28,11 +28,21 @@ fun MutableVec3i.set(that: Vec3d) = set(that.x.toInt(), that.y.toInt(), that.z.t // End of template section, DO NOT EDIT BELOW THIS! +fun Vec3d(xy: Vec2d, z: Double): Vec3d = Vec3d(xy.x, xy.y, z) +fun Vec3d(x: Double, yz: Vec2d): Vec3d = Vec3d(x, yz.x, yz.y) + open class Vec3d(open val x: Double, open val y: Double, open val z: Double) { constructor(f: Double): this(f, f, f) constructor(v: Vec3d): this(v.x, v.y, v.z) + val xy: Vec2d get() = Vec2d(x, y) + val yz: Vec2d get() = Vec2d(y, z) + + operator fun component1(): Double = x + operator fun component2(): Double = y + operator fun component3(): Double = z + /** * Component-wise addition with the given [Vec3d]. Returns the result as a new [Vec3d]. Consider using [add] with * a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations. @@ -807,11 +827,21 @@ open class MutableVec3d(override var x: Double, override var y: Double, override } +fun Vec3i(xy: Vec2i, z: Int): Vec3i = Vec3i(xy.x, xy.y, z) +fun Vec3i(x: Int, yz: Vec2i): Vec3i = Vec3i(x, yz.x, yz.y) + open class Vec3i(open val x: Int, open val y: Int, open val z: Int) { constructor(f: Int): this(f, f, f) constructor(v: Vec3i): this(v.x, v.y, v.z) + val xy: Vec2i get() = Vec2i(x, y) + val yz: Vec2i get() = Vec2i(y, z) + + operator fun component1(): Int = x + operator fun component2(): Int = y + operator fun component3(): Int = z + /** * Component-wise addition with the given [Vec3i]. Returns the result as a new [Vec3i]. Consider using [add] with * a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations. diff --git a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec4.kt b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec4.kt index 5f1c9dced..ae5adf89a 100644 --- a/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec4.kt +++ b/kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec4.kt @@ -28,15 +28,27 @@ fun MutableVec4i.set(that: Vec4d) = set(that.x.toInt(), that.y.toInt(), that.z.t // End of template section, DO NOT EDIT BELOW THIS! -open class Vec4d(open val x: Double, open val y: Double, open val z: Double, open val w: Double) { +fun Vec4d(xyz: Vec3d, w: Double): Vec4d = Vec4d(xyz.x, xyz.y, xyz.z, w) +fun Vec4d(x: Double, yzw: Vec3d): Vec4d = Vec4d(x, yzw.x, yzw.y, yzw.z) +fun Vec4d(xy: Vec2d, zw: Vec2d): Vec4d = Vec4d(xy.x, xy.y, zw.x, zw.y) +fun Vec4d(x: Double, yz: Vec2d, w: Double): Vec4d = Vec4d(x, yz.x, yz.y, w) - val xyz: Vec3d - get() = Vec3d(x, y, z) +open class Vec4d(open val x: Double, open val y: Double, open val z: Double, open val w: Double) { constructor(f: Double): this(f, f, f, f) - constructor(xyz: Vec3d, w: Double): this(xyz.x, xyz.y, xyz.z, w) constructor(v: Vec4d): this(v.x, v.y, v.z, v.w) + val xyz: Vec3d get() = Vec3d(x, y, z) + val yzw: Vec3d get() = Vec3d(y, z, w) + val xy: Vec2d get() = Vec2d(x, y) + val yz: Vec2d get() = Vec2d(y, z) + val zw: Vec2d get() = Vec2d(z, w) + + operator fun component1(): Double = x + operator fun component2(): Double = y + operator fun component3(): Double = z + operator fun component4(): Double = w + /** * Component-wise addition with the given [Vec4d]. Returns the result as a new [Vec4d]. Consider using [add] with * a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations. @@ -755,15 +779,27 @@ open class MutableVec4d(override var x: Double, override var y: Double, override } -open class Vec4i(open val x: Int, open val y: Int, open val z: Int, open val w: Int) { +fun Vec4i(xyz: Vec3i, w: Int): Vec4i = Vec4i(xyz.x, xyz.y, xyz.z, w) +fun Vec4i(x: Int, yzw: Vec3i): Vec4i = Vec4i(x, yzw.x, yzw.y, yzw.z) +fun Vec4i(xy: Vec2i, zw: Vec2i): Vec4i = Vec4i(xy.x, xy.y, zw.x, zw.y) +fun Vec4i(x: Int, yz: Vec2i, w: Int): Vec4i = Vec4i(x, yz.x, yz.y, w) - val xyz: Vec3i - get() = Vec3i(x, y, z) +open class Vec4i(open val x: Int, open val y: Int, open val z: Int, open val w: Int) { constructor(f: Int): this(f, f, f, f) - constructor(xyz: Vec3i, w: Int): this(xyz.x, xyz.y, xyz.z, w) constructor(v: Vec4i): this(v.x, v.y, v.z, v.w) + val xyz: Vec3i get() = Vec3i(x, y, z) + val yzw: Vec3i get() = Vec3i(y, z, w) + val xy: Vec2i get() = Vec2i(x, y) + val yz: Vec2i get() = Vec2i(y, z) + val zw: Vec2i get() = Vec2i(z, w) + + operator fun component1(): Int = x + operator fun component2(): Int = y + operator fun component3(): Int = z + operator fun component4(): Int = w + /** * Component-wise addition with the given [Vec4i]. Returns the result as a new [Vec4i]. Consider using [add] with * a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations.