Skip to content

Commit

Permalink
added destructuring and swizzle functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabmax committed Dec 26, 2023
1 parent c007b6c commit 9a23f6f
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 12 deletions.
8 changes: 8 additions & 0 deletions kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
10 changes: 10 additions & 0 deletions kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Mat4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
10 changes: 10 additions & 0 deletions kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Quat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@ fun MutableVec3i.set(that: Vec3d) = set(that.x.toInt(), that.y.toInt(), that.z.t

// <template> Changes made within the template section will also affect the other type variants of this class

fun Vec3f(xy: Vec2f, z: Float): Vec3f = Vec3f(xy.x, xy.y, z)
fun Vec3f(x: Float, yz: Vec2f): Vec3f = Vec3f(x, yz.x, yz.y)

open class Vec3f(open val x: Float, open val y: Float, open val z: Float) {

constructor(f: Float): this(f, f, f)
constructor(v: Vec3f): this(v.x, v.y, v.z)

val xy: Vec2f get() = Vec2f(x, y)
val yz: Vec2f get() = Vec2f(y, z)

operator fun component1(): Float = x
operator fun component2(): Float = y
operator fun component3(): Float = z

/**
* Component-wise addition with the given [Vec3f]. Returns the result as a new [Vec3f]. Consider using [add] with
* a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations.
Expand Down Expand Up @@ -422,11 +432,21 @@ open class MutableVec3f(override var x: Float, override var y: Float, override v
// </template> 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.
Expand Down Expand Up @@ -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.
Expand Down
60 changes: 48 additions & 12 deletions kool-core/src/commonMain/kotlin/de/fabmax/kool/math/Vec4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,27 @@ fun MutableVec4i.set(that: Vec4d) = set(that.x.toInt(), that.y.toInt(), that.z.t

// <template> Changes made within the template section will also affect the other type variants of this class

open class Vec4f(open val x: Float, open val y: Float, open val z: Float, open val w: Float) {
fun Vec4f(xyz: Vec3f, w: Float): Vec4f = Vec4f(xyz.x, xyz.y, xyz.z, w)
fun Vec4f(x: Float, yzw: Vec3f): Vec4f = Vec4f(x, yzw.x, yzw.y, yzw.z)
fun Vec4f(xy: Vec2f, zw: Vec2f): Vec4f = Vec4f(xy.x, xy.y, zw.x, zw.y)
fun Vec4f(x: Float, yz: Vec2f, w: Float): Vec4f = Vec4f(x, yz.x, yz.y, w)

val xyz: Vec3f
get() = Vec3f(x, y, z)
open class Vec4f(open val x: Float, open val y: Float, open val z: Float, open val w: Float) {

constructor(f: Float): this(f, f, f, f)
constructor(xyz: Vec3f, w: Float): this(xyz.x, xyz.y, xyz.z, w)
constructor(v: Vec4f): this(v.x, v.y, v.z, v.w)

val xyz: Vec3f get() = Vec3f(x, y, z)
val yzw: Vec3f get() = Vec3f(y, z, w)
val xy: Vec2f get() = Vec2f(x, y)
val yz: Vec2f get() = Vec2f(y, z)
val zw: Vec2f get() = Vec2f(z, 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 [Vec4f]. Returns the result as a new [Vec4f]. Consider using [add] with
* a pre-allocated result vector in performance-critical situations, to avoid unnecessary object allocations.
Expand Down Expand Up @@ -396,15 +408,27 @@ open class MutableVec4f(override var x: Float, override var y: Float, override v
// </template> 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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9a23f6f

Please sign in to comment.