Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.8

0.7.8 / src / math / vector.lucb

## Vectors in right-handed coordinates, +Y up and camera forward along
## local -Z.
from luce_std import math
import errors

## A point or direction in f64.
pub struct Vector3:
    ## The X coordinate.
    pub var x: f64
    ## The Y coordinate (up).
    pub var y: f64
    ## The Z coordinate.
    pub var z: f64

    ## The vector (x, y, z); the origin by default.
    pub func init(x: f64 = 0.0, y: f64 = 0.0, z: f64 = 0.0):
        self.x = x
        self.y = y
        self.z = z

    ## This plus `other`.
    pub func add(other: Vector3) -> Vector3:
        return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)

    ## This minus `other`.
    pub func subtract(other: Vector3) -> Vector3:
        return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)

    ## This scaled by `scale`.
    pub func multiply_scalar(scale: f64) -> Vector3:
        return Vector3(self.x * scale, self.y * scale, self.z * scale)

    ## The dot product with `other`.
    pub func dot(other: Vector3) -> f64:
        return self.x * other.x + self.y * other.y + self.z * other.z

    ## The cross product with `other` (right-handed).
    pub func cross(other: Vector3) -> Vector3:
        return Vector3(self.y * other.z - self.z * other.y,
                       self.z * other.x - self.x * other.z,
                       self.x * other.y - self.y * other.x)

    ## The Euclidean length, without overflow.
    pub func length() -> f64:
        return math.hypot(math.hypot(self.x, self.y), self.z)

    ## This at unit length; an error when it has no direction.
    pub func normalized() -> Vector3!:
        let length = self.length()
        if not (length > 0.000000000001 and math.is_finite(length)):
            error(errors.invalid, "a direction must be finite and nonzero")
        return self.multiply_scalar(1.0 / length)

    ## Whether every coordinate is finite.
    pub func is_finite() -> bool:
        return math.is_finite(self.x) and math.is_finite(self.y) and math.is_finite(self.z)

    ## `length` along axis `index` (0 X, 1 Y, 2 Z).
    pub static func along(index: usize, length: f64 = 1.0) -> Vector3:
        return Vector3(length if index == 0 else 0.0, length if index == 1 else 0.0, length if index == 2 else 0.0)

    ## The coordinate along axis `index` (0 X, 1 Y, 2 Z).
    pub func coordinate(index: usize) -> f64:
        return self.x if index == 0 else (self.y if index == 1 else self.z)

    ## This vector with its coordinate along axis `index` set to `value`.
    pub func with_coordinate(index: usize, value: f64) -> Vector3:
        return Vector3(value if index == 0 else self.x, value if index == 1 else self.y, value if index == 2 else self.z)

    ## Reflected across the plane where axis `index`'s coordinate is `level`.
    pub func reflected(index: usize, level: f64 = 0.0) -> Vector3:
        return self.with_coordinate(index, 2.0 * level - self.coordinate(index))

    ## Turned by `angle` radians about `axis`, right-handed (Rodrigues);
    ## unchanged when `axis` has no direction.
    pub func rotated_about(axis: Vector3, angle: f64) -> Vector3:
        let pivot = axis.normalized() catch failure: return self
        let c = math.cos(angle)
        let s = math.sin(angle)
        return self.multiply_scalar(c).add(pivot.cross(self).multiply_scalar(s)).add(pivot.multiply_scalar(pivot.dot(self) * (1.0 - c)))

    ## XYZ intrinsic Euler rotation; column vectors encounter Z, then Y, then X.
    pub func rotated(angles: Vector3) -> Vector3:
        let sx = math.sin(angles.x)
        let cx = math.cos(angles.x)
        let sy = math.sin(angles.y)
        let cy = math.cos(angles.y)
        let sz = math.sin(angles.z)
        let cz = math.cos(angles.z)
        let zx = cz * self.x - sz * self.y
        let zy = sz * self.x + cz * self.y
        let yx = cy * zx + sy * self.z
        let yz = -sy * zx + cy * self.z
        return Vector3(yx, cx * zy - sx * yz, sx * zy + cx * yz)