9 people like it.

XNA's Vector3 with units of measure

A vector type with units of measure built on top of XNA's Vector3. Not complete, the point is mainly to show how to use generic units of measure to adapt an existing type.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
open Microsoft.Xna.Framework

/// A three-dimensional vector with a unit of measure. Built on top of Xna's Vector3.
type TypedVector3<[<Measure>] 'M> =
    struct
        val v : Vector3
        new(x : float32<'M>, y : float32<'M>, z : float32<'M>) =
            { v = Vector3(float32 x, float32 y, float32 z) }
        new(V) = { v = V }
    end

[<RequireQualifiedAccessAttribute>]
module TypedVector =
    let add3 (U : TypedVector3<'M>, V : TypedVector3<'M>) =
        new TypedVector3<'M>(U.v + V.v)

    let sub3 (U : TypedVector3<'M>, V : TypedVector3<'M>) =
        new TypedVector3<'M>(U.v - V.v)

    let dot3 (U : TypedVector3<'M>, V : TypedVector3<'N>) =
        Vector3.Dot(U.v, V.v)
        |> LanguagePrimitives.Float32WithMeasure<'M 'N>

    let len3 (U : TypedVector3<'M>) =
        LanguagePrimitives.Float32WithMeasure<'M> (U.v.Length())

    let scale3 (k : float32<'K>, U : TypedVector3<'M>) : TypedVector3<'K 'M> =
        let conv = LanguagePrimitives.Float32WithMeasure<'K 'M>
        let v = Vector3.Multiply(U.v, float32 k)
        new TypedVector3<_>(conv v.X, conv v.Y, conv v.Z)

    let normalize3 (U : TypedVector3<'M>) =
        let len = len3 U
        scale3 ((1.0f / len), U)

type TypedVector3<[<Measure>] 'M>
with
    static member public (*) (k, U) = TypedVector.scale3 (k, U)
    static member public (+) (U, V) = TypedVector.add3 (U, V)
    static member public (-) (U, V) = TypedVector.sub3 (U, V)
    member public this.Length = this |> TypedVector.len3
namespace Microsoft
Multiple items
type TypedVector3<'M> =
  struct
    new : V:obj -> TypedVector3<'M>
    new : x:float32<'M> * y:float32<'M> * z:float32<'M> -> TypedVector3<'M>
    val v: obj
    member Length : float32<'M>
    static member ( + ) : U:TypedVector3<'u> * V:TypedVector3<'u> -> TypedVector3<'u>
    static member ( * ) : k:float32<'u> * U:TypedVector3<'v> -> TypedVector3<'u 'v>
    static member ( - ) : U:TypedVector3<'u> * V:TypedVector3<'u> -> TypedVector3<'u>
  end

Full name: Script.TypedVector3<_>


 A three-dimensional vector with a unit of measure. Built on top of Xna's Vector3.


--------------------
TypedVector3()
new : V:obj -> TypedVector3<'M>
new : x:float32<'M> * y:float32<'M> * z:float32<'M> -> TypedVector3<'M>
Multiple items
type MeasureAttribute =
  inherit Attribute
  new : unit -> MeasureAttribute

Full name: Microsoft.FSharp.Core.MeasureAttribute

--------------------
new : unit -> MeasureAttribute
TypedVector3.v: obj
val x : float32<'M>
Multiple items
val float32 : value:'T -> float32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float32

--------------------
type float32 = System.Single

Full name: Microsoft.FSharp.Core.float32

--------------------
type float32<'Measure> = float32

Full name: Microsoft.FSharp.Core.float32<_>
val y : float32<'M>
val z : float32<'M>
val V : obj
Multiple items
type RequireQualifiedAccessAttribute =
  inherit Attribute
  new : unit -> RequireQualifiedAccessAttribute

Full name: Microsoft.FSharp.Core.RequireQualifiedAccessAttribute

--------------------
new : unit -> RequireQualifiedAccessAttribute
val add3 : U:TypedVector3<'M> * V:TypedVector3<'M> -> TypedVector3<'M>

Full name: Script.TypedVector.add3
val U : TypedVector3<'M>
val V : TypedVector3<'M>
val sub3 : U:TypedVector3<'M> * V:TypedVector3<'M> -> TypedVector3<'M>

Full name: Script.TypedVector.sub3
val dot3 : U:TypedVector3<'M> * V:TypedVector3<'N> -> float32<'M 'N>

Full name: Script.TypedVector.dot3
val V : TypedVector3<'N>
module LanguagePrimitives

from Microsoft.FSharp.Core
val Float32WithMeasure : float32 -> float32<'Measure>

Full name: Microsoft.FSharp.Core.LanguagePrimitives.Float32WithMeasure
val len3 : U:TypedVector3<'M> -> float32<'M>

Full name: Script.TypedVector.len3
val scale3 : k:float32<'K> * U:TypedVector3<'M> -> TypedVector3<'K 'M>

Full name: Script.TypedVector.scale3
val k : float32<'K>
val conv : (float32 -> float32<'K 'M>)
val v : obj
val normalize3 : U:TypedVector3<'M> -> TypedVector3<1>

Full name: Script.TypedVector.normalize3
val len : float32<'M>
val k : float32<'u>
val U : TypedVector3<'v>
module TypedVector

from Script
val U : TypedVector3<'u>
val V : TypedVector3<'u>
val this : byref<TypedVector3<'M>>
member TypedVector3.Length : float32<'M>

Full name: Script.TypedVector3.Length

More information

Link:http://fssnip.net/9H
Posted:7 years ago
Author:Johann Deneux
Tags: math , xna , units of measure