Make sure every type has a hash.

This commit is contained in:
treeform 2021-02-28 11:02:59 -08:00
parent 6160e80b31
commit 4cb11b87b0
2 changed files with 54 additions and 35 deletions

View file

@ -674,6 +674,19 @@ proc `pos=`*(a: var Mat3, b: Vec2) {.inline.} =
a[2, 0] = b.x
a[2, 1] = b.y
proc hash*(a: Mat3): Hash {.inline.} =
hash((
a[0],
a[1],
a[2],
a[3],
a[4],
a[5],
a[6],
a[7],
a[8]
))
type Mat4* = array[16, float32] ## 4x4 Matrix - OpenGL row order
proc `[]`*(a: Mat4, i, j: int): float32 = a[i * 4 + j]
@ -914,41 +927,6 @@ proc dist*(a, b: Mat4): float32 {.inline.} =
z = a[14] - b[14]
sqrt(x * x + y * y + z * z)
#[
proc translate*(a: Mat4, v: Vec3): Mat4 =
var
a00 = a[0]
a01 = a[1]
a02 = a[2]
a03 = a[3]
a10 = a[4]
a11 = a[5]
a12 = a[6]
a13 = a[7]
a20 = a[8]
a21 = a[9]
a22 = a[10]
a23 = a[11]
result[0] = a00
result[1] = a01
result[2] = a02
result[3] = a03
result[4] = a10
result[5] = a11
result[6] = a12
result[7] = a13
result[8] = a20
result[9] = a21
result[10] = a22
result[11] = a23
result[12] = a00*v.x + a10*v.y + a20*v.z + a[12]
result[13] = a01*v.x + a11*v.y + a21*v.z + a[13]
result[14] = a02*v.x + a12*v.y + a22*v.z + a[14]
result[15] = a03*v.x + a13*v.y + a23*v.z + a[15]
]#
proc translate*(v: Vec3): Mat4 =
result[0] = 1
result[5] = 1
@ -1167,6 +1145,26 @@ proc mat4Rotation*(m: Mat3): Mat4 =
result[2, 1] = m[2, 1]
result[2, 2] = m[2, 2]
proc hash*(a: Mat4): Hash {.inline.} =
hash((
a[0],
a[1],
a[2],
a[3],
a[4],
a[5],
a[6],
a[7],
a[8],
a[9],
a[10],
a[11],
a[12],
a[13],
a[14],
a[15],
))
proc `$`*(a: Mat4): string =
&"""[{a[0]:.5f}, {a[1]:.5f}, {a[2]:.5f}, {a[3]:.5f},
{a[4]:.5f}, {a[5]:.5f}, {a[6]:.5f}, {a[7]:.5f},
@ -1467,6 +1465,9 @@ proc nlerp*(a: Quat, b: Quat, v: float32): Quat =
else:
(a * (1.0 - v) + b * v).normalize()
proc hash*(a: Quat): Hash {.inline.} =
hash((a.x, a.y, a.z, a.w))
proc `$`*(a: Quat): string =
&"q({a.x:.8f}, {a.y:.8f}, {a.z:.8f}, {a.w:.8f})"

View file

@ -284,3 +284,21 @@ block:
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)
block:
# hashing
doAssert hash(vec2(PI, E)) == 1311648097060332001
doAssert hash(vec3(PI, E, TAU)) == 5625953707464987239
doAssert hash(vec4(PI, E, TAU, sqrt(2.0))) == -6538384897102876123
doAssert hash(quat(1, 0, 0, sqrt(2.0))) == 1497919211694084820
doAssert hash( mat3(
300.0000, 360.0000, 420.0000,
660.0000, 810.0000, 960.0000,
1020.0000, 1260.0000, 1500.0000
)) == -4194936143766837151
doAssert hash(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,
7.00000, 8.00000, 9.00000, 1.00000
)) == 7507518476139335223