Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Supports c, cpp and js backend.

## About

Your one stop shop for vector math routines for 2d and 3d graphics.
Your one stop shop for vector math routines for 2D and 3D graphics.

* Pure Nim with no dependencies.
* Very similar to GLSL Shader Language with extra stuff.
Expand Down Expand Up @@ -42,7 +42,7 @@ float64 | double | DVec2 | DVec3 | DVec4 | DMat3 | DMat4 | DQuat |

## 2D & 3D matrix math

You can combine and create 2d and 3d matrices by passing 2d or 3d vectors to matrix functions:
You can combine and create 2D and 3D matrices by passing 2D or 3D vectors to matrix functions:

```nim
let mat2d = translate(vec2(10, 20)) * rotate(45.toRadians) * scale(vec2(2))
Expand All @@ -63,15 +63,14 @@ quat(1.0, 2.0, 3.0, 4.0) ~= quat(1.0, 2.0, 3.0, 4.0)

* `between` - Returns true if value is between min and max or equal to them.
* `sign` - Returns the sign of a number, -1 or 1.
* `quantize` - Makes v be a multiple of n. Rounding to integer quantize by 1.0.
* `fractional` - Returns fractional part of a number. 3.14 -> 0.14
* `quantize` - Makes v be a multiple of n. Rounding to integer quantizes by 1.0.
* `lerp` - Interpolates value between a and b.

## Angle functions

* `fixAngle` - Make angle be from -PI to PI radians.
* `angleBetween` - Find the angle between angle a and angle b.
* `turnAngle` - Move from angle a to angle b with step of v.
* `fixAngle` - Makes angle be from -PI to PI radians.
* `angleBetween` - Finds the angle between angle a and angle b.
* `turnAngle` - Moves from angle a to angle b with step of v.

## Vector and matrix representation and benchmarks.

Expand All @@ -93,11 +92,11 @@ vmathObjArrayBased ................ 73.968 ms 74.292 ms ±0.631 x100

## Zmod - GLSL mod

GLSL uses a different type of float point mod. Because mod is a Nim keyword please use `zmod` when you need GLSL `mod` behavior.
GLSL uses a different type of float point mod. Because mod is a Nim keyword, please use `zmod` when you need GLSL `mod` behavior.

## Coordinate System

Right-hand z-forward coordinate system
Right-hand Z-forward coordinate system

This is the same system used in the GLTF file format.

Expand All @@ -124,18 +123,18 @@ OpenGL/GLSL/vmath vs Math/Specification notation:
```

# 1.x.x to 2.0.0 vmath breaking changes:
* New right-hand-z-forward cordate system and functions that care about
coordinate system where moved there.
* deprecated `lookAt()` please use `toAngles()`/`fromAngles()` instead.
* deprecated `fractional()` use `frac()` instead.
* New right-hand-Z-forward coordinate system and functions that care about
coordinate system were moved there.
* Deprecated `lookAt()`, please use `toAngles()`/`fromAngles()` instead.
* Deprecated `fractional()`, use `frac()` instead.

# 0.x.x to 1.0.0 vmath breaking changes:

* `vec3(v)` no longer works please use `vec3(v.x, v.y, 0)` instead.
* `vec3(v, 0)` no longer works please use `vec3(v.x, v.y, 0)` instead.
* `2 * v` no longer works due to more vec types please use `v * 2` instead.
* `vec3(v)` no longer works, please use `vec3(v.x, v.y, 0)` instead.
* `vec3(v, 0)` no longer works, please use `vec3(v.x, v.y, 0)` instead.
* `2 * v` no longer works due to more vec types, please use `v * 2` instead.
* `m[15]` no longer works because matrices are now m[x, y].
* Concept of 3x3 rotation 3d matrix was removed.
* Concept of 3x3 rotation 3D matrix was removed.
* `angleBetween` got renamed to `angle(a, b)`
* `scaleMat` got renamed to `scale(v)`
* `rotationMat3` got renamed to `rotate(x)`
9 changes: 5 additions & 4 deletions src/vmath.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ float64 double DVec2 DVec3 DVec4 DMat3 DMat4 DQuat

]##

import macros, math, strutils
import
std/[macros, math, strutils]
export math except isNan

{.push inline.}
Expand Down Expand Up @@ -389,8 +390,8 @@ type

proc `~=`*[T: SomeFloat](a, b: T): bool =
## Almost equal.
const epsilon = 0.000001
abs(a - b) <= epsilon
const Epsilon = 0.000001
abs(a - b) <= Epsilon

proc between*[T](value, min, max: T): bool =
## Returns true if value is between min and max or equal to them.
Expand All @@ -411,7 +412,7 @@ proc fract*[T: SomeFloat](v: T): T =
result = abs(v)
result = result - trunc(result)

proc fractional*[T: SomeFloat](v: T): T {.deprecated: "Use frac() insetad"} =
proc fractional*[T: SomeFloat](v: T): T {.deprecated: "Use fract() instead"} =
## Returns fractional part of a number.
fract(v)

Expand Down
3 changes: 3 additions & 0 deletions src/vmath/macroswizzle.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

import
std/[macros]

{.experimental: "dotOperators".}
proc num(letter: char, fields: NimNode): int =
## Given a swizzle character gives back the location number.
Expand Down
2 changes: 1 addition & 1 deletion src/vmath/swizzle.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by tools/gensswizzle - don't edit manually.
# Generated by tools/genswizzle - don't edit manually.

# 1 x rgba
proc r*[T](a: GVec234[T]): T = a[0]
Expand Down
4 changes: 3 additions & 1 deletion tests/bench.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import benchy, vmath
import
benchy,
vmath

# TODO: I don't trust these simple benchmarks, make a better test.
# echo "new vmath"
Expand Down
4 changes: 3 additions & 1 deletion tests/bench_isNan.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import benchy, vmath
import
benchy,
vmath

proc isNaNSlow(f: SomeFloat): bool =
## Returns true if number is a NaN.
Expand Down
4 changes: 3 additions & 1 deletion tests/bench_raytracer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
## MIT License
## Copyright (c) 2021 Edin Omeragic

import benchy, chroma, math, pixie, vmath
import
std/math,
benchy, chroma, pixie, vmath

{.push inline, noinit, checks: off.}

Expand Down
4 changes: 3 additions & 1 deletion tests/bench_raytracer_glm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
## MIT License
## Copyright (c) 2021 Edin Omeragic

import benchy, chroma, math, glm
import
std/math,
benchy, chroma, glm
from pixie import Image, newImage, writeFile, dataIndex

type Vec3 = glm.Vec3[float32]
Expand Down
3 changes: 2 additions & 1 deletion tests/bench_rep.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import benchy
import
benchy

type
Vec3Obj = object
Expand Down
4 changes: 3 additions & 1 deletion tests/bench_swizzle.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import benchy, vmath
import
benchy,
vmath

block:
var
Expand Down
4 changes: 3 additions & 1 deletion tests/test.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import random, vmath
import
std/random,
vmath

randomize(1234)

Expand Down
5 changes: 3 additions & 2 deletions tools/genswizzle.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import strformat
import
std/strformat

var
swizzles = @["xyzw", "rgba", "stpq"]
code = ""

code.add "# Generated by tools/gensswizzle - don't edit manually.\n"
code.add "# Generated by tools/genswizzle - don't edit manually.\n"

for swizzle in swizzles[1 .. ^1]:
code.add "\n# 1 x " & swizzle & "\n"
Expand Down
Loading