vmath/README.md
2020-11-30 01:35:46 -06:00

17 KiB

VMath

nimble install vmath

Collection of math routines for 2d and 3d graphics.

Has functions for Vec2, Vec3, Vec4, Mat3, Mat4 and Quat.

API: vmath

import vmath

proc between

Returns true if value is between min and max or equal to them.

proc between(value, min, max: float32): bool {.inline.}

proc sign

Returns the sign of a number, -1 or 1.

proc sign(v: float32): float32 {.inline.}

proc quantize

Makes v be multipe of n. Rounding to integer quantize by 1.0.

proc quantize(v, n: float32): float32 {.inline.}

proc lerp

Interpolates value between a and b.

  • 0 -> a
  • 1 -> b
  • 0.5 -> between a and b
proc lerp(a, b, v: float32): float32 {.inline.}

proc fixAngle

Make angle be from -PI to PI radians.

proc fixAngle(angle: float32): float32

proc angleBetween

Angle between angle a and angle b.

proc angleBetween(a, b: float32): float32 {.inline.}

proc turnAngle

Move from angle a to angle b with step of v.

proc turnAngle(a, b, speed: float32): float32

type Vec2

2D vector

Vec2 = object
 x*: float32
 y*: float32

proc vec2

proc vec2(x, y: float32): Vec2 {.inline.}

proc vec2

proc vec2(v: float32): Vec2 {.inline.}

proc vec2

proc vec2(a: Vec2): Vec2 {.inline.}

proc +

proc `+`(a, b: Vec2): Vec2 {.inline.}

proc -

proc `-`(a, b: Vec2): Vec2 {.inline.}

proc *

proc `*`(a: Vec2; b: float32): Vec2 {.inline.}

proc *

proc `*`(a: float32; b: Vec2): Vec2 {.inline.}

proc /

proc `/`(a: Vec2; b: float32): Vec2 {.inline.}

proc +=

proc `+=`(a: var Vec2; b: Vec2) {.inline.}

proc -=

proc `-=`(a: var Vec2; b: Vec2) {.inline.}

proc *=

proc `*=`(a: var Vec2; b: float32) {.inline.}

proc /=

proc `/=`(a: var Vec2; b: float32) {.inline.}

proc zero

proc zero(a: var Vec2) {.inline.}

proc -

proc `-`(a: Vec2): Vec2 {.inline.}

proc hash

proc hash(a: Vec2): Hash {.inline.}

proc lengthSq

proc lengthSq(a: Vec2): float32 {.inline.}

proc length

proc length(a: Vec2): float32 {.inline.}

proc length=

proc length=(a: var Vec2; b: float32) {.inline.}

proc normalize

proc normalize(a: Vec2): Vec2 {.inline.}

proc dot

proc dot(a, b: Vec2): float32 {.inline.}

proc dir

proc dir(at, to: Vec2): Vec2 {.inline.}

proc dir

proc dir(th: float32): Vec2 {.inline.}

proc dist

proc dist(at, to: Vec2): float32 {.inline.}

proc distSq

proc distSq(at, to: Vec2): float32 {.inline.}

proc lerp

proc lerp(a, b: Vec2; v: float32): Vec2 {.inline.}

proc quantize

proc quantize(v: Vec2; n: float32): Vec2 {.inline.}

proc inRect

Check to see if v is inside a rectange formed by a and b. It does not matter how a and b are arranged.

proc inRect(v, a, b: Vec2): bool {.inline.}

proc []

proc `[]`(a: Vec2; i: int): float32

proc []=

proc `[]=`(a: var Vec2; i: int; b: float32)

proc randVec2

proc randVec2(r: var Rand): Vec2

proc $

proc `$`(a: Vec2): string {.raises: [ValueError].}

proc angle

Angle of a Vec2.

proc angle(a: Vec2): float32 {.inline.}

proc angleBetween

Angle between 2 Vec2.

proc angleBetween(a: Vec2; b: Vec2): float32 {.inline.}

type Vec3

3D vector

Vec3 = object
 x*: float32
 y*: float32
 z*: float32

proc vec3

proc vec3(x, y, z: float32): Vec3 {.inline.}

proc vec3

proc vec3(v: float32): Vec3 {.inline.}

proc vec3

proc vec3(a: Vec3): Vec3 {.inline.}

const X_DIR

X_DIR = (x: 1.0, y: 0.0, z: 0.0)

const Y_DIR

Y_DIR = (x: 0.0, y: 1.0, z: 0.0)

const Z_DIR

Z_DIR = (x: 0.0, y: 0.0, z: 1.0)

proc +

