Merge branch 'master' of github.com:treeform/vmath
This commit is contained in:
commit
c53be23c65
|
@ -125,13 +125,13 @@ when defined(vmathArrayBased):
|
|||
template `[]`*[T](a: GMat234[T], i, j: int): T = a[i][j]
|
||||
|
||||
template `[]=`*[T](a: var GMat2[T], i, j: int, v: T) =
|
||||
cast[ptr T](cast[uint64](a.addr) + (i * 2 + j) * sizeof(T))[] = v
|
||||
cast[ptr T](cast[ByteAddress](a.addr) + (i * 2 + j) * sizeof(T))[] = v
|
||||
|
||||
template `[]=`*[T](a: var GMat3[T], i, j: int, v: T) =
|
||||
cast[ptr T](cast[uint64](a.addr) + (i * 3 + j) * sizeof(T))[] = v
|
||||
cast[ptr T](cast[ByteAddress](a.addr) + (i * 3 + j) * sizeof(T))[] = v
|
||||
|
||||
template `[]=`*[T](a: var GMat4[T], i, j: int, v: T) =
|
||||
cast[ptr T](cast[uint64](a.addr) + (i * 4 + j) * sizeof(T))[] = v
|
||||
cast[ptr T](cast[ByteAddress](a.addr) + (i * 4 + j) * sizeof(T))[] = v
|
||||
|
||||
elif defined(vmathObjBased):
|
||||
type
|
||||
|
@ -158,13 +158,13 @@ elif defined(vmathObjBased):
|
|||
template `[]`*[T](a: GVec4[T], i: int): T = cast[array[4, T]](a)[i]
|
||||
|
||||
template `[]=`*[T](a: var GVec2[T], i: int, v: T) =
|
||||
cast[ptr T](cast[uint64](a.addr) + i * sizeof(T))[] = v
|
||||
cast[ptr T](cast[ByteAddress](a.addr) + i * sizeof(T))[] = v
|
||||
|
||||
template `[]=`*[T](a: var GVec3[T], i: int, v: T) =
|
||||
cast[ptr T](cast[uint64](a.addr) + i * sizeof(T))[] = v
|
||||
cast[ptr T](cast[ByteAddress](a.addr) + i * sizeof(T))[] = v
|
||||
|
||||
template `[]=`*[T](a: var GVec4[T], i: int, v: T) =
|
||||
cast[ptr T](cast[uint64](a.addr) + i * sizeof(T))[] = v
|
||||
cast[ptr T](cast[ByteAddress](a.addr) + i * sizeof(T))[] = v
|
||||
|
||||
type
|
||||
GMat2*[T] {.bycopy.} = object
|
||||
|
@ -217,13 +217,13 @@ elif defined(vmathObjBased):
|
|||
cast[array[16, T]](a)[i * 4 + j]
|
||||
|
||||
template `[]=`*[T](a: var GMat2[T], i, j: int, v: T) =
|
||||
cast[ptr T](cast[uint64](a.addr) + (i * 2 + j) * sizeof(T))[] = v
|
||||
cast[ptr T](cast[ByteAddress](a.addr) + (i * 2 + j) * sizeof(T))[] = v
|
||||
|
||||
template `[]=`*[T](a: var GMat3[T], i, j: int, v: T) =
|
||||
cast[ptr T](cast[uint64](a.addr) + (i * 3 + j) * sizeof(T))[] = v
|
||||
cast[ptr T](cast[ByteAddress](a.addr) + (i * 3 + j) * sizeof(T))[] = v
|
||||
|
||||
template `[]=`*[T](a: var GMat4[T], i, j: int, v: T) =
|
||||
cast[ptr T](cast[uint64](a.addr) + (i * 4 + j) * sizeof(T))[] = v
|
||||
cast[ptr T](cast[ByteAddress](a.addr) + (i * 4 + j) * sizeof(T))[] = v
|
||||
|
||||
template `[]`*[T](a: GMat2[T], i: int): GVec2[T] =
|
||||
gvec2[T](
|
||||
|
@ -475,7 +475,8 @@ proc `zmod`*(a, b: float32): float32 =
|
|||
template lowerType(a: typed): string =
|
||||
($type(a)).toLowerAscii()
|
||||
|
||||
template genConstructor(lower, upper, typ: untyped) =
|
||||
template genVecConstructor*(lower, upper, typ: untyped) =
|
||||
## Generate vector constructor for your own type.
|
||||
|
||||
proc `lower 2`*(): `upper 2` = gvec2[typ](typ(0), typ(0))
|
||||
proc `lower 3`*(): `upper 3` = gvec3[typ](typ(0), typ(0), typ(0))
|
||||
|
@ -511,11 +512,11 @@ template genConstructor(lower, upper, typ: untyped) =
|
|||
proc `$`*(a: `upper 4`): string =
|
||||
lowerType(a) & "(" & $a.x & ", " & $a.y & ", " & $a.z & ", " & $a.w & ")"
|
||||
|
||||
genConstructor(bvec, BVec, bool)
|
||||
genConstructor(ivec, IVec, int32)
|
||||
genConstructor(uvec, UVec, uint32)
|
||||
genConstructor(vec, Vec, float32)
|
||||
genConstructor(dvec, DVec, float64)
|
||||
genVecConstructor(bvec, BVec, bool)
|
||||
genVecConstructor(ivec, IVec, int32)
|
||||
genVecConstructor(uvec, UVec, uint32)
|
||||
genVecConstructor(vec, Vec, float32)
|
||||
genVecConstructor(dvec, DVec, float64)
|
||||
|
||||
proc vec2*(ivec2: Ivec2): Vec2 =
|
||||
vec2(ivec2.x.float32, ivec2.y.float32)
|
||||
|
@ -872,8 +873,8 @@ proc matToString[T](a: T, n: int): string =
|
|||
result.setLen(result.len - 2)
|
||||
result.add "\n)"
|
||||
|
||||
template genMatConstructor(lower, upper, T: untyped) =
|
||||
|
||||
template genMatConstructor*(lower, upper, T: untyped) =
|
||||
## Generate matrix constructor for your own type.
|
||||
proc `lower 2`*(
|
||||
m00, m01,
|
||||
m10, m11: T
|
||||
|
@ -1345,7 +1346,7 @@ proc lookAt*[T](eye, center, up: GVec3[T]): GMat4[T] =
|
|||
centerz = center[2]
|
||||
|
||||
if eyex == centerx and eyey == centery and eyez == centerz:
|
||||
return mat4[T]()
|
||||
return
|
||||
|
||||
var
|
||||
# vec3.direction(eye, center, z)
|
||||
|
@ -1434,7 +1435,8 @@ type
|
|||
Quat* = GVec4[float32]
|
||||
DQuat* = GVec4[float64]
|
||||
|
||||
template genQuatConstructor(lower, upper, typ: untyped) =
|
||||
template genQuatConstructor*(lower, upper, typ: untyped) =
|
||||
## Generate quaternion constructor for your own type.
|
||||
proc `lower`*(): `upper` = gvec4[typ](0, 0, 0, 1)
|
||||
proc `lower`*(x, y, z, w: typ): `upper` = gvec4[typ](x, y, z, w)
|
||||
proc `lower`*(x: typ): `upper` = gvec4[typ](x, x, x, x)
|
||||
|
|
File diff suppressed because it is too large
Load diff
31
tests/bench_swizzle.nim
Normal file
31
tests/bench_swizzle.nim
Normal file
|
@ -0,0 +1,31 @@
|
|||
import benchy, vmath
|
||||
|
||||
block:
|
||||
var
|
||||
a = vec2(1, 10)
|
||||
b = vec2(2, 20)
|
||||
c = vec2(3, 30)
|
||||
timeIt "xy":
|
||||
for i in 0 ..< 1_000_000:
|
||||
c.yx = a.xy + b.rg + c.xy
|
||||
echo c.xy
|
||||
|
||||
block:
|
||||
var
|
||||
a = vec3(1, 10, 100)
|
||||
b = vec3(2, 20, 200)
|
||||
c = vec3(3, 30, 300)
|
||||
timeIt "xyz":
|
||||
for i in 0 ..< 1_000_000:
|
||||
c.zyx = a.xyz + b.rgb + c.xyz
|
||||
echo c.xyz
|
||||
|
||||
block:
|
||||
var
|
||||
a = vec4(1, 10, 100, -1)
|
||||
b = vec4(2, 20, 200, -1)
|
||||
c = vec4(3, 30, 300, -1)
|
||||
timeIt "xyzw":
|
||||
for i in 0 ..< 1_000_000:
|
||||
c.wzyx = a.xyzw + b.rgba + c.xyzw
|
||||
echo c.xyzw
|
|
@ -356,11 +356,45 @@ block:
|
|||
|
||||
var b = dvec4(1, 2, 3, 4)
|
||||
doAssert b == dvec4(1, 2, 3, 4)
|
||||
b.wzyx = b
|
||||
doAssert b == dvec4(4, 3, 2, 1)
|
||||
|
||||
b.g = 123
|
||||
doAssert b == dvec4(4.0, 123.0, 2.0, 1.0)
|
||||
doAssert b == dvec4(1.0, 123.0, 3.0, 4.0)
|
||||
|
||||
block:
|
||||
# test swizzle self-assignment
|
||||
var a = dvec2(1, 2)
|
||||
a.yx = a
|
||||
doAssert a == dvec2(2, 1)
|
||||
|
||||
var b = dvec3(1, 2, 3)
|
||||
b.zyx = b
|
||||
doAssert b == dvec3(3, 2, 1)
|
||||
|
||||
var c = dvec4(1, 2, 3, 4)
|
||||
c.wzyx = c
|
||||
doAssert c == dvec4(4, 3, 2, 1)
|
||||
|
||||
block:
|
||||
# Test swizzle calls only once
|
||||
var callCount = 0
|
||||
proc countsCalls(): Vec2 =
|
||||
inc callCount
|
||||
|
||||
doAssert countsCalls().yx == vec2(0, 0)
|
||||
doAssert callCount == 1
|
||||
|
||||
callCount = 0
|
||||
doAssert vec2(0, 0) == countsCalls().yx
|
||||
doAssert callCount == 1
|
||||
|
||||
var tmp: Vec2
|
||||
proc countsCalls2(): var Vec2 =
|
||||
inc callCount
|
||||
return tmp
|
||||
|
||||
callCount = 0
|
||||
countsCalls2().yx = vec2(0, 0)
|
||||
doAssert callCount == 1
|
||||
|
||||
block:
|
||||
# Test swizzle with complex expressions
|
||||
|
@ -375,7 +409,7 @@ block:
|
|||
# function with side effects
|
||||
result = a[i]
|
||||
inc i
|
||||
|
||||
|
||||
doAssert f().yx == vec2(2, 1)
|
||||
doAssert f().gr == vec2(4, 3)
|
||||
doAssert f().ts == vec2(6, 5)
|
||||
|
@ -398,7 +432,7 @@ block:
|
|||
# function with side effects
|
||||
result = b[i]
|
||||
inc i
|
||||
|
||||
|
||||
doAssert g().yxz == vec3(2, 1, 3)
|
||||
doAssert g().bgr == vec3(6, 5, 4)
|
||||
doAssert g().tps == vec3(8, 9, 7)
|
||||
|
@ -419,7 +453,7 @@ block:
|
|||
# function with side effects
|
||||
result = c[i]
|
||||
inc i
|
||||
|
||||
|
||||
doAssert h().yxzw == vec4(2, 1, 3, 4)
|
||||
doAssert h().tqsp == vec4(6, 8, 5, 7)
|
||||
doAssert i == 2
|
||||
|
@ -848,6 +882,10 @@ block:
|
|||
)
|
||||
doAssert lookAt(vec3(0, 0, 1), vec3(0, 0, 0)).quat ~= quat(0.0, 0.0, 0.0, 1.0)
|
||||
|
||||
let
|
||||
a = lookAt(vec3(1, 2, 3), vec3(0, 0, 0))
|
||||
b = lookAt(dvec3(1, 2, 3), dvec3(0, 0, 0))
|
||||
|
||||
doAssert ortho[float32](-1, 1, 1, -1, -1000, 1000) ~= mat4(
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, -1.0, 0.0, 0.0,
|
||||
|
|
|
@ -1,47 +1,49 @@
|
|||
import strformat
|
||||
|
||||
var swizzles = @["xyzw", "rgba", "stpq"]
|
||||
var
|
||||
swizzles = @["xyzw", "rgba", "stpq"]
|
||||
code = ""
|
||||
|
||||
echo "# Generated by tools/gensswizzle"
|
||||
code.add "# Generated by tools/gensswizzle - don't edit manually.\n"
|
||||
|
||||
for swizzle in swizzles[1 .. ^1]:
|
||||
echo "\n# 1 x ", swizzle
|
||||
code.add "\n# 1 x " & swizzle & "\n"
|
||||
for i1, s1 in swizzle:
|
||||
echo &"template {s1}*[T](a: GVec234[T]): T = a[{i1}]"
|
||||
echo &"template `{s1}=`*[T](a: var GVec234[T], b: T) = a[{i1}] = b"
|
||||
code.add &"proc {s1}*[T](a: GVec234[T]): T = a[{i1}]\n"
|
||||
for i1, s1 in swizzle:
|
||||
code.add &"proc `{s1}=`*[T](a: var GVec234[T], b: T) = a[{i1}] = b\n"
|
||||
|
||||
for swizzle in swizzles:
|
||||
echo "\n# 2 x ", swizzle
|
||||
code.add "\n# 2 x " & swizzle & "\n"
|
||||
for i1, s1 in swizzle:
|
||||
for i2, s2 in swizzle:
|
||||
echo &"template {s1}{s2}*[T](a: GVec234[T]): GVec2[T] ="
|
||||
echo &" let a2 = a"
|
||||
echo &" gvec2(a2[{i1}], a2[{i2}])"
|
||||
echo &"func `{s1}{s2}=`*[T](a: var GVec234[T], b: GVec2[T]) ="
|
||||
echo &" let b2 = b"
|
||||
echo &" a[{i1}] = b2.x; a[{i2}] = b2.y"
|
||||
code.add &"proc {s1}{s2}*[T](a: GVec234[T]): GVec2[T] = gvec2(a[{i1}], a[{i2}])\n"
|
||||
for i1, s1 in swizzle:
|
||||
for i2, s2 in swizzle:
|
||||
code.add &"proc `{s1}{s2}=`*[T](a: var GVec234[T], b: GVec2[T]) = (let b = b; a[{i1}] = b.x; a[{i2}] = b.y)\n"
|
||||
|
||||
for swizzle in swizzles:
|
||||
echo "\n# 3 x ", swizzle
|
||||
code.add "\n# 3 x " & swizzle & "\n"
|
||||
for i1, s1 in swizzle:
|
||||
for i2, s2 in swizzle:
|
||||
for i3, s3 in swizzle:
|
||||
echo &"template {s1}{s2}{s3}*[T](a: GVec234[T]): GVec3[T] ="
|
||||
echo &" let a2 = a"
|
||||
echo &" gvec3(a2[{i1}], a2[{i2}], a2[{i3}])"
|
||||
echo &"func `{s1}{s2}{s3}=`*[T](a: var GVec234[T], b: GVec3[T]) ="
|
||||
echo &" let b2 = b"
|
||||
echo &" a[{i1}] = b2.x; a[{i2}] = b2.y; a[{i3}] = b2.z"
|
||||
code.add &"proc {s1}{s2}{s3}*[T](a: GVec234[T]): GVec3[T] = gvec3(a[{i1}], a[{i2}], a[{i3}])\n"
|
||||
for i1, s1 in swizzle:
|
||||
for i2, s2 in swizzle:
|
||||
for i3, s3 in swizzle:
|
||||
code.add &"proc `{s1}{s2}{s3}=`*[T](a: var GVec234[T], b: GVec3[T]) = (let b = b; a[{i1}] = b.x; a[{i2}] = b.y; a[{i3}] = b.z)\n"
|
||||
|
||||
for swizzle in swizzles:
|
||||
echo "\n# 4 x ", swizzle
|
||||
code.add "\n# 4 x " & swizzle & "\n"
|
||||
for i1, s1 in swizzle:
|
||||
for i2, s2 in swizzle:
|
||||
for i3, s3 in swizzle:
|
||||
for i4, s4 in swizzle:
|
||||
echo &"template {s1}{s2}{s3}{s4}*[T](a: GVec234[T]): GVec4[T] ="
|
||||
echo &" let a2 = a"
|
||||
echo &" gvec4(a2[{i1}], a2[{i2}], a2[{i3}], a2[{i4}])"
|
||||
echo &"func `{s1}{s2}{s3}{s4}=`*[T](a: var GVec234[T], b: GVec4[T]) ="
|
||||
echo &" let b2 = b"
|
||||
echo &" a[{i1}] = b2.x; a[{i2}] = b2.y; a[{i3}] = b2.z; a[{i4}] = b2.w"
|
||||
code.add &"proc {s1}{s2}{s3}{s4}*[T](a: GVec234[T]): GVec4[T] = gvec4(a[{i1}], a[{i2}], a[{i3}], a[{i4}])\n"
|
||||
for i1, s1 in swizzle:
|
||||
for i2, s2 in swizzle:
|
||||
for i3, s3 in swizzle:
|
||||
for i4, s4 in swizzle:
|
||||
code.add &"proc `{s1}{s2}{s3}{s4}=`*[T](a: var GVec234[T], b: GVec4[T]) = (let b = b; a[{i1}] = b.x; a[{i2}] = b.y; a[{i3}] = b.z; a[{i4}] = b.w)\n"
|
||||
|
||||
writeFile("src/vmath/swizzle.nim", code)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
version = "1.1.4"
|
||||
version = "1.2.0"
|
||||
author = "Andre von Houck"
|
||||
description = "Your single stop for vector math routines for 2d and 3d graphics."
|
||||
license = "MIT"
|
||||
|
|
Loading…
Reference in a new issue