lmath

package
v0.0.0-...-793ea6c Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2021 License: BSD-3-Clause Imports: 2 Imported by: 22

Documentation

Overview

Package lmath implements a 3D linear math library.

Index

Constants

View Source
const EPSILON = 1.0E-8

The default epsilon value used for floating point comparisons.

Variables

View Source
var (
	Mat3Identity = Matrix3(
		1, 0, 0,
		0, 1, 0,
		0, 0, 1,
	)

	Mat3YToZUp = Matrix3(
		1, 0, 0,
		0, 0, 1,
		0, -1, 0,
	)

	Mat3ZToYUp = Matrix3(
		1, 0, 0,
		0, 0, -1,
		0, 1, 0,
	)

	Mat3FlipY = Matrix3(
		1, 0, 0,
		0, -1, 0,
		0, 0, 1,
	)

	Mat3FlipZ = Matrix3(
		1, 0, 0,
		0, 1, 0,
		0, 0, -1,
	)

	Mat3LZToRY = Mat3FlipY.Mul(Mat3ZToYUp)
	Mat3LYToRZ = Mat3FlipZ.Mul(Mat3YToZUp)
)
View Source
var (
	Mat4Identity = Matrix4(
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1,
	)

	Mat4Zeros = Matrix4(
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0,
	)

	Mat4Ones = Matrix4(
		1, 1, 1, 1,
		1, 1, 1, 1,
		1, 1, 1, 1,
		1, 1, 1, 1,
	)

	Mat4YToZUp = Matrix4(
		1, 0, 0, 0,
		0, 0, 1, 0,
		0, -1, 0, 0,
		0, 0, 0, 1,
	)

	Mat4ZToYUp = Matrix4(
		1, 0, 0, 0,
		0, 0, -1, 0,
		0, 1, 0, 0,
		0, 0, 0, 1,
	)

	Mat4FlipY = Matrix4(
		1, 0, 0, 0,
		0, -1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1,
	)

	Mat4FlipZ = Matrix4(
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, -1, 0,
		0, 0, 0, 1,
	)

	Mat4LZToRY = Mat4FlipY.Mul(Mat4ZToYUp)
	Mat4LYToRZ = Mat4FlipZ.Mul(Mat4YToZUp)
)
View Source
var (
	QuatIdentity = Quat{1, 0, 0, 0}
	QuatZero     = Quat{0, 0, 0, 0}
)
View Source
var (
	Vec2One   = Vec2{1, 1}
	Vec2XUnit = Vec2{1, 0}
	Vec2YUnit = Vec2{0, 1}
	Vec2Zero  = Vec2{0, 0}
)
View Source
var (
	Vec3One   = Vec3{1, 1, 1}
	Vec3XUnit = Vec3{1, 0, 0}
	Vec3YUnit = Vec3{0, 1, 0}
	Vec3ZUnit = Vec3{0, 0, 1}
	Vec3Zero  = Vec3{0, 0, 0}
)
View Source
var (
	Vec4One   = Vec4{1, 1, 1, 1}
	Vec4XUnit = Vec4{1, 0, 0, 0}
	Vec4YUnit = Vec4{0, 1, 0, 0}
	Vec4ZUnit = Vec4{0, 0, 1, 0}
	Vec4WUnit = Vec4{0, 0, 0, 1}
	Vec4Zero  = Vec4{0, 0, 0, 0}
)
View Source
var Rect3Zero = Rect3{}

Rect3Zero is the zero rectangle.

Functions

func AlmostEqual

func AlmostEqual(x, y, absTol float64) bool

AlmostEqual tells if the two floating point values x and y are considered equal within the specified absolute==relative tolerence value.

The method of comparison used is that described at:

http://realtimecollisiondetection.net/blog/?p=89

func Clamp

func Clamp(v, min, max float64) float64

Clamp returns the value v clamped to the range of [min, max].

func Degrees

func Degrees(radians float64) (degrees float64)

Degrees converts from radians to degrees.

func Equal

func Equal(a, b float64) bool

Equal tells if the two floating point values a and b are considered equal within the default EPSILON comparison value. It is short-handed for:

AlmostEqual(a, b, EPSILON)

func Lerp

func Lerp(a, b, t float64) float64

Lerp performs a linear interpolation between a and b. The t parameter is a number in the range 0.0-1.0. Some examples:

Lerp(0, 10, 0) == 0
Lerp(0, 10, 0.5) == 5
Lerp(0, 10, 1) == 10

The interpolation method is precise, so it is guaranteed that:

Lerp(a, b, 1) == a

func Radians

func Radians(degrees float64) (radians float64)

Radians converts from degrees to radians.

func Rounded

func Rounded(v float64) float64

Rounded returns the value rounded to the nearest whole number.

Types

type CoordSys

type CoordSys uint8

CoordSys represents an specific coordinate system.

const (
	// Invalid coordinate system
	CoordSysInvalid CoordSys = iota

	// Z up axis, right handed coordinate system
	CoordSysZUpRight

	// Y up axis, right handed coordinate system
	CoordSysYUpRight

	// Z up axis, left handed coordinate system
	CoordSysZUpLeft

	// Y up axis, left handed coordinate system
	CoordSysYUpLeft
)

func (CoordSys) Back

func (c CoordSys) Back() Vec3

Back returns the back vector for the given coordinate system A panic will occur if the coordinate system is invalid.

func (CoordSys) ConvertMat3

func (c CoordSys) ConvertMat3(to CoordSys) Mat3

ConvertMat3 returns a matrix that transforms from the indicated coordinate system, to the coordinate system specified. A panic will occur if the coordinate system is invalid.

func (CoordSys) ConvertMat4

func (c CoordSys) ConvertMat4(to CoordSys) Mat4

ConvertMat4 returns a matrix that transforms from the indicated coordinate system, to the coordinate system specified. A panic will occur if the coordinate system is invalid.

func (CoordSys) Down

func (c CoordSys) Down() Vec3

Down returns the down vector for the given coordinate system A panic will occur if the coordinate system is invalid.

func (CoordSys) Forward

func (c CoordSys) Forward() Vec3

Forward returns the forward vector for the given coordinate system A panic will occur if the coordinate system is invalid.

