annotate src/semicongine/math/matrix.nim @ 518:5d406c17bbcb

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