dymokomi/luce-3d
Source of release 0.9.5
0.9.5 / src / renderers / edit_overlay.lucb
## Edit-mode overlays kept in Base: the full wireframe is one retained GPU batch
## per mesh, selection geometry is rebuilt only when the selection changes, and
## point markers only when the camera does. A Luce loop over a CAD mesh's 1.4M
## edges cost 0.6-2.4 s per frame.
from luce_gpu import gpu
import interop
from luce_std import math
import memory
import ownership
import errors
from luce_geocore.geocore import Vector3, Mesh, Selection
from luce_geocore.kernel import selection_bits, trailing_zeros, selection_weights
from cameras.camera import Camera, Projection
from renderers.wire import WireRenderer
from renderers.projection import projection_matrix
from renderers.retained import RetainedBatch
from renderers.edit_markers import Markers
from renderers.edit_screen import Screen
import renderers.edit_screen as screens
from luce_geocore.core.growing import Growing, same_ids
## Component modes: 0 points, 1 edges, 2 faces, 3 vertices (corners).
pub struct EditOverlay:
var mesh: interop.Reference[Mesh]?
var mode: i64
var selection: Growing[i64]
var hovered: i64
# Wireframe of every edge, uploaded once per mesh; the CPU copy exists only
# for targets without a device.
var wires: RetainedBatch
var wire_points: Growing[Vector3]
# Accent edges and face triangles of the current selection.
var accent: Growing[Vector3]
var faces: Growing[Vector3]
var hover: Growing[Vector3]
## What a gesture would take (the loop a double-click selects), drawn
## over the model, and the selection it was built from.
var preview: Growing[Vector3]
var preview_key: u64
## Points every face of which is hidden (an editor's Hide): no marker.
var hidden_points: Growing[u32]
var hidden_key: u64
var selected: Growing[bool]
# Point-to-edge adjacency (CSR), built once per mesh for point selections.
var edge_starts: Growing[u32]
var point_edges: Growing[u32]
var screen: Screen
var markers: Markers
var builds: i64
## The selection object last shown (its column id) and its listed ids.
var shown_key: u64
var listed: Growing[i64]
## Whether selected faces are filled here (false when the renderer tints
## them: Renderer.set_face_selection).
var fill: bool
## Soft selection (radius 0: off): every point's weight, computed in Base
## when the selection or the settings change, tints the point markers.
var soft_radius: f64
var soft_falloff: i64
var soft_symmetry: i64
var weights: Growing[f32]
## The selection build the weights belong to (-1: stale).
var weighed: i64
pub func init():
self.mesh = none
self.mode = -1
self.hovered = -1
self.selection = Growing[i64]()
self.wires = RetainedBatch()
self.wire_points = Growing[Vector3]()
self.accent = Growing[Vector3]()
self.faces = Growing[Vector3]()
self.hover = Growing[Vector3]()
self.preview = Growing[Vector3]()
self.preview_key = 0
self.hidden_points = Growing[u32]()
self.hidden_key = 0
self.selected = Growing[bool]()
self.edge_starts = Growing[u32]()
self.point_edges = Growing[u32]()
self.screen = Screen()
self.markers = Markers()
self.builds = 0
self.shown_key = 0
self.listed = Growing[i64]()
self.fill = true
self.soft_radius = 0.0
self.soft_falloff = 3
self.soft_symmetry = 0
self.weights = Growing[f32]()
self.weighed = -1
## Cheap when nothing changed: an identity compare and one pass over the
## selection list. The mesh is retained, so its address cannot be reused.
pub func update(mesh: interop.Reference[Mesh], mode: i64, selection: const i64[], hovered: i64) -> !:
if mode < 0 or mode > 3: error(errors.invalid, "edit component mode must be 0, 1, 2 or 3")
let geometry = try mesh.get()
var changed_mesh = true
if let current = self.mesh:
changed_mesh = current.value() != geometry
if changed_mesh:
# The field holds the new mesh before the old one is released:
# a last release runs a dispose that can re-enter Luce code, so
# nothing reachable may point at the dropped mesh by then.
let previous = self.mesh
self.mesh = mesh.clone()
if let current = previous: current.release()
self.wires.close()
self.wire_points.clear()
self.edge_starts.clear()
self.point_edges.clear()
self.screen.invalidate()
let changed = changed_mesh or mode != self.mode or not same_ids(&self.selection, selection)
if changed:
self.mode = mode
try self.selection.assign(selection)
try self.rebuild_selection(geometry)
self.builds += 1
self.markers.invalidate()
if changed or hovered != self.hovered:
self.hovered = hovered
try self.rebuild_hover(geometry)
## Show a luce-geocore selection (none: nothing selected) in `mode`; its
## bits are listed only when the selection object changed.
pub func show(mesh: interop.Reference[Mesh], mode: i64, selection: interop.Reference[Selection]?, hovered: i64) -> !:
var key: u64 = 0
if let chosen = selection: key = (try chosen.get()).key()
if key != self.shown_key or mode != self.mode:
self.shown_key = key
self.listed.clear()
if let chosen = selection:
let bits = selection_bits(try chosen.get())
try self.listed.reserve(bits.population())
for word in 0..<bits.words.length:
var pending = bits.words[word]
while pending != 0:
self.listed.items[self.listed.count] = (i64)(word * 64 + (usize)trailing_zeros(pending))
self.listed.count += 1
pending = pending & (pending - 1)
try self.update(mesh, mode, self.listed.view(), hovered)
try self.weigh(mesh, selection)
## Show what a gesture would take: an edge selection's edges, a face
## selection's outlines, as lines over the model; none clears it. The
## lines are made again only when the selection object changes.
pub func set_preview(mesh: interop.Reference[Mesh], selection: interop.Reference[Selection]?) -> !:
var key: u64 = 0
if let chosen = selection: key = (try chosen.get()).key() ^ (u64)(try mesh.get()).corners_id()
if key == self.preview_key: return
self.preview_key = key
self.preview.clear()
let chosen = selection else return
let geometry = try mesh.get()
let picked = try chosen.get()
let bits = selection_bits(picked)
if picked.domain() != 2 and picked.domain() != 4: return
if (usize)(try geometry.domain_count(picked.domain())) != bits.count: return
for word in 0..<bits.words.length:
var pending = bits.words[word]
while pending != 0:
let id = (i64)(word * 64 + (usize)trailing_zeros(pending))
pending = pending & (pending - 1)
if picked.domain() == 4:
try self.preview.push(try geometry.point(try geometry.edge_point(id, 0)))
try self.preview.push(try geometry.point(try geometry.edge_point(id, 1)))
continue
let size = try geometry.face_size(id)
for at in 0..<size:
try self.preview.push(try geometry.point(try geometry.face_point(id, at)))
try self.preview.push(try geometry.point(try geometry.face_point(id, (at + 1) % size)))
## Hide the point markers of points whose faces are all in face
## `selection` of `mesh` (the renderer leaves those faces out); none
## shows every marker again.
pub func set_hidden(mesh: interop.Reference[Mesh], selection: interop.Reference[Selection]?) -> !:
var key: u64 = 0
if let chosen = selection: key = (try chosen.get()).key() ^ (u64)(try mesh.get()).corners_id()
if key == self.hidden_key: return
self.hidden_key = key
self.hidden_points.clear()
self.markers.invalidate()
let chosen = selection else return
let picked = try chosen.get()
if picked.domain() != 2: return
let geometry = try mesh.get()
let bits = selection_bits(picked)
if (usize)geometry.face_count() != bits.count: return
let around = try geometry.point_face_span()
for point in 0..<(usize)geometry.point_count():
let first = (usize)around.0[point]
let end = (usize)around.0[point + 1]
if first == end: continue
var all = true
for at in first..<end:
if not bits.get((usize)around.1[at]):
all = false
break
if all: try self.hidden_points.push((u32)point)
## Segments the gesture preview draws (tests).
pub func preview_segments() -> i64:
return (i64)self.preview.count // 2
## Show a soft selection's reach: points within `radius` of the selection
## are tinted by their weight (`falloff` 0 linear, 1 quadratic, 2 cubic,
## 3 smooth; `symmetry` 0 none, 1..3 mirrors across X, Y, Z), as
## Transform Components moves them. A radius of 0 turns it off.
pub func set_soft(radius: f64, falloff: i64 = 3, symmetry: i64 = 0) -> !:
if not (radius >= 0.0) or not math.is_finite(radius): error(errors.invalid, "the soft radius must be finite and not negative")
if falloff < 0 or falloff > 3: error(errors.invalid, "falloff must be 0..3")
if symmetry < 0 or symmetry > 3: error(errors.invalid, "symmetry must be 0..3")
if radius == self.soft_radius and falloff == self.soft_falloff and symmetry == self.soft_symmetry: return
self.soft_radius = radius
self.soft_falloff = falloff
self.soft_symmetry = symmetry
self.weighed = -1
## Points the soft selection reaches (weight above 0), for tests.
pub func soft_points() -> i64:
var count: i64 = 0
for weight in self.weights.view():
if weight > 0.0: count += 1
return count
## A point's soft weight (0 when soft selection is off), for tests.
pub func soft_weight(point: i64) -> f64:
if point < 0 or (usize)point >= self.weights.count: return 0.0
return (f64)self.weights.items[(usize)point]
func weigh(mesh: interop.Reference[Mesh], selection: interop.Reference[Selection]?) -> !:
if self.weighed == self.builds: return
self.weighed = self.builds
self.markers.invalidate()
self.weights.clear()
let chosen = selection else return
if self.soft_radius <= 0.0 or (try chosen.get()).count() == 0: return
let geometry = try mesh.get()
try self.weights.fill((usize)geometry.point_count(), 0.0)
try selection_weights(geometry, try chosen.get(), self.soft_radius, self.soft_falloff, self.soft_symmetry, self.weights.items[..<self.weights.count])
## Fill selected faces here (true) or leave them to the renderer's tint.
pub func set_fill(on: bool):
if on != self.fill:
self.fill = on
self.mode = -1
## `wires` false leaves the full wireframe to the renderer, which draws it
## from the mesh's GPU arrays (Renderer.set_edges).
## `hidden`, when given, first draws the selection's lines over
## everything in that color, so a selection behind or inside a surface
## (a pick mesh the viewport does not draw, such as SDF proxies) still
## shows; the visible parts then draw in `accent` over it.
pub func draw(camera: interop.Reference[Camera], target: interop.View[gpu.RenderTarget],
wire: gpu.Color, accent: gpu.Color, point: gpu.Color, wires: bool = true, hidden: gpu.Color? = none, preview: gpu.Color? = none) -> !:
let current = self.mesh else return
let mesh = try current.get()
let native = try target.get()
let size = try native.size()
if size.width <= 0.0 or size.height <= 0.0: return
if wires: try self.draw_wires(mesh, camera, target, wire)
if let dim = hidden:
if self.accent.count > 0: try WireRenderer.lines(self.accent.view(), camera, target, dim, 1.5, 0.000001, false)
if self.faces.count > 0:
try WireRenderer.triangles(self.faces.view(), camera, target, accent)
if self.accent.count > 0:
try WireRenderer.lines(self.accent.view(), camera, target, accent, 2.5)
if self.preview.count > 0:
try WireRenderer.lines(self.preview.view(), camera, target, preview else point, 2.5)
if self.hover.count > 0:
try WireRenderer.lines(self.hover.view(), camera, target, accent, 3.0 if self.mode == 2 else 2.5)
# Point markers in point mode; in the others only the soft reach.
if self.mode == 0 or self.weights.count > 0:
let projection = try camera.value().projection(size.width / size.height)
try self.screen.update(mesh, projection, size.width, size.height)
for gone in self.hidden_points.view():
if (usize)gone < self.screen.depths.count: self.screen.depths.items[(usize)gone] = 0.0
try self.markers.draw(native, &self.screen, self.selected.view(), self.weights.view(), self.mode == 0,
self.hovered if self.mode == 0 else -1, projection, size.width, size.height, point, accent)
## Mean of the distinct points under a selection, or the bounds center when
## nothing is selected: the Move gizmo's origin, without Luce list scans.
pub static func center(mesh: interop.Reference[Mesh], mode: i64, selection: const i64[]) -> Vector3!:
let geometry = try mesh.get()
let marked = try new bool[(usize)geometry.point_count()] --- in memory.heap
defer free(marked) in memory.heap
for at in 0..<marked.length: marked[at] = false
var sum = Vector3()
var count = 0.0
for id in selection:
var size: i64 = 0
if mode == 0 and id >= 0 and id < geometry.point_count(): size = 1
elif mode == 1 and id >= 0 and id < (try geometry.edge_count()): size = 2
elif mode == 2 and id >= 0 and id < geometry.face_count(): size = try geometry.face_size(id)
for at in 0..<size:
var point = id
if mode == 1: point = try geometry.edge_point(id, at)
elif mode == 2: point = try geometry.face_point(id, at)
if marked[(usize)point]: continue
marked[(usize)point] = true
sum = sum.add(try geometry.point(point))
count += 1.0
if count == 0.0: return geometry.bounds().minimum.add(geometry.bounds().maximum).multiply_scalar(0.5)
return sum.multiply_scalar(1.0 / count)
## Selection batch sizes, for tests and diagnostics.
pub func accent_segments() -> i64:
return (i64)self.accent.count // 2
pub func selected_triangles() -> i64:
return (i64)self.faces.count // 3
## Selection rebuilds so far: hover and camera changes must not add any.
pub func hover_segments() -> i64:
return (i64)self.hover.count // 2
pub func selection_builds() -> i64:
return self.builds
## The point (mode 0) or edge (mode 1) nearest the cursor on the visible
## face under it, within 11 pixels, or -1. `face` is that face when the
## caller knows it (a GPU pick; -1 for none); otherwise one BVH ray.
pub func pick(camera: interop.Reference[Camera], x: f64, y: f64, width: f64, height: f64, face: i64 = -2) -> i64!:
let current = self.mesh else return -1
if self.mode == 2 or width <= 0.0 or height <= 0.0: return -1
let projection = try camera.value().projection(math.max(1.0, width) / math.max(1.0, height))
let mesh = try current.get()
let found = try screens.pick(mesh, self.mode == 1, projection, x, y, width, height, face)
if self.mode != 3 or found < 0: return found
# A vertex: the picked face's corner at the picked point.
let hit = try screens.face_under(mesh, projection, x, y, width, height, face)
if hit < 0: return -1
for at in 0..<(try mesh.face_size(hit)):
if (try mesh.face_point(hit, at)) == found: return (try mesh.face_offset(hit)) + at
return -1
func draw_wires(mesh: const Mesh*, camera: interop.Reference[Camera], target: interop.View[gpu.RenderTarget], color: gpu.Color) -> !:
let native = try target.get()
var device = gpu.device_of(native) catch failure:
if failure.code != gpu.unavailable: error(failure.code, failure.message)
recover gpu.Device()
defer device.destroy()
let edges = (usize)(try mesh.edge_count())
if edges == 0: return
if device.is_open():
if not self.wires.is_open():
let vertices = try new gpu.Vertex[edges] --- in memory.heap
defer free(vertices) in memory.heap
for edge in 0..<edges:
let a = try mesh.point(try mesh.edge_point((i64)edge, 0))
let b = try mesh.point(try mesh.edge_point((i64)edge, 1))
vertices[edge] = gpu.Vertex(x = (f32)a.x, y = (f32)a.y, z = (f32)a.z, red = (f32)b.x, green = (f32)b.y, blue = (f32)b.z)
try self.wires.upload(device, vertices, true)
let size = try native.size()
let projection = try camera.value().projection(size.width / size.height)
try self.wires.draw(native, projection_matrix(projection), color, 1.0)
return
# Deviceless frames (tests, recording) expand wires on the CPU into the
# frame's vertex budget; a wireframe that cannot fit is left out.
if edges * 6 > 4194304: return
if self.wire_points.count == 0:
try self.wire_points.reserve(edges * 2)
for edge in 0..<edges:
try self.wire_points.push(try mesh.point(try mesh.edge_point((i64)edge, 0)))
try self.wire_points.push(try mesh.point(try mesh.edge_point((i64)edge, 1)))
try WireRenderer.lines(self.wire_points.view(), camera, target, color)
## Selection geometry costs O(selection), never O(mesh): selected faces add
## their triangles (a contiguous range per face) and edges; selected points
## light the edges whose ends are both selected, found by adjacency.
func rebuild_selection(mesh: const Mesh*) -> !:
self.accent.clear()
self.faces.clear()
let points = (usize)mesh.point_count()
if self.mode == 0:
try self.selected.fill(points, false)
for id in self.selection.view():
if id >= 0 and (usize)id < points: self.selected.items[(usize)id] = true
if self.selection.count > 0: try self.adjacency(mesh)
for id in self.selection.view():
if id < 0 or (usize)id >= points: continue
for at in self.edge_starts.items[(usize)id]..<self.edge_starts.items[(usize)id + 1]:
let edge = (i64)self.point_edges.items[(usize)at]
let a = try mesh.edge_point(edge, 0)
let b = try mesh.edge_point(edge, 1)
# Reached from both selected ends; emit from the low one.
if a == id and self.selected.items[(usize)b]:
try self.accent.push(try mesh.point(a))
try self.accent.push(try mesh.point(b))
return
self.selected.clear()
if self.mode == 3:
# Vertices: a short stroke from each corner's point into its face.
for id in self.selection.view():
if id < 0 or id >= mesh.vertex_count(): continue
let face = try mesh.corner_face(id)
let p = try mesh.point(try mesh.corner_point(id))
try self.accent.push(p)
try self.accent.push(p.add((try mesh.face_center(face)).subtract(p).multiply_scalar(0.3)))
return
# Outlines of more faces than this would cover the model: the tint
# alone shows a selection that large.
let outline = self.mode == 1 or self.selection.count <= 20000
for id in self.selection.view():
if self.mode == 1:
if id < 0 or id >= (try mesh.edge_count()): continue
try self.accent.push(try mesh.point(try mesh.edge_point(id, 0)))
try self.accent.push(try mesh.point(try mesh.edge_point(id, 1)))
continue
if id < 0 or id >= mesh.face_count(): continue
let offset = try mesh.face_offset(id)
let size = try mesh.face_size(id)
if outline:
for at in 0..<size:
try self.accent.push(try mesh.point(try mesh.face_point(id, at)))
try self.accent.push(try mesh.point(try mesh.face_point(id, (at + 1) % size)))
if not self.fill: continue
# Triangles are grouped by face, size - 2 per polygon.
let first = (offset - id * 2) * 3
for at in first..<first + (size - 2) * 3:
let value = mesh.index(at)
defer value.release()
try self.faces.push(try mesh.point(try mesh.corner_point(try value.get())))
func adjacency(mesh: const Mesh*) -> !:
if self.edge_starts.count > 0: return
let points = (usize)mesh.point_count()
let edges = (try mesh.edge_count())
try self.edge_starts.fill(points + 1, 0)
for edge in 0..<edges:
self.edge_starts.items[(usize)(try mesh.edge_point(edge, 0)) + 1] += 1
self.edge_starts.items[(usize)(try mesh.edge_point(edge, 1)) + 1] += 1
for at in 0..<points: self.edge_starts.items[at + 1] += self.edge_starts.items[at]
try self.point_edges.fill((usize)edges * 2, 0)
var cursor = try new u32[points] --- in memory.heap
defer free(cursor) in memory.heap
memory.copy(cursor, self.edge_starts.items, points)
for edge in 0..<edges:
for end in 0..<(i64)2:
let point = (usize)(try mesh.edge_point(edge, end))
self.point_edges.items[(usize)cursor[point]] = (u32)edge
cursor[point] += 1
## The hovered edge, or the outline of a hovered unselected face.
func rebuild_hover(mesh: const Mesh*) -> !:
self.hover.clear()
let id = self.hovered
if id < 0: return
if self.mode == 1 and id < (try mesh.edge_count()):
let tag = mesh.find_attribute("cad_edge", 4)
let owner = mesh.find_attribute("cad_edge_model", 4)
if tag >= 0 and owner >= 0:
let edge_id = try mesh.attribute_value(tag, id)
let model_id = try mesh.attribute_value(owner, id)
if edge_id < 0.0: return
for edge in 0..<(try mesh.edge_count()):
if (try mesh.attribute_value(tag, edge)) != edge_id or (try mesh.attribute_value(owner, edge)) != model_id: continue
try self.hover.push(try mesh.point(try mesh.edge_point(edge, 0)))
try self.hover.push(try mesh.point(try mesh.edge_point(edge, 1)))
else:
try self.hover.push(try mesh.point(try mesh.edge_point(id, 0)))
try self.hover.push(try mesh.point(try mesh.edge_point(id, 1)))
if self.mode == 2 and id < mesh.face_count():
for chosen in self.selection.view():
if chosen == id: return
let size = try mesh.face_size(id)
for at in 0..<size:
try self.hover.push(try mesh.point(try mesh.face_point(id, at)))
try self.hover.push(try mesh.point(try mesh.face_point(id, (at + 1) % size)))
pub func close():
let previous = self.mesh
self.mesh = none
if let current = previous: current.release()
self.wires.close()
self.wire_points.close()
self.selection.close()
self.accent.close()
self.faces.close()
self.hover.close()
self.preview.close()
self.hidden_points.close()
self.selected.close()
self.edge_starts.close()
self.listed.close()
self.point_edges.close()
self.screen.close()
self.markers.close()
self.weights.close()
func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
if let current = self.mesh: current.trace(visit, context)
pub let edit_overlay_type: interop.Type[EditOverlay] = interop.Type[EditOverlay]("EditOverlay", EditOverlay.close, EditOverlay.trace, closeable = true, main_thread = true)