proc `+`(a, b: Vec3): Vec3 {.inline.}

proc -

proc `-`(a, b: Vec3): Vec3 {.inline.}

proc -

proc `-`(a: Vec3): Vec3 {.inline.}

proc *

proc `*`(a: Vec3; b: float32): Vec3 {.inline.}

proc *

proc `*`(a: float32; b: Vec3): Vec3 {.inline.}

proc /

proc `/`(a: Vec3; b: float32): Vec3 {.inline.}

proc /

proc `/`(a: float32; b: Vec3): Vec3 {.inline.}

proc +=

proc `+=`(a: var Vec3; b: Vec3) {.inline.}

proc -=

proc `-=`(a: var Vec3; b: Vec3) {.inline.}

proc *=

proc `*=`(a: var Vec3; b: float32) {.inline.}

proc /=

proc `/=`(a: var Vec3; b: float32) {.inline.}

proc zero

proc zero(a: var Vec3) {.inline.}

proc -

proc `-`(a: var Vec3): Vec3 {.inline.}

proc hash

proc hash(a: Vec3): Hash {.inline.}

proc lengthSq

proc lengthSq(a: Vec3): float32 {.inline.}

proc length

proc length(a: Vec3): float32 {.inline.}

proc length=

proc length=(a: var Vec3; b: float32) {.inline.}

proc floor

proc floor(a: Vec3): Vec3 {.inline.}

proc round

proc round(a: Vec3): Vec3 {.inline.}

proc ceil

proc ceil(a: Vec3): Vec3 {.inline.}

proc normalize

proc normalize(a: Vec3): Vec3 {.inline.}

proc cross

proc cross(a, b: Vec3): Vec3 {.inline.}

proc computeNormal

proc computeNormal(a, b, c: Vec3): Vec3

proc dot

proc dot(a, b: Vec3): float32 {.inline.}

proc dir

proc dir(at, to: Vec3): Vec3 {.inline.}

proc dist

proc dist(at, to: Vec3): float32 {.inline.}

proc distSq

proc distSq(at, to: Vec3): float32 {.inline.}

proc lerp

proc lerp(a, b: Vec3; v: float32): Vec3 {.inline.}

proc quantize

proc quantize(v: Vec3; n: float32): Vec3

proc angleBetween

proc angleBetween(a, b: Vec3): float32

proc []

proc `[]`(a: Vec3; i: int): float32

proc []=

proc `[]=`(a: var Vec3; i: int; b: float32)

proc xy

proc xy(a: Vec3): Vec2 {.inline.}

proc xz

proc xz(a: Vec3): Vec2 {.inline.}

proc yx

proc yx(a: Vec3): Vec2 {.inline.}

proc yz

proc yz(a: Vec3): Vec2 {.inline.}

proc zx

proc zx(a: Vec3): Vec2 {.inline.}

proc zy

proc zy(a: Vec3): Vec2 {.inline.}

proc almostEquals

proc almostEquals(a, b: Vec3; precision = 1e-006): bool {.inline, tags: [].}

proc randVec3

Generates a random unit vector based on http://mathworld.wolfram.com/SpherePointPicking.html

proc randVec3(r: var Rand): Vec3

proc $

proc `$`(a: Vec3): string {.raises: [ValueError].}

type Vec4

4D Vector.

Vec4 = object
 x*: float32
 y*: float32
 z*: float32
 w*: float32

proc vec4

proc vec4(x, y, z, w: float32): Vec4 {.inline.}

proc vec4

proc vec4(v: float32): Vec4 {.inline.}

proc +

proc `+`(a, b: Vec4): Vec4 {.inline.}

proc -

proc `-`(a, b: Vec4): Vec4 {.inline.}

proc -

proc `-`(a: Vec4): Vec4 {.inline.}

proc *

proc `*`(a: Vec4; b: float32): Vec4 {.inline.}

proc *

proc `*`(a: float32; b: Vec4): Vec4 {.inline.}

proc /

proc `/`(a: Vec4; b: float32): Vec4 {.inline.}

proc /

proc `/`(a: float32; b: Vec4): Vec4 {.inline.}

proc +=

proc `+=`(a: var Vec4; b: Vec4) {.inline.}

proc -=

proc `-=`(a: var Vec4; b: Vec4) {.inline.}

proc *=

proc `*=`(a: var Vec4; b: float32) {.inline.}

proc /=

proc `/=`(a: var Vec4; b: float32) {.inline.}

proc zero

proc zero(a: var Vec4) {.inline.}

proc hash

proc hash(a: Vec4): Hash {.inline.}

proc []

