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/matrix2f.sml
 [sml3d] / trunk / sml3d / src / sml3d / base / matrix2f.sml

# Diff of /trunk/sml3d/src/sml3d/base/matrix2f.sml

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  (* matrix2f.sml  (* matrix2f.sml
2   *   *
3   * COPYRIGHT (c) 2010 John Reppy (http://cs.uchicago.edu/~jhr)   * COPYRIGHT (c) 2012 The SML3d Project (http://sml3d.cs.uchicago.edu)
5   *   *
6   * Single-precision 2x2 matrix types and operations.   * Single-precision 2x2 matrix types and operations.
# Line 51  Line 51
51
52    (* create a matrix from two column vectors *)    (* create a matrix from two column vectors *)
53      fun fromCols (c1 : vec2, c2 : vec2) = new {      fun fromCols (c1 : vec2, c2 : vec2) = new {
54              m11 = #x c1, m12 = #x c2,              m11 = #1 c1, m12 = #1 c2,
55              m21 = #y c1, m22 = #y c2              m21 = #2 c1, m22 = #2 c2
56            }            }
57    (* create a matrix from two row vectors *)    (* create a matrix from two row vectors *)
58      fun fromRows (r1 : vec2, r2 : vec2) = new {      fun fromRows (r1 : vec2, r2 : vec2) = new {
59              m11 = #x r1, m12 = #y r1,              m11 = #1 r1, m12 = #2 r1,
60              m21 = #x r2, m22 = #y r2              m21 = #1 r2, m22 = #2 r2
61            }            }
62
63    (* return the columns of the matrix *)    (* return the columns of the matrix *)
64      fun toCols (M v) = (      fun toCols (M v) = (
65              {x = v!(1,1), y = v!(2,1)}, (* column 1 *)              (v!(1,1), v!(2,1)), (* column 1 *)
66              {x = v!(1,2), y = v!(2,2)}  (* column 2 *)              (v!(1,2), v!(2,2))  (* column 2 *)
67            )            )
68    (* return the rows of the matrix *)    (* return the rows of the matrix *)
69      fun toRows (M v) = (      fun toRows (M v) = (
70              {x = v!(1,1), y = v!(1,2)}, (* row 1 *)              (v!(1,1), v!(1,2)), (* row 1 *)
71              {x = v!(2,1), y = v!(2,2)}  (* row 2 *)              (v!(2,1), v!(2,2))  (* row 2 *)
72            )            )
73
74    (* project specific columns/rows *)    (* project specific columns/rows *)
75      fun col1 (M v) = {x = v!(1,1), y = v!(2,1)}      fun col1 (M v) = (v!(1,1), v!(2,1))
76      fun col2 (M v) = {x = v!(1,2), y = v!(2,2)}      fun col2 (M v) = (v!(1,2), v!(2,2))
77      fun row1 (M v) = {x = v!(1,1), y = v!(1,2)}      fun row1 (M v) = (v!(1,1), v!(1,2))
78      fun row2 (M v) = {x = v!(2,1), y = v!(2,2)}      fun row2 (M v) = (v!(2,1), v!(2,2))
79
80    (* standard matrices *)    (* standard matrices *)
81      val identity = new {      val identity = new {
# Line 88  Line 88
88              m21 = 0.0, m22 = s              m21 = 0.0, m22 = s
89            }            }
90
91      fun scale {x, y} = new {      fun scale (x, y) = new {
92              m11 = x,   m12 = 0.0,              m11 = x,   m12 = 0.0,
93              m21 = 0.0, m22 = y              m21 = 0.0, m22 = y
94            }            }
# Line 119  Line 119
119     * vector and perp_a(p) is the component of p that is perpendicular to a.     * vector and perp_a(p) is the component of p that is perpendicular to a.
120     *)     *)
121      fun reflect axis = let      fun reflect axis = let
122            val {x, y} = V.normalize axis            val (x, y) = V.normalize axis
123            val xy = ~2.0*x*y            val xy = ~2.0*x*y
124            in            in
125              new {              new {
# Line 146  Line 146
146      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)
147
148    (* multiply a matrix times a column vector *)    (* multiply a matrix times a column vector *)
149      fun mxv (M mat, {x, y}) = let      fun mxv (M mat, (x, y)) = let
150            fun m arg = mat ! arg            fun m arg = mat ! arg
151            in {            in
152              x = m(1,1) * x + m(1,2) * y,              (m(1,1) * x + m(1,2) * y, m(2,1) * x + m(2,2) * y)
153              y = m(2,1) * x + m(2,2) * y            end
} end
154
155    (* multiply a row vector times a matrix *)    (* multiply a row vector times a matrix *)
156      fun vxm ({x, y}, M mat) = let      fun vxm ((x, y), M mat) = let
157            fun m arg = mat ! arg            fun m arg = mat ! arg
158            in {            in
159              x = m(1,1) * x + m(2,1) * y,              (m(1,1) * x + m(2,1) * y, m(1,2) * x + m(2,2) * y)
160              y = m(1,2) * x + m(2,2) * y            end
} end
161
162    (* matrix multiplication *)    (* matrix multiplication *)
163      fun mxm (M aMat, M bMat) = let      fun mxm (M aMat, M bMat) = let

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