Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-3d

Source of release 0.9.1

0.9.1 / src / renderers / knife.lucb

## A knife stroke in the viewport, in Base: the plane through the camera's
## eye that contains the stroke's screen line (the plane of its two ends'
## view rays), the faces the line passes over on screen, and the stroke's
## end with its angle snapped to 15 degrees. The editor records the plane
## (a point and a normal) in its step, so replaying never needs the camera.
import interop
from luce_std import math
import memory
import errors
from luce_geocore.geocore import Vector3, Mesh, Selection
from luce_geocore.kernel import BitsBuilder, GroupReport, Domain, adopt_selection
from cameras.perspective import PerspectiveCamera, Projection
from renderers.edit_screen import project

## A cutting plane: a point on it and its unit normal.
pub struct KnifePlane:
    pub let point: Vector3
    pub let normal: Vector3

pub struct Knife:
    let reserved: u8 = 0

    ## The plane through the eye containing the line from (x0, y0) to (x1, y1)
    ## of a `width` by `height` view.
    pub static func plane(camera: interop.Reference[PerspectiveCamera], x0: f64, y0: f64, x1: f64, y1: f64, width: f64, height: f64) -> KnifePlane!:
        if width <= 0.0 or height <= 0.0: error(errors.invalid, "the view has no size")
        let p = try camera.value().projection(width / height)
        let normal = ray(p, x0, y0, width, height).cross(ray(p, x1, y1, width, height))
        let length = normal.length()
        if length < 0.000000001: error(errors.invalid, "the knife stroke is too short")
        return KnifePlane(p.position, normal.multiply_scalar(1.0 / length))

    ## The end of a stroke from (x0, y0) toward (x1, y1) with its angle
    ## snapped to the nearest 15 degrees (its length kept), as x and y.
    pub static func snapped(x0: f64, y0: f64, x1: f64, y1: f64) -> Vector3:
        let dx = x1 - x0
        let dy = y1 - y0
        let length = math.sqrt(dx * dx + dy * dy)
        let step = math.pi / 12.0
        let angle = math.round(math.atan2(dy, dx) / step) * step
        return Vector3(x0 + math.cos(angle) * length, y0 + math.sin(angle) * length, 0.0)

    ## The faces of `mesh` the screen line from (x0, y0) to (x1, y1) passes
    ## over (in front of the camera), whatever hides them.
    pub static func faces_under(mesh: interop.Reference[Mesh], camera: interop.Reference[PerspectiveCamera], x0: f64, y0: f64, x1: f64, y1: f64, width: f64, height: f64) -> interop.Reference[Selection]!:
        let geometry = try mesh.get()
        let p = try camera.value().projection(math.max(1.0, width) / math.max(1.0, height))
        let points = geometry.point_span()
        let origin = geometry.position_origin()
        let screen = try new Vector3[points.length] --- in memory.heap
        defer free(screen) in memory.heap
        for at in 0..<points.length: screen[at] = project(points[at].vector().add(origin), p, width, height)
        let offsets = geometry.offset_span()
        let corners = geometry.corner_span()
        let faces = offsets.length - 1
        var bits = try BitsBuilder.zeros(faces)
        errdefer bits.close()
        let middle = Vector3((x0 + x1) * 0.5, (y0 + y1) * 0.5, 0.0)
        for face in 0..<faces:
            let first = (usize)offsets[face]
            let end = (usize)offsets[face + 1]
            var visible = false
            var crossed = false
            var winding = 0.0
            for corner in first..<end:
                let a = screen[(usize)corners[corner]]
                let b = screen[(usize)corners[corner + 1 if corner + 1 < end else first]]
                if a.z > p.near: visible = true
                if a.z <= p.near or b.z <= p.near: continue
                if segments_cross(x0, y0, x1, y1, a.x, a.y, b.x, b.y): crossed = true
                winding += angle_at(middle, a, b)
            # Crossed by the line, or holding it whole (winding round its middle).
            if visible and (crossed or math.abs(winding) > math.pi): bits.set(face)
        return try adopt_selection(bits.finish(), faces, Domain.face, (u64)geometry.topology_hash(), GroupReport())

## The view ray through (x, y).
func ray(p: Projection, x: f64, y: f64, width: f64, height: f64) -> Vector3:
    return p.right.multiply_scalar((x / width * 2.0 - 1.0) * p.aspect / p.focal).add(p.up.multiply_scalar((1.0 - y / height * 2.0) / p.focal)).subtract(p.backward)

## Whether segments (a, b) and (c, d) of the plane cross.
func segments_cross(ax: f64, ay: f64, bx: f64, by: f64, cx: f64, cy: f64, dx: f64, dy: f64) -> bool:
    let d1 = side(cx, cy, dx, dy, ax, ay)
    let d2 = side(cx, cy, dx, dy, bx, by)
    let d3 = side(ax, ay, bx, by, cx, cy)
    let d4 = side(ax, ay, bx, by, dx, dy)
    return ((d1 > 0.0 and d2 < 0.0) or (d1 < 0.0 and d2 > 0.0)) and ((d3 > 0.0 and d4 < 0.0) or (d3 < 0.0 and d4 > 0.0))

func side(ax: f64, ay: f64, bx: f64, by: f64, px: f64, py: f64) -> f64:
    return (bx - ax) * (py - ay) - (by - ay) * (px - ax)

## The signed angle edge (a, b) turns through seen from `center`.
func angle_at(center: Vector3, a: Vector3, b: Vector3) -> f64:
    let ax = a.x - center.x
    let ay = a.y - center.y
    let bx = b.x - center.x
    let by = b.y - center.y
    return math.atan2(ax * by - ay * bx, ax * bx + ay * by)