comparison semiconginev2/old/core/vector.nim @ 1218:56781cc0fc7c compiletime-tests

did: renamge main package
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 21:01:37 +0700
parents semicongine/old/core/vector.nim@a3eb305bcac2
children
comparison
equal deleted inserted replaced
1217:f819a874058f 1218:56781cc0fc7c
1 import std/random
2 import std/math
3 import std/strutils
4 import std/strformat
5 import std/macros
6 import std/typetraits
7 import std/tables
8
9 import ./vulkanapi
10
11 type
12 TVec1*[T: SomeNumber] = array[1, T]
13 TVec2*[T: SomeNumber] = array[2, T]
14 TVec3*[T: SomeNumber] = array[3, T]
15 TVec4*[T: SomeNumber] = array[4, T]
16 TVec* = TVec1|TVec2|TVec3|TVec4
17 Vec1f* = TVec1[float32]
18 Vec2f* = TVec2[float32]
19 Vec3f* = TVec3[float32]
20 Vec4f* = TVec4[float32]
21 Vec1i* = TVec1[int32]
22 Vec2i* = TVec2[int32]
23 Vec3i* = TVec3[int32]
24 Vec4i* = TVec4[int32]
25 Vec1u* = TVec1[uint32]
26 Vec2u* = TVec2[uint32]
27 Vec3u* = TVec3[uint32]
28 Vec4u* = TVec4[uint32]
29
30 converter ToVec1*[T: SomeNumber](orig: TVec3[T]|TVec4[T]): TVec1[T] =
31 TVec1[T]([orig[0]])
32 converter ToVec2*[T: SomeNumber](orig: TVec3[T]|TVec4[T]): TVec2[T] =
33 TVec2[T]([orig[0], orig[1]])
34 converter ToVec3*[T: SomeNumber](orig: TVec4[T]): TVec3[T] =
35 TVec3[T]([orig[0], orig[1], orig[2]])
36
37 func ToVec4*[T: SomeNumber](orig: TVec3[T], value: T = default(T)): TVec4[T] =
38 TVec4[T]([orig[0], orig[1], orig[2], value])
39 func ToVec3*[T: SomeNumber](orig: TVec2[T], value: T = default(T)): TVec3[T] =
40 TVec3[T]([orig[0], orig[1], value])
41 func ToVec2*[T: SomeNumber](orig: TVec1[T], value: T = default(T)): TVec2[T] =
42 TVec2[T]([orig[0], value])
43
44 # define some often used constants
45 func ConstOne1[T: SomeNumber](): auto {.compiletime.} = TVec1[T]([T(1)])
46 func ConstOne2[T: SomeNumber](): auto {.compiletime.} = TVec2[T]([T(1), T(1)])
47 func ConstOne3[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(1), T(1), T(1)])
48 func ConstOne4[T: SomeNumber](): auto {.compiletime.} = TVec4[T]([T(1), T(1), T(1), T(1)])
49 func ConstX[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(1), T(0), T(0)])
50 func ConstY[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(0), T(1), T(0)])
51 func ConstZ[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(0), T(0), T(1)])
52 func ConstR[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(1), T(0), T(0)])
53 func ConstG[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(0), T(1), T(0)])
54 func ConstB[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(0), T(0), T(1)])
55
56 func NewVec2f*(x = 0'f32, y = 0'f32): auto =
57 Vec2f([x, y])
58 func NewVec3f*(x = 0'f32, y = 0'f32, z = 0'f32): auto =
59 Vec3f([x, y, z])
60 func NewVec4f*(x = 0'f32, y = 0'f32, z = 0'f32, a = 0'f32): auto =
61 Vec4f([x, y, z, a])
62 func NewVec2i*(x = 0'i32, y = 0'i32): auto =
63 Vec2i([x, y])
64 func NewVec3i*(x = 0'i32, y = 0'i32, z = 0'i32): auto =
65 Vec3i([x, y, z])
66 func NewVec4i*(x = 0'i32, y = 0'i32, z = 0'i32, a = 0'i32): auto =
67 Vec4i([x, y, z, a])
68 func NewVec2u*(x = 0'u32, y = 0'u32): auto =
69 Vec2u([x, y])
70 func NewVec3u*(x = 0'u32, y = 0'u32, z = 0'u32): auto =
71 Vec3u([x, y, z])
72 func NewVec4u*(x = 0'u32, y = 0'u32, z = 0'u32, a = 0'u32): auto =
73 Vec4u([x, y, z, a])
74
75 # generates constants: Xf, Xf32, Xf64, Xi, Xi8, Xi16, Xi32, Xi64
76 # Also for Y, Z, R, G, B and One
77 # not sure if this is necessary or even a good idea...
78 macro generateAllConsts() =
79 result = newStmtList()
80 for component in ["X", "Y", "Z", "R", "G", "B", "One2", "One3", "One4"]:
81 for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]:
82 var typename = theType[0 .. 0]
83 if theType[^2].isDigit:
84 typename = typename & theType[^2]
85 if theType[^1].isDigit:
86 typename = typename & theType[^1]
87 result.add(
88 newConstStmt(
89 postfix(ident(component & typename), "*"),
90 newCall(nnkBracketExpr.newTree(ident("Const" & component), ident(theType)))
91 )
92 )
93
94 generateAllConsts()
95
96 const X* = ConstX[float32]()
97 const Y* = ConstY[float32]()
98 const Z* = ConstZ[float32]()
99 const One1* = ConstOne1[float32]()
100 const One2* = ConstOne2[float32]()
101 const One3* = ConstOne3[float32]()
102 const One4* = ConstOne4[float32]()
103
104 func NewVec1*[T](x: T): auto = TVec1([x])
105 func NewVec2*[T](x, y: T): auto = TVec2([x, y])
106 func NewVec3*[T](x, y, z: T): auto = TVec3([x, y, z])
107 func NewVec4*[T](x, y, z, w: T): auto = TVec4([x, y, z, w])
108
109 func To*[T](v: TVec1): auto = TVec1([T(v[0])])
110 func To*[T](v: TVec2): auto = TVec2([T(v[0]), T(v[1])])
111 func To*[T](v: TVec3): auto = TVec3([T(v[0]), T(v[1]), T(v[2])])
112 func To*[T](v: TVec4): auto = TVec4([T(v[0]), T(v[1]), T(v[2]), T(v[3])])
113
114 func toString[T](value: T): string =
115 var items: seq[string]
116 for item in value:
117 items.add(&"{item.float:.5f}")
118 & "(" & join(items, " ") & ")"
119
120 func `$`*(v: TVec1[SomeNumber]): string = toString[TVec1[SomeNumber]](v)
121 func `$`*(v: TVec2[SomeNumber]): string = toString[TVec2[SomeNumber]](v)
122 func `$`*(v: TVec3[SomeNumber]): string = toString[TVec3[SomeNumber]](v)
123 func `$`*(v: TVec4[SomeNumber]): string = toString[TVec4[SomeNumber]](v)
124
125 func Length*(vec: TVec1): auto = vec[0]
126 func Length*(vec: TVec2[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1])
127 func Length*(vec: TVec2[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1]))
128 func Length*(vec: TVec3[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2])
129 func Length*(vec: TVec3[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]))
130 func Length*(vec: TVec4[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3])
131 func Length*(vec: TVec4[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]))
132
133 func Normal*[T: SomeFloat](vec: TVec2[T]): auto =
134 TVec2[T]([vec[1], -vec[0]])
135
136 func Normalized*[T: SomeFloat](vec: TVec1[T]): auto =
137 return T(1)
138 func Normalized*[T: SomeFloat](vec: TVec2[T]): auto =
139 let l = vec.Length
140 if l == 0: vec
141 else: TVec2[T]([vec[0] / l, vec[1] / l])
142 func Normalized*[T: SomeFloat](vec: TVec3[T]): auto =
143 let l = vec.Length
144 if l == 0: return vec
145 else: TVec3[T]([vec[0] / l, vec[1] / l, vec[2] / l])
146 func Normalized*[T: SomeFloat](vec: TVec4[T]): auto =
147 let l = vec.Length
148 if l == 0: return vec
149 else: TVec4[T]([vec[0] / l, vec[1] / l, vec[2] / l, vec[3] / l])
150
151 # scalar operations
152 func `+`*(a: TVec1, b: SomeNumber): auto = TVec1([a[0] + b])
153 func `+`*(a: TVec2, b: SomeNumber): auto = TVec2([a[0] + b, a[1] + b])
154 func `+`*(a: TVec3, b: SomeNumber): auto = TVec3([a[0] + b, a[1] + b, a[2] + b])
155 func `+`*(a: TVec4, b: SomeNumber): auto = TVec4([a[0] + b, a[1] + b, a[2] + b, a[3] + b])
156 func `-`*(a: TVec1, b: SomeNumber): auto = TVec1([a[0] - b])
157 func `-`*(a: TVec2, b: SomeNumber): auto = TVec2([a[0] - b, a[1] - b])
158 func `-`*(a: TVec3, b: SomeNumber): auto = TVec3([a[0] - b, a[1] - b, a[2] - b])
159 func `-`*(a: TVec4, b: SomeNumber): auto = TVec4([a[0] - b, a[1] - b, a[2] - b,
160 a[3] - b])
161 func `*`*(a: TVec1, b: SomeNumber): auto = TVec1([a[0] * b])
162 func `*`*(a: TVec2, b: SomeNumber): auto = TVec2([a[0] * b, a[1] * b])
163 func `*`*(a: TVec3, b: SomeNumber): auto = TVec3([a[0] * b, a[1] * b, a[2] * b])
164 func `*`*(a: TVec4, b: SomeNumber): auto = TVec4([a[0] * b, a[1] * b, a[2] * b,
165 a[3] * b])
166 func `/`*[T: SomeInteger](a: TVec1[T], b: SomeInteger): auto = TVec1([a[0] div b])
167 func `/`*[T: SomeFloat](a: TVec1[T], b: SomeFloat): auto = TVec1([a[0] / b])
168 func `/`*[T: SomeInteger](a: TVec2[T], b: SomeInteger): auto = TVec2([a[0] div b, a[1] div b])
169 func `/`*[T: SomeFloat](a: TVec2[T], b: SomeFloat): auto = TVec2([a[0] / b, a[1] / b])
170 func `/`*[T: SomeInteger](a: TVec3[T], b: SomeInteger): auto = TVec3([a[0] div b, a[1] div b, a[2] div b])
171 func `/`*[T: SomeFloat](a: TVec3[T], b: SomeFloat): auto = TVec3([a[0] / b, a[1] / b, a[2] / b])
172 func `/`*[T: SomeInteger](a: TVec4[T], b: SomeInteger): auto = TVec4([a[0] div b, a[1] div b, a[2] div b, a[3] div b])
173 func `/`*[T: SomeFloat](a: TVec4[T], b: SomeFloat): auto = TVec4([a[0] / b, a[1] / b, a[2] / b, a[3] / b])
174
175 func `+`*(a: SomeNumber, b: TVec1): auto = TVec1([a + b[0]])
176 func `+`*(a: SomeNumber, b: TVec2): auto = TVec2([a + b[0], a + b[1]])
177 func `+`*(a: SomeNumber, b: TVec3): auto = TVec3([a + b[0], a + b[1], a + b[2]])
178 func `+`*(a: SomeNumber, b: TVec4): auto = TVec4([a + b[0], a + b[1], a + b[2], a + b[3]])
179 func `-`*(a: SomeNumber, b: TVec1): auto = TVec1([a - b[0]])
180 func `-`*(a: SomeNumber, b: TVec2): auto = TVec2([a - b[0], a - b[1]])
181 func `-`*(a: SomeNumber, b: TVec3): auto = TVec3([a - b[0], a - b[1], a - b[2]])
182 func `-`*(a: SomeNumber, b: TVec4): auto = TVec4([a - b[0], a - b[1], a - b[2], a - b[3]])
183 func `*`*(a: SomeNumber, b: TVec1): auto = TVec1([a * b[0]])
184 func `*`*(a: SomeNumber, b: TVec2): auto = TVec2([a * b[0], a * b[1]])
185 func `*`*(a: SomeNumber, b: TVec3): auto = TVec3([a * b[0], a * b[1], a * b[2]])
186 func `*`*(a: SomeNumber, b: TVec4): auto = TVec4([a * b[0], a * b[1], a * b[2], a * b[3]])
187 func `/`*[T: SomeInteger](a: SomeInteger, b: TVec1[T]): auto = TVec1([a div b[0]])
188 func `/`*[T: SomeFloat](a: SomeFloat, b: TVec1[T]): auto = TVec1([a / b[0]])
189 func `/`*[T: SomeInteger](a: SomeInteger, b: TVec2[T]): auto = TVec2([a div b[0], a div b[1]])
190 func `/`*[T: SomeFloat](a: SomeFloat, b: TVec2[T]): auto = TVec2([a / b[0], a / b[1]])
191 func `/`*[T: SomeInteger](a: SomeInteger, b: TVec3[T]): auto = TVec3([a div b[0], a div b[1], a div b[2]])
192 func `/`*[T: SomeFloat](a: SomeFloat, b: TVec3[T]): auto = TVec3([a / b[0], a / b[1], a / b[2]])
193 func `/`*[T: SomeInteger](a: SomeInteger, b: TVec4[T]): auto = TVec4([a div b[
194 0], a div b[1], a div b[2], a div b[3]])
195 func `/`*[T: SomeFloat](a: SomeFloat, b: TVec4[T]): auto = TVec4([a / b[0], a /
196 b[1], a / b[2], a / b[3]])
197
198 # compontent-wise operations
199 func `+`*(a, b: TVec1): auto = TVec1([a[0] + b[0]])
200 func `+`*(a, b: TVec2): auto = TVec2([a[0] + b[0], a[1] + b[1]])
201 func `+`*(a, b: TVec3): auto = TVec3([a[0] + b[0], a[1] + b[1], a[2] + b[2]])
202 func `+`*(a, b: TVec4): auto = TVec4([a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]])
203 func `-`*(a: TVec1): auto = TVec1([-a[0]])
204 func `-`*(a: TVec2): auto = TVec2([-a[0], -a[1]])
205 func `-`*(a: TVec3): auto = TVec3([-a[0], -a[1], -a[2]])
206 func `-`*(a: TVec4): auto = TVec4([-a[0], -a[1], -a[2], -a[3]])
207 func `-`*(a, b: TVec1): auto = TVec1([a[0] - b[0]])
208 func `-`*(a, b: TVec2): auto = TVec2([a[0] - b[0], a[1] - b[1]])
209 func `-`*(a, b: TVec3): auto = TVec3([a[0] - b[0], a[1] - b[1], a[2] - b[2]])
210 func `-`*(a, b: TVec4): auto = TVec4([a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]])
211 func `*`*(a, b: TVec1): auto = TVec1([a[0] * b[0]])
212 func `*`*(a, b: TVec2): auto = TVec2([a[0] * b[0], a[1] * b[1]])
213 func `*`*(a, b: TVec3): auto = TVec3([a[0] * b[0], a[1] * b[1], a[2] * b[2]])
214 func `*`*(a, b: TVec4): auto = TVec4([a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]])
215 func `/`*[T: SomeInteger](a, b: TVec1[T]): auto = TVec1([a[0] div b[0]])
216 func `/`*[T: SomeFloat](a, b: TVec1[T]): auto = TVec1([a[0] / b[0]])
217 func `/`*[T: SomeInteger](a, b: TVec2[T]): auto = TVec2([a[0] div b[0], a[1] div b[1]])
218 func `/`*[T: SomeFloat](a, b: TVec2[T]): auto = TVec2([a[0] / b[0], a[1] / b[1]])
219 func `/`*[T: SomeInteger](a, b: TVec3[T]): auto = TVec3([a[0] div b[0], a[1] div b[1], a[2] div b[2]])
220 func `/`*[T: SomeFloat](a, b: TVec3[T]): auto = TVec3([a[0] / b[0], a[1] / b[1], a[2] / b[2]])
221 func `/`*[T: SomeInteger](a, b: TVec4[T]): auto = TVec4([a[0] div b[0], a[1] div b[1], a[2] div b[2], a[3] div b[3]])
222 func `/`*[T: SomeFloat](a, b: TVec4[T]): auto = TVec4([a[0] / b[0], a[1] / b[1], a[2] / b[2], a[3] / b[3]])
223
224 # assignment operations, scalar
225 func `+=`*(a: var TVec1, b: SomeNumber) = a = a + b
226 func `+=`*(a: var TVec2, b: SomeNumber) = a = a + b
227 func `+=`*(a: var TVec3, b: SomeNumber) = a = a + b
228 func `+=`*(a: var TVec4, b: SomeNumber) = a = a + b
229 func `-=`*(a: var TVec1, b: SomeNumber) = a = a - b
230 func `-=`*(a: var TVec2, b: SomeNumber) = a = a - b
231 func `-=`*(a: var TVec3, b: SomeNumber) = a = a - b
232 func `-=`*(a: var TVec4, b: SomeNumber) = a = a - b
233 func `*=`*(a: var TVec1, b: SomeNumber) = a = a * b
234 func `*=`*(a: var TVec2, b: SomeNumber) = a = a * b
235 func `*=`*(a: var TVec3, b: SomeNumber) = a = a * b
236 func `*=`*(a: var TVec4, b: SomeNumber) = a = a * b
237 func `/=`*(a: var TVec1, b: SomeNumber) = a = a / b
238 func `/=`*(a: var TVec2, b: SomeNumber) = a = a / b
239 func `/=`*(a: var TVec3, b: SomeNumber) = a = a / b
240 func `/=`*(a: var TVec4, b: SomeNumber) = a = a / b
241 # assignment operations, vector
242 func `+=`*(a: var TVec1, b: TVec1) = a = a + b
243 func `+=`*(a: var TVec2, b: TVec2) = a = a + b
244 func `+=`*(a: var TVec3, b: TVec3) = a = a + b
245 func `+=`*(a: var TVec4, b: TVec4) = a = a + b
246 func `-=`*(a: var TVec1, b: TVec1) = a = a - b
247 func `-=`*(a: var TVec2, b: TVec2) = a = a - b
248 func `-=`*(a: var TVec3, b: TVec3) = a = a - b
249 func `-=`*(a: var TVec4, b: TVec4) = a = a - b
250 func `*=`*(a: var TVec1, b: TVec1) = a = a * b
251 func `*=`*(a: var TVec2, b: TVec2) = a = a * b
252 func `*=`*(a: var TVec3, b: TVec3) = a = a * b
253 func `*=`*(a: var TVec4, b: TVec4) = a = a * b
254 func `/=`*(a: var TVec1, b: TVec1) = a = a / b
255 func `/=`*(a: var TVec2, b: TVec2) = a = a / b
256 func `/=`*(a: var TVec3, b: TVec3) = a = a / b
257 func `/=`*(a: var TVec4, b: TVec4) = a = a / b
258
259
260 # special operations
261 func Pow*(a: TVec1, b: SomeNumber): auto =
262 TVec1([pow(a[0], b)])
263 func Pow*(a: TVec2, b: SomeNumber): auto =
264 TVec2([pow(a[0], b), pow(a[1], b)])
265 func Pow*(a: TVec3, b: SomeNumber): auto =
266 TVec3([pow(a[0], b), pow(a[1], b), pow(a[2], b)])
267 func Pow*(a: TVec4, b: SomeNumber): auto =
268 TVec4([pow(a[0], b), pow(a[1], b), pow(a[2], b), pow(a[3], b)])
269 func Dot*(a, b: TVec1): auto = a[0] * b[0]
270 func Dot*(a, b: TVec2): auto = a[0] * b[0] + a[1] * b[1]
271 func Dot*(a, b: TVec3): auto = a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
272 func Dot*(a, b: TVec4): auto = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]
273 func Cross*(a, b: TVec3): auto = TVec3([
274 a[1] * b[2] - a[2] * b[1],
275 a[2] * b[0] - a[0] * b[2],
276 a[0] * b[1] - a[1] * b[0],
277 ])
278
279
280 # macro to allow creation of new vectors by specifying vector components as attributes
281 # e.g. myVec.xxy will return a new Vec3 that contains the components x, x an y of the original vector
282 # (instead of x, y, z for a simple copy)
283 proc vectorAttributeAccessor(accessor: string): seq[NimNode] =
284 const ACCESSOR_INDICES = {
285 'x': 0,
286 'y': 1,
287 'z': 2,
288 'w': 3,
289 'r': 0,
290 'g': 1,
291 'b': 2,
292 'a': 3,
293 }.toTable
294 var getterCode, setterCode: NimNode
295 let accessorvalue = accessor
296
297 if accessorvalue.len == 0:
298 raise newException(Exception, "empty attribute")
299 elif accessorvalue.len == 1:
300 getterCode = nnkBracketExpr.newTree(ident("vec"), newLit(ACCESSOR_INDICES[accessorvalue[0]]))
301 setterCode = nnkStmtList.newTree(
302 nnkAsgn.newTree(
303 nnkBracketExpr.newTree(ident("vec"), newLit(ACCESSOR_INDICES[accessorvalue[0]])), ident("value"))
304 )
305 if accessorvalue.len > 1:
306 var attrs = nnkBracket.newTree()
307 for attrname in accessorvalue:
308 attrs.add(nnkBracketExpr.newTree(ident("vec"), newLit(ACCESSOR_INDICES[attrname])))
309 getterCode = nnkCall.newTree(ident("TVec" & $accessorvalue.len), attrs)
310 setterCode = nnkStmtList.newTree()
311 var i = 0
312 for attrname in accessorvalue:
313 setterCode.add nnkAsgn.newTree(
314 nnkBracketExpr.newTree(ident("vec"), newLit(ACCESSOR_INDICES[attrname])),
315 nnkBracketExpr.newTree(ident("value"), newLit(i)),
316 )
317 inc i
318
319 result.add newProc(
320 name = nnkPostfix.newTree(ident("*"), ident(accessor)),
321 params = [ident("auto"), nnkIdentDefs.newTree(ident("vec"), ident("TVec"), newEmptyNode())],
322 body = newStmtList(getterCode),
323 procType = nnkFuncDef,
324 )
325
326 result.add nnkFuncDef.newTree(
327 nnkPostfix.newTree(
328 newIdentNode("*"),
329 nnkAccQuoted.newTree(newIdentNode(accessor), newIdentNode("="))
330 ),
331 newEmptyNode(),
332 nnkGenericParams.newTree(nnkIdentDefs.newTree(newIdentNode("T"), newEmptyNode(), newEmptyNode())),
333 nnkFormalParams.newTree(
334 newEmptyNode(),
335 nnkIdentDefs.newTree(newIdentNode("vec"), nnkVarTy.newTree(newIdentNode("TVec")), newEmptyNode()),
336 nnkIdentDefs.newTree(newIdentNode("value"), newIdentNode("T"), newEmptyNode())
337 ),
338 newEmptyNode(),
339 newEmptyNode(),
340 setterCode
341 )
342
343 macro createVectorAttribAccessorFuncs() =
344 const COORD_ATTRS = ["x", "y", "z", "w"]
345 const COLOR_ATTRS = ["r", "g", "b", "a"]
346 result = nnkStmtList.newTree()
347 for attlist in [COORD_ATTRS, COLOR_ATTRS]:
348 for i in attlist:
349 result.add(vectorAttributeAccessor(i))
350 for j in attlist:
351 result.add(vectorAttributeAccessor(i & j))
352 for k in attlist:
353 result.add(vectorAttributeAccessor(i & j & k))
354 for l in attlist:
355 result.add(vectorAttributeAccessor(i & j & k & l))
356
357 createVectorAttribAccessorFuncs()
358
359 # call e.g. Vec2[int]().randomized() to get a random matrix
360 template makeRandomInit(mattype: typedesc) =
361 proc Randomized*[T: SomeInteger](m: mattype[T]): mattype[T] =
362 for i in 0 ..< result.len:
363 result[i] = rand(low(typeof(m[0])) .. high(typeof(m[0])))
364 proc Randomized*[T: SomeFloat](m: mattype[T]): mattype[T] =
365 for i in 0 ..< result.len:
366 result[i] = rand(1.0)
367
368 makeRandomInit(TVec1)
369 makeRandomInit(TVec2)
370 makeRandomInit(TVec3)
371 makeRandomInit(TVec4)
372
373 converter Vec2VkExtent*(vec: TVec2[uint32]): VkExtent2D = VkExtent2D(width: vec[0], height: vec[1])
374 converter Vec3VkExtent*(vec: TVec2[uint32]): VkExtent3D = VkExtent3D(width: vec[0], height: vec[1], depth: vec[2])
375
376 func AngleBetween*(a, b: Vec3f): float32 =
377 arccos(a.Dot(b) / (a.Length * b.Length))