some matrix tests

This commit is contained in:
Moritz Zwerger committed 2025-10-12 14:31:28 +02:00
1 parent d58e2c0ebb
commit a90646e720
5 files changed
+167 -11

No files matched your search

@@ -55,9 +55,9 @@ value class MMat4f(val _0: UnsafeMat4f) : _Mat4f {
val unsafe get() = Mat4f(_0)
override inline operator fun get(x: Int) = Vec4f(this[x, 0], this[x, 1], this[x, 2], this[x, 3])
override inline operator fun get(x: Int, y: Int) = _0[x, y]
override inline operator fun get(x: Int, y: Int) = _0[x, y]// TODO: access them column/row or row/column
inline operator fun set(x: Int, vec4: Vec4f) {
inline operator fun set(x: Int, vec4: Vec4f) {// TODO: access them column/row or row/column
this[x, 0] = vec4.x
this[x, 1] = vec4.y
this[x, 2] = vec4.z
@@ -65,7 +65,7 @@ value class MMat4f(val _0: UnsafeMat4f) : _Mat4f {
}
inline operator fun set(x: Int, y: Int, value: Float) {
_0[x, y] = value
_0[x, y] = value // TODO: access them column/row or row/column
}
inline operator fun plus(number: Number) = MMat4f().apply { Mat4Operations.plus(this@MMat4f, number, this) }
@@ -88,6 +88,7 @@ value class MMat4f(val _0: UnsafeMat4f) : _Mat4f {
inline fun transpose() = MMat4f(
// TODO: access them column/row or row/column
this[0, 0], this[1, 0], this[2, 0], this[3, 0],
this[0, 1], this[1, 1], this[2, 1], this[3, 1],
this[0, 2], this[1, 2], this[2, 2], this[3, 2],
@@ -51,8 +51,10 @@ value class Mat4f(val _0: UnsafeMat4f) : _Mat4f {
val unsafe get() = MMat4f(_0)
override inline operator fun get(x: Int) = Vec4f(this[x, 0], this[x, 1], this[x, 2], this[x, 3])
override inline operator fun get(x: Int, y: Int) = _0[x, y]
override inline operator fun get(row: Int) = Vec4f(this[row, 0], this[row, 1], this[row, 2], this[row, 3])
override inline operator fun get(row: Int, column: Int) = _0[row, column]
inline operator fun plus(number: Number) = MMat4f().apply { Mat4Operations.plus(this@Mat4f, number, this) }.unsafe
inline operator fun plus(other: _Mat4f) = MMat4f().apply { Mat4Operations.plus(this@Mat4f, other, this) }.unsafe
@@ -24,16 +24,16 @@ class UnsafeMat4f(
fun final() = Mat4f(this)
fun mutable() = MMat4f(this)
inline operator fun get(x: Int, y: Int): Float {
inline operator fun get(row: Int, column: Int): Float {
// assert(x in 0 until 4)
// assert(y in 0 until 4)
return array[(y shl 2) or x]
return array[(row shl 2) or column]
}
inline operator fun set(x: Int, y: Int, value: Float) {
inline operator fun set(row: Int, column: Int, value: Float) {
// assert(x in 0 until 4)
// assert(y in 0 until 4)
array[(y shl 2) or x] = value
array[(row shl 2) or column] = value
}
@@ -21,8 +21,6 @@ import de.bixilon.minosoft.util.KUtil.rad
object Mat4Util {
fun Mat4f.times(vec3: Vec3f, res: Vec3f): Vec3f = TODO()
fun rotateX(m: MMat4f, angle: Float): Unit = TODO()
fun rotateY(m: MMat4f, angle: Float): Unit = TODO()
@@ -0,0 +1,155 @@
/*
* Minosoft
* Copyright (C) 2020-2025 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.kmath.mat.mat4.f
import de.bixilon.kmath.vec.vec4.f.Vec4f
import org.junit.jupiter.api.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class Mat4fTest {
@Test
fun `creation single value`() {
val mat = Mat4f(1.0f)
assertContentEquals(mat._0.array, floatArrayOf(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
))
}
@Test
fun `creation diagonal values`() {
val mat = Mat4f(1.0f, 2.0f, 3.0f, 4.0f)
assertContentEquals(mat._0.array, floatArrayOf(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 3.0f, 0.0f,
0.0f, 0.0f, 0.0f, 4.0f,
))
}
@Test
fun `get row column`() {
val mat = Mat4f(
1.0f, 2.0f, 3.0f, 4.0f,
0.0f, 0.0f, 0.0f, 9.0f,
6.0f, 5.0f, 7.0f, 0.0f,
6.0f, 7.0f, 5.0f, 8.0f
)
assertEquals(mat[0, 0], 1.0f)
assertEquals(mat[1, 0], 0.0f)
assertEquals(mat[0, 1], 2.0f)
}
@Test
fun `get vector by row`() {
val mat = Mat4f(
1.0f, 2.0f, 3.0f, 4.0f,
0.0f, 0.0f, 0.0f, 9.0f,
6.0f, 5.0f, 7.0f, 0.0f,
6.0f, 7.0f, 5.0f, 8.0f
)
assertEquals(mat[0], Vec4f(1.0f, 2.0f, 3.0f, 4.0f))
}
@Test
fun `plus number`() {
val mat = Mat4f(1.0f, 2.0f, 3.0f, 4.0f)
assertEquals((mat + 5.0f), Mat4f(
6.0f, 5.0f, 5.0f, 5.0f,
5.0f, 7.0f, 5.0f, 5.0f,
5.0f, 5.0f, 8.0f, 5.0f,
5.0f, 5.0f, 5.0f, 9.0f,
))
}
@Test
fun `plus matrix`() {
val mat = Mat4f(1.0f, 2.0f, 3.0f, 4.0f)
assertEquals((mat + Mat4f(5.0f)), Mat4f(
6.0f, 5.0f, 5.0f, 5.0f,
5.0f, 7.0f, 5.0f, 5.0f,
5.0f, 5.0f, 8.0f, 5.0f,
5.0f, 5.0f, 5.0f, 9.0f,
))
}
@Test
fun `times number`() {
val mat = Mat4f(1.0f, 2.0f, 3.0f, 4.0f)
assertEquals((mat * 2.0f), Mat4f(
2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 4.0f, 0.0f, 0.0f,
0.0f, 0.0f, 6.0f, 0.0f,
0.0f, 0.0f, 0.0f, 8.0f,
))
}
@Test
fun `times matrix`() {
val mat = Mat4f(1.0f, 2.0f, 3.0f, 4.0f)
assertEquals((mat * Mat4f(2.0f, 2.0f, 3.0f, 3.0f)), Mat4f(
2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 6.0f, 0.0f,
0.0f, 0.0f, 0.0f, 6.0f,
))
}
@Test
fun transpose() {
val mat = Mat4f(
1.0f, 2.0f, 3.0f, 4.0f,
0.0f, 0.0f, 0.0f, 9.0f,
6.0f, 5.0f, 7.0f, 0.0f,
6.0f, 7.0f, 5.0f, 8.0f,
)
assertEquals(mat.transpose(), Mat4f(
1.0f, 0.0f, 6.0f, 6.0f,
2.0f, 0.0f, 5.0f, 7.0f,
3.0f, 0.0f, 7.0f, 5.0f,
4.0f, 9.0f, 0.0f, 8.0f,
))
}
@Test
fun `translate numbers`() {
val mat = Mat4f(
1.0f, 2.0f, 3.0f, 4.0f,
0.0f, 0.0f, 0.0f, 9.0f,
6.0f, 5.0f, 7.0f, 0.0f,
6.0f, 7.0f, 5.0f, 8.0f,
)
assertEquals(mat.translate(2.0f, 3.0f, 4.0f), Mat4f(
// TODO
1.0f, 0.0f, 6.0f, 6.0f,
2.0f, 0.0f, 5.0f, 7.0f,
3.0f, 0.0f, 7.0f, 5.0f,
4.0f, 9.0f, 0.0f, 8.0f,
))
}
// TODO: scale, rotate (x,y,z), times (vector)
}