glm

package module
v0.0.0-...-9c08f4d Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2017 License: BSD-3-Clause Imports: 5 Imported by: 7

README

glm Build Status Tests Coverage GoDoc

VENDOR THIS IF YOU'RE USING IT. The API is not quite stable yet. Specialy geo*.

More efficient version then go-gl math lib and better name (mgl32 is too long to type).

The problem with go-gl implementation is that every operation returns a new matrix/quaternion/vector, you can't reuse memory. Benchmark reflect that this causes quite the slowdown. see issue 29. This library is a fork of mgl32 but a ton of methods we're added in order to allow the user to have more control over the memory. First, most methods take pointer argument and second there is more then 1 method to do the same operation.

This library uses lux math (native float32 math) instead of the standard library math.

In the future, when we have more knowledge of plan9 we intend to insert some SIMD operations for the more hardcore stuff.

func (m1 *Mat2) Add(m2 *Mat2) *Mat2 {
	return &Mat2{m1[0] + m2[0], m1[1] + m2[1], m1[2] + m2[2], m1[3] + m2[3]}
}

func (m1 *Mat2) SumOf(m2, m3 *Mat2) *Mat2 {
	m1[0] = m2[0] + m3[0]
	m1[1] = m2[1] + m3[1]
	m1[2] = m2[2] + m3[2]
	m1[3] = m2[3] + m3[3]
	return m1
}

func (m1 *Mat2) SumWith(m2 *Mat2) *Mat2 {
	m1[0] += m2[0]
	m1[1] += m2[1]
	m1[2] += m2[2]
	m1[3] += m2[3]
	return m1
}

nameofop takes the 2 elements and does op with them, storing the result in a new element. x1 := x2 op x3 nameofopOf takes 3 argument, does the operation on the last 2 and stores the result in the first x1 = x2 op x3 nameofopWith takes 2 element, does op with them and stores the results in the first. x1 = x1 op x2

Documentation

Overview

Package glm declares many linear algrebric types to do computer geometry. The matrices are column major (same as OpenGL) and all the angles are in radians. It is based off github.com/go-gl/mathgl version but has a very different API to take advantage of cache locality. This makes this library generally faster to execute but harder to read and write. It also uses a native math library for float32.

Index

Constants

This section is empty.

Variables

View Source
var (
	// MinNormal is the smallest normal value possible.
	MinNormal = float32(1.1754943508222875e-38) // 1 / 2**(127 - 1)
	// MinValue is the smallest non zero value possible.
	MinValue = float32(math.SmallestNonzeroFloat32)
	// MaxValue is the highest value a float32 can have.
	MaxValue = float32(math.MaxFloat32)

	// InfPos is the positive infinity value.
	InfPos = float32(math.Inf(1))
	// InfNeg is the positive infinity value.
	InfNeg = float32(math.Inf(-1))
	// NaN is a shortcut for not a number
	NaN = float32(math.NaN())
)
View Source
var Epsilon float32 = 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 CartesianToCylindrical

func CartesianToCylindrical(coord Vec3) (rho, phi, z float32)

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

func CartesianToSpherical

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

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

func Clamp

func Clamp(a, low, high float32) float32

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.

func CylindricalToSpherical

func CylindricalToSpherical(rho, phi, z float32) (r, theta, phi2 float32)

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

func DegToRad

func DegToRad(angle float32) float32

DegToRad converts degrees to radians

func Extract3DScale

func Extract3DScale(m *Mat4) (x, y, z float32)

Extract3DScale extracts the 3d scaling from a homogeneous matrix.

func ExtractMaxScale

func ExtractMaxScale(m *Mat4) float32

ExtractMaxScale extracts the maximum scaling from a homogeneous matrix.

func FloatEqual

func FloatEqual(a, b float32) 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 FloatEqualThreshold

func FloatEqualThreshold(a, b, epsilon float32) 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 IsClamped

func IsClamped(a, low, high float32) 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.

func LocalToWorldDirnIn

func LocalToWorldDirnIn(local *Vec3, transform *Mat3x4, dst *Vec3)

LocalToWorldDirnIn is a memory friendly version of LocalToWorldDirn.

func LocalToWorldIn

func LocalToWorldIn(local *Vec3, transform *Mat3x4, dst *Vec3)

LocalToWorldIn is the same as LocalToWorld but with destination vector.

func RadToDeg

func RadToDeg(angle float32) float32

RadToDeg converts radians to degrees

func Round

func Round(v float32, precision int) float32

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 ScalarTripleProduct

func ScalarTripleProduct(v0, v1, v2 *Vec3) float32

ScalarTripleProduct returns Dot(v1, Cross(v2,v3)), its also called the box or mixed product.

https://en.wikipedia.org/wiki/Triple_product

func SetMax

func SetMax(a, b *float32)

SetMax sets a to the Max(a, b).

func SetMin

func SetMin(a, b *float32)

SetMin sets a to the Min(a, b).

func SphericalToCylindrical

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

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

func WorldToLocalDirnIn

func WorldToLocalDirnIn(world *Vec3, transform *Mat3x4, dst *Vec3)

WorldToLocalDirnIn is a memory friendly version of WorldToLocalDirn.

func WorldToLocalIn

func WorldToLocalIn(world *Vec3, transform *Mat3x4, dst *Vec3)

WorldToLocalIn is a memory friendly version of WorldToLocal.

Types

type Mat2

type Mat2 [4]float32

Mat2 represents a column major 2x2 matrix.

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.

func Ident2

func Ident2() Mat2

Ident2 returns the 2x2 identity matrix.

func Mat2FromCols

func Mat2FromCols(col0, col1 *Vec2) Mat2

Mat2FromCols builds a new matrix from column vectors.

func Mat2FromRows

func Mat2FromRows(row0, row1 *Vec2) Mat2

Mat2FromRows builds a new matrix from row vectors. The resulting matrix will still be in column major order, but this can be good for hand-building matrices.

func Rotate2D

func Rotate2D(angle float32) 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 (m1 *Mat2) Abs() Mat2

Abs returns the element-wise absolute value of this matrix

func (*Mat2) AbsOf

func (m1 *Mat2) AbsOf(m2 *Mat2)

AbsOf is a memory friendly version of Abs.

func (*Mat2) AbsSelf

func (m1 *Mat2) AbsSelf()

AbsSelf is a memory friendly version of Abs.

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) AddOf

func (m1 *Mat2) AddOf(m2, m3 *Mat2)

AddOf is a memory friendly version of Add.

func (*Mat2) AddWith

func (m1 *Mat2) AddWith(m2 *Mat2)

AddWith is a memory friendly version of Add.

func (*Mat2) At

func (m1 *Mat2) At(row, col int) float32

At returns the matrix element at the given row and column.

func (*Mat2) Col

func (m1 *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) ColLen

func (Mat2) ColLen() int

ColLen returns the length of a col for this matrix type.

func (*Mat2) Cols

