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/matrix3d.sml
ViewVC logotype

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

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

revision 1260, Tue Jan 17 14:01:50 2012 UTC revision 1261, Wed Jan 18 02:21:03 2012 UTC
# Line 1  Line 1 
1  (* matrix3d.sml  (* matrix3d.sml
2   *   *
3   * COPYRIGHT (c) 2009 John Reppy (http://cs.uchicago.edu/~jhr)   * COPYRIGHT (c) 2012 The SML3d Project (http://sml3d.cs.uchicago.edu)
4   * All rights reserved.   * All rights reserved.
5   *   *
6   * Double-precision 3x3 matrices   * Double-precision 3x3 matrices
# Line 9  Line 9 
9  structure Matrix3d :> MATRIX3 where type flt = SML3dTypes.double  structure Matrix3d :> MATRIX3 where type flt = SML3dTypes.double
10    = struct    = struct
11    
12      type double = SML3dTypes.double      type flt = SML3dTypes.double
     type flt = double  
13      type vec3 = SML3dTypes.vec3d      type vec3 = SML3dTypes.vec3d
14    
15      val epsilon = Double.epsilon      val epsilon = Double.epsilon
# Line 52  Line 51 
51    
52    (* create a matrix from three column vectors *)    (* create a matrix from three column vectors *)
53      fun fromCols (c1 : vec3, c2 : vec3, c3 : vec3) = new {      fun fromCols (c1 : vec3, c2 : vec3, c3 : vec3) = new {
54              m11 = #x c1, m12 = #x c2, m13 = #x c3,              m11 = #1 c1, m12 = #1 c2, m13 = #1 c3,
55              m21 = #y c1, m22 = #y c2, m23 = #y c3,              m21 = #2 c1, m22 = #2 c2, m23 = #2 c3,
56              m31 = #z c1, m32 = #z c2, m33 = #z c3              m31 = #3 c1, m32 = #3 c2, m33 = #3 c3
57            }            }
58    (* create a matrix from three row vectors *)    (* create a matrix from three row vectors *)
59      fun fromRows (r1 : vec3, r2 : vec3, r3 : vec3) = new {      fun fromRows (r1 : vec3, r2 : vec3, r3 : vec3) = new {
60              m11 = #x r1, m12 = #y r1, m13 = #z r1,              m11 = #1 r1, m12 = #2 r1, m13 = #3 r1,
61              m21 = #x r2, m22 = #y r2, m23 = #z r2,              m21 = #1 r2, m22 = #2 r2, m23 = #3 r2,
62              m31 = #x r3, m32 = #y r3, m33 = #z r3              m31 = #1 r3, m32 = #2 r3, m33 = #3 r3
63            }            }
64    
65    (* create a matrix from three column vectors *)    (* return the columns of the matrix *)
66      fun toCols (M v) = (      fun toCols (M v) = (
67              {x = v!(1,1), y = v!(2,1), z = v!(3,1)},    (* column 1 *)              (v!(1,1), v!(2,1), v!(3,1)),        (* column 1 *)
68              {x = v!(1,2), y = v!(2,2), z = v!(3,2)},    (* column 2 *)              (v!(1,2), v!(2,2), v!(3,2)),        (* column 2 *)
69              {x = v!(1,3), y = v!(2,3), z = v!(3,3)}     (* column 3 *)              (v!(1,3), v!(2,3), v!(3,3))         (* column 3 *)
70            )            )
71    (* create a matrix from three row vectors *)    (* return the rows of the matrix *)
72      fun toRows (M v) = (      fun toRows (M v) = (
73              {x = v!(1,1), y = v!(1,2), z = v!(1,3)},    (* row 1 *)              (v!(1,1), v!(1,2), v!(1,3)),        (* row 1 *)
74              {x = v!(2,1), y = v!(2,2), z = v!(2,3)},    (* row 2 *)              (v!(2,1), v!(2,2), v!(2,3)),        (* row 2 *)
75              {x = v!(3,1), y = v!(3,2), z = v!(3,3)}     (* row 3 *)              (v!(3,1), v!(3,2), v!(3,3))         (* row 3 *)
76            )            )
77    
78    (* project specific columns/rows *)    (* project specific columns/rows *)
79      fun col1 (M v) = {x = v!(1,1), y = v!(2,1), z = v!(3,1)}      fun col1 (M v) = (v!(1,1), v!(2,1), v!(3,1))
80      fun col2 (M v) = {x = v!(1,2), y = v!(2,2), z = v!(3,2)}      fun col2 (M v) = (v!(1,2), v!(2,2), v!(3,2))
81      fun col3 (M v) = {x = v!(1,3), y = v!(2,3), z = v!(3,3)}      fun col3 (M v) = (v!(1,3), v!(2,3), v!(3,3))
82      fun row1 (M v) = {x = v!(1,1), y = v!(1,2), z = v!(1,3)}      fun row1 (M v) = (v!(1,1), v!(1,2), v!(1,3))
83      fun row2 (M v) = {x = v!(2,1), y = v!(2,2), z = v!(2,3)}      fun row2 (M v) = (v!(2,1), v!(2,2), v!(2,3))
84      fun row3 (M v) = {x = v!(3,1), y = v!(3,2), z = v!(3,3)}      fun row3 (M v) = (v!(3,1), v!(3,2), v!(3,3))
85    
86      val identity = new {      val identity = new {
87              m11 = 1.0, m12 = 0.0, m13 = 0.0,              m11 = 1.0, m12 = 0.0, m13 = 0.0,
# Line 96  Line 95 
95              m31 = 0.0, m32 = 0.0, m33 = s              m31 = 0.0, m32 = 0.0, m33 = s
96            }            }
97    
98      fun scale {x, y, z} = new {      fun scale (x, y, z) = new {
99              m11 = x,   m12 = 0.0, m13 = 0.0,              m11 = x,   m12 = 0.0, m13 = 0.0,
100              m21 = 0.0, m22 = y,   m23 = 0.0,              m21 = 0.0, m22 = y,   m23 = 0.0,
101              m31 = 0.0, m32 = 0.0, m33 = z              m31 = 0.0, m32 = 0.0, m33 = z
# Line 142  Line 141 
141            end            end
142    
143    (* rotation around an axis (in degrees) *)    (* rotation around an axis (in degrees) *)
144      fun rotate (angle, {x, y, z}) = let      fun rotate (angle, (x, y, z)) = let
145            val angle = Double.toRadians angle            val angle = Double.toRadians angle
146            val s = Double.sin angle            val s = Double.sin angle
147            val c = Double.cos angle            val c = Double.cos angle
# Line 168  Line 167 
167      fun sxm (s, M v) = M(Vector.map (fn x => s*x) v)      fun sxm (s, M v) = M(Vector.map (fn x => s*x) v)
168    
169    (* multiply a matrix times a column vector *)    (* multiply a matrix times a column vector *)
170      fun mxv (M mat, {x, y, z}) = let      fun mxv (M mat, (x, y, z)) = let
171            fun m arg = mat ! arg            fun m arg = mat ! arg
172            in {            in (
173              x = m(1,1) * x + m(1,2) * y + m(1,3) * z,              m(1,1) * x + m(1,2) * y + m(1,3) * z,
174              y = m(2,1) * x + m(2,2) * y + m(2,3) * z,              m(2,1) * x + m(2,2) * y + m(2,3) * z,
175              z = m(3,1) * x + m(3,2) * y + m(3,3) * z              m(3,1) * x + m(3,2) * y + m(3,3) * z
176            } end            ) end
177    
178    (* multiply a row vector times a matrix *)    (* multiply a row vector times a matrix *)
179      fun vxm ({x, y, z}, M mat) = let      fun vxm ((x, y, z), M mat) = let
180            fun m arg = mat ! arg            fun m arg = mat ! arg
181            in {            in (
182              x = m(1,1) * x + m(2,1) * y + m(3,1) * z,              m(1,1) * x + m(2,1) * y + m(3,1) * z,
183              y = m(1,2) * x + m(2,2) * y + m(3,2) * z,              m(1,2) * x + m(2,2) * y + m(3,2) * z,
184              z = m(1,3) * x + m(2,3) * y + m(3,3) * z              m(1,3) * x + m(2,3) * y + m(3,3) * z
185            } end            ) end
186    
187      (* matrix transpose *)
188        fun transpose (M mat) = let
189              fun m arg = mat ! arg
190              in
191                new {
192                    m11 = m(1,1), m12 = m(2,1), m13 = m(3,1),
193                    m21 = m(1,2), m22 = m(2,2), m23 = m(3,2),
194                    m31 = m(1,3), m32 = m(2,3), m33 = m(3,3)
195                  }
196              end
197    
198    (* matrix multiplication *)    (* matrix multiplication *)
199      fun mxm (M aMat, M bMat) = let      fun mxm (M aMat, M bMat) = let
# Line 221  Line 231 
231              t(1,2,3) - t(1,3,2) - t(2,1,3) + t(2,3,1) + t(3,1,2) - t(3,2,1)              t(1,2,3) - t(1,3,2) - t(2,1,3) + t(2,3,1) + t(3,1,2) - t(3,2,1)
232            end            end
233    
234      fun trace (M v) = v!(1,1) + v!(2,2) + v!(3,3)      fun trace (M mat) = mat!(1,1) + mat!(2,2) + mat!(3,3)
235    
236      fun inverse (M mat) = let      fun inverse (M mat) = let
237            fun m arg = mat ! arg            fun m arg = mat ! arg
# Line 260  Line 270 
270            end            end
271    
272    end    end
   

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

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