comparison src/matrix.nim @ 13:a571db114152

add: vector/matrix modules, tests, nim config script
author Sam <sam@basx.dev>
date Wed, 28 Dec 2022 11:42:21 +0700
parents
children 617c6dcddbe2
comparison
equal deleted inserted replaced
12:9e5fe647ff91 13:a571db114152
1 import random
2 import std/strutils
3 import std/typetraits
4
5 import ./vector
6
7 type
8 # layout is row-first
9 # having an object instead of directly aliasing the array seems a bit ugly at
10 # first, but is necessary to be able to work correctly with distinguished
11 # types (i.e. Mat23 and Mat32 would be an alias for the same type array[6, T]
12 # which prevents the type system from identifying the correct type at times)
13 #
14 # Though, great news is that objects have zero overhead!
15 Mat22*[T: SomeNumber] = object
16 data: array[4, T]
17 Mat23*[T: SomeNumber] = object
18 data: array[6, T]
19 Mat32*[T: SomeNumber] = object
20 data: array[6, T]
21 Mat33*[T: SomeNumber] = object
22 data: array[9, T]
23 Mat34*[T: SomeNumber] = object
24 data: array[12, T]
25 Mat43*[T: SomeNumber] = object
26 data: array[12, T]
27 Mat44*[T: SomeNumber] = object
28 data: array[16, T]
29 MatMM* = Mat22|Mat33|Mat44
30 MatMN* = Mat23|Mat32|Mat34|Mat43
31 Mat* = MatMM|MatMN
32 IntegerMat = Mat22[SomeInteger]|Mat33[SomeInteger]|Mat44[SomeInteger]|Mat23[SomeInteger]|Mat32[SomeInteger]|Mat34[SomeInteger]|Mat43[SomeInteger]
33 FloatMat = Mat22[SomeFloat]|Mat33[SomeFloat]|Mat44[SomeFloat]|Mat23[SomeFloat]|Mat32[SomeFloat]|Mat34[SomeFloat]|Mat43[SomeFloat]
34
35
36 func rowCount*(m: Mat22): int = 2
37 func columnCount*(m: Mat22): int = 2
38 func rowCount*(m: Mat23): int = 2
39 func columnCount*(m: Mat23): int = 3
40 func rowCount*(m: Mat32): int = 3
41 func columnCount*(m: Mat32): int = 2
42 func rowCount*(m: Mat33): int = 3
43 func columnCount*(m: Mat33): int = 3
44 func rowCount*(m: Mat34): int = 3
45 func columnCount*(m: Mat34): int = 4
46 func rowCount*(m: Mat43): int = 4
47 func columnCount*(m: Mat43): int = 3
48 func rowCount*(m: Mat44): int = 4
49 func columnCount*(m: Mat44): int = 4
50
51
52 func toString[T](value: T): string =
53 var
54 strvalues: seq[string]
55 maxwidth = 0
56
57 for n in value.data:
58 let strval = $n
59 strvalues.add(strval)
60 if strval.len > maxwidth:
61 maxwidth = strval.len
62
63 for i in 0 ..< strvalues.len:
64 let filler = " ".repeat(maxwidth - strvalues[i].len)
65 if i mod value.columnCount == value.columnCount - 1:
66 result &= filler & strvalues[i] & "\n"
67 else:
68 if i mod value.columnCount == 0:
69 result &= " "
70 result &= filler & strvalues[i] & " "
71 result = $T & "\n" & result
72
73 func `$`*(v: Mat22[SomeNumber]): string = toString[Mat22[SomeNumber]](v)
74 func `$`*(v: Mat23[SomeNumber]): string = toString[Mat23[SomeNumber]](v)
75 func `$`*(v: Mat32[SomeNumber]): string = toString[Mat32[SomeNumber]](v)
76 func `$`*(v: Mat33[SomeNumber]): string = toString[Mat33[SomeNumber]](v)
77 func `$`*(v: Mat34[SomeNumber]): string = toString[Mat34[SomeNumber]](v)
78 func `$`*(v: Mat43[SomeNumber]): string = toString[Mat43[SomeNumber]](v)
79 func `$`*(v: Mat44[SomeNumber]): string = toString[Mat44[SomeNumber]](v)
80
81 func `[]`*[T: Mat](m: T, row, col: int): auto = m.data[col + row * m.columnCount]
82 proc `[]=`*[T: Mat, U](m: var T, row, col: int, value: U) = m.data[col + row * m.columnCount] = value
83
84 func row*[T: Mat22](m: T, i: 0..1): auto = Vec2([m[i, 0], m[i, 1]])
85 func row*[T: Mat32](m: T, i: 0..2): auto = Vec2([m[i, 0], m[i, 1]])
86 func row*[T: Mat23](m: T, i: 0..1): auto = Vec3([m[i, 0], m[i, 1], m[i, 2]])
87 func row*[T: Mat33](m: T, i: 0..2): auto = Vec3([m[i, 0], m[i, 1], m[i, 2]])
88 func row*[T: Mat43](m: T, i: 0..3): auto = Vec3([m[i, 0], m[i, 1], m[i, 2]])
89 func row*[T: Mat34](m: T, i: 0..2): auto = Vec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]])
90 func row*[T: Mat44](m: T, i: 0..3): auto = Vec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]])
91
92 func col*[T: Mat22](m: T, i: 0..1): auto = Vec2([m[0, i], m[1, i]])
93 func col*[T: Mat23](m: T, i: 0..2): auto = Vec2([m[0, i], m[1, i]])
94 func col*[T: Mat32](m: T, i: 0..1): auto = Vec3([m[0, i], m[1, i], m[2, i]])
95 func col*[T: Mat33](m: T, i: 0..2): auto = Vec3([m[0, i], m[1, i], m[2, i]])
96 func col*[T: Mat34](m: T, i: 0..3): auto = Vec3([m[0, i], m[1, i], m[2, i]])
97 func col*[T: Mat43](m: T, i: 0..2): auto = Vec4([m[0, i], m[1, i], m[2, i], m[3, i]])
98 func col*[T: Mat44](m: T, i: 0..3): auto = Vec4([m[0, i], m[1, i], m[2, i], m[3, i]])
99
100 func transposed*[T](m: Mat22[T]): Mat22[T] = Mat22[T](data: [
101 m[0, 0], m[1, 0],
102 m[0, 1], m[1, 1],
103 ])
104 func transposed*[T](m: Mat23[T]): Mat32[T] = Mat32[T](data: [
105 m[0, 0], m[1, 0],
106 m[0, 1], m[1, 1],
107 m[0, 2], m[1, 2],
108 ])
109 func transposed*[T](m: Mat32[T]): Mat23[T] = Mat23[T](data: [
110 m[0, 0], m[1, 0], m[2, 0],
111 m[0, 1], m[1, 1], m[2, 1],
112 ])
113 func transposed*[T](m: Mat33[T]): Mat33[T] = Mat33[T](data: [
114 m[0, 0], m[1, 0], m[2, 0],
115 m[0, 1], m[1, 1], m[2, 1],
116 m[0, 2], m[1, 2], m[2, 2],
117 ])
118 func transposed*[T](m: Mat43[T]): Mat34[T] = Mat34[T](data: [
119 m[0, 0], m[1, 0], m[2, 0], m[3, 0],
120 m[0, 1], m[1, 1], m[2, 1], m[3, 1],
121 m[0, 2], m[1, 2], m[2, 2], m[3, 2],
122 ])
123 func transposed*[T](m: Mat34[T]): Mat43[T] = Mat43[T](data: [
124 m[0, 0], m[1, 0], m[2, 0],
125 m[0, 1], m[1, 1], m[2, 1],
126 m[0, 2], m[1, 2], m[2, 2],
127 m[0, 3], m[1, 3], m[2, 3],
128 ])
129 func transposed*[T](m: Mat44[T]): Mat44[T] = Mat44[T](data: [
130 m[0, 0], m[1, 0], m[2, 0], m[3, 0],
131 m[0, 1], m[1, 1], m[2, 1], m[3, 1],
132 m[0, 2], m[1, 2], m[2, 2], m[3, 2],
133 m[0, 3], m[1, 3], m[2, 3], m[3, 3],
134 ])
135
136 # call e.g. Mat32[int]().initRandom() to get a random matrix
137 template makeRandomInit(mattype: typedesc) =
138 proc randomized*[T: SomeInteger](m: mattype[T]): mattype[T] =
139 for i in 0 ..< result.data.len:
140 result.data[i] = rand(low(typeof(m.data[0])) .. high(typeof(m.data[0])))
141 proc randomized*[T: SomeFloat](m: mattype[T]): mattype[T] =
142 for i in 0 ..< result.data.len:
143 result.data[i] = rand(1.0)
144
145 makeRandomInit(Mat22)
146 makeRandomInit(Mat23)
147 makeRandomInit(Mat32)
148 makeRandomInit(Mat33)
149 makeRandomInit(Mat34)
150 makeRandomInit(Mat43)
151 makeRandomInit(Mat44)