Opentk/tests/OpenTK.Tests/Vector3Tests.fs

745 lines
26 KiB
Forth
Raw Normal View History

2017-05-31 18:08:41 +00:00
namespace OpenTK.Tests
open Xunit
open FsCheck
open FsCheck.Xunit
open System
2017-06-01 14:05:02 +00:00
open System.Runtime.InteropServices
2017-05-31 18:08:41 +00:00
open OpenTK
2017-06-01 19:12:59 +00:00
module Vector3 =
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module Constructors =
2017-05-31 18:08:41 +00:00
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Triple value constructor sets all components to the correct values`` (a, b, c) =
2017-05-31 18:08:41 +00:00
let v = Vector3(a, b, c)
Assert.Equal(a, v.X)
Assert.Equal(b, v.Y)
Assert.Equal(c, v.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Single value constructor sets all components to the correct values`` (a : float32) =
2017-05-31 18:08:41 +00:00
let v = Vector3(a)
Assert.Equal(a, v.X)
Assert.Equal(a, v.Y)
Assert.Equal(a, v.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector2 value constructor sets all components to the correct values`` (a, b) =
2017-05-31 18:08:41 +00:00
let v1 = Vector2(a, b)
let v2 = Vector3(v1)
Assert.Equal(v1.X, v2.X)
Assert.Equal(v1.Y, v2.Y)
Assert.Equal(a, v2.X)
Assert.Equal(b, v2.Y)
2017-06-01 20:09:04 +00:00
Assert.Equal(0.0f, v2.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3 value constructor sets all components to the correct values`` (a, b, c) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(a, b, c)
let v2 = Vector3(v1)
Assert.Equal(v1.X, v2.X)
Assert.Equal(v1.Y, v2.Y)
Assert.Equal(v1.Z, v2.Z)
Assert.Equal(a, v2.X)
Assert.Equal(b, v2.Y)
Assert.Equal(c, v2.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 19:40:44 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector4 value constructor sets all components to the correct values`` (a, b, c, d) =
2017-05-31 19:40:44 +00:00
let v1 = Vector4(a, b, c, d)
let v2 = Vector3(v1)
Assert.Equal(v1.X, v2.X)
Assert.Equal(v1.Y, v2.Y)
Assert.Equal(v1.Z, v2.Z)
Assert.Equal(a, v2.X)
Assert.Equal(b, v2.Y)
Assert.Equal(c, v2.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module Indexing =
2017-05-31 18:08:41 +00:00
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Index operator accesses the correct components`` (x, y, z) =
2017-06-01 19:09:10 +00:00
let v = Vector3(x, y, z)
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
Assert.Equal(x, v.[0])
Assert.Equal(y, v.[1])
Assert.Equal(z, v.[2])
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Indexed set operator throws exception for negative indices`` (x, y, z) =
2017-06-01 19:09:10 +00:00
let mutable v = Vector3(x, y, z)
(fun() -> v.[-1] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Indexed get operator throws exception for negative indices`` (x, y, z) =
2017-06-01 19:09:10 +00:00
let mutable v = Vector3(x, y, z)
2017-05-31 18:08:41 +00:00
2017-06-01 19:09:10 +00:00
(fun() -> v.[-1] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Indexed set operator throws exception for large indices`` (x, y, z) =
2017-06-01 19:09:10 +00:00
let mutable v = Vector3(x, y, z)
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
(fun() -> v.[4] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Indexed get operator throws exception for large indices`` (x, y, z) =
2017-06-01 19:09:10 +00:00
let mutable v = Vector3(x, y, z)
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
(fun() -> v.[4] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module Length =
2017-05-31 18:08:41 +00:00
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Length method follows the pythagorean theorem`` (a, b, c) =
2017-05-31 18:08:41 +00:00
let v = Vector3(a, b, c)
let l = System.Math.Sqrt((float)(a * a + b * b + c * c))
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal((float32)l, v.Length)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Fast length method is the same as one divided by the fast inverse square`` (a, b, c) =
2017-05-31 18:08:41 +00:00
let v = Vector3(a, b, c)
let l = 1.0f / MathHelper.InverseSqrtFast(a * a + b * b + c * c)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(l, v.LengthFast)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Length squared method returns each component squared and summed`` (a, b, c) =
2017-05-31 18:08:41 +00:00
let v = Vector3(a, b, c)
let lsq = a * a + b * b + c * c
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(lsq, v.LengthSquared)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module Normalization =
2017-05-31 18:08:41 +00:00
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Normalization creates a new unit length vector with the correct components`` (a, b, c) =
2017-05-31 18:08:41 +00:00
let v = Vector3(a, b, c)
let l = v.Length
2017-06-01 19:12:59 +00:00
// Dividing by zero is not supported
if not (approxEq l 0.0f) then
let norm = v.Normalized()
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v.X / l, norm.X)
Assert.ApproximatelyEqual(v.Y / l, norm.Y)
Assert.ApproximatelyEqual(v.Z / l, norm.Z)
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b, c) =
2017-05-31 18:08:41 +00:00
let v = Vector3(a, b, c)
let l = v.Length
2017-06-01 19:12:59 +00:00
if not (approxEq l 0.0f) then
let norm = Vector3(a, b, c)
norm.Normalize()
2017-06-01 19:09:10 +00:00
Assert.ApproximatelyEqual(v.X / l, norm.X)
Assert.ApproximatelyEqual(v.Y / l, norm.Y)
Assert.ApproximatelyEqual(v.Z / l, norm.Z)
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Fast approximate normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b, c) =
2017-05-31 18:08:41 +00:00
let v = Vector3(a, b, c)
let norm = Vector3(a, b, c)
norm.NormalizeFast()
let scale = MathHelper.InverseSqrtFast(a * a + b * b + c * c)
Assert.ApproximatelyEqual(v.X * scale, norm.X)
Assert.ApproximatelyEqual(v.Y * scale, norm.Y)
Assert.ApproximatelyEqual(v.Z * scale, norm.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Normalization by reference is the same as division by magnitude`` (a : Vector3) =
let norm = a / a.Length
let vRes = Vector3.Normalize(ref a)
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
Assert.ApproximatelyEqual(norm, vRes)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Normalization is the same as division by magnitude`` (a : Vector3) =
let norm = a / a.Length
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
Assert.ApproximatelyEqual(norm, Vector3.Normalize(a));
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Fast approximate normalization by reference is the same as multiplication by the fast inverse square`` (a : Vector3) =
let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z)
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
let norm = a * scale
2017-05-31 18:08:41 +00:00
let vRes = Vector3.NormalizeFast(ref a)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.ApproximatelyEqual(norm, vRes)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Fast approximate normalization is the same as multiplication by fast inverse square`` (a : Vector3) =
let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z)
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
let norm = a * scale
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.ApproximatelyEqual(norm, Vector3.NormalizeFast(a));
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module Addition =
2017-05-31 18:08:41 +00:00
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3 addition is the same as component addition`` (a : Vector3, b : Vector3) =
2017-05-31 18:08:41 +00:00
let c = a + b
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.ApproximatelyEqual(a.X + b.X,c.X)
Assert.ApproximatelyEqual(a.Y + b.Y,c.Y)
2017-06-01 17:53:32 +00:00
Assert.ApproximatelyEqual(a.Z + b.Z,c.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3 addition is commutative`` (a : Vector3, b : Vector3) =
2017-05-31 18:08:41 +00:00
let c = a + b
let c2 = b + a
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.ApproximatelyEqual(c, c2)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3 addition is associative`` (a : Vector3, b : Vector3, c : Vector3) =
2017-05-31 18:08:41 +00:00
let r1 = (a + b) + c
let r2 = a + (b + c)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.ApproximatelyEqual(r1, r2)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3 addition method is the same as component addition`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z)
let sum = Vector3.Add(a, b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3 addition method by reference is the same as component addition`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z)
let sum = Vector3.Add(ref a, ref b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module Subtraction =
2017-05-31 18:08:41 +00:00
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3 subtraction is the same as component subtraction`` (a : Vector3, b : Vector3) =
2017-05-31 18:08:41 +00:00
let c = a - b
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(a.X - b.X,c.X)
Assert.Equal(a.Y - b.Y,c.Y)
Assert.Equal(a.Z - b.Z,c.Z)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3 subtraction method is the same as component addition`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z)
let sum = Vector3.Subtract(a, b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3 subtraction method by reference is the same as component addition`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z)
let sum = Vector3.Subtract(ref a, ref b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module Multiplication =
2017-05-31 18:08:41 +00:00
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3 multiplication is the same as component multiplication`` (a : Vector3, b : Vector3) =
2017-05-31 18:08:41 +00:00
let c = a * b
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(a.X * b.X,c.X)
Assert.Equal(a.Y * b.Y,c.Y)
Assert.Equal(a.Z * b.Z,c.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3 multiplication is commutative`` (a : Vector3, b : Vector3) =
2017-05-31 18:08:41 +00:00
let r1 = a * b
let r2 = b * a
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(r1, r2)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Left-handed Vector3-scalar multiplication is the same as component-scalar multiplication`` (a : Vector3, f : float32) =
2017-05-31 18:08:41 +00:00
let r = a * f
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y)
Assert.Equal(a.Z * f,r.Z)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Right-handed Vector3-scalar multiplication is the same as component-scalar multiplication`` (a : Vector3, f : float32) =
2017-05-31 18:08:41 +00:00
let r = f * a
Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y)
Assert.Equal(a.Z * f,r.Z)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static method Vector3-scalar multiplication is the same as component-scalar multiplication`` (a : Vector3, f : float32) =
let r = Vector3.Multiply(a, f)
2017-06-01 19:12:59 +00:00
Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y)
Assert.Equal(a.Z * f,r.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3-Matrix3 multiplication using right-handed notation is the same as vector/row multiplication and summation`` (a : Matrix3, b : Vector3) =
2017-05-31 18:08:41 +00:00
let res = a*b
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let c1 = b.X * a.M11 + b.Y * a.M12 + b.Z * a.M13
let c2 = b.X * a.M21 + b.Y * a.M22 + b.Z * a.M23
let c3 = b.X * a.M31 + b.Y * a.M32 + b.Z * a.M33
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let exp = Vector3(c1, c2, c3)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(exp, res)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3-Matrix3 multiplication using left-handed notation is the same as vector/column multiplication and summation`` (a : Matrix3, b : Vector3) =
let res = b*a
2017-06-01 19:12:59 +00:00
let c1 = b.X * a.M11 + b.Y * a.M21 + b.Z * a.M31
let c2 = b.X * a.M12 + b.Y * a.M22 + b.Z * a.M32
let c3 = b.X * a.M13 + b.Y * a.M23 + b.Z * a.M33
2017-06-01 19:12:59 +00:00
let exp = Vector3(c1, c2, c3)
2017-06-01 19:12:59 +00:00
Assert.Equal(exp, res)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3 multiplication method is the same as component multiplication`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X * b.X, a.Y * b.Y, a.Z * b.Z)
let sum = Vector3.Multiply(a, b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3 multiplication method by reference is the same as component multiplication`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X * b.X, a.Y * b.Y, a.Z * b.Z)
let sum = Vector3.Multiply(ref a, ref b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module Division =
2017-05-31 18:08:41 +00:00
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Vector3-float division is the same as component-float division`` (a : Vector3, f : float32) =
2017-05-31 18:08:41 +00:00
if not (approxEq f 0.0f) then // we don't support diving by zero.
let r = a / f
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.ApproximatelyEqual(a.X / f,r.X)
Assert.ApproximatelyEqual(a.Y / f,r.Y)
Assert.ApproximatelyEqual(a.Z / f,r.Z)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3-Vector3 division method is the same as component division`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X / b.X, a.Y / b.Y, a.Z / b.Z)
let sum = Vector3.Divide(a, b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3-Vector3 divison method by reference is the same as component division`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X / b.X, a.Y / b.Y, a.Z / b.Z)
let sum = Vector3.Divide(ref a, ref b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3-scalar division method is the same as component division`` (a : Vector3, b : float32) =
let v1 = Vector3(a.X / b, a.Y / b, a.Z / b)
let sum = Vector3.Divide(a, b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Static Vector3-scalar divison method by reference is the same as component division`` (a : Vector3, b : float32) =
let v1 = Vector3(a.X / b, a.Y / b, a.Z / b)
let sum = Vector3.Divide(ref a, b)
2017-06-01 19:12:59 +00:00
Assert.ApproximatelyEqual(v1, sum)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Negation =
//
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Vector negation operator negates all components`` (x, y, z) =
2017-05-31 18:08:41 +00:00
let v = Vector3(x, y, z)
let vNeg = -v
Assert.Equal(-x, vNeg.X)
Assert.Equal(-y, vNeg.Y)
Assert.Equal(-z, vNeg.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Equality =
//
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Vector equality operator is by component`` (x, y, z) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(x, y, z)
let v2 = Vector3(x, y, z)
let equality = v1 = v2
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(equality)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Vector inequality operator is by component`` (x, y, z) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(x, y, z)
2017-06-01 20:09:04 +00:00
let v2 = Vector3(x + 1.0f , y + 1.0f, z + 1.0f)
2017-05-31 18:08:41 +00:00
let inequality = v1 <> v2
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(inequality)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Vector equality method is by component`` (x, y, z) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(x, y, z)
let v2 = Vector3(x, y, z)
let notVector = Matrix2()
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let equality = v1.Equals(v2)
let inequalityByOtherType = v1.Equals(notVector)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(equality)
Assert.False(inequalityByOtherType)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Swizzling =
//
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Vector swizzling returns the correct composite for X-primary components`` (x, y, z) =
2017-05-31 18:08:41 +00:00
let v = Vector3(x, y, z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let xyz = Vector3(x, y, z)
let xzy = Vector3(x, z, y)
let xy = Vector2(x, y)
let xz = Vector2(x, z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(xyz, v);
Assert.Equal(xzy, v.Xzy);
Assert.Equal(xy, v.Xy);
Assert.Equal(xz, v.Xz);
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
[<Property>]
let ``Vector swizzling returns the correct composite for Y-primary components`` (x, y, z) =
let v = Vector3(x, y, z)
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
let yxz = Vector3(y, x, z)
let yzx = Vector3(y, z, x)
let yx = Vector2(y, x)
let yz = Vector2(y, z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(yxz, v.Yxz);
Assert.Equal(yzx, v.Yzx);
Assert.Equal(yx, v.Yx);
Assert.Equal(yz, v.Yz);
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
[<Property>]
let ``Vector swizzling returns the correct composite for Z-primary components`` (x, y, z) =
let v = Vector3(x, y, z)
2017-06-01 19:12:59 +00:00
2017-06-01 19:09:10 +00:00
let zxy = Vector3(z, x, y)
let zyx = Vector3(z, y, x)
let zx = Vector2(z, x)
let zy = Vector2(z, y);
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(zxy, v.Zxy);
Assert.Equal(zyx, v.Zyx);
Assert.Equal(zx, v.Zx);
Assert.Equal(zy, v.Zy);
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Interpolation =
//
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Linear interpolation is by component`` (a : Vector3, b : Vector3, q) =
2017-05-31 18:08:41 +00:00
let blend = q
2017-06-01 19:12:59 +00:00
let rX = blend * (b.X - a.X) + a.X
2017-05-31 18:08:41 +00:00
let rY = blend * (b.Y - a.Y) + a.Y
let rZ = blend * (b.Z - a.Z) + a.Z
let vExp = Vector3(rX, rY, rZ)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(vExp, Vector3.Lerp(a, b, q))
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vRes = Vector3.Lerp(ref a, ref b, q)
Assert.Equal(vExp, vRes)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Barycentric interpolation follows the barycentric formula`` (a : Vector3, b : Vector3, c : Vector3, u, v) =
2017-05-31 18:08:41 +00:00
let r = a + u * (b - a) + v * (c - a)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(r, Vector3.BaryCentric(a, b, c, u, v))
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vRes = Vector3.BaryCentric(ref a, ref b, ref c, u, v)
Assert.Equal(r, vRes)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Vector products`` =
//
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Dot product follows the dot product formula`` (a : Vector3, b : Vector3) =
2017-05-31 18:08:41 +00:00
let dot = a.X * b.X + a.Y * b.Y + a.Z * b.Z
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(dot, Vector3.Dot(a, b));
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vRes = Vector3.Dot(ref a, ref b)
Assert.Equal(dot, vRes)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Cross product follows the cross product formula`` (a : Vector3, b : Vector3) =
2017-05-31 18:08:41 +00:00
let crossX = a.Y * b.Z - a.Z * b.Y
let crossY = a.Z * b.X - a.X * b.Z
let crossZ = a.X * b.Y - a.Y * b.X
let cross = Vector3(crossX, crossY, crossZ)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(cross, Vector3.Cross(a, b));
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vRes = Vector3.Cross(ref a, ref b)
Assert.Equal(cross, vRes)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Component min and max`` =
//
[<Property>]
2017-06-01 19:09:10 +00:00
let ``ComponentMin produces a new vector from the smallest components of the given vectors`` (x, y, z, u, w, q) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vMin = Vector3.ComponentMin(v1, v2)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMin.X <= v1.X)
Assert.True(vMin.X <= v2.X)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMin.Y <= v1.Y)
Assert.True(vMin.Y <= v2.Y)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMin.Z <= v1.Z)
Assert.True(vMin.Z <= v2.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``ComponentMax producing a new vector from the largest components of the given vectors`` (x, y, z, u, w, q) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vMax = Vector3.ComponentMax(v1, v2)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMax.X >= v1.X)
Assert.True(vMax.X >= v2.X)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMax.Y >= v1.Y)
Assert.True(vMax.Y >= v2.Y)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMax.Z >= v1.Z)
Assert.True(vMax.Z >= v2.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``ComponentMin by reference produces a new vector from the smallest components of the given vectors`` (x, y, z, u, w, q) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vMin = Vector3.ComponentMin(ref v1, ref v2)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMin.X <= v1.X)
Assert.True(vMin.X <= v2.X)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMin.Y <= v1.Y)
Assert.True(vMin.Y <= v2.Y)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMin.Z <= v1.Z)
Assert.True(vMin.Z <= v2.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``ComponentMax produces a new vector from the smallest components of the given vectors`` (x, y, z, u, w, q) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vMax = Vector3.ComponentMax(ref v1, ref v2)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMax.X >= v1.X)
Assert.True(vMax.X >= v2.X)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMax.Y >= v1.Y)
Assert.True(vMax.Y >= v2.Y)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.True(vMax.Z >= v1.Z)
Assert.True(vMax.Z >= v2.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Min selects the vector with lesser magnitude given two vectors`` (x, y, z, u, w, q) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let l1 = v1.LengthSquared
let l2 = v2.LengthSquared
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vMin = Vector3.Min(v1, v2)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
if l1 < l2 then
2017-06-01 19:12:59 +00:00
let equalsFirst = vMin = v1
2017-05-31 18:08:41 +00:00
Assert.True(equalsFirst)
2017-06-01 19:12:59 +00:00
else
let equalsLast = vMin = v2
Assert.True(equalsLast)
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Max selects the vector with greater magnitude given two vectors`` (x, y, z, u, w, q) =
2017-05-31 18:08:41 +00:00
let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let l1 = v1.LengthSquared
let l2 = v2.LengthSquared
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let vMin = Vector3.Max(v1, v2)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
if l1 >= l2 then
2017-06-01 19:12:59 +00:00
let equalsFirst = vMin = v1
2017-05-31 18:08:41 +00:00
Assert.True(equalsFirst)
2017-06-01 19:12:59 +00:00
else
let equalsLast = vMin = v2
Assert.True(equalsLast)
2017-05-31 18:08:41 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Clamping =
//
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Clamping one vector between two other vectors clamps all components between corresponding components`` (a : Vector3, b : Vector3, w : Vector3) =
2017-05-31 18:08:41 +00:00
let res = Vector3.Clamp(w, a, b)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X
let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y
let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(expX, res.X)
Assert.Equal(expY, res.Y)
Assert.Equal(expZ, res.Z)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
[<Property>]
2017-06-01 19:09:10 +00:00
let ``Clamping one vector between two other vectors by reference clamps all components between corresponding components`` (a : Vector3, b : Vector3, w : Vector3) =
2017-05-31 18:08:41 +00:00
let res = Vector3.Clamp(ref w, ref a, ref b)
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X
let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y
let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z
2017-06-01 19:12:59 +00:00
2017-05-31 18:08:41 +00:00
Assert.Equal(expX, res.X)
Assert.Equal(expY, res.Y)
Assert.Equal(expZ, res.Z)
2017-06-01 19:12:59 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module ``Unit vectors``=
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Unit X is correct`` =
2017-06-01 20:09:04 +00:00
let unitX = Vector3(1.0f, 0.0f, 0.0f)
2017-06-01 19:12:59 +00:00
Assert.Equal(Vector3.UnitX, unitX)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Unit Y is correct`` =
2017-06-01 20:09:04 +00:00
let unitY = Vector3(0.0f, 1.0f, 0.0f)
2017-06-01 19:12:59 +00:00
Assert.Equal(Vector3.UnitY, unitY)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Unit Z is correct`` =
2017-06-01 20:09:04 +00:00
let unitZ = Vector3(0.0f, 0.0f, 1.0f)
2017-06-01 19:12:59 +00:00
Assert.Equal(Vector3.UnitZ, unitZ)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Unit zero is correct`` =
2017-06-01 20:09:04 +00:00
let unitZero = Vector3(0.0f, 0.0f, 0.0f)
2017-06-01 19:12:59 +00:00
Assert.Equal(Vector3.Zero, unitZero)
2017-06-01 19:12:59 +00:00
[<Property>]
2017-06-01 19:12:59 +00:00
let ``Unit one is correct`` =
let unitOne = Vector3(1.0f, 1.0f, 1.0f)
2017-06-01 19:12:59 +00:00
2017-06-01 14:05:02 +00:00
Assert.Equal(Vector3.One, unitOne)
2017-06-01 19:12:59 +00:00
2017-06-01 14:05:02 +00:00
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 19:12:59 +00:00
module Serialization =
2017-06-01 14:05:02 +00:00
//
[<Property>]
2017-06-01 19:12:59 +00:00
let ``The absolute size of a Vector3 is always the size of its components`` (v : Vector3) =
2017-06-01 14:05:02 +00:00
let expectedSize = sizeof<float32> * 3
2017-06-01 19:12:59 +00:00
2017-06-01 14:05:02 +00:00
Assert.Equal(expectedSize, Vector3.SizeInBytes)
Assert.Equal(expectedSize, Marshal.SizeOf(Vector3()))
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Transformation =
//
[<Property>]
let ``Transformation by quaternion is the same as multiplication by quaternion and its conjugate`` (v : Vector3, q : Quaternion) =
let vectorQuat = Quaternion(v.X, v.Y, v.Z, 0.0f)
let inverse = Quaternion.Invert(q)
let transformedQuat = q * vectorQuat * inverse
2017-06-01 20:09:04 +00:00
let transformedVector = transformedQuat.Xyz
Assert.ApproximatelyEqual(transformedVector, Vector3.Transform(v, q))
[<Property>]
let ``Transformation by quaternion by reference is the same as multiplication by quaternion and its conjugate`` (v : Vector3, q : Quaternion) =
let vectorQuat = Quaternion(v.X, v.Y, v.Z, 0.0f)
let inverse = Quaternion.Invert(q)
let transformedQuat = q * vectorQuat * inverse
2017-06-01 20:09:04 +00:00
let transformedVector = transformedQuat.Xyz
Assert.ApproximatelyEqual(transformedVector, Vector3.Transform(ref v, ref q))
[<Property>]
let ``Transformation by quaternion by multiplication using right-handed notation is the same as multiplication by quaternion and its conjugate`` (v : Vector3, q : Quaternion) =
let vectorQuat = Quaternion(v.X, v.Y, v.Z, 0.0f)
let inverse = Quaternion.Invert(q)
let transformedQuat = q * vectorQuat * inverse
let transformedVector = transformedQuat.Xyz
Assert.ApproximatelyEqual(transformedVector, q * v)
[<Property>]
let ``Transformation by identity quaternion does not alter vector`` (v : Vector3) =
let q = Quaternion.Identity
let vectorQuat = Quaternion(v.X, v.Y, v.Z, 0.0f)
let inverse = Quaternion.Invert(q)
let transformedQuat = q * vectorQuat * inverse
let transformedVector = transformedQuat.Xyz
Assert.ApproximatelyEqual(v, transformedVector)
Assert.ApproximatelyEqual(v, Vector3.Transform(v, q))
Assert.ApproximatelyEqual(transformedVector, Vector3.Transform(v, q))