mgl64

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2023 License: BSD-3-Clause Imports: 7 Imported by: 475

Documentation

Overview

Package mgl[32|64] (an abbreviation of mathgl since the packages were split between 32 and 64-bit versions) is a pure Go math package specialized for 3D math, with inspiration from GLM. It provides statically-sized vectors and matrices with compile-time generated calculations for most basic math operations. It also provides several basic graphics utilities such as bezier curves and surfaces, generation of basic primitives like circles, easy creation of common matrices such as perspective or rotation, and common operations like converting to/from screen/OpenGL coordinates or Projecting/Unprojecting from an MVP matrix. Quaternions are also supported.

The basic vectors and matrices are written with code generation, so looking directly at the source will probably be a bit confusing. I recommend looking at the Godoc instead, as all basic functions are documented.

This package is written in Column Major Order to make it easier with OpenGL. This means for uniform blocks you can use the default ordering, and when you call pass-in functions you can leave the "transpose" argument as false.

The package now contains variable sized vectors and matrices. Using these is discouraged. They exist for corner cases where you need "small" matrices that are still bigger than 4x4. An example may be a Jacobean used for inverse kinematics. Things like computer vision or general linear algebra are best left to packages more directly suited for that task -- OpenCV, BLAS, LAPACK, numpy, gonum (if you want to stay in Go), and so on.

Index

Constants

This section is empty.

Variables

View Source
var (
	MinNormal = float64(1.1754943508222875e-38) // 1 / 2**(127 - 1)
	MinValue  = float64(math.SmallestNonzeroFloat64)
	MaxValue  = float64(math.MaxFloat64)

	InfPos = float64(math.Inf(1))
	InfNeg = float64(math.Inf(-1))
	NaN    = float64(math.NaN())
)

Various useful constants.

View Source
var Epsilon float64 = 1e-10

Epsilon is some tiny value that determines how precisely equal we want our floats to be This is exported and left as a variable in case you want to change the default threshold for the purposes of certain methods (e.g. Unproject uses the default epsilon when determining if the determinant is "close enough" to zero to mean there's no inverse).

This is, obviously, not mutex protected so be **absolutely sure** that no functions using Epsilon are being executed when you change this.

Functions

func Abs

func Abs(a float64) float64

Abs is a direct copy of the math package's Abs. This is here for the mgl32 package, to prevent rampant type conversions during equality tests.

func CartesianToCylindical

func CartesianToCylindical(coord Vec3) (rho, phi, z float64)

CartesianToCylindical converts 3-dimensional cartesian coordinates (x,y,z) to cylindrical coordinates with radial distance r, azimuth phi, and height z.

All angles are in radians.

func CartesianToSpherical

func CartesianToSpherical(coord Vec3) (r, theta, phi float64)

CartesianToSpherical converts 3-dimensional cartesian coordinates (x,y,z) to spherical coordinates with radius r, inclination theta, and azimuth phi.

All angles are in radians.

func Clamp

func Clamp(a, low, high float64) float64

Clamp takes in a value and two thresholds. If the value is smaller than the low threshold, it returns the low threshold. If it's bigger than the high threshold it returns the high threshold. Otherwise it returns the value.

Useful to prevent some functions from freaking out because a value was teeeeechnically out of range.

func ClampFunc

func ClampFunc(low, high float64) func(float64) float64

ClampFunc generates a closure that returns its parameter clamped to the range [low,high].

func CopyMatMN

func CopyMatMN(dst, src *MatMxN)

CopyMatMN copies src into dst. This Reshapes dst to the same size as src.

If dst or src is nil, this is a no-op

func CylindircalToSpherical

func CylindircalToSpherical(rho, phi, z float64) (r, theta, phi2 float64)

CylindircalToSpherical converts cylindrical coordinates with radial distance r, azimuth phi, and height z to spherical coordinates with radius r, inclination theta, and azimuth phi.

Angles are in radians

func DegToRad

func DegToRad(angle float64) float64

DegToRad converts degrees to radians

func DisableMemoryPooling

func DisableMemoryPooling()

func Extract3DScale

func Extract3DScale(m Mat4) (x, y, z float64)

Extract3DScale extracts the 3d scaling from a homogeneous matrix

func ExtractMaxScale

func ExtractMaxScale(m Mat4) float64

ExtractMaxScale extracts the maximum scaling from a homogeneous matrix

func FloatEqual

func FloatEqual(a, b float64) bool

FloatEqual is a safe utility function to compare floats. It's Taken from http://floating-point-gui.de/errors/comparison/

It is slightly altered to not call Abs when not needed.

func FloatEqualFunc

func FloatEqualFunc(epsilon float64) func(float64, float64) bool

FloatEqualFunc is a utility closure that will generate a function that always approximately compares floats like FloatEqualThreshold with a different threshold.

func FloatEqualThreshold

func FloatEqualThreshold(a, b, epsilon float64) bool

FloatEqualThreshold is a utility function to compare floats. It's Taken from http://floating-point-gui.de/errors/comparison/

It is slightly altered to not call Abs when not needed.

This differs from FloatEqual in that it lets you pass in your comparison threshold, so that you can adjust the comparison value to your specific needs

func GLToScreenCoords

func GLToScreenCoords(x, y float64, screenWidth, screenHeight int) (xOut, yOut int)

GLToScreenCoords transforms from GL's proportional system to pixel coordinates.

Assumes the pixel coordinate system has its origin in the top left corner. (GL's is in the bottom left)

GL's coordinate system maps [screenWidth-1,0] to [1.0,1.0] and [0,screenHeight-1] to [-1.0,-1.0]. If x and y are out of the range, they'll still be mapped correctly, just off the screen. (e.g. if y=-3.0, you'll get 2*(screenHeight-1) for yOut)

This is similar to Project, except for 2D cases and much simpler

func IsClamped

func IsClamped(a, low, high float64) bool

IsClamped checks if a is clamped between low and high as if Clamp(a, low, high) had been called.

In most cases it's probably better to just call Clamp without checking this since it's relatively cheap.

The IsClamped functions use strict equality (meaning: not the FloatEqual function) there shouldn't be any major issues with this since clamp is often used to fix minor errors

func RadToDeg

func RadToDeg(angle float64) float64

RadToDeg converts radians to degrees

func ReticulateSplines

func ReticulateSplines(ranges [][][2]float64, cPoints [][][]Vec2, withLlamas bool)

ReticulateSplines reticulates ALL the Splines.

For the overly serious: the function is just for fun. It does nothing except prints a Maxis reference. Technically you could "reticulate splines" by joining a bunch of splines together, but that ruins the joke.

func Round

func Round(v float64, precision int) float64

Round shortens a float32 value to a specified precision (number of digits after the decimal point) with "round half up" tie-braking rule. Half-way values (23.5) are always rounded up (24).

func ScreenToGLCoords

func ScreenToGLCoords(x, y int, screenWidth, screenHeight int) (xOut, yOut float64)

ScreenToGLCoords transforms from pixel coordinates to GL coordinates.

This assumes that your pixel coordinate system considers its origin to be in the top left corner (GL's is in the bottom left). The coordinates x and y may be out of the range [0,screenWidth-1] and [0,screeneHeight-1].

GL's coordinate system maps [screenWidth-1,0] to [1.0,1.0] and [0,screenHeight-1] to [-1.0,-1.0]. If x and y are out of the range, they'll still be mapped correctly, just off the screen. (e.g. if y = 2*(screenHeight-1) you'll get -3.0 for yOut)

This is similar to Unproject, except for 2D cases and much simpler (especially since an inverse may always be found)

func SetMax

func SetMax(a, b *float64)

SetMax sets a to b if a < b.

func SetMin

func SetMin(a, b *float64)

SetMin sets a to b if a > b.

func SphericalToCylindrical

func SphericalToCylindrical(r, theta, phi float64) (rho, phi2, z float64)

SphericalToCylindrical converts spherical coordinates with radius r, inclination theta, and azimuth phi to cylindrical coordinates with radial distance r, azimuth phi, and height z.

Angles are in radians

Types

type InferMatrixError

type InferMatrixError struct{}

InferMatrixError may be returned by InferMatrix.

Make sure you're using a constant matrix such as Mat3 from within the same package (meaning: mgl32.MatMxN can't handle a mgl64.Mat2x3).

func (InferMatrixError) Error

func (me InferMatrixError) Error() string

type Mat2

type Mat2 [4]float64

func Diag2

func Diag2(v Vec2) Mat2

Diag2 creates a diagonal matrix from the entries of the input vector. That is, for each pointer for row==col, vector[row] is the entry. Otherwise it's 0.

Another way to think about it is that the identity is this function where the every vector element is 1.

func Ident2

func Ident2() Mat2

Ident2 returns the 2x2 identity matrix. The identity matrix is a square matrix with the value 1 on its diagonals. The characteristic property of the identity matrix is that any matrix multiplied by it is itself. (MI = M; IN = N)

func Mat2FromCols

func Mat2FromCols(col0, col1 Vec2) Mat2

Mat2FromCols builds a new matrix from column vectors.

func Mat2FromRows

func Mat2FromRows(row0, row1 Vec2) Mat2

func Rotate2D

func Rotate2D(angle float64) Mat2

Rotate2D returns a rotation Matrix about a angle in 2-D space. Specifically about the origin. It is a 2x2 matrix, if you need a 3x3 for Homogeneous math (e.g. composition with a Translation matrix) see HomogRotate2D

func (Mat2) Abs

func (m Mat2) Abs() Mat2

Abs returns the element-wise absolute value of this matrix

func (Mat2) Add

func (m1 Mat2) Add(m2 Mat2) Mat2

Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.

func (Mat2) ApproxEqual

func (m1 Mat2) ApproxEqual(m2 Mat2) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat2) ApproxEqualThreshold

func (m1 Mat2) ApproxEqualThreshold(m2 Mat2, threshold float64) bool

ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.

func (Mat2) ApproxFuncEqual

func (m1 Mat2) ApproxFuncEqual(m2 Mat2, eq func(float64, float64) bool) bool

ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.

func (Mat2) At

func (m Mat2) At(row, col int) float64

At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)

This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.

func (Mat2) Col

func (m Mat2) Col(col int) Vec2

Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.

func (Mat2) Cols

func (m Mat2) Cols() (col0, col1 Vec2)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat2) Det

func (m Mat2) Det() float64

Det returns the determinant of a matrix. It is a measure of a square matrix's singularity and invertability, among other things. In this library, the determinant is hard coded based on pre-computed cofactor expansion, and uses no loops. Of course, the addition and multiplication must still be done.

func (Mat2) Diag

func (m Mat2) Diag() Vec2

Diag is a basic operation on a square matrix that simply returns main diagonal (meaning all elements such that row==col).

func (Mat2) Index

func (m Mat2) Index(row, col int) int

Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.

This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.

func (Mat2) Inv

func (m Mat2) Inv() Mat2

Inv computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the original, yields the identity.

M_inv * M = M * M_inv = I

In this library, the math is precomputed, and uses no loops, though the multiplications, additions, determinant calculation, and scaling are still done. This can still be (relatively) expensive for a 4x4.

This function checks the determinant to see if the matrix is invertible. If the determinant is 0.0, this function returns the zero matrix. However, due to floating point errors, it is entirely plausible to get a false positive or negative. In the future, an alternate function may be written which takes in a pre-computed determinant.

func (Mat2) Mat3

func (m Mat2) Mat3() Mat3

func (Mat2) Mat4

func (m Mat2) Mat4() Mat4

func (Mat2) Mul

func (m1 Mat2) Mul(c float64) Mat2

Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.

func (Mat2) Mul2

func (m1 Mat2) Mul2(m2 Mat2) Mat2

Mul2 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2) Mul2x1

