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