27 people like it.

Triple (3D Vector)

This class is a 3D vector representation. There is a module called Triple with operations on the triple 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: 
42: 
[<AutoOpen>]
module Triple

type triple(x: float, y: float, z: float) =
  member o.X = x
  member o.Y = y
  member o.Z = z
  
  new (x: int, y: int, z: int) = triple(float x, float y, float z)
  (A lot of code omitted :) )

module Triple =
  let toArray (t: triple) = [| t.X; t.Y; t.Z |]
  let toList (t: triple) = [t.X; t.Y; t.Z]
  let toTuple (t: triple) = t.X, t.Y, t.Z
  let dot (a: triple) (b: triple) = a.X * b.X + a.Y * b.Y + a.Z * b.Z
  let outer (a: triple) (b: triple) = 
    array2D [|  [| a.X * b.X; a.X * b.Y; a.X * b.Z |];
                [| a.Y * b.X; a.Y * b.Y; a.Y * b.Z |];
                [| a.Z * b.X; a.Z * b.Y; a.Z * b.Z |] |]
  let norm (t: triple) = sqrt (t.X * t.X + t.Y * t.Y + t.Z * t.Z)
  let norm2 (t: triple) = t.X * t.X + t.Y * t.Y + t.Z * t.Z
  let cross (a: triple) (b: triple) = triple(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X)    
  let map2 (fn: float->float->float) (a: triple) (b: triple) = triple(fn a.X b.X, fn a.Y b.Y, fn a.Z b.Z)
  let Zero = triple(0., 0., 0.)
  let Ones = triple(1.,1.,1.)
  let create (n: float) = triple(n,n,n)
  let exists fn (el: triple) =
    if fn el.X then true
    elif fn el.Y then true
    elif fn el.Z then true
    else false
  let map (fn: float->float) (el: triple) = triple(fn el.X, fn el.Y, fn el.Z)
  let normalize (t: triple) = t / (norm t)
  let init (fn: int->float) = triple(fn 0, fn 1, fn 2)

// usage
let tr1 = triple(5,2,3)
let tr2 = triple(5,2,3)
let v1 = (-tr1 + tr2 - tr1) .* tr2 ./ tr1 .** 2. * tr1 + tr1.[0] + tr2.X
let v2 = tr1.norm + Triple.norm2 tr2
// ... etc
Multiple items
type AutoOpenAttribute =
  inherit Attribute
  new : unit -> AutoOpenAttribute
  new : path:string -> AutoOpenAttribute
  member Path : string

Full name: Microsoft.FSharp.Core.AutoOpenAttribute

--------------------
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
Multiple items
module Triple

from Triple

--------------------
module Triple
Multiple items
type triple =
  new : x:float * y:float * z:float -> triple
  new : x:int * y:int * z:int -> triple
  override Equals : ob:obj -> bool
  override GetHashCode : unit -> int
  member Item : idx:int -> float
  override ToString : unit -> string
  member X : float
  member Y : float
  member Z : float
  member norm : float
  ...

Full name: Triple.triple

--------------------
new : x:int * y:int * z:int -> triple
new : x:float * y:float * z:float -> triple
val x : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

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

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val y : float
val z : float
val o : triple
member triple.X : float

Full name: Triple.triple.X
member triple.Y : float

Full name: Triple.triple.Y
member triple.Z : float