func (m1 *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 (m1 *Mat2) Det() float32

Det returns the determinant of a matrix. The determinant 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 (m1 *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) Equal

func (m1 *Mat2) Equal(m2 *Mat2) bool

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

func (*Mat2) EqualThreshold

func (m1 *Mat2) EqualThreshold(m2 *Mat2, threshold float32) bool

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

func (*Mat2) Ident

func (m1 *Mat2) Ident()

Ident sets this matrix to the identity matrix.

func (Mat2) Index

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

Index returns the index of the given row and column. Used to directly access the array.

func (*Mat2) Inverse

func (m1 *Mat2) Inverse() Mat2

Inverse computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the original, yields the identity. Return the zero matrix if the determinant is zero.

func (*Mat2) InverseOf

func (m1 *Mat2) InverseOf(m2 *Mat2)

InverseOf sets m1 to the inverse of m2.

func (*Mat2) Invert

func (m1 *Mat2) Invert()

Invert is the same as Inverse but it acts on the caller.

func (*Mat2) Mat3

func (m1 *Mat2) Mat3() Mat3

Mat3 returns the mat3 values in the top-left corner and the rest filled with the identity matrix values.

[m0 m2  0]
[m1 m3  0]
[ 0  0  1]

func (*Mat2) Mat4

func (m1 *Mat2) Mat4() Mat4

Mat4 returns the mat2 values in the top-left corner and the rest filled with the identity matrix values.

[m0 m2  0  0]
[m1 m3  0  0]
[ 0  0  1  0]
[ 0  0  0  1]

func (*Mat2) Mul

func (m1 *Mat2) Mul(c float32) 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) Mul2Of

func (m1 *Mat2) Mul2Of(m2, m3 *Mat2)

Mul2Of is a memory friendly version of Mul2.

func (*Mat2) Mul2With

func (m1 *Mat2) Mul2With(m2 *Mat2)

Mul2With is a memory friendly version of Mul2.

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) MulOf

func (m1 *Mat2) MulOf(m2 *Mat2, c float32)

MulOf is a memory friendly version of Mul.

func (*Mat2) MulWith

func (m1 *Mat2) MulWith(c float32)

MulWith is a memory friendly version of Mul.

func (*Mat2) Row

func (m1 *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) RowLen

func (Mat2) RowLen() int

RowLen returns the length of a row for this matrix type.

func (*Mat2) Rows

func (m1 *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 (m1 *Mat2) Set(row, col int, value float32)

Set sets the corresponding matrix element at the given row and column.

func (*Mat2) SetCol

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

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

func (*Mat2) SetRow

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

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

func (*Mat2) String

func (m1 *Mat2) String() 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) SubOf

func (m1 *Mat2) SubOf(m2, m3 *Mat2)

SubOf is a memory friendly version of Sub.

func (*Mat2) SubWith

func (m1 *Mat2) SubWith(m2 *Mat2)

SubWith is a memory friendly version of Sub.

func (*Mat2) Trace

func (m1 *Mat2) Trace() float32

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()

Transpose transpose this matrix with itself as destination. 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]]

func (*Mat2) TransposeOf

func (m1 *Mat2) TransposeOf(m2 *Mat2)

TransposeOf is a memory friendly version of Transposed.

func (*Mat2) Transposed

func (m1 *Mat2) Transposed() Mat2

Transposed 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]float32

Mat2x3 is a 2 row 3 column matrix.

func Ident2x3

func Ident2x3() Mat2x3

Ident2x3 returns the 2x3 fake identity matrix.

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

Mat2x3FromRows builds a new matrix from row vectors. The resulting matrix will still be in column major order, but this can be good for hand-building matrices.

func (*Mat2x3) Abs

func (m1 *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) At

func (m1 *Mat2x3) At(row, col int) float32

At returns the matrix element at the given row and column.

func (*Mat2x3) Col

func (m1 *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) ColLen

func (Mat2x3) ColLen() int

ColLen returns the col length for this matrix type.

func (*Mat2x3) Cols

func (m1 *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) Det

func (m1 *Mat2x3) Det() float32

Det on 2x3 matrix is a cheat, it assumes the last row is [0 0 1].

func (*Mat2x3) Equal

func (m1 *Mat2x3) Equal(m2 *Mat2x3) bool

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

func (*Mat2x3) EqualThreshold

func (m1 *Mat2x3) EqualThreshold(m2 *Mat2x3, threshold float32) bool

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

func (*Mat2x3) Ident

func (m1 *Mat2x3) Ident()

Ident sets this matrix to the identity matrix.

func (Mat2x3) Index

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

Index returns the index of the given row and column. Used to directly access the array.

func (*Mat2x3) Inverse

func (m1 *Mat2x3) Inverse() Mat2x3

Inverse is a cheat function that returns the inverse of this matrix as if it was a 3x3 matrix.

func (*Mat2x3) Mat2

func (m1 *Mat2x3) Mat2() Mat2

Mat2 returns a Mat2 with the last row as [0 0 1].

func (*Mat2x3) Mat2In

func (m1 *Mat2x3) Mat2In(m2 *Mat2)

Mat2In is a memory friendly version of Mat2.

func (*Mat2x3) Mat3

func (m1 *Mat2x3) Mat3() Mat3

Mat3 returns a Mat3 with the last row as [0 0 1].

func (*Mat2x3) Mat3In

func (m1 *Mat2x3) Mat3In(m2 *Mat3)

Mat3In is a memory friendly version of Mat3.

func (*Mat2x3) Mul

func (m1 *Mat2x3) Mul(c float32) 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) Mul2x1

func (m1 *Mat2x3) Mul2x1(v1 *Vec2) Vec2

Mul2x1 is a cheat function that assumes the last row is [0 0 1] and the vectors last coordinate is 1. It's used in the physics engine to transform coordinates.

func (*Mat2x3) Mul2x1In

func (m1 *Mat2x3) Mul2x1In(v1, dst *Vec2)

Mul2x1In is a memory friendly version of Mul3x1, its declaration differs from the rest of the memory utility function to keep the api clean.

func (*Mat2x3) Mul2x3

func (m1 *Mat2x3) Mul2x3(m2 *Mat2x3) Mat2x3

Mul2x3 is a cheat function that assumes the last row of both matrices is [0 0 1] and performs a 3x3 matrix multiplication.

func (*Mat2x3) Mul2x3Of

func (m1 *Mat2x3) Mul2x3Of(m2, m3 *Mat2x3)

Mul2x3Of is a memory friendly version of Mul2x3.

func (*Mat2x3) Mul2x3With

func (m1 *Mat2x3) Mul2x3With(m2 *Mat2x3)

Mul2x3With is a memory friendly version of Mul2x3.

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(v1 *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) Row

func (m1 *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) RowLen

func (Mat2x3) RowLen() int

RowLen returns the row length for this matrix type.

func (*Mat2x3) Rows

func (m1 *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 (m1 *Mat2x3) Set(row, col int, value float32)

Set sets the corresponding matrix element at the given row and column.

func (*Mat2x3) SetCol

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

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

func (*Mat2x3) SetRow

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

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

func (*Mat2x3) String

func (m1 *Mat2x3) String() 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.

type Mat3

type Mat3 [9]float32

Mat3 represents a column major 3x3 matrix.

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 float32) 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.

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

Mat3FromRows builds a new matrix from row vectors. The resulting matrix will still be in column major order, but this can be good for hand-building matrices.

func Mat4Normal

func Mat4Normal(m *Mat4) Mat3

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

func Rotate3DX

func Rotate3DX(angle float32) 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 float32) 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 float32) 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 float32) Mat3

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

https://en.wikipedia.org/wiki/Scaling_(geometry)

func Translate2D

func Translate2D(Tx, Ty float32) 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 (m1 *Mat3) Abs() Mat3

Abs returns the element-wise absolute value of this matrix

func (*Mat3) AbsOf

func (m1 *Mat3) AbsOf(m2 *Mat3)

AbsOf is a memory friendly version of Abs.

func (*Mat3) AbsSelf

func (m1 *Mat3) AbsSelf()

AbsSelf is a memory friendly version of Abs.

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) AddOf

func (m1 *Mat3) AddOf(m2, m3 *Mat3)

AddOf is a memory friendly version of Add.

func (*Mat3) AddWith

func (m1 *Mat3) AddWith(m2 *Mat3)

AddWith is a memory friendly version of Add.

func (*Mat3) At

func (m1 *Mat3) At(row, col int) float32

At returns the matrix element at the given row and column.

func (*Mat3) Col

func (m1 *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) ColLen

func (Mat3) ColLen() int

ColLen returns the length of the col of this matrix type.

func (*Mat3) Cols

func (m1 *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 (m1 *Mat3) Det() float32

Det returns the determinant of a matrix. The determinant 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 (m1 *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) Equal

func (m1 *Mat3) Equal(m2 *Mat3) bool

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

func (*Mat3) EqualThreshold

func (m1 *Mat3) EqualThreshold(m2 *Mat3, threshold float32) bool

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

func (*Mat3) Ident

func (m1 *Mat3) Ident()

Ident sets this matrix to the identity matrix.

func (Mat3) Index

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

Index returns the index of the given row and column. Used to directly access the array.

func (*Mat3) Inverse

func (m1 *Mat3) Inverse() Mat3

Inverse computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the original, yields the identity. Return the zero matrix if the determinant is zero.

func (*Mat3) InverseOf

func (m1 *Mat3) InverseOf(m2 *Mat3)

InverseOf is a memory friendly version fo Inverse.

func (*Mat3) Invert

func (m1 *Mat3) Invert()

Invert is a memory friendly version of Inverse.

func (*Mat3) Mat2

func (m1 *Mat3) Mat2() Mat2

Mat2 returns the upper 2x2 matrix.

[m0 m3  ?]
[m1 m4  ?]
[ ?  ?  ?]

func (*Mat3) Mat2x3

func (m1 *Mat3) Mat2x3() Mat2x3

Mat2x3 returns the top 2x3 matrix.

[m0 m3 m6]
[m1 m4 m7]
[ ?  ?  ?]

func (*Mat3) Mat3x4

func (m1 *Mat3) Mat3x4() Mat3x4

Mat3x4 returns the top 2x3 matrix.

[m0 m3 m6 0]
[m1 m4 m7 0]
[m2 m5 m8 0]

func (*Mat3) Mat4

func (m1 *Mat3) Mat4() Mat4

Mat4 returns the mat3 values in the top-left corner and the rest filled with the identity matrix values.

[m0 m3 m6  0]
[m1 m4 m7  0]
[m2 m5 m8  0]
[ 0  0  0  1]

func (*Mat3) Mul

func (m1 *Mat3) Mul(c float32) 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) Mul3Of

func (m1 *Mat3) Mul3Of(m2, m3 *Mat3)

Mul3Of is a memory friendly version of Mul3.

func (*Mat3) Mul3With

func (m1 *Mat3) Mul3With(m2 *Mat3)

Mul3With is a memory friendly version of Mul3.

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) Mul3x1In

func (m1 *Mat3) Mul3x1In(m2, dst *Vec3)

Mul3x1In is a memory friendly version of Mul3x1

func (*Mat3) Mul3x1Transpose

func (m1 *Mat3) Mul3x1Transpose(v *Vec3) Vec3

Mul3x1Transpose is the same as Mul3x1 except it uses the inplace transpose of this matrix.

func (*Mat3) MulOf

func (m1 *Mat3) MulOf(m2 *Mat3, c float32)

MulOf is a memory friendly version fo Mul.

func (*Mat3) MulWith

func (m1 *Mat3) MulWith(c float32)

MulWith is a memory friendly version fo Mul.

func (*Mat3) Row

func (m1 *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) RowLen

func (Mat3) RowLen() int

RowLen returns the length of the row of this matrix type.

func (*Mat3) Rows

func (m1 *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 (m1 *Mat3) Set(row, col int, value float32)

Set sets the corresponding matrix element at the given row and column.

func (*Mat3) SetCol

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

SetCol sets a column within the matrix.

func (*Mat3) SetOrientation

func (m1 *Mat3) SetOrientation(q1 *Quat)

SetOrientation sets this matrix to the orientation matrix represented by that quaternion.

func (*Mat3) SetRow

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

SetRow sets a row within the matrix.

func (*Mat3) String

func (m1 *Mat3) String() 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) SubOf

func (m1 *Mat3) SubOf(m2, m3 *Mat3)

SubOf is a memory friendly version of Sub.

func (*Mat3) SubWith

func (m1 *Mat3) SubWith(m2 *Mat3)

SubWith is a memory friendly version of Sub.

func (*Mat3) Trace

func (m1 *Mat3) Trace() float32

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()

Transpose is a memory friendly version of Transposed.

func (*Mat3) TransposeOf

func (m1 *Mat3) TransposeOf(m2 *Mat3)

TransposeOf is a memory friendly version of Transposed.

func (*Mat3) Transposed

func (m1 *Mat3) Transposed() Mat3

Transposed 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]float32

Mat3x4 is a 3 row 4 column matrix.

func Ident3x4

func Ident3x4() Mat3x4

Ident3x4 returns the 3x4 fake identity matrix.

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

Mat3x4FromRows builds a new matrix from row vectors. The resulting matrix will still be in column major order, but this can be good for hand-building matrices.

func (*Mat3x4) Abs

func (m1 *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) At

func (m1 *Mat3x4) At(row, col int) float32

At returns the matrix element at the given row and column.

func (*Mat3x4) Col

func (m1 *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) ColLen

func (Mat3x4) ColLen() int

ColLen returns the col length for this matrix type.

func (*Mat3x4) Cols

func (m1 *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) Det

func (m1 *Mat3x4) Det() float32

Det on 3x4 matrix is a cheat, it assumes the last row is [0 0 0 1].

[a d g j]
[b e h k]
[c f i l]
[0 0 0 1]

aei - afh - bdi + bfg + cdh - ceg

func (*Mat3x4) Equal

func (m1 *Mat3x4) Equal(m2 *Mat3x4) bool

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

func (*Mat3x4) EqualThreshold

func (m1 *Mat3x4) EqualThreshold(m2 *Mat3x4, threshold float32) bool

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

func (*Mat3x4) GetAxis

func (m1 *Mat3x4) GetAxis(i int) Vec3

GetAxis return one of the axis of the matrix. i needs to be between 0 and 3 or else this will panic

func (*Mat3x4) Ident

func (m1 *Mat3x4) Ident()

Ident sets this matrix to the identity matrix.

func (Mat3x4) Index

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

Index returns the index of the given row and column. Used to directly access the array.

func (*Mat3x4) Inverse

func (m1 *Mat3x4) Inverse() Mat3x4

Inverse is a cheat function that returns the inverse of this matrix as if it was a 4x4 matrix.

func (*Mat3x4) Mat4

func (m1 *Mat3x4) Mat4() Mat4

Mat4 returns a mat4 with the last row as [0 0 0 1].

func (*Mat3x4) Mat4In

func (m1 *Mat3x4) Mat4In(m2 *Mat4)

Mat4In is a memory friendly version of Mat4.

func (*Mat3x4) Mul

func (m1 *Mat3x4) Mul(c float32) 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) Mul3x1

func (m1 *Mat3x4) Mul3x1(v1 *Vec3) Vec3

Mul3x1 is a cheat function that assumes the last row is [0 0 0 1] and the vectors last coordinate is 1. It's used in the physics engine to transform coordinates.

func (*Mat3x4) Mul3x1In

func (m1 *Mat3x4) Mul3x1In(v1, dst *Vec3)

Mul3x1In is a memory friendly version of Mul3x1, its declaration differs from the rest of the memory utility function to keep the api clean.

func (*Mat3x4) Mul3x4

func (m1 *Mat3x4) Mul3x4(m2 *Mat3x4) Mat3x4

Mul3x4 is a cheat function that assumes the last row of both matrices is [0 0 0 1] and performs a 4x4 matrix multiplication.

func (*Mat3x4) Mul3x4Of

func (m1 *Mat3x4) Mul3x4Of(m2, m3 *Mat3x4)

Mul3x4Of is a memory friendly version of Mul3x4.

func (*Mat3x4) Mul3x4With

func (m1 *Mat3x4) Mul3x4With(m2 *Mat3x4)

Mul3x4With is a memory friendly version of Mul3x4.

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(v1 *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) Row

func (m1 *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) RowLen

func (Mat3x4) RowLen() int

RowLen returns the row length for this matrix type.

func (*Mat3x4) Rows

func (m1 *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 (m1 *Mat3x4) Set(row, col int, value float32)

Set sets the corresponding matrix element at the given row and column.

func (*Mat3x4) SetCol

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

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

func (*Mat3x4) SetOrientationAndPos

func (m1 *Mat3x4) SetOrientationAndPos(q1 *Quat, v1 *Vec3)

SetOrientationAndPos sets this matrix to represent this quaternion's orientation and this vector's position.

func (*Mat3x4) SetRow

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

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

func (*Mat3x4) String

func (m1 *Mat3x4) String() 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) Transform

func (m1 *Mat3x4) Transform(v1 *Vec3) Vec3

Transform is really just calling Mul3x1 but for the physics engine we'll redeclare it that way.

func (*Mat3x4) TransformDirection

func (m1 *Mat3x4) TransformDirection(v1 *Vec3) Vec3

TransformDirection transforms the given direction by this inner rotation matrix.

func (*Mat3x4) TransformDirectionIn

func (m1 *Mat3x4) TransformDirectionIn(v1, dst *Vec3)

TransformDirectionIn is a memory friendly version of TransformDirection.

func (*Mat3x4) TransformIn

func (m1 *Mat3x4) TransformIn(v1, dst *Vec3)

TransformIn is really just calling Mul3x1In but for the physics engine we'll redeclare it that way.

func (*Mat3x4) TransformInverse

func (m1 *Mat3x4) TransformInverse(v1 *Vec3) Vec3

TransformInverse will transform v1 by using shortcut. Like assuming that the 4th column is a translation and that the inner 3x3 matrix is a rotation matrix (meaning that we can use the transpose.

func (*Mat3x4) TransformInverseDirection

func (m1 *Mat3x4) TransformInverseDirection(v1 *Vec3) Vec3

TransformInverseDirection uses the fact that the inner 3x3 matrix is a rotation matrix to use the transpose to do the inverse of TransformDirection.

func (*Mat3x4) TransformInverseDirectionIn

func (m1 *Mat3x4) TransformInverseDirectionIn(v1, dst *Vec3)

TransformInverseDirectionIn is a memory friendly version of TransformInverseDirection.

func (*Mat3x4) TransformInverseIn

func (m1 *Mat3x4) TransformInverseIn(v1, dst *Vec3)

TransformInverseIn is a memory friendly version of TransformInverse.

type Mat4

type Mat4 [16]float32

Mat4 represents a column major 4x4 matrix.

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 float32) Mat4

Frustum returns a Mat4 representing a frustrum transform (squared pyramid with the top cut off)

func HomogRotate3D

func HomogRotate3D(angle float32, axis *Vec3) Mat4

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

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 float32) 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 float32) 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 float32) 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.

func LookAt

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

LookAt returns a Mat4 that represents a camera transform from the given arguments.

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

Mat4FromRows builds a new matrix from row vectors. The resulting matrix will still be in column major order, but this can be good for hand-building matrices.

func Ortho

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

Ortho returns a Mat4 that represents a orthographic projection from the given arguments.

func Ortho2D

func Ortho2D(left, right, bottom, top float32) 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 float32) Mat4

Perspective returns a Mat4 representing a perspective projection of the given arguments.

func Scale3D

func Scale3D(scaleX, scaleY, scaleZ float32) Mat4

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

https://en.wikipedia.org/wiki/Scaling_(geometry)

func Translate3D

func Translate3D(Tx, Ty, Tz float32) 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 (m1 *Mat4) Abs() Mat4

Abs returns the element-wise absolute value of this matrix

func (*Mat4) AbsOf

func (m1 *Mat4) AbsOf(m2 *Mat4)

AbsOf is a memory friendly version of Abs.

func (*Mat4) AbsSelf

func (m1 *Mat4) AbsSelf()

AbsSelf is a memory friendly version of Abs.

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) AddOf

func (m1 *Mat4) AddOf(m2, m3 *Mat4)

AddOf is a memory friendly version of Add.

func (*Mat4) AddWith

func (m1 *Mat4) AddWith(m2 *Mat4)

AddWith is a memory friendly version of Add.

func (*Mat4) At

func (m1 *Mat4) At(row, col int) float32

At returns the matrix element at the given row and column.

func (*Mat4) Col

func (m1 *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) ColLen

func (Mat4) ColLen() int

ColLen returns the col length for this matrix type.

func (*Mat4) Cols

func (m1 *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 (m1 *Mat4) Det() float32

Det returns the determinant of a matrix. The determinant 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 (m1 *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) Equal

func (m1 *Mat4) Equal(m2 *Mat4) bool

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

func (*Mat4) EqualThreshold

func (m1 *Mat4) EqualThreshold(m2 *Mat4, threshold float32) bool

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

func (*Mat4) Ident

func (m1 *Mat4) Ident()

Ident sets this matrix to the identity matrix.

func (Mat4) Index

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

Index returns the index of the given row and column. Used to directly access the array.

func (*Mat4) Inverse

func (m1 *Mat4) Inverse() Mat4

Inverse computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the original, yields the identity. Return the zero matrix if the determinant is zero.

func (*Mat4) InverseOf

func (m1 *Mat4) InverseOf(m2 *Mat4)

InverseOf is a memory friendly version of Inverse.

func (*Mat4) Invert

func (m1 *Mat4) Invert()

Invert is a memory friendly version of Inverse.

func (*Mat4) Mat2

func (m1 *Mat4) Mat2() Mat2

Mat2 returns the upper 2x2 matrix.

[m0 m4  ?  ?]
[m1 m5  ?  ?]
[ ?  ?  ?  ?]
[ ?  ?  ?  ?]

func (*Mat4) Mat3

func (m1 *Mat4) Mat3() Mat3

Mat3 returns returns the upper 3x3 matrix.

[m0  m4   m8  ?]
[m1  m5   m9  ?]
[m2  m6  m10  ?]
[ ?   ?    ?  ?]

func (*Mat4) Mat3x4

func (m1 *Mat4) Mat3x4() Mat3x4

Mat3x4 returns the top 3x4 matrix.

[m0  m4  m7 m10]
[m1  m5  m8 m11]
[m2  m6  m9 m12]
[ ?   ?   ?   ?]

func (*Mat4) Mul

func (m1 *Mat4) Mul(c float32) 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) Mul4Of

func (m1 *Mat4) Mul4Of(m2, m3 *Mat4)

Mul4Of is a memory friendly version fo Mul4.

func (*Mat4) Mul4With

func (m1 *Mat4) Mul4With(m2 *Mat4)

Mul4With is a memory friendly version fo Mul4.

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) MulOf

func (m1 *Mat4) MulOf(m2 *Mat4, c float32)

MulOf is a memory friendly version of Mul.

func (*Mat4) MulWith

func (m1 *Mat4) MulWith(c float32)

MulWith is a memory friendly version of Mul.

func (*Mat4) Row

func (m1 *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) RowLen

func (Mat4) RowLen() int

RowLen returns the row length for this matrix type.

func (*Mat4) Rows

func (m1 *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 (m1 *Mat4) Set(row, col int, value float32)

Set sets the corresponding matrix element at the given row and column.

func (*Mat4) SetCol

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

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

func (*Mat4) SetRow

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

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

func (*Mat4) String

func (m1 *Mat4) String() 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) SubOf

func (m1 *Mat4) SubOf(m2, m3 *Mat4)

SubOf is a memory friendly version of Sub.

func (*Mat4) SubWith

func (m1 *Mat4) SubWith(m2 *Mat4)

SubWith is a memory friendly version of Sub.

func (*Mat4) Trace

func (m1 *Mat4) Trace() float32

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()

Transpose is a memory friendly version of Transposed.

func (*Mat4) TransposeOf

func (m1 *Mat4) TransposeOf(m2 *Mat4)

TransposeOf is a memory friendly version of Transposed.

func (*Mat4) Transposed

func (m1 *Mat4) Transposed() Mat4

Transposed 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 Quat

type Quat struct {
	W float32
	V Vec3
}

Quat is a Quaternion. A Quaternion is an extension of the imaginary numbers. 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. i² = j² = k² = ijk

func AnglesToQuat

func AnglesToQuat(angle1, angle2, angle3 float32, 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.

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 return the identity quaternion. 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 float32) Quat

QuatLerp is *L*inear Int*erp*olation between two Quaternions.

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 float32) Quat

QuatNlerp is *N*ormalized *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 float32, 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 float32) Quat

QuatSlerp is *S*pherical *L*inear Int*erp*olation, a method of interpolating between two quaternions. This always takes the straightest 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) AddOf

func (q1 *Quat) AddOf(q2, q3 *Quat)

AddOf is a memory friendly version of add. q1 = q2 + q3

func (*Quat) AddScaledVec

func (q1 *Quat) AddScaledVec(f float32, v1 *Vec3)

AddScaledVec takes an input vector and scaled it by f then adds that rotation to q1.

func (*Quat) AddWith

func (q1 *Quat) AddWith(q2 *Quat)

AddWith is a memory friendly version of Add. In quaternion cases you COULD use AddOf with q1 twice: q1.AddOf(&q1,&q2). This is here just for API consistency.

func (*Quat) Conjugate

func (q1 *Quat) Conjugate()

Conjugate is a memory friendly version of Conjugated. q1 = conjugate(q1)

func (*Quat) ConjugateOf

func (q1 *Quat) ConjugateOf(q2 *Quat)

ConjugateOf is a memory friendly version of Conjugated. q1 = conjugate(q2)

func (*Quat) Conjugated

func (q1 *Quat) Conjugated() Quat

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

func (*Quat) Dot

func (q1 *Quat) Dot(q2 *Quat) float32

Dot returns the dot product between two quaternions.

func (*Quat) Equal

func (q1 *Quat) Equal(q2 *Quat) bool

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

func (*Quat) EqualThreshold

func (q1 *Quat) EqualThreshold(q2 *Quat, epsilon float32) bool

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

func (*Quat) I

func (q1 *Quat) I() float32

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

func (*Quat) Iden

func (q1 *Quat) Iden()

Iden sets this quaternion to the identity quaternion.

func (*Quat) Inverse

func (q1 *Quat) Inverse() Quat

Inverse returns the 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) InverseOf

func (q1 *Quat) InverseOf(q2 *Quat)

InverseOf is a memory friendly version of Inverse.

func (*Quat) Invert

func (q1 *Quat) Invert()

Invert is a memory friendly version of Inverse.

func (*Quat) J

func (q1 *Quat) J() float32

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

func (*Quat) K

func (q1 *Quat) K() float32

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

func (*Quat) Len

func (q1 *Quat) Len() float32

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

func (*Quat) Mat3

func (q1 *Quat) Mat3() Mat3

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

func (*Quat) Mat4

func (q1 *Quat) Mat4() Mat4

Mat4 returns the homogeneous 3D rotation matrix corresponding to the quaternion. with last row and last column as [0 0 0 1]

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) MulOf

func (q1 *Quat) MulOf(q2, q3 *Quat)

MulOf is a memory friendly version of Mul.

func (*Quat) MulWith

func (q1 *Quat) MulWith(q2 *Quat)

MulWith is a memory friendly version of Mul. Use this when you want q1 both as dest and arg.

func (*Quat) Norm

func (q1 *Quat) Norm() float32

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

func (*Quat) Normalize

func (q1 *Quat) Normalize()

Normalize Normalizes the quaternion in place.

func (*Quat) Normalized

func (q1 *Quat) Normalized() Quat

Normalized Normalizes the quaternion, returning its versor (unit quaternion).

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 float32) bool

OrientationEqualThreshold returns whether the quaternions represents the same orientation with a given tolerance.

func (*Quat) Rotate

func (q1 *Quat) Rotate(v *Vec3) Vec3

Rotate rotates 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 float32) Quat

Scale scales every element of the quaternion by some constant factor.

func (*Quat) ScaleOf

func (q1 *Quat) ScaleOf(c float32, q2 *Quat)

ScaleOf scales every element of the quaternion by some constant factor.

func (*Quat) ScaleWith

func (q1 *Quat) ScaleWith(c float32)

ScaleWith scales every element of the quaternion by some constant factor.

func (*Quat) SetNormalizedOf

func (q1 *Quat) SetNormalizedOf(q2 *Quat)

SetNormalizedOf Normalizes the quaternion, returning its versor (unit quaternion).

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) SubOf

func (q1 *Quat) SubOf(q2, q3 *Quat)

SubOf is a memory friendly version of add. q1 = q2 + q3

func (*Quat) SubWith

func (q1 *Quat) SubWith(q2 *Quat)

SubWith is a memory friendly version of Sub. In quaternion cases you COULD use SubOf with q1 twice: q1.SubOf(&q1,&q2). This is here just for API consistency.

func (*Quat) X

func (q1 *Quat) X() float32

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

func (*Quat) Y

func (q1 *Quat) Y() float32

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

func (*Quat) Z

func (q1 *Quat) Z() float32

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

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
)

All the possible rotation orders quaternion can take.

type Transform

type Transform Mat4

Transform is a utility type used to aggregate transformations. Transform concatenation, like matrix multiplication, is not commutative.

func NewTransform

func NewTransform() Transform

NewTransform returns a new, initialized transform.

func (*Transform) Concatenate

func (t *Transform) Concatenate(t2 *Transform)

Concatenate Transform t2 into t.

func (*Transform) Iden

func (t *Transform) Iden()

Iden sets this transform to the identity transform. You NEED to call this EXCEPT IF: - You get your transform from NewTransform - You're gonna call Set* BEFORE Translate* or Rotate*

func (*Transform) LocalToWorld

func (t *Transform) LocalToWorld(v *Vec3) Vec3

LocalToWorld transform a given point and returns the world point that this transform generates.

func (*Transform) Mat4

func (t *Transform) Mat4() Mat4

Mat4 simply returns the Mat4 associated with this Transform. This effectively makes a copy.

func (*Transform) Normal

func (t *Transform) Normal() Mat3

Normal returns the normal matrix of this transform, this is used in most light shading algorithms.

func (*Transform) Pointer

func (t *Transform) Pointer() unsafe.Pointer

Pointer returns the pointer to the first element of the underlying 4x4 matrix. This is can be passed directly to OpenGL function.

func (*Transform) RotateQuat

func (t *Transform) RotateQuat(q *Quat)

RotateQuat rotates this transform by q.

func (*Transform) SetRotateQuat

func (t *Transform) SetRotateQuat(q *Quat)

SetRotateQuat rotates this transform by q.

func (*Transform) SetTranslate3f

func (t *Transform) SetTranslate3f(x, y, z float32)

SetTranslate3f sets the transform to a translate transform of {x, y, z}.

func (*Transform) SetTranslateVec3

func (t *Transform) SetTranslateVec3(v *Vec3)

SetTranslateVec3 sets the transform to a translate transform of v.

func (*Transform) String

func (t *Transform) String() string

String return a string that represents this transform (a mat4).

func (*Transform) Translate3f

func (t *Transform) Translate3f(x, y, z float32)

Translate3f concatenates a translation to this transform of {x, y, z}.

func (*Transform) TranslateVec3

func (t *Transform) TranslateVec3(v *Vec3)

TranslateVec3 concatenates a translation to this transform of v.

func (*Transform) WorldToLocal

func (t *Transform) WorldToLocal(v *Vec3) Vec3

WorldToLocal transform a given point and returns the local point that this transform generates.

type Transform2D

type Transform2D Mat3

Transform2D is a utility type used to aggregate transformations. Transform concatenation, like matrix multiplication, is not commutative.

func NewTransform2D

func NewTransform2D() Transform2D

NewTransform2D returns a new, initialized transform.

func (*Transform2D) Concatenate

func (t *Transform2D) Concatenate(t2 *Transform2D)

Concatenate Transform t2 into t.

func (*Transform2D) Iden

func (t *Transform2D) Iden()

Iden sets this transform to the identity transform. You NEED to call this EXCEPT IF: - You get your transform from NewTransform - You're gonna call Set* BEFORE Translate* or Rotate*

func (*Transform2D) LocalToWorld

func (t *Transform2D) LocalToWorld(v *Vec2) Vec2

LocalToWorld transform a given point and returns the world point that this transform generates.

func (*Transform2D) Mat3

func (t *Transform2D) Mat3() Mat3

Mat3 simply returns the Mat3 associated with this Transform. This effectively makes a copy.

func (*Transform2D) Pointer

func (t *Transform2D) Pointer() unsafe.Pointer

Pointer returns the pointer to the first element of the underlying 4x4 matrix. This is can be passed directly to OpenGL function.

func (*Transform2D) Rotate

func (t *Transform2D) Rotate(angle float32)

Rotate concatenates a rotation of angle (radian).

func (*Transform2D) SetRotate

func (t *Transform2D) SetRotate(angle float32)

SetRotate sets the transform to a rotate transform of angle (radian).

func (*Transform2D) SetTranslate2f

func (t *Transform2D) SetTranslate2f(x, y float32)

SetTranslate2f sets the transform to a translate transform of {x, y, z}.

func (*Transform2D) SetTranslateVec2

func (t *Transform2D) SetTranslateVec2(v *Vec2)

SetTranslateVec2 sets the transform to a translate transform of v.

func (*Transform2D) String

func (t *Transform2D) String() string

String return a string that represents this transform (a mat4).

func (*Transform2D) Translate2f

func (t *Transform2D) Translate2f(x, y float32)

Translate2f concatenates a translation to this transform of {x, y, z}.

func (*Transform2D) TranslateVec2

func (t *Transform2D) TranslateVec2(v *Vec2)

TranslateVec2 concatenates a translation to this transform of v.

func (*Transform2D) WorldToLocal

func (t *Transform2D) WorldToLocal(v *Vec2) Vec2

WorldToLocal transform a given point and returns the local point that this transform generates.

type Vec2

type Vec2 [2]float32

Vec2 is the representation of a vector with 2 components.

func NormalizeVec2

func NormalizeVec2(v Vec2) Vec2

NormalizeVec2 normalizes given vector. shortcut for when you don't want to use pointers.

func (*Vec2) Add

func (v1 *Vec2) Add(v2 *Vec2) Vec2

Add is equivalent to v3 := v1+v2

func (*Vec2) AddOf

func (v1 *Vec2) AddOf(v2, v3 *Vec2)

AddOf is equivalent to v1 = v2+v3

func (*Vec2) AddScaledVec

func (v1 *Vec2) AddScaledVec(c float32, v2 *Vec2)

AddScaledVec is a shortcut for v1 += c*v2

func (*Vec2) AddWith

func (v1 *Vec2) AddWith(v2 *Vec2)

AddWith is equivalent to v1+=v2

func (*Vec2) ComponentProduct

func (v1 *Vec2) ComponentProduct(v2 *Vec2) Vec2

ComponentProduct returns {v1[0]*v2[0],v1[1]*v2[1], ... v1[n]*v2[n]}. It's equivalent to v3 := v1 * v2

func (*Vec2) ComponentProductOf

func (v1 *Vec2) ComponentProductOf(v2, v3 *Vec2)

ComponentProductOf is equivalent to v1 = v2*v3

func (*Vec2) ComponentProductWith

func (v1 *Vec2) ComponentProductWith(v2 *Vec2)

ComponentProductWith is equivalent to v1 = v1*v2

func (*Vec2) Cross

func (v1 *Vec2) Cross(v2 *Vec2) float32

Cross computes the pseudo 2D cross product, Dot(Perp(u), v)

func (*Vec2) Dot

func (v1 *Vec2) Dot(v2 *Vec2) float32

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[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2].

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) Dotf

func (v1 *Vec2) Dotf(x, y float32) float32

Dotf is the same as Dot but takes 2 float32 as input instead (API convinience function)

func (Vec2) Elem

func (v1 Vec2) Elem() (x, y float32)

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

func (*Vec2) Equal

func (v1 *Vec2) Equal(v2 *Vec2) bool

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

func (*Vec2) EqualThreshold

func (v1 *Vec2) EqualThreshold(v2 *Vec2, threshold float32) bool

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

func (*Vec2) Inverse

func (v1 *Vec2) Inverse() Vec2

Inverse return a new vector with invert sign for every component

func (*Vec2) Invert

func (v1 *Vec2) Invert()

Invert changes the sign of every component of this vector.

func (*Vec2) Len

func (v1 *Vec2) Len() float32

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) Len2

func (v1 *Vec2) Len2() float32

Len2 returns the square of the length, this function is used when optimising out the sqrt operation.

func (*Vec2) Mul

func (v1 *Vec2) Mul(c float32) Vec2

Mul is equivalent to v3 := c*v1

func (*Vec2) MulOf

func (v1 *Vec2) MulOf(c float32, v2 *Vec2)

MulOf is equivalent to v1 = c*v2

func (*Vec2) MulWith

func (v1 *Vec2) MulWith(c float32)

MulWith is equivalent to v1*=c

func (*Vec2) Normalize

func (v1 *Vec2) Normalize()

Normalize is the same as Normalize but doesn't return a new vector.

func (*Vec2) Normalized

func (v1 *Vec2) Normalized() Vec2

Normalized 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) Perp

func (v1 *Vec2) Perp() Vec2

Perp returns the vector perpendicular to v1

func (*Vec2) SetNormalizeOf

func (v1 *Vec2) SetNormalizeOf(v2 *Vec2)

SetNormalizeOf sets this vector as v2 normalized. v1 = normalize(v2).

func (*Vec2) SetPerp

func (v1 *Vec2) SetPerp()

SetPerp sets this vector to its perpendicular

func (*Vec2) String

func (v1 *Vec2) String() string

String returns a pretty string for this vector. eg. {-1.00000, 0.00000}

func (*Vec2) Sub

func (v1 *Vec2) Sub(v2 *Vec2) Vec2

Sub is equivalent to v3 := v1-v2

func (*Vec2) SubOf

func (v1 *Vec2) SubOf(v2, v3 *Vec2)

SubOf is equivalent to v1 = v2-v3

func (*Vec2) SubWith

func (v1 *Vec2) SubWith(v2 *Vec2)

SubWith is equivalent to v1-=v2

func (*Vec2) Vec3

func (v1 *Vec2) Vec3(z float32) Vec3

Vec3 return a Vec3 from this Vec2 with {z}. Similar to GLSL

vec3(v2, z);

func (*Vec2) Vec4

func (v1 *Vec2) Vec4(z, w float32) Vec4

Vec4 return a Vec4 from this Vec2 with {z,w}. Similar to GLSL

vec4(v2, z, w);

func (Vec2) X

func (v1 Vec2) X() float32

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 (v1 Vec2) Y() float32

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 (*Vec2) Zero

func (v1 *Vec2) Zero()

Zero sets this vector to all zero components.

type Vec3

type Vec3 [3]float32

Vec3 is the representation of a vector with 3 components.

func CylindricalToCartesian

func CylindricalToCartesian(rho, phi, z float32) Vec3

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

func LocalToWorld

func LocalToWorld(local *Vec3, transform *Mat3x4) Vec3

LocalToWorld applies the transform matrix to the local vector. Its a shortcut when using 3x4 matrices.

func LocalToWorldDirn

func LocalToWorldDirn(local *Vec3, transform *Mat3x4) Vec3

LocalToWorldDirn transforms this direction by this matrix.

func NormalizeVec3

func NormalizeVec3(v Vec3) Vec3

NormalizeVec3 normalizes given vector. shortcut for when you don't want to use pointers.

func Project

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

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

Window coordinates are continuous, not discrete, so you won't get exact pixel locations without rounding.

func SphericalToCartesian

func SphericalToCartesian(r, theta, phi float32) Vec3

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

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[0]yz;

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[0]yz

func UnProject

func UnProject(win *Vec3, modelview, projection *Mat4, initialX, initialY, width, height int) Vec3

UnProject transforms a set of window coordinates to object space. If your MVP matrix is not invertible this will return garbage.

Note that the projection may not be perfect if you use strict pixel locations rather than the exact values given by Project.

func WorldToLocal

func WorldToLocal(world *Vec3, transform *Mat3x4) Vec3

WorldToLocal applies the inverse of the transform to the vector.

func WorldToLocalDirn

func WorldToLocalDirn(world *Vec3, transform *Mat3x4) Vec3

WorldToLocalDirn inverse transforms this direction by this matrix.

func (*Vec3) Add

func (v1 *Vec3) Add(v2 *Vec3) Vec3

Add is equivalent to v3 := v1+v2

func (*Vec3) AddOf

func (v1 *Vec3) AddOf(v2, v3 *Vec3)

AddOf is equivalent to v1 = v2+v3

func (*Vec3) AddScaledVec

func (v1 *Vec3) AddScaledVec(c float32, v2 *Vec3)

AddScaledVec is a shortcut for v1 += c*v2

func (*Vec3) AddWith

func (v1 *Vec3) AddWith(v2 *Vec3)

AddWith is equivalent to v1+=v2

func (*Vec3) ComponentProduct

func (v1 *Vec3) ComponentProduct(v2 *Vec3) Vec3

ComponentProduct returns {v1[0]*v2[0],v1[1]*v2[1], ... v1[n]*v2[n]}. It's equivalent to v3 := v1 * v2

func (*Vec3) ComponentProductOf

func (v1 *Vec3) ComponentProductOf(v2, v3 *Vec3)

ComponentProductOf is equivalent to v1 = v2*v3

func (*Vec3) ComponentProductWith

func (v1 *Vec3) ComponentProductWith(v2 *Vec3)

ComponentProductWith is equivalent to v1 = v1*v2

func (*Vec3) Cross

func (v1 *Vec3) Cross(v2 *Vec3) Vec3

Cross is an operation only defined on 3D vectors, commonly referred to as "the cross product". 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.

https://en.wikipedia.org/wiki/Cross_product

func (*Vec3) CrossOf

func (v1 *Vec3) CrossOf(v2, v3 *Vec3)

CrossOf is the same as Cross but with destination vector. v1 = v2 X v3.

func (*Vec3) CrossWith

func (v1 *Vec3) CrossWith(v2 *Vec3)

CrossWith is the same as cross except it stores the result in v1.

func (*Vec3) Dot

func (v1 *Vec3) Dot(v2 *Vec3) float32

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[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2].

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) Dotf

func (v1 *Vec3) Dotf(x, y, z float32) float32

Dotf is the same as Dot but takes 3 float32 as input instead (API convinience function)

func (Vec3) Elem

func (v1 Vec3) Elem() (x, y, z float32)

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

func (*Vec3) Equal

func (v1 *Vec3) Equal(v2 *Vec3) bool

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

func (*Vec3) EqualThreshold

func (v1 *Vec3) EqualThreshold(v2 *Vec3, threshold float32) bool

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

func (*Vec3) Inverse

func (v1 *Vec3) Inverse() Vec3

Inverse return a new vector with invert sign for every component

func (*Vec3) Invert

func (v1 *Vec3) Invert()

Invert changes the sign of every component of this vector.

func (*Vec3) Len

func (v1 *Vec3) Len() float32

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) Len2

func (v1 *Vec3) Len2() float32

Len2 returns the square of the length, this function is used when optimising out the sqrt operation.

func (*Vec3) Mul

func (v1 *Vec3) Mul(c float32) Vec3

Mul is equivalent to v3 := c*v1

func (*Vec3) MulOf

func (v1 *Vec3) MulOf(c float32, v2 *Vec3)

MulOf is equivalent to v1 = c*v2

func (*Vec3) MulWith

func (v1 *Vec3) MulWith(c float32)

MulWith is equivalent to v1*=c

func (*Vec3) Normalize

func (v1 *Vec3) Normalize()

Normalize is the same as Normalize but doesn't return a new vector.

func (*Vec3) Normalized

func (v1 *Vec3) Normalized() Vec3

Normalized 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) 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) SetNormalizeOf

func (v1 *Vec3) SetNormalizeOf(v2 *Vec3)

SetNormalizeOf sets this vector as v2 normalized. v1 = normalize(v2).

func (*Vec3) String

func (v1 *Vec3) String() string

String returns a pretty string for this vector. eg. {-1.00000, 0.00000, 0.00000}

func (*Vec3) Sub

func (v1 *Vec3) Sub(v2 *Vec3) Vec3

Sub is equivalent to v3 := v1-v2

func (*Vec3) SubOf

func (v1 *Vec3) SubOf(v2, v3 *Vec3)

SubOf is equivalent to v1 = v2-v3

func (*Vec3) SubWith

func (v1 *Vec3) SubWith(v2 *Vec3)

SubWith is equivalent to v1-=v2

func (*Vec3) Vec2

func (v1 *Vec3) Vec2() Vec2

Vec2 return a Vec2 from the first 2 components of this Vec3. Similar to GLSL

vec2(v3);

func (*Vec3) Vec4

func (v1 *Vec3) Vec4(w float32) Vec4

Vec4 return a Vec4 from this Vec3 with {w}. Similar to GLSL

vec4(v3, w);

func (Vec3) X

func (v1 Vec3) X() float32

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 (v1 Vec3) Y() float32

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 (v1 Vec3) Z() float32

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.

func (*Vec3) Zero

func (v1 *Vec3) Zero()

Zero sets this vector to all zero components.

type Vec4

type Vec4 [4]float32

Vec4 is the representation of a vector with 4 components.

func NormalizeVec4

func NormalizeVec4(v Vec4) Vec4

NormalizeVec4 normalizes given vector. shortcut for when you don't want to use pointers.

func (*Vec4) Add

func (v1 *Vec4) Add(v2 *Vec4) Vec4

Add is equivalent to v3 := v1+v2

func (*Vec4) AddOf

func (v1 *Vec4) AddOf(v2, v3 *Vec4)

AddOf is equivalent to v1 = v2+v3

func (*Vec4) AddScaledVec

func (v1 *Vec4) AddScaledVec(c float32, v2 *Vec4)

AddScaledVec is a shortcut for v1 += c*v2

func (*Vec4) AddWith

func (v1 *Vec4) AddWith(v2 *Vec4)

AddWith is equivalent to v1+=v2

func (*Vec4) ComponentProduct

func (v1 *Vec4) ComponentProduct(v2 *Vec4) Vec4

ComponentProduct returns {v1[0]*v2[0],v1[1]*v2[1], ... v1[n]*v2[n]}. It's equivalent to v3 := v1 * v2

func (*Vec4) ComponentProductOf

func (v1 *Vec4) ComponentProductOf(v2, v3 *Vec4)

ComponentProductOf is equivalent to v1 = v2*v3

func (*Vec4) ComponentProductWith

func (v1 *Vec4) ComponentProductWith(v2 *Vec4)

ComponentProductWith is equivalent to v1 = v1*v2

func (*Vec4) Dot

func (v1 *Vec4) Dot(v2 *Vec4) float32

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[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2].

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) Dotf

func (v1 *Vec4) Dotf(x, y, z, w float32) float32

Dotf is the same as Dot but takes 4 float32 as input instead (API convinience function)

func (Vec4) Elem

func (v1 Vec4) Elem() (x, y, z, w float32)

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

func (*Vec4) Equal

func (v1 *Vec4) Equal(v2 *Vec4) bool

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

func (*Vec4) EqualThreshold

func (v1 *Vec4) EqualThreshold(v2 *Vec4, threshold float32) bool

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

func (*Vec4) Inverse

func (v1 *Vec4) Inverse() Vec4

Inverse return a new vector with invert sign for every component

func (*Vec4) Invert

func (v1 *Vec4) Invert()

Invert changes the sign of every component of this vector.

func (*Vec4) Len

func (v1 *Vec4) Len() float32

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) Len2

func (v1 *Vec4) Len2() float32

Len2 returns the square of the length, this function is used when optimising out the sqrt operation.

func (*Vec4) Mul

func (v1 *Vec4) Mul(c float32) Vec4

Mul is equivalent to v3 := c*v1

func (*Vec4) MulOf

func (v1 *Vec4) MulOf(c float32, v2 *Vec4)

MulOf is equivalent to v1 = c*v2

func (*Vec4) MulWith

func (v1 *Vec4) MulWith(c float32)

MulWith is equivalent to v1*=c

func (*Vec4) Normalize

func (v1 *Vec4) Normalize()

Normalize is the same as Normalize but doesn't return a new vector.

func (*Vec4) Normalized

func (v1 *Vec4) Normalized() Vec4

Normalized 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) SetNormalizeOf

func (v1 *Vec4) SetNormalizeOf(v2 *Vec4)

SetNormalizeOf sets this vector as v2 normalized. v1 = normalize(v2).

func (*Vec4) String

func (v1 *Vec4) String() string

String returns a pretty string for this vector. eg. {-1.00000, 0.00000, 0.00000, 0.00000}

func (*Vec4) Sub

func (v1 *Vec4) Sub(v2 *Vec4) Vec4

Sub is equivalent to v3 := v1-v2

func (*Vec4) SubOf

func (v1 *Vec4) SubOf(v2, v3 *Vec4)

SubOf is equivalent to v1 = v2-v3

func (*Vec4) SubWith

func (v1 *Vec4) SubWith(v2 *Vec4)

SubWith is equivalent to v1-=v2

func (*Vec4) Vec2

func (v1 *Vec4) Vec2() Vec2

Vec2 return a Vec2 from the first 2 components of this Vec4. Similar to GLSL

vec2(v4);

func (*Vec4) Vec3

func (v1 *Vec4) Vec3() Vec3

Vec3 return a Vec3 from the first 3 components of this Vec4. Similar to GLSL

vec3(v4);

func (Vec4) W

func (v1 Vec4) W() float32

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 (v1 Vec4) X() float32

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 (v1 Vec4) Y() float32

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 (v1 Vec4) Z() float32

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.

func (*Vec4) Zero

func (v1 *Vec4) Zero()

Zero sets this vector to all zero components.

Notes

Bugs

  • the current implementation currently inverse the matrix on every call ... that may not be the most efficient.

  • the current implementation currently inverse the matrix on every call ... that may not be the most efficient.

Directories

Path Synopsis
flops
32/flops
Package flops defines a bunch of floating point comparison operations.
Package flops defines a bunch of floating point comparison operations.
64/flops
Package flops defines a bunch of floating point comparison operations.
Package flops defines a bunch of floating point comparison operations.
geo

Jump to

Keyboard shortcuts

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