changeset 1318:03400de84910

add: more utils
author sam <sam@basx.dev>
date Tue, 13 Aug 2024 14:45:18 +0700
parents c5a23e910fea
children 43db6f5abb61
files semicongine/core/matrix.nim semicongine/core/vector.nim
diffstat 2 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/core/matrix.nim	Mon Aug 12 21:33:08 2024 +0700
+++ b/semicongine/core/matrix.nim	Tue Aug 13 14:45:18 2024 +0700
@@ -453,6 +453,14 @@
   result[3, 2] = (-a[3, 0] * s3 + a[3, 1] * s1 - a[3, 2] * s0) * invdet;
   result[3, 3] = ( a[2, 0] * s3 - a[2, 1] * s1 + a[2, 2] * s0) * invdet;
 
+func transformed*[T, S](points: openArray[S], mat: TMat4[T]): seq[S] =
+  for p in points:
+    result.add mat * p
+
+func transform*[T, S](points: var openArray[S], mat: TMat4[T]) =
+  for p in points.mitems:
+    p = mat * p
+
 func projection*(fovy, aspect, zNear, zFar: float32): Mat4 =
   let tanHalfFovy = 1 / tan(fovy / 2)
   return Mat4(data: [
--- a/semicongine/core/vector.nim	Mon Aug 12 21:33:08 2024 +0700
+++ b/semicongine/core/vector.nim	Tue Aug 13 14:45:18 2024 +0700
@@ -172,6 +172,12 @@
 func length*(vec: TVec4[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3])
 func length*(vec: TVec4[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]))
 
+
+func abs*[T](vec: TVec1[T]): auto = TVec1([abs(vec[0])])
+func abs*[T](vec: TVec2[T]): auto = TVec2([abs(vec[0]), abs(vec[1])])
+func abs*[T](vec: TVec3[T]): auto = TVec3([abs(vec[0]), abs(vec[1]), abs(vec[2])])
+func abs*[T](vec: TVec4[T]): auto = TVec4([abs(vec[0]), abs(vec[1]), abs(vec[2]), abs(vec[3])])
+
 func manhattan*(vec: TVec1): auto = abs(vec[0])
 func manhattan*(vec: TVec2): auto = abs(vec[0]) + abs(vec[1])
 func manhattan*(vec: TVec3): auto = abs(vec[0]) + abs(vec[1]) + abs(vec[2])