Skip to content

Commit

Permalink
Continued unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HexagonNico committed Mar 26, 2024
1 parent a616fac commit c53098b
Show file tree
Hide file tree
Showing 22 changed files with 403 additions and 66 deletions.
10 changes: 5 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ Scala example:
val transform = Mat3x4f.translation(tx, ty, tz) * Mat4f.rotation(rx, ry, rz) * Mat4f.scaling(sx, sy, sz)
var point = Vec3f(x, y, z)

// Applies first a translation by (tx, ty, tz),
// Applies first a scaling by (sx, sy, sz),
// then a rotation by (rx, ry, rz) in radians,
// then a scaling by (sx, sy, sz)
// then a translation by (tx, ty, tz)
point = transform * (point, 1.0f)
```

Expand All @@ -102,9 +102,9 @@ Mat3x4f transform = Mat3x4f.translation(tx, ty, tz)
.multiply(Mat4f.scaling(sx, sy, sz));
Vec3f point = new Vec3f(x, y, z);

// Applies first a translation by (tx, ty, tz),
// Applies first a scaling by (sx, sy, sz),
// then a rotation by (rx, ry, rz) in radians,
// then a scaling by (sx, sy, sz)
// then a translation by (tx, ty, tz)
point = transform.multiply(point, 1.0f);
```

Expand All @@ -123,7 +123,7 @@ Unit quaternions can be used to represent rotations.

```Scala
val quaternion = Quatd(Vec3d.Up, math.Pi / 2.0) // Represents a rotation of 90 degrees around the y axis
val point = Vec3d(1.0, 0.0, 0.0)
var point = Vec3d(1.0, 0.0, 0.0)
point = quaternion.rotate(point) // Results in Vec3d(0.0, 0.0, -1.0)
```

