Mercurial > games > semicongine
comparison src/vector.nim @ 16:617c6dcddbe2
add: matrix multiplications, tests
| author | Sam <sam@basx.dev> |
|---|---|
| date | Fri, 30 Dec 2022 15:56:17 +0700 |
| parents | a571db114152 |
| children |
comparison
equal
deleted
inserted
replaced
| 15:dde536a70483 | 16:617c6dcddbe2 |
|---|---|
| 1 import std/random | |
| 1 import std/math | 2 import std/math |
| 2 import std/strutils | 3 import std/strutils |
| 3 import std/macros | 4 import std/macros |
| 4 import std/typetraits | 5 import std/typetraits |
| 5 import std/tables | 6 import std/tables |
| 10 Vec3*[T: SomeNumber] = array[3, T] | 11 Vec3*[T: SomeNumber] = array[3, T] |
| 11 Vec4*[T: SomeNumber] = array[4, T] | 12 Vec4*[T: SomeNumber] = array[4, T] |
| 12 Vec* = Vec2|Vec3|Vec4 | 13 Vec* = Vec2|Vec3|Vec4 |
| 13 | 14 |
| 14 # define some often used constants | 15 # define some often used constants |
| 16 func ConstOne2[T: SomeNumber](): auto {.compiletime.} = Vec2[T]([T(1), T(1)]) | |
| 17 func ConstOne3[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(1), T(1)]) | |
| 18 func ConstOne4[T: SomeNumber](): auto {.compiletime.} = Vec4[T]([T(1), T(1), T(1), T(1)]) | |
| 15 func ConstX[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(0), T(0)]) | 19 func ConstX[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(0), T(0)]) |
| 16 func ConstY[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(1), T(0)]) | 20 func ConstY[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(1), T(0)]) |
| 17 func ConstZ[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(0), T(1)]) | 21 func ConstZ[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(0), T(1)]) |
| 18 func ConstR[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(0), T(0)]) | 22 func ConstR[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(1), T(0), T(0)]) |
| 19 func ConstG[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(1), T(0)]) | 23 func ConstG[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(1), T(0)]) |
| 20 func ConstB[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(0), T(1)]) | 24 func ConstB[T: SomeNumber](): auto {.compiletime.} = Vec3[T]([T(0), T(0), T(1)]) |
| 21 | 25 |
| 22 # generates constants: Xf, Xf32, Xf64, Xi, Xi8, Xi16, Xi32, Xi64 | 26 # generates constants: Xf, Xf32, Xf64, Xi, Xi8, Xi16, Xi32, Xi64 |
| 23 # Also for Y, Z, R, G, B | 27 # Also for Y, Z, R, G, B and One |
| 24 # not sure if this is necessary or even a good idea... | 28 # not sure if this is necessary or even a good idea... |
| 25 macro generateAllConsts() = | 29 macro generateAllConsts() = |
| 26 result = newStmtList() | 30 result = newStmtList() |
| 27 for component in ["X", "Y", "Z", "R", "G", "B"]: | 31 for component in ["X", "Y", "Z", "R", "G", "B", "One2", "One3", "One4"]: |
| 28 for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]: | 32 for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]: |
| 29 var typename = theType[0 .. 0] | 33 var typename = theType[0 .. 0] |
| 30 if theType[^2].isDigit: | 34 if theType[^2].isDigit: |
| 31 typename = typename & theType[^2] | 35 typename = typename & theType[^2] |
| 32 if theType[^1].isDigit: | 36 if theType[^1].isDigit: |
| 41 generateAllConsts() | 45 generateAllConsts() |
| 42 | 46 |
| 43 const X* = ConstX[float]() | 47 const X* = ConstX[float]() |
| 44 const Y* = ConstY[float]() | 48 const Y* = ConstY[float]() |
| 45 const Z* = ConstZ[float]() | 49 const Z* = ConstZ[float]() |
| 50 const One2* = ConstOne2[float]() | |
| 51 const One3* = ConstOne3[float]() | |
| 52 const One4* = ConstOne4[float]() | |
| 46 | 53 |
| 47 func newVec2*[T](x, y: T): auto = Vec2([x, y]) | 54 func newVec2*[T](x, y: T): auto = Vec2([x, y]) |
| 48 func newVec3*[T](x, y, z: T): auto = Vec3([x, y, z]) | 55 func newVec3*[T](x, y, z: T): auto = Vec3([x, y, z]) |
| 49 func newVec4*[T](x, y, z, w: T): auto = Vec4([x, y, z, w]) | 56 func newVec4*[T](x, y, z, w: T): auto = Vec4([x, y, z, w]) |
| 50 | 57 |
| 199 result.add(vectorAttributeAccessor(i & j & k)) | 206 result.add(vectorAttributeAccessor(i & j & k)) |
| 200 for l in attlist: | 207 for l in attlist: |
| 201 result.add(vectorAttributeAccessor(i & j & k & l)) | 208 result.add(vectorAttributeAccessor(i & j & k & l)) |
| 202 | 209 |
| 203 createVectorAttribAccessorFuncs() | 210 createVectorAttribAccessorFuncs() |
| 211 | |
| 212 # call e.g. Vec2[int]().randomized() to get a random matrix | |
| 213 template makeRandomInit(mattype: typedesc) = | |
| 214 proc randomized*[T: SomeInteger](m: mattype[T]): mattype[T] = | |
| 215 for i in 0 ..< result.len: | |
| 216 result[i] = rand(low(typeof(m[0])) .. high(typeof(m[0]))) | |
| 217 proc randomized*[T: SomeFloat](m: mattype[T]): mattype[T] = | |
| 218 for i in 0 ..< result.len: | |
| 219 result[i] = rand(1.0) | |
| 220 | |
| 221 makeRandomInit(Vec2) | |
| 222 makeRandomInit(Vec3) | |
| 223 makeRandomInit(Vec4) |
