r3

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2022 License: BSD-3-Clause Imports: 5 Imported by: 108

Documentation

Overview

Package r3 provides 3D vectors and boxes and operations on them.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cos added in v0.8.0

func Cos(p, q Vec) float64

Cos returns the cosine of the opening angle between p and q.

func Dot added in v0.9.0

func Dot(p, q Vec) float64

Dot returns the dot product p·q.

func Norm added in v0.8.0

func Norm(p Vec) float64

Norm returns the Euclidean norm of p

|p| = sqrt(p_x^2 + p_y^2 + p_z^2).

func Norm2 added in v0.8.0

func Norm2(p Vec) float64

Norm2 returns the Euclidean squared norm of p

|p|^2 = p_x^2 + p_y^2 + p_z^2.

Types

type Box

type Box struct {
	Min, Max Vec
}

Box is a 3D bounding box.

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 Eye added in v0.11.0

func Eye() *Mat

Eye returns the 3×3 Identity matrix

func NewMat added in v0.11.0

func NewMat(val []float64) *Mat

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

func Skew(v Vec) (M *Mat)

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

func (m *Mat) Add(a, b mat.Matrix)

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

func (m *Mat) At(i, j int) float64

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

func (m *Mat) CloneFrom(a mat.Matrix)

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

func (m *Mat) Det() float64

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

func (m *Mat) Dims() (r, c int)

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

func (m *Mat) Mul(a, b mat.Matrix)

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) MulVec added in v0.11.0

func (m *Mat) MulVec(v Vec) Vec

MulVec returns the matrix-vector product M⋅v.

func (*Mat) MulVecTrans added in v0.11.0

func (m *Mat) MulVecTrans(v Vec) Vec

MulVecTrans returns the matrix-vector product Mᵀ⋅v.

func (*Mat) Outer added in v0.11.0

func (m *Mat) Outer(alpha float64, x, y Vec)

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

func (m *Mat) RawMatrix() blas64.General

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

func (m *Mat) Scale(f float64, a mat.Matrix)

Scale multiplies the elements of a by f, placing the result in the receiver.

See the mat.Scaler interface for more information.

func (*Mat) Set added in v0.11.0

func (m *Mat) Set(i, j int, v float64)

Set sets the element at row i, column j to the value v.

func (*Mat) Sub added in v0.11.0

func (m *Mat) Sub(a, b mat.Matrix)

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

func (m *Mat) T() mat.Matrix

T returns the transpose of Mat. Changes in the receiver will be reflected in the returned matrix.

func (*Mat) VecCol added in v0.11.0

func (m *Mat) VecCol(j int) Vec

VecCol returns the elements in the jth column of the receiver.

func (*Mat) VecRow added in v0.11.0

func (m *Mat) VecRow(i int) Vec

VecRow returns the elements in the ith row of the receiver.

type Rotation added in v0.9.0

type Rotation quat.Number

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

func NewRotation(alpha float64, axis Vec) Rotation

NewRotation creates a rotation by alpha, around axis.

func (Rotation) Mat added in v0.11.0

func (r Rotation) Mat() *Mat

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.

func (Rotation) Rotate added in v0.9.0

func (r Rotation) Rotate(p Vec) Vec

Rotate returns p rotated according to the parameters used to construct the receiver.

type Vec

type Vec struct {
	X, Y, Z float64
}

Vec is a 3D vector.

func Add added in v0.9.0

func Add(p, q Vec) Vec

Add returns the vector sum of p and q.

func Cross added in v0.9.0

func Cross(p, q Vec) Vec

Cross returns the cross product p×q.

func Rotate added in v0.9.0

func Rotate(p Vec, alpha float64, axis Vec) Vec

Rotate returns a new vector, rotated by alpha around the provided axis.

func Scale added in v0.9.0

func Scale(f float64, p Vec) Vec

Scale returns the vector p scaled by f.

func Sub added in v0.9.0

func Sub(p, q Vec) Vec

Sub returns the vector sum of p and -q.

func Unit added in v0.8.0

func Unit(p Vec) Vec

Unit returns the unit vector colinear to p. Unit returns {NaN,NaN,NaN} for the zero vector.

func (Vec) Add

func (p Vec) Add(q Vec) Vec

Add returns the vector sum of p and q.

DEPRECATED: use r3.Add.

func (Vec) Cross added in v0.8.0

func (p Vec) Cross(q Vec) Vec

Cross returns the cross product p×q.

DEPRECATED: use r3.Cross.

func (Vec) Dot added in v0.8.0

func (p Vec) Dot(q Vec) float64

Dot returns the dot product p·q.

DEPRECATED: use r3.Dot.

func (Vec) Rotate added in v0.9.0

func (p Vec) Rotate(alpha float64, axis Vec) Vec

Rotate returns a new vector, rotated by alpha around the provided axis.

DEPRECATED: use r3.Rotate

func (Vec) Scale

func (p Vec) Scale(f float64) Vec

Scale returns the vector p scaled by f.

DEPRECATED: use r3.Scale.

func (Vec) Sub

func (p Vec) Sub(q Vec) Vec

Sub returns the vector sum of p and -q.

DEPRECATED: use r3.Sub.

Jump to

Keyboard shortcuts

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