annotate semiconginev2/core/vector.nim @ 1222:3c3d4d7a8ecd compiletime-tests

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