Full name: Triple.triple.Z
val x : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val y : int
val z : int
static member ( * ) (a, b: triple) = triple(a * b.X, a * b.Y, a * b.Z)
  static member ( * ) (a, b: triple) =
    let af = float a
    triple(af * b.X, af * b.Y, af * b.Z)
  static member ( * ) (a: triple, b) = triple(b * a.X, b * a.Y, b * a.Z)
  static member ( * ) (a: triple, b) =
    let bf = float b
    triple(bf * a.X, bf * a.Y, bf * a.Z)
  static member ( * ) (a: triple, b: triple) = a.X * b.X + a.Y * b.Y + a.Z * b.Z
  static member ( .* ) (a: triple, b: triple) = triple(a.X * b.X, a.Y * b.Y, a.Z * b.Z)
  static member ( * ) (a: float [,], b: triple) = triple(a.[0,0] * b.X + a.[0,1] * b.Y + a.[0,2] * b.Z, a.[1,0] * b.X + a.[1,1] * b.Y + a.[1,2] * b.Z, a.[2,0] * b.X + a.[2,1] * b.Y + a.[2,2] * b.Z)

  static member ( / ) (a, b: triple) = triple(a / b.X, a / b.Y, a / b.Z)
  static member ( / ) (a, b: triple) =
    let af = float a
    triple(af / b.X, af / b.Y, af / b.Z)
  static member ( / ) (a: triple, b) = triple(a.X / b, a.Y / b, a.Z / b)
  static member ( / ) (a: triple, b) =
    let bf = float b
    triple(a.X / bf, a.Y / bf, a.Z / bf)
  static member ( ./ ) (a: triple, b: triple) = triple(a.X / b.X, a.Y / b.Y, a.Z / b.Z)

  static member (+) (a, b: triple) = triple(a + b.X, a + b.Y, a + b.Z)
  static member (+) (a, b: triple) =
    let af = float a
    triple(af + b.X, af + b.Y, af + b.Z)
  static member (+) (a: triple, b) = triple(b + a.X, b + a.Y, b + a.Z)
  static member (+) (a: triple, b) =
    let bf = float b
    triple(bf + a.X, bf + a.Y, bf + a.Z)
  static member (+) (a: triple, b: triple) = triple(a.X + b.X, a.Y + b.Y, a.Z + b.Z)

  static member (-) (a, b: triple) = triple(a - b.X, a - b.Y, a - b.Z)
  static member (-) (a, b: triple) =
    let af = float a
    triple(af - b.X, af - b.Y, af - b.Z)
  static member (-) (a: triple, b) = triple(a.X - b, a.Y - b, a.Z - b)
  static member (-) (a: triple, b) =
    let bf = float b
    triple(a.X - bf, a.Y - bf, a.Z - bf)
  static member (-) (a: triple, b: triple) = triple(a.X - b.X, a.Y - b.Y, a.Z - b.Z)

  static member (.**) (a: triple, b) = triple(a.X**b, a.Y**b, a.Z**b)

  static member (~-) (a: triple) = triple(-a.X, -a.Y, -a.Z)

  static member toTriple (x: int, y, z) = triple(x, y, z)
  static member toTriple (x: int list) = triple(x.[0], x.[1], x.[2])
  static member get_Zero() = triple (0.,0.,0.)
  member o.toTuple = x, y, z
  member o.toList = [x;y;z]
  member o.toArray = [|x;y;z|]
  member o.norm = sqrt (o.X * o.X + o.Y * o.Y + o.Z * o.Z)

  member o.Item (idx: int) =
    match idx with
      | 0 -> x
      | 1 -> y
      | 2 -> z
      | _ -> sprintf "invalid index %d in triple" idx |> failwith
  
  override o.ToString() = sprintf "[%f; %f; %f]" x y z

  override o.Equals(ob : obj) =
    match ob with
    | :? triple as other -> other.X = o.X && other.Y = o.Y && other.Z = o.Z
    | _ -> false
  override o.GetHashCode() =
    let hash = 23.
    let hash = hash * 31. + o.X
    let hash = hash * 31. + o.Y
    int (hash * 31. + o.Z)
val toArray : t:triple -> float []

Full name: Triple.Triple.toArray
val t : triple
property triple.X: float
property triple.Y: float
property triple.Z: float
val toList : t:triple -> float list

Full name: Triple.Triple.toList
val toTuple : t:triple -> float * float * float

Full name: Triple.Triple.toTuple
val dot : a:triple -> b:triple -> float

Full name: Triple.Triple.dot
val a : triple
val b : triple
val outer : a:triple -> b:triple -> float [,]

Full name: Triple.Triple.outer
val array2D : rows:seq<#seq<'T>> -> 'T [,]

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.array2D
val norm : t:triple -> float

Full name: Triple.Triple.norm
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
val norm2 : t:triple -> float

Full name: Triple.Triple.norm2
val cross : a:triple -> b:triple -> triple

Full name: Triple.Triple.cross
val map2 : fn:(float -> float -> float) -> a:triple -> b:triple -> triple

Full name: Triple.Triple.map2
val fn : (float -> float -> float)
val Zero : triple

Full name: Triple.Triple.Zero
val Ones : triple

Full name: Triple.Triple.Ones
val create : n:float -> triple

Full name: Triple.Triple.create
val n : float
val exists : fn:(float -> bool) -> el:triple -> bool

Full name: Triple.Triple.exists
val fn : (float -> bool)
val el : triple
val map : fn:(float -> float) -> el:triple -> triple

Full name: Triple.Triple.map
val fn : (float -> float)
val normalize : t:triple -> triple

Full name: Triple.Triple.normalize
val init : fn:(int -> float) -> triple

Full name: Triple.Triple.init
val fn : (int -> float)
val tr1 : triple

Full name: Triple.tr1
val tr2 : triple

Full name: Triple.tr2
val v1 : float

Full name: Triple.v1
val v2 : float

Full name: Triple.v2
property triple.norm: float
module Triple

from Triple
Raw view Test code New version

More information

Link:http://fssnip.net/X
Posted:13 years ago
Author:Oldrich Svec
Tags: triple , 3d vector