Expand Down
14 changes: 7 additions & 7 deletions src/main/scala/io/github/scalamath/vecmatlib/Mat3d.scala
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ object Mat3d {
* @return A 3x3 rotation matrix.
*/
def rotationX(x: Double): Mat3d = {
val sin = math.sin(-x)
val cos = math.cos(-x)
val sin = math.sin(x)
val cos = math.cos(x)
Mat3d(1.0, 0.0, 0.0, 0.0, cos, -sin, 0.0, sin, cos)
}

Expand All @@ -646,8 +646,8 @@ object Mat3d {
* @return A 3x3 rotation matrix.
*/
def rotationY(y: Double): Mat3d = {
val sin = math.sin(-y)
val cos = math.cos(-y)
val sin = math.sin(y)
val cos = math.cos(y)
Mat3d(cos, 0.0, sin, 0.0, 1.0, 0.0, -sin, 0.0, cos)
}

Expand All @@ -658,8 +658,8 @@ object Mat3d {
* @return A 3x3 rotation matrix.
*/
def rotationZ(z: Double): Mat3d = {
val sin = math.sin(-z)
val cos = math.cos(-z)
val sin = math.sin(z)
val cos = math.cos(z)
Mat3d(cos, -sin, 0.0, sin, cos, 0.0, 0.0, 0.0, 1.0)
}

Expand All @@ -672,7 +672,7 @@ object Mat3d {
* @param z Rotation angle in radians on the z axis.
* @return A 3x3 rotation matrix with the given rotation.
*/
def rotation(x: Double, y: Double, z: Double): Mat3d = this.rotationZ(z) * this.rotationY(y) * this.rotationX(x)
def rotation(x: Double, y: Double, z: Double): Mat3d = this.rotationX(x) * this.rotationY(y) * this.rotationZ(z)

/**
* Returns a 3x3 rotation matrix with the given rotation.
Expand Down
14 changes: 7 additions & 7 deletions src/main/scala/io/github/scalamath/vecmatlib/Mat3f.scala
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ object Mat3f {
* @return A 3x3 rotation matrix.
*/
def rotationX(x: Double): Mat3f = {
val sin = math.sin(-x).toFloat
val cos = math.cos(-x).toFloat
val sin = math.sin(x).toFloat
val cos = math.cos(x).toFloat
Mat3f(1.0f, 0.0f, 0.0f, 0.0f, cos, -sin, 0.0f, sin, cos)
}

Expand All @@ -646,8 +646,8 @@ object Mat3f {
* @return A 3x3 rotation matrix.
*/
def rotationY(y: Double): Mat3f = {
val sin = math.sin(-y).toFloat
val cos = math.cos(-y).toFloat
val sin = math.sin(y).toFloat
val cos = math.cos(y).toFloat
Mat3f(cos, 0.0f, sin, 0.0f, 1.0f, 0.0f, -sin, 0.0f, cos)
}

Expand All @@ -658,8 +658,8 @@ object Mat3f {
* @return A 3x3 rotation matrix.
*/
def rotationZ(z: Double): Mat3f = {
val sin = math.sin(-z).toFloat
val cos = math.cos(-z).toFloat
val sin = math.sin(z).toFloat
val cos = math.cos(z).toFloat
Mat3f(cos, -sin, 0.0f, sin, cos, 0.0f, 0.0f, 0.0f, 1.0f)
}

Expand All @@ -672,7 +672,7 @@ object Mat3f {
* @param z Rotation angle in radians on the z axis.
* @return A 3x3 rotation matrix with the given rotation.
*/
def rotation(x: Double, y: Double, z: Double): Mat3f = this.rotationZ(z) * this.rotationY(y) * this.rotationX(x)
def rotation(x: Double, y: Double, z: Double): Mat3f = this.rotationX(x) * this.rotationY(y) * this.rotationZ(z)

/**
* Returns a 3x3 rotation matrix with the given rotation.
Expand Down
14 changes: 7 additions & 7 deletions src/main/scala/io/github/scalamath/vecmatlib/Mat3x4d.scala
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ object Mat3x4d {
* @return A 3x4 rotation matrix.
*/
def rotationX(x: Double): Mat3x4d = {
val sin = math.sin(-x)
val cos = math.cos(-x)
val sin = math.sin(x)
val cos = math.cos(x)
Mat3x4d(1.0, 0.0, 0.0, 0.0, 0.0, cos, -sin, 0.0, 0.0, sin, cos, 0.0)
}

Expand All @@ -580,8 +580,8 @@ object Mat3x4d {
* @return A 3x4 rotation matrix.
*/
def rotationY(y: Double): Mat3x4d = {
val sin = math.sin(-y)
val cos = math.cos(-y)
val sin = math.sin(y)
val cos = math.cos(y)
Mat3x4d(cos, 0.0, sin, 0.0, 0.0, 1.0, 0.0, 0.0, -sin, 0.0, cos, 0.0)
}

Expand All @@ -592,15 +592,15 @@ object Mat3x4d {
* @return A 3x4 rotation matrix.
*/
def rotationZ(z: Double): Mat3x4d = {
val sin = math.sin(-z)
val cos = math.cos(-z)
val sin = math.sin(z)
val cos = math.cos(z)
Mat3x4d(cos, -sin, 0.0, 0.0, sin, cos, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
}

/**
* Returns a 3x4 rotation matrix with a rotation of the given angle around the given axis.
*
* @param axis A unit vector representing the rotation axis. The result is undefined if this vector is not normalized.
* @param axis A unit vector representing the rotation axis. The result is undefined if this vector is not [[Vec3d.normalized]].
* @param angle The rotation angle in radians.
* @return A 3x4 rotation matrix with the given rotation.
*/
Expand Down
14 changes: 7 additions & 7 deletions src/main/scala/io/github/scalamath/vecmatlib/Mat3x4f.scala
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ object Mat3x4f {
* @return A 3x4 rotation matrix.
*/
def rotationX(x: Double): Mat3x4f = {
val sin = math.sin(-x).toFloat
val cos = math.cos(-x).toFloat
val sin = math.sin(x).toFloat
val cos = math.cos(x).toFloat
Mat3x4f(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, cos, -sin, 0.0f, 0.0f, sin, cos, 0.0f)
}

Expand All @@ -580,8 +580,8 @@ object Mat3x4f {
* @return A 3x4 rotation matrix.
*/
def rotationY(y: Double): Mat3x4f = {
val sin = math.sin(-y).toFloat
val cos = math.cos(-y).toFloat
val sin = math.sin(y).toFloat
val cos = math.cos(y).toFloat
Mat3x4f(cos, 0.0f, sin, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -sin, 0.0f, cos, 0.0f)
}

Expand All @@ -592,15 +592,15 @@ object Mat3x4f {
* @return A 3x4 rotation matrix.
*/
def rotationZ(z: Double): Mat3x4f = {
val sin = math.sin(-z).toFloat
val cos = math.cos(-z).toFloat
val sin = math.sin(z).toFloat
val cos = math.cos(z).toFloat
Mat3x4f(cos, -sin, 0.0f, 0.0f, sin, cos, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f)
}

/**
* Returns a 3x4 rotation matrix with a rotation of the given angle around the given axis.
*
* @param axis A unit vector representing the rotation axis. The result is undefined if this vector is not normalized.
* @param axis A unit vector representing the rotation axis. The result is undefined if this vector is not [[Vec3f.normalized]].
* @param angle The rotation angle in radians.
* @return A 3x4 rotation matrix with the given rotation.
*/
Expand Down
16 changes: 8 additions & 8 deletions src/main/scala/io/github/scalamath/vecmatlib/Mat4d.scala
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ object Mat4d {
* @return A 4x4 rotation matrix.
*/
def rotationX(x: Double): Mat4d = {
val sin = math.sin(-x)
val cos = math.cos(-x)
val sin = math.sin(x)
val cos = math.cos(x)
Mat4d(1.0, 0.0, 0.0, 0.0, 0.0, cos, -sin, 0.0, 0.0, sin, cos, 0.0, 0.0, 0.0, 0.0, 1.0)
}

Expand All @@ -724,8 +724,8 @@ object Mat4d {
* @return A 4x4 rotation matrix.
*/
def rotationY(y: Double): Mat4d = {
val sin = math.sin(-y)
val cos = math.cos(-y)
val sin = math.sin(y)
val cos = math.cos(y)
Mat4d(cos, 0.0, sin, 0.0, 0.0, 1.0, 0.0, 0.0, -sin, 0.0, cos, 0.0, 0.0, 0.0, 0.0, 1.0)
}

Expand All @@ -736,8 +736,8 @@ object Mat4d {
* @return A 4x4 rotation matrix.
*/
def rotationZ(z: Double): Mat4d = {
val sin = math.sin(-z)
val cos = math.cos(-z)
val sin = math.sin(z)
val cos = math.cos(z)
Mat4d(cos, -sin, 0.0, 0.0, sin, cos, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0)
}

Expand All @@ -750,7 +750,7 @@ object Mat4d {
* @param z Rotation angle in radians on the z axis.
* @return A 4x4 rotation matrix with the given rotation.
*/
def rotation(x: Double, y: Double, z: Double): Mat4d = this.rotationZ(z) * this.rotationY(y) * this.rotationX(x)
def rotation(x: Double, y: Double, z: Double): Mat4d = this.rotationX(x) * this.rotationY(y) * this.rotationZ(z)

/**
* Returns a 4x4 rotation matrix with the given rotation.
Expand All @@ -764,7 +764,7 @@ object Mat4d {
/**
* Returns a 4x4 rotation matrix with a rotation of the given angle around the given axis.
*
* @param axis A unit vector representing the rotation axis. The result is undefined if this vector is not normalized.
* @param axis A unit vector representing the rotation axis. The result is undefined if this vector is not [[Vec3d.normalized]].
* @param angle The rotation angle in radians.
* @return A 4x4 rotation matrix with the given rotation.
*/
Expand Down
16 changes: 8 additions & 8 deletions src/main/scala/io/github/scalamath/vecmatlib/Mat4f.scala
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ object Mat4f {
* @return A 4x4 rotation matrix.
*/
def rotationX(x: Double): Mat4f = {
val sin = math.sin(-x).toFloat
val cos = math.cos(-x).toFloat
val sin = math.sin(x).toFloat
val cos = math.cos(x).toFloat
Mat4f(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, cos, -sin, 0.0f, 0.0f, sin, cos, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
}

Expand All @@ -724,8 +724,8 @@ object Mat4f {
* @return A 4x4 rotation matrix.
*/
def rotationY(y: Double): Mat4f = {
val sin = math.sin(-y).toFloat
val cos = math.cos(-y).toFloat
val sin = math.sin(y).toFloat
val cos = math.cos(y).toFloat
Mat4f(cos, 0.0f, sin, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -sin, 0.0f, cos, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
}

Expand All @@ -736,8 +736,8 @@ object Mat4f {
* @return A 4x4 rotation matrix.
*/
def rotationZ(z: Double): Mat4f = {
val sin = math.sin(-z).toFloat
val cos = math.cos(-z).toFloat
val sin = math.sin(z).toFloat
val cos = math.cos(z).toFloat
Mat4f(cos, -sin, 0.0f, 0.0f, sin, cos, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
}

Expand All @@ -750,7 +750,7 @@ object Mat4f {
* @param z Rotation angle in radians on the z axis.
* @return A 4x4 rotation matrix with the given rotation.
*/
def rotation(x: Double, y: Double, z: Double): Mat4f = this.rotationZ(z) * this.rotationY(y) * this.rotationX(x)
def rotation(x: Double, y: Double, z: Double): Mat4f = this.rotationX(x) * this.rotationY(y) * this.rotationZ(z)

/**
* Returns a 4x4 rotation matrix with the given rotation.
Expand All @@ -773,7 +773,7 @@ object Mat4f {
/**
* Returns a 4x4 rotation matrix with a rotation of the given angle around the given axis.
*
* @param axis A unit vector representing the rotation axis. The result is undefined if this vector is not normalized.
* @param axis A unit vector representing the rotation axis. The result is undefined if this vector is not [[Vec4f.normalized]].
* @param angle The rotation angle in radians.
* @return A 4x4 rotation matrix with the given rotation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ class Mat2dSuite extends AnyFunSuite {
assert(mat.floor == floor)
}

// TODO: Orthonormalize
test("Orthonormalize matrix") {
val mat = Mat2d(1.0, 2.0, 3.0, 4.0)
val res = Mat2d(1.0, 3.0, 3.0, -1.0) / math.sqrt(10.0)
assert(mat.orthonormalized === res)
}

test("Matrix equals approx") {
val m1 = Mat2d(1.00000001, 1.99999999, 3.00000001, 3.99999999)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ class Mat2fSuite extends AnyFunSuite {
assert(mat.floor == floor)
}

// TODO: Orthonormalize
test("Orthonormalize matrix") {
val mat = Mat2f(1.0f, 2.0f, 3.0f, 4.0f)
val res = Mat2f(1.0f, 3.0f, 3.0f, -1.0f) / math.sqrt(10.0).toFloat
assert(mat.orthonormalized === res)
}

test("Matrix equals approx") {
val m1 = Mat2f(1.00000001f, 1.99999999f, 3.00000001f, 3.99999999f)
Expand Down
51 changes: 50 additions & 1 deletion src/test/scala/io/github/scalamath/vecmatlib/Mat3dSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,56 @@ class Mat3dSuite extends AnyFunSuite {
assert((m * (v, 1.0)).xy === v + t)
}

// TODO: Rotation matrices
test("Rotation matrix around the x axis") {
val v1 = Vec3d(1.0, 2.0, 1.0)
val v2 = Vec3d(1.0, 0.70710678, 2.12132034)
val m = Mat3d.rotationX(math.Pi / 4.0)
assert(m * v1 === v2)
}

test("Rotation matrix around the y axis") {
val v1 = Vec3d(1.0, 2.0, 1.0)
val v2 = Vec3d(1.41421356, 2.0, 0.0)
val m = Mat3d.rotationY(math.Pi / 4.0)
assert(m * v1 === v2)
}

test("Rotation matrix around the z axis") {
val v1 = Vec3d(1.0, 2.0, 1.0)
val v2 = Vec3d(-0.70710678, 2.12132034, 1.0)
val m = Mat3d.rotationZ(math.Pi / 4.0)
assert(m * v1 === v2)
}

test("Rotation matrix around the three axes") {
val v1 = Vec3d(1.0, 2.0, 1.0)
val v2 = Vec3d(0.61237244, 0.42161062, 2.33393327)
val m = Mat3d.rotation(math.Pi / 3.0, math.Pi / 4.0, math.Pi / 6.0)
assert(m * v1 === v2)
}

test("Rotation matrix from rotation vector") {
val v1 = Vec3d(1.0, 2.0, 1.0)
val r = Vec3d(math.Pi / 3.0, math.Pi / 4.0, math.Pi / 6.0)
val v2 = Vec3d(0.61237244, 0.42161062, 2.33393327)
val m = Mat3d.rotation(r)
assert(m * v1 === v2)
}

test("Rotation around an arbitrary axis") {
val axis = Vec3d.One.normalized
val point = Vec3d(1.0, 2.0, 1.0)
val res = Vec3d(0.68938278, 1.80473785, 1.50587936)
val matrix = Mat3d.rotation(axis, math.Pi / 4.0)
assert(matrix * point === res)
}

test("Rotation matrix from quaternion") {
val quaternion = Quatd(0.7233174, 0.3919038, 0.2005621, 0.5319757)
val matrix = Mat3d.rotation(quaternion)
val point = Vec3d(1.0, 2.0, 1.0)
assert(matrix * point === quaternion.rotate(point))
}

test("Scaling matrix from values") {
val v1 = Vec3d(1.5, 1.5, 1.5)
Expand Down
Loading

0 comments on commit c53098b

Please sign in to comment.