func (m1 Mat2) Mul2x1(m2 Vec2) Vec2

Mul2x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2) Mul2x3

func (m1 Mat2) Mul2x3(m2 Mat2x3) Mat2x3

Mul2x3 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2) Mul2x4

func (m1 Mat2) Mul2x4(m2 Mat2x4) Mat2x4

Mul2x4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2) Row

func (m Mat2) Row(row int) Vec2

Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.

func (Mat2) Rows

func (m Mat2) Rows() (row0, row1 Vec2)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat2) Set

func (m *Mat2) Set(row, col int, value float64)

Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.

This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.

func (*Mat2) SetCol

func (m *Mat2) SetCol(col int, v Vec2)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat2) SetRow

func (m *Mat2) SetRow(row int, v Vec2)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat2) String

func (m Mat2) String() string

Pretty prints the matrix

func (Mat2) Sub

func (m1 Mat2) Sub(m2 Mat2) Mat2

Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.

func (Mat2) Trace

func (m Mat2) Trace() float64

Trace is a basic operation on a square matrix that simply sums up all elements on the main diagonal (meaning all elements such that row==col).

func (Mat2) Transpose

func (m1 Mat2) Transpose() Mat2

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type Mat2x3

type Mat2x3 [6]float64

func Mat2x3FromCols

func Mat2x3FromCols(col0, col1, col2 Vec2) Mat2x3

Mat2x3FromCols builds a new matrix from column vectors.

func Mat2x3FromRows

func Mat2x3FromRows(row0, row1 Vec3) Mat2x3

func (Mat2x3) Abs

func (m Mat2x3) Abs() Mat2x3

Abs returns the element-wise absolute value of this matrix

func (Mat2x3) Add

func (m1 Mat2x3) Add(m2 Mat2x3) Mat2x3

Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.

func (Mat2x3) ApproxEqual

func (m1 Mat2x3) ApproxEqual(m2 Mat2x3) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat2x3) ApproxEqualThreshold

func (m1 Mat2x3) ApproxEqualThreshold(m2 Mat2x3, threshold float64) bool

ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.

func (Mat2x3) ApproxFuncEqual

func (m1 Mat2x3) ApproxFuncEqual(m2 Mat2x3, eq func(float64, float64) bool) bool

ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.

func (Mat2x3) At

func (m Mat2x3) At(row, col int) float64

At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)

This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.

func (Mat2x3) Col

func (m Mat2x3) Col(col int) Vec2

Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.

func (Mat2x3) Cols

func (m Mat2x3) Cols() (col0, col1, col2 Vec2)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat2x3) Index

func (m Mat2x3) Index(row, col int) int

Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.

This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.

func (Mat2x3) Mul

func (m1 Mat2x3) Mul(c float64) Mat2x3

Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.

func (Mat2x3) Mul3

func (m1 Mat2x3) Mul3(m2 Mat3) Mat2x3

Mul3 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2x3) Mul3x1

func (m1 Mat2x3) Mul3x1(m2 Vec3) Vec2

Mul3x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2x3) Mul3x2

func (m1 Mat2x3) Mul3x2(m2 Mat3x2) Mat2

Mul3x2 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2x3) Mul3x4

func (m1 Mat2x3) Mul3x4(m2 Mat3x4) Mat2x4

Mul3x4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2x3) Row

func (m Mat2x3) Row(row int) Vec3

Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.

func (Mat2x3) Rows

func (m Mat2x3) Rows() (row0, row1 Vec3)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat2x3) Set

func (m *Mat2x3) Set(row, col int, value float64)

Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.

This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.

func (*Mat2x3) SetCol

func (m *Mat2x3) SetCol(col int, v Vec2)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat2x3) SetRow

func (m *Mat2x3) SetRow(row int, v Vec3)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat2x3) String

func (m Mat2x3) String() string

Pretty prints the matrix

func (Mat2x3) Sub

func (m1 Mat2x3) Sub(m2 Mat2x3) Mat2x3

Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.

func (Mat2x3) Transpose

func (m1 Mat2x3) Transpose() Mat3x2

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type Mat2x4

type Mat2x4 [8]float64

func Mat2x4FromCols

func Mat2x4FromCols(col0, col1, col2, col3 Vec2) Mat2x4

Mat2x4FromCols builds a new matrix from column vectors.

func Mat2x4FromRows

func Mat2x4FromRows(row0, row1 Vec4) Mat2x4

func (Mat2x4) Abs

func (m Mat2x4) Abs() Mat2x4

Abs returns the element-wise absolute value of this matrix

func (Mat2x4) Add

func (m1 Mat2x4) Add(m2 Mat2x4) Mat2x4

Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.

func (Mat2x4) ApproxEqual

func (m1 Mat2x4) ApproxEqual(m2 Mat2x4) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat2x4) ApproxEqualThreshold

func (m1 Mat2x4) ApproxEqualThreshold(m2 Mat2x4, threshold float64) bool

ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.

func (Mat2x4) ApproxFuncEqual

func (m1 Mat2x4) ApproxFuncEqual(m2 Mat2x4, eq func(float64, float64) bool) bool

ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.

func (Mat2x4) At

func (m Mat2x4) At(row, col int) float64

At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)

This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.

func (Mat2x4) Col

func (m Mat2x4) Col(col int) Vec2

Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.

func (Mat2x4) Cols

func (m Mat2x4) Cols() (col0, col1, col2, col3 Vec2)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat2x4) Index

func (m Mat2x4) Index(row, col int) int

Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.

This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.

func (Mat2x4) Mul

func (m1 Mat2x4) Mul(c float64) Mat2x4

Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.

func (Mat2x4) Mul4

func (m1 Mat2x4) Mul4(m2 Mat4) Mat2x4

Mul4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2x4) Mul4x1

func (m1 Mat2x4) Mul4x1(m2 Vec4) Vec2

Mul4x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2x4) Mul4x2

func (m1 Mat2x4) Mul4x2(m2 Mat4x2) Mat2

Mul4x2 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2x4) Mul4x3

func (m1 Mat2x4) Mul4x3(m2 Mat4x3) Mat2x3

Mul4x3 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat2x4) Row

func (m Mat2x4) Row(row int) Vec4

Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.

func (Mat2x4) Rows

func (m Mat2x4) Rows() (row0, row1 Vec4)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat2x4) Set

func (m *Mat2x4) Set(row, col int, value float64)

Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.

This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.

func (*Mat2x4) SetCol

func (m *Mat2x4) SetCol(col int, v Vec2)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat2x4) SetRow

func (m *Mat2x4) SetRow(row int, v Vec4)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat2x4) String

func (m Mat2x4) String() string

Pretty prints the matrix

func (Mat2x4) Sub

func (m1 Mat2x4) Sub(m2 Mat2x4) Mat2x4

Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.

func (Mat2x4) Transpose

func (m1 Mat2x4) Transpose() Mat4x2

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type Mat3

type Mat3 f64.Mat3

func Diag3

func Diag3(v Vec3) Mat3

Diag3 creates a diagonal matrix from the entries of the input vector. That is, for each pointer for row==col, vector[row] is the entry. Otherwise it's 0.

Another way to think about it is that the identity is this function where the every vector element is 1.

func HomogRotate2D

func HomogRotate2D(angle float64) Mat3

HomogRotate2D is the same as Rotate2D, except homogeneous (3x3 with the extra row/col being all zeroes with a one in the bottom right)

func Ident3

func Ident3() Mat3

Ident3 returns the 3x3 identity matrix. The identity matrix is a square matrix with the value 1 on its diagonals. The characteristic property of the identity matrix is that any matrix multiplied by it is itself. (MI = M; IN = N)

func Mat3FromCols

func Mat3FromCols(col0, col1, col2 Vec3) Mat3

Mat3FromCols builds a new matrix from column vectors.

func Mat3FromRows

func Mat3FromRows(row0, row1, row2 Vec3) Mat3

func Mat4Normal

func Mat4Normal(m Mat4) Mat3

Mat4Normal calculates the Normal of the Matrix (aka the inverse transpose)

func Rotate3DX

func Rotate3DX(angle float64) Mat3

Rotate3DX returns a 3x3 (non-homogeneous) Matrix that rotates by angle about the X-axis

Where c is cos(angle) and s is sin(angle)

[1 0 0]
[0 c -s]
[0 s c ]

func Rotate3DY

func Rotate3DY(angle float64) Mat3

Rotate3DY returns a 3x3 (non-homogeneous) Matrix that rotates by angle about the Y-axis

Where c is cos(angle) and s is sin(angle)

[c 0 s]
[0 1 0]
[s 0 c ]

func Rotate3DZ

func Rotate3DZ(angle float64) Mat3

Rotate3DZ returns a 3x3 (non-homogeneous) Matrix that rotates by angle about the Z-axis

Where c is cos(angle) and s is sin(angle)

[c -s 0]
[s c 0]
[0 0 1 ]

func Scale2D

func Scale2D(scaleX, scaleY float64) Mat3

Scale2D creates a homogeneous 2D scaling matrix [[ scaleX, 0 , 0 ]] [[ 0 , scaleY, 0 ]] [[ 0 , 0 , 1 ]]

func ShearX2D

func ShearX2D(shear float64) Mat3

ShearX2D creates a homogeneous 2D shear matrix along the X-axis

func ShearY2D

func ShearY2D(shear float64) Mat3

ShearY2D creates a homogeneous 2D shear matrix along the Y-axis

func Translate2D

func Translate2D(Tx, Ty float64) Mat3

Translate2D returns a homogeneous (3x3 for 2D-space) Translation matrix that moves a point by Tx units in the x-direction and Ty units in the y-direction

[[1, 0, Tx]]
[[0, 1, Ty]]
[[0, 0, 1 ]]

func (Mat3) Abs

func (m Mat3) Abs() Mat3

Abs returns the element-wise absolute value of this matrix

func (Mat3) Add

func (m1 Mat3) Add(m2 Mat3) Mat3

Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.

func (Mat3) ApproxEqual

func (m1 Mat3) ApproxEqual(m2 Mat3) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat3) ApproxEqualThreshold

func (m1 Mat3) ApproxEqualThreshold(m2 Mat3, threshold float64) bool

ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.

func (Mat3) ApproxFuncEqual

func (m1 Mat3) ApproxFuncEqual(m2 Mat3, eq func(float64, float64) bool) bool

ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.

func (Mat3) At

func (m Mat3) At(row, col int) float64

At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)

This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.

func (Mat3) Col

func (m Mat3) Col(col int) Vec3

Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.

func (Mat3) Cols

