Sign in

    quaternion

    MoonBit Quaternion Library

    quaternion
    math
    Download zip
    Author
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    2 days ago
    Downloads
    41

    Dependencies

    #quaternion

    Luna-Flow/quaternion is a MoonBit library for Hamilton's quaternions . Unit quaternions represent 3D rotations that compose, invert and interpolate cheaply and have no gimbal lock, and general quaternions form a non-commutative ring. The library provides one generic type, Quaternion[T], with ring arithmetic, right and left division, norms, vector rotation, spherical interpolation and Euler-angle conversion, and it plugs into the luna-generic algebraic traits.

    #Installation

    moon add Luna-Flow/quaternion@0.2.0

    Then import "Luna-Flow/quaternion" in your package's moon.pkg. The library requires MoonBit with moonc 0.10 or later and depends on Luna-Flow/luna-generic 0.4.0.

    #Example

    fn main {
    // a quarter turn around the z axis
    let q = @quaternion.from_axis_angle((0.0, 0.0, 1.0), @math.PI / 2.0)
    let (x, y, _) = q.rotate((1.0, 0.0, 0.0))
    println("\{x.abs() < 1.0e-12}, \{y}") // true, 1
    // half of the turn, by spherical interpolation
    let half = @quaternion.slerp(@quaternion.id(), q, 0.5)
    println(half) // 0.9238795325112868 + 0i + 0j + 0.3826834323650898k
    // `/` is right division: (q / r) * r == q
    let r = @quaternion.Quaternion::from_vec((1.0, 2.0, 3.0, 4.0))
    println(((q / r) * r - q).square_len() < 1.0e-24) // true
    }

    The example imports moonbitlang/core/math for @math.PI.

    #Package

    PackageImport pathContents
    core (src)Luna-Flow/quaternionQuaternion[T]: arithmetic, / (right division) and left_div, inv, conjugate, norms, rotate, slerp, from_axis_angle, Euler-angle conversion, DoubleConvert

    Quaternion[T] implements luna-generic's Zero, One, AddMonoid, MulMonoid, Semiring, Ring, Inverse and Conjugate. It deliberately does not implement Field: quaternion multiplication is not commutative.

    #Documentation

    The manual is published at https://lunaflow.cn/en/quaternion/ with Chinese and Japanese translations. Its English source is in doc/manual/: the tutorial, the API reference (with warnings for the known issues of the Euler-angle conversions and pow_by_T) and the design notes, which derive the Hamilton product, the rotation formula, slerp and the Euler-angle conventions. The public interface is src/pkg.generated.mbti, and changes between versions are in CHANGELOG.md.

    #Contributing

    Issues and pull requests are welcome at https://github.com/Luna-Flow/quaternion. Before opening a pull request, run:

    moon fmt moon check --target all moon test --target all moon info

    and commit the regenerated src/pkg.generated.mbti. Documentation follows the Luna-Flow documentation standard: edit the English pages in doc/manual/, then update the translation catalogs with lunadoc update.

    #License

    Apache-2.0. See LICENSE.

    DoubleConvert

    pub trait DoubleConvert {
    fn to_double(Self) -> Double
    fn from_double(Double) -> Self
    }

    Quaternion

    type Quaternion[T] derive(Eq, Hash,
    Debug
    )

    Quaternion struct with real component r and vector components vec
    impl AddMonoid for Quaternion[T]
    impl Conjugate for Quaternion[T]
    impl MulMonoid for Quaternion[T]
    impl One for Quaternion[T]
    impl Ring for Quaternion[T]
    impl Semiring for Quaternion[T]
    impl Zero for Quaternion[T]
    impl Add for Quaternion[T]
    impl Div for Quaternion[T]
    impl Mul for Quaternion[T]
    impl Neg for Quaternion[T]
    impl Show for Quaternion[T]
    impl Sub for Quaternion[T]

    Quaternion::add

    fn[T : Add] Quaternion::add(self : Quaternion[T], other : Quaternion[T]) -> Quaternion[T]

    Quaternion::conjugate

    fn[T : Neg] Quaternion::conjugate(self : Quaternion[T]) -> Quaternion[T]

    The quaternion conjugate r - xi - yj - zk.

    Quaternion::div

    fn[T : Add + Mul + Div + Sub] Quaternion::div(q : Quaternion[T], r : Quaternion[T]) -> Quaternion[T]

    Quaternion::dot

    fn[T : Mul + Add] Quaternion::dot(self : Quaternion[T], other : Quaternion[T]) -> T

    Computes the dot product of two quaternions.

    Quaternion::equal

    fn[T : Eq] Quaternion::equal(Quaternion[T], Quaternion[T]) -> Bool

    Quaternion::from_vec

    fn[T] Quaternion::from_vec(v : (T, T, T, T)) -> Quaternion[T]

    Creates a quaternion from a 4-component vector (scalar, i, j, k).

    Quaternion::hash

    fn[T : Hash] Quaternion::hash(self : Quaternion[T]) -> Int

    Quaternion::left_div

    fn[T : Add + Mul + Div + Sub] Quaternion::left_div(self : Quaternion[T], r : Quaternion[T]) -> Quaternion[T]

    Left division: q.left_div(r) == r.inv() * q, the solution x of r * x == q. Quaternion multiplication is not commutative, so this differs from q / r (right division) in general.

    Quaternion::map

    fn[T, U] Quaternion::map(self : Quaternion[T], f : (T) -> U) -> Quaternion[U]

    Applies a function to each component of the quaternion.

    Quaternion::mul

    fn[T : Mul + Sub + Add] Quaternion::mul(self : Quaternion[T], other : Quaternion[T]) -> Quaternion[T]

    Quaternion::neg

    fn[T : Neg] Quaternion::neg(self : Quaternion[T]) -> Quaternion[T]

    Quaternion::one

    Quaternion::one() is the multiplicative identity.

    Quaternion::pow_by_T

    If the exponent is of type Int, please use the pow_by_int function. Computes the power of a quaternion. Like q^(2.5)

    Quaternion::pow_by_int

    If the exponent isn't of type Int, please use the pow_by_T function to calculate. Computes the power of a quaternion. Like q^2

    Quaternion::scale

    fn[T : Mul] Quaternion::scale(self : Quaternion[T], s : T) -> Quaternion[T]

    Scales the quaternion by a scalar value.

    Quaternion::square_len

    fn[T : Mul + Add] Quaternion::square_len(self : Quaternion[T]) -> T

    Computes the squared length (norm) of the quaternion.

    Quaternion::sub

    fn[T : Neg + Add] Quaternion::sub(self : Quaternion[T], other : Quaternion[T]) -> Quaternion[T]

    Quaternion::to_euler

    Converts the quaternion to Euler angles in different Orders, all measured in radians. The k-th returned angle is the rotation around the k-th axis of order. 'order' in {"XYZ", "XZY", "YZX", "ZYX"} 'external' is 'true', Result will be External rotation. Otherwise, Result will be Internal rotation

    Quaternion::to_euler_external_XYZ

    Converts the quaternion to Euler angles (roll, pitch, yaw) in external XYZ-Order

    Quaternion::to_euler_external_XZY

    Converts the quaternion to Euler angles (roll, pitch, yaw) in external XZY-Order (active rotation)

    Quaternion::to_euler_external_YZX

    Converts the quaternion to Euler angles (roll, pitch, yaw) in external YZX-Order

    Quaternion::to_euler_external_ZYX

    Converts the quaternion to Euler angles (roll, pitch, yaw) in external ZYX-Order

    Quaternion::to_euler_internal_XYZ

    Converts the quaternion to Euler angles (roll, pitch, yaw) in internal XYZ-Order

    Quaternion::to_euler_internal_XZY

    Converts the quaternion to Euler angles (roll, pitch, yaw) in internal XZY-Order

    Quaternion::to_euler_internal_YZX

    Converts the quaternion to Euler angles (roll, pitch, yaw) in internal YZX-Order

    Quaternion::to_euler_internal_ZYX

    Converts the quaternion to Euler angles (roll, pitch, yaw) in internal ZYX-Order

    Quaternion::to_string

    fn[T : Show] Quaternion::to_string(self : Quaternion[T]) -> String

    a + bi + cj + dk is the canonical text form of a quaternion.

    Quaternion::zero

    Quaternion::zero() is the additive identity.

    from_axis_angle

    fn[T : DoubleConvert + Mul + Add + Div] from_axis_angle(axis : (T, T, T), angle : T) -> Quaternion[T]

    Creates a quaternion from an axis and angle.

    from_euler

    fn[T : DoubleConvert + Mul + Add + Sub] from_euler(roll : T, pitch : T, yaw : T, order? : String) -> Quaternion[T]

    Converts the Euler angles to the quaternion (x, ai, bj, ck) In different Orders 'order' in {"XYZ", "YXZ", "ZYX", "YZX", "ZXY"}

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io