0.5.0 - remove inRect, add ~=, change tests.
This commit is contained in:
parent
807c802e97
commit
437f3cc604
104
src/vmath.nim
104
src/vmath.nim
|
@ -2,6 +2,11 @@ import hashes, math, random, strformat, strutils
|
|||
|
||||
export math
|
||||
|
||||
proc `~=`*(a, b: float32): bool =
|
||||
## Almost equal.
|
||||
const epsilon = 0.000001
|
||||
abs(a - b) < epsilon
|
||||
|
||||
proc between*(value, min, max: float32): bool {.inline.} =
|
||||
## Returns true if value is between min and max or equal to them.
|
||||
(value >= min) and (value <= max)
|
||||
|
@ -63,6 +68,9 @@ proc vec2*(a: Vec2): Vec2 {.inline.} =
|
|||
result.x = a.x
|
||||
result.y = a.y
|
||||
|
||||
proc `~=`*(a, b: Vec2): bool =
|
||||
a.x ~= b.x and a.y ~= b.y
|
||||
|
||||
proc `+`*(a, b: Vec2): Vec2 {.inline.} =
|
||||
result.x = a.x + b.x
|
||||
result.y = a.y + b.y
|
||||
|
@ -152,14 +160,6 @@ proc quantize*(v: Vec2, n: float32): Vec2 {.inline.} =
|
|||
result.x = sign(v.x) * floor(abs(v.x) / n) * n
|
||||
result.y = sign(v.y) * floor(abs(v.y) / n) * n
|
||||
|
||||
proc inRect*(v, a, b: Vec2): bool {.inline.} =
|
||||
## Check to see if v is inside a rectange formed by a and b.
|
||||
## It does not matter how a and b are arranged.
|
||||
let
|
||||
min = vec2(min(a.x, b.x), min(a.y, b.y))
|
||||
max = vec2(max(a.x, b.x), max(a.y, b.y))
|
||||
v.x > min.x and v.x < max.x and v.y > min.y and v.y < max.y
|
||||
|
||||
proc `[]`*(a: Vec2, i: int): float32 =
|
||||
case i
|
||||
of 0: a.x
|
||||
|
@ -178,7 +178,7 @@ proc randVec2*(r: var Rand): Vec2 =
|
|||
vec2(cos(a) * v, sin(a) * v)
|
||||
|
||||
proc `$`*(a: Vec2): string =
|
||||
&"({a.x:.4f}, {a.y:.4f})"
|
||||
&"({a.x:.7f}, {a.y:.7f})"
|
||||
|
||||
proc angle*(a: Vec2): float32 {.inline.} =
|
||||
## Angle of a Vec2.
|
||||
|
@ -213,6 +213,9 @@ const X_DIR* = vec3(1.0, 0.0, 0.0)
|
|||
const Y_DIR* = vec3(0.0, 1.0, 0.0)
|
||||
const Z_DIR* = vec3(0.0, 0.0, 1.0)
|
||||
|
||||
proc `~=`*(a, b: Vec3): bool =
|
||||
a.x ~= b.x and a.y ~= b.y and a.z ~= b.z
|
||||
|
||||
proc `+`*(a, b: Vec3): Vec3 {.inline.} =
|
||||
result.x = a.x + b.x
|
||||
result.y = a.y + b.y
|
||||
|
@ -347,27 +350,40 @@ proc `[]=`*(a: var Vec3, i: int, b: float32) =
|
|||
of 2: a.z = b
|
||||
else: raise newException(IndexDefect, "Index not in 0 .. 2")
|
||||
|
||||
proc xy*(a: Vec3): Vec2 {.inline.} =
|
||||
vec2(a.x, a.y)
|
||||
proc xy*(a: Vec3): Vec2 {.inline.} = vec2(a.x, a.y)
|
||||
proc xz*(a: Vec3): Vec2 {.inline.} = vec2(a.x, a.z)
|
||||
proc yx*(a: Vec3): Vec2 {.inline.} = vec2(a.y, a.x)
|
||||
proc yz*(a: Vec3): Vec2 {.inline.} = vec2(a.y, a.z)
|
||||
proc zx*(a: Vec3): Vec2 {.inline.} = vec2(a.z, a.x)
|
||||
proc zy*(a: Vec3): Vec2 {.inline.} = vec2(a.z, a.y)
|
||||
|
||||
proc xz*(a: Vec3): Vec2 {.inline.} =
|
||||
vec2(a.x, a.z)
|
||||
|
||||
proc yx*(a: Vec3): Vec2 {.inline.} =
|
||||
vec2(a.y, a.x)
|
||||
|
||||
proc yz*(a: Vec3): Vec2 {.inline.} =
|
||||
vec2(a.y, a.z)
|
||||
|
||||
proc zx*(a: Vec3): Vec2 {.inline.} =
|
||||
vec2(a.z, a.x)
|
||||
|
||||
proc zy*(a: Vec3): Vec2 {.inline.} =
|
||||
vec2(a.z, a.y)
|
||||
|
||||
proc almostEquals*(a, b: Vec3, precision = 1e-6): bool {.inline.} =
|
||||
let c = a - b
|
||||
abs(c.x) < precision and abs(c.y) < precision and abs(c.z) < precision
|
||||
proc xxx*(a: Vec3): Vec3 {.inline.} = vec3(a.x, a.x, a.x)
|
||||
proc xxy*(a: Vec3): Vec3 {.inline.} = vec3(a.x, a.x, a.y)
|
||||
proc xxz*(a: Vec3): Vec3 {.inline.} = vec3(a.x, a.x, a.z)
|
||||
proc xyx*(a: Vec3): Vec3 {.inline.} = vec3(a.x, a.y, a.x)
|
||||
proc xyy*(a: Vec3): Vec3 {.inline.} = vec3(a.x, a.y, a.y)
|
||||
proc xyz*(a: Vec3): Vec3 {.inline.} = vec3(a.x, a.y, a.z)
|
||||
proc xzx*(a: Vec3): Vec3 {.inline.} = vec3(a.x, a.z, a.x)
|
||||
proc xzy*(a: Vec3): Vec3 {.inline.} = vec3(a.x, a.z, a.y)
|
||||
proc xzz*(a: Vec3): Vec3 {.inline.} = vec3(a.x, a.z, a.z)
|
||||
proc yxx*(a: Vec3): Vec3 {.inline.} = vec3(a.y, a.x, a.x)
|
||||
proc yxy*(a: Vec3): Vec3 {.inline.} = vec3(a.y, a.x, a.y)
|
||||
proc yxz*(a: Vec3): Vec3 {.inline.} = vec3(a.y, a.x, a.z)
|
||||
proc yyx*(a: Vec3): Vec3 {.inline.} = vec3(a.y, a.y, a.x)
|
||||
proc yyy*(a: Vec3): Vec3 {.inline.} = vec3(a.y, a.y, a.y)
|
||||
proc yyz*(a: Vec3): Vec3 {.inline.} = vec3(a.y, a.y, a.z)
|
||||
proc yzx*(a: Vec3): Vec3 {.inline.} = vec3(a.y, a.z, a.x)
|
||||
proc yzy*(a: Vec3): Vec3 {.inline.} = vec3(a.y, a.z, a.y)
|
||||
proc yzz*(a: Vec3): Vec3 {.inline.} = vec3(a.y, a.z, a.z)
|
||||
proc zxx*(a: Vec3): Vec3 {.inline.} = vec3(a.z, a.x, a.x)
|
||||
proc zxy*(a: Vec3): Vec3 {.inline.} = vec3(a.z, a.x, a.y)
|
||||
proc zxz*(a: Vec3): Vec3 {.inline.} = vec3(a.z, a.x, a.z)
|
||||
proc zyx*(a: Vec3): Vec3 {.inline.} = vec3(a.z, a.y, a.x)
|
||||
proc zyy*(a: Vec3): Vec3 {.inline.} = vec3(a.z, a.y, a.y)
|
||||
proc zyz*(a: Vec3): Vec3 {.inline.} = vec3(a.z, a.y, a.z)
|
||||
proc zzx*(a: Vec3): Vec3 {.inline.} = vec3(a.z, a.z, a.x)
|
||||
proc zzy*(a: Vec3): Vec3 {.inline.} = vec3(a.z, a.z, a.y)
|
||||
proc zzz*(a: Vec3): Vec3 {.inline.} = vec3(a.z, a.z, a.z)
|
||||
|
||||
proc randVec3*(r: var Rand): Vec3 =
|
||||
## Generates a random unit vector based on
|
||||
|
@ -405,6 +421,9 @@ proc vec4*(v: float32): Vec4 {.inline.} =
|
|||
result.z = v
|
||||
result.w = v
|
||||
|
||||
proc `~=`*(a, b: Vec4): bool =
|
||||
a.x ~= b.x and a.y ~= b.y and a.z ~= b.z and a.w ~= b.w
|
||||
|
||||
proc `+`*(a, b: Vec4): Vec4 {.inline.} =
|
||||
result.x = a.x + b.x
|
||||
result.y = a.y + b.y
|
||||
|
@ -535,6 +554,12 @@ proc mat3*(a, b, c, d, e, f, g, h, i: float32): Mat3 {.inline.} =
|
|||
proc mat3*(a: Mat3): Mat3 {.inline.} =
|
||||
a
|
||||
|
||||
proc `~=`*(a: Mat3, b: Mat3): bool =
|
||||
for i in 0 .. 8:
|
||||
if not(a[i] ~= b[i]):
|
||||
return false
|
||||
true
|
||||
|
||||
proc identity*(a: var Mat3) {.inline.} =
|
||||
a = [
|
||||
1.float32, 0, 0,
|
||||
|
@ -553,9 +578,9 @@ proc transpose*(a: Mat3): Mat3 {.inline.} =
|
|||
]
|
||||
|
||||
proc `$`*(a: Mat3): string =
|
||||
&"""[{a[0]:.4f}, {a[1]:.4f}, {a[2]:.4f},
|
||||
{a[3]:.4f}, {a[4]:.4f}, {a[5]:.4f},
|
||||
{a[6]:.4f}, {a[7]:.4f}, {a[8]:.4f}]"""
|
||||
&"""[{a[0]:.7f}, {a[1]:.7f}, {a[2]:.7f},
|
||||
{a[3]:.7f}, {a[4]:.7f}, {a[5]:.7f},
|
||||
{a[6]:.7f}, {a[7]:.7f}, {a[8]:.7f}]"""
|
||||
|
||||
proc `*`*(a, b: Mat3): Mat3 =
|
||||
result[0, 0] = b[0, 0] * a[0, 0] + b[0, 1] * a[1, 0] + b[0, 2] * a[2, 0]
|
||||
|
@ -678,6 +703,12 @@ proc identity*(): Mat4 {.inline.} =
|
|||
proc mat4*(): Mat4 {.inline.} =
|
||||
identity()
|
||||
|
||||
proc `~=`*(a: Mat4, b: Mat4): bool =
|
||||
for i in 0 .. 15:
|
||||
if not(a[i] ~= b[i]):
|
||||
return false
|
||||
true
|
||||
|
||||
proc transpose*(a: Mat4): Mat4 {.inline.} =
|
||||
[
|
||||
a[0, 0], a[1, 0], a[2, 0], a[3, 0],
|
||||
|
@ -933,12 +964,6 @@ proc scale*(v: Vec3): Mat4 =
|
|||
result[10] = v.z
|
||||
result[15] = 1
|
||||
|
||||
proc close*(a: Mat4, b: Mat4): bool =
|
||||
for i in 0..15:
|
||||
if abs(a[i] - b[i]) > 0.001:
|
||||
return false
|
||||
true
|
||||
|
||||
proc hrp*(m: Mat4): Vec3 =
|
||||
var heading, pitch, roll: float32
|
||||
if m[1] > 0.998: # singularity at north pole
|
||||
|
@ -1160,6 +1185,9 @@ proc quat*(x, y, z, w: float32): Quat {.inline.} =
|
|||
result.z = z
|
||||
result.w = w
|
||||
|
||||
proc `~=`*(a, b: Quat): bool =
|
||||
a.x ~= b.x and a.y ~= b.y and a.z ~= b.z and a.w ~= b.w
|
||||
|
||||
proc conjugate*(q: Quat): Quat {.inline.} =
|
||||
result.w = +q.w
|
||||
result.x = -q.x
|
||||
|
|
356
tests/test.nim
356
tests/test.nim
|
@ -1,262 +1,286 @@
|
|||
import vmath, osproc, random, streams
|
||||
|
||||
var v2 = vec2(0, 0)
|
||||
v2 *= 1
|
||||
v2 /= 1
|
||||
|
||||
var v3 = vec3(0, 0, 0)
|
||||
v3 *= 1
|
||||
v3 /= 1
|
||||
|
||||
var v4 = vec4(0, 0, 0, 0)
|
||||
v4 *= 1
|
||||
v4 /= 1
|
||||
|
||||
var q = quat(0, 0, 0, 0)
|
||||
q *= 1
|
||||
q /= 1
|
||||
|
||||
var s = newFileStream("tests/test-output.txt", fmWrite)
|
||||
import vmath, random
|
||||
|
||||
randomize(1234)
|
||||
|
||||
block:
|
||||
s.writeLine "# angle stuff"
|
||||
s.writeLine angleBetween(0.1, 0.2), " should be: ", 0.1
|
||||
# Test ~=.
|
||||
doAssert 1.0 ~= 1.0
|
||||
doAssert 0.0 ~= 0.0
|
||||
doAssert -1.0 ~= -1.0
|
||||
doAssert not(0.1 ~= 0.2)
|
||||
doAssert not(0.01 ~= 0.02)
|
||||
doAssert not(0.001 ~= 0.002)
|
||||
doAssert not(0.0001 ~= 0.0002)
|
||||
doAssert not(0.00001 ~= 0.00002)
|
||||
# Diff < epsilon.
|
||||
doAssert 0.000001 ~= 0.000002
|
||||
|
||||
s.writeLine angleBetween(0.1, 0.2 + PI*2), " should be: ", 0.1
|
||||
s.writeLine angleBetween(0.1, 0.2 - PI*2), " should be: ", 0.1
|
||||
s.writeLine angleBetween(0.1 + PI*2, 0.2), " should be: ", 0.1
|
||||
s.writeLine angleBetween(0.1 - PI*2, 0.2), " should be: ", 0.1
|
||||
|
||||
s.writeLine angleBetween(0.2, 0.1), " should be: ", -0.1
|
||||
|
||||
s.writeLine angleBetween(0.2, 0.1 - PI*2), " should be: ", -0.1
|
||||
s.writeLine angleBetween(0.2, 0.1 + PI*2), " should be: ", -0.1
|
||||
s.writeLine angleBetween(0.2 + PI*2, 0.1), " should be: ", -0.1
|
||||
s.writeLine angleBetween(0.2 - PI*2, 0.1), " should be: ", -0.1
|
||||
doAssert vec2(1.0, 2.0) ~= vec2(1.0, 2.0)
|
||||
doAssert vec3(1.0, 2.0, 3.0) ~= vec3(1.0, 2.0, 3.0)
|
||||
doAssert vec4(1.0, 2.0, 3.0, 4.0) ~= vec4(1.0, 2.0, 3.0, 4.0)
|
||||
doAssert quat(1.0, 2.0, 3.0, 4.0) ~= quat(1.0, 2.0, 3.0, 4.0)
|
||||
|
||||
block:
|
||||
s.writeLine "# basic vector vec2"
|
||||
# Test simple functions.
|
||||
doAssert between(0.5, 0, 1)
|
||||
doAssert not between(1.5, 0, 1)
|
||||
|
||||
doAssert sign(-1) == -1.0
|
||||
doAssert sign(0) == 1.0
|
||||
doAssert sign(1) == 1.0
|
||||
|
||||
doAssert quantize(1.23456789, 1.0) ~= 1
|
||||
doAssert quantize(1.23456789, 0.1) ~= 1.2
|
||||
doAssert quantize(1.23456789, 0.01) ~= 1.23
|
||||
|
||||
doAssert lerp(0.0, 1.0, 0.5) ~= 0.5
|
||||
doAssert lerp(0.0, 10.0, 0.5) ~= 5.0
|
||||
doAssert lerp(0.0, 100.0, 0.5) ~= 50.0
|
||||
doAssert lerp(-1.0, 1.0, 0.25) ~= -0.5
|
||||
doAssert lerp(-10.0, 10.0, 0.25) ~= -5.0
|
||||
doAssert lerp(-100.0, 100.0, 0.25) ~= -50.0
|
||||
|
||||
doAssert fixAngle(0.1) ~= 0.1
|
||||
doAssert fixAngle(1.1) ~= 1.1
|
||||
doAssert fixAngle(2.1) ~= 2.1
|
||||
doAssert fixAngle(3.1) ~= 3.1
|
||||
doAssert fixAngle(4.1) ~= -2.183185577392578
|
||||
doAssert fixAngle(-0.1) ~= -0.1
|
||||
doAssert fixAngle(-1.1) ~= -1.1
|
||||
doAssert fixAngle(-2.1) ~= -2.1
|
||||
doAssert fixAngle(-3.1) ~= -3.1
|
||||
doAssert fixAngle(-4.1) ~= 2.183185577392578
|
||||
|
||||
doAssert angleBetween(0, 1.0) ~= 1.0
|
||||
doAssert angleBetween(0, PI) ~= PI
|
||||
doAssert angleBetween(0, PI + 0.2) ~= (-PI + 0.2)
|
||||
doAssert angleBetween(0.1, 0.2) ~= 0.1
|
||||
doAssert angleBetween(0.1, 0.2 + PI*2) ~= 0.1
|
||||
doAssert angleBetween(0.1, 0.2 - PI*2) ~= 0.1
|
||||
doAssert angleBetween(0.1 + PI*2, 0.2) ~= 0.1
|
||||
doAssert angleBetween(0.1 - PI*2, 0.2) ~= 0.1
|
||||
doAssert angleBetween(0.2, 0.1) ~= -0.1
|
||||
doAssert angleBetween(0.2, 0.1 - PI*2) ~= -0.1
|
||||
doAssert angleBetween(0.2, 0.1 + PI*2) ~= -0.1
|
||||
doAssert angleBetween(0.2 + PI*2, 0.1) ~= -0.1
|
||||
doAssert angleBetween(0.2 - PI*2, 0.1) ~= -0.1
|
||||
|
||||
doAssert turnAngle(0, PI, 0.5) ~= 0.5
|
||||
doAssert turnAngle(0.5, PI, 3.5) ~= PI
|
||||
|
||||
block:
|
||||
# Test vec2 cast.
|
||||
var v = vec2(1.0, 2.0)
|
||||
var a = cast[array[2, float32]](v)
|
||||
doAssert a[0] ~= 1.0
|
||||
doAssert a[1] ~= 2.0
|
||||
|
||||
block:
|
||||
# Test vec2 constructor.
|
||||
doAssert vec2(PI, PI) ~= vec2(PI)
|
||||
|
||||
block:
|
||||
# Test basic vector vec2.
|
||||
var a = vec2(1, 2)
|
||||
var b = vec2(7, 6)
|
||||
var n = 13.7
|
||||
s.writeLine a + b
|
||||
s.writeLine a - b
|
||||
s.writeLine a * n
|
||||
s.writeLine a / n
|
||||
doAssert a + b ~= vec2(8.0, 8.0)
|
||||
doAssert a - b ~= vec2(-6.0, -4.0)
|
||||
doAssert a * n ~= vec2(13.7, 27.4)
|
||||
doAssert a / n ~= vec2(0.0729927, 0.1459854)
|
||||
a += b
|
||||
s.writeLine a
|
||||
doAssert a ~= vec2(8.0, 8.0)
|
||||
a -= b
|
||||
s.writeLine a
|
||||
doAssert a ~= vec2(1.0, 2.0)
|
||||
a *= n
|
||||
s.writeLine a
|
||||
doAssert a ~= vec2(13.7, 27.4)
|
||||
a /= n
|
||||
s.writeLine a
|
||||
doAssert a ~= vec2(1.0, 2.0)
|
||||
|
||||
block:
|
||||
s.writeLine "# basic vector vec3"
|
||||
# Test basic vector vec3.
|
||||
var a = vec3(1, 2, 3)
|
||||
var b = vec3(7, 6, 5)
|
||||
var n = 13.7
|
||||
s.writeLine a + b
|
||||
s.writeLine a - b
|
||||
s.writeLine a * n
|
||||
s.writeLine a / n
|
||||
doAssert a + b ~= vec3(8.0, 8.0, 8.0)
|
||||
doAssert a - b ~= vec3(-6.0, -4.0, -2.0)
|
||||
doAssert a * n ~= vec3(13.69999981, 27.39999962, 41.09999847)
|
||||
doAssert a / n ~= vec3(0.07299270, 0.14598541, 0.21897811)
|
||||
a += b
|
||||
s.writeLine a
|
||||
doAssert a ~= vec3(8.0, 8.0, 8.0)
|
||||
a -= b
|
||||
s.writeLine a
|
||||
doAssert a ~= vec3(1.0, 2.0, 3.0)
|
||||
a *= n
|
||||
s.writeLine a
|
||||
doAssert a ~= vec3(13.69999981, 27.39999962, 41.09999847)
|
||||
a /= n
|
||||
s.writeLine a
|
||||
doAssert a ~= vec3(1.0, 2.0, 3.0)
|
||||
|
||||
block:
|
||||
s.writeLine "# basic vector vec4"
|
||||
# Test basic vector vec4.
|
||||
var a = vec4(1, 2, 3, 4)
|
||||
var b = vec4(7, 6, 5, 4)
|
||||
var n = 13.7
|
||||
s.writeLine a + b
|
||||
s.writeLine a - b
|
||||
s.writeLine a * n
|
||||
s.writeLine a / n
|
||||
doAssert a + b ~= vec4(8.0, 8.0, 8.0, 8.0)
|
||||
doAssert a - b ~= vec4(-6.0, -4.0, -2.0, 0.0)
|
||||
doAssert a * n ~= vec4(13.69999981, 27.39999962, 41.09999847, 54.79999924)
|
||||
doAssert a / n ~= vec4(0.07299270, 0.14598541, 0.21897811, 0.29197082)
|
||||
a += b
|
||||
s.writeLine a
|
||||
doAssert a ~= vec4(8.0, 8.0, 8.0, 8.0)
|
||||
a -= b
|
||||
s.writeLine a
|
||||
doAssert a ~= vec4(1.0, 2.0, 3.0, 4.0)
|
||||
a *= n
|
||||
s.writeLine a
|
||||
doAssert a ~= vec4(13.69999981, 27.39999962, 41.09999847, 54.79999924)
|
||||
a /= n
|
||||
s.writeLine a
|
||||
doAssert a ~= vec4(1.0, 2.0, 3.0, 4.0)
|
||||
|
||||
block:
|
||||
s.writeLine "# basic vector mat4"
|
||||
# Test basic vector mat4 and quat.
|
||||
var m1 = mat4(
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1)
|
||||
s.writeLine m1
|
||||
var q1 = m1.quat()
|
||||
s.writeLine q1
|
||||
var m2 = q1.mat4()
|
||||
s.writeLine m2
|
||||
assert m1.close(m2)
|
||||
doAssert m2 ~= mat4(
|
||||
1.00000, 0.00000, 0.00000, 0.00000,
|
||||
0.00000, 1.00000, 0.00000, 0.00000,
|
||||
0.00000, 0.00000, 1.00000, 0.00000,
|
||||
0.00000, 0.00000, 0.00000, 1.00000
|
||||
)
|
||||
doAssert m1 ~= (m2)
|
||||
|
||||
block:
|
||||
s.writeLine "# basic vector mat4 -1"
|
||||
# Test basic vector mat4 -1.
|
||||
var m1 = mat4(
|
||||
1, 0, 0, 0,
|
||||
0, 0, -1, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 0, 1)
|
||||
s.writeLine m1
|
||||
var q1 = m1.quat()
|
||||
s.writeLine q1
|
||||
var m2 = q1.mat4()
|
||||
s.writeLine m2
|
||||
assert m1.close(m2)
|
||||
doAssert m1 ~= m2
|
||||
|
||||
block:
|
||||
s.writeLine "# Y 90"
|
||||
# Test Y 90.
|
||||
var m1 = rotate(PI/2, vec3(0, 1, 0))
|
||||
s.writeLine m1
|
||||
var q1 = m1.quat()
|
||||
s.writeLine q1
|
||||
var m2 = q1.mat4()
|
||||
s.writeLine m2
|
||||
assert m1.close(m2)
|
||||
doAssert m1 ~= m2
|
||||
|
||||
block:
|
||||
s.writeLine "# -Y 90"
|
||||
# Test -Y 90.
|
||||
var m1 = rotate(PI/2, vec3(0, -1, 0))
|
||||
s.writeLine m1
|
||||
var q1 = m1.quat()
|
||||
s.writeLine q1
|
||||
var m2 = q1.mat4()
|
||||
s.writeLine m2
|
||||
assert m1.close(m2)
|
||||
doAssert m1 ~= m2
|
||||
|
||||
block:
|
||||
s.writeLine "# X 90"
|
||||
# Test X 90.
|
||||
var m1 = rotate(PI/2, vec3(1, 0, 0))
|
||||
s.writeLine m1
|
||||
var q1 = m1.quat()
|
||||
s.writeLine q1
|
||||
var m2 = q1.mat4()
|
||||
s.writeLine m2
|
||||
s.writeLine m1.close(m2)
|
||||
doAssert m1 ~= m2
|
||||
|
||||
block:
|
||||
s.writeLine "# Y 90"
|
||||
# Test Y 90.
|
||||
var m1 = rotate(PI/2, vec3(1, 0, 0))
|
||||
s.writeLine m1
|
||||
var q1 = m1.quat()
|
||||
s.writeLine q1
|
||||
var m2 = q1.mat4()
|
||||
s.writeLine m2
|
||||
s.writeLine m1.close(m2)
|
||||
doAssert m1 ~= m2
|
||||
|
||||
block:
|
||||
s.writeLine "# 1,1,1 1.11rad"
|
||||
# Test 1,1,1 1.11rad.
|
||||
var m1 = rotate(PI*1.11, vec3(1, 1, 1).normalize())
|
||||
s.writeLine m1
|
||||
var q1 = m1.quat()
|
||||
s.writeLine q1
|
||||
var m2 = q1.mat4()
|
||||
s.writeLine m2
|
||||
assert m1.close(m2)
|
||||
doAssert m1 ~= m2
|
||||
|
||||
block:
|
||||
s.writeLine "# 1,1,1 1.11rad"
|
||||
# Test 1,1,1 1.11rad.
|
||||
var m1 = rotate(PI*1.11, vec3(-1, 1, 1).normalize())
|
||||
s.writeLine m1
|
||||
var q1 = m1.quat()
|
||||
s.writeLine q1
|
||||
var m2 = q1.mat4()
|
||||
s.writeLine m2
|
||||
assert m1.close(m2)
|
||||
doAssert m1 ~= m2
|
||||
|
||||
block:
|
||||
s.writeLine "# 1,1,1 1.11rad"
|
||||
# Test 1,1,1 1.11rad.
|
||||
var m1 = rotate(PI*1.11, vec3(-1, 0.34, 1.123).normalize())
|
||||
s.writeLine m1
|
||||
var q1 = m1.quat()
|
||||
s.writeLine q1
|
||||
var m2 = q1.mat4()
|
||||
s.writeLine m2
|
||||
assert m1.close(m2)
|
||||
|
||||
# block:
|
||||
# s.writeLine "# super random"
|
||||
# for i in 0..100:
|
||||
# var m1 = rotate(
|
||||
# PI*rand(2.0),
|
||||
# vec3(rand(2.0)-0.5, rand(2.0)-0.5, rand(2.0)-0.5).normalize()
|
||||
# )
|
||||
# s.writeLine m1
|
||||
# var q1 = m1.quat()
|
||||
# s.writeLine q1
|
||||
# var m2 = q1.mat4()
|
||||
# s.writeLine m2
|
||||
# assert m1.close(m2)
|
||||
doAssert m1 ~= m2
|
||||
|
||||
block:
|
||||
s.writeLine "# matrix to quat test"
|
||||
# TODO: Fix this test
|
||||
# var m1 = mat4(
|
||||
# -0.33089, -0.51266, -0.79227, 0.00000,
|
||||
# 0, -1.0, 0, 0.00000,
|
||||
# 0, 0, 1.0, 0.00000,
|
||||
# 0.00000, 0.00000, 0.00000, 1.00000)
|
||||
# s.writeLine m1
|
||||
# var q1 = m1.quat()
|
||||
# s.writeLine q1
|
||||
# var m2 = q1.mat4()
|
||||
# s.writeLine m2
|
||||
# assert m1.close(m2)
|
||||
# Test super random quat test.
|
||||
for i in 0 .. 100:
|
||||
var m1 = rotate(
|
||||
PI*rand(2.0),
|
||||
vec3(rand(2.0)-0.5, rand(2.0)-0.5, rand(2.0)-0.5).normalize()
|
||||
)
|
||||
var q1 = m1.quat()
|
||||
var m2 = q1.mat4()
|
||||
doAssert m1 ~= m2
|
||||
|
||||
block:
|
||||
s.writeLine "# matrix to quat test"
|
||||
# TODO: Fix this test
|
||||
# var m1 = mat4(
|
||||
# -0.33089, -0.51266, -0.79227, 0.00000,
|
||||
# -0.44681, 0.82460, -0.34697, 0.00000,
|
||||
# -0.83119, -0.23918, 0.50191, 0.00000,
|
||||
# 0.00000, 0.00000, 0.00000, 1.00000)
|
||||
# Test *=1 /=1 don't change anything.
|
||||
var v2 = vec2(0, 0)
|
||||
v2 *= 1
|
||||
v2 /= 1
|
||||
doAssert v2 == vec2(0, 0)
|
||||
|
||||
# s.writeLine vec3( -0.33089, -0.51266, -0.79227).length
|
||||
# s.writeLine vec3( -0.44681, 0.82460, -0.34697).length
|
||||
# s.writeLine vec3( -0.83119, -0.23918, 0.50191).length
|
||||
var v3 = vec3(0, 0, 0)
|
||||
v3 *= 1
|
||||
v3 /= 1
|
||||
doAssert v3 == vec3(0, 0, 0)
|
||||
|
||||
# s.writeLine m1
|
||||
# var q1 = m1.quat().normalize()
|
||||
# s.writeLine q1
|
||||
# var m2 = q1.mat4()
|
||||
# s.writeLine m2
|
||||
# assert m1.close(m2)
|
||||
var v4 = vec4(0, 0, 0, 0)
|
||||
v4 *= 1
|
||||
v4 /= 1
|
||||
doAssert v4 == vec4(0, 0, 0, 0)
|
||||
|
||||
var q = quat(0, 0, 0, 0)
|
||||
q *= 1
|
||||
q /= 1
|
||||
doAssert q == quat(0, 0, 0, 0)
|
||||
|
||||
block:
|
||||
s.writeLine "# matrix to quat test"
|
||||
var a3 = mat3(0.9659258723258972, -0.258819043636322, 0.0, 0.258819043636322, 0.9659258723258972, 0.0, -25.00000953674316, 70.09619140625, 1.0)
|
||||
var b3 = mat3(0.9659258127212524, 0.258819043636322, 0.0, -0.258819043636322, 0.9659258127212524, 0.0, 77.64571380615234, 0.0, 1.0)
|
||||
# Test matrix to quat test.
|
||||
var a3 = mat3(
|
||||
0.9659258723258972, -0.258819043636322, 0.0,
|
||||
0.258819043636322, 0.9659258723258972, 0.0,
|
||||
-25.00000953674316, 70.09619140625, 1.0
|
||||
)
|
||||
var b3 = mat3(
|
||||
0.9659258127212524, 0.258819043636322, 0.0,
|
||||
-0.258819043636322, 0.9659258127212524, 0.0,
|
||||
77.64571380615234, 0.0, 1.0
|
||||
)
|
||||
|
||||
s.writeLine "Test Mat3 * Mat3:"
|
||||
s.writeLine (a3.mat4 * b3.mat4).mat3
|
||||
s.writeLine a3 * b3
|
||||
doAssert (a3.mat4 * b3.mat4).mat3 ~= mat3(
|
||||
1.0000, 0.0000, 0.0000,
|
||||
0.0000, 1.0000, 0.0000,
|
||||
50.0000, 50.0000, 0.0000
|
||||
)
|
||||
doAssert a3 * b3 ~= mat3(
|
||||
1.0000, 0.0000, 0.0000,
|
||||
0.0000, 1.0000, 0.0000,
|
||||
50.0000, 50.0000, 1.0000
|
||||
)
|
||||
|
||||
s.writeLine (mat3(1,2,3,4,5,6,7,8,9).mat4Rotation * mat3(10,20,30,40,50,60,70,80,90).mat4Rotation).mat3Rotation
|
||||
s.writeLine mat3(1,2,3,4,5,6,7,8,9) * mat3(10,20,30,40,50,60,70,80,90)
|
||||
doAssert (
|
||||
mat3(1,2,3,4,5,6,7,8,9).mat4Rotation *
|
||||
mat3(10,20,30,40,50,60,70,80,90).mat4Rotation
|
||||
).mat3Rotation ~= mat3(
|
||||
300.0000, 360.0000, 420.0000,
|
||||
660.0000, 810.0000, 960.0000,
|
||||
1020.0000, 1260.0000, 1500.0000
|
||||
)
|
||||
|
||||
s.writeLine "Test Mat3 * Vec2 and Vec3:"
|
||||
s.writeLine a3.mat4 * vec3(77.64571380615234, 0, 1)
|
||||
s.writeLine a3 * vec2(77.64571380615234, 0)
|
||||
s.writeLine a3 * vec3(77.64571380615234, 0, 1.0)
|
||||
|
||||
s.close()
|
||||
|
||||
let (outp, _) = execCmdEx("git diff tests/test-output.txt")
|
||||
if len(outp) != 0:
|
||||
echo outp
|
||||
quit("Output does not match")
|
||||
doAssert mat3(1,2,3,4,5,6,7,8,9) * mat3(10,20,30,40,50,60,70,80,90) ~= mat3(
|
||||
300.0000, 360.0000, 420.0000,
|
||||
660.0000, 810.0000, 960.0000,
|
||||
1020.0000, 1260.0000, 1500.0000
|
||||
)
|
||||
doAssert a3.mat4 * vec3(77.64571380615234, 0, 1) ~= vec3(50.0, 50.0, 1.0)
|
||||
doAssert a3 * vec2(77.64571380615234, 0) ~= vec2(50.0, 50.0)
|
||||
doAssert a3 * vec3(77.64571380615234, 0, 1.0) ~= vec3(50.0, 50.0, 1.0)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
version = "0.4.1"
|
||||
version = "0.5.0"
|
||||
author = "treeform"
|
||||
description = "Math vector library for graphical things."
|
||||
license = "MIT"
|
||||
|
|
Loading…
Reference in a new issue