quaternion
This manual documents the v0.2.1 release of Luna-Flow/quaternion.
Overview
quaternion is the Luna-Flow library for Hamilton’s quaternions : the four-dimensional number system in which . Unit quaternions describe 3D rotations without the singularities of Euler angles, and they compose, invert and interpolate cheaply. The library provides one generic type, Quaternion[T], with ring arithmetic, right and left division, norms, rotation of vectors, spherical interpolation and Euler-angle conversion.
The 0.2.0 release centers on three changes:
q / ris right division ; the old left quotient is nowQuaternion::left_div.Quaternion[T]implements luna-generic’sRing(and the traits below it), but notField, because multiplication is not commutative.- The package builds with MoonBit 0.10 and promotes trait methods explicitly.
Install
moon add Luna-Flow/quaternion@0.2.1
Then import "Luna-Flow/quaternion" in your moon.pkg. The package needs the MoonBit toolchain 0.10.20 or later (moonc ≥ 0.10.20) and depends on Luna-Flow/luna-generic 0.3.3. The code is target-independent and is tested on the wasm, wasm-gc, js and native backends.
Pages
The repository is one MoonBit package at src, documented as core. Its sources are src/quaternion.mbt (the type and all operations), src/double_conv.mbt (DoubleConvert) and src/extends.mbt (which trait methods are callable as methods). The generated interface src/pkg.generated.mbti lists every public signature.
| Part | Tutorial | API | Design |
|---|---|---|---|
core: Quaternion[T], arithmetic, rotations, DoubleConvert | tutorial | API | design |
Exported type and trait
Quaternion[T]: the quaternion with components of typeT, abstract, with derivedEq,HashandDebugDoubleConvert: the bridge toDoubleused by every function that needs square roots or trigonometry; implemented forIntandDouble
Arithmetic
- Construction:
Quaternion::from_vec,id,Quaternion::zero,Quaternion::one - Operators:
+,-, unary-,*(the Hamilton product, not commutative),/(right division) Quaternion::left_div,Quaternion::scale,Quaternion::conjugate,Quaternion::inv- Powers:
Quaternion::pow_by_int,Quaternion::pow_by_T - Norms:
Quaternion::dot,Quaternion::square_len,Quaternion::magnitude,Quaternion::normalize - luna-generic instances:
Zero,One,AddMonoid,MulMonoid,Semiring,Ring,Inverse,Conjugate
Rotations
from_axis_angle,from_euler: build a unit quaternionQuaternion::rotate: apply it to a vectorslerp: interpolate between two orientationsQuaternion::to_eulerand its eight fixed-order variants: decompose into Euler angles
Several Euler-angle orders, the gimbal-lock branch and pow_by_T have known issues; the API marks each with a warning.
Where to read next
The core tutorial rotates, composes, compares and interpolates orientations. The core API lists every function with its exact signature, conventions and edge cases, and the core design derives the Hamilton product, the rotation formula, division, slerp and the Euler-angle extraction from Hamilton’s relations.
- New to quaternions: read the tutorial from the quick start to “Compare two rotations”. It needs no mathematics beyond vectors and angles.
- Using it in a library: keep the API at hand, and check the Euler-angle conventions and the warnings there before converting angles.
- Contributing: read the design, in particular its list of known deviations, before changing an operation.
Validation
Recommended release checks, from the repository root:
moon check --target all
moon test --target all
moon info
moon info regenerates src/pkg.generated.mbti; a change in that file is a change of the public API.