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