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

SCM Repository

[sml3d] View of /trunk/sml3d/src/sml3d/base/vec4f.sml
ViewVC logotype

View of /trunk/sml3d/src/sml3d/base/vec4f.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1260 - (download) (annotate)
Tue Jan 17 14:01:50 2012 UTC (7 years, 8 months ago) by jhr
File size: 1507 byte(s)
  Working on new SML3d core library
(* vec4f.sml
 *
 * COPYRIGHT (c) 2012 The SML3d Project (http://sml3d.cs.uchicago.edu)
 * All rights reserved.
 *
 * Operations on vectors in R^4 (float version).
 *)


structure Vec4f :> VEC4 where type flt = Float.flt
  = struct

    structure Flt = Float

    type flt = Flt.flt

    val epsilon : flt = Flt.epsilon

    type vec4 = flt SML3dTypes.vec4

    open Vec4

    val zero : vec4 = (0.0, 0.0, 0.0, 0.0)

    fun toString ((x, y, z, w) : vec4) = concat[
	    "<", Flt.toString x, ",", Flt.toString y, ",", Flt.toString z,
	    Flt.toString w, ">"
	  ]

    fun neg ((x, y, z, w) : vec4) = (~x, ~y, ~z, ~w)

    fun add ((x1, y1, z1, w1) : vec4, (x2, y2, z2, w2)) =
	  (x1+x2, y1+y2, z1+z2, w1+w2)

    fun sub ((x1, y1, z1, w1) : vec4, (x2, y2, z2, w2)) =
	  (x1-x2, y1-y2, z1-z2, w1-w2)

    fun mul ((x1, y1, z1, w1) : vec4, (x2, y2, z2, w2)) =
	  (x1*x2, y1*y2, z1*z2, w1*w2)

    fun scale (s, (x, y, z, w) : vec4) = (s*x, s*y, s*z, s*w)

    fun adds (v1, s, v2) = add(v1, scale(s, v2))

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

    fun dot ((x1, y1, z1, w1) : vec4, (x2, y2, z2, w2)) =
	  (x1*x2 + y1*y2 + z1*z2 + w1*w2)

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

    fun length v = Flt.sqrt(lengthSq v)

    fun distanceSq (u : vec4, 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

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