package ballistics.utils.math

class Matrix3D(var first: Vector3D, var second: Vector3D, var third: Vector3D) {
    constructor() : this(Vector3D(), Vector3D(), Vector3D())

    fun firstColumn(): Vector3D {
        return Vector3D(first.x, second.x, third.x)
    }

    fun secondColumn(): Vector3D {
        return Vector3D(first.y, second.y, third.y)
    }

    fun thirdColumn(): Vector3D {
        return Vector3D(first.z, second.z, third.z)
    }

    fun empty() {
        first.setData(0.0, 0.0, 0.0)
        second.setData(0.0, 0.0, 0.0)
        third.setData(0.0, 0.0, 0.0)
    }

    fun makeOxMatrix(angle: Double) {
        first.setData(1.0, 0.0, 0.0)
        second.setData(0.0, Math.cos(angle), -Math.sin(angle))
        third.setData(0.0, Math.sin(angle), Math.cos(angle))
    }

    fun makeOyMatrix(angle: Double) {
        first.setData(Math.cos(angle), 0.0, Math.sin(angle))
        second.setData(0.0, 1.0, 0.0)
        third.setData(-Math.sin(angle), 0.0, Math.cos(angle))
    }

    fun makeOzMatrix(angle: Double) {
        first.setData(Math.cos(angle), -Math.sin(angle), 0.0)
        second.setData(Math.sin(angle), Math.cos(angle), 0.0)
        third.setData(0.0, 0.0, 1.0)
    }

    fun transpose(): Matrix3D {
        return Matrix3D(
            Vector3D(first.x, second.x, third.x),
            Vector3D(first.y, second.y, third.y),
            Vector3D(first.z, second.z, third.z),
        )
    }

    operator fun times(other: Vector3D) = Vector3D(first * other, second * other, third * other)

    operator fun times(other: Matrix3D) =
        Matrix3D(
            Vector3D(first * other.firstColumn(), first * other.secondColumn(), first * other.thirdColumn()),
            Vector3D(second * other.firstColumn(), second * other.secondColumn(), second * other.thirdColumn()),
            Vector3D(third * other.firstColumn(), third * other.secondColumn(), third * other.thirdColumn()),
        )
}