func (m Mat3) Cols() (col0, col1, col2 Vec3)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat3) Det

func (m Mat3) Det() float64

Det returns the determinant of a matrix. It is a measure of a square matrix's singularity and invertability, among other things. In this library, the determinant is hard coded based on pre-computed cofactor expansion, and uses no loops. Of course, the addition and multiplication must still be done.

func (Mat3) Diag

func (m Mat3) Diag() Vec3

Diag is a basic operation on a square matrix that simply returns main diagonal (meaning all elements such that row==col).

func (Mat3) Index

func (m Mat3) Index(row, col int) int

Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.

This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.

func (Mat3) Inv

func (m Mat3) Inv() Mat3

Inv computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the original, yields the identity.

M_inv * M = M * M_inv = I

In this library, the math is precomputed, and uses no loops, though the multiplications, additions, determinant calculation, and scaling are still done. This can still be (relatively) expensive for a 4x4.

This function checks the determinant to see if the matrix is invertible. If the determinant is 0.0, this function returns the zero matrix. However, due to floating point errors, it is entirely plausible to get a false positive or negative. In the future, an alternate function may be written which takes in a pre-computed determinant.

func (Mat3) Mat2

func (m Mat3) Mat2() Mat2

func (Mat3) Mat4

func (m Mat3) Mat4() Mat4

func (Mat3) Mul

func (m1 Mat3) Mul(c float64) Mat3

Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.

func (Mat3) Mul3

func (m1 Mat3) Mul3(m2 Mat3) Mat3

Mul3 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3) Mul3x1

func (m1 Mat3) Mul3x1(m2 Vec3) Vec3

Mul3x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3) Mul3x2

func (m1 Mat3) Mul3x2(m2 Mat3x2) Mat3x2

Mul3x2 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3) Mul3x4

func (m1 Mat3) Mul3x4(m2 Mat3x4) Mat3x4

Mul3x4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3) Row

func (m Mat3) Row(row int) Vec3

Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.

func (Mat3) Rows

func (m Mat3) Rows() (row0, row1, row2 Vec3)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat3) Set

func (m *Mat3) Set(row, col int, value float64)

Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.

This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.

func (*Mat3) SetCol

func (m *Mat3) SetCol(col int, v Vec3)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat3) SetRow

func (m *Mat3) SetRow(row int, v Vec3)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat3) String

func (m Mat3) String() string

Pretty prints the matrix

func (Mat3) Sub

func (m1 Mat3) Sub(m2 Mat3) Mat3

Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.

func (Mat3) Trace

func (m Mat3) Trace() float64

Trace is a basic operation on a square matrix that simply sums up all elements on the main diagonal (meaning all elements such that row==col).

func (Mat3) Transpose

func (m1 Mat3) Transpose() Mat3

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type Mat3x2

type Mat3x2 [6]float64

func Mat3x2FromCols

func Mat3x2FromCols(col0, col1 Vec3) Mat3x2

Mat3x2FromCols builds a new matrix from column vectors.

func Mat3x2FromRows

func Mat3x2FromRows(row0, row1, row2 Vec2) Mat3x2

func (Mat3x2) Abs

func (m Mat3x2) Abs() Mat3x2

Abs returns the element-wise absolute value of this matrix

func (Mat3x2) Add

func (m1 Mat3x2) Add(m2 Mat3x2) Mat3x2

Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.

func (Mat3x2) ApproxEqual

func (m1 Mat3x2) ApproxEqual(m2 Mat3x2) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat3x2) ApproxEqualThreshold

func (m1 Mat3x2) ApproxEqualThreshold(m2 Mat3x2, threshold float64) bool

ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.

func (Mat3x2) ApproxFuncEqual

func (m1 Mat3x2) ApproxFuncEqual(m2 Mat3x2, eq func(float64, float64) bool) bool

ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.

func (Mat3x2) At

func (m Mat3x2) At(row, col int) float64

At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)

This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.

func (Mat3x2) Col

func (m Mat3x2) Col(col int) Vec3

Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.

func (Mat3x2) Cols

func (m Mat3x2) Cols() (col0, col1 Vec3)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat3x2) Index

func (m Mat3x2) Index(row, col int) int

Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.

This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.

func (Mat3x2) Mul

func (m1 Mat3x2) Mul(c float64) Mat3x2

Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.

func (Mat3x2) Mul2

func (m1 Mat3x2) Mul2(m2 Mat2) Mat3x2

Mul2 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3x2) Mul2x1

func (m1 Mat3x2) Mul2x1(m2 Vec2) Vec3

Mul2x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3x2) Mul2x3

func (m1 Mat3x2) Mul2x3(m2 Mat2x3) Mat3

Mul2x3 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3x2) Mul2x4

func (m1 Mat3x2) Mul2x4(m2 Mat2x4) Mat3x4

Mul2x4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3x2) Row

func (m Mat3x2) Row(row int) Vec2

Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.

func (Mat3x2) Rows

func (m Mat3x2) Rows() (row0, row1, row2 Vec2)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat3x2) Set

func (m *Mat3x2) Set(row, col int, value float64)

Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.

This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.

func (*Mat3x2) SetCol

func (m *Mat3x2) SetCol(col int, v Vec3)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat3x2) SetRow

func (m *Mat3x2) SetRow(row int, v Vec2)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat3x2) String

func (m Mat3x2) String() string

Pretty prints the matrix

func (Mat3x2) Sub

func (m1 Mat3x2) Sub(m2 Mat3x2) Mat3x2

Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.

func (Mat3x2) Transpose

func (m1 Mat3x2) Transpose() Mat2x3

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type Mat3x4

type Mat3x4 [12]float64

func Mat3x4FromCols

func Mat3x4FromCols(col0, col1, col2, col3 Vec3) Mat3x4

Mat3x4FromCols builds a new matrix from column vectors.

func Mat3x4FromRows

func Mat3x4FromRows(row0, row1, row2 Vec4) Mat3x4

func (Mat3x4) Abs

func (m Mat3x4) Abs() Mat3x4

Abs returns the element-wise absolute value of this matrix

func (Mat3x4) Add

func (m1 Mat3x4) Add(m2 Mat3x4) Mat3x4

Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.

func (Mat3x4) ApproxEqual

func (m1 Mat3x4) ApproxEqual(m2 Mat3x4) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat3x4) ApproxEqualThreshold

func (m1 Mat3x4) ApproxEqualThreshold(m2 Mat3x4, threshold float64) bool

ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.

func (Mat3x4) ApproxFuncEqual

func (m1 Mat3x4) ApproxFuncEqual(m2 Mat3x4, eq func(float64, float64) bool) bool

ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.

func (Mat3x4) At

func (m Mat3x4) At(row, col int) float64

At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)

This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.

func (Mat3x4) Col

func (m Mat3x4) Col(col int) Vec3

Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.

func (Mat3x4) Cols

func (m Mat3x4) Cols() (col0, col1, col2, col3 Vec3)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat3x4) Index

func (m Mat3x4) Index(row, col int) int

Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.

This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.

func (Mat3x4) Mul

func (m1 Mat3x4) Mul(c float64) Mat3x4

Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.

func (Mat3x4) Mul4

func (m1 Mat3x4) Mul4(m2 Mat4) Mat3x4

Mul4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3x4) Mul4x1

func (m1 Mat3x4) Mul4x1(m2 Vec4) Vec3

Mul4x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3x4) Mul4x2

func (m1 Mat3x4) Mul4x2(m2 Mat4x2) Mat3x2

Mul4x2 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3x4) Mul4x3

func (m1 Mat3x4) Mul4x3(m2 Mat4x3) Mat3

Mul4x3 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat3x4) Row

func (m Mat3x4) Row(row int) Vec4

Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.

func (Mat3x4) Rows

func (m Mat3x4) Rows() (row0, row1, row2 Vec4)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat3x4) Set

func (m *Mat3x4) Set(row, col int, value float64)

Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.

This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.

func (*Mat3x4) SetCol

func (m *Mat3x4) SetCol(col int, v Vec3)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat3x4) SetRow

func (m *Mat3x4) SetRow(row int, v Vec4)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat3x4) String

func (m Mat3x4) String() string

Pretty prints the matrix

func (Mat3x4) Sub

func (m1 Mat3x4) Sub(m2 Mat3x4) Mat3x4

Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.

func (Mat3x4) Transpose

func (m1 Mat3x4) Transpose() Mat4x3

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type Mat4

type Mat4 f64.Mat4

func Diag4

func Diag4(v Vec4) Mat4

Diag4 creates a diagonal matrix from the entries of the input vector. That is, for each pointer for row==col, vector[row] is the entry. Otherwise it's 0.

Another way to think about it is that the identity is this function where the every vector element is 1.

func Frustum

func Frustum(left, right, bottom, top, near, far float64) Mat4

Frustum generates a Frustum Matrix.

func HomogRotate3D

func HomogRotate3D(angle float64, axis Vec3) Mat4

HomogRotate3D creates a 3D rotation Matrix that rotates by (radian) angle about some arbitrary axis given by a normalized Vector. It produces a homogeneous matrix (4x4)

Where c is cos(angle) and s is sin(angle), and x, y, and z are the first, second, and third elements of the axis vector (respectively):

[[ x^2(1-c)+c, xy(1-c)-zs, xz(1-c)+ys, 0 ]]
[[ xy(1-c)+zs, y^2(1-c)+c, yz(1-c)-xs, 0 ]]
[[ xz(1-c)-ys, yz(1-c)+xs, z^2(1-c)+c, 0 ]]
[[ 0         , 0         , 0         , 1 ]]

func HomogRotate3DX

func HomogRotate3DX(angle float64) Mat4

HomogRotate3DX is the same as Rotate3DX, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)

func HomogRotate3DY

func HomogRotate3DY(angle float64) Mat4

HomogRotate3DY is the same as Rotate3DY, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)

func HomogRotate3DZ

func HomogRotate3DZ(angle float64) Mat4

HomogRotate3DZ is the same as Rotate3DZ, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)

func Ident4

func Ident4() Mat4

Ident4 returns the 4x4 identity matrix. The identity matrix is a square matrix with the value 1 on its diagonals. The characteristic property of the identity matrix is that any matrix multiplied by it is itself. (MI = M; IN = N)

func LookAt

func LookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ float64) Mat4

LookAt generates a transform matrix from world space to the given eye space.

func LookAtV

func LookAtV(eye, center, up Vec3) Mat4

LookAtV generates a transform matrix from world space into the specific eye space.

func Mat4FromCols

func Mat4FromCols(col0, col1, col2, col3 Vec4) Mat4

Mat4FromCols builds a new matrix from column vectors.

func Mat4FromRows

func Mat4FromRows(row0, row1, row2, row3 Vec4) Mat4

func Ortho

func Ortho(left, right, bottom, top, near, far float64) Mat4

Ortho generates an Ortho Matrix.

func Ortho2D

func Ortho2D(left, right, bottom, top float64) Mat4

Ortho2D is equivalent to Ortho with the near and far planes being -1 and 1, respectively.

func Perspective

func Perspective(fovy, aspect, near, far float64) Mat4

Perspective generates a Perspective Matrix.

