Mercurial > games > semicongine
annotate src/vector.nim @ 474:d4750f2e0760
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 |
rev | line source |
---|---|
474
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/math |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/strutils |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/macros |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/typetraits |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/tables |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 type |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 Vec2*[T: SomeNumber] = array[2, T] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 Vec3*[T: SomeNumber] = array[3, T] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 Vec4*[T: SomeNumber] = array[4, T] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 Vec* = Vec2|Vec3|Vec4 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 # define some often used constants |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 func ConstX[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(0), T(0)]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 func ConstY[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(1), T(0)]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 func ConstZ[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(0), T(1)]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 func ConstR[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(0), T(0)]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 func ConstG[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(1), T(0)]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 func ConstB[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(0), T(1)]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 # generates constants: Xf, Xf32, Xf64, Xi, Xi8, Xi16, Xi32, Xi64 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 # Also for Y, Z, R, G, B |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 # not sure if this is necessary or even a good idea... |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 macro generateAllConsts() = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 result = newStmtList() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 for component in ["X", "Y", "Z", "R", "G", "B"]: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 var typename = theType[0 .. 0] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 if theType[^2].isDigit: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 typename = typename & theType[^2] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 if theType[^1].isDigit: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 typename = typename & theType[^1] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 result.add( |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 newConstStmt( |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 postfix(ident(component & typename), "*"), |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 newCall(nnkBracketExpr.newTree(ident("Const" & component), ident(theType))) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 ) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 ) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 generateAllConsts() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 const X* = ConstX[float]() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 const Y* = ConstY[float]() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 const Z* = ConstZ[float]() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 func newVec2*[T](x, y: T): auto = Vec2([x, y]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 func newVec3*[T](x, y, z: T): auto = Vec3([x, y, z]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 func newVec4*[T](x, y, z, w: T): auto = Vec4([x, y, z, w]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 func to*[T](v: Vec2): auto = Vec2([T(v[0]), T(v[1])]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 func to*[T](v: Vec3): auto = Vec3([T(v[0]), T(v[1]), T(v[2])]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 func to*[T](v: Vec4): auto = Vec4([T(v[0]), T(v[1]), T(v[2]), T(v[3])]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 func toString[T](value: T): string = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 var items: seq[string] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 for item in value: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 items.add($item) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 $T & "(" & join(items, " ") & ")" |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
60 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
61 func `$`*(v: Vec2[SomeNumber]): string = toString[Vec2[SomeNumber]](v) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 func `$`*(v: Vec3[SomeNumber]): string = toString[Vec3[SomeNumber]](v) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
63 func `$`*(v: Vec4[SomeNumber]): string = toString[Vec4[SomeNumber]](v) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 func length*(vec: Vec2[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 func length*(vec: Vec2[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1])) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
67 func length*(vec: Vec3[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 func length*(vec: Vec3[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2])) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 func length*(vec: Vec4[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 func length*(vec: Vec4[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3])) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 func normalized*[T](vec: Vec2[T]): auto = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
73 let l = vec.length |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 when T is SomeFloat: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
75 Vec2[T]([vec[0] / l, vec[1] / l]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
76 else: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 Vec2[float]([float(vec[0]) / l, float(vec[1]) / l]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 func normalized*[T](vec: Vec3[T]): auto = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
79 let l = vec.length |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
80 when T is SomeFloat: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
81 Vec3[T]([vec[0] / l, vec[1] / l, vec[2] / l]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
82 else: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
83 Vec3[float]([float(vec[0]) / l, float(vec[1]) / l, float(vec[2]) / l]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
84 func normalized*[T](vec: Vec4[T]): auto = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
85 let l = vec.length |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
86 when T is SomeFloat: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
87 Vec4[T]([vec[0] / l, vec[1] / l, vec[2] / l, vec[3] / l]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
88 else: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
89 Vec4[float]([float(vec[0]) / l, float(vec[1]) / l, float(vec[2]) / l, float(vec[3]) / l]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
90 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 # scalar operations |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 func `+`*(a: Vec2, b: SomeNumber): auto = Vec2([a[0] + b, a[1] + b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
93 func `+`*(a: Vec3, b: SomeNumber): auto = Vec3([a[0] + b, a[1] + b, a[2] + b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 func `+`*(a: Vec4, b: SomeNumber): auto = Vec4([a[0] + b, a[1] + b, a[2] + b, a[3] + b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 func `-`*(a: Vec2, b: SomeNumber): auto = Vec2([a[0] - b, a[1] - b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 func `-`*(a: Vec3, b: SomeNumber): auto = Vec3([a[0] - b, a[1] - b, a[2] - b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 func `-`*(a: Vec4, b: SomeNumber): auto = Vec4([a[0] - b, a[1] - b, a[2] - b, a[3] - b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 func `*`*(a: Vec2, b: SomeNumber): auto = Vec2([a[0] * b, a[1] * b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
99 func `*`*(a: Vec3, b: SomeNumber): auto = Vec3([a[0] * b, a[1] * b, a[2] * b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 func `*`*(a: Vec4, b: SomeNumber): auto = Vec4([a[0] * b, a[1] * b, a[2] * b, a[3] * b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
101 func `/`*[T: SomeInteger](a: Vec2[T], b: SomeInteger): auto = Vec2([a[0] div b, a[1] div b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 func `/`*[T: SomeFloat](a: Vec2[T], b: SomeFloat): auto = Vec2([a[0] / b, a[1] / b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
103 func `/`*[T: SomeInteger](a: Vec3[T], b: SomeInteger): auto = Vec3([a[0] div b, a[1] div b, a[2] div b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 func `/`*[T: SomeFloat](a: Vec3[T], b: SomeFloat): auto = Vec3([a[0] / b, a[1] / b, a[2] / b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
105 func `/`*[T: SomeInteger](a: Vec4[T], b: SomeInteger): auto = Vec4([a[0] div b, a[1] div b, a[2] div b, a[3] div b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
106 func `/`*[T: SomeFloat](a: Vec4[T], b: SomeFloat): auto = Vec4([a[0] / b, a[1] / b, a[2] / b, a[3] / b]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
107 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 func `+`*(a: SomeNumber, b: Vec2): auto = Vec2([a + b[0], a + b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 func `+`*(a: SomeNumber, b: Vec3): auto = Vec3([a + b[0], a + b[1], a + b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
110 func `+`*(a: SomeNumber, b: Vec4): auto = Vec4([a + b[0], a + b[1], a + b[2], a + b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
111 func `-`*(a: SomeNumber, b: Vec2): auto = Vec2([a - b[0], a - b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
112 func `-`*(a: SomeNumber, b: Vec3): auto = Vec3([a - b[0], a - b[1], a - b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
113 func `-`*(a: SomeNumber, b: Vec4): auto = Vec4([a - b[0], a - b[1], a - b[2], a - b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 func `*`*(a: SomeNumber, b: Vec2): auto = Vec2([a * b[0], a * b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
115 func `*`*(a: SomeNumber, b: Vec3): auto = Vec3([a * b[0], a * b[1], a * b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
116 func `*`*(a: SomeNumber, b: Vec4): auto = Vec4([a * b[0], a * b[1], a * b[2], a * b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
117 func `/`*[T: SomeInteger](a: SomeInteger, b: Vec2[T]): auto = Vec2([a div b[0], a div b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
118 func `/`*[T: SomeFloat](a: SomeFloat, b: Vec2[T]): auto = Vec2([a / b[0], a / b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
119 func `/`*[T: SomeInteger](a: SomeInteger, b: Vec3[T]): auto = Vec3([a div b[0], a div b[1], a div b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
120 func `/`*[T: SomeFloat](a: SomeFloat, b: Vec3[T]): auto = Vec3([a / b[0], a / b[1], a / b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
121 func `/`*[T: SomeInteger](a: SomeInteger, b: Vec4[T]): auto = Vec4([a div b[0], a div b[1], a div b[2], a div b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
122 func `/`*[T: SomeFloat](a: SomeFloat, b: Vec4[T]): auto = Vec4([a / b[0], a / b[1], a / b[2], a / b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
123 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
124 # compontent-wise operations |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
125 func `+`*(a, b: Vec2): auto = Vec2([a[0] + b[0], a[1] + b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
126 func `+`*(a, b: Vec3): auto = Vec3([a[0] + b[0], a[1] + b[1], a[2] + b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
127 func `+`*(a, b: Vec4): auto = Vec4([a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
128 func `-`*(a: Vec2): auto = Vec2([-a[0], -a[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
129 func `-`*(a: Vec3): auto = Vec3([-a[0], -a[1], -a[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
130 func `-`*(a: Vec4): auto = Vec4([-a[0], -a[1], -a[2], -a[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
131 func `-`*(a, b: Vec2): auto = Vec2([a[0] - b[0], a[1] - b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
132 func `-`*(a, b: Vec3): auto = Vec3([a[0] - b[0], a[1] - b[1], a[2] - b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
133 func `-`*(a, b: Vec4): auto = Vec4([a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
134 func `*`*(a, b: Vec2): auto = Vec2([a[0] * b[0], a[1] * b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
135 func `*`*(a, b: Vec3): auto = Vec3([a[0] * b[0], a[1] * b[1], a[2] * b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
136 func `*`*(a, b: Vec4): auto = Vec4([a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
137 func `/`*[T: SomeInteger](a, b: Vec2[T]): auto = Vec2([a[0] div b[0], a[1] div b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
138 func `/`*[T: SomeFloat](a, b: Vec2[T]): auto = Vec2([a[0] / b[0], a[1] / b[1]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
139 func `/`*[T: SomeInteger](a, b: Vec3[T]): auto = Vec3([a[0] div b[0], a[1] div b[1], a[2] div b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
140 func `/`*[T: SomeFloat](a, b: Vec3[T]): auto = Vec3([a[0] / b[0], a[1] / b[1], a[2] / b[2]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
141 func `/`*[T: SomeInteger](a, b: Vec4[T]): auto = Vec4([a[0] div b[0], a[1] div b[1], a[2] div b[2], a[3] div b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
142 func `/`*[T: SomeFloat](a, b: Vec4[T]): auto = Vec4([a[0] / b[0], a[1] / b[1], a[2] / b[2], a[3] / b[3]]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
143 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
144 # special operations |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
145 func dot*(a, b: Vec2): auto = a[0] * b[0] + a[1] * b[1] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
146 func dot*(a, b: Vec3): auto = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
147 func dot*(a, b: Vec4): auto = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
148 func cross*(a, b: Vec3): auto = Vec3([ |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
149 a[1] * b[2] - a[2] * b[1], |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
150 a[2] * b[0] - a[0] * b[2], |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
151 a[0] * b[1] - a[1] * b[0], |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
152 ]) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
153 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
154 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
155 # macro to allow creation of new vectors by specifying vector components as attributes |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
156 # e.g. myVec.xxy will return a new Vec3 that contains the components x, x an y of the original vector |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
157 # (instead of x, y, z for a simple copy) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
158 proc vectorAttributeAccessor(accessor: string): NimNode = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
159 const ACCESSOR_INDICES = { |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
160 'x': 0, |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
161 'y': 1, |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
162 'z': 2, |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
163 'w': 3, |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
164 'r': 0, |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
165 'g': 1, |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
166 'b': 2, |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
167 'a': 3, |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
168 }.toTable |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
169 var ret: NimNode |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
170 let accessorvalue = accessor |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
171 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
172 if accessorvalue.len == 0: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
173 raise newException(Exception, "empty attribute") |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
174 elif accessorvalue.len == 1: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
175 ret = nnkBracket.newTree(ident("value"), newLit(ACCESSOR_INDICES[accessorvalue[0]])) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
176 if accessorvalue.len > 1: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
177 var attrs = nnkBracket.newTree() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
178 for attrname in accessorvalue: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
179 attrs.add(nnkBracketExpr.newTree(ident("value"), newLit(ACCESSOR_INDICES[attrname]))) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
180 ret = nnkCall.newTree(ident("Vec" & $accessorvalue.len), attrs) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
181 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
182 newProc( |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
183 name=nnkPostfix.newTree(ident("*"), ident(accessor)), |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
184 params=[ident("auto"), nnkIdentDefs.newTree(ident("value"), ident("Vec"), newEmptyNode())], |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
185 body=newStmtList(ret), |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
186 procType = nnkFuncDef, |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
187 ) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
188 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
189 macro createVectorAttribAccessorFuncs() = |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
190 const COORD_ATTRS = ["x", "y", "z", "w"] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
191 const COLOR_ATTRS = ["r", "g", "b", "a"] |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
192 result = nnkStmtList.newTree() |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
193 for attlist in [COORD_ATTRS, COLOR_ATTRS]: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
194 for i in attlist: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
195 result.add(vectorAttributeAccessor(i)) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
196 for j in attlist: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
197 result.add(vectorAttributeAccessor(i & j)) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
198 for k in attlist: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
199 result.add(vectorAttributeAccessor(i & j & k)) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
200 for l in attlist: |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
201 result.add(vectorAttributeAccessor(i & j & k & l)) |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
202 |
d4750f2e0760
add: vector/matrix modules, tests, nim config script
Sam <sam@basx.dev>
parents:
diff
changeset
|
203 createVectorAttribAccessorFuncs() |