Skip to content

Commit

Permalink
Add infix operator for transform
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasHassing committed May 20, 2016
1 parent 8680512 commit 33108fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/Ray/Ray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,19 @@ let getOrigin (R(o, _)) = o
let getVector (R(_, d)) = d

/// <summary>
/// Transform the ray with some transformation.
/// Transform a ray with a transformation.
/// </summary>
/// <param name=r>The ray to transform.</param>
/// <param name=t>The transformation to apply to the ray.</param>
/// <returns>The transformed ray.</returns>
let transform (R(o, d)) (t:Transformation) =
R(o * t, d * t)

type Ray with
/// <summary>
/// Transform a ray with a transformation.
/// </summary>
/// <param name=r>The ray to transform.</param>
/// <param name=t>The transformation to apply to the ray.</param>
/// <returns>The transformed ray.</returns>
static member (*) (r, t) = transform r t
11 changes: 10 additions & 1 deletion src/Ray/Ray.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ open Point
open Transform
open Vector

type Ray
[<Sealed>]
[<NoComparison>]
type Ray =
/// <summary>
/// Transform a ray with a transformation.
/// </summary>
/// <param name=r>The ray to transform.</param>
/// <param name=t>The transformation to apply to the ray.</param>
/// <returns>The transformed ray.</returns>
static member (*) : r:Ray * t:Transformation -> Ray

/// <summary>
/// Create a ray; a normalized vector with a point of origin.
Expand Down
2 changes: 1 addition & 1 deletion src/Ray/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0",
"version": "0.2.0",
"description": "A library for modelling 3-dimensional rays of light",
"authors": [
"Andreas Bjørn Hassing Nielsen <[email protected]>"
Expand Down
12 changes: 12 additions & 0 deletions test/Ray.Test/RayTest.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ let ``transform transforms a ray with the appropriate transformation`` () =
Ray.getOrigin tr |> should equal (Point.make -1. 1. 1.)
Ray.getVector tr |> should equal tv

[<Fact>]
let ``transform transforms a ray with the appropriate transformation, using the static member`` () =
let p = Point.make 1. 1. 1.
let v = Vector.make -1. -1. -1.
let r = Ray.make p v
let tr = r * (Transform.mirror Transform.Axis.X)

let tv = (Vector.normalise v) * (Transform.mirror Transform.Axis.X)

Ray.getOrigin tr |> should equal (Point.make -1. 1. 1.)
Ray.getVector tr |> should equal tv

[<Fact>]
let ``transform with an empty matrix does nothing to the ray`` () =
let p = Point.make 1. 1. 1.
Expand Down

0 comments on commit 33108fe

Please sign in to comment.