func Scale3D

func Scale3D(scaleX, scaleY, scaleZ float64) Mat4

Scale3D creates a homogeneous 3D scaling matrix [[ scaleX, 0 , 0 , 0 ]] [[ 0 , scaleY, 0 , 0 ]] [[ 0 , 0 , scaleZ, 0 ]] [[ 0 , 0 , 0 , 1 ]]

func ShearX3D

func ShearX3D(shearY, shearZ float64) Mat4

ShearX3D creates a homogeneous 3D shear matrix along the X-axis

func ShearY3D

func ShearY3D(shearX, shearZ float64) Mat4

ShearY3D creates a homogeneous 3D shear matrix along the Y-axis

func ShearZ3D

func ShearZ3D(shearX, shearY float64) Mat4

ShearZ3D creates a homogeneous 3D shear matrix along the Z-axis

func Translate3D

func Translate3D(Tx, Ty, Tz float64) Mat4

Translate3D returns a homogeneous (4x4 for 3D-space) Translation matrix that moves a point by Tx units in the x-direction, Ty units in the y-direction, and Tz units in the z-direction

[[1, 0, 0, Tx]]
[[0, 1, 0, Ty]]
[[0, 0, 1, Tz]]
[[0, 0, 0, 1 ]]

func (Mat4) Abs

func (m Mat4) Abs() Mat4

Abs returns the element-wise absolute value of this matrix

func (Mat4) Add

func (m1 Mat4) Add(m2 Mat4) Mat4

Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.

func (Mat4) ApproxEqual

func (m1 Mat4) ApproxEqual(m2 Mat4) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat4) ApproxEqualThreshold

func (m1 Mat4) ApproxEqualThreshold(m2 Mat4, threshold float64) bool

ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.

func (Mat4) ApproxFuncEqual

func (m1 Mat4) ApproxFuncEqual(m2 Mat4, eq func(float64, float64) bool) bool

ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.

func (Mat4) At

func (m Mat4) At(row, col int) float64

At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)

This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.

func (Mat4) Col

func (m Mat4) Col(col int) Vec4

Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.

func (Mat4) Cols

func (m Mat4) Cols() (col0, col1, col2, col3 Vec4)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat4) Det

func (m Mat4) Det() float64

Det returns the determinant of a matrix. It is a measure of a square matrix's singularity and invertability, among other things. In this library, the determinant is hard coded based on pre-computed cofactor expansion, and uses no loops. Of course, the addition and multiplication must still be done.

func (Mat4) Diag

func (m Mat4) Diag() Vec4

Diag is a basic operation on a square matrix that simply returns main diagonal (meaning all elements such that row==col).

func (Mat4) Index

func (m Mat4) Index(row, col int) int

Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.

This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.

func (Mat4) Inv

func (m Mat4) Inv() Mat4

Inv computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the original, yields the identity.

M_inv * M = M * M_inv = I

In this library, the math is precomputed, and uses no loops, though the multiplications, additions, determinant calculation, and scaling are still done. This can still be (relatively) expensive for a 4x4.

This function checks the determinant to see if the matrix is invertible. If the determinant is 0.0, this function returns the zero matrix. However, due to floating point errors, it is entirely plausible to get a false positive or negative. In the future, an alternate function may be written which takes in a pre-computed determinant.

func (Mat4) Mat2

func (m Mat4) Mat2() Mat2

func (Mat4) Mat3

func (m Mat4) Mat3() Mat3

func (Mat4) Mul

func (m1 Mat4) Mul(c float64) Mat4

Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.

func (Mat4) Mul4

func (m1 Mat4) Mul4(m2 Mat4) Mat4

Mul4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4) Mul4x1

func (m1 Mat4) Mul4x1(m2 Vec4) Vec4

Mul4x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4) Mul4x2

func (m1 Mat4) Mul4x2(m2 Mat4x2) Mat4x2

Mul4x2 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4) Mul4x3

func (m1 Mat4) Mul4x3(m2 Mat4x3) Mat4x3

Mul4x3 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4) Row

func (m Mat4) Row(row int) Vec4

Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.

func (Mat4) Rows

func (m Mat4) Rows() (row0, row1, row2, row3 Vec4)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat4) Set

func (m *Mat4) Set(row, col int, value float64)

Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.

This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.

func (*Mat4) SetCol

func (m *Mat4) SetCol(col int, v Vec4)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat4) SetRow

func (m *Mat4) SetRow(row int, v Vec4)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat4) String

func (m Mat4) String() string

Pretty prints the matrix

func (Mat4) Sub

func (m1 Mat4) Sub(m2 Mat4) Mat4

Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.

func (Mat4) Trace

func (m Mat4) Trace() float64

Trace is a basic operation on a square matrix that simply sums up all elements on the main diagonal (meaning all elements such that row==col).

func (Mat4) Transpose

func (m1 Mat4) Transpose() Mat4

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type Mat4x2

type Mat4x2 [8]float64

func Mat4x2FromCols

func Mat4x2FromCols(col0, col1 Vec4) Mat4x2

Mat4x2FromCols builds a new matrix from column vectors.

func Mat4x2FromRows

func Mat4x2FromRows(row0, row1, row2, row3 Vec2) Mat4x2

func (Mat4x2) Abs

func (m Mat4x2) Abs() Mat4x2

Abs returns the element-wise absolute value of this matrix

func (Mat4x2) Add

func (m1 Mat4x2) Add(m2 Mat4x2) Mat4x2

Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.

func (Mat4x2) ApproxEqual

func (m1 Mat4x2) ApproxEqual(m2 Mat4x2) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat4x2) ApproxEqualThreshold

func (m1 Mat4x2) ApproxEqualThreshold(m2 Mat4x2, threshold float64) bool

ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.

func (Mat4x2) ApproxFuncEqual

func (m1 Mat4x2) ApproxFuncEqual(m2 Mat4x2, eq func(float64, float64) bool) bool

ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.

func (Mat4x2) At

func (m Mat4x2) At(row, col int) float64

At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)

This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.

func (Mat4x2) Col

func (m Mat4x2) Col(col int) Vec4

Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.

func (Mat4x2) Cols

func (m Mat4x2) Cols() (col0, col1 Vec4)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat4x2) Index

func (m Mat4x2) Index(row, col int) int

Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.

This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.

func (Mat4x2) Mul

func (m1 Mat4x2) Mul(c float64) Mat4x2

Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.

func (Mat4x2) Mul2

func (m1 Mat4x2) Mul2(m2 Mat2) Mat4x2

Mul2 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4x2) Mul2x1

func (m1 Mat4x2) Mul2x1(m2 Vec2) Vec4

Mul2x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4x2) Mul2x3

func (m1 Mat4x2) Mul2x3(m2 Mat2x3) Mat4x3

Mul2x3 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4x2) Mul2x4

func (m1 Mat4x2) Mul2x4(m2 Mat2x4) Mat4

Mul2x4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4x2) Row

func (m Mat4x2) Row(row int) Vec2

Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.

func (Mat4x2) Rows

func (m Mat4x2) Rows() (row0, row1, row2, row3 Vec2)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat4x2) Set

func (m *Mat4x2) Set(row, col int, value float64)

Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.

This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.

func (*Mat4x2) SetCol

func (m *Mat4x2) SetCol(col int, v Vec4)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat4x2) SetRow

func (m *Mat4x2) SetRow(row int, v Vec2)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat4x2) String

func (m Mat4x2) String() string

Pretty prints the matrix

func (Mat4x2) Sub

func (m1 Mat4x2) Sub(m2 Mat4x2) Mat4x2

Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.

func (Mat4x2) Transpose

func (m1 Mat4x2) Transpose() Mat2x4

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type Mat4x3

type Mat4x3 [12]float64

func Mat4x3FromCols

func Mat4x3FromCols(col0, col1, col2 Vec4) Mat4x3

Mat4x3FromCols builds a new matrix from column vectors.

func Mat4x3FromRows

func Mat4x3FromRows(row0, row1, row2, row3 Vec3) Mat4x3

func (Mat4x3) Abs

func (m Mat4x3) Abs() Mat4x3

Abs returns the element-wise absolute value of this matrix

func (Mat4x3) Add

func (m1 Mat4x3) Add(m2 Mat4x3) Mat4x3

Add performs an element-wise addition of two matrices, this is equivalent to iterating over every element of m1 and adding the corresponding value of m2.

func (Mat4x3) ApproxEqual

func (m1 Mat4x3) ApproxEqual(m2 Mat4x3) bool

ApproxEqual performs an element-wise approximate equality test between two matrices, as if FloatEqual had been used.

func (Mat4x3) ApproxEqualThreshold

func (m1 Mat4x3) ApproxEqualThreshold(m2 Mat4x3, threshold float64) bool

ApproxEqualThreshold performs an element-wise approximate equality test between two matrices with a given epsilon threshold, as if FloatEqualThreshold had been used.

func (Mat4x3) ApproxFuncEqual

func (m1 Mat4x3) ApproxFuncEqual(m2 Mat4x3, eq func(float64, float64) bool) bool

ApproxFuncEqual performs an element-wise approximate equality test between two matrices with a given equality functions, intended to be used with FloatEqualFunc; although and comparison function may be used in practice.

func (Mat4x3) At

func (m Mat4x3) At(row, col int) float64

At returns the matrix element at the given row and column. This is equivalent to mat[col * numRow + row] where numRow is constant (E.G. for a Mat3x2 it's equal to 3)

This method is garbage-in garbage-out. For instance, on a Mat4 asking for At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.

func (Mat4x3) Col

func (m Mat4x3) Col(col int) Vec4

Col returns a vector representing the corresponding column (starting at col 0). This package makes no distinction between row and column vectors, so it will be a normal VecN for a MxN matrix.

func (Mat4x3) Cols

func (m Mat4x3) Cols() (col0, col1, col2 Vec4)

Cols decomposes a matrix into its corresponding column vectors. This is equivalent to calling mat.Col for each column.

func (Mat4x3) Index

func (m Mat4x3) Index(row, col int) int

Index returns the index of the given row and column, to be used with direct access. E.G. Index(0,0) = 0.

This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause a panic if you try to access the array with it if it's truly out of bounds.

func (Mat4x3) Mul

func (m1 Mat4x3) Mul(c float64) Mat4x3

Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating over every element of the matrix and multiply it by c.

func (Mat4x3) Mul3

func (m1 Mat4x3) Mul3(m2 Mat3) Mat4x3

Mul3 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4x3) Mul3x1

func (m1 Mat4x3) Mul3x1(m2 Vec3) Vec4

Mul3x1 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4x3) Mul3x2

func (m1 Mat4x3) Mul3x2(m2 Mat3x2) Mat4x2

Mul3x2 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4x3) Mul3x4

func (m1 Mat4x3) Mul3x4(m2 Mat3x4) Mat4

Mul3x4 performs a "matrix product" between this matrix and another of the given dimension. For any two matrices of dimensionality MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a Mat4x2.

func (Mat4x3) Row

func (m Mat4x3) Row(row int) Vec3

Row returns a vector representing the corresponding row (starting at row 0). This package makes no distinction between row and column vectors, so it will be a normal VecM for a MxN matrix.

