Documentation
¶
Overview ¶
package ms3 is a 32bit 3D math package based around the Vec type.
This is a different take compared to mgl32, a package that treats all data equally by giving them their own namespace with methods. This package is more similar to gonum's take on spatial operations where package-level functions like `Add` are reserved specifically for vector operations, which tend to be the most common. This aids in readability since long string of operations using methods can be remarkably hard to follow.
The name roughly stands for (m)ath for (s)hort floats in (3)D. "short" since there are no native 16 bit floats in Go.
Index ¶
- func Cos(p, q Vec) float32
- func Divergence(p, step Vec, field func(Vec) Vec) float32
- func Dot(p, q Vec) float32
- func Norm(p Vec) float32
- func Norm2(p Vec) float32
- func Sort(a, b, c float32) (l1, l2, l3 float32)
- type Box
- type Mat
- func (m *Mat) Add(A, B *Mat)
- func (m *Mat) At(i, j int) float32
- func (m *Mat) Dims() (r, c int)
- func (m *Mat) Mul(a, b *Mat)
- func (m *Mat) MulVec(v Vec) (result Vec)
- func (m *Mat) MulVecTrans(v Vec) (result Vec)
- func (m *Mat) Prod(v1, v2t Vec)
- func (m *Mat) RotationFromQuat(q Quat)
- func (m *Mat) Scale(f float32, a *Mat)
- func (m *Mat) Set(i, j int, v float32)
- func (m *Mat) Skew(v Vec)
- func (m Mat) String() string
- func (m *Mat) Sub(a, b *Mat)
- func (m *Mat) VecCol(j int) Vec
- func (m *Mat) VecRow(i int) Vec
- type Quat
- func AnglesToQuat(angle1, angle2, angle3 float32, order RotationOrder) Quat
- func QuatBetweenVectors(start, dest Vec) Quat
- func QuatIdent() Quat
- func QuatLerp(q1, q2 Quat, amount float32) Quat
- func QuatLookAtV(eye, center, up Vec) Quat
- func QuatNlerp(q1, q2 Quat, amount float32) Quat
- func QuatRotate(angle float32, axis Vec) Quat
- func QuatSlerp(q1, q2 Quat, amount float32) Quat
- func (q1 Quat) Add(q2 Quat) Quat
- func (q1 Quat) Conjugate() Quat
- func (q1 Quat) Dot(q2 Quat) float32
- func (q1 Quat) Inverse() Quat
- func (q1 Quat) Mul(q2 Quat) Quat
- func (q1 Quat) Norm() float32
- func (q1 Quat) Rotate(v Vec) Vec
- func (q1 Quat) Scale(c float32) Quat
- func (q1 Quat) Sub(q2 Quat) Quat
- func (q1 Quat) Unit() Quat
- type RotationOrder
- type Triangle
- type Vec
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Divergence ¶
Divergence returns the divergence of the vector field at the point p, approximated using finite differences with the given step sizes.
Types ¶
type Box ¶
type Box struct {
Min, Max Vec
}
Box is a 3D bounding box. Well formed Boxes Min components are smaller than Max components.
func NewBox ¶
NewBox is shorthand for Box{Min:Vec{x0,y0,z0}, Max:Vec{x1,y1,z1}}. The sides are swapped so that the resulting Box is well formed.
func (Box) Add ¶
Add adds v to the bounding box components. It is the equivalent of translating the Box by v.
func (Box) Canon ¶
Canon returns the canonical version of a. The returned Box has minimum and maximum coordinates swapped if necessary so that it is well-formed.
func (Box) Empty ¶
IsEmpty returns true if a Box's volume is zero or if a Min component is greater than its Max component.
func (Box) Scale ¶
Scale returns a new Box scaled by a size vector around its center. The scaling is done element wise which is to say the Box's X dimension is scaled by scale.X. Negative elements of scale are interpreted as zero.
func (Box) Vertices ¶
Vertices returns a slice of the 8 vertices corresponding to each of the Box's corners.
Ordering of vertices 0-3 is CCW in the XY plane starting at box minimum. Ordering of vertices 4-7 is CCW in the XY plane starting at box minimum for X and Y values and maximum Z value.
Edges for the box can be constructed with the following indices:
edges := [12][2]int{ {0, 1}, {1, 2}, {2, 3}, {3, 0}, {4, 5}, {5, 6}, {6, 7}, {7, 4}, {0, 4}, {1, 5}, {2, 6}, {3, 7}, }
type Mat ¶
type Mat [3 * 3]float32
func (*Mat) Add ¶
Add adds a and b element-wise, placing the result in the receiver. Add will panic if the two matrices do not have the same shape.
func (*Mat) Mul ¶
Mul takes the matrix product of a and b, placing the result in the receiver. If the number of columns in a does not equal 3, Mul will panic.
func (*Mat) RotationFromQuat ¶
RotationFromQuat stores the rotation matrix from a quaternion (must be normalized).
[R] = q.Real*q.Real * [E] - dot([q],[q])*[E] + 2*[q]*[q]ᵀ + 2*q.Real * skew([q])
where
[q] = {q.Imag, q.Jmag, q.Kmag}
and
[E]=Eye()
func (*Mat) Scale ¶
Scale multiplies the elements of a by f, placing the result in the receiver.
See the Scaler interface for more information.
func (*Mat) Skew ¶
Skew returns the skew symmetric matrix (right hand system) of v.
[0 -z y] Skew({x,y,z}) = [z 0 -x] [-y x 0]
type Quat ¶
Quat represents a Quaternion, which is an extension of the imaginary numbers; there's all sorts of interesting theory behind it. In 3D graphics we mostly use it as a cheap way of representing rotation since quaternions are cheaper to multiply by, and easier to interpolate than matrices.
A Quaternion has two parts: W, the so-called scalar component, and "V", the vector component. The vector component is considered to be the part in 3D space, while W (loosely interpreted) is its 4D coordinate.
The imaginary V part is guaranteed to have an offset of zero in the Quat struct:
unsafe.Offsetof(q.V) // == 0
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.
Based off the code for the Matlab function "angle2quat", though this implementation only supports 3 single angles as opposed to multiple angles.
func QuatBetweenVectors ¶
QuatBetweenVectors calculates the rotation between two vectors
func QuatIdent ¶
func QuatIdent() Quat
QuatIdent returns the quaternion identity: W=1; V=(0,0,0).
As with all identities, multiplying any quaternion by this will yield the same quaternion you started with.
func QuatLerp ¶
QuatLerp is a *L*inear Int*erp*olation between two Quaternions, cheap and simple.
Not excessively useful, but uses can be found.
func QuatLookAtV ¶
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 ¶
QuatNlerp is a *Normalized* *L*inear Int*erp*olation between two Quaternions. Cheaper than Slerp and usually just as good. This is literally Lerp with Normalize() called on it.
Unlike Slerp, constant velocity isn't maintained, but it's much faster and Nlerp(q1,q2) and Nlerp(q2,q1) return the same path. You should probably use this more often unless you're suffering from choppiness due to the non-constant velocity problem.
func QuatRotate ¶
QuatRotate creates an angle from an axis and an angle relative to that axis.
This is cheaper than HomogRotate3D.
func QuatSlerp ¶
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 ¶
Add adds two quaternions. It's no more complicated than adding their W and V components.
func (Quat) Conjugate ¶
Conjugate returns the conjugate of a quaternion. Equivalent to Quat{q1.W, q1.V.Mul(-1)}.
func (Quat) Inverse ¶
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) Mul ¶
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) Rotate ¶
Rotate a vector by the rotation this quaternion represents. This will result in a 3D vector. Strictly speaking, this is equivalent to q1.v.q* where the "."" is quaternion multiplication and v is interpreted as a quaternion with W 0 and V v. In code: q1.Mul(Quat{0,v}).Mul(q1.Conjugate()), and then retrieving the imaginary (vector) part.
In practice, we hand-compute this in the general case and simplify to save a few operations.
type RotationOrder ¶
type RotationOrder int
RotationOrder is the order in which rotations will be transformed for the purposes of AnglesToQuat.
const ( XYX RotationOrder = iota XYZ XZX XZY YXY YXZ YZY YZX ZYZ ZYX ZXZ ZXY )
The RotationOrder constants represent a series of rotations along the given axes for the use of AnglesToQuat.
type Triangle ¶
type Triangle [3]Vec
Triangle represents a triangle in 3D space and is composed by 3 vectors corresponding to the position of each of the vertices. Ordering of these vertices decides the "normal" direction. Inverting ordering of two vertices inverts the resulting direction.
func (Triangle) Centroid ¶
Centroid returns the intersection of the three medians of the triangle as a point in space.
func (Triangle) IsDegenerate ¶
IsDegenerate returns true if all of triangle's vertices are within tol distance of its longest side.
type Vec ¶
type Vec struct {
X, Y, Z float32
}
Vec is a 3D vector.
func Gradient ¶
Gradient returns the gradient of the scalar field at the point p, approximated using finite differences with the given step sizes.