Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / view_projection.luc

## The viewport camera's projection, the one place screen and world meet:
## a world point on screen (project), the ray through a screen point (ray),
## where it crosses a plane (plane), the world axes, and screen-space
## distances and containment. Gizmos, picking, sketching and placing all
## use these; no geometry evaluation here.
from luce_3d.graphics import Vector3
from workspace import Workspace
from luce_std import math

## World axis `index` (0 X, 1 Y, 2 Z).
pub func axis(index: int) -> Vector3:
    return Vector3(1.0 if index == 0 else 0.0, 1.0 if index == 1 else 0.0, 1.0 if index == 2 else 0.0)

## World point `p` on a viewport `width` by `height`: [x, y, depth], the
## depth above 0 in front of the camera's near plane (behind it, x and y
## are as if at the near plane: only for clipping).
pub func project(w: Workspace, p: Vector3, width: float, height: float) -> list[float]!:
    let lens = w.view.camera.projection(math.max(1.0, width) / math.max(1.0, height))
    let at = lens.screen(p, width, height)
    return [at.x, at.y, at.z - lens.near]

## Where the view ray through (x, y) starts: the eye in perspective, a point
## of the film in orthographic.
pub func origin(w: Workspace, x: float, y: float, width: float, height: float) -> Vector3!:
    return w.view.camera.projection(math.max(1.0, width) / math.max(1.0, height)).ray_origin(x, y, width, height)

## The view ray's direction through (x, y), not normalized.
pub func ray(w: Workspace, x: float, y: float, width: float, height: float) -> Vector3!:
    return w.view.camera.projection(math.max(1.0, width) / math.max(1.0, height)).ray_direction(x, y, width, height)

## The world length a pixel covers at `point` (zoom-independent sizes:
## handles, picking reach).
pub func pixel_at(w: Workspace, point: Vector3, width: float, height: float) -> float!:
    let lens = w.view.camera.projection(math.max(1.0, width) / math.max(1.0, height))
    return lens.pixel(lens.depth(point), height)

## Where the view ray through (x, y) crosses the plane through `center`
## facing `normal` (`center` when the ray runs along it).
pub func plane(w: Workspace, x: float, y: float, width: float, height: float, center: Vector3, normal: Vector3) -> Vector3!:
    let start = origin(w, x, y, width, height)
    let direction = ray(w, x, y, width, height)
    let denominator = direction.dot(normal)
    if math.abs(denominator) < 0.000001:
        return center
    return start.add(direction.multiply_scalar(center.subtract(start).dot(normal) / denominator))

## How far screen point (x, y) is from the screen segment `a`-`b`.
pub func segment_distance(x: float, y: float, a: list[float], b: list[float]) -> float:
    let dx = b[0] - a[0]
    let dy = b[1] - a[1]
    let t = math.clamp(((x - a[0]) * dx + (y - a[1]) * dy) / math.max(0.000001, dx * dx + dy * dy), 0.0, 1.0)
    return math.hypot(x - a[0] - t * dx, y - a[1] - t * dy)

## Whether screen point (x, y) is inside the convex polygon `points`.
pub func inside(x: float, y: float, points: list[list[float]]) -> bool:
    var sign = 0.0
    for at in 0..<points.length:
        let a = points[at]
        let b = points[(at + 1) % points.length]
        let cross = (b[0] - a[0]) * (y - a[1]) - (b[1] - a[1]) * (x - a[0])
        if math.abs(cross) < 0.00001:
            continue
        if sign * cross < 0.0:
            return false
        sign = cross
    return math.abs(sign) > 0.00001