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

# SCM Repository

[sml3d] View of /src/common/vec4f.sml
 [sml3d] / src / common / vec4f.sml

# View of /src/common/vec4f.sml

Thu Mar 13 14:29:38 2008 UTC (11 years, 9 months ago) by jhr
File size: 3084 byte(s)
```  Merged math library into common; fixed type errors in ONB code
```
```(* vec4f.sml
*
* COPYRIGHT (c) 2008 John Reppy (http://www.cs.uchicago.edu/~jhr)
*
* Operations on vectors in R^4 (float version).
*)

structure Vec4f : sig

type float = FP.float

type vec4f = {x : float, y : float, z : float, w : float}

val unpack : vec4f -> (float * float * float * float)
val pack   : (float * float * float * float) -> vec4f
val toList : vec4f -> float list
val toVec  : vec4f -> float vector

(* functional update *)
val setX : vec4f * float -> vec4f
val setY : vec4f * float -> vec4f
val setZ : vec4f * float -> vec4f
val setW : vec4f * float -> vec4f

val nth : vec4f * int -> float

val zero : vec4f

val toString : vec4f -> string

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

val scale : (float * vec4f) -> vec4f

(* adds (u, s, v) = u + s*v *)
val adds : (vec4f * float * vec4f) -> vec4f

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

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

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

end  = struct

structure Math = Real32.Math

type float = FP.float

val epsilon : float = 1.0e~5

type vec4f = float SML3dTypes.vec4

open Vec4

fun toVec v = Vector.fromList(toList v)

val zero : vec4f = {x=0.0, y = 0.0, z = 0.0, w = 0.0}

fun toString ({x, y, z, w} : vec4f) = concat[
"<", Real32.toString x, ",", Real32.toString y, ",", Real32.toString z,
Real32.toString w, ">"
]

fun neg ({x, y, z, w} : vec4f) = {x = ~x, y = ~y, z = ~z, w = ~w}

fun add ({x=x1, y=y1, z=z1, w=w1} : vec4f, {x=x2, y=y2, z=z2, w=w2}) =
{x=x1+x2, y=y1+y2, z=z1+z2, w=w1+w2}

fun sub ({x=x1, y=y1, z=z1, w=w1} : vec4f, {x=x2, y=y2, z=z2, w=w2}) =
{x=x1-x2, y=y1-y2, z=z1-z2, w=w1-w2}

fun mul ({x=x1, y=y1, z=z1, w=w1} : vec4f, {x=x2, y=y2, z=z2, w=w2}) =
{x=x1*x2, y=y1*y2, z=z1*z2, w=w1*w2}

fun scale (s, {x, y, z, w} : vec4f) = {x = s*x, y = s*y, z = s*z, w = s*w}

fun lerp (v1, t, v2) = add(scale(1.0-t, v1), scale(t, v2))

fun dot ({x=x1, y=y1, z=z1, w=w1} : vec4f, {x=x2, y=y2, z=z2, w=w2}) =
(x1*x2 + y1*y2 + z1*z2 + w1*w2)

fun lengthSq (v : vec4f) = dot(v, v)

fun length v = Math.sqrt(lengthSq v)

fun distanceSq (u : vec4f, v) = lengthSq (sub (u, v))

fun lengthAndDir (v as {x, y, z, w}) = let
val l = length v
in
if (l > epsilon)
then (l, scale(1.0 / l, v))
else (l, v)
end

fun normalize v = #2(lengthAndDir v)

end
```