dymokomi/luce-3d
Source of release 0.9.1
0.9.1 / src / renderers / edit_screen.lucb
## Screen projection for Edit overlays: every point once per camera, for the
## point markers, and single points for picking.
from luce_std import math
from luce_geocore.geocore import Vector3, Mesh
from cameras.perspective import Projection
from luce_geocore.core.growing import Growing
## Picking radius in logical pixels.
let reach: f64 = 11.0
## Logical-pixel positions and view depths of every point for one camera.
## A depth of -1 marks a point behind the near plane.
pub struct Screen:
pub var xs: Growing[f32]
pub var ys: Growing[f32]
pub var depths: Growing[f32]
var key: f64[20]
pub var version: u64 = 0
pub func init():
self.xs = Growing[f32]()
self.ys = Growing[f32]()
self.depths = Growing[f32]()
var key: f64[20]
for at in 0..<(usize)20: key[at] = math.nan
self.key = key
self.version = 0
pub func invalidate():
self.key[0] = math.nan
pub func update(mesh: const Mesh*, p: Projection, width: f64, height: f64) -> !:
let key: f64[20] = [p.position.x, p.position.y, p.position.z, p.right.x, p.right.y, p.right.z, p.up.x, p.up.y, p.up.z,
p.backward.x, p.backward.y, p.backward.z, p.focal, p.aspect, p.near, p.far, width, height, (f64)mesh.point_count(), 0.0]
var same = true
for at in 0..<(usize)20: same = same and key[at] == self.key[at]
if same: return
let count = (usize)mesh.point_count()
try self.xs.fill(count, 0.0)
try self.ys.fill(count, 0.0)
try self.depths.fill(count, -1.0)
for id in 0..<count:
let offset = (try mesh.point((i64)id)).subtract(p.position)
let depth = -offset.dot(p.backward)
let d = math.max(p.near, depth)
self.xs.items[id] = (f32)((offset.dot(p.right) * p.focal / p.aspect / d + 1.0) * width * 0.5)
self.ys.items[id] = (f32)((1.0 - offset.dot(p.up) * p.focal / d) * height * 0.5)
self.depths.items[id] = (f32)depth if depth > p.near else -1.0
self.key = key
self.version += 1
pub func close():
self.xs.close()
self.ys.close()
self.depths.close()
## Logical-pixel position and view depth of one point; depth <= near means
## the point is behind the camera.
pub func project(point: Vector3, p: Projection, width: f64, height: f64) -> Vector3:
let offset = point.subtract(p.position)
let depth = -offset.dot(p.backward)
let d = math.max(p.near, depth)
return Vector3((offset.dot(p.right) * p.focal / p.aspect / d + 1.0) * width * 0.5, (1.0 - offset.dot(p.up) * p.focal / d) * height * 0.5, depth)
## The face under the cursor: `known` when the caller has it (a GPU pick),
## else one BVH ray.
pub func face_under(mesh: const Mesh*, p: Projection, x: f64, y: f64, width: f64, height: f64, known: i64 = -2) -> i64!:
if known >= -1: return known
let direction = 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)
return try mesh.ray_face(p.position, try direction.normalized())
## Pick the point or edge of the face under the cursor nearest to it on
## screen, within `reach` pixels. The face comes from one BVH ray, so it is
## the visible one; the nearest edge to a point inside a visible polygon is
## one of its own edges. -1 when the cursor is off the mesh or too far.
## A mesh without faces (a point cloud, such as CAD control vertices) has
## nothing to hide its points: the nearest point on screen, in front of the
## camera, within `reach`.
pub func pick(mesh: const Mesh*, edges: bool, p: Projection, x: f64, y: f64, width: f64, height: f64, known: i64 = -2) -> i64!:
if mesh.face_count() == 0:
if edges: return -1
var nearest: i64 = -1
var closest = reach
for point in 0..<mesh.point_count():
let here = project(try mesh.point(point), p, width, height)
let distance = math.hypot(here.x - x, here.y - y)
if here.z > p.near and distance < closest:
closest = distance
nearest = point
return nearest
let face = try face_under(mesh, p, x, y, width, height, known)
if face < 0 or face >= mesh.face_count(): return -1
let size = try mesh.face_size(face)
var found: i64 = -1
var best = reach
var previous = project(try mesh.point(try mesh.face_point(face, size - 1)), p, width, height)
for at in 0..<size:
let here = project(try mesh.point(try mesh.face_point(face, at)), p, width, height)
if edges:
let dx = here.x - previous.x
let dy = here.y - previous.y
let t = math.max(0.0, math.min(1.0, ((x - previous.x) * dx + (y - previous.y) * dy) / math.max(0.000001, dx * dx + dy * dy)))
let distance = math.hypot(x - previous.x - t * dx, y - previous.y - t * dy)
if distance < best:
best = distance
found = try mesh.face_edge(face, (at + size - 1) % size)
else:
let distance = math.hypot(here.x - x, here.y - y)
if here.z > p.near and distance < best:
best = distance
found = try mesh.face_point(face, at)
previous = here
return found