func (Mat4x3) Rows

func (m Mat4x3) Rows() (row0, row1, row2, row3 Vec3)

Rows decomposes a matrix into its corresponding row vectors. This is equivalent to calling mat.Row for each row.

func (*Mat4x3) Set

func (m *Mat4x3) Set(row, col int, value float64)

Set sets the corresponding matrix element at the given row and column. This has a pointer receiver because it mutates the matrix.

This method is garbage-in garbage-out. For instance, on a Mat4 asking for Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.

func (*Mat4x3) SetCol

func (m *Mat4x3) SetCol(col int, v Vec4)

SetCol sets a Column within the Matrix, so it mutates the calling matrix.

func (*Mat4x3) SetRow

func (m *Mat4x3) SetRow(row int, v Vec3)

SetRow sets a Row within the Matrix, so it mutates the calling matrix.

func (Mat4x3) String

func (m Mat4x3) String() string

Pretty prints the matrix

func (Mat4x3) Sub

func (m1 Mat4x3) Sub(m2 Mat4x3) Mat4x3

Sub performs an element-wise subtraction of two matrices, this is equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.

func (Mat4x3) Transpose

func (m1 Mat4x3) Transpose() Mat3x4

Transpose produces the transpose of this matrix. For any MxN matrix the transpose is an NxM matrix with the rows swapped with the columns. For instance the transpose of the Mat3x2 is a Mat2x3 like so:

[[a b]]    [[a c e]]
[[c d]] =  [[b d f]]
[[e f]]

type MatMxN

type MatMxN struct {
	// contains filtered or unexported fields
}

MatMxN is an arbitrary mxn matrix backed by a slice of floats.

This is emphatically not recommended for hardcore n-dimensional linear algebra. For that purpose I recommend github.com/gonum/matrix or well-tested C libraries such as BLAS or LAPACK.

This is meant to complement future algorithms that may require matrices larger than 4x4, but still relatively small (e.g. Jacobeans for inverse kinematics).

It makes use of the same memory sync.Pool set that VecN does, with the same sizing rules.

MatMN will always check if the receiver is nil on any method. Meaning MathMN(nil).Add(dst,m2) should always work. Except for the Reshape function, the semantics of this is to "propogate" nils forward, so if an invalid operation occurs in a long chain of matrix operations, the overall result will be nil.

func DiagN

func DiagN(dst *MatMxN, diag *VecN) *MatMxN

DiagN creates an NxN diagonal matrix seeded by the diagonal vector diag. Meaning: for all entries, where i==j, dst.At(i,j) = diag[i]. Otherwise dst.At(i,j) = 0

This reshapes dst to the correct size, returning/grabbing from the memory pool as necessary.

func IdentN

func IdentN(dst *MatMxN, n int) *MatMxN

IdentN stores the NxN identity matrix in dst, reallocating as necessary.

func NewMatrix

func NewMatrix(m, n int) (mat *MatMxN)

NewMatrix creates a matrix backed by a new slice of size m*n

func NewMatrixFromData

func NewMatrixFromData(src []float64, m, n int) *MatMxN

NewMatrixFromData returns a matrix with data specified by the data in src

For instance, to create a 3x3 MatMN from a Mat3

m1 := mgl32.Rotate3DX(3.14159)
mat := mgl32.NewBackedMatrix(m1[:],3,3)

will create an MN matrix matching the data in the original rotation matrix. This matrix is NOT backed by the initial slice; it's a copy of the data

If m*n > cap(src), this function will panic.

func (*MatMxN) Add

func (mat *MatMxN) Add(dst *MatMxN, addend *MatMxN) *MatMxN

Add is the arithemtic + operator defined on a MatMxN.

func (*MatMxN) ApproxEqual

func (mat *MatMxN) ApproxEqual(m2 *MatMxN) bool

ApproxEqual returns whether the two vectors are approximately equal (See FloatEqual).

func (*MatMxN) ApproxEqualFunc

func (mat *MatMxN) ApproxEqualFunc(m2 *MatMxN, comp func(float64, float64) bool) bool

ApproxEqualFunc returns whether the two vectors are approximately equal, given a function which compares two scalar elements.

func (*MatMxN) ApproxEqualThreshold

func (mat *MatMxN) ApproxEqualThreshold(m2 *MatMxN, epsilon float64) bool

ApproxEqualThreshold returns whether the two vectors are approximately equal to within the given threshold given by "epsilon" (See ApproxEqualThreshold).

func (*MatMxN) At

func (mat *MatMxN) At(row, col int) float64

At returns the element at the given row and column. This is garbage in/garbage out and does no bounds checking. If the computation happens to lead to an invalid element, it will be returned; or it may panic.

func (*MatMxN) InferMatrix

func (mat *MatMxN) InferMatrix(m interface{}) (*MatMxN, error)

InferMatrix infers an MxN matrix from a constant matrix from this package. For instance, a Mat2x3 inferred with this function will work just like NewMatrixFromData(m[:],2,3) where m is the Mat2x3. This uses a type switch.

I personally recommend using NewMatrixFromData, because it avoids a potentially costly type switch. However, this is also more robust and less error prone if you change the size of your matrix somewhere.

If the value passed in is not recognized, it returns an InferMatrixError.

func (*MatMxN) Mul

func (mat *MatMxN) Mul(dst *MatMxN, c float64) *MatMxN

Mul performs a scalar multiplication between mat and some constant c, storing the result in dst. Mat and dst can be equal. If dst is not the correct size, a Reshape will occur.

func (*MatMxN) MulMxN

func (mat *MatMxN) MulMxN(dst *MatMxN, mul *MatMxN) *MatMxN

MulMxN performs matrix multiplication on MxN matrix mat and NxO matrix mul, storing the result in dst. This returns dst, or nil if the operation is not able to be performed.

If mat == dst, or mul == dst a temporary matrix will be used.

This uses the naive algorithm (though on smaller matrices, this can actually be faster; about len(mat)+len(mul) < ~100)

func (*MatMxN) MulNx1

func (mat *MatMxN) MulNx1(dst, v *VecN) *VecN

MulNx1 multiplies the matrix by a vector of size n. If mat or v is nil, this returns nil. If the number of columns in mat does not match the Size of v, this also returns nil.

Dst will be resized if it's not big enough. If dst == v; a temporary vector will be allocated and returned via the realloc callback when complete.

func (*MatMxN) NumCols

func (mat *MatMxN) NumCols() int

NumCols returns the number of columns in this matrix

func (*MatMxN) NumRowCols

func (mat *MatMxN) NumRowCols() (rows, cols int)

NumRowCols returns the number of rows and columns in this matrix as a single operation

func (*MatMxN) NumRows

func (mat *MatMxN) NumRows() int

NumRows returns the number of rows in this matrix

func (*MatMxN) Raw

func (mat *MatMxN) Raw() []float64

Raw returns the raw slice backing this matrix

func (*MatMxN) Reshape

func (mat *MatMxN) Reshape(m, n int) *MatMxN

Reshape reshapes the matrix to the desired dimensions. If the overall size of the new matrix (m*n) is bigger than the current size, the underlying slice will be grown, sending the current slice to the memory pool and grabbing a bigger one if necessary

If the caller is a nil pointer, the return value will be a new matrix, as if NewMatrix(m,n) had been called. Otherwise it's simply the caller.

func (*MatMxN) Set

func (mat *MatMxN) Set(row, col int, val float64)

Set sets the element at the given row and column. This is garbage in/garbage out and does no bounds checking. If the computation happens to lead to an invalid element, it will be set; or it may panic.

func (*MatMxN) Sub

func (mat *MatMxN) Sub(dst *MatMxN, subtrahend *MatMxN) *MatMxN

Sub is the arithemtic - operator defined on a MatMxN.

func (*MatMxN) Trace

func (mat *MatMxN) Trace() float64

Trace returns the trace of a square matrix (sum of all diagonal elements). If the matrix is nil, or not square, the result will be NaN.

func (*MatMxN) Transpose

func (mat *MatMxN) Transpose(dst *MatMxN) (t *MatMxN)

Transpose takes the transpose of mat and puts it in dst.

If dst is not of the correct dimensions, it will be Reshaped, if dst and mat are the same, a temporary matrix of the correct size will be allocated; these resources will be released via the memory pool.

This should be improved in the future.

func (*MatMxN) Zero

func (mat *MatMxN) Zero(m, n int)

Zero reshapes the matrix to m by n and zeroes out all elements.

type NilMatrixError

type NilMatrixError struct{}

NilMatrixError is returned when an operand to a function was unexpectedly 'nil'.

func (NilMatrixError) Error

func (me NilMatrixError) Error() string

type Quat

type Quat struct {
	W float64
	V Vec3
}

Quat represents a Quaternion, which is an extension of the imaginary numbers; there's all sorts of interesting theory behind it. In 3D graphics we mostly use it as a cheap way of representing rotation since quaternions are cheaper to multiply by, and easier to interpolate than matrices.

A Quaternion has two parts: W, the so-called scalar component, and "V", the vector component. The vector component is considered to be the part in 3D space, while W (loosely interpreted) is its 4D coordinate.

func AnglesToQuat

func AnglesToQuat(angle1, angle2, angle3 float64, order RotationOrder) Quat

AnglesToQuat performs a rotation in the specified order. If the order is not a valid RotationOrder, this function will panic

The rotation "order" is more of an axis descriptor. For instance XZX would tell the function to interpret angle1 as a rotation about the X axis, angle2 about the Z axis, and angle3 about the X axis again.

Based off the code for the Matlab function "angle2quat", though this implementation only supports 3 single angles as opposed to multiple angles.

func Mat4ToQuat

func Mat4ToQuat(m Mat4) Quat

Mat4ToQuat converts a pure rotation matrix into a quaternion

func QuatBetweenVectors

func QuatBetweenVectors(start, dest Vec3) Quat

QuatBetweenVectors calculates the rotation between two vectors

func QuatIdent

func QuatIdent() Quat

QuatIdent returns the quaternion identity: W=1; V=(0,0,0).

As with all identities, multiplying any quaternion by this will yield the same quaternion you started with.

func QuatLerp

func QuatLerp(q1, q2 Quat, amount float64) Quat

QuatLerp is a *L*inear Int*erp*olation between two Quaternions, cheap and simple.

Not excessively useful, but uses can be found.

func QuatLookAtV

func QuatLookAtV(eye, center, up Vec3) Quat

QuatLookAtV creates a rotation from an eye vector to a center vector

It assumes the front of the rotated object at Z- and up at Y+

func QuatNlerp

func QuatNlerp(q1, q2 Quat, amount float64) Quat

QuatNlerp is a *Normalized* *L*inear Int*erp*olation between two Quaternions. Cheaper than Slerp and usually just as good. This is literally Lerp with Normalize() called on it.

Unlike Slerp, constant velocity isn't maintained, but it's much faster and Nlerp(q1,q2) and Nlerp(q2,q1) return the same path. You should probably use this more often unless you're suffering from choppiness due to the non-constant velocity problem.

func QuatRotate

func QuatRotate(angle float64, axis Vec3) Quat

