Documentation
¶
Overview ¶
Package r3 provides 3D vectors and boxes and operations on them.
Index ¶
- func Cos(p, q Vec) float64
- func Dot(p, q Vec) float64
- func Norm(p Vec) float64
- func Norm2(p Vec) float64
- type Box
- type Mat
- func (m *Mat) Add(a, b mat.Matrix)
- func (m *Mat) At(i, j int) float64
- func (m *Mat) CloneFrom(a mat.Matrix)
- func (m *Mat) Det() float64
- func (m *Mat) Dims() (r, c int)
- func (m *Mat) Mul(a, b mat.Matrix)
- func (m *Mat) MulVec(v Vec) Vec
- func (m *Mat) MulVecTrans(v Vec) Vec
- func (m *Mat) Outer(alpha float64, x, y Vec)
- func (m *Mat) RawMatrix() blas64.General
- func (m *Mat) Scale(f float64, a mat.Matrix)
- func (m *Mat) Set(i, j int, v float64)
- func (m *Mat) Sub(a, b mat.Matrix)
- func (m *Mat) T() mat.Matrix
- func (m *Mat) VecCol(j int) Vec
- func (m *Mat) VecRow(i int) Vec
- type Rotation
- type Vec
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Mat ¶ added in v0.11.0
type Mat struct {
// contains filtered or unexported fields
}
Mat represents a 3×3 matrix. Useful for rotation matrices and such. The zero value is usable as the 3×3 zero matrix.
func NewMat ¶ added in v0.11.0
NewMat returns a new 3×3 matrix Mat type and populates its elements with values passed as argument in row-major form. If val argument is nil then NewMat returns a matrix filled with zeros.
func Skew ¶ added in v0.11.0
Skew returns the 3×3 skew symmetric matrix (right hand system) of v.
⎡ 0 -z y⎤ Skew({x,y,z}) = ⎢ z 0 -x⎥ ⎣-y x 0⎦
func (*Mat) Add ¶ added in v0.11.0
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) At ¶ added in v0.11.0
At returns the value of a matrix element at row i, column j. At expects indices in the range [0,2]. It will panic if i or j are out of bounds for the matrix.
func (*Mat) CloneFrom ¶ added in v0.11.0
CloneFrom makes a copy of a into the receiver m. Mat expects a 3×3 input matrix.
func (*Mat) Det ¶ added in v0.11.0
Det calculates the determinant of the receiver using the following formula
⎡a b c⎤ m = ⎢d e f⎥ ⎣g h i⎦ det(m) = a(ei − fh) − b(di − fg) + c(dh − eg)
func (*Mat) Dims ¶ added in v0.11.0
Dims returns the number of rows and columns of this matrix. This method will always return 3×3 for a Mat.
func (*Mat) Mul ¶ added in v0.11.0
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) MulVecTrans ¶ added in v0.11.0
MulVecTrans returns the matrix-vector product Mᵀ⋅v.
func (*Mat) Outer ¶ added in v0.11.0
Outer calculates the outer product of the vectors x and y, where x and y are treated as column vectors, and stores the result in the receiver.
m = alpha * x * yᵀ
func (*Mat) RawMatrix ¶ added in v0.11.0
RawMatrix returns the blas representation of the matrix with the backing data of this matrix. Changes to the returned matrix will be reflected in the receiver.
func (*Mat) Scale ¶ added in v0.11.0
Scale multiplies the elements of a by f, placing the result in the receiver.
See the mat.Scaler interface for more information.
func (*Mat) Sub ¶ added in v0.11.0
Sub subtracts the matrix b from a, placing the result in the receiver. Sub will panic if the two matrices do not have the same shape.
func (*Mat) T ¶ added in v0.11.0
T returns the transpose of Mat. Changes in the receiver will be reflected in the returned matrix.
type Rotation ¶ added in v0.9.0
Rotation describes a rotation in space.
Example (EulerAngles) ¶
package main import ( "fmt" "math" "gonum.org/v1/gonum/num/quat" "gonum.org/v1/gonum/spatial/r3" ) // euler returns an r3.Rotation that corresponds to the Euler // angles alpha, beta and gamma which are rotations around the x, // y and z axes respectively. The order of rotations is x, y, z; // there are many conventions for this ordering. func euler(alpha, beta, gamma float64) r3.Rotation { // Note that this function can be algebraically simplified // to reduce floating point operations, but is left in this // form for clarity. var rot1, rot2, rot3 quat.Number rot1.Imag, rot1.Real = math.Sincos(alpha / 2) // x-axis rotation rot2.Jmag, rot2.Real = math.Sincos(beta / 2) // y-axis rotation rot3.Kmag, rot3.Real = math.Sincos(gamma / 2) // z-axis rotation return r3.Rotation(quat.Mul(rot3, quat.Mul(rot2, rot1))) // order of rotations } func main() { // It is possible to interconvert between the quaternion representation // of a rotation and Euler angles, but this leads to problems. // // The first of these is that there are a variety of conventions for // application of the rotations. // // The more serious consequence of using Euler angles is that it is // possible to put the rotation system into a singularity which results // in loss of degrees of freedom and so causes gimbal lock. This happens // when the second axis to be rotated around is rotated to 𝝿/2. // // See https://en.wikipedia.org/wiki/Euler_angles for more details. pt := r3.Vec{1, 0, 0} // For the Euler conversion function in this example, the second rotation // is around the y-axis. const singularY = math.Pi / 2 arb := math.Pi / 4 fmt.Printf("rotate around x-axis: %.2f\n", euler(arb, 0, 0).Rotate(pt)) fmt.Printf("rotate around y-axis: %.2f\n", euler(0, arb, 0).Rotate(pt)) fmt.Printf("rotate around z-axis: %.2f\n", euler(0, 0, arb).Rotate(pt)) fmt.Printf("rotate around x+y-axes: %.2f\n", euler(arb, arb, 0).Rotate(pt)) fmt.Printf("rotate around x+z-axes: %.2f\n", euler(arb, 0, arb).Rotate(pt)) fmt.Printf("rotate around y+z-axes: %.2f\n", euler(0, arb, arb).Rotate(pt)) fmt.Printf("rotate around y-axis to singularity: %.2f\n", euler(0, singularY, 0).Rotate(pt)) fmt.Printf("rotate around x+y-axes with singularity → gimbal lock: %.2f\n", euler(arb, singularY, 0).Rotate(pt)) fmt.Printf("rotate around z+y-axes with singularity → gimbal lock: %.2f\n", euler(0, singularY, arb).Rotate(pt)) fmt.Printf("rotate around all-axes with singularity → gimbal lock: %.2f\n", euler(arb, singularY, arb).Rotate(pt)) }
Output: rotate around x-axis: {1.00 0.00 0.00} rotate around y-axis: {0.71 0.00 -0.71} rotate around z-axis: {0.71 0.71 0.00} rotate around x+y-axes: {0.71 0.00 -0.71} rotate around x+z-axes: {0.71 0.71 0.00} rotate around y+z-axes: {0.50 0.50 -0.71} rotate around y-axis to singularity: {0.00 0.00 -1.00} rotate around x+y-axes with singularity → gimbal lock: {0.00 0.00 -1.00} rotate around z+y-axes with singularity → gimbal lock: {0.00 0.00 -1.00} rotate around all-axes with singularity → gimbal lock: {0.00 0.00 -1.00}
func NewRotation ¶ added in v0.9.0
NewRotation creates a rotation by alpha, around axis.
func (Rotation) Mat ¶ added in v0.11.0
Mat returns a 3×3 rotation matrix corresponding to the receiver. It may be used to perform rotations on a 3-vector or to apply the rotation to a 3×n matrix of column vectors. If the receiver is not a unit quaternion, the returned matrix will not be a pure rotation.
type Vec ¶
type Vec struct {
X, Y, Z float64
}
Vec is a 3D vector.
func Rotate ¶ added in v0.9.0
Rotate returns a new vector, rotated by alpha around the provided axis.
func Unit ¶ added in v0.8.0
Unit returns the unit vector colinear to p. Unit returns {NaN,NaN,NaN} for the zero vector.
func (Vec) Rotate ¶ added in v0.9.0
Rotate returns a new vector, rotated by alpha around the provided axis.
DEPRECATED: use r3.Rotate