@@ -1,118 +1,47 @@
1−module MJS where
1+module Math.Matrix where
22
33{-| A high performance linear algebra library using native JS arrays. Geared
4−towards 3D graphics and use with `Graphics.WebGL`.
4+towards 3D graphics and use with `Graphics.WebGL`. All matrices are immutable.
55
6−Both vectors and matrices are immutable.
6+This library uses the convention that the prefix `make` is creating a new
7+array, whereas without the prefix, you are applying some transform to an
8+existing matrix.
79
8−# Constructing and Destructing Vectors
10+@docs mul4x4
911
10−@docs v3, toTuple3
11−
12−# Vector Math
13−
14−@docs add, sub, neg, direction, length, lengthSquared, normalize, scale
15−
16−# Vector Operations
17−
18−@docs dot, cross, mul4x4
19−
20−Matrix operations
12+# Operations
2113
2214@docs identity, inverseOrthonormal, mul, mulAffine, transpose, makeBasis
2315
24−Matrix projections
16+# Projections
2517
2618@docs makeFrustrum, makePerspective, makeOrtho, makeOrtho2D, makeLookAt
2719
28−Matrix transforms
20+# Transforms
2921
3022@docs makeRotate, rotate, makeScale3, makeScale, scaleM3, scaleM, makeTranslate3, makeTranslate, translate3, translate
3123
3224-}
3325
3426import Native.MJS
27+import Math.Vector (Vec3)
3528
36−data V2 = V2
37−data V3 = V3
38−data V4 = V4
39−
40−{-| Creates a new 3-element vector with the given values
41−-}
42−v3 : Float -> Float -> Float -> V3
43−v3 = Native.MJS.v3
44−
45−{-| Convert to a native tuple
46−-}
47−toTuple3 : V3 -> (Float,Float,Float)
48−toTuple3 = Native.MJS.toTuple3
49−
50−{-| Vector addition: a + b
51−-}
52−add : V3 -> V3 -> V3
53−add = Native.MJS.v3add
54−
55−{-| Vector subtraction: a - b
56−-}
57−sub : V3 -> V3 -> V3
58−sub = Native.MJS.v3sub
59−
60−{-| Vector negation: -a
61−-}
62−neg : V3 -> V3
63−neg = Native.MJS.v3neg
64−
65−{-| The normalized direction from a to b: (a - b) / |a - b|
66−-}
67−direction : V3 -> V3 -> V3
68−direction = Native.MJS.v3direction
69−
70−{-| The length of the given vector: |a|
71−-}
72−length : V3 -> Float
73−length = Native.MJS.v3length
74−
75−{-| The square of the length of the given vector: |a| * |a|
76−-}
77−lengthSquared : V3 -> Float
78−lengthSquared = Native.MJS.v3lengthSquared
79−
80−{-| A unit vector with the same direction as the given vector: a / |a|
81−-}
82−normalize : V3 -> V3
83−normalize = Native.MJS.v3normalize
84−
85−{-| Multiply the vector by a scalar: a * k
86−-}
87−scale : V3 -> Float -> V3
88−scale = Native.MJS.v3scale
89−
90−{-| The dot product of a and b
91−-}
92−dot : V3 -> V3 -> Float
93−dot = Native.MJS.v3dot
94−
95−{-| The cross product of a and b
96−-}
97−cross : V3 -> V3 -> V3
98−cross = Native.MJS.v3cross
99−
100−data M4x4 = M4x4
29+data Mat4 = Mat4
10130
10231{-| Multiply a vector by a 4x4 matrix: m * v
10332-}
104−mul4x4 : M4x4 -> V3 -> V3
33+mul4x4 : Mat4 -> Vec3 -> Vec3
10534mul4x4 = Native.MJS.v3mul4x4
10635
10736{-| A matrix with all 0s, except 1s on the diagonal.
10837-}
109−identity : M4x4
38+identity : Mat4
11039identity = Native.MJS.m4x4identity
11140
11241{-| Computes the inverse of the given matrix m, assuming that the matrix is
11342orthonormal.
11443-}
115−inverseOrthonormal : M4x4 -> M4x4
44+inverseOrthonormal : Mat4 -> Mat4
11645inverseOrthonormal = Native.MJS.m4x4inverseOrthonormal
11746
11847{-| Creates a matrix for a projection frustum with the given parameters.
@@ -126,7 +55,7 @@ Parameters:
12655 * znear - the near z distance of the frustum
12756 * zfar - the far z distance of the frustum
12857-}
129−makeFrustrum : Float -> Float -> Float -> Float -> Float -> Float -> Float -> M4x4
58+makeFrustrum : Float -> Float -> Float -> Float -> Float -> Float -> Float -> Mat4
13059makeFrustrum = Native.MJS.m4x4makeFrustrum
13160
13261{-| Creates a matrix for a perspective projection with the given parameters.
@@ -138,7 +67,7 @@ Parameters:
13867 * znear - the near z distance of the projection
13968 * zfar - the far z distance of the projection
14069-}
141−makePerspective : Float -> Float -> Float -> Float -> M4x4
70+makePerspective : Float -> Float -> Float -> Float -> Mat4
14271makePerspective = Native.MJS.m4x4makePerspective
14372
14473{-|
@@ -153,7 +82,7 @@ Parameters:
15382 * znear - the near z distance of the frustum
15483 * zfar - the far z distance of the frustum
15584-}
156−makeOrtho : Float -> Float -> Float -> Float -> Float -> Float -> Float -> M4x4
85+makeOrtho : Float -> Float -> Float -> Float -> Float -> Float -> Float -> Mat4
15786makeOrtho = Native.MJS.m4x4makeOrtho
15887
15988{-| Creates a matrix for a 2D orthogonal frustum projection with the given
@@ -166,75 +95,75 @@ Parameters:
16695 * bottom - the bottom coordinate of the frustum
16796 * top - the top coordinate of the frustum
16897-}
169−makeOrtho2D : Float -> Float -> Float -> Float -> M4x4
98+makeOrtho2D : Float -> Float -> Float -> Float -> Mat4
17099makeOrtho2D = Native.MJS.m4x4makeOrtho2D
171100
172101{-| Matrix multiplcation: a * b
173102-}
174−mul : M4x4 -> M4x4 -> M4x4
103+mul : Mat4 -> Mat4 -> Mat4
175104mul = Native.MJS.m4x4mul
176105
177106{-| Matrix multiplication, assuming a and b are affine: a * b
178107-}
179−mulAffine : M4x4 -> M4x4 -> M4x4
108+mulAffine : Mat4 -> Mat4 -> Mat4
180109mulAffine = Native.MJS.m4x4mulAffine
181110
182111{-| Creates a transformation matrix for rotation in radians about the
1831123-element vector axis.
184113-}
185−makeRotate : Float -> V3 -> M4x4
114+makeRotate : Float -> Vec3 -> Mat4
186115makeRotate = Native.MJS.m4x4makeRotate
187116
188117{-| Concatenates a rotation of angle radians about the axis to the given matrix.
189118-}
190−rotate : Float -> V3 -> M4x4 -> M4x4
119+rotate : Float -> Vec3 -> Mat4 -> Mat4
191120rotate = Native.MJS.m4x4rotate
192121
193122{-| Creates a transformation matrix for scaling by 3 scalar values, one for
194123each of the x, y, and z directions.
195124-}
196−makeScale3 : Float -> Float -> Float -> M4x4
125+makeScale3 : Float -> Float -> Float -> Mat4
197126makeScale3 = Native.MJS.m4x4makeScale3
198127
199128{-| Creates a transformation matrix for scaling each of the x, y, and z axes by
200129the amount given in the corresponding element of the 3-element vector.
201130-}
202−makeScale : V3 -> M4x4
131+makeScale : Vec3 -> Mat4
203132makeScale = Native.MJS.m4x4makeScale
204133
205134{-| Concatenates a scaling to the given matrix.
206135-}
207−scaleM3 : Float -> Float -> Float -> M4x4 -> M4x4
136+scaleM3 : Float -> Float -> Float -> Mat4 -> Mat4
208137scaleM3 = Native.MJS.m4x4scale3
209138
210139{-| Concatenates a scaling to the given matrix.
211140-}
212−scaleM : V3 -> M4x4 -> M4x4
141+scaleM : Vec3 -> Mat4 -> Mat4
213142scaleM = Native.MJS.m4x4scale
214143
215144{-|
216145Creates a transformation matrix for translating by 3 scalar values, one for
217146each of the x, y, and z directions.
218147-}
219−makeTranslate3 : Float -> Float -> Float -> M4x4
148+makeTranslate3 : Float -> Float -> Float -> Mat4
220149makeTranslate3 = Native.MJS.m4x4makeTranslate3
221150
222151{-| Creates a transformation matrix for translating each of the x, y, and z
223152axes by the amount given in the corresponding element of the 3-element vector.
224153-}
225−makeTranslate : V3 -> M4x4
154+makeTranslate : Vec3 -> Mat4
226155makeTranslate = Native.MJS.m4x4makeTranslate
227156
228157{-|
229158Concatenates a translation to the given matrix.
230159-}
231−translate3 : Float -> Float -> Float -> M4x4 -> M4x4
160+translate3 : Float -> Float -> Float -> Mat4 -> Mat4
232161translate3 = Native.MJS.m4x4translate3
233162
234163{-|
235164Concatenates a translation to the given matrix.
236165-}
237−translate : V3 -> M4x4 -> M4x4
166+translate : Vec3 -> Mat4 -> Mat4
238167translate = Native.MJS.m4x4translate
239168
240169{-|
@@ -246,16 +175,16 @@ Parameters:
246175 * center - The location of the focused object
247176 * up - The "up" direction according to the camera
248177-}
249−makeLookAt : V3 -> V3 -> V3 -> M4x4
178+makeLookAt : Vec3 -> Vec3 -> Vec3 -> Mat4
250179makeLookAt = Native.MJS.m4x4makeLookAt
251180
252181{-| "Flip" the matrix across the diagonal by swapping row index and column
253182index.
254183-}
255−transpose : M4x4 -> M4x4
184+transpose : Mat4 -> Mat4
256185transpose = Native.MJS.m4x4transpose
257186
258187{-| Creates a transform from a basis consisting of 3 linearly independent vectors.
259188-}
260−makeBasis : V3 -> V3 -> V3 -> M4x4
189+makeBasis : Vec3 -> Vec3 -> Vec3 -> Mat4
261190makeBasis = Native.MJS.m4x4makeBasis