proc `[]`(a: Vec4; i: int): float32

proc []=

proc `[]=`(a: var Vec4; i: int; b: float32)

proc lerp

proc lerp(a: Vec4; b: Vec4; v: float32): Vec4 {.inline.}

proc xyz

proc xyz(a: Vec4): Vec3 {.inline.}

proc $

proc `$`(a: Vec4): string {.raises: [ValueError].}

proc vec3

proc vec3(a: Vec2; z = 0.0): Vec3 {.inline.}

proc vec4

proc vec4(a: Vec3; w = 0.0): Vec4 {.inline.}

proc vec4

proc vec4(a: Vec2; z = 0.0; w = 0.0): Vec4 {.inline.}

type Mat3

3x3 Matrix

Mat3 = array[9, float32]

template []

template `[]`(a: Mat3; i, j: int): float32

template []=

template `[]=`(a: Mat3; i, j: int; v: float32)

proc mat3

proc mat3(a, b, c, d, e, f, g, h, i: float32): Mat3 {.inline, tags: [].}

proc mat3

proc mat3(a: Mat3): Mat3 {.inline.}

proc identity

proc identity(a: var Mat3) {.inline.}

proc mat3

proc mat3(): Mat3 {.inline.}

proc transpose

proc transpose(a: Mat3): Mat3 {.inline.}

proc $

proc `$`(a: Mat3): string {.raises: [ValueError].}

proc *

proc `*`(a, b: Mat3): Mat3

proc scale

proc scale(a: Mat3; v: Vec2): Mat3 {.inline.}

proc scale

proc scale(a: Mat3; v: Vec3): Mat3 {.inline.}

proc translate

proc translate(v: Vec2): Mat3 {.inline.}

proc scale

proc scale(v: Vec2): Mat3 {.inline.}

proc rotationMat3

proc rotationMat3(angle: float32): Mat3 {.inline.}

proc rotate

proc rotate(a: Mat3; angle: float32): Mat3 {.inline.}

proc *

proc `*`(a: Mat3; b: Vec2): Vec2

proc *

proc `*`(a: Mat3; b: Vec3): Vec3

proc inverse

proc inverse(a: Mat3): Mat3

type Mat4

4x4 Matrix - OpenGL row order

Mat4 = array[16, float32]

template []

template `[]`(a: Mat4; i, j: int): float32

template []=

template `[]=`(a: Mat4; i, j: int; v: float32)

proc mat4

proc mat4(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15: float32): Mat4 {. inline.}

proc mat4

proc mat4(a: Mat4): Mat4 {.inline.}

proc identity

proc identity(): Mat4 {.inline.}

proc mat4

proc mat4(): Mat4 {.inline.}

proc transpose

proc transpose(a: Mat4): Mat4 {.inline.}

proc determinant

proc determinant(a: Mat4): float32

proc inverse

proc inverse(a: Mat4): Mat4

proc *

proc `*`(a, b: Mat4): Mat4

proc *

proc `*`(a: Mat4; b: Vec3): Vec3

proc *

proc `*`(a: Mat4; b: Vec4): Vec4

proc right

proc right(a: Mat4): Vec3 {.inline.}

proc right=

proc right=(a: var Mat4; b: Vec3) {.inline.}

proc up

proc up(a: Mat4): Vec3 {.inline.}

proc up=

proc up=(a: var Mat4; b: Vec3) {.inline.}

proc forward

proc forward(a: Mat4): Vec3 {.inline.}

proc forward=

proc forward=(a: var Mat4; b: Vec3) {.inline.}

proc pos

proc pos(a: Mat4): Vec3 {.inline.}

proc pos=

proc pos=(a: var Mat4; b: Vec3) {.inline.}

proc rotationOnly

proc rotationOnly(a: Mat4): Mat4 {.inline.}

proc dist

proc dist(a, b: Mat4): float32 {.inline.}

proc translate

proc translate(v: Vec3): Mat4

proc scale

proc scale(v: Vec3): Mat4

proc close

proc close(a: Mat4; b: Mat4): bool

proc hrp

proc hrp(m: Mat4): Vec3

proc frustum

proc frustum(left, right, bottom, top, near, far: float32): Mat4

proc perspective

proc perspective(fovy, aspect, near, far: float32): Mat4

proc ortho

proc ortho(left, right, bottom, top, near, far: float32): Mat4

proc lookAt

proc lookAt(eye, center, up: Vec3): Mat4

proc mat3

Gets rotation and translation, ignoring z coordinates.

proc mat3(m: Mat4): Mat3

proc mat3Rotation

Gets the rotational part of the 4x4 matrix.