QuatRotate creates an angle from an axis and an angle relative to that axis.

This is cheaper than HomogRotate3D.

func QuatSlerp

func QuatSlerp(q1, q2 Quat, amount float64) Quat

QuatSlerp is *S*pherical *L*inear Int*erp*olation, a method of interpolating between two quaternions. This always takes the straightest and shortest path on the sphere between the two quaternions, and maintains constant velocity.

However, it's expensive and QuatSlerp(q1,q2) is not the same as QuatSlerp(q2,q1)

func (Quat) Add

func (q1 Quat) Add(q2 Quat) Quat

Add adds two quaternions. It's no more complicated than adding their W and V components.

func (Quat) ApproxEqual

func (q1 Quat) ApproxEqual(q2 Quat) bool

ApproxEqual returns whether the quaternions are approximately equal, as if FloatEqual was called on each matching element

func (Quat) ApproxEqualFunc

func (q1 Quat) ApproxEqualFunc(q2 Quat, f func(float64, float64) bool) bool

ApproxEqualFunc returns whether the quaternions are approximately equal using the given comparison function, as if the function had been called on each individual element

func (Quat) ApproxEqualThreshold

func (q1 Quat) ApproxEqualThreshold(q2 Quat, epsilon float64) bool

ApproxEqualThreshold returns whether the quaternions are approximately equal with a given tolerence, as if FloatEqualThreshold was called on each matching element with the given epsilon

func (Quat) Conjugate

func (q1 Quat) Conjugate() Quat

Conjugate returns the conjugate of a quaternion. Equivalent to Quat{q1.W, q1.V.Mul(-1)}.

func (Quat) Dot

func (q1 Quat) Dot(q2 Quat) float64

Dot product between two quaternions, equivalent to if this was a Vec4.

func (Quat) Inverse

func (q1 Quat) Inverse() Quat

Inverse of a quaternion. The inverse is equivalent to the conjugate divided by the square of the length.

This method computes the square norm by directly adding the sum of the squares of all terms instead of actually squaring q1.Len(), both for performance and precision.

func (Quat) Len

func (q1 Quat) Len() float64

Len gives the Length of the quaternion, also known as its Norm. This is the same thing as the Len of a Vec4.

func (Quat) Mat4

func (q1 Quat) Mat4() Mat4

Mat4 returns the homogeneous 3D rotation matrix corresponding to the quaternion.

func (Quat) Mul

func (q1 Quat) Mul(q2 Quat) Quat

Mul multiplies two quaternions. This can be seen as a rotation. Note that Multiplication is NOT commutative, meaning q1.Mul(q2) does not necessarily equal q2.Mul(q1).

func (Quat) Norm

func (q1 Quat) Norm() float64

Norm is an alias for Len() since both are very common terms.

func (Quat) Normalize

func (q1 Quat) Normalize() Quat

Normalize the quaternion, returning its versor (unit quaternion).

This is the same as normalizing it as a Vec4.

func (Quat) OrientationEqual

func (q1 Quat) OrientationEqual(q2 Quat) bool

OrientationEqual returns whether the quaternions represents the same orientation

Different values can represent the same orientation (q == -q) because quaternions avoid singularities and discontinuities involved with rotation in 3 dimensions by adding extra dimensions

func (Quat) OrientationEqualThreshold

func (q1 Quat) OrientationEqualThreshold(q2 Quat, epsilon float64) bool

OrientationEqualThreshold returns whether the quaternions represents the same orientation with a given tolerence

func (Quat) Rotate

func (q1 Quat) Rotate(v Vec3) Vec3

Rotate a vector by the rotation this quaternion represents. This will result in a 3D vector. Strictly speaking, this is equivalent to q1.v.q* where the "."" is quaternion multiplication and v is interpreted as a quaternion with W 0 and V v. In code: q1.Mul(Quat{0,v}).Mul(q1.Conjugate()), and then retrieving the imaginary (vector) part.

In practice, we hand-compute this in the general case and simplify to save a few operations.

func (Quat) Scale

func (q1 Quat) Scale(c float64) Quat

Scale every element of the quaternion by some constant factor.

func (Quat) Sub

func (q1 Quat) Sub(q2 Quat) Quat

Sub subtracts two quaternions. It's no more complicated than subtracting their W and V components.

func (Quat) X

func (q Quat) X() float64

X is a convenient alias for q.V[0]

func (Quat) Y

func (q Quat) Y() float64

Y is a convenient alias for q.V[1]

func (Quat) Z

func (q Quat) Z() float64

Z is a convenient alias for q.V[2]

type RectangularMatrixError

type RectangularMatrixError struct{}

RectangularMatrixError is returned when a rectangular matrix was expected but not given.

func (RectangularMatrixError) Error

func (mse RectangularMatrixError) Error() string

type RotationOrder

type RotationOrder int

RotationOrder is the order in which rotations will be transformed for the purposes of AnglesToQuat.

const (
	XYX RotationOrder = iota
	XYZ
	XZX
	XZY
	YXY
	YXZ
	YZY
	YZX
	ZYZ
	ZYX
	ZXZ
	ZXY
)

The RotationOrder constants represent a series of rotations along the given axes for the use of AnglesToQuat.

type Vec2

type Vec2 f64.Vec2

func BezierCurve2D

func BezierCurve2D(t float64, cPoints []Vec2) Vec2

BezierCurve2D returns the point at point t along an n-control point Bezier curve

t must be in the range 0.0 and 1.0 or this function will panic. Consider [0.0,1.0] to be similar to a percentage, 0.0 is first control point, and the point at 1.0 is the last control point. Any point in between is how far along the path you are between 0 and 1.

This function is not sensitive to the coordinate system of the control points. It will correctly interpolate regardless of whether they're in screen coords, gl coords, or something else entirely

func BezierSplineInterpolate2D

func BezierSplineInterpolate2D(t float64, ranges [][2]float64, cPoints [][]Vec2) Vec2

BezierSplineInterpolate2D does interpolation over a spline of several bezier curves. Each bezier curve must have a finite range, though the spline may be disjoint. The bezier curves are not required to be in any particular order.

If t is out of the range of all given curves, this function will panic

func Circle

func Circle(radiusX, radiusY float64, numSlices int) []Vec2

Circle generates a circle centered at (0,0) with a given radius. The radii are assumed to be in GL's coordinate sizing.

Technically this draws an ellipse with two axes that match with the X and Y axes, the reason it has a radiusX and radiusY is because GL's coordinate system is proportional to screen width and screen height. So if you have a non-square viewport, a single radius will appear to "squash" the circle in one direction (usually the Y direction), so the X and Y radius allow for a circle to be made. A good way to get the correct radii is with mathgl.ScreenToGLCoords(radius, radius, screenWidth, screenHeight) which will get you the correct proportional GL coords.

The numSlices argument specifies how many triangles you want your circle divided into, setting this number to too low a value may cause problem (and too high will cause it to take a lot of memory and time to compute without much gain in resolution).

This uses discrete triangles, not a triangle fan

func CubicBezierCurve2D

func CubicBezierCurve2D(t float64, cPoint1, cPoint2, cPoint3, cPoint4 Vec2) Vec2

CubicBezierCurve2D interpolates the point t on the given bezier curve.

func MakeBezierCurve2D

func MakeBezierCurve2D(numPoints int, cPoints []Vec2) (line []Vec2)

MakeBezierCurve2D generates a bezier curve with controlPoints cPoints. The numPoints argument determines how many "samples" it makes along the line. For instance, a call to this with numPoints 2 will have exactly two points: the start and end points For any points above 2 it will divide it into numPoints-1 chunks (which means it will generate numPoints-2 vertices other than the beginning and end). So for 3 points it will divide it in half, 4 points into thirds, and so on.

This is likely to get rather expensive for anything over perhaps a cubic curve.

func QuadraticBezierCurve2D

func QuadraticBezierCurve2D(t float64, cPoint1, cPoint2, cPoint3 Vec2) Vec2

QuadraticBezierCurve2D interpolates the point t on the given bezier curve.

func Rect

func Rect(width, height float64) []Vec2

Rect generates a 2-triangle rectangle for use with GL_TRIANGLES. The width and height should use GL's proportions (that is, where a width of 1.0 is equivalent to half of the width of the render target); however, the y-coordinates grow downwards, not upwards. That is, it assumes you want the origin of the rectangle with the top-left corner at (0.0,0.0).

Keep in mind that GL's coordinate system is proportional, so width=height will not result in a square unless your viewport is square. If you want to maintain proportionality regardless of screen size, use the results of w,h := ScreenToGLCoordsf(absoluteWidth, absoluteHeight, screenWidth, screenHeight); w,h=w+1,h-1 in the call to this function. (The w+1,h-1 step maps the coordinates to start at 0.0 rather than -1.0)

func (Vec2) Add

func (v1 Vec2) Add(v2 Vec2) Vec2

Add performs element-wise addition between two vectors. It is equivalent to iterating over every element of v1 and adding the corresponding element of v2 to it.

func (Vec2) ApproxEqual

func (v1 Vec2) ApproxEqual(v2 Vec2) bool

ApproxEqual takes in a vector and does an element-wise approximate float comparison as if FloatEqual had been used

func (Vec2) ApproxEqualThreshold

func (v1 Vec2) ApproxEqualThreshold(v2 Vec2, threshold float64) bool

ApproxEqualThreshold takes in a threshold for comparing two floats, and uses it to do an element-wise comparison of the vector to another.

func (Vec2) ApproxFuncEqual

func (v1 Vec2) ApproxFuncEqual(v2 Vec2, eq func(float64, float64) bool) bool

ApproxFuncEqual takes in a func that compares two floats, and uses it to do an element-wise comparison of the vector to another. This is intended to be used with FloatEqualFunc

func (Vec2) Dot

func (v1 Vec2) Dot(v2 Vec2) float64

Dot returns the dot product of this vector with another. There are multiple ways to describe this value. One is the multiplication of their lengths and cos(theta) where theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).

The other (and what is actually done) is the sum of the element-wise multiplication of all elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.

This means that the dot product of a vector and itself is the square of its Len (within the bounds of floating points error).

The dot product is roughly a measure of how closely two vectors are to pointing in the same direction. If both vectors are normalized, the value will be -1 for opposite pointing, one for same pointing, and 0 for perpendicular vectors.

func (Vec2) Elem

func (v Vec2) Elem() (x, y float64)

Elem extracts the elements of the vector for direct value assignment.

func (Vec2) Len

func (v1 Vec2) Len() float64

Len returns the vector's length. Note that this is NOT the dimension of the vector (len(v)), but the mathematical length. This is equivalent to the square root of the sum of the squares of all elements. E.G. for a Vec2 it's math.Hypot(v[0], v[1]).

func (Vec2) LenSqr

func (v1 Vec2) LenSqr() float64

LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.

func (Vec2) Mul

func (v1 Vec2) Mul(c float64) Vec2

Mul performs a scalar multiplication between the vector and some constant value c. This is equivalent to iterating over every vector element and multiplying by c.

func (Vec2) Normalize

func (v1 Vec2) Normalize() Vec2

