@@ -1,118 +1,47 @@
1−module MJS where
1+module Math.Matrix where
3{-| A high performance linear algebra library using native JS arrays. Geared
3{-| 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.
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−# Constructing and Destructing Vectors
10+@docs mul4x4
14−@docs add, sub, neg, direction, length, lengthSquared, normalize, scale
16−# Vector Operations
12+# Operations
18−@docs dot, cross, mul4x4
22@docs identity, inverseOrthonormal, mul, mulAffine, transpose, makeBasis
14@docs identity, inverseOrthonormal, mul, mulAffine, transpose, makeBasis
24−Matrix projections
16+# Projections
26@docs makeFrustrum, makePerspective, makeOrtho, makeOrtho2D, makeLookAt
18@docs makeFrustrum, makePerspective, makeOrtho, makeOrtho2D, makeLookAt
28−Matrix transforms
20+# Transforms
30@docs makeRotate, rotate, makeScale3, makeScale, scaleM3, scaleM, makeTranslate3, makeTranslate, translate3, translate
22@docs makeRotate, rotate, makeScale3, makeScale, scaleM3, scaleM, makeTranslate3, makeTranslate, translate3, translate
34import Native.MJS
26import Native.MJS
27+import Math.Vector (Vec3)
36−data V2 = V2
29+data Mat4 = Mat4
40−{-| Creates a new 3-element vector with the given values
42−v3 : Float -> Float -> Float -> V3
45−{-| Convert to a native tuple
47−toTuple3 : V3 -> (Float,Float,Float)
48−toTuple3 = Native.MJS.toTuple3
50−{-| Vector addition: a + b
53−add = Native.MJS.v3add
55−{-| Vector subtraction: a - b
58−sub = Native.MJS.v3sub
60−{-| Vector negation: -a
63−neg = Native.MJS.v3neg
65−{-| The normalized direction from a to b: (a - b) / |a - b|
67−direction : V3 -> V3 -> V3
68−direction = Native.MJS.v3direction
70−{-| The length of the given vector: |a|
73−length = Native.MJS.v3length
75−{-| The square of the length of the given vector: |a| * |a|
77−lengthSquared : V3 -> Float
78−lengthSquared = Native.MJS.v3lengthSquared
80−{-| A unit vector with the same direction as the given vector: a / |a|
83−normalize = Native.MJS.v3normalize
85−{-| Multiply the vector by a scalar: a * k
87−scale : V3 -> Float -> V3
88−scale = Native.MJS.v3scale
90−{-| The dot product of a and b
92−dot : V3 -> V3 -> Float
93−dot = Native.MJS.v3dot
95−{-| The cross product of a and b
97−cross : V3 -> V3 -> V3
98−cross = Native.MJS.v3cross
102{-| Multiply a vector by a 4x4 matrix: m * v
31{-| Multiply a vector by a 4x4 matrix: m * v
104−mul4x4 : M4x4 -> V3 -> V3
33+mul4x4 : Mat4 -> Vec3 -> Vec3
105mul4x4 = Native.MJS.v3mul4x4
34mul4x4 = Native.MJS.v3mul4x4
107{-| A matrix with all 0s, except 1s on the diagonal.
36{-| A matrix with all 0s, except 1s on the diagonal.
109−identity : M4x4
38+identity : Mat4
110identity = Native.MJS.m4x4identity
39identity = Native.MJS.m4x4identity
112{-| Computes the inverse of the given matrix m, assuming that the matrix is
41{-| Computes the inverse of the given matrix m, assuming that the matrix is
113orthonormal.
42orthonormal.
115−inverseOrthonormal : M4x4 -> M4x4
44+inverseOrthonormal : Mat4 -> Mat4
116inverseOrthonormal = Native.MJS.m4x4inverseOrthonormal
45inverseOrthonormal = Native.MJS.m4x4inverseOrthonormal
118{-| Creates a matrix for a projection frustum with the given parameters.
47{-| Creates a matrix for a projection frustum with the given parameters.
@@ -126,7 +55,7 @@ Parameters:
126 * znear - the near z distance of the frustum
55 * znear - the near z distance of the frustum
127 * zfar - the far z distance of the frustum
56 * zfar - the far z distance of the frustum
129−makeFrustrum : Float -> Float -> Float -> Float -> Float -> Float -> Float -> M4x4
58+makeFrustrum : Float -> Float -> Float -> Float -> Float -> Float -> Float -> Mat4
130makeFrustrum = Native.MJS.m4x4makeFrustrum
59makeFrustrum = Native.MJS.m4x4makeFrustrum
132{-| Creates a matrix for a perspective projection with the given parameters.
61{-| Creates a matrix for a perspective projection with the given parameters.
@@ -138,7 +67,7 @@ Parameters:
138 * znear - the near z distance of the projection
67 * znear - the near z distance of the projection
139 * zfar - the far z distance of the projection
68 * zfar - the far z distance of the projection
141−makePerspective : Float -> Float -> Float -> Float -> M4x4
70+makePerspective : Float -> Float -> Float -> Float -> Mat4
142makePerspective = Native.MJS.m4x4makePerspective
71makePerspective = Native.MJS.m4x4makePerspective
@@ -153,7 +82,7 @@ Parameters:
153 * znear - the near z distance of the frustum
82 * znear - the near z distance of the frustum
154 * zfar - the far z distance of the frustum
83 * zfar - the far z distance of the frustum
156−makeOrtho : Float -> Float -> Float -> Float -> Float -> Float -> Float -> M4x4
85+makeOrtho : Float -> Float -> Float -> Float -> Float -> Float -> Float -> Mat4
157makeOrtho = Native.MJS.m4x4makeOrtho
86makeOrtho = Native.MJS.m4x4makeOrtho
159{-| Creates a matrix for a 2D orthogonal frustum projection with the given
88{-| Creates a matrix for a 2D orthogonal frustum projection with the given
@@ -166,75 +95,75 @@ Parameters:
166 * bottom - the bottom coordinate of the frustum
95 * bottom - the bottom coordinate of the frustum
167 * top - the top coordinate of the frustum
96 * top - the top coordinate of the frustum
169−makeOrtho2D : Float -> Float -> Float -> Float -> M4x4
98+makeOrtho2D : Float -> Float -> Float -> Float -> Mat4
170makeOrtho2D = Native.MJS.m4x4makeOrtho2D
99makeOrtho2D = Native.MJS.m4x4makeOrtho2D
172{-| Matrix multiplcation: a * b
101{-| Matrix multiplcation: a * b
174−mul : M4x4 -> M4x4 -> M4x4
103+mul : Mat4 -> Mat4 -> Mat4
175mul = Native.MJS.m4x4mul
104mul = Native.MJS.m4x4mul
177{-| Matrix multiplication, assuming a and b are affine: a * b
106{-| Matrix multiplication, assuming a and b are affine: a * b
179−mulAffine : M4x4 -> M4x4 -> M4x4
108+mulAffine : Mat4 -> Mat4 -> Mat4
180mulAffine = Native.MJS.m4x4mulAffine
109mulAffine = Native.MJS.m4x4mulAffine
182{-| Creates a transformation matrix for rotation in radians about the
111{-| Creates a transformation matrix for rotation in radians about the
1833-element vector axis.
1123-element vector axis.
185−makeRotate : Float -> V3 -> M4x4
114+makeRotate : Float -> Vec3 -> Mat4
186makeRotate = Native.MJS.m4x4makeRotate
115makeRotate = Native.MJS.m4x4makeRotate
188{-| Concatenates a rotation of angle radians about the axis to the given matrix.
117{-| Concatenates a rotation of angle radians about the axis to the given matrix.
190−rotate : Float -> V3 -> M4x4 -> M4x4
119+rotate : Float -> Vec3 -> Mat4 -> Mat4
191rotate = Native.MJS.m4x4rotate
120rotate = Native.MJS.m4x4rotate
193{-| Creates a transformation matrix for scaling by 3 scalar values, one for
122{-| Creates a transformation matrix for scaling by 3 scalar values, one for
194each of the x, y, and z directions.
123each of the x, y, and z directions.
196−makeScale3 : Float -> Float -> Float -> M4x4
125+makeScale3 : Float -> Float -> Float -> Mat4
197makeScale3 = Native.MJS.m4x4makeScale3
126makeScale3 = Native.MJS.m4x4makeScale3
199{-| Creates a transformation matrix for scaling each of the x, y, and z axes by
128{-| Creates a transformation matrix for scaling each of the x, y, and z axes by
200the amount given in the corresponding element of the 3-element vector.
129the amount given in the corresponding element of the 3-element vector.
202−makeScale : V3 -> M4x4
131+makeScale : Vec3 -> Mat4
203makeScale = Native.MJS.m4x4makeScale
132makeScale = Native.MJS.m4x4makeScale
205{-| Concatenates a scaling to the given matrix.
134{-| Concatenates a scaling to the given matrix.
207−scaleM3 : Float -> Float -> Float -> M4x4 -> M4x4
136+scaleM3 : Float -> Float -> Float -> Mat4 -> Mat4
208scaleM3 = Native.MJS.m4x4scale3
137scaleM3 = Native.MJS.m4x4scale3
210{-| Concatenates a scaling to the given matrix.
139{-| Concatenates a scaling to the given matrix.
212−scaleM : V3 -> M4x4 -> M4x4
141+scaleM : Vec3 -> Mat4 -> Mat4
213scaleM = Native.MJS.m4x4scale
142scaleM = Native.MJS.m4x4scale
216Creates a transformation matrix for translating by 3 scalar values, one for
145Creates a transformation matrix for translating by 3 scalar values, one for
217each of the x, y, and z directions.
146each of the x, y, and z directions.
219−makeTranslate3 : Float -> Float -> Float -> M4x4
148+makeTranslate3 : Float -> Float -> Float -> Mat4
220makeTranslate3 = Native.MJS.m4x4makeTranslate3
149makeTranslate3 = Native.MJS.m4x4makeTranslate3
222{-| Creates a transformation matrix for translating each of the x, y, and z
151{-| Creates a transformation matrix for translating each of the x, y, and z
223axes by the amount given in the corresponding element of the 3-element vector.
152axes by the amount given in the corresponding element of the 3-element vector.
225−makeTranslate : V3 -> M4x4
154+makeTranslate : Vec3 -> Mat4
226makeTranslate = Native.MJS.m4x4makeTranslate
155makeTranslate = Native.MJS.m4x4makeTranslate
229Concatenates a translation to the given matrix.
158Concatenates a translation to the given matrix.
231−translate3 : Float -> Float -> Float -> M4x4 -> M4x4
160+translate3 : Float -> Float -> Float -> Mat4 -> Mat4
232translate3 = Native.MJS.m4x4translate3
161translate3 = Native.MJS.m4x4translate3
235Concatenates a translation to the given matrix.
164Concatenates a translation to the given matrix.
237−translate : V3 -> M4x4 -> M4x4
166+translate : Vec3 -> Mat4 -> Mat4
238translate = Native.MJS.m4x4translate
167translate = Native.MJS.m4x4translate
@@ -246,16 +175,16 @@ Parameters:
246 * center - The location of the focused object
175 * center - The location of the focused object
247 * up - The "up" direction according to the camera
176 * up - The "up" direction according to the camera
249−makeLookAt : V3 -> V3 -> V3 -> M4x4
178+makeLookAt : Vec3 -> Vec3 -> Vec3 -> Mat4
250makeLookAt = Native.MJS.m4x4makeLookAt
179makeLookAt = Native.MJS.m4x4makeLookAt
252{-| "Flip" the matrix across the diagonal by swapping row index and column
181{-| "Flip" the matrix across the diagonal by swapping row index and column
255−transpose : M4x4 -> M4x4
184+transpose : Mat4 -> Mat4
256transpose = Native.MJS.m4x4transpose
185transpose = Native.MJS.m4x4transpose
258{-| Creates a transform from a basis consisting of 3 linearly independent vectors.
187{-| Creates a transform from a basis consisting of 3 linearly independent vectors.
260−makeBasis : V3 -> V3 -> V3 -> M4x4
189+makeBasis : Vec3 -> Vec3 -> Vec3 -> Mat4
261makeBasis = Native.MJS.m4x4makeBasis
190makeBasis = Native.MJS.m4x4makeBasis