Mercurial > games > semicongine
comparison old_tests/test_matrix.nim @ 1227:4d97cfc4888b
merge
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 23:45:43 +0700 |
parents | 6360c8d17ce0 |
children |
comparison
equal
deleted
inserted
replaced
1170:2addc5f6804f | 1227:4d97cfc4888b |
---|---|
1 import random | |
2 import math | |
3 | |
4 import semicongine | |
5 | |
6 | |
7 proc echoInfo(v: TVec) = | |
8 echo v | |
9 echo " Length: ", v.length | |
10 echo " Normlized: ", v.normalized | |
11 echo " negated: ", -v | |
12 | |
13 proc echoAdd[T, U](v1: T, v2: U) = | |
14 echo v1, " + ", v2, " = ", v1 + v2 | |
15 proc echoSub[T, U](v1: T, v2: U) = | |
16 echo v1, " - ", v2, " = ", v1 - v2 | |
17 proc echoMul[T, U](v1: T, v2: U) = | |
18 echo v1, " * ", v2, " = ", v1 * v2 | |
19 proc echoDiv[T, U](v1: T, v2: U) = | |
20 echo v1, " / ", v2, " = ", v1 / v2 | |
21 proc echoDot[T, U](v1: T, v2: U) = | |
22 echo v1, " o ", v2, " = ", v1.dot(v2) | |
23 proc echoCross[T, U](v1: T, v2: U) = | |
24 echo v1, " x ", v2, " = ", v1.cross(v2) | |
25 | |
26 proc randVec2I(): auto = NewVec2(rand(1 .. 10), rand(1 .. 10)) | |
27 proc randVec2F(): auto = NewVec2(rand(10'f) + 0.01, rand(10'f) + 0.01) | |
28 proc randVec3I(): auto = NewVec3(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10)) | |
29 proc randVec3F(): auto = NewVec3(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01) | |
30 proc randVec4I(): auto = NewVec4(rand(1 .. 10), rand(1 .. 10), rand(1 .. 10), rand(1 .. 10)) | |
31 proc randVec4F(): auto = NewVec4(rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01, rand(10'f) + 0.01) | |
32 | |
33 | |
34 template withAllIntegerMats(stuff: untyped) = | |
35 stuff(TMat2[int32]) | |
36 stuff(TMat23[int32]) | |
37 stuff(TMat32[int32]) | |
38 stuff(TMat3[int32]) | |
39 stuff(TMat34[int32]) | |
40 stuff(TMat43[int32]) | |
41 stuff(TMat4[int32]) | |
42 stuff(TMat2[int64]) | |
43 stuff(TMat23[int64]) | |
44 stuff(TMat32[int64]) | |
45 stuff(TMat3[int64]) | |
46 stuff(TMat34[int64]) | |
47 stuff(TMat43[int64]) | |
48 stuff(TMat4[int64]) | |
49 | |
50 template withAllFloatMats(stuff: untyped) = | |
51 stuff(TMat2[float32]) | |
52 stuff(TMat23[float32]) | |
53 stuff(TMat32[float32]) | |
54 stuff(TMat3[float32]) | |
55 stuff(TMat34[float32]) | |
56 stuff(TMat43[float32]) | |
57 stuff(TMat4[float32]) | |
58 stuff(TMat2[float64]) | |
59 stuff(TMat23[float64]) | |
60 stuff(TMat32[float64]) | |
61 stuff(TMat3[float64]) | |
62 stuff(TMat34[float64]) | |
63 stuff(TMat43[float64]) | |
64 stuff(TMat4[float64]) | |
65 | |
66 template withAllMats(stuff: untyped) = | |
67 stuff(TMat2[int]) | |
68 stuff(TMat23[int]) | |
69 stuff(TMat32[int]) | |
70 stuff(TMat3[int]) | |
71 stuff(TMat34[int]) | |
72 stuff(TMat43[int]) | |
73 stuff(TMat4[int]) | |
74 stuff(TMat2[float]) | |
75 stuff(TMat23[float]) | |
76 stuff(TMat32[float]) | |
77 stuff(TMat3[float]) | |
78 stuff(TMat34[float]) | |
79 stuff(TMat43[float]) | |
80 stuff(TMat4[float]) | |
81 | |
82 template testTranspose(t: typedesc) = | |
83 echo "testTranspose: ", t | |
84 let m = t().Randomized() | |
85 assert m == m.Transposed().Transposed() | |
86 | |
87 template testInversed(t: typedesc) = | |
88 echo "testTranspose: ", t | |
89 let m = t().Randomized() | |
90 var unit = t() | |
91 for i in unit.RowCount: | |
92 unit[i][i] = 1 | |
93 assert m.Transposed() * m == unit | |
94 | |
95 template testAssignI(t: typedesc) = | |
96 echo "testAssignI: ", t | |
97 var m = t() | |
98 for i in 0 ..< t.data.len: | |
99 m[rand(0 ..< t.RowCount), rand(0 ..< t.ColumnCount)] = rand(0'i32 .. 100'i32) | |
100 | |
101 template testAssignF(t: typedesc) = | |
102 echo "testAssignF: ", t | |
103 var m = t() | |
104 for i in 0 ..< t.data.len: | |
105 m[rand(0 ..< t.RowCount), rand(0 ..< t.ColumnCount)] = rand(100'f) | |
106 | |
107 template testRowCols(t: typedesc) = | |
108 echo "testRowCols: ", t | |
109 var m = t().Randomized() | |
110 for i in 0 ..< t.RowCount: | |
111 echo m.Row(i) | |
112 for i in 0 ..< t.ColumnCount: | |
113 echo m.Col(i) | |
114 | |
115 | |
116 proc testMatrix() = | |
117 withAllMats(testTranspose) | |
118 withAllIntegerMats(testAssignI) | |
119 withAllFloatMats(testAssignF) | |
120 withAllMats(testRowCols) | |
121 | |
122 echo Unit2 | |
123 echo Unit2i | |
124 echo Unit2i8 | |
125 echo Unit2i16 | |
126 echo Unit2i32 | |
127 echo Unit2i64 | |
128 | |
129 echo Unit3 | |
130 echo Unit3i | |
131 echo Unit3i8 | |
132 echo Unit3i16 | |
133 echo Unit3i32 | |
134 echo Unit3i64 | |
135 | |
136 echo Unit4 | |
137 echo Unit4i | |
138 echo Unit4i8 | |
139 echo Unit4i16 | |
140 echo Unit4i32 | |
141 echo Unit4i64 | |
142 | |
143 echo TMat2[float32]().Randomized() * One2.Randomized() | |
144 echo TMat3[float32]().Randomized() * One3.Randomized() | |
145 echo TMat4[float32]().Randomized() * One4.Randomized() | |
146 | |
147 echo float32(rand(1'f32)) * TMat2[float32]().Randomized() | |
148 echo TMat2[float]().Randomized() * rand(1'f) | |
149 echo TMat2[float]().Randomized() * rand(1'f) | |
150 echo TMat23[float]().Randomized() * rand(1'f) | |
151 echo TMat23[float]().Randomized() * rand(1'f) | |
152 echo TMat3[float]().Randomized() * rand(1'f) | |
153 echo TMat34[float]().Randomized() * rand(1'f) | |
154 echo TMat43[float]().Randomized() * rand(1'f) | |
155 echo TMat4[float]().Randomized() * rand(1'f) | |
156 | |
157 randomize() | |
158 testMatrix() |