dymokomi/luce-3d
Source of release 0.9.5
0.9.5 / 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.camera 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[21]
pub var version: u64 = 0
pub func init():
self.xs = Growing[f32]()
self.ys = Growing[f32]()
self.depths = Growing[f32]()
var key: f64[21]
for at in 0..<(usize)21: 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[21] = [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, p.ortho, width, height, (f64)mesh.point_count(), 0.0]
var same = true
for at in 0..<(usize)21: 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 at = p.screen(try mesh.point((i64)id), width, height)
self.xs.items[id] = (f32)at.x
self.ys.items[id] = (f32)at.y
self.depths.items[id] = (f32)at.z if at.z > 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:
return p.screen(point, width, height)
## 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
return try mesh.ray_face(p.ray_origin(x, y, width, height), try p.ray_direction(x, y, width, height).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
if edges and mesh.find_attribute("cad_edge", 4) >= 0:
return try cad_edge_at(mesh, p, x, y, width, height)
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
## CAD boundaries may span many tessellation polygons. Search their tagged
## segments, ignoring interior mesh edges, and test visibility at the edge.
func cad_edge_at(mesh: const Mesh*, p: Projection, x: f64, y: f64, width: f64, height: f64) -> i64!:
let owners = try mesh.attribute_entry(mesh.find_attribute("cad_edge", 4))
var found: i64 = -1
var best = reach
for edge in 0..<(try mesh.edge_count()):
if owners.number((usize)edge, 0) < 0.0: continue
let a = try mesh.point(try mesh.edge_point(edge, 0))
let b = try mesh.point(try mesh.edge_point(edge, 1))
let start = project(a, p, width, height)
let end = project(b, p, width, height)
if start.z <= p.near or end.z <= p.near: continue
let dx = end.x - start.x
let dy = end.y - start.y
let t = math.clamp(((x - start.x) * dx + (y - start.y) * dy) / math.max(1e-12, dx * dx + dy * dy), 0.0, 1.0)
let distance = math.hypot(x - start.x - t * dx, y - start.y - t * dy)
if distance >= best: continue
# The point on the actual segment (perspective-correct), and whether
# the surface hides it from the view ray through it.
let fraction = t if p.orthographic() else (t / end.z) / ((1.0 - t) / start.z + t / end.z)
let point = a.add(b.subtract(a).multiply_scalar(fraction))
let on = p.screen(point, width, height)
let origin = p.ray_origin(on.x, on.y, width, height)
let offset = point.subtract(origin)
let depth = offset.length()
let hit = try mesh.ray_distance(origin, try offset.normalized())
if hit >= 0.0 and hit < depth - math.max(1e-5, depth * 1e-5): continue
found = edge
best = distance
return found