Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
 Summary Activity SCM

# SCM Repository

[sml3d] Diff of /trunk/sml3d/src/base/common/vec4d.sml
 [sml3d] / trunk / sml3d / src / base / common / vec4d.sml

# Diff of /trunk/sml3d/src/base/common/vec4d.sml

revision 725, Fri Jan 22 02:27:14 2010 UTC revision 726, Fri Jan 22 02:37:06 2010 UTC
# Line 1  Line 1
1  (* vec4d.sml  (* vec4.sml
2   *   *
3   * COPYRIGHT (c) 2008 John Reppy (http://www.cs.uchicago.edu/~jhr)   * COPYRIGHT (c) 2008 John Reppy (http://www.cs.uchicago.edu/~jhr)
5   *   *
6   * Operations on vectors in R^4 (double version).   * Operations on vectors in R^4 (flt version).
7   *)   *)
8
9  structure Vec4d : sig  structure Vec4d :> VEC4 where type flt = Double.flt
10      = struct
11
12      type double = FP.double      structure Flt = Double
13
14      type vec4d = {x : double, y : double, z : double, w : double}      type flt = Flt.flt
15
16      val unpack : vec4d -> (double * double * double * double)      val epsilon : flt = Flt.epsilon
val pack   : (double * double * double * double) -> vec4d
val toList : vec4d -> double list
val toVec  : vec4d -> double vector
17
18    (* functional update *)      type vec4 = flt SML3dTypes.vec4
val setX : vec4d * double -> vec4d
val setY : vec4d * double -> vec4d
val setZ : vec4d * double -> vec4d
val setW : vec4d * double -> vec4d

val nth : vec4d * int -> double

val zero : vec4d

val toString : vec4d -> string

val neg : vec4d -> vec4d
val add : (vec4d * vec4d) -> vec4d
val sub : (vec4d * vec4d) -> vec4d
val mul : (vec4d * vec4d) -> vec4d

val scale : (double * vec4d) -> vec4d

(* adds (u, s, v) = u + s*v *)
val adds : (vec4d * double * vec4d) -> vec4d

(* lerp (u, t, v) = (1-t)*u + t*v; we assume that 0 <= t <= 1 *)
val lerp : (vec4d * double * vec4d) -> vec4d

val dot : (vec4d * vec4d) -> double
val normalize : vec4d -> vec4d
val length : vec4d -> double
val lengthSq : vec4d -> double                      (* length squared *)
val lengthAndDir : vec4d -> (double * vec4d)        (* length and normal vector *)
val distanceSq : (vec4d * vec4d) -> double          (* distance squared *)

(* iterators *)
val app  : (double -> unit) -> vec4d -> unit
val map  : (double -> 'a) -> vec4d -> {x : 'a, y : 'a, z : 'a, w : 'a}
val map2 : (double * double -> 'a) -> (vec4d * vec4d) -> {x : 'a, y : 'a, z : 'a, w : 'a}

end  = struct

structure Math = Real64.Math

type double = FP.double

val epsilon : double = 1.0e~5

type vec4d = double SML3dTypes.vec4
19
20      open Vec4      open Vec4
21
22      fun toVec v = Vector.fromList(toList v)      fun toVec v = Vector.fromList(toList v)
23
24      val zero : vec4d = {x=0.0, y = 0.0, z = 0.0, w = 0.0}      val zero : vec4 = {x=0.0, y = 0.0, z = 0.0, w = 0.0}
25
26      fun toString ({x, y, z, w} : vec4d) = concat[      fun toString ({x, y, z, w} : vec4) = concat[
27              "<", Real64.toString x, ",", Real64.toString y, ",", Real64.toString z,              "<", Flt.toString x, ",", Flt.toString y, ",", Flt.toString z,
28              Real64.toString w, ">"              Flt.toString w, ">"
29            ]            ]
30
31      fun neg ({x, y, z, w} : vec4d) = {x = ~x, y = ~y, z = ~z, w = ~w}      fun neg ({x, y, z, w} : vec4) = {x = ~x, y = ~y, z = ~z, w = ~w}
32
33      fun add ({x=x1, y=y1, z=z1, w=w1} : vec4d, {x=x2, y=y2, z=z2, w=w2}) =      fun add ({x=x1, y=y1, z=z1, w=w1} : vec4, {x=x2, y=y2, z=z2, w=w2}) =
34            {x=x1+x2, y=y1+y2, z=z1+z2, w=w1+w2}            {x=x1+x2, y=y1+y2, z=z1+z2, w=w1+w2}
35
36      fun sub ({x=x1, y=y1, z=z1, w=w1} : vec4d, {x=x2, y=y2, z=z2, w=w2}) =      fun sub ({x=x1, y=y1, z=z1, w=w1} : vec4, {x=x2, y=y2, z=z2, w=w2}) =
37            {x=x1-x2, y=y1-y2, z=z1-z2, w=w1-w2}            {x=x1-x2, y=y1-y2, z=z1-z2, w=w1-w2}
38
39      fun mul ({x=x1, y=y1, z=z1, w=w1} : vec4d, {x=x2, y=y2, z=z2, w=w2}) =      fun mul ({x=x1, y=y1, z=z1, w=w1} : vec4, {x=x2, y=y2, z=z2, w=w2}) =
40            {x=x1*x2, y=y1*y2, z=z1*z2, w=w1*w2}            {x=x1*x2, y=y1*y2, z=z1*z2, w=w1*w2}
41
42      fun scale (s, {x, y, z, w} : vec4d) = {x = s*x, y = s*y, z = s*z, w = s*w}      fun scale (s, {x, y, z, w} : vec4) = {x = s*x, y = s*y, z = s*z, w = s*w}
43
44      fun adds (v1, s, v2) = add(v1, scale(s, v2))      fun adds (v1, s, v2) = add(v1, scale(s, v2))
45
46      fun lerp (v1, t, v2) = add(scale(1.0-t, v1), scale(t, v2))      fun lerp (v1, t, v2) = add(scale(1.0-t, v1), scale(t, v2))
47
48      fun dot ({x=x1, y=y1, z=z1, w=w1} : vec4d, {x=x2, y=y2, z=z2, w=w2}) =      fun dot ({x=x1, y=y1, z=z1, w=w1} : vec4, {x=x2, y=y2, z=z2, w=w2}) =
49            (x1*x2 + y1*y2 + z1*z2 + w1*w2)            (x1*x2 + y1*y2 + z1*z2 + w1*w2)
50
51      fun lengthSq (v : vec4d) = dot(v, v)      fun lengthSq (v : vec4) = dot(v, v)
52
53      fun length v = Math.sqrt(lengthSq v)      fun length v = Flt.sqrt(lengthSq v)
54
55      fun distanceSq (u : vec4d, v) = lengthSq (sub (u, v))      fun distanceSq (u : vec4, v) = lengthSq (sub (u, v))
56
57      fun lengthAndDir (v as {x, y, z, w}) = let      fun lengthAndDir (v as {x, y, z, w}) = let
58            val l = length v            val l = length v

Legend:
 Removed from v.725 changed lines Added in v.726

 root@smlnj-gforge.cs.uchicago.edu ViewVC Help Powered by ViewVC 1.0.0