func (CoordSys) Left

func (c CoordSys) Left() Vec3

Left returns the left vector for the given coordinate system A panic will occur if the coordinate system is invalid.

func (CoordSys) LeftHanded

func (c CoordSys) LeftHanded() bool

LeftHanded is short for:

!cs.RightHanded()

func (CoordSys) Right

func (c CoordSys) Right() Vec3

Right returns the right vector for the given coordinate system A panic will occur if the coordinate system is invalid.

func (CoordSys) RightFrontUp

func (c CoordSys) RightFrontUp(right, forward, up float64) Vec3

RightFrontUp returns an vector that is described by it's right, forward, and up components in whatever way the specified coordinate system represents that vector. A panic will occur if the coordinate system is invalid.

func (CoordSys) RightHanded

func (c CoordSys) RightHanded() bool

RightHanded tells whether this coordinate system is right-handed. A panic will occur if the coordinate system is invalid.

func (CoordSys) Up

func (c CoordSys) Up() Vec3

Up returns the up vector for the given coordinate system A panic will occur if the coordinate system is invalid.

type Mat3

type Mat3 [3][3]float64

Mat3 represents an 3x3 matrix, indices are in m[row][column] order.

func Mat3Compose

func Mat3Compose(scale, shear, hpr Vec3, cs CoordSys) Mat3

Mat3Compose composes a transformation matrix that applies the given scaling, shearing, and hew/pitch/roll euler rotation values for the given coordinate system.

func Mat3FromAxisAngle

func Mat3FromAxisAngle(axis Vec3, angle float64, cs CoordSys) Mat3

Mat3FromAxisAngle returns a rotation matrix that will rotate by the given angle in radians counterclockwise about the indicated axis.

func Mat3FromScaleShear

func Mat3FromScaleShear(scale, shear Vec3, cs CoordSys) Mat3

Mat3FromScaleShear returns a matrix that will apply the given scaling and shearing values along their respective axis in the specified coordinate system. A panic will occur if the coordinate system is invalid.

func Mat3FromTranslation

func Mat3FromTranslation(translation Vec2) Mat3

Mat3FromTranslation returns a matrix that will apply the given translation vector.

func Matrix3

func Matrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22 float64) Mat3

Matrix3 returns an new Mat3 given the specified matrix components.

func (Mat3) Add

func (a Mat3) Add(b Mat3) Mat3

Add performs memberwise addition a + b and returns the result.

func (Mat3) AddScalar

func (a Mat3) AddScalar(s float64) Mat3

AddScalar performs memberwise addition a + s and returns the result.

func (Mat3) AlmostEquals

func (a Mat3) AlmostEquals(b Mat3, epsilon float64) bool

AlmostEquals tells whether a is memberwise equal to b using the specified epsilon value.

func (Mat3) Col

func (a Mat3) Col(column int) Vec3

Col returns the values in the specified matrix column as an three-component vector. The column parameter must be 0, 1, or 2 or else a panic will occur.

func (Mat3) ColVec2

func (a Mat3) ColVec2(column int) Vec2

ColVec2 returns the values in the specified matrix column as an two-component vector, ignoring the third element of the column. The column parameter must be 0, 1, or 2 or else an panic will occur.

func (Mat3) Decompose

func (a Mat3) Decompose(cs CoordSys) (scale, shear, hpr Vec3)

Decompose extracts out the scaling, shearing, and hew/pitch/roll components from the composed rotation matrix. The coordinate system must be one of: CoordSysZUpRight, CoordSysZUpLeft, or CoordSysYUpLeft.

func (Mat3) Determinant

func (a Mat3) Determinant() float64

Determinant calculates and returns the determinant of the matrix.

func (Mat3) DivScalar

func (a Mat3) DivScalar(s float64) Mat3

DivScalar performs memberwise division a / s and returns the result.

func (Mat3) Equals

func (a Mat3) Equals(b Mat3) bool

Equals tells whether a is memberwise equal to b using the default EPSILON value.

func (Mat3) Inverse

func (a Mat3) Inverse() (result Mat3, ok bool)

Inverse returns the inverse of the matrix a. This is a fully general operation and has no requirements about the transformation represented by this matrix. Returned is result=Mat3Identity, ok=false if the matrix was singular and could not be inverted.

func (Mat3) InverseTransposed

func (a Mat3) InverseTransposed() (result Mat3, ok bool)

InverseTransposed simultaneously inverts and transposes the matrix and returns it. Returned is result=Mat3Identity, ok=false if the matrix was singular and could not be inverted.

func (Mat3) IsNaN

func (a Mat3) IsNaN() bool

IsNan tells if any components of this matrix are not an number.

func (Mat3) Mul

func (a Mat3) Mul(b Mat3) Mat3

Mul returns the result of a * b

func (Mat3) MulQuat

func (a Mat3) MulQuat(b Quat) Mat3

MulQuat multiplies the matrix by the specified quaternion rotation and returns the result,

func (Mat3) MulScalar

func (a Mat3) MulScalar(s float64) Mat3

MulScalar performs memberwise multiplication a * s and returns the result.

func (Mat3) Row

func (a Mat3) Row(row int) Vec3

Row returns the values in the specified matrix row as an three-component vector. The row parameter must be 0, 1, or 2 or else a panic will occur.

func (Mat3) RowVec2

func (a Mat3) RowVec2(row int) Vec2

RowVec2 returns the values in the specified matrix row as an two-component vector, the third element of the row is ignored. The row parameter must be 0, 1, or 2 or else a panic will occur.

func (Mat3) SetCol

func (a Mat3) SetCol(column int, values Vec3) Mat3

SetCol sets the values in the specified matrix column to the values in the specified three-component vector. The column parameter must be 0, 1, or 2 or else a panic will occur.

func (Mat3) SetColVec2

func (a Mat3) SetColVec2(column int, values Vec2) Mat3

SetColVec2 sets the values in the specified matrix column to the values in the specified two-component vector, leaving the third element in the column untouched. The column parameter must be 0, 1, or 2 or else an panic will occur.

func (Mat3) SetRow

func (a Mat3) SetRow(row int, values Vec3) Mat3

SetRow sets the values of the specified matrix row to the values in the specified three-component vector. The row parameter must be 0, 1, or 2 or else a panic will occur.

