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

SCM Repository

[sml3d] Diff of /trunk/sml3d/src/sml3d/base/vec3d.sml
ViewVC logotype

Diff of /trunk/sml3d/src/sml3d/base/vec3d.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1259, Mon Jan 16 16:01:49 2012 UTC revision 1260, Tue Jan 17 14:01:50 2012 UTC
# Line 16  Line 16 
16      val epsilon = Flt.epsilon      val epsilon = Flt.epsilon
17      val slerpTolerance = 0.999      val slerpTolerance = 0.999
18    
19      type vec3 = {x : flt, y : flt, z : flt}      type vec3 = flt SML3dTypes.vec3
20        type vec4 = flt SML3dTypes.vec4
     type vec4 = {x : flt, y : flt, z : flt, w : flt}  
21    
22      open Vec3      open Vec3
23    
24    (* lift a 3D vector into homogeneous space *)    (* lift a 3D vector into homogeneous space *)
25      fun vector {x, y, z} : vec4 = {x=x, y=y, z=z, w=0.0}      fun vector (x, y, z) : vec4 = (x, y, z, 0.0)
26      fun point {x, y, z} : vec4 = {x=x, y=y, z=z, w=1.0}      fun point (x, y, z) : vec4 = (x, y, z, 1.0)
27    
28      val zero : vec3 = {x=0.0, y = 0.0, z = 0.0}      val zero : vec3 = (0.0, 0.0, 0.0)
29    
30      val e1 : vec3 = {x = 1.0, y = 0.0, z = 0.0}      val e1 : vec3 = (1.0, 0.0, 0.0)
31      val e2 : vec3 = {x = 0.0, y = 1.0, z = 0.0}      val e2 : vec3 = (0.0, 1.0, 0.0)
32      val e3 : vec3 = {x = 0.0, y = 0.0, z = 1.0}      val e3 : vec3 = (0.0, 0.0, 1.0)
33    
34      fun toString ({x, y, z} : vec3) = concat[      fun toString ((x, y, z) : vec3) = concat[
35              "<", Flt.toString x, ",", Flt.toString y, ",", Flt.toString z, ">"              "<", Flt.toString x, ",", Flt.toString y, ",", Flt.toString z, ">"
36            ]            ]
37    
38      fun neg ({x, y, z} : vec3) = {x = ~x, y = ~y, z = ~z}      fun neg ((x, y, z) : vec3) = (~x, ~y, ~z)
39    
40      fun add ({x=x1, y=y1, z=z1} : vec3, {x=x2, y=y2, z=z2}) =      fun add ((x1, y1, z1) : vec3, (x2, y2, z2)) = (x1+x2, y1+y2, z1+z2)
           {x=x1+x2, y=y1+y2, z=z1+z2}  
41    
42      fun sub ({x=x1, y=y1, z=z1} : vec3, {x=x2, y=y2, z=z2}) =      fun sub ((x1, y1, z1) : vec3, (x2, y2, z2)) = (x1-x2, y1-y2, z1-z2)
           {x=x1-x2, y=y1-y2, z=z1-z2}  
43    
44      fun mul ({x=x1, y=y1, z=z1} : vec3, {x=x2, y=y2, z=z2}) =      fun mul ((x1, y1, z1) : vec3, (x2, y2, z2)) = (x1*x2, y1*y2, z1*z2)
           {x=x1*x2, y=y1*y2, z=z1*z2}  
45    
46      fun scale (s, {x, y, z} : vec3) = {x = s*x, y = s*y, z = s*z}      fun scale (s, (x, y, z) : vec3) = (s*x, s*y, s*z)
47    
48      fun adds (v1, s, v2) = add(v1, scale(s, v2))      fun adds (v1, s, v2) = add(v1, scale(s, v2))
49    
50      fun abs {x, y, z} = {x = Flt.abs x, y = Flt.abs y, z = Flt.abs z}      fun abs (x, y, z) = (Flt.abs x, Flt.abs y, Flt.abs z)
51      fun min ({x=x1, y=y1, z=z1}, {x=x2, y=y2, z=z2}) =      fun min ((x1, y1, z1), (x2, y2, z2)) = (Flt.min(x1, x2), Flt.min(y1, y2), Flt.min(z1, z2))
52            {x = Flt.min(x1, x2), y = Flt.min(y1, y2), z = Flt.min(z1, z2)}      fun max ((x1, y1, z1), (x2, y2, z2)) = (Flt.max(x1, x2), Flt.max(y1, y2), Flt.max(z1, z2))
     fun max ({x=x1, y=y1, z=z1}, {x=x2, y=y2, z=z2}) =  
           {x = Flt.max(x1, x2), y = Flt.max(y1, y2), z = Flt.max(z1, z2)}  
53    
54      fun dot ({x=x1, y=y1, z=z1} : vec3, {x=x2, y=y2, z=z2}) =      fun dot ((x1, y1, z1) : vec3, (x2, y2, z2)) = (x1*x2 + y1*y2 +z1*z2)
           (x1*x2 + y1*y2 +z1*z2)  
55    
56      fun lerp (v1, t, v2) = adds (scale(1.0-t, v1), t, v2)      fun lerp (v1, t, v2) = adds (scale(1.0-t, v1), t, v2)
57    
# Line 78  Line 71 
71                else lerp(v1, t, v2)                else lerp(v1, t, v2)
72            end            end
73    
74      fun cross ({x=x1, y=y1, z=z1} : vec3, {x=x2, y=y2, z=z2}) = {      fun cross ((x1, y1, z1) : vec3, (x2, y2, z2)) = (
75              x = y1*z2 - z1*y2,              y1*z2 - z1*y2,
76              y = z1*x2 - x1*z2,              z1*x2 - x1*z2,
77              z = x1*y2 - y1*x2              x1*y2 - y1*x2
78            }            )
79    
80      fun lengthSq v = dot(v, v)      fun lengthSq v = dot(v, v)
81      fun length v = Flt.sqrt(lengthSq v)      fun length v = Flt.sqrt(lengthSq v)
# Line 112  Line 105 
105    
106      fun perpendicularComponent {basis, v} = sub(v, parallelComponent {basis=basis, v=v})      fun perpendicularComponent {basis, v} = sub(v, parallelComponent {basis=basis, v=v})
107    
   (* rays *)  
     type ray = {orig : vec3, dir : vec3}  
   
     fun rayToPoint ({orig, dir}, s) = adds(orig, s, dir)  
   
108    end    end

Legend:
Removed from v.1259  
changed lines
  Added in v.1260

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