proc mat3Rotation(m: Mat4): Mat3

proc mat4

Takes a 2d Mat3 with position and converts to a 3d matrix.

proc mat4(m: Mat3): Mat4

proc mat4Rotation

Gets the rotational part of the 3x3 matrix into a 4x4 matrix.

proc mat4Rotation(m: Mat3): Mat4

proc $

proc `$`(a: Mat4): string {.raises: [ValueError].}

type Quat

Quat = object
 x*: float32
 y*: float32
 z*: float32
 w*: float32

proc quat

proc quat(x, y, z, w: float32): Quat {.inline.}

proc conjugate

proc conjugate(q: Quat): Quat {.inline.}

proc length

proc length(q: Quat): float32 {.inline.}

proc normalize

proc normalize(q: Quat): Quat

proc xyz

proc xyz(q: Quat): Vec3 {.inline.}

proc xyz=

proc xyz=(q: var Quat; v: Vec3) {.inline.}

proc -

proc `-`(a: var Quat): Quat {.inline.}

proc +

proc `+`(a: Quat; b: Quat): Quat {.inline.}

proc *

Multiply the quaternion by a quaternion.

proc `*`(a, b: Quat): Quat

proc *

Multiply the quaternion by a float32.

proc `*`(q: Quat; v: float32): Quat {.inline.}

proc /

Divide the quaternion by a float32.

proc `/`(q: Quat; v: float32): Quat {.inline.}

proc *=

proc `*=`(a: var Quat; b: float32) {.inline.}

proc /=

proc `/=`(a: var Quat; b: float32) {.inline.}

proc *

Multiply the quaternion by a vector.

proc `*`(q: Quat; v: Vec3): Vec3

proc []

proc `[]`(a: var Quat; i: int; b: float32)

proc []=

proc `[]=`(a: var Quat; i: int; b: float32)

proc mat3

proc mat3(q: Quat): Mat3

proc mat4

proc mat4(q: Quat): Mat4

proc recifuncalSqrt

proc recifuncalSqrt(x: float32): float32 {.inline.}

proc quat

proc quat(m: Mat4): Quat

proc fromAxisAngle

proc fromAxisAngle(axis: Vec3; angle: float32): Quat

proc toAxisAngle

proc toAxisAngle(q: Quat; axis: var Vec3; angle: var float32)

proc quat

proc quat(heading, pitch, roll: float32): Quat

proc quat

proc quat(hpr: Vec3): Quat {.inline.}

proc hrp

proc hrp(q: Quat): Vec3

proc dot

proc dot(a: Quat; b: Quat): float32 {.inline.}

proc nlerp

proc nlerp(a: Quat; b: Quat; v: float32): Quat

proc $

proc `$`(a: Quat): string {.raises: [ValueError].}

proc rotate

proc rotate(angle: float32; axis: Vec3): Mat4 {.inline.}

proc rotateX

proc rotateX(angle: float32): Mat4 {.inline.}

proc rotateY

proc rotateY(angle: float32): Mat4 {.inline.}

proc rotateZ

proc rotateZ(angle: float32): Mat4 {.inline.}

proc scaleMat

proc scaleMat(scale: Vec3): Mat4 {.inline.}

proc scaleMat

proc scaleMat(scale: float32): Mat4 {.inline.}

type Rect

Rect = object
 x*: float32
 y*: float32
 w*: float32
 h*: float32

proc rect

proc rect(x, y, w, h: float32): Rect

proc rect

proc rect(pos, size: Vec2): Rect

proc xy

Gets the xy as a Vec2.

proc xy(rect: Rect): Vec2

proc xy=

Sets the xy from Vec2.

proc xy=(rect: var Rect; v: Vec2)

proc wh

Gets the wh as a Vec2.

proc wh(rect: Rect): Vec2

proc wh=

Sets the wh from Vec2.

proc wh=(rect: var Rect; v: Vec2)

proc *

  • all elements of a Rect.
proc `*`(r: Rect; v: float): Rect

proc /

/ all elements of a Rect.

proc `/`(r: Rect; v: float): Rect

proc +

Add two boxes together.

proc `+`(a, b: Rect): Rect

proc $

proc `$`(a: Rect): string {.raises: [ValueError].}

proc inside

Checks if pos is inside rect.

proc inside(pos: Vec2; rect: Rect): bool

proc overlap

Returns true if box a overlaps box b.

proc overlap(a, b: Rect): bool

proc or

Union of two rectangles.

proc `or`(a, b: Rect): Rect

proc and

Intersection of two rectangles.

proc `and`(a, b: Rect): Rect