Normalize normalizes the vector. Normalization is (1/|v|)*v, making this equivalent to v.Scale(1/v.Len()). If the len is 0.0, this function will return an infinite value for all elements due to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).

Normalization makes a vector's Len become 1.0 (within the margin of floating point error), while maintaining its directionality.

(Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )

func (Vec2) OuterProd2

func (v1 Vec2) OuterProd2(v2 Vec2) Mat2

OuterProd2 does the vector outer product of two vectors. The outer product produces an 2x2 matrix. E.G. a Vec2 * Vec2 = Mat2.

The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.

The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.

func (Vec2) OuterProd3

func (v1 Vec2) OuterProd3(v2 Vec3) Mat2x3

OuterProd3 does the vector outer product of two vectors. The outer product produces an 2x3 matrix. E.G. a Vec2 * Vec3 = Mat2x3.

The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.

The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.

func (Vec2) OuterProd4

func (v1 Vec2) OuterProd4(v2 Vec4) Mat2x4

OuterProd4 does the vector outer product of two vectors. The outer product produces an 2x4 matrix. E.G. a Vec2 * Vec4 = Mat2x4.

The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.

The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.

func (Vec2) Sub

func (v1 Vec2) Sub(v2 Vec2) Vec2

Sub performs element-wise subtraction between two vectors. It is equivalent to iterating over every element of v1 and subtracting the corresponding element of v2 from it.

func (Vec2) Vec3

func (v Vec2) Vec3(z float64) Vec3

Vec3 constructs a 3-dimensional vector by appending the given coordinates.

func (Vec2) Vec4

func (v Vec2) Vec4(z, w float64) Vec4

Vec4 constructs a 4-dimensional vector by appending the given coordinates.

func (Vec2) X

func (v Vec2) X() float64

X is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.

func (Vec2) Y

func (v Vec2) Y() float64

Y is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.

type Vec3

type Vec3 f64.Vec3

func BezierCurve3D

func BezierCurve3D(t float64, cPoints []Vec3) Vec3

BezierCurve3D same as the 2D version, except the line is in 3D space

func BezierSplineInterpolate3D

func BezierSplineInterpolate3D(t float64, ranges [][2]float64, cPoints [][]Vec3) Vec3

BezierSplineInterpolate3D does interpolation over a spline of several bezier curves. Each bezier curve must have a finite range, though the spline may be disjoint. The bezier curves are not required to be in any particular order.

If t is out of the range of all given curves, this function will panic

func BezierSurface

func BezierSurface(u, v float64, cPoints [][]Vec3) Vec3

BezierSurface creates a 2-dimensional Bezier surface of arbitrary degree in 3D Space Like the curve functions, if u or v are not in the range [0.0,1.0] the function will panic, use Clamp[f|d] to ensure it is correct.

The control point matrix must not be jagged, or this will end up panicking from an index out of bounds exception

func CubicBezierCurve3D

func CubicBezierCurve3D(t float64, cPoint1, cPoint2, cPoint3, cPoint4 Vec3) Vec3

CubicBezierCurve3D interpolates the point t on the given bezier curve.

func CylindricalToCartesian

func CylindricalToCartesian(rho, phi, z float64) Vec3

CylindricalToCartesian converts cylindrical coordinates with radial distance r, azimuth phi, and height z to cartesian coordinates (x,y,z)

Angles are in radians.

func MakeBezierCurve3D

func MakeBezierCurve3D(numPoints int, cPoints []Vec3) (line []Vec3)

MakeBezierCurve3D same as the 2D version, except with the line in 3D space.

func Project

func Project(obj Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (win Vec3)

Project transforms a set of coordinates from object space (in obj) to window coordinates (with depth).

Window coordinates are continuous, not discrete (well, as continuous as an IEEE Floating Point can be), so you won't get exact pixel locations without rounding or similar

func QuadraticBezierCurve3D

func QuadraticBezierCurve3D(t float64, cPoint1, cPoint2, cPoint3 Vec3) Vec3

QuadraticBezierCurve3D interpolates the point t on the given bezier curve.

func SphericalToCartesian

func SphericalToCartesian(r, theta, phi float64) Vec3

SphericalToCartesian converts spherical coordinates with radius r, inclination theta, and azimuth phi to cartesian coordinates (x,y,z).

Angles are in radians.

func TransformCoordinate

func TransformCoordinate(v Vec3, m Mat4) Vec3

TransformCoordinate multiplies a 3D vector by a transformation given by the homogeneous 4D matrix m, applying any translation. If this transformation is non-affine, it will project this vector onto the plane w=1 before returning the result.

This is similar to saying you're transforming and projecting a point.

This is effectively equivalent to the GLSL code

vec4 r = (m * vec4(v,1.));
r = r/r.w;
vec3 newV = r.xyz;

func TransformNormal

func TransformNormal(v Vec3, m Mat4) Vec3

TransformNormal multiplies a 3D vector by a transformation given by the homogeneous 4D matrix m, NOT applying any translations.

This is similar to saying you're applying a transformation to a direction or normal. Rotation still applies (as does scaling), but translating a direction or normal is meaningless.

This is effectively equivalent to the GLSL code

vec4 r = (m * vec4(v,0.));
vec3 newV = r.xyz

func UnProject

func UnProject(win Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (obj Vec3, err error)

UnProject transforms a set of window coordinates to object space. If your MVP (projection.Mul(modelview) matrix is not invertible, this will return an error.

Note that the projection may not be perfect if you use strict pixel locations rather than the exact values given by Projectf. (It's still unlikely to be perfect due to precision errors, but it will be closer)

func (Vec3) Add

func (v1 Vec3) Add(v2 Vec3) Vec3

Add performs element-wise addition between two vectors. It is equivalent to iterating over every element of v1 and adding the corresponding element of v2 to it.

func (Vec3) ApproxEqual

func (v1 Vec3) ApproxEqual(v2 Vec3) bool

ApproxEqual takes in a vector and does an element-wise approximate float comparison as if FloatEqual had been used

func (Vec3) ApproxEqualThreshold

func (v1 Vec3) ApproxEqualThreshold(v2 Vec3, threshold float64) bool

ApproxEqualThreshold takes in a threshold for comparing two floats, and uses it to do an element-wise comparison of the vector to another.

func (Vec3) ApproxFuncEqual

func (v1 Vec3) ApproxFuncEqual(v2 Vec3, eq func(float64, float64) bool) bool

ApproxFuncEqual takes in a func that compares two floats, and uses it to do an element-wise comparison of the vector to another. This is intended to be used with FloatEqualFunc

func (Vec3) Cross

func (v1 Vec3) Cross(v2 Vec3) Vec3

Cross is the vector cross product. This operation is only defined on 3D vectors. It is equivalent to Vec3{v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}. Another interpretation is that it's the vector whose magnitude is |v1||v2|sin(theta) where theta is the angle between v1 and v2.

The cross product is most often used for finding surface normals. The cross product of vectors will generate a vector that is perpendicular to the plane they form.

Technically, a generalized cross product exists as an "(N-1)ary" operation (that is, the 4D cross product requires 3 4D vectors). But the binary 3D (and 7D) cross product is the most important. It can be considered the area of a parallelogram with sides v1 and v2.

Like the dot product, the cross product is roughly a measure of directionality. Two normalized perpendicular vectors will return a vector with a magnitude of 1.0 or -1.0 and two parallel vectors will return a vector with magnitude 0.0. The cross product is "anticommutative" meaning v1.Cross(v2) = -v2.Cross(v1), this property can be useful to know when finding normals, as taking the wrong cross product can lead to the opposite normal of the one you want.

func (Vec3) Dot

func (v1 Vec3) Dot(v2 Vec3) float64

Dot returns the dot product of this vector with another. There are multiple ways to describe this value. One is the multiplication of their lengths and cos(theta) where theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).

The other (and what is actually done) is the sum of the element-wise multiplication of all elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.

This means that the dot product of a vector and itself is the square of its Len (within the bounds of floating points error).

The dot product is roughly a measure of how closely two vectors are to pointing in the same direction. If both vectors are normalized, the value will be -1 for opposite pointing, one for same pointing, and 0 for perpendicular vectors.

func (Vec3) Elem

func (v Vec3) Elem() (x, y, z float64)

Elem extracts the elements of the vector for direct value assignment.

func (Vec3) Len

func (v1 Vec3) Len() float64

Len returns the vector's length. Note that this is NOT the dimension of the vector (len(v)), but the mathematical length. This is equivalent to the square root of the sum of the squares of all elements. E.G. for a Vec2 it's math.Hypot(v[0], v[1]).

func (Vec3) LenSqr

func (v1 Vec3) LenSqr() float64

LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.

func (Vec3) Mul

func (v1 Vec3) Mul(c float64) Vec3

Mul performs a scalar multiplication between the vector and some constant value c. This is equivalent to iterating over every vector element and multiplying by c.

func (Vec3) Normalize

func (v1 Vec3) Normalize() Vec3

Normalize normalizes the vector. Normalization is (1/|v|)*v, making this equivalent to v.Scale(1/v.Len()). If the len is 0.0, this function will return an infinite value for all elements due to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).

Normalization makes a vector's Len become 1.0 (within the margin of floating point error), while maintaining its directionality.

(Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )

func (Vec3) OuterProd2

func (v1 Vec3) OuterProd2(v2 Vec2) Mat3x2

OuterProd2 does the vector outer product of two vectors. The outer product produces an 3x2 matrix. E.G. a Vec3 * Vec2 = Mat3x2.

The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.

The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.

func (Vec3) OuterProd3

func (v1 Vec3) OuterProd3(v2 Vec3) Mat3

OuterProd3 does the vector outer product of two vectors. The outer product produces an 3x3 matrix. E.G. a Vec3 * Vec3 = Mat3.

The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.

The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.

func (Vec3) OuterProd4

func (v1 Vec3) OuterProd4(v2 Vec4) Mat3x4

OuterProd4 does the vector outer product of two vectors. The outer product produces an 3x4 matrix. E.G. a Vec3 * Vec4 = Mat3x4.

The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.

The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.

func (Vec3) Sub

func (v1 Vec3) Sub(v2 Vec3) Vec3

Sub performs element-wise subtraction between two vectors. It is equivalent to iterating over every element of v1 and subtracting the corresponding element of v2 from it.

func (Vec3) Vec2

func (v Vec3) Vec2() Vec2

Vec2 constructs a 2-dimensional vector by discarding coordinates.

func (Vec3) Vec4

func (v Vec3) Vec4(w float64) Vec4

Vec4 constructs a 4-dimensional vector by appending the given coordinates.

func (Vec3) X

func (v Vec3) X() float64

X is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.

func (Vec3) Y

func (v Vec3) Y() float64

Y is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.

func (Vec3) Z

func (v Vec3) Z() float64

Z is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.

type Vec4

type Vec4 f64.Vec4

func (Vec4) Add

func (v1 Vec4) Add(v2 Vec4) Vec4

Add performs element-wise addition between two vectors. It is equivalent to iterating over every element of v1 and adding the corresponding element of v2 to it.

func (Vec4) ApproxEqual

func (v1 Vec4) ApproxEqual(v2 Vec4) bool

ApproxEqual takes in a vector and does an element-wise approximate float comparison as if FloatEqual had been used

func (Vec4) ApproxEqualThreshold

func (v1 Vec4) ApproxEqualThreshold(v2 Vec4, threshold float64) bool

ApproxEqualThreshold takes in a threshold for comparing two floats, and uses it to do an element-wise comparison of the vector to another.

func (Vec4) ApproxFuncEqual

func (v1 Vec4) ApproxFuncEqual(v2 Vec4, eq func(float64, float64) bool) bool

ApproxFuncEqual takes in a func that compares two floats, and uses it to do an element-wise comparison of the vector to another. This is intended to be used with FloatEqualFunc

func (Vec4) Dot

func (v1 Vec4) Dot(v2 Vec4) float64

Dot returns the dot product of this vector with another. There are multiple ways to describe this value. One is the multiplication of their lengths and cos(theta) where theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).

The other (and what is actually done) is the sum of the element-wise multiplication of all elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.

This means that the dot product of a vector and itself is the square of its Len (within the bounds of floating points error).

The dot product is roughly a measure of how closely two vectors are to pointing in the same direction. If both vectors are normalized, the value will be -1 for opposite pointing, one for same pointing, and 0 for perpendicular vectors.

func (Vec4) Elem

func (v Vec4) Elem() (x, y, z, w float64)

Elem extracts the elements of the vector for direct value assignment.

func (Vec4) Len

func (v1 Vec4) Len() float64

Len returns the vector's length. Note that this is NOT the dimension of the vector (len(v)), but the mathematical length. This is equivalent to the square root of the sum of the squares of all elements. E.G. for a Vec2 it's math.Hypot(v[0], v[1]).

func (Vec4) LenSqr

func (v1 Vec4) LenSqr() float64

LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.

func (Vec4) Mul

func (v1 Vec4) Mul(c float64) Vec4

Mul performs a scalar multiplication between the vector and some constant value c. This is equivalent to iterating over every vector element and multiplying by c.

func (Vec4) Normalize

func (v1 Vec4) Normalize() Vec4

Normalize normalizes the vector. Normalization is (1/|v|)*v, making this equivalent to v.Scale(1/v.Len()). If the len is 0.0, this function will return an infinite value for all elements due to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).

