pose

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package pose is the orientation arithmetic an XR viewer needs: quaternions, the Euler conventions headsets actually report, recentring, and smoothing.

It knows nothing about devices or rendering. That is the point — orientation is where sign and axis-order mistakes hide, and they hide best when the only way to exercise the code is to put a headset on. Everything here is pure and tested against known rotations, so a wrong axis fails a test instead of making the horizon tilt.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Euler

type Euler struct{ Roll, Pitch, Yaw float64 }

Euler is an orientation as three angles in DEGREES, the unit head trackers report. Yaw turns the head left/right, pitch tips it up/down, roll tilts it.

type Quat

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

Quat is a rotation as a unit quaternion. The zero value is not a rotation; use Identity.

func FromEulerZXY

func FromEulerZXY(e Euler) Quat

FromEulerZXY builds a rotation applying ROLL about Z first, then pitch about X, then yaw about Y -- the Z, X, Y of the name is the order the rotations are applied in, giving R = Ry(yaw) . Rx(pitch) . Rz(roll).

The order is not a detail to pick by taste, and getting it backwards is not a subtle error. Yaw must be applied LAST, about the global up axis, or it stops being a horizontal turn: compose it first instead and pitching to 90 degrees no longer looks straight up, so the horizon swings as the viewer raises their head. This is the convention head trackers report in, and the one that makes pitch = 90 degrees the degenerate case rather than an arbitrary direction.

func Identity

func Identity() Quat

Identity is the null rotation.

func Slerp

func Slerp(q, r Quat, t float64) Quat

Slerp interpolates along the shortest arc from q to r, with t clamped to [0,1]. Taking the shortest arc matters: a quaternion and its negation are the same rotation, so interpolating without the sign check can travel the long way round and spin the view through 300° to reach a neighbouring angle.

func (Quat) Angle

func (q Quat) Angle() float64

Angle returns the rotation's magnitude in radians, in [0, π].

func (Quat) Conj

func (q Quat) Conj() Quat

Conj is the conjugate, which for a unit quaternion is the inverse rotation.

func (Quat) EulerZXY

func (q Quat) EulerZXY() Euler

EulerZXY decomposes a rotation back into the same convention FromEulerZXY builds from. Pitch is clamped to ±90°, where yaw and roll become degenerate (gimbal lock): there the decomposition puts the whole remaining rotation into yaw and leaves roll at zero, which is a choice, not a recovery of information the orientation no longer distinguishes.

func (Quat) Len

func (q Quat) Len() float64

Len is the quaternion's norm.

func (Quat) Mul

func (q Quat) Mul(r Quat) Quat

Mul composes rotations: q.Mul(r) applies r first, then q — the same order as matrix multiplication, so a reader familiar with either is not surprised.

func (Quat) Rotate

func (q Quat) Rotate(v Vec3) Vec3

Rotate applies the rotation to v.

func (Quat) Unit

func (q Quat) Unit() Quat

Unit renormalises. Repeated composition drifts off the unit sphere, and a non-unit quaternion scales what it rotates, so anything long-lived should be renormalised. A zero quaternion cannot be normalised and yields Identity rather than NaNs.

type Recentre

type Recentre struct {
	// contains filtered or unexported fields
}

Recentre makes one orientation the new "straight ahead". A viewer sits how they like, presses recentre, and the content is in front of them.

func NewRecentre

func NewRecentre() *Recentre

NewRecentre starts with no offset, so Apply is the identity.

func (*Recentre) Apply

func (r *Recentre) Apply(q Quat) Quat

Apply expresses q relative to the reference.

func (*Recentre) Reference

func (r *Recentre) Reference() Quat

Reference returns the current reference orientation.

func (*Recentre) Set

func (r *Recentre) Set(q Quat)

Set makes q the reference: Apply(q) then returns Identity.

type Smoother

type Smoother struct {
	// Alpha is how much of each new sample is taken, in (0,1]. 1 is no
	// smoothing; smaller is smoother and lags more. Values outside the range are
	// clamped, so a zero value means "no smoothing" rather than "freeze".
	Alpha float64
	// contains filtered or unexported fields
}

Smoother low-pass filters a stream of orientations. A head tracker's output is noisy at rest, and that noise is visible as a shimmer in a magnified view.

func (*Smoother) Current

func (s *Smoother) Current() (Quat, bool)

Current returns the last smoothed value, and whether any sample has arrived.

func (*Smoother) Reset

func (s *Smoother) Reset()

Reset forgets the history, so the next Update is adopted as-is.

func (*Smoother) Update

func (s *Smoother) Update(q Quat) Quat

Update folds in a new sample and returns the smoothed orientation. The first sample is adopted as-is: easing in from an arbitrary starting orientation would swing the view on the first frame.

type Vec3

type Vec3 struct{ X, Y, Z float64 }

Vec3 is a vector in a right-handed space: +X right, +Y up, +Z towards the viewer, which is the convention OpenGL, OpenXR and this package share. A viewer looking straight ahead looks down -Z.

func (Vec3) Add

func (v Vec3) Add(w Vec3) Vec3

Add, Sub and Scale are the vector arithmetic the projection code needs.

func (Vec3) Dot

func (v Vec3) Dot(w Vec3) float64

Dot is the scalar product.

func (Vec3) Len

func (v Vec3) Len() float64

Len is the Euclidean length.

func (Vec3) Scale

func (v Vec3) Scale(s float64) Vec3

func (Vec3) Sub

func (v Vec3) Sub(w Vec3) Vec3

func (Vec3) Unit

func (v Vec3) Unit() Vec3

Unit returns v scaled to length 1. The zero vector has no direction, and is returned unchanged rather than producing NaNs that would propagate silently into a sampling coordinate.

Jump to

Keyboard shortcuts

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