Compare commits

...

18 commits

Author SHA1 Message Date
2ddb51917c Add normalize_safe to avoid a division by zero
Some checks failed
Github Actions / build (1.4.0, ubuntu-latest) (push) Has been cancelled
Github Actions / build (1.4.0, windows-latest) (push) Has been cancelled
Github Actions / build (1.4.x, ubuntu-latest) (push) Has been cancelled
Github Actions / build (1.4.x, windows-latest) (push) Has been cancelled
Github Actions / build (stable, ubuntu-latest) (push) Has been cancelled
Github Actions / build (stable, windows-latest) (push) Has been cancelled
2024-08-17 02:48:51 +02:00
6a63d64453 Fix compatibility with nimskull 2024-08-17 02:46:06 +02:00
Andre von Houck
535a45b57c
Merge pull request #74 from lilkeet/master
Fix angle proc for 2D vectors
2024-07-30 07:55:39 -07:00
lilkeet
aee0c310fe bug fix for some inputs into angle proc 2024-07-27 02:02:49 -05:00
lilkeet
c2aa04505b add tests for angle proc 2024-07-27 02:01:54 -05:00
lilkeet
f6bc3ccc08 remove runnable examples per comment in pr 2024-07-27 00:48:34 -05:00
lilkeet
3d42a8ed2a fix generic typing bug: prev commit allowed mixing vector types in arguments like angle(vec2, vec3) 2024-06-24 02:14:37 -05:00
lilkeet
57ad916ab1 fixes #73 by removing the faulty vec2 proc and making the vec3 angle proc work for vec2 2024-06-24 02:04:12 -05:00
Andre von Houck
7282ae1247
Merge pull request #66 from treeform/dev
Right-hand z-forward coordinate system
2023-05-11 22:26:03 -07:00
treeform
5e6ca01b16 v2.0.0 2023-05-11 22:24:49 -07:00
treeform
968a5692b0 Fix doc url. 2023-05-10 17:55:43 -07:00
treeform
c53be23c65 Merge branch 'master' of github.com:treeform/vmath 2023-05-10 17:43:09 -07:00
treeform
7bf472e527 Switch to github action doc generator. 2023-05-10 17:43:05 -07:00
Andre von Houck
b0782541e1
Merge pull request #64 from treeform/dev
Fix for lookAt: https://github.com/treeform/vmath/issues/63
2023-03-01 07:40:50 -08:00
Andre von Houck
6f232063c0
Merge pull request #62 from SMT50001/master
Fixed platform-dependent type casts
2023-02-13 09:20:56 -08:00
SMT50001
7c521adb94 Fixed platform-dependent type casts 2022-12-14 21:38:21 +03:00
Andre von Houck
f4c668874e
Merge pull request #59 from treeform/dev
1.2.0
2022-08-08 11:39:55 -07:00
Andre von Houck
0ca6d7ebd5
Merge pull request #58 from treeform/dev
More work on swizzle.
2022-08-08 11:38:08 -07:00
3 changed files with 55 additions and 19 deletions

View file

@ -123,13 +123,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
@ -156,13 +156,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
@ -215,13 +215,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](
@ -501,9 +501,9 @@ template genVecConstructor*(lower, upper, typ: untyped) =
proc `lower 4`*[T](x: GVec4[T]): `upper 4` =
gvec4[typ](typ(x[0]), typ(x[1]), typ(x[2]), typ(x[3]))
proc `lower 3`*[T](x: GVec2[T], z: T = 0): `upper 3` =
proc `lower 3`*[T](x: GVec2[T], z: T = 0.T): `upper 3` =
gvec3[typ](typ(x[0]), typ(x[1]), z)
proc `lower 4`*[T](x: GVec3[T], w: T = 0): `upper 4` =
proc `lower 4`*[T](x: GVec3[T], w: T = 0.T): `upper 4` =
gvec4[typ](typ(x[0]), typ(x[1]), typ(x[2]), w)
proc `lower 4`*[T](a, b: GVec2[T]): `upper 4` =
@ -791,6 +791,12 @@ proc lengthSq*[T](a: GVec4[T]): T =
proc normalize*[T](a: GVec234[T]): type(a) =
a / a.length
proc normalize_safe*[T](a: GVec234[T]): type(a) =
let l = a.length
if l != 0:
return a / l
# else, implicit result is vec(0)
proc mix*[T: SomeFloat](a, b: GVec234[T], v: T): type(a) =
a * (1.0 - v) + b * v
@ -1573,15 +1579,15 @@ proc angle*[T](a: GVec2[T]): T =
## Angle of a Vec2.
arctan2(a.y, a.x)
proc angle*[T](a, b: GVec2[T]): T =
## Angle between 2 Vec2.
fixAngle(arctan2(a.y - b.y, a.x - b.x))
proc angle*[T](a, b: GVec3[T]): T =
## Angle between 2 Vec3.
proc angle*[T; S: GVec2[T]|GVec3[T]](a, b: S): T =
## Angle between 2 Vec2 or Vec3.
var dot = dot(a, b)
dot = dot / (a.length * b.length)
arccos(dot)
# The cases of angle((1, 1), (-1, -1)) and its 3d counterpart
# angle((1, 1, 1), (-1, -1, -1)) result in NaN due to a domain defect going
# into the arcos proc: abs(x) > 1.0.
# Therefore, we must `clamp` here.
arccos(dot.clamp(-1.0, 1.0))
type
Quat* = GVec4[float32]

View file

@ -1155,4 +1155,34 @@ block:
else:
doAssert abs(angleBetween(a.y, b.y - b.z)) < 0.001
block:
# Test for https://github.com/treeform/vmath/issues/73
template gen2DTestsFor(constructor: untyped): void =
doAssert angle(constructor(1, 0), constructor(1, 0)) ~= 0
doAssert angle(constructor(1, 1), constructor(-1, -1)) ~= Pi
doAssert angle(constructor(1, 0), constructor(0, 1)) ~= Pi/2
doAssert angle(constructor(1, 0), constructor(-1, 0)) ~= Pi
doAssert angle(constructor(1, 1), constructor(1, -1)) ~= Pi/2
# Edge cases:
doAssert angle(constructor(0, 0), constructor(1, 0)).isNaN()
gen2DTestsFor vec2
gen2DTestsFor dvec2
template gen3DTestsFor(constructor: untyped): void =
doAssert angle(constructor(1, 0, 0), constructor(1, 0, 0)) ~= 0
doAssert angle(constructor(1, 1, 1), constructor(-1, -1, -1)) ~= Pi
doAssert angle(constructor(1, 0, 0), constructor(0, 1, 0)) ~= Pi/2
doAssert angle(constructor(1, 0, 0), constructor(-1, 0, 0)) ~= Pi
doAssert angle(constructor(1, 1, 1), constructor(1, -1, 1)) ~= arccos(1/3)
doAssert angle(constructor(1, 0, 0), constructor(0, 0, 1)) ~= Pi/2
doAssert angle(constructor(1, 1, 1), constructor(-1, -1, 1)) ~= arccos(-1/3)
# Edge cases:
doAssert angle(vec3(0, 0, 0), vec3(1, 0, 0)).isNaN()
gen3DTestsFor vec3
gen3DTestsFor dvec3
echo "test finished successfully"

View file

@ -1,4 +1,4 @@
version = "1.2.0"
version = "2.0.0"
author = "Andre von Houck"
description = "Your single stop for vector math routines for 2d and 3d graphics."
license = "MIT"