Remove the choice of left-handed product. Refactor vectorProduct. Remove leviChivita function.

This commit is contained in:
Gleb Minaev 2023-03-14 20:13:34 +03:00
parent 4871baf0e5
commit 28b85b0f53

View File

@ -103,45 +103,21 @@ public object Euclidean3DSpace : GeometrySpace<DoubleVector3D>, ScaleOperations<
override fun DoubleVector3D.dot(other: DoubleVector3D): Double = override fun DoubleVector3D.dot(other: DoubleVector3D): Double =
x * other.x + y * other.y + z * other.z x * other.x + y * other.y + z * other.z
private fun leviCivita(i: Int, j: Int, k: Int): Int = when {
// even permutation
i == 0 && j == 1 && k == 2 -> 1
i == 1 && j == 2 && k == 0 -> 1
i == 2 && j == 0 && k == 1 -> 1
// odd permutations
i == 2 && j == 1 && k == 0 -> -1
i == 0 && j == 2 && k == 1 -> -1
i == 1 && j == 0 && k == 2 -> -1
else -> 0
}
/** /**
* Compute vector product of [first] and [second]. The basis assumed to be right-handed if [rightBasis] is true and * Compute vector product of [first] and [second]. The basis assumed to be right-handed.
* left-handed otherwise
*/ */
public fun vectorProduct( public fun vectorProduct(
first: DoubleVector3D, first: DoubleVector3D,
second: DoubleVector3D, second: DoubleVector3D,
rightBasis: Boolean = true,
): DoubleVector3D { ): DoubleVector3D {
var x = 0.0 val (x1, y1, z1) = first
var y = 0.0 val (x2, y2, z2) = second
var z = 0.0
for (j in (0..2)) { return vector(y1 * z2 - y2 * z2, z1 * x2 - z2 * x2, x1 * y2 - x2 * y2)
for (k in (0..2)) {
x += leviCivita(0, j, k) * first[j] * second[k]
y += leviCivita(1, j, k) * first[j] * second[k]
z += leviCivita(2, j, k) * first[j] * second[k]
}
}
return vector(x, y, z) * (if (rightBasis) 1 else -1)
} }
/** /**
* Vector product with right basis * Vector product with a right basis
*/ */
public infix fun DoubleVector3D.cross(other: DoubleVector3D): Vector3D<Double> = vectorProduct(this, other) public infix fun DoubleVector3D.cross(other: DoubleVector3D): Vector3D<Double> = vectorProduct(this, other)