Mercurial > games > semicongine
annotate semiconginev2/old/core/matrix.nim @ 1226:c8e3037aca66 compiletime-tests
add: contrib stuff
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 23:41:51 +0700 |
parents | 56781cc0fc7c |
children |
rev | line source |
---|---|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/math |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/macros |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/random |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/strutils |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/strformat |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
6 import std/typetraits |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
8 import ./vector |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
9 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
10 export math |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
11 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
12 type |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
13 # layout is row-first |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
14 # having an object instead of directly aliasing the array seems a bit ugly at |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
15 # first, but is necessary to be able to work correctly with distinguished |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
16 # types (i.e. TMat23 and TMat32 would be an alias for the same type array[6, T] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
17 # which prevents the type system from identifying the correct type at times) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
18 # |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
19 # Though, great news is that objects have zero overhead! |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
20 TMat2*[T: SomeNumber] = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
21 data*: array[4, T] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
22 TMat23*[T: SomeNumber] = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
23 data*: array[6, T] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
24 TMat32*[T: SomeNumber] = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
25 data*: array[6, T] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
26 TMat3*[T: SomeNumber] = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
27 data*: array[9, T] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
28 TMat34*[T: SomeNumber] = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
29 data*: array[12, T] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
30 TMat43*[T: SomeNumber] = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
31 data*: array[12, T] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
32 TMat4*[T: SomeNumber] = object |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
33 data*: array[16, T] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
34 TMat* = TMat2|TMat3|TMat4|TMat23|TMat32|TMat34|TMat43 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
35 Mat2* = TMat2[float32] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
36 Mat23* = TMat23[float32] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
37 Mat32* = TMat32[float32] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
38 Mat3* = TMat3[float32] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
39 Mat34* = TMat34[float32] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
40 Mat43* = TMat43[float32] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
41 Mat4* = TMat4[float32] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
42 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
43 func MakeUnit2*[T: SomeNumber](): auto {.compiletime.} = TMat2[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
44 T(1), T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
45 T(0), T(1), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
46 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
47 func MakeUnit3*[T: SomeNumber](): auto {.compiletime.} = TMat3[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
48 T(1), T(0), T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
49 T(0), T(1), T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
50 T(0), T(0), T(1), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
51 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
52 func MakeUnit4*[T: SomeNumber](): auto {.compiletime.} = TMat4[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
53 T(1), T(0), T(0), T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
54 T(0), T(1), T(0), T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
55 T(0), T(0), T(1), T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
56 T(0), T(0), T(0), T(1), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
57 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
58 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
59 # generates constants: Unit |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
60 # Also for Y, Z, R, G, B |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
61 # not sure if this is necessary or even a good idea... |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
62 macro generateAllConsts() = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
63 result = newStmtList() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
64 for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
65 var typename = theType[0 .. 0] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
66 if theType[^2].isDigit: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
67 typename = typename & theType[^2] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
68 if theType[^1].isDigit: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
69 typename = typename & theType[^1] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
70 result.add(newConstStmt( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
71 postfix(ident("Unit2" & typename), "*"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
72 newCall(nnkBracketExpr.newTree(ident("MakeUnit2"), ident(theType))) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
73 )) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
74 result.add(newConstStmt( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
75 postfix(ident("Unit3" & typename), "*"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
76 newCall(nnkBracketExpr.newTree(ident("MakeUnit3"), ident(theType))) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
77 )) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
78 result.add(newConstStmt( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
79 postfix(ident("Unit4" & typename), "*"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
80 newCall(nnkBracketExpr.newTree(ident("MakeUnit4"), ident(theType))) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
81 )) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
82 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
83 generateAllConsts() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
84 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
85 const Unit2* = MakeUnit2[float32]() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
86 const Unit3* = MakeUnit3[float32]() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
87 const Unit4* = MakeUnit4[float32]() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
88 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
89 template RowCount*(m: typedesc): int = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
90 when m is TMat2: 2 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
91 elif m is TMat23: 2 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
92 elif m is TMat32: 3 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
93 elif m is TMat3: 3 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
94 elif m is TMat34: 3 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
95 elif m is TMat43: 4 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
96 elif m is TMat4: 4 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
97 template ColumnCount*(m: typedesc): int = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
98 when m is TMat2: 2 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
99 elif m is TMat23: 3 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
100 elif m is TMat32: 2 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
101 elif m is TMat3: 3 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
102 elif m is TMat34: 4 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
103 elif m is TMat43: 3 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
104 elif m is TMat4: 4 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
105 template matlen(m: typedesc): int = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
106 when m is TMat2: 4 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
107 elif m is TMat23: 6 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
108 elif m is TMat32: 6 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
109 elif m is TMat3: 9 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
110 elif m is TMat34: 12 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
111 elif m is TMat43: 12 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
112 elif m is TMat4: 16 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
113 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
114 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
115 func toString[T](value: T): string = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
116 var |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
117 strvalues: seq[string] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
118 maxwidth = 0 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
119 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
120 for n in value.data: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
121 let strval = &"{float(n):.4f}" |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
122 strvalues.add(strval) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
123 if strval.len > maxwidth: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
124 maxwidth = strval.len |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
125 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
126 for i in 0 ..< strvalues.len: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
127 let filler = " ".repeat(maxwidth - strvalues[i].len) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
128 if i mod T.ColumnCount == T.ColumnCount - 1: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
129 result &= filler & strvalues[i] & "\n" |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
130 else: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
131 if i mod T.ColumnCount == 0: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
132 result &= " " |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
133 result &= filler & strvalues[i] & " " |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
134 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
135 func `$`*(v: TMat2[SomeNumber]): string = toString[TMat2[SomeNumber]](v) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
136 func `$`*(v: TMat23[SomeNumber]): string = toString[TMat23[SomeNumber]](v) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
137 func `$`*(v: TMat32[SomeNumber]): string = toString[TMat32[SomeNumber]](v) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
138 func `$`*(v: TMat3[SomeNumber]): string = toString[TMat3[SomeNumber]](v) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
139 func `$`*(v: TMat34[SomeNumber]): string = toString[TMat34[SomeNumber]](v) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
140 func `$`*(v: TMat43[SomeNumber]): string = toString[TMat43[SomeNumber]](v) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
141 func `$`*(v: TMat4[SomeNumber]): string = toString[TMat4[SomeNumber]](v) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
142 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
143 func `[]`*[T: TMat](m: T, row, col: int): auto = m.data[col + row * T.ColumnCount] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
144 func `[]=`*[T: TMat, U](m: var T, row, col: int, value: U) = m.data[col + row * T.ColumnCount] = value |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
145 func `[]`*[T: TMat](m: T, i: int): auto = m.data[i] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
146 func `[]=`*[T: TMat, U](m: var T, i: int, value: U) = m.data[i] = value |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
147 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
148 func Row*[T: TMat2](m: T, i: 0..1): auto = TVec2([m[i, 0], m[i, 1]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
149 func Row*[T: TMat32](m: T, i: 0..2): auto = TVec2([m[i, 0], m[i, 1]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
150 func Row*[T: TMat23](m: T, i: 0..1): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
151 func Row*[T: TMat3](m: T, i: 0..2): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
152 func Row*[T: TMat43](m: T, i: 0..3): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
153 func Row*[T: TMat34](m: T, i: 0..2): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
154 func Row*[T: TMat4](m: T, i: 0..3): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
155 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
156 func Col*[T: TMat2](m: T, i: 0..1): auto = TVec2([m[0, i], m[1, i]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
157 func Col*[T: TMat23](m: T, i: 0..2): auto = TVec2([m[0, i], m[1, i]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
158 func Col*[T: TMat32](m: T, i: 0..1): auto = TVec3([m[0, i], m[1, i], m[2, i]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
159 func Col*[T: TMat3](m: T, i: 0..2): auto = TVec3([m[0, i], m[1, i], m[2, i]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
160 func Col*[T: TMat34](m: T, i: 0..3): auto = TVec3([m[0, i], m[1, i], m[2, i]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
161 func Col*[T: TMat43](m: T, i: 0..2): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
162 func Col*[T: TMat4](m: T, i: 0..3): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
163 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
164 proc createMatMatMultiplicationOperator(leftType: typedesc, rightType: typedesc, outType: typedesc): NimNode = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
165 var data = nnkBracket.newTree() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
166 for i in 0 ..< RowCount(leftType): |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
167 for j in 0 ..< rightType.ColumnCount: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
168 data.add(newCall( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
169 ident("sum"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
170 infix( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
171 newCall(newDotExpr(ident("a"), ident("Row")), newLit(i)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
172 "*", |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
173 newCall(newDotExpr(ident("b"), ident("Col")), newLit(j)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
174 ) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
175 )) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
176 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
177 return newProc( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
178 postfix(nnkAccQuoted.newTree(ident("*")), "*"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
179 params = [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
180 ident("auto"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
181 newIdentDefs(ident("a"), ident(leftType.name)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
182 newIdentDefs(ident("b"), ident(rightType.name)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
183 ], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
184 body = nnkObjConstr.newTree(ident(outType.name), nnkExprColonExpr.newTree(ident("data"), data)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
185 procType = nnkFuncDef, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
186 ) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
187 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
188 proc createMatMatAdditionOperator(theType: typedesc): NimNode = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
189 var data = nnkBracket.newTree() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
190 for i in 0 ..< matlen(theType): |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
191 data.add( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
192 infix( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
193 nnkBracketExpr.newTree(ident("a"), newLit(i)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
194 "+", |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
195 nnkBracketExpr.newTree(ident("b"), newLit(i)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
196 )) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
197 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
198 return newProc( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
199 postfix(nnkAccQuoted.newTree(ident("+")), "*"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
200 params = [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
201 ident("auto"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
202 newIdentDefs(ident("a"), ident(theType.name)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
203 newIdentDefs(ident("b"), ident(theType.name)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
204 ], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
205 body = nnkObjConstr.newTree(ident(theType.name), nnkExprColonExpr.newTree(ident("data"), data)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
206 procType = nnkFuncDef, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
207 ) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
208 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
209 proc createVecMatMultiplicationOperator(matType: typedesc, vecType: typedesc): NimNode = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
210 var data = nnkBracket.newTree() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
211 for i in 0 ..< matType.RowCount: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
212 data.add(newCall( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
213 ident("sum"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
214 infix( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
215 ident("v"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
216 "*", |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
217 newCall(newDotExpr(ident("m"), ident("Row")), newLit(i)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
218 ) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
219 )) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
220 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
221 let resultVec = newCall( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
222 nnkBracketExpr.newTree(ident(vecType.name), ident("T")), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
223 data, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
224 ) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
225 let name = postfix(nnkAccQuoted.newTree(ident("*")), "*") |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
226 let genericParams = nnkGenericParams.newTree(nnkIdentDefs.newTree(ident("T"), ident("SomeNumber"), newEmptyNode())) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
227 let formalParams = nnkFormalParams.newTree( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
228 ident("auto"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
229 newIdentDefs(ident("m"), nnkBracketExpr.newTree(ident(matType.name), ident("T"))), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
230 newIdentDefs(ident("v"), nnkBracketExpr.newTree(ident(vecType.name), ident("T"))), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
231 ) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
232 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
233 return nnkFuncDef.newTree( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
234 name, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
235 newEmptyNode(), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
236 genericParams, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
237 formalParams, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
238 newEmptyNode(), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
239 newEmptyNode(), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
240 resultVec |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
241 ) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
242 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
243 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
244 proc createMatScalarOperator(matType: typedesc, op: string): NimNode = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
245 result = newStmtList() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
246 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
247 var data = nnkBracket.newTree() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
248 for i in 0 ..< matType.RowCount * matType.ColumnCount: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
249 data.add(infix(nnkBracketExpr.newTree(newDotExpr(ident("a"), ident("data")), newLit(i)), op, ident("b"))) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
250 result.add(newProc( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
251 postfix(nnkAccQuoted.newTree(ident(op)), "*"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
252 params = [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
253 ident("auto"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
254 newIdentDefs(ident("a"), ident(matType.name)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
255 newIdentDefs(ident("b"), ident("SomeNumber")), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
256 ], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
257 body = nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
258 procType = nnkFuncDef, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
259 )) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
260 result.add(newProc( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
261 postfix(nnkAccQuoted.newTree(ident(op)), "*"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
262 params = [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
263 ident("auto"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
264 newIdentDefs(ident("b"), ident("SomeNumber")), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
265 newIdentDefs(ident("a"), ident(matType.name)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
266 ], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
267 body = nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
268 procType = nnkFuncDef, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
269 )) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
270 if op == "-": |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
271 var data2 = nnkBracket.newTree() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
272 for i in 0 ..< matType.RowCount * matType.ColumnCount: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
273 data2.add(prefix(nnkBracketExpr.newTree(newDotExpr(ident("a"), ident("data")), newLit(i)), op)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
274 result.add(newProc( |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
275 postfix(nnkAccQuoted.newTree(ident(op)), "*"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
276 params = [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
277 ident("auto"), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
278 newIdentDefs(ident("a"), ident(matType.name)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
279 ], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
280 body = nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data2)), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
281 procType = nnkFuncDef, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
282 )) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
283 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
284 macro createAllMultiplicationOperators() = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
285 result = newStmtList() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
286 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
287 for op in ["+", "-", "*", "/"]: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
288 result.add(createMatScalarOperator(TMat2, op)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
289 result.add(createMatScalarOperator(TMat23, op)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
290 result.add(createMatScalarOperator(TMat32, op)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
291 result.add(createMatScalarOperator(TMat3, op)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
292 result.add(createMatScalarOperator(TMat34, op)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
293 result.add(createMatScalarOperator(TMat43, op)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
294 result.add(createMatScalarOperator(TMat4, op)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
295 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
296 result.add(createMatMatMultiplicationOperator(TMat2, TMat2, TMat2)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
297 result.add(createMatMatMultiplicationOperator(TMat2, TMat23, TMat23)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
298 result.add(createMatMatMultiplicationOperator(TMat23, TMat32, TMat2)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
299 result.add(createMatMatMultiplicationOperator(TMat23, TMat3, TMat23)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
300 result.add(createMatMatMultiplicationOperator(TMat32, TMat2, TMat32)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
301 result.add(createMatMatMultiplicationOperator(TMat32, TMat23, TMat3)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
302 result.add(createMatMatMultiplicationOperator(TMat3, TMat32, TMat32)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
303 result.add(createMatMatMultiplicationOperator(TMat3, TMat3, TMat3)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
304 result.add(createMatMatMultiplicationOperator(TMat3, TMat34, TMat34)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
305 result.add(createMatMatMultiplicationOperator(TMat43, TMat3, TMat43)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
306 result.add(createMatMatMultiplicationOperator(TMat43, TMat34, TMat4)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
307 result.add(createMatMatMultiplicationOperator(TMat4, TMat43, TMat43)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
308 result.add(createMatMatMultiplicationOperator(TMat4, TMat4, TMat4)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
309 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
310 result.add(createMatMatAdditionOperator(TMat2)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
311 result.add(createMatMatAdditionOperator(TMat23)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
312 result.add(createMatMatAdditionOperator(TMat32)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
313 result.add(createMatMatAdditionOperator(TMat3)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
314 result.add(createMatMatAdditionOperator(TMat34)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
315 result.add(createMatMatAdditionOperator(TMat43)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
316 result.add(createMatMatAdditionOperator(TMat4)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
317 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
318 result.add(createVecMatMultiplicationOperator(TMat2, TVec2)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
319 result.add(createVecMatMultiplicationOperator(TMat3, TVec3)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
320 result.add(createVecMatMultiplicationOperator(TMat4, TVec4)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
321 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
322 createAllMultiplicationOperators() |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
323 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
324 func `*`*(mat: Mat4, vec: Vec3f): Vec3f = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
325 (mat * vec.ToVec4(1)).ToVec3 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
326 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
327 func Transposed*[T](m: TMat2[T]): TMat2[T] = TMat2[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
328 m[0, 0], m[1, 0], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
329 m[0, 1], m[1, 1], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
330 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
331 func Transposed*[T](m: TMat23[T]): TMat32[T] = TMat32[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
332 m[0, 0], m[1, 0], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
333 m[0, 1], m[1, 1], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
334 m[0, 2], m[1, 2], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
335 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
336 func Transposed*[T](m: TMat32[T]): TMat23[T] = TMat23[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
337 m[0, 0], m[1, 0], m[2, 0], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
338 m[0, 1], m[1, 1], m[2, 1], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
339 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
340 func Transposed*[T](m: TMat3[T]): TMat3[T] = TMat3[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
341 m[0, 0], m[1, 0], m[2, 0], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
342 m[0, 1], m[1, 1], m[2, 1], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
343 m[0, 2], m[1, 2], m[2, 2], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
344 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
345 func Transposed*[T](m: TMat43[T]): TMat34[T] = TMat34[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
346 m[0, 0], m[1, 0], m[2, 0], m[3, 0], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
347 m[0, 1], m[1, 1], m[2, 1], m[3, 1], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
348 m[0, 2], m[1, 2], m[2, 2], m[3, 2], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
349 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
350 func Transposed*[T](m: TMat34[T]): TMat43[T] = TMat43[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
351 m[0, 0], m[1, 0], m[2, 0], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
352 m[0, 1], m[1, 1], m[2, 1], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
353 m[0, 2], m[1, 2], m[2, 2], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
354 m[0, 3], m[1, 3], m[2, 3], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
355 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
356 func Transposed*[T](m: TMat4[T]): TMat4[T] = TMat4[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
357 m[0, 0], m[1, 0], m[2, 0], m[3, 0], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
358 m[0, 1], m[1, 1], m[2, 1], m[3, 1], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
359 m[0, 2], m[1, 2], m[2, 2], m[3, 2], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
360 m[0, 3], m[1, 3], m[2, 3], m[3, 3], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
361 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
362 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
363 func Translate2d*[T](x, y: T): TMat3[T] = TMat3[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
364 T(1), T(0), x, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
365 T(0), T(1), y, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
366 T(0), T(0), T(1), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
367 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
368 func Scale2d*[T](sx, sy: T): TMat3[T] = TMat3[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
369 sx, T(0), T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
370 T(0), sy, T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
371 T(0), T(0), T(1), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
372 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
373 func Rotate2d*[T](angle: T): TMat3[T] = TMat3[T](data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
374 cos(angle), -sin(angle), T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
375 sin(angle), cos(angle), T(0), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
376 T(0), T(0), T(1), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
377 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
378 func Translate*(x = 0'f32, y = 0'f32, z = 0'f32): TMat4[float32] = Mat4(data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
379 1'f32, 0'f32, 0'f32, x, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
380 0'f32, 1'f32, 0'f32, y, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
381 0'f32, 0'f32, 1'f32, z, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
382 0'f32, 0'f32, 0'f32, 1'f32, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
383 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
384 func Translate*[T: TVec3](v: T): TMat4[float32] = Translate(v[0], v[1], v[2]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
385 func Scale*(x = 1'f32, y = 1'f32, z = 1'f32): Mat4 = Mat4(data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
386 x, 0'f32, 0'f32, 0'f32, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
387 0'f32, y, 0'f32, 0'f32, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
388 0'f32, 0'f32, z, 0'f32, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
389 0'f32, 0'f32, 0'f32, 1'f32, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
390 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
391 func Scale*[T: TVec3](v: T): TMat4[float32] = Scale(v[0], v[1], v[2]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
392 func Rotate*(angle: float32, a: Vec3f): Mat4 = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
393 let |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
394 cosa = cos(angle) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
395 sina = sin(angle) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
396 x = a[0] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
397 y = a[1] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
398 z = a[2] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
399 Mat4(data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
400 x * x * (1 - cosa) + cosa, y * x * (1 - cosa) - z * sina, z * x * (1 - cosa) + y * sina, 0'f32, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
401 x * y * (1 - cosa) + z * sina, y * y * (1 - cosa) + cosa, z * y * (1 - cosa) - x * sina, 0'f32, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
402 x * z * (1 - cosa) - y * sina, y * z * (1 - cosa) + x * sina, z * z * (1 - cosa) + cosa, 0'f32, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
403 0'f32, 0'f32, 0'f32, 1'f32, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
404 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
405 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
406 func asMat3(m: Mat4): auto = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
407 Mat3(data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
408 m[0, 0], m[0, 1], m[0, 2], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
409 m[1, 0], m[1, 1], m[1, 2], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
410 m[2, 0], m[2, 1], m[2, 2], |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
411 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
412 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
413 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
414 func Inversed*(m: Mat4): Mat4 = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
415 var m3 = m.asMat3.Transposed |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
416 m3[0, 0] = 1'f32 / m3[0, 0] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
417 m3[1, 1] = 1'f32 / m3[1, 1] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
418 m3[2, 2] = 1'f32 / m3[2, 2] |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
419 let col3 = -(m3 * m.Col(3).xyz) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
420 return Mat4(data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
421 m3[0, 0], m3[0, 1], m3[0, 2], col3.x, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
422 m3[1, 0], m3[1, 1], m3[1, 2], col3.y, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
423 m3[2, 0], m3[2, 1], m3[2, 2], col3.z, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
424 0, 0, 0, 1, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
425 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
426 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
427 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
428 # call e.g. TMat32[int]().randomized() to get a random matrix |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
429 template makeRandomInit(mattype: typedesc) = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
430 proc Randomized*[T: SomeInteger](m: mattype[T]): mattype[T] = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
431 for i in 0 ..< result.data.len: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
432 result.data[i] = rand(low(typeof(m.data[0])) .. high(typeof(m.data[0]))) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
433 proc Randomized*[T: SomeFloat](m: mattype[T]): mattype[T] = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
434 for i in 0 ..< result.data.len: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
435 result.data[i] = rand(T(1.0)) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
436 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
437 makeRandomInit(TMat2) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
438 makeRandomInit(TMat23) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
439 makeRandomInit(TMat32) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
440 makeRandomInit(TMat3) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
441 makeRandomInit(TMat34) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
442 makeRandomInit(TMat43) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
443 makeRandomInit(TMat4) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
444 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
445 func Perspective*(fovy, aspect, zNear, zFar: float32): Mat4 = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
446 let tanHalfFovy = tan(fovy / 2) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
447 return Mat4(data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
448 1 / (aspect * tanHalfFovy), 0, 0, 0, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
449 0, 1 / tanHalfFovy, 0, 0, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
450 0, 0, zFar / (zFar - zNear), -(zFar * zNear) / (zFar - zNear), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
451 0, 0, 1, 1, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
452 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
453 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
454 func Ortho*(left, right, top, bottom, zNear, zFar: float32): Mat4 = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
455 Mat4(data: [ |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
456 2 / (right - left), 0, 0, -(right + left) / (right - left), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
457 0, 2 / (bottom - top), 0, -(bottom + top) / (bottom - top), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
458 0, 0, 1 / (zFar - zNear), zNear / (zFar - zNear), |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
459 0, 0, 0, 1, |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
460 ]) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
461 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
462 # create an orthographic perspective that will map from -1 .. 1 on all axis and keep a 1:1 aspect ratio |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
463 # the smaller dimension (width or height) will always be 1 and the larger dimension will be larger, to keep the ratio |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
464 func OrthoWindowAspect*(windowAspect: float32): Mat4 = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
465 if windowAspect < 1: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
466 let space = 2 * (1 / windowAspect - 1) / 2 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
467 Ortho(-1, 1, -1 - space, 1 + space, 0, 1) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
468 else: |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
469 let space = 2 * (windowAspect - 1) / 2 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
470 Ortho(-1 - space, 1 + space, -1, 1, 0, 1) |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
471 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
472 func Position*(mat: Mat4): Vec3f {.deprecated.} = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
473 mat.Col(3).ToVec3 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
474 |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
475 func Scaling*(mat: Mat4): Vec3f {.deprecated.} = |
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
476 NewVec4f(mat[0, 0], mat[1, 1], mat[2, 2]) |