annotate src/zamikongine/math/matrix.nim @ 37:6859bcfabc62

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