diff --git a/src/transformations.nim b/src/transformations.nim index 99c9378..2730894 100644 --- a/src/transformations.nim +++ b/src/transformations.nim @@ -1,4 +1,4 @@ -import "./matrix" +from "./matrix" import Matrix, matrix, `*` from "./tuple" import Tuple4 import math @@ -40,11 +40,14 @@ proc shear*(tup: Tuple4, x_y, x_z, y_x, y_z, z_x, z_y: float): Tuple4 = shearing when isMainModule: import unittest + from "./tuple" import point, vector, `==` + from "./matrix" import inverse suite "transformations": test "Multiplying by a translation matrix": let transform = translation(5.0, -3.0, 2.0) let p = point(-3.0, 4.0, 5.0) check(transform * p == point(2.0, 1.0, 7.0)) + check(p.translate(5.0, -3.0, 2.0) == point(2.0, 1.0, 7.0)) test "Multiplying by the inverse of a translation matrix": let transform = translation(5.0, -3.0, 2.0).inverse() @@ -60,11 +63,13 @@ when isMainModule: let transform = scaling(2.0, 3.0, 4.0) let p = point(-4.0, 6.0, 8.0) check(transform * p == point(-8.0, 18.0, 32.0)) + check(p.scale(2.0, 3.0, 4.0) == point(-8.0, 18.0, 32.0)) test "Scaling matrix applied to a vector": let transform = scaling(2.0, 3.0, 4.0) let v = vector(-4.0, 6.0, 8.0) check(transform * v == vector(-8.0, 18.0, 32.0)) + check(v.scale(2.0, 3.0, 4.0) == vector(-8.0, 18.0, 32.0)) test "Multiplying by the inverse of a scaling matrix": let transform = scaling(2.0, 3.0, 4.0).inverse() @@ -75,13 +80,16 @@ when isMainModule: let transform = scaling(-1.0, 1.0, 1.0) let p = point(2.0, 3.0, 4.0) check(transform * p == point(-2.0, 3.0, 4.0)) + check(p.scale(-1.0, 1.0, 1.0) == point(-2.0, 3.0, 4.0)) test "Rotation around the X axis": let p = point(0.0, 1.0, 0.0) let halfQuarter = rotationX(PI / 4) let fullQuarter = rotationX(PI / 2) check(halfQuarter * p == point(0.0, sqrt(2.0)/2.0, sqrt(2.0)/2.0)) + check(p.rotateX(PI / 4) == point(0.0, sqrt(2.0)/2.0, sqrt(2.0)/2.0)) check(fullQuarter * p == point(0.0, 0.0, 1.0)) + check(p.rotateX(PI / 2) == point(0.0, 0.0, 1.0)) test "Inverse of a rotation rotates in the opposite direction": let p = point(0.0, 1.0, 0.0) @@ -93,44 +101,54 @@ when isMainModule: let halfQuarter = rotationY(PI / 4) let fullQuarter = rotationY(PI / 2) check(halfQuarter * p == point(sqrt(2.0)/2.0, 0.0, sqrt(2.0)/2.0)) + check(p.rotateY(PI / 4) == point(sqrt(2.0)/2.0, 0.0, sqrt(2.0)/2.0)) check(fullQuarter * p == point(1.0, 0.0, 0.0)) + check(p.rotateY(PI / 2) == point(1.0, 0.0, 0.0)) test "Rotation around the Z axis": let p = point(0.0, 1.0, 0.0) let halfQuarter = rotationZ(PI / 4) let fullQuarter = rotationZ(PI / 2) check(halfQuarter * p == point(-sqrt(2.0)/2.0, sqrt(2.0)/2.0, 0.0)) + check(p.rotateZ(PI / 4) == point(-sqrt(2.0)/2.0, sqrt(2.0)/2.0, 0.0)) check(fullQuarter * p == point(-1.0, 0.0, 0.0)) + check(p.rotateZ(PI / 2) == point(-1.0, 0.0, 0.0)) test "Shearing transformation moves X in proportion to Y": let t = shearing(1.0, 0.0, 0.0, 0.0, 0.0, 0.0) let p = point(2.0, 3.0, 4.0) check(t * p == point(5.0, 3.0, 4.0)) + check(p.shear(1.0, 0.0, 0.0, 0.0, 0.0, 0.0) == point(5.0, 3.0, 4.0)) test "Shearing transformation moves X in proportion to Z": let t = shearing(0.0, 1.0, 0.0, 0.0, 0.0, 0.0) let p = point(2.0, 3.0, 4.0) check(t * p == point(6.0, 3.0, 4.0)) + check(p.shear(0.0, 1.0, 0.0, 0.0, 0.0, 0.0) == point(6.0, 3.0, 4.0)) test "Shearing transformation moves Y in proportion to X": let t = shearing(0.0, 0.0, 1.0, 0.0, 0.0, 0.0) let p = point(2.0, 3.0, 4.0) check(t * p == point(2.0, 5.0, 4.0)) + check(p.shear(0.0, 0.0, 1.0, 0.0, 0.0, 0.0) == point(2.0, 5.0, 4.0)) test "Shearing transformation moves Y in proportion to Z": let t = shearing(0.0, 0.0, 0.0, 1.0, 0.0, 0.0) let p = point(2.0, 3.0, 4.0) check(t * p == point(2.0, 7.0, 4.0)) + check(p.shear(0.0, 0.0, 0.0, 1.0, 0.0, 0.0) == point(2.0, 7.0, 4.0)) test "Shearing transformation moves Z in proportion to X": let t = shearing(0.0, 0.0, 0.0, 0.0, 1.0, 0.0) let p = point(2.0, 3.0, 4.0) check(t * p == point(2.0, 3.0, 6.0)) + check(p.shear(0.0, 0.0, 0.0, 0.0, 1.0, 0.0) == point(2.0, 3.0, 6.0)) test "Shearing transformation moves Z in proportion to Y": let t = shearing(0.0, 0.0, 0.0, 0.0, 0.0, 1.0) let p = point(2.0, 3.0, 4.0) check(t * p == point(2.0, 3.0, 7.0)) + check(p.shear(0.0, 0.0, 0.0, 0.0, 0.0, 1.0) == point(2.0, 3.0, 7.0)) test "Individual transformations applied in sequence": let p = point(1.0, 0.0, 1.0) @@ -150,4 +168,5 @@ when isMainModule: let s = scaling(5.0, 5.0, 5.0) let t = translation(10.0, 5.0, 7.0) let combo = t * s * r - check(combo * p == point(15.0, 0.0, 7.0)) \ No newline at end of file + check(combo * p == point(15.0, 0.0, 7.0)) + check(p.rotateX(PI / 2).scale(5.0, 5.0, 5.0).translate(10.0, 5.0, 7.0) == point(15.0, 0.0, 7.0)) \ No newline at end of file