diff --git a/README.md b/README.md index 42313f7..6b5b644 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ quat(1.0, 2.0, 3.0, 4.0) ~= quat(1.0, 2.0, 3.0, 4.0) * `sign` - Returns the sign of a number, -1 or 1. * `quantize` - Makes v be a multiple of n. Rounding to integer quantizes by 1.0. * `lerp` - Interpolates value between a and b. +* `len` - Returns the vector component count or matrix column count. * `step` - Returns 0.0 if x is less than edge, otherwise 1.0. * `smoothstep` - Performs smooth Hermite interpolation between 0.0 and 1.0. diff --git a/src/vmath.nim b/src/vmath.nim index 833e2e3..a320736 100644 --- a/src/vmath.nim +++ b/src/vmath.nim @@ -519,6 +519,31 @@ type DVec3* = GVec3[float64] DVec4* = GVec4[float64] +when not defined(vmathArrayBased): + proc len*[T](a: GVec2[T]): int = + ## Returns the number of vector components, mirroring GLSL's .length() method. + 2 + + proc len*[T](a: GVec3[T]): int = + ## Returns the number of vector components, mirroring GLSL's .length() method. + 3 + + proc len*[T](a: GVec4[T]): int = + ## Returns the number of vector components, mirroring GLSL's .length() method. + 4 + + proc len*[T](a: GMat2[T]): int = + ## Returns the number of matrix columns, mirroring GLSL's .length() method. + 2 + + proc len*[T](a: GMat3[T]): int = + ## Returns the number of matrix columns, mirroring GLSL's .length() method. + 3 + + proc len*[T](a: GMat4[T]): int = + ## Returns the number of matrix columns, mirroring GLSL's .length() method. + 4 + proc `~=`*[T: SomeFloat](a, b: T): bool = ## Almost equal. const Epsilon = 0.000001 diff --git a/tests/tests.nim b/tests/tests.nim index f0e7a52..b035cf2 100644 --- a/tests/tests.nim +++ b/tests/tests.nim @@ -28,6 +28,24 @@ suite "approximate equality": test "int ~= should not compile": check not compiles(1 ~= 1) +suite "len": + test "vector component count": + check vec2(10.0, 2.0).len == 2 + check len(vec3(1, 2, 3)) == 3 + check vec4(0).len == 4 + check dvec4(0).len == 4 + check ivec3(0).len == 3 + check uvec2(0).len == 2 + check bvec4(false).len == 4 + + test "matrix column count": + check mat2().len == 2 + check mat3().len == 3 + check mat4().len == 4 + check dmat2().len == 2 + check dmat3().len == 3 + check dmat4().len == 4 + suite "scalar utilities": test "between": check between(0.5, 0, 1)