Mercurial > games > semicongine
annotate src/semicongine/math/matrix.nim @ 518:5d406c17bbcb
did: refactor Vector names
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 20 Jan 2023 16:13:32 +0700 |
parents | 836790efab48 |
children | 03a94b905f1a |
rev | line source |
---|---|
501 | 1 import std/math |
2 import std/macros | |
3 import std/random | |
4 import std/strutils | |
5 import std/typetraits | |
6 | |
7 import ./vector | |
8 | |
9 export math | |
10 | |
11 type | |
12 # layout is row-first | |
13 # having an object instead of directly aliasing the array seems a bit ugly at | |
14 # first, but is necessary to be able to work correctly with distinguished | |
15 # types (i.e. Mat23 and Mat32 would be an alias for the same type array[6, T] | |
16 # which prevents the type system from identifying the correct type at times) | |
17 # | |
18 # Though, great news is that objects have zero overhead! | |
19 Mat22*[T: SomeNumber] = object | |
20 data*: array[4, T] | |
21 Mat23*[T: SomeNumber] = object | |
22 data*: array[6, T] | |
23 Mat32*[T: SomeNumber] = object | |
24 data*: array[6, T] | |
25 Mat33*[T: SomeNumber] = object | |
26 data*: array[9, T] | |
27 Mat34*[T: SomeNumber] = object | |
28 data*: array[12, T] | |
29 Mat43*[T: SomeNumber] = object | |
30 data*: array[12, T] | |
31 Mat44*[T: SomeNumber] = object | |
32 data*: array[16, T] | |
33 MatMM* = Mat22|Mat33|Mat44 | |
34 MatMN* = Mat23|Mat32|Mat34|Mat43 | |
35 Mat* = MatMM|MatMN | |
36 | |
37 func unit22[T: SomeNumber](): auto {.compiletime.} = Mat22[T](data:[ | |
38 T(1), T(0), | |
39 T(0), T(1), | |
40 ]) | |
41 func unit33[T: SomeNumber](): auto {.compiletime.} = Mat33[T](data:[ | |
42 T(1), T(0), T(0), | |
43 T(0), T(1), T(0), | |
44 T(0), T(0), T(1), | |
45 ]) | |
46 func unit44[T: SomeNumber](): auto {.compiletime.} = Mat44[T](data: [ | |
47 T(1), T(0), T(0), T(0), | |
48 T(0), T(1), T(0), T(0), | |
49 T(0), T(0), T(1), T(0), | |
50 T(0), T(0), T(0), T(1), | |
51 ]) | |
52 | |
53 # generates constants: Unit | |
54 # Also for Y, Z, R, G, B | |
55 # not sure if this is necessary or even a good idea... | |
56 macro generateAllConsts() = | |
57 result = newStmtList() | |
58 for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]: | |
59 var typename = theType[0 .. 0] | |
60 if theType[^2].isDigit: | |
61 typename = typename & theType[^2] | |
62 if theType[^1].isDigit: | |
63 typename = typename & theType[^1] | |
64 result.add(newConstStmt( | |
65 postfix(ident("Unit22" & typename), "*"), | |
66 newCall(nnkBracketExpr.newTree(ident("unit22"), ident(theType))) | |
67 )) | |
68 result.add(newConstStmt( | |
69 postfix(ident("Unit33" & typename), "*"), | |
70 newCall(nnkBracketExpr.newTree(ident("unit33"), ident(theType))) | |
71 )) | |
72 result.add(newConstStmt( | |
73 postfix(ident("Unit44" & typename), "*"), | |
74 newCall(nnkBracketExpr.newTree(ident("unit44"), ident(theType))) | |
75 )) | |
76 | |
77 generateAllConsts() | |
78 | |
79 const Unit22* = unit22[float]() | |
80 const Unit33* = unit33[float]() | |
81 const Unit44* = unit44[float]() | |
82 | |
83 template rowCount*(m: typedesc): int = | |
84 when m is Mat22: 2 | |
85 elif m is Mat23: 2 | |
86 elif m is Mat32: 3 | |
87 elif m is Mat33: 3 | |
88 elif m is Mat34: 3 | |
89 elif m is Mat43: 4 | |
90 elif m is Mat44: 4 | |
91 template columnCount*(m: typedesc): int = | |
92 when m is Mat22: 2 | |
93 elif m is Mat23: 3 | |
94 elif m is Mat32: 2 | |
95 elif m is Mat33: 3 | |
96 elif m is Mat34: 4 | |
97 elif m is Mat43: 3 | |
98 elif m is Mat44: 4 | |
99 | |
100 | |
101 func toString[T](value: T): string = | |
102 var | |
103 strvalues: seq[string] | |
104 maxwidth = 0 | |
105 | |
106 for n in value.data: | |
107 let strval = $n | |
108 strvalues.add(strval) | |
109 if strval.len > maxwidth: | |
110 maxwidth = strval.len | |
111 | |
112 for i in 0 ..< strvalues.len: | |
113 let filler = " ".repeat(maxwidth - strvalues[i].len) | |
114 if i mod T.columnCount == T.columnCount - 1: | |
115 result &= filler & strvalues[i] & "\n" | |
116 else: | |
117 if i mod T.columnCount == 0: | |
118 result &= " " | |
119 result &= filler & strvalues[i] & " " | |
120 result = $T & "\n" & result | |
121 | |
122 func `$`*(v: Mat22[SomeNumber]): string = toString[Mat22[SomeNumber]](v) | |
123 func `$`*(v: Mat23[SomeNumber]): string = toString[Mat23[SomeNumber]](v) | |
124 func `$`*(v: Mat32[SomeNumber]): string = toString[Mat32[SomeNumber]](v) | |
125 func `$`*(v: Mat33[SomeNumber]): string = toString[Mat33[SomeNumber]](v) | |
126 func `$`*(v: Mat34[SomeNumber]): string = toString[Mat34[SomeNumber]](v) | |
127 func `$`*(v: Mat43[SomeNumber]): string = toString[Mat43[SomeNumber]](v) | |
128 func `$`*(v: Mat44[SomeNumber]): string = toString[Mat44[SomeNumber]](v) | |
129 | |
130 func `[]`*[T: Mat](m: T, row, col: int): auto = m.data[col + row * T.columnCount] | |
131 proc `[]=`*[T: Mat, U](m: var T, row, col: int, value: U) = m.data[col + row * T.columnCount] = value | |
132 | |
518 | 133 func row*[T: Mat22](m: T, i: 0..1): auto = TVec2([m[i, 0], m[i, 1]]) |
134 func row*[T: Mat32](m: T, i: 0..2): auto = TVec2([m[i, 0], m[i, 1]]) | |
135 func row*[T: Mat23](m: T, i: 0..1): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) | |
136 func row*[T: Mat33](m: T, i: 0..2): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) | |
137 func row*[T: Mat43](m: T, i: 0..3): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) | |
138 func row*[T: Mat34](m: T, i: 0..2): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]]) | |
139 func row*[T: Mat44](m: T, i: 0..3): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]]) | |
501 | 140 |
518 | 141 func col*[T: Mat22](m: T, i: 0..1): auto = TVec2([m[0, i], m[1, i]]) |
142 func col*[T: Mat23](m: T, i: 0..2): auto = TVec2([m[0, i], m[1, i]]) | |
143 func col*[T: Mat32](m: T, i: 0..1): auto = TVec3([m[0, i], m[1, i], m[2, i]]) | |
144 func col*[T: Mat33](m: T, i: 0..2): auto = TVec3([m[0, i], m[1, i], m[2, i]]) | |
145 func col*[T: Mat34](m: T, i: 0..3): auto = TVec3([m[0, i], m[1, i], m[2, i]]) | |
146 func col*[T: Mat43](m: T, i: 0..2): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]]) | |
147 func col*[T: Mat44](m: T, i: 0..3): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]]) | |
501 | 148 |
149 proc createMatMatMultiplicationOperator(leftType: typedesc, rightType: typedesc, outType: typedesc): NimNode = | |
150 var data = nnkBracket.newTree() | |
151 for i in 0 ..< rowCount(leftType): | |
152 for j in 0 ..< rightType.columnCount: | |
153 data.add(newCall( | |
154 ident("sum"), | |
155 infix( | |
156 newCall(newDotExpr(ident("a"), ident("row")), newLit(i)), | |
157 "*", | |
158 newCall(newDotExpr(ident("b"), ident("col")), newLit(j)) | |
159 ) | |
160 )) | |
161 | |
162 return newProc( | |
163 postfix(nnkAccQuoted.newTree(ident("*")), "*"), | |
164 params=[ | |
165 ident("auto"), | |
166 newIdentDefs(ident("a"), ident(leftType.name)), | |
167 newIdentDefs(ident("b"), ident(rightType.name)) | |
168 ], | |
169 body=nnkObjConstr.newTree(ident(outType.name), nnkExprColonExpr.newTree(ident("data"), data)), | |
170 procType=nnkFuncDef, | |
171 ) | |
172 | |
173 proc createVecMatMultiplicationOperator(matType: typedesc, vecType: typedesc): NimNode = | |
174 var data = nnkBracket.newTree() | |
175 for i in 0 ..< matType.rowCount: | |
176 data.add(newCall( | |
177 ident("sum"), | |
178 infix( | |
179 ident("v"), | |
180 "*", | |
181 newCall(newDotExpr(ident("m"), ident("row")), newLit(i)) | |
182 ) | |
183 )) | |
184 | |
185 let resultVec = newCall( | |
186 nnkBracketExpr.newTree(ident(vecType.name), ident("T")), | |
187 data, | |
188 ) | |
189 let name = postfix(nnkAccQuoted.newTree(ident("*")), "*") | |
190 let genericParams = nnkGenericParams.newTree(nnkIdentDefs.newTree(ident("T"), ident("SomeNumber"), newEmptyNode())) | |
191 let formalParams = nnkFormalParams.newTree( | |
192 ident("auto"), | |
193 newIdentDefs(ident("m"), nnkBracketExpr.newTree(ident(matType.name), ident("T"))), | |
194 newIdentDefs(ident("v"), nnkBracketExpr.newTree(ident(vecType.name), ident("T"))), | |
195 ) | |
196 | |
197 return nnkFuncDef.newTree( | |
198 name, | |
199 newEmptyNode(), | |
200 genericParams, | |
201 formalParams, | |
202 newEmptyNode(), | |
203 newEmptyNode(), | |
204 resultVec | |
205 ) | |
206 | |
207 | |
208 proc createMatScalarOperator(matType: typedesc, op: string): NimNode = | |
209 result = newStmtList() | |
210 | |
211 var data = nnkBracket.newTree() | |
212 for i in 0 ..< matType.rowCount * matType.columnCount: | |
213 data.add(infix(nnkBracketExpr.newTree(newDotExpr(ident("a"), ident("data")), newLit(i)), op, ident("b"))) | |
214 result.add(newProc( | |
215 postfix(nnkAccQuoted.newTree(ident(op)), "*"), | |
216 params=[ | |
217 ident("auto"), | |
218 newIdentDefs(ident("a"), ident(matType.name)), | |
219 newIdentDefs(ident("b"), ident("SomeNumber")), | |
220 ], | |
221 body=nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data)), | |
222 procType=nnkFuncDef, | |
223 )) | |
224 result.add(newProc( | |
225 postfix(nnkAccQuoted.newTree(ident(op)), "*"), | |
226 params=[ | |
227 ident("auto"), | |
228 newIdentDefs(ident("b"), ident("SomeNumber")), | |
229 newIdentDefs(ident("a"), ident(matType.name)), | |
230 ], | |
231 body=nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data)), | |
232 procType=nnkFuncDef, | |
233 )) | |
234 if op == "-": | |
235 var data2 = nnkBracket.newTree() | |
236 for i in 0 ..< matType.rowCount * matType.columnCount: | |
237 data2.add(prefix(nnkBracketExpr.newTree(newDotExpr(ident("a"), ident("data")), newLit(i)), op)) | |
238 result.add(newProc( | |
239 postfix(nnkAccQuoted.newTree(ident(op)), "*"), | |
240 params=[ | |
241 ident("auto"), | |
242 newIdentDefs(ident("a"), ident(matType.name)), | |
243 ], | |
244 body=nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data2)), | |
245 procType=nnkFuncDef, | |
246 )) | |
247 | |
248 macro createAllMultiplicationOperators() = | |
249 result = newStmtList() | |
250 | |
251 for op in ["+", "-", "*", "/"]: | |
252 result.add(createMatScalarOperator(Mat22, op)) | |
253 result.add(createMatScalarOperator(Mat23, op)) | |
254 result.add(createMatScalarOperator(Mat32, op)) | |
255 result.add(createMatScalarOperator(Mat33, op)) | |
256 result.add(createMatScalarOperator(Mat34, op)) | |
257 result.add(createMatScalarOperator(Mat43, op)) | |
258 result.add(createMatScalarOperator(Mat44, op)) | |
259 | |
260 result.add(createMatMatMultiplicationOperator(Mat22, Mat22, Mat22)) | |
261 result.add(createMatMatMultiplicationOperator(Mat22, Mat23, Mat23)) | |
262 result.add(createMatMatMultiplicationOperator(Mat23, Mat32, Mat22)) | |
263 result.add(createMatMatMultiplicationOperator(Mat23, Mat33, Mat23)) | |
264 result.add(createMatMatMultiplicationOperator(Mat32, Mat22, Mat32)) | |
265 result.add(createMatMatMultiplicationOperator(Mat32, Mat23, Mat33)) | |
266 result.add(createMatMatMultiplicationOperator(Mat33, Mat32, Mat32)) | |
267 result.add(createMatMatMultiplicationOperator(Mat33, Mat33, Mat33)) | |
268 result.add(createMatMatMultiplicationOperator(Mat33, Mat34, Mat34)) | |
269 result.add(createMatMatMultiplicationOperator(Mat43, Mat33, Mat43)) | |
270 result.add(createMatMatMultiplicationOperator(Mat43, Mat34, Mat44)) | |
271 result.add(createMatMatMultiplicationOperator(Mat44, Mat43, Mat43)) | |
272 result.add(createMatMatMultiplicationOperator(Mat44, Mat44, Mat44)) | |
273 | |
518 | 274 result.add(createVecMatMultiplicationOperator(Mat22, TVec2)) |
275 result.add(createVecMatMultiplicationOperator(Mat33, TVec3)) | |
276 result.add(createVecMatMultiplicationOperator(Mat44, TVec4)) | |
501 | 277 |
278 createAllMultiplicationOperators() | |
279 | |
280 | |
281 func transposed*[T](m: Mat22[T]): Mat22[T] = Mat22[T](data: [ | |
282 m[0, 0], m[1, 0], | |
283 m[0, 1], m[1, 1], | |
284 ]) | |
285 func transposed*[T](m: Mat23[T]): Mat32[T] = Mat32[T](data: [ | |
286 m[0, 0], m[1, 0], | |
287 m[0, 1], m[1, 1], | |
288 m[0, 2], m[1, 2], | |
289 ]) | |
290 func transposed*[T](m: Mat32[T]): Mat23[T] = Mat23[T](data: [ | |
291 m[0, 0], m[1, 0], m[2, 0], | |
292 m[0, 1], m[1, 1], m[2, 1], | |
293 ]) | |
294 func transposed*[T](m: Mat33[T]): Mat33[T] = Mat33[T](data: [ | |
295 m[0, 0], m[1, 0], m[2, 0], | |
296 m[0, 1], m[1, 1], m[2, 1], | |
297 m[0, 2], m[1, 2], m[2, 2], | |
298 ]) | |
299 func transposed*[T](m: Mat43[T]): Mat34[T] = Mat34[T](data: [ | |
300 m[0, 0], m[1, 0], m[2, 0], m[3, 0], | |
301 m[0, 1], m[1, 1], m[2, 1], m[3, 1], | |
302 m[0, 2], m[1, 2], m[2, 2], m[3, 2], | |
303 ]) | |
304 func transposed*[T](m: Mat34[T]): Mat43[T] = Mat43[T](data: [ | |
305 m[0, 0], m[1, 0], m[2, 0], | |
306 m[0, 1], m[1, 1], m[2, 1], | |
307 m[0, 2], m[1, 2], m[2, 2], | |
308 m[0, 3], m[1, 3], m[2, 3], | |
309 ]) | |
310 func transposed*[T](m: Mat44[T]): Mat44[T] = Mat44[T](data: [ | |
311 m[0, 0], m[1, 0], m[2, 0], m[3, 0], | |
312 m[0, 1], m[1, 1], m[2, 1], m[3, 1], | |
313 m[0, 2], m[1, 2], m[2, 2], m[3, 2], | |
314 m[0, 3], m[1, 3], m[2, 3], m[3, 3], | |
315 ]) | |
316 | |
317 func translate2d*[T](x, y: T): Mat33[T] = Mat33[T](data: [ | |
318 T(1), T(0), x, | |
319 T(0), T(1), y, | |
320 T(0), T(0), T(1), | |
321 ]) | |
322 func scale2d*[T](sx, sy: T): Mat33[T] = Mat33[T](data: [ | |
323 sx, T(0), T(0), | |
324 T(0), sy, T(0), | |
325 T(0), T(0), T(1), | |
326 ]) | |
327 func rotate2d*[T](angle: T): Mat33[T] = Mat33[T](data: [ | |
328 cos(angle), -sin(angle), T(0), | |
329 sin(angle), cos(angle), T(0), | |
330 T(0), T(0), T(1), | |
331 ]) | |
332 func translate3d*[T](x, y, z: T): Mat44[T] = Mat44[T](data: [ | |
333 T(1), T(0), T(0), x, | |
334 T(0), T(1), T(0), y, | |
335 T(0), T(0), T(1), z, | |
336 T(0), T(0), T(0), T(1), | |
337 ]) | |
338 func scale3d*[T](sx, sy, sz: T): Mat44[T] = Mat44[T](data: [ | |
339 sx, T(0), T(0), T(0), | |
340 T(0), sy, T(0), T(0), | |
341 T(0), T(0), sz, T(0), | |
342 T(0), T(0), T(0), T(1), | |
343 ]) | |
518 | 344 func rotate3d*[T](angle: T, a: TVec3[T]): Mat44[T] = |
501 | 345 let |
346 cosa = cos(angle) | |
347 sina = sin(angle) | |
348 x = a[0] | |
349 y = a[1] | |
350 z = a[2] | |
351 Mat44[T](data: [ | |
352 x * x * (1 - cosa) + cosa, y * x * (1 - cosa) - z * sina, z * x * (1 - cosa) + y * sina, T(0), | |
353 x * y * (1 - cosa) + z * sina, y * y * (1 - cosa) + cosa, z * y * (1 - cosa) - x * sina, T(0), | |
354 x * z * (1 - cosa) - y * sina, y * z * (1 - cosa) + x * sina, z * z * (1 - cosa) + cosa, T(0), | |
355 T(0), T(0), T(0), T(1), | |
356 ]) | |
357 | |
358 | |
359 # call e.g. Mat32[int]().randomized() to get a random matrix | |
360 template makeRandomInit(mattype: typedesc) = | |
361 proc randomized*[T: SomeInteger](m: mattype[T]): mattype[T] = | |
362 for i in 0 ..< result.data.len: | |
363 result.data[i] = rand(low(typeof(m.data[0])) .. high(typeof(m.data[0]))) | |
364 proc randomized*[T: SomeFloat](m: mattype[T]): mattype[T] = | |
365 for i in 0 ..< result.data.len: | |
366 result.data[i] = rand(1.0) | |
367 | |
368 makeRandomInit(Mat22) | |
369 makeRandomInit(Mat23) | |
370 makeRandomInit(Mat32) | |
371 makeRandomInit(Mat33) | |
372 makeRandomInit(Mat34) | |
373 makeRandomInit(Mat43) | |
374 makeRandomInit(Mat44) | |
375 | |
376 func perspective*[T: SomeFloat](fovy, aspect, zNear, zFar: T): Mat44[T] = | |
377 let tanHalfFovy = tan(fovy / T(2)) | |
378 return Mat44[T](data:[ | |
379 T(1) / (aspect * tanHalfFovy), T(0), T(0), T(0), | |
380 T(0), T(1) / tanHalfFovy, T(0), T(0), | |
381 T(0), T(0), T(zFar / (zFar - zNear)), T(-(zFar * zNear) / (zFar - zNear)), | |
382 T(0), T(0), T(1), T(1), | |
383 ]) | |
384 | |
517
836790efab48
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
385 func ortho*[T: SomeFloat](left, right, top, bottom, zNear, zFar: T): Mat44[T] = |
501 | 386 Mat44[T](data:[ |
387 T(2) / (right - left), T(0), T(0), -(right + left) / (right - left), | |
517
836790efab48
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
388 T(0), T(2) / (bottom - top), T(0), -(bottom + top) / (bottom - top), |
501 | 389 T(0), T(0), T(1) / (zFar - zNear), -zNear / (zFar - zNear), |
517
836790efab48
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
501
diff
changeset
|
390 T(0), T(0), T(1), T(1), |
501 | 391 ]) |