Normalization makes a vector's Len become 1.0 (within the margin of floating point error), while maintaining its directionality.

(Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )

func (Vec4) OuterProd2

func (v1 Vec4) OuterProd2(v2 Vec2) Mat4x2

OuterProd2 does the vector outer product of two vectors. The outer product produces an 4x2 matrix. E.G. a Vec4 * Vec2 = Mat4x2.

The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.

The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.

func (Vec4) OuterProd3

func (v1 Vec4) OuterProd3(v2 Vec3) Mat4x3

OuterProd3 does the vector outer product of two vectors. The outer product produces an 4x3 matrix. E.G. a Vec4 * Vec3 = Mat4x3.

The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.

The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.

func (Vec4) OuterProd4

func (v1 Vec4) OuterProd4(v2 Vec4) Mat4

OuterProd4 does the vector outer product of two vectors. The outer product produces an 4x4 matrix. E.G. a Vec4 * Vec4 = Mat4.

The outer product can be thought of as the "opposite" of the Dot product. The Dot product treats both vectors like matrices oriented such that the left one has N columns and the right has N rows. So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.

The outer product orients it so they're facing "outward": Vec2*Vec3 = Mat2x1*Mat1x3 = Mat2x3.

func (Vec4) Quat

func (v Vec4) Quat() Quat

Quat reinterprets this vector as a quaternion, with the individual elements staying the same.

func (Vec4) Sub

func (v1 Vec4) Sub(v2 Vec4) Vec4

Sub performs element-wise subtraction between two vectors. It is equivalent to iterating over every element of v1 and subtracting the corresponding element of v2 from it.

func (Vec4) Vec2

func (v Vec4) Vec2() Vec2

Vec2 constructs a 2-dimensional vector by discarding coordinates.

func (Vec4) Vec3

func (v Vec4) Vec3() Vec3

Vec3 constructs a 3-dimensional vector by discarding coordinates.

func (Vec4) W

func (v Vec4) W() float64

W is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.

func (Vec4) X

func (v Vec4) X() float64

X is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.

func (Vec4) Y

func (v Vec4) Y() float64

Y is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.

func (Vec4) Z

func (v Vec4) Z() float64

Z is an element access func, it is equivalent to v[n] where n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks show that this is more or less as fast as direct acces, probably due to inlining, so use v[0] or v.X() depending on personal preference.

type VecN

type VecN struct {
	// contains filtered or unexported fields
}

VecN represents a vector of N elements backed by a slice.

As with MatMxN, this is not for hardcore linear algebra with large dimensions. Use github.com/gonum/matrix or something like BLAS/LAPACK for that. This is for corner cases in 3D math where you require something a little bigger that 4D, but still relatively small.

This VecN uses several sync.Pool objects as a memory pool. The rule is that for any sized vector, the backing slice has CAPACITY (not length) of 2^p where p is Ceil(log_2(N)) -- or in other words, rounding up the base-2 log of the size of the vector. E.G. a VecN of size 17 will have a backing slice of Cap 32.

func NewVecN

func NewVecN(n int) *VecN

NewVecN creates a new vector with a backing slice of 2^p where p = Ceil(log_2(n))

func NewVecNFromData

func NewVecNFromData(initial []float64) *VecN

NewVecNFromData creates a new vector with a backing slice filled with the contents of initial. It is NOT backed by initial, but rather a slice with cap 2^p where p is Ceil(log_2(len(initial))), with the data from initial copied into it.

func (*VecN) Add

func (vn *VecN) Add(dst *VecN, subtrahend *VecN) *VecN

Add adds vn and addend, storing the result in dst. If dst does not have sufficient size it will be resized Dst may be one of the other arguments. If dst is nil, it will be allocated. The value returned is dst, for easier method chaining

If vn and addend are not the same size, this function will add min(vn.Size(), addend.Size()) elements.

func (*VecN) ApproxEqual

func (vn *VecN) ApproxEqual(vn2 *VecN) bool

ApproxEqual returns whether the two vectors are approximately equal (See FloatEqual).

func (*VecN) ApproxEqualFunc

func (vn *VecN) ApproxEqualFunc(vn2 *VecN, comp func(float64, float64) bool) bool

ApproxEqualFunc returns whether the two vectors are approximately equal, given a function which compares two scalar elements.

func (*VecN) ApproxEqualThreshold

func (vn *VecN) ApproxEqualThreshold(vn2 *VecN, epsilon float64) bool

ApproxEqualThreshold returns whether the two vectors are approximately equal to within the given threshold given by "epsilon" (See ApproxEqualThreshold).

func (*VecN) Cap

func (vn *VecN) Cap() int

Cap Returns the cap of the vector's underlying slice.

func (*VecN) Cross

func (vn *VecN) Cross(dst *VecN, other *VecN) *VecN

Cross takes the binary cross product of vn and other, and stores it in dst. If either vn or other are not of size 3 this function will panic

If dst is not of sufficient size, or is nil, a new slice is allocated. Dst is permitted to be one of the other arguments

func (*VecN) Dot

func (vn *VecN) Dot(other *VecN) float64

Dot computes the dot product of two VecNs, if the two vectors are not of the same length -- this will return NaN.

func (VecN) Get

func (vn VecN) Get(i int) float64

Get the element at index i from the vector. This does not bounds check, and will panic if i is out of range.

func (*VecN) Len

func (vn *VecN) Len() float64

Len computes the vector length (also called the Norm) of the vector. Equivalent to math.Sqrt(vn.Dot(vn)) with the appropriate type conversions.

If vn is nil, this returns NaN

func (*VecN) LenSqr

func (vn *VecN) LenSqr() float64

LenSqr returns the vector's square length. This is equivalent to the sum of the squares of all elements.

func (*VecN) Mul

func (vn *VecN) Mul(dst *VecN, c float64) *VecN

Mul multiplies the vector by some scalar value and stores the result in dst, which will be returned. Dst will be appropriately resized to the size of vn.

The destination can be vn itself and nothing will go wrong.

func (*VecN) Normalize

func (vn *VecN) Normalize(dst *VecN) *VecN

Normalize the vector and stores the result in dst, which will be returned. Dst will be appropraitely resized to the size of vn.

The destination can be vn itself and nothing will go wrong.

This is equivalent to vn.Mul(dst, 1/vn.Len())

func (*VecN) OuterProd

func (vn *VecN) OuterProd(dst *MatMxN, v2 *VecN) *MatMxN

OuterProd performs the vector outer product between vn and v2. The outer product is like a "reverse" dot product. Where the dot product aligns both vectors with the "sized" part facing "inward" (Vec3*Vec3=Mat1x3*Mat3x1=Mat1x1=Scalar). The outer product multiplied them with it facing "outward" (Vec3*Vec3=Mat3x1*Mat1x3=Mat3x3).

The matrix dst will be Reshaped to the correct size, if vn or v2 are nil, this returns nil.

func (VecN) Raw

func (vn VecN) Raw() []float64

Raw returns the raw slice backing the VecN

This may be sent back to the memory pool at any time and you aren't advised to rely on this value

func (*VecN) Resize

func (vn *VecN) Resize(n int) *VecN

Resize the underlying slice to the desired amount, reallocating or retrieving from the pool if necessary. The values after a Resize cannot be expected to be related to the values before a Resize.

If the caller is a nil pointer, this returns a value as if NewVecN(n) had been called, otherwise it simply returns the caller.

func (*VecN) Set

func (vn *VecN) Set(i int, val float64)

Set the element at index i to val.

func (*VecN) SetBackingSlice

func (vn *VecN) SetBackingSlice(newSlice []float64)

SetBackingSlice sets the vector's backing slice to the given new one.

func (*VecN) Size

func (vn *VecN) Size() int

Size returns the len of the vector's underlying slice. This is not titled Len because it conflicts the package's convention of calling the Norm the Len.

func (*VecN) Sub

func (vn *VecN) Sub(dst *VecN, addend *VecN) *VecN

Sub subtracts addend from vn, storing the result in dst. If dst does not have sufficient size it will be resized Dst may be one of the other arguments. If dst is nil, it will be allocated. The value returned is dst, for easier method chaining

If vn and addend are not the same size, this function will add min(vn.Size(), addend.Size()) elements.

func (*VecN) Vec2

func (vn *VecN) Vec2() Vec2

Vec2 constructs a 2-dimensional vector by discarding coordinates.

func (*VecN) Vec3

func (vn *VecN) Vec3() Vec3

Vec3 constructs a 3-dimensional vector by discarding coordinates.

func (*VecN) Vec4

func (vn *VecN) Vec4() Vec4

Vec4 constructs a 4-dimensional vector by discarding coordinates.

func (*VecN) Zero

func (vn *VecN) Zero(n int)

Zero sets the vector's size to n and zeroes out the vector. If n is bigger than the vector's size, it will realloc.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL