Basic 3d support with vectors, points, matrices and some basic utilities. Vectors are implemented as direction vectors, ie. when transformed with a matrix the translation part of matrix is ignored. The coordinate system used is right handed, because its compatible with 2d coordinate system (rotation around zaxis equals 2d rotation). Operators + , - , * , / , += , -= , *= and /= are implemented for vectors and scalars.
Quick start example:
# Create a matrix which first rotates, then scales and at last translates var m:Matrix3d=rotate(PI,vector3d(1,1,2.5)) & scale(2.0) & move(100.0,200.0,300.0) # Create a 3d point at (100,150,200) and a vector (5,2,3) var pt:Point3d=point3d(100.0,150.0,200.0) var vec:Vector3d=vector3d(5.0,2.0,3.0) pt &= m # transforms pt in place var pt2:Point3d=pt & m #concatenates pt with m and returns a new point var vec2:Vector3d=vec & m #concatenates vec with m and returns a new vector
Matrix3d = object ax*, ay*, az*, aw*, bx*, by*, bz*, bw*, cx*, cy*, cz*, cw*, tx*, ty*, tz*, tw*: float
Point3d = object x*, y*, z*: float
Vector3d = object x*, y*, z*: float
IDMATRIX: Matrix3d = matrix3d(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0)
ORIGO: Point3d = point3d(0.0, 0.0, 0.0)
XAXIS: Vector3d = vector3d(1.0, 0.0, 0.0)
YAXIS: Vector3d = vector3d(0.0, 1.0, 0.0)
ZAXIS: Vector3d = vector3d(0.0, 0.0, 1.0)
proc setElements(t: var Matrix3d; ax, ay, az, aw, bx, by, bz, bw, cx, cy, cz, cw, tx, ty, tz, tw: float) {. inline, raises: [], tags: [].}
proc matrix3d(ax, ay, az, aw, bx, by, bz, bw, cx, cy, cz, cw, tx, ty, tz, tw: float): Matrix3d {. noInit, raises: [], tags: [].}
proc `&`(a, b: Matrix3d): Matrix3d {.noinit, raises: [], tags: [].}
proc scale(s: float): Matrix3d {.noInit, raises: [], tags: [].}
proc scale(s: float; org: Point3d): Matrix3d {.noInit, raises: [], tags: [].}
proc stretch(sx, sy, sz: float): Matrix3d {.noInit, raises: [], tags: [].}
proc stretch(sx, sy, sz: float; org: Point3d): Matrix3d {.noInit, raises: [], tags: [].}
proc move(dx, dy, dz: float): Matrix3d {.noInit, raises: [], tags: [].}
proc move(v: Vector3d): Matrix3d {.noInit, raises: [], tags: [].}
proc rotate(angle: float; axis: Vector3d): Matrix3d {.noInit, raises: [Exception, DivByZeroError], tags: [RootEffect].}
proc rotate(angle: float; org: Point3d; axis: Vector3d): Matrix3d {.noInit, raises: [Exception, DivByZeroError], tags: [RootEffect].}
proc rotateX(angle: float): Matrix3d {.noInit, raises: [], tags: [].}
proc rotateY(angle: float): Matrix3d {.noInit, raises: [], tags: [].}
proc rotateZ(angle: float): Matrix3d {.noInit, raises: [], tags: [].}
proc isUniform(m: Matrix3d; tol = 1e-006): bool {.raises: [], tags: [].}
proc mirror(planeperp: Vector3d): Matrix3d {.noInit, raises: [Exception, DivByZeroError], tags: [RootEffect].}
proc mirror(org: Point3d; planeperp: Vector3d): Matrix3d {.noInit, raises: [Exception, DivByZeroError], tags: [RootEffect].}
proc determinant(m: Matrix3d): float {.raises: [], tags: [].}
proc inverse(m: Matrix3d): Matrix3d {.noInit, raises: [DivByZeroError], tags: [].}
proc equals(m1: Matrix3d; m2: Matrix3d; tol = 1e-006): bool {.raises: [], tags: [].}
proc `=~`(m1, m2: Matrix3d): bool {.raises: [], tags: [].}
proc transpose(m: Matrix3d): Matrix3d {.noInit, raises: [], tags: [].}
proc getXAxis(m: Matrix3d): Vector3d {.noInit, raises: [], tags: [].}
proc getYAxis(m: Matrix3d): Vector3d {.noInit, raises: [], tags: [].}
proc getZAxis(m: Matrix3d): Vector3d {.noInit, raises: [], tags: [].}
proc `$`(m: Matrix3d): string {.raises: [], tags: [].}
proc apply(m: Matrix3d; x, y, z: var float; translate = false) {.raises: [], tags: [].}
proc vector3d(x, y, z: float): Vector3d {.noInit, inline, raises: [], tags: [].}
proc len(v: Vector3d): float {.raises: [], tags: [].}
proc len=(v: var Vector3d; newlen: float) {.noInit, raises: [], tags: [].}
proc sqrLen(v: Vector3d): float {.inline, raises: [], tags: [].}
proc `$`(v: Vector3d): string {.raises: [], tags: [].}
proc `&`(v: Vector3d; m: Matrix3d): Vector3d {.noInit, raises: [], tags: [].}
proc `&=`(v: var Vector3d; m: Matrix3d) {.noInit, raises: [], tags: [].}
proc transformNorm(v: var Vector3d; m: Matrix3d) {.raises: [DivByZeroError], tags: [].}
proc transformInv(v: var Vector3d; m: Matrix3d) {.raises: [DivByZeroError], tags: [].}
proc transformNormInv(vec: var Vector3d; m: Matrix3d) {.raises: [], tags: [].}
proc tryNormalize(v: var Vector3d): bool {.raises: [], tags: [].}
proc normalize(v: var Vector3d) {.inline, raises: [DivByZeroError], tags: [].}
proc rotate(vec: var Vector3d; angle: float; axis: Vector3d) {.raises: [DivByZeroError], tags: [].}
proc scale(v: var Vector3d; s: float) {.raises: [], tags: [].}
proc stretch(v: var Vector3d; sx, sy, sz: float) {.raises: [], tags: [].}
proc mirror(v: var Vector3d; planeperp: Vector3d) {.raises: [DivByZeroError], tags: [].}
proc `-`(v: Vector3d): Vector3d {.raises: [], tags: [].}
proc `+`(a135939, b135941: Vector3d): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `+`(a135943: Vector3d; b135945: float): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `+`(a135947: float; b135949: Vector3d): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `-`(a135960, b135962: Vector3d): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `-`(a135964: Vector3d; b135966: float): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `-`(a135968: float; b135970: Vector3d): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `*`(a135981, b135983: Vector3d): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `*`(a135985: Vector3d; b135987: float): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `*`(a135989: float; b135991: Vector3d): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `/`(a136002, b136004: Vector3d): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `/`(a136006: Vector3d; b136008: float): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `/`(a136010: float; b136012: Vector3d): Vector3d {.inline, noInit, raises: [], tags: [].}
proc `+=`(a136023: var Vector3d; b136025: Vector3d) {.inline, raises: [], tags: [].}
proc `+=`(a136027: var Vector3d; b136029: float) {.inline, raises: [], tags: [].}
proc `-=`(a136097: var Vector3d; b136099: Vector3d) {.inline, raises: [], tags: [].}
proc `-=`(a136101: var Vector3d; b136103: float) {.inline, raises: [], tags: [].}
proc `*=`(a136171: var Vector3d; b136173: Vector3d) {.inline, raises: [], tags: [].}
proc `*=`(a136175: var Vector3d; b136177: float) {.inline, raises: [], tags: [].}
proc `/=`(a136245: var Vector3d; b136247: Vector3d) {.inline, raises: [], tags: [].}
proc `/=`(a136249: var Vector3d; b136251: float) {.inline, raises: [], tags: [].}
proc dot(v1, v2: Vector3d): float {.inline, raises: [], tags: [].}
proc cross(v1, v2: Vector3d): Vector3d {.inline, raises: [], tags: [].}
proc equals(v1, v2: Vector3d; tol = 1e-006): bool {.raises: [], tags: [].}
proc `=~`(v1, v2: Vector3d): bool {.raises: [], tags: [].}
proc angleTo(v1, v2: Vector3d): float {.raises: [], tags: [].}
proc arbitraryAxis(norm: Vector3d): Matrix3d {.noInit, raises: [DivByZeroError], tags: [].}
proc bisect(v1, v2: Vector3d): Vector3d {.noInit, raises: [DivByZeroError], tags: [].}
proc point3d(x, y, z: float): Point3d {.noInit, inline, raises: [], tags: [].}
proc sqrDist(a, b: Point3d): float {.raises: [], tags: [].}
proc dist(a, b: Point3d): float {.inline, raises: [], tags: [].}
proc `$`(p: Point3d): string {.raises: [], tags: [].}
proc `&`(p: Point3d; m: Matrix3d): Point3d {.raises: [], tags: [].}
proc `&=`(p: var Point3d; m: Matrix3d) {.raises: [], tags: [].}
proc transformInv(p: var Point3d; m: Matrix3d) {.raises: [DivByZeroError], tags: [].}
proc `+`(p: Point3d; v: Vector3d): Point3d {.noInit, inline, raises: [], tags: [].}
proc `+=`(p: var Point3d; v: Vector3d) {.noInit, inline, raises: [], tags: [].}
proc `-`(p: Point3d; v: Vector3d): Point3d {.noInit, inline, raises: [], tags: [].}
proc `-`(p1, p2: Point3d): Vector3d {.noInit, inline, raises: [], tags: [].}
proc `-=`(p: var Point3d; v: Vector3d) {.noInit, inline, raises: [], tags: [].}
proc `=~`(p1, p2: Point3d): bool {.inline, raises: [], tags: [].}
proc rotate(p: var Point3d; rad: float; axis: Vector3d) {.raises: [DivByZeroError], tags: [].}
proc rotate(p: var Point3d; angle: float; org: Point3d; axis: Vector3d) {. raises: [DivByZeroError], tags: [].}
proc scale(p: var Point3d; fac: float) {.inline, raises: [], tags: [].}
proc scale(p: var Point3d; fac: float; org: Point3d) {.inline, raises: [], tags: [].}
proc stretch(p: var Point3d; facx, facy, facz: float) {.inline, raises: [], tags: [].}
proc stretch(p: var Point3d; facx, facy, facz: float; org: Point3d) {.inline, raises: [], tags: [].}
proc move(p: var Point3d; dx, dy, dz: float) {.inline, raises: [], tags: [].}
proc move(p: var Point3d; v: Vector3d) {.inline, raises: [], tags: [].}
proc area(a, b, c: Point3d): float {.inline, raises: [], tags: [].}
© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/basic3d.html