func (Mat3) SetRowVec2

func (a Mat3) SetRowVec2(row int, values Vec2) Mat3

SetRowVec2 sets the values in the specified matrix row to the values in the specified three-component vector, leaving the third element in the row untouched. The row parameter must be 0, 1, or 2 or else a panic will occur.

func (Mat3) String

func (a Mat3) String() string

String returns an string representation of this matrix.

func (Mat3) Sub

func (a Mat3) Sub(b Mat3) Mat3

Sub performs in-place subtraction a - b and returns the result.

func (Mat3) SubScalar

func (a Mat3) SubScalar(s float64) Mat3

SubScalar performs memberwise subtraction a - s and returns the result.

func (Mat3) Transposed

func (a Mat3) Transposed() Mat3

Transposed returns the transposed version of matrix a.

type Mat4

type Mat4 [4][4]float64

Mat4 represents an 4x4 matrix, indices are in m[row][column] order.

func Mat4FromAxisAngle

func Mat4FromAxisAngle(axis Vec3, angle float64, cs CoordSys) Mat4

Mat4FromAxisAngle returns a rotation matrix that will rotate by the given angle in radians counterclockwise about the indicated axis.

func Mat4FromFrustum

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

Mat4FromFrustum returns a matrix that represents the given frustum given the specified bounds.

func Mat4FromScale

func Mat4FromScale(scale Vec3) Mat4

Mat4FromScale returns a matrix that will apply the given scaling values along their respective axis.

func Mat4FromScaleShear

func Mat4FromScaleShear(scale, shear Vec3, cs CoordSys) Mat4

Mat4FromScaleShear returns a matrix that will apply the given scaling and shearing values along their respective axis in the specified coordinate system. A panic will occur if the coordinate system is invalid.

func Mat4FromTranslation

func Mat4FromTranslation(translation Vec3) Mat4

Mat4FromTranslation returns a matrix that will apply the given translation vector.

func Mat4Ortho

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

Mat4Ortho returns a orthographic projection matrix given the specified frustum bounds. See: http://en.wikipedia.org/wiki/Orthographic_projection_(geometry)

func Mat4Perspective

func Mat4Perspective(fovY, aspectRatio, near, far float64) Mat4

Mat4Perspective returns a matrix that represents a perspective viewing frustum given the specified field of view, aspect ratio, and near/far values.

func Mat4UnOrtho

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

Mat4UnOrtho returns a orthographic unprojection matrix given the specified frustum bounds. See: http://en.wikipedia.org/wiki/Orthographic_projection_(geometry)

func Matrix4

func Matrix4(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33 float64) Mat4

Matrix4 returns an new *Mat4 given the specified matrix components.

func (Mat4) Add

func (a Mat4) Add(b Mat4) Mat4

Add performs in-place memberwise addition a + b and returns the result.

func (Mat4) AddScalar

func (a Mat4) AddScalar(s float64) Mat4

AddScalar performs in-place memberwise addition a + s and returns the result.

func (Mat4) AffineInverse

func (a Mat4) AffineInverse() (out Mat4, ok bool)

AffineInverse returns the inverse of the affine transformation matrix and returns the result. Returned is Mat4Identity, ok=false if the matrix was singular and could not be inverted,

func (Mat4) AlmostEquals

func (a Mat4) AlmostEquals(b Mat4, epsilon float64) bool

AlmostEquals tells whether a is memberwise equal to b using the specified epsilon value.

func (Mat4) Col

func (a Mat4) Col(column int) Vec4

Col returns the values in the specified matrix column as an four-component vector. The column parameter must be 0, 1, 2, or 3 or else an panic will occur.

func (Mat4) ColVec3

func (a Mat4) ColVec3(column int) Vec3

ColVec3 returns the values in the specified matrix column as an three-component vector, ignoring the fourth element of the column. The column parameter must be 0, 1, 2, or 3 or else an panic will occur.

func (Mat4) Determinant

func (a Mat4) Determinant() float64

Determinant calculates and returns the determinant of the matrix.

func (Mat4) DivScalar

func (a Mat4) DivScalar(s float64) Mat4

DivScalar performs in-place memberwise division a / s and returns the result.

func (Mat4) Equals

func (a Mat4) Equals(b Mat4) bool

Equals tells whether a is memberwise equal to b using the default EPSILON value.

func (Mat4) Inverse

func (a Mat4) Inverse() (result Mat4, ok bool)

Inverse returns the inverse of the matrix, as a fully general operation and makes no requirements about the type of transform represented by the matrix. Returned is Mat4Identity, ok=false if the matrix was singular and could not be inverted.

func (Mat4) IsNaN

func (a Mat4) IsNaN() bool

IsNan tells if any components of this matrix are not an number.

func (Mat4) Mul

func (a Mat4) Mul(b Mat4) Mat4

Mul performs matrix multiplication a * b and returns the result.

func (Mat4) MulQuat

func (a Mat4) MulQuat(b Quat) Mat4

MulQuat multiplies the matrix by the quaternion a * b and returns the result.

func (Mat4) MulScalar

func (a Mat4) MulScalar(s float64) Mat4

MulScalar performs in-place memberwise multiplication a * s and returns the result.

func (Mat4) Project

func (a Mat4) Project(p3 Vec3) (p2 Vec2, ok bool)

Project returns a 2D point in the range -1 to +1 given a 3D point also in the frustum matrix a's coordinate space.

If ok=false is returned then the point is outside of the frustum matrix a, and the returned point may not be meaningful.

func (Mat4) Row

func (a Mat4) Row(row int) Vec4

Row returns the values in the specified matrix row as an three-component vector. The row parameter must be 0, 1, 2, or 3 or else an panic will occur.

func (Mat4) RowVec3

func (a Mat4) RowVec3(row int) Vec3

RowVec3 returns the values in the specified matrix row as an three-component vector, the fourth element of the row is ignored. The row parameter must be 0, 1, 2, or 3 or else an panic will occur.

func (Mat4) SetCol

func (a Mat4) SetCol(column int, values Vec4) Mat4

SetCol sets the values in the specified matrix column to the values in the specified four-component vector. The column parameter must be 0, 1, 2, or 3 or else an panic will occur.

func (Mat4) SetColVec3

func (a Mat4) SetColVec3(column int, values Vec3) Mat4

SetColVec3 sets the values in the specified matrix column to the values in the specified three-component vector, leaving the fourth element in the column untouched. The column parameter must be 0, 1, 2, or 3 or else an panic will occur.

func (Mat4) SetRow

func (a Mat4) SetRow(row int, values Vec4) Mat4

SetRow sets the values in the specified matrix row to the values in the specified four-component vector. The row parameter must be 0, 1, 2, or 3 or else an panic will occur.

func (Mat4) SetRowVec3

func (a Mat4) SetRowVec3(row int, values Vec3) Mat4

SetRowVec3 sets the values in the specified matrix row to the values in the specified three-component vector, leaving the fourth element in the row untouched. The row parameter must be 0, 1, 2, or 3 or else an panic will occur.

func (Mat4) SetTranslation

func (a Mat4) SetTranslation(t Vec3) Mat4

SetTranslation sets the translation components of the matrix to the given vector and returns the result.

func (Mat4) SetUpperMat3

func (a Mat4) SetUpperMat3(b Mat3) Mat4

SetUpperMat3 sets the upper-left 3x3 matrix to the specified one and returns the new 4x4 matrix.

func (Mat4) String

func (a Mat4) String() string

String returns an string representation of this matrix.

func (Mat4) Sub

func (a Mat4) Sub(b Mat4) Mat4

Sub performs in-place memberwise subtraction a - b and returns the result.

func (Mat4) SubScalar

func (a Mat4) SubScalar(s float64) Mat4

SubScalar performs in-place memberwise subtraction a - s and returns the result.

func (Mat4) Translation

func (a Mat4) Translation() Vec3

Translation returns the translation components of the matrix as a vector.

func (Mat4) Transposed

func (a Mat4) Transposed() Mat4

Transposed returns the transposed version of matrix a.

func (Mat4) UpperMat3

func (a Mat4) UpperMat3() Mat3

UpperMat3 returns the upper-left 3x3 matrix.

type Quat

type Quat struct {
	W, X, Y, Z float64
}

Quat represents a four component vector.

func QuatFromAxisAngle

func QuatFromAxisAngle(axis Vec3, angle float64) Quat

QuatFromAxisAngle returns a quaternion rotation which represents the given angle of rotation in radians about the given axis.

func QuatFromHpr

func QuatFromHpr(hpr Vec3, cs CoordSys) Quat

QuatFromHpr returns a quaternion equivilent to the heading, pithc, and roll Euler angles in radians for the given coordinate system.

func QuatFromMat3

func QuatFromMat3(m Mat3) Quat

QuatFromMat3 returns a quaternion rotation according to the rotation represented by the matrix.

func (Quat) Add

func (a Quat) Add(b Quat) Quat

Add performs a componentwise addition of the two quaternions, returning a + b.

func (Quat) AddScalar

func (a Quat) AddScalar(b float64) Quat

AddScalar performs a componentwise scalar addition of a + b.

func (Quat) AlmostEquals

func (a Quat) AlmostEquals(b Quat, epsilon float64) bool

AlmostEquals tells if a == b using the specified epsilon value.

func (Quat) Angle

func (a Quat) Angle() float64

Angle returns the rotation represented by the quaternion as an angle about an arbitrary axis (returned by the Axis() function), the returned value is expressed in radian units counterclockwise about the axis.

func (Quat) AngleQuat

func (a Quat) AngleQuat(cs CoordSys, b Quat) float64

Angle returns the angle between the orientation represented by this quaternion and the orientation represent by the quaternion b in radians.

func (Quat) Axis

func (a Quat) Axis() Vec3

Axis returns the axis of the rotation represented by the quaternion. The returned vector is not normalized.

func (Quat) Clamp

func (a Quat) Clamp(min, max float64) Quat

Clamp clamps each value in the quaternion to the range of [min, max] and returns it.

func (Quat) Conjugate

func (a Quat) Conjugate() Quat

Conjugate calculates and returns the conjugate of this quaternion.

func (Quat) Div

func (a Quat) Div(b Quat) Quat

Div performs a componentwise division of the two quaternions, returning a * b.

func (Quat) DivScalar

func (a Quat) DivScalar(b float64) Quat

DivScalar performs a componentwise scalar division of a * b.

func (Quat) Dot

func (a Quat) Dot(b Quat) float64

Dot returns the dot product of a and b.

func (Quat) Equals

func (a Quat) Equals(b Quat) bool

Equals tells if a == b using the default EPSILON value.

func (Quat) ExtractToMat3

func (a Quat) ExtractToMat3() Mat3

ExtractToMat3 extracts the quaternion into a three-component matrix and returns it.

func (Quat) ExtractToMat4

func (a Quat) ExtractToMat4() Mat4

ExtractToMat4 extracts the quaternion into a four-component matrix and returns it.

func (Quat) Forward

func (a Quat) Forward(cs CoordSys) Vec3

Forward returns the orientation represented by this quaternion expressed as an forward vector.

func (Quat) Hpr

func (a Quat) Hpr(cs CoordSys) (hpr Vec3)

Hpr extracts the equivilent heading, pitch, and roll euler angles in radians from the quaternion for the given coordinate system.

func (Quat) Inverse

func (a Quat) Inverse() Quat

Inverse returns the inverse of this quaternion.

func (Quat) IsNaN

func (a Quat) IsNaN() bool

IsNaN tells if any components of this quaternion are not an number.

func (Quat) Length

func (a Quat) Length() float64

Length returns the magnitude of this quaternion. To avoid a sqrt call when strictly comparing distances, LengthSq can be used instead.

func (Quat) LengthSq

func (a Quat) LengthSq() float64

LengthSq returns the magnitude squared of this quaternion, useful for comparing distances.

func (Quat) Lerp

func (a Quat) Lerp(b Quat, t float64) Quat

Lerp returns a quaternion representing the linear interpolation between the quaternions a and b. The t parameter is the amount to interpolate (0.0 - 1.0) between the quaternions.

func (Quat) Max

func (a Quat) Max(b Quat) Quat

Max returns a quaternion representing the largest components of both the quaternions.

func (Quat) Min

func (a Quat) Min(b Quat) Quat

Min returns a quaternion representing the smallest components of both the quaternions.

func (Quat) Mul

func (a Quat) Mul(b Quat) Quat

Mul returns the result of the quaternion multiplication a * b.

func (Quat) MulScalar

func (a Quat) MulScalar(b float64) Quat

MulScalar performs a componentwise scalar multiplication of a * b.

func (Quat) Normalized

func (a Quat) Normalized() Quat

Normalized returns the normalized (i.e. length/magnitude == 1) quaternion of a. The quaternion a must be non-zero or else division by zero may occur.

func (Quat) Right

func (a Quat) Right(cs CoordSys) Vec3

Right returns the orientation represented by this quaternion expressed as an right vector.

func (Quat) String

func (a Quat) String() string

String returns an string representation of this vector.

func (Quat) Sub

func (a Quat) Sub(b Quat) Quat

Sub performs a componentwise subtraction of the two quaternions, returning a - b.

func (Quat) SubScalar

func (a Quat) SubScalar(b float64) Quat

SubScalar performs a componentwise scalar subtraction of a - b.

func (Quat) TransformVec3

func (a Quat) TransformVec3(v Vec3) Vec3

TransformVec3 transforms the 3-component vector by the specified quaternion rotation and returns the result.

func (Quat) TransformVec4

func (a Quat) TransformVec4(v Vec4) Vec4

TransformVec4 transforms the 4-component vector by the specified quaternion rotation and returns the result.

func (Quat) Up

func (a Quat) Up(cs CoordSys) Vec3

Up returns the orientation represented by this quaternion expressed as an up vector.

func (Quat) Vec4

func (a Quat) Vec4() Vec4

Vec4 converts the quaternion into a four-component vector; Short-hand for:

Vec4{a.W, a.X, a.Y, a.Z}

type Rect3

type Rect3 struct {
	Min, Max Vec3
}

Rect3 represents a 3D rectangle. Although it can be used to represent any form of 3D rectangle, a common use is for representing axis-aligned bounding boxes.

The rectangle contains all points where:

Min.X <= X < Max.X
Min.Y <= Y < Max.Y
Min.Z <= Z < Max.Z

A rectangle is considered empty if it would contain no points.

func (Rect3) Add

func (r Rect3) Add(p Vec3) Rect3

Add returns the rectangle r translated by p.

func (Rect3) AlmostEmpty

func (r Rect3) AlmostEmpty(epsilon float64) bool

AlmostEmpty reports whether the rectangle contains no points using the specified epsilon value.

func (Rect3) AlmostEquals

func (r Rect3) AlmostEquals(s Rect3, epsilon float64) bool

AlmostEquals tells whether a is memberwise equal to b using the specified epsilon value.

func (Rect3) Area

func (r Rect3) Area() float64

Area returns the area of this rectangle (the sum of it's sides).

func (Rect3) Canon

func (r Rect3) Canon() Rect3

Canon returns the canonical version of r. The returned rectangle has minimum and maximum coordinates swapped if necessary so that it is well-formed.

func (Rect3) Center

func (r Rect3) Center() Vec3

Center returns the center point of this rectangle.

func (Rect3) Closest

func (r Rect3) Closest(p Vec3) Vec3

Closest returns the closest point towards p contained by this rectangle.

func (Rect3) Contains

func (r Rect3) Contains(p Vec3) bool

Contains tells if the point p is within this rectangle.

func (Rect3) Corners

func (r Rect3) Corners() [8]Vec3

Corners returns an array of the eight corner points of this 3D rectangle. The corners are returned in the order of left to right, back to front, bottom to top:

left,  back,  bottom
right, back,  bottom
left,  front, bottom
right, front, bottom
left,  back,  top
right, back,  top
left,  front, top
right, front, top

func (Rect3) Empty

func (r Rect3) Empty() bool

Empty reports whether the rectangle contains no points using the default epsilon for equality.

func (Rect3) Equals

func (r Rect3) Equals(s Rect3) bool

Equals reports whether r and s are equal using the default epsilon for equality.

func (Rect3) Furthest

func (r Rect3) Furthest(p Vec3) Vec3

Furthest returns the furthest point away from p contained by this rectangle.

func (Rect3) In

func (r Rect3) In(s Rect3) bool

In reports whether every point in r is in s.

func (Rect3) InSphere

func (r Rect3) InSphere(s Sphere) bool

InSphere reports whether the rectangle r is completely inside the sphere s.

func (Rect3) Inset

func (r Rect3) Inset(n float64) Rect3

Inset returns the rectangle r inset by n, which may be negative. If either of r's dimensions is less than 2*n then an empty rectangle near the center of r will be returned.

func (Rect3) Intersect

func (r Rect3) Intersect(s Rect3) (largest Rect3, ok bool)

Intersect intersects the two rectangles and returns the largest rectangle that is contained by both r and s. If the two rectangles do not overlap then Rect3Zero, ok=false will be returned.

For simple boolean tests, one should instead use the Overlaps method (as it is equivilent and faster).

func (Rect3) Overlaps

func (r Rect3) Overlaps(s Rect3) bool

Overlaps reports whether r and s have a non-empty intersection. It is functionally equivilent to, but faster than:

_, overlaps := r.Intersect(s)

func (Rect3) Size

func (r Rect3) Size() Vec3

Size returns a vector whose X, Y, and Z components directly relate to the width, depth, and height of this rectangle.

func (Rect3) SqDistToPoint

func (r Rect3) SqDistToPoint(p Vec3) float64

SqDistToPoint returns the squared distance between the point p and the rectangle r. It is functionally equivilent to (but faster than):

dist2 := r.Closest(p).Sub(p).LengthSq()

func (Rect3) String

func (r Rect3) String() string

String returns a string representation of the rectangle r.

func (Rect3) Sub

func (r Rect3) Sub(p Vec3) Rect3

Add returns the rectangle r translated by -p.

func (Rect3) Union

func (r Rect3) Union(s Rect3) Rect3

Union returns the smallest rectangle that contains both r and s.

type Sphere

type Sphere struct {
	Center Vec3
	Radius float64
}

Sphere describes a 3D sphere composed of a center point and radius.

func (Sphere) Contains

func (s Sphere) Contains(p Vec3) bool

Contains tells if the point p is within this sphere.

func (Sphere) In

func (s Sphere) In(b Sphere) bool

In tells if the sphere s inside the sphere b.

func (Sphere) InRect3

func (s Sphere) InRect3(r Rect3) bool

InRect3 reports whether the sphere s is completely inside the rectangle r. It is short-hand for:

s.Rect3().In(r)

func (Sphere) Overlaps

func (s Sphere) Overlaps(b Sphere) bool

Overlaps reports whether s and b have a non-empty intersection.

func (Sphere) OverlapsRect3

func (s Sphere) OverlapsRect3(r Rect3) bool

OverlapsRect3 reports whether the sphere s has a non-empty intersection with the rectangle r.

func (Sphere) Rect3

func (s Sphere) Rect3() Rect3

Rect3 returns a 3D rectangle encapsulating this sphere.

type Vec2

type Vec2 struct {
	X, Y float64
}

Vec2 represents a 2D vector or point.

func (Vec2) Add

func (a Vec2) Add(b Vec2) Vec2

Add performs a componentwise addition of the two vectors, returning a + b.

func (Vec2) AddScalar

func (a Vec2) AddScalar(b float64) Vec2

AddScalar performs a componentwise scalar addition of a + b.

func (Vec2) AlmostEquals

func (a Vec2) AlmostEquals(b Vec2, epsilon float64) bool

AlmostEquals tells if a == b using the specified epsilon value.

func (Vec2) Angle

func (a Vec2) Angle(b Vec2) float64

Angle returns the angle in radians between the two vectors.

func (Vec2) AnyGreater

func (a Vec2) AnyGreater(b Vec2) bool

AnyGreater tells if a is componentwise any greater than b:

return a.X > b.X || a.Y > b.Y

func (Vec2) AnyLess

func (a Vec2) AnyLess(b Vec2) bool

AnyLess tells if a is componentwise any less than b:

return a.X < b.X || a.Y < b.Y

func (Vec2) Clamp

func (a Vec2) Clamp(min, max float64) Vec2

Clamp clamps each value in the vector to the range of [min, max] and returns it.

func (Vec2) Degrees

func (a Vec2) Degrees() Vec2

Degrees converts each value in the vector from radians to degrees and returns it.

func (Vec2) Div

func (a Vec2) Div(b Vec2) Vec2

Div performs a componentwise division of the two vectors, returning a * b.

func (Vec2) DivScalar

func (a Vec2) DivScalar(b float64) Vec2

DivScalar performs a componentwise scalar division of a * b.

func (Vec2) Dot

func (a Vec2) Dot(b Vec2) float64

Dot returns the dot product of a and b.

func (Vec2) Equals

func (a Vec2) Equals(b Vec2) bool

Equals tells if a == b using the default EPSILON value.

func (Vec2) Greater

func (a Vec2) Greater(b Vec2) bool

Greater tells if a is componentwise greater than b:

return a.X > b.X && a.Y > b.Y

func (Vec2) Inverse

func (a Vec2) Inverse() Vec2

Inverse returns the inverse (negated) vector -a.

func (Vec2) IsNaN

func (a Vec2) IsNaN() bool

IsNaN tells if any components of this vector are not an number.

func (Vec2) Length

func (a Vec2) Length() float64

Length returns the magnitude of this vector. To avoid a sqrt call when strictly comparing distances, LengthSq can be used instead.

func (Vec2) LengthSq

func (a Vec2) LengthSq() float64

LengthSq returns the magnitude squared of this vector, useful for comparing distances.

func (Vec2) Lerp

func (a Vec2) Lerp(b Vec2, t float64) Vec2

Lerp returns a vector representing the linear interpolation between the vectors a and b. The t parameter is the amount to interpolate (0.0 - 1.0) between the vectors.

func (Vec2) Less

func (a Vec2) Less(b Vec2) bool

Less tells if a is componentwise less than b:

return a.X < b.X && a.Y < b.Y

func (Vec2) Max

func (a Vec2) Max(b Vec2) Vec2

Max returns a vector representing the largest components of both the vectors.

func (Vec2) Min

func (a Vec2) Min(b Vec2) Vec2

Min returns a vector representing the smallest components of both the vectors.

func (Vec2) Mul

func (a Vec2) Mul(b Vec2) Vec2

Mul performs a componentwise multiplication of the two vectors, returning a * b.

func (Vec2) MulScalar

func (a Vec2) MulScalar(b float64) Vec2

MulScalar performs a componentwise scalar multiplication of a * b.

func (Vec2) Normalized

func (a Vec2) Normalized() (v Vec2, ok bool)

Normalized returns the normalized (i.e. length/magnitude == 1) vector of a. If the vector's length is zero (and division by zero would occur) then [Vec2Zero, false] is returned.

func (Vec2) Proj

func (a Vec2) Proj(b Vec2) Vec2

Proj returns a vector representing the projection of vector a onto b.

func (Vec2) Radians

func (a Vec2) Radians() Vec2

Radians converts each value in the vector from degrees to radians and returns it.

func (Vec2) Rounded

func (a Vec2) Rounded() Vec2

Rounded rounds each value in the vector to the nearest whole number and returns it.

func (Vec2) String

func (a Vec2) String() string

String returns an string representation of this vector.

func (Vec2) Sub

func (a Vec2) Sub(b Vec2) Vec2

Sub performs a componentwise subtraction of the two vectors, returning a - b.

func (Vec2) SubScalar

func (a Vec2) SubScalar(b float64) Vec2

SubScalar performs a componentwise scalar subtraction of a - b.

func (Vec2) TransformPointVec2

func (a Vec2) TransformPointVec2(b Mat3) Vec2

TransformPointVec2 transforms a 2-component point vector by the matrix (with translation component) and returns the result. This function assumes that the matrix is an affine transformation.

func (Vec2) TransformVec2

func (a Vec2) TransformVec2(b Mat3) Vec2

TransformVec2 transforms a 2-component point vector by the matrix (without translation component) and returns the result. This function assumes that the matrix is an affine transformation.

type Vec3

type Vec3 struct {
	X, Y, Z float64
}

Vec3 represents a 3D vector or point.

func CartToSphere

func CartToSphere(p Vec3) Vec3

CartToSphere converts the point in cartesian coordinate space, p, into spherical coordinates in the form of Vec3{radiys, inclination, azimuth} and returns it.

It is implemented according to:

http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates

func SphereToCart

func SphereToCart(r float64, p Vec2) Vec3

SphereToCart converts the Vec2{inclination, azimuth} point on a sphere of the given radius to cartesian coordinates and returns it.

It is implemented according to:

http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates

func (Vec3) Add

func (a Vec3) Add(b Vec3) Vec3

Add performs a componentwise addition of the two vectors, returning a + b.

func (Vec3) AddScalar

func (a Vec3) AddScalar(b float64) Vec3

AddScalar performs a componentwise scalar addition of a + b.

func (Vec3) AlmostEquals

func (a Vec3) AlmostEquals(b Vec3, epsilon float64) bool

AlmostEquals tells if a == b using the specified epsilon value.

func (Vec3) Angle

func (a Vec3) Angle(b Vec3) float64

Angle returns the unsigned angle between the vectors a and b, in radians.

func (Vec3) AnyGreater

func (a Vec3) AnyGreater(b Vec3) bool

AnyGreater tells if a is componentwise any greater than b:

return a.X > b.X || a.Y > b.Y || a.Z > b.Z

func (Vec3) AnyLess

func (a Vec3) AnyLess(b Vec3) bool

AnyLess tells if a is componentwise any less than b:

return a.X < b.X || a.Y < b.Y || a.Z < b.Z

func (Vec3) Clamp

func (a Vec3) Clamp(min, max float64) Vec3

Clamp clamps each value in the vector to the range of [min, max] and returns it.

func (Vec3) Cross

func (a Vec3) Cross(b Vec3) Vec3

Cross returns the cross product of the two vectors.

func (Vec3) Degrees

func (a Vec3) Degrees() Vec3

Degrees converts each value in the vector from radians to degrees and returns it.

func (Vec3) Div

func (a Vec3) Div(b Vec3) Vec3

Div performs a componentwise division of the two vectors, returning a * b.

func (Vec3) DivScalar

func (a Vec3) DivScalar(b float64) Vec3

DivScalar performs a componentwise scalar division of a * b.

func (Vec3) Dot

func (a Vec3) Dot(b Vec3) float64

Dot returns the dot product of a and b.

func (Vec3) Equals

func (a Vec3) Equals(b Vec3) bool

Equals tells if a == b using the default EPSILON value.

func (Vec3) Greater

func (a Vec3) Greater(b Vec3) bool

Greater tells if a is componentwise greater than b:

return a.X > b.X && a.Y > b.Y && a.Z > b.Z

func (Vec3) HprToXyz

func (v Vec3) HprToXyz() Vec3

HprToXyz converts Hew, Pitch and Roll rotation to X, Y, and Z axis rotation.

func (Vec3) Inverse

func (a Vec3) Inverse() Vec3

Inverse returns the inverse (negated) vector -a.

func (Vec3) IsNaN

func (a Vec3) IsNaN() bool

IsNaN tells if any components of this vector are not an number.

func (Vec3) Length

func (a Vec3) Length() float64

Length returns the magnitude of this vector. To avoid a sqrt call when strictly comparing distances, LengthSq can be used instead.

func (Vec3) LengthSq

func (a Vec3) LengthSq() float64

LengthSq returns the magnitude squared of this vector, useful for comparing distances.

func (Vec3) Lerp

func (a Vec3) Lerp(b Vec3, t float64) Vec3

Lerp returns a vector representing the linear interpolation between the vectors a and b. The t parameter is the amount to interpolate (0.0 - 1.0) between the vectors.

func (Vec3) Less

func (a Vec3) Less(b Vec3) bool

Less tells if a is componentwise less than b:

return a.X < b.X && a.Y < b.Y && a.Z < b.Z

func (Vec3) Max

func (a Vec3) Max(b Vec3) Vec3

Max returns a vector representing the largest components of both the vectors.

func (Vec3) Min

func (a Vec3) Min(b Vec3) Vec3

Min returns a vector representing the smallest components of both the vectors.

func (Vec3) Mul

func (a Vec3) Mul(b Vec3) Vec3

Mul performs a componentwise multiplication of the two vectors, returning a * b.

func (Vec3) MulScalar

func (a Vec3) MulScalar(b float64) Vec3

MulScalar performs a componentwise scalar multiplication of a * b.

func (Vec3) Normalized

func (a Vec3) Normalized() (v Vec3, ok bool)

Normalized returns the normalized (i.e. length/magnitude == 1) vector of a. If the vector's length is zero (and division by zero would occur) then [Vec3Zero, false] is returned.

func (Vec3) Proj

func (a Vec3) Proj(b Vec3) Vec3

Proj returns a vector representing the projection of vector a onto b.

func (Vec3) Radians

func (a Vec3) Radians() Vec3

Radians converts each value in the vector from degrees to radians and returns it.

func (Vec3) Rounded

func (a Vec3) Rounded() Vec3

Rounded rounds each value in the vector to the nearest whole number and returns it.

func (Vec3) SignedAngle

func (a Vec3) SignedAngle(b, reference Vec3) float64

SignedAngle returns the signed angle between the vectors a and b, in radians. The returned angle is positive if the rotation from a to b is clockwise when looking in the direction of the reference vector.

func (Vec3) String

func (a Vec3) String() string

String returns an string representation of this vector.

func (Vec3) Sub

func (a Vec3) Sub(b Vec3) Vec3

Sub performs a componentwise subtraction of the two vectors, returning a - b.

func (Vec3) SubScalar

func (a Vec3) SubScalar(b float64) Vec3

SubScalar performs a componentwise scalar subtraction of a - b.

func (Vec3) TransformGeneralMat3

func (a Vec3) TransformGeneralMat3(b Mat3) Vec3

TransformGeneralMat3 transforms this vector by the matrix (vector * matrix) without translation component, and returns the result, as a fully general operation.

func (Vec3) TransformGeneralMat4

func (a Vec3) TransformGeneralMat4(b Mat4) Vec3

TransformGeneralMat4 transforms this vector by the matrix (vector * matrix) without translation component, and returns the result, as a fully general operation.

func (Vec3) TransformMat3

func (a Vec3) TransformMat3(b Mat3) Vec3

TransformMat3 transforms this point vector by the matrix (vector * matrix), and returns the result. Can operate on orthonormal transformation matrices.

func (Vec3) TransformMat4

func (a Vec3) TransformMat4(b Mat4) Vec3

TransformMat4 transforms this point vector by the affine transformation matrix (vector * matrix) and returns the result. The matrix parameter must be an affine transformation matrix.

func (Vec3) TransformVecMat4

func (a Vec3) TransformVecMat4(b Mat4) Vec3

TransformVecMat4 transforms this vector (without translation component) by the orthonormal matrix and returns the result.

func (Vec3) XyzToHpr

func (v Vec3) XyzToHpr() Vec3

XyzToHpr converts X, Y, and Z axis rotation to Hew, Pitch, and Roll rotation.

type Vec4

type Vec4 struct {
	X, Y, Z, W float64
}

Vec4 represents a four component vector.

func (Vec4) Add

func (a Vec4) Add(b Vec4) Vec4

Add performs a componentwise addition of the two vectors, returning a + b.

func (Vec4) AddScalar

func (a Vec4) AddScalar(b float64) Vec4

AddScalar performs a componentwise scalar addition of a + b.

func (Vec4) AlmostEquals

func (a Vec4) AlmostEquals(b Vec4, epsilon float64) bool

AlmostEquals tells if a == b using the specified epsilon value.

func (Vec4) AnyGreater

func (a Vec4) AnyGreater(b Vec4) bool

AnyGreater tells if a is componentwise any greater than b:

return a.X > b.X || a.Y > b.Y || a.Z > b.Z || a.W > b.W

func (Vec4) AnyLess

func (a Vec4) AnyLess(b Vec4) bool

AnyLess tells if a is componentwise any less than b:

return a.X < b.X || a.Y < b.Y || a.Z < b.Z || a.W < b.W

func (Vec4) Clamp

func (a Vec4) Clamp(min, max float64) Vec4

Clamp clamps each value in the vector to the range of [min, max] and returns it.

func (Vec4) Degrees

func (a Vec4) Degrees() Vec4

Degrees converts each value in the vector from radians to degrees and returns it.

func (Vec4) Div

func (a Vec4) Div(b Vec4) Vec4

Div performs a componentwise division of the two vectors, returning a * b.

func (Vec4) DivScalar

func (a Vec4) DivScalar(b float64) Vec4

DivScalar performs a componentwise scalar division of a * b.

func (Vec4) Dot

func (a Vec4) Dot(b Vec4) float64

Dot returns the dot product of a and b.

func (Vec4) Equals

func (a Vec4) Equals(b Vec4) bool

Equals tells if a == b using the default EPSILON value.

func (Vec4) Greater

func (a Vec4) Greater(b Vec4) bool

Greater tells if a is componentwise greater than b:

return a.X > b.X && a.Y > b.Y

func (Vec4) IsNaN

func (a Vec4) IsNaN() bool

IsNaN tells if any components of this vector are not an number.

func (Vec4) Length

func (a Vec4) Length() float64

Length returns the magnitude of this vector. To avoid a sqrt call when strictly comparing distances, LengthSq can be used instead.

func (Vec4) LengthSq

func (a Vec4) LengthSq() float64

LengthSq returns the magnitude squared of this vector, useful for comparing distances.

func (Vec4) Lerp

func (a Vec4) Lerp(b Vec4, t float64) Vec4

Lerp returns a vector representing the linear interpolation between the vectors a and b. The t parameter is the amount to interpolate (0.0 - 1.0) between the vectors.

func (Vec4) Less

func (a Vec4) Less(b Vec4) bool

Less tells if a is componentwise less than b:

return a.X < b.X && a.Y < b.Y

func (Vec4) Max

func (a Vec4) Max(b Vec4) Vec4

Max returns a vector representing the largest components of both the vectors.

func (Vec4) Min

func (a Vec4) Min(b Vec4) Vec4

Min returns a vector representing the smallest components of both the vectors.

func (Vec4) Mul

func (a Vec4) Mul(b Vec4) Vec4

Mul performs a componentwise multiplication of the two vectors, returning a * b.

func (Vec4) MulScalar

func (a Vec4) MulScalar(b float64) Vec4

MulScalar performs a componentwise scalar multiplication of a * b.

func (Vec4) Normalized

func (a Vec4) Normalized() (v Vec4, ok bool)

Normalized returns the normalized (i.e. length/magnitude == 1) vector of a. If the vector's length is zero (and division by zero would occur) then [Vec4Zero, false] is returned.

func (Vec4) Proj

func (a Vec4) Proj(b Vec4) Vec4

Proj returns a vector representing the projection of vector a onto b.

func (Vec4) Quat

func (a Vec4) Quat() Quat

Quat converts this vector to a quaternion. It's short-hand for:

Quat{a.X, a.Y, a.Z, a.W}

func (Vec4) Radians

func (a Vec4) Radians() Vec4

Radians converts each value in the vector from degrees to radians and returns it.

func (Vec4) Rounded

func (a Vec4) Rounded() Vec4

Rounded rounds each value in the vector to the nearest whole number and returns it.

func (Vec4) String

func (a Vec4) String() string

String returns an string representation of this vector.

func (Vec4) Sub

func (a Vec4) Sub(b Vec4) Vec4

Sub performs a componentwise subtraction of the two vectors, returning a - b.

func (Vec4) SubScalar

func (a Vec4) SubScalar(b float64) Vec4

SubScalar performs a componentwise scalar subtraction of a - b.

func (Vec4) Transform

func (a Vec4) Transform(b Mat4) Vec4

Transform transforms this vector by the matrix (vector * matrix) and returns the result. This is an fully general operation.

func (Vec4) Vec3

func (a Vec4) Vec3() Vec3

Vec3 converts this four-component vector to a three-component one. It's short-hand for:

Vec3{a.X, a.Y, a.Z}

Jump to

Keyboard shortcuts

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