dymokomi/luce-3d
Source of release 0.9.6
0.9.6 / src / renderers / edit_overlay.lucb
## Edit-mode overlays kept in Base and on the GPU. Selection, hover, preview,
## edge marks and Mesh Check layers are lists of the mesh's point or corner
## ids (overlay_layers.lucb), drawn over the mesh's GPU positions: they are
## made again only when the topology, the mode or what they show changes,
## never because points moved, so a drag uploads its moved positions and
## nothing else. Point markers are laid out once per projection.
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, Domain
from luce_geocore.kernel import Attribute, selection_bits, trailing_zeros, selection_weights
from cameras.camera import Camera, Projection
from renderers.wire import WireRenderer
from renderers.edit_markers import Markers
from renderers.edit_screen import Screen, Segments
from renderers.cad_edges import CadEdges
from renderers.gpu_arrays import GpuArrays
from renderers.mesh_batch import position_buffer, corner_buffer
from renderers.overlay_layers import LayerFrame, PairLines, CornerTriangles, PointLines
import renderers.edit_screen as screens
from luce_geocore.core.growing import Growing, same_ids
## The depth bias that draws a line on a surface 0.2% of the way to the
## eye: in perspective a fixed bias would reach the square of the distance
## further, so it is set for the distance of the mesh's middle.
func line_bias(projection: Projection, mesh: const Mesh*) -> f64:
let box = mesh.bounds()
let depth = math.max(projection.near, projection.depth(box.minimum.add(box.maximum).multiply_scalar(0.5)))
if projection.orthographic(): return 0.002 * depth / math.max(projection.far - projection.near, 0.000001)
return 0.002 * projection.near / depth
## The bias of lines and fills that only need to clear their own surface.
let near_bias = 0.000001
let fill_bias = 0.0000005
## One layer of the Mesh Check's findings: edges and face outlines as lines,
## faces as a tint, points as small crosses, in one color.
struct FindingLayer:
var lines: PairLines
var crosses: PointLines
var faces: CornerTriangles
var color: gpu.Color
func drop():
self.lines.drop()
self.crosses.drop()
self.faces.drop()
func close():
self.lines.close()
self.crosses.close()
self.faces.close()
## How wide (logical pixels) the edge marks and the selection's lines draw.
let mark_width = 3.5
let selection_width = 2.0
## The mean edge length of `mesh` (1 without edges).
func mean_edge(mesh: const Mesh*) -> f64!:
let edges = try mesh.edge_span()
if edges.length == 0: return 1.0
let points = mesh.point_span()
var total = 0.0
for edge in 0..<edges.length // 2:
total += points[(usize)edges[edge * 2 + 1]].vector().subtract(points[(usize)edges[edge * 2]].vector()).length()
return total / (f64)(edges.length // 2)
## The triangles of face `face` (grouped by face, size - 2 each), as corner
## ids into `out`.
func face_triangles(mesh: const Mesh*, face: i64, out: CornerTriangles*) -> !:
let triangles = mesh.triangle_span()
let offset = try mesh.face_offset(face)
let first = (usize)((offset - face * 2) * 3)
let size = try mesh.face_size(face)
for at in first..<first + (usize)(size - 2) * 3: try out.add((u32)triangles[at])
## Face `face`'s outline as point pairs into `out`.
func face_outline(mesh: const Mesh*, face: i64, out: PairLines*) -> !:
let size = try mesh.face_size(face)
for at in 0..<size:
try out.add(try mesh.face_point(face, at), try mesh.face_point(face, (at + 1) % size))
## Component modes: 0 points, 1 edges, 2 faces, 3 vertices (corners).
pub struct EditOverlay:
var mesh: interop.Reference[Mesh]?
## The topology (corners id) and point count the id layers are of, and
## the positions (points id) the point lines were made from.
var topology: u64
var point_count: i64
var points: u64
var mode: i64
var selection: Growing[i64]
var hovered: i64
## The mesh's GPU positions and corner points for the layers, and the
## device the layers' copies live on.
var arrays: GpuArrays
var device: gpu.Device
## Every edge (only when the overlay draws the wireframe itself).
var wires: PairLines
var wired: u64
## The selection: its lines, a vertex's strokes into its faces, and
## selected faces' fill.
var accent: PairLines
var strokes: PointLines
var faces: CornerTriangles
var hover: PairLines
## What a gesture would take (the loop a double-click selects), drawn
## over the model, and the selection it was built from.
var preview: PairLines
var preview_key: u64
## Edges marked as UV seams and hard (luce-geocore's `seam` and `sharp`
## edge marks, above 0); made when the topology or the marks change
## (`marks_key`, 0 for not made), wider than the selection's lines.
var seams: PairLines
var sharps: PairLines
var marks_key: u64
## The Mesh Check's findings, layer by layer (luced-3d sets them when
## its overlay is on and the mesh changed).
var findings: Growing[FindingLayer]
## Points every face of which is hidden (an editor's Hide): no marker.
var hidden_points: Growing[u32]
var hidden_key: u64
## Whether each element of the mode's domain is selected (points in
## mode 0, faces in mode 2): set and cleared one selected id at a time.
var selected: Growing[bool]
var screen: Screen
var segments: Segments
var groups: CadEdges
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.topology = 0
self.point_count = -1
self.points = 0
self.mode = -1
self.hovered = -1
self.selection = Growing[i64]()
self.arrays = GpuArrays()
self.device = gpu.Device()
self.wires = PairLines()
self.wired = 0
self.accent = PairLines()
self.strokes = PointLines()
self.faces = CornerTriangles()
self.hover = PairLines()
self.preview = PairLines()
self.preview_key = 0
self.seams = PairLines()
self.sharps = PairLines()
self.marks_key = 0
self.findings = Growing[FindingLayer]()
self.hidden_points = Growing[u32]()
self.hidden_key = 0
self.selected = Growing[bool]()
self.screen = Screen()
self.segments = Segments()
self.groups = CadEdges()
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
## Show `selection` (ids of `mode`'s components) of `mesh` with `hovered`
## lit. Cheap when nothing changed: a compare of the list; a mesh whose
## points moved (a drag) keeps every id layer.
pub func update(mesh: interop.Reference[Mesh], mode: i64, selection: const i64[], hovered: i64) -> !:
try self.adopt(mesh, mode, selection, not same_ids(&self.selection, selection), hovered)
## 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()
let fresh = key != self.shown_key or mode != self.mode
if fresh:
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.adopt(mesh, mode, self.listed.view(), fresh, hovered)
try self.weigh(mesh, selection)
func adopt(mesh: interop.Reference[Mesh], mode: i64, selection: const i64[], fresh: bool, 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 same_mesh = false
if let current = self.mesh: same_mesh = current.value() == geometry
if not same_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()
let topology = geometry.corners_id() != self.topology or geometry.point_count() != self.point_count
let moved = geometry.points_id() != self.points
if topology:
self.topology = geometry.corners_id()
self.point_count = geometry.point_count()
self.marks_key = 0
let remade = topology or fresh or mode != self.mode
if remade:
self.mode = mode
try self.rebuild_selection(geometry, selection)
self.builds += 1
self.markers.invalidate()
elif moved and self.mode == 3:
# Vertex strokes reach into their faces: their ends move.
try self.rebuild_strokes(geometry)
self.points = geometry.points_id()
if remade or hovered != self.hovered:
self.hovered = hovered
try self.rebuild_hover(geometry)
## 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.add(try geometry.edge_point(id, 0), try geometry.edge_point(id, 1))
else: try face_outline(geometry, id, &self.preview)
## 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)
## Drop every Mesh Check layer.
pub func clear_findings():
for at in 0..<self.findings.count: self.findings.items[at].close()
self.findings.clear()
## Add a Mesh Check layer: `selection`'s elements of `mesh` in `color`
## (edges as lines, faces as outlines and a tint, points as crosses a
## third of the mesh's mean edge long).
pub func add_finding(mesh: interop.Reference[Mesh], selection: interop.Reference[Selection], color: gpu.Color) -> !:
let geometry = try mesh.get()
let picked = try selection.get()
let bits = selection_bits(picked)
let domain = picked.domain()
if (usize)(try geometry.domain_count(domain)) != bits.count: error(errors.invalid, "the finding is of another mesh")
var layer = FindingLayer(PairLines(), PointLines(), CornerTriangles(), color)
errdefer layer.close()
var arm = 0.0
if domain == 0: arm = (try mean_edge(geometry)) / 6.0
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 domain == 4:
try layer.lines.add(try geometry.edge_point(id, 0), try geometry.edge_point(id, 1))
elif domain == 0:
let p = try geometry.point(id)
for axis in 0..<(usize)3:
try layer.crosses.add(p.subtract(Vector3.along(axis, arm)), p.add(Vector3.along(axis, arm)))
elif domain == 2:
try face_outline(geometry, id, &layer.lines)
try face_triangles(geometry, id, &layer.faces)
try self.findings.push(layer)
## Segments the Mesh Check layers draw (tests).
pub func finding_segments() -> i64:
var total: usize = 0
for at in 0..<self.findings.count:
total += self.findings.items[at].lines.segments() + self.findings.items[at].crosses.segments()
return (i64)total
## Segments the gesture preview draws (tests).
pub func preview_segments() -> i64:
return (i64)self.preview.segments()
## 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. `seam` and
## `sharp`, when given, draw the edges marked as seams and hard (Blender's
## red and cyan), a little wider than the wireframe and under the
## selection, which draws over them.
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,
seam: gpu.Color? = none, sharp: 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
let projection = try camera.value().projection(size.width / size.height)
var device = gpu.device_of(native) catch failure:
if failure.code != gpu.unavailable: error(failure.code, failure.message)
recover gpu.Device()
defer device.destroy()
var frame = LayerFrame(native, camera, target, device, projection, gpu.Buffer(), gpu.Buffer(), mesh.position_origin())
if device.is_open():
if self.device.is_open() and not self.device.same(&device): self.drop_copies()
if not self.device.is_open(): self.device = try device.share()
try self.arrays.begin(device)
# A mesh without points (an empty cloud) has no positions to send.
if mesh.point_count() > 0: frame.positions = try position_buffer(&self.arrays, mesh)
if mesh.vertex_count() > 0 and (self.faces.count() > 0 or self.findings.count > 0): frame.corners = try corner_buffer(&self.arrays, mesh)
if wires:
if self.wired != self.topology: try self.rebuild_wires(mesh)
try self.wires.draw(&frame, mesh, wire, 1.0, near_bias)
if let dim = hidden:
# Over everything: on the CPU, as the GPU layers test depth.
var lines = try self.accent.points(mesh)
defer lines.close()
if lines.count > 0: try WireRenderer.lines(lines.view(), camera, target, dim, 1.5, near_bias, false)
if self.strokes.segments() > 0: try WireRenderer.lines(self.strokes.points.view(), camera, target, dim, 1.5, near_bias, false)
try self.faces.draw(&frame, mesh, accent, fill_bias)
# The selection's lines and the marks stand a little toward the eye,
# so the surface does not break them up; the marks write no depth,
# so the selection's lines along the same edges draw over them
# instead of fighting them.
let bias = line_bias(projection, mesh)
if seam != none or sharp != none:
try self.marks_ready(mesh)
if let color = sharp: try self.sharps.draw(&frame, mesh, color, mark_width, bias, writes_depth = false)
if let color = seam: try self.seams.draw(&frame, mesh, color, mark_width, bias, writes_depth = false)
for at in 0..<self.findings.count:
let layer = &self.findings.items[at]
try layer.faces.draw(&frame, mesh, gpu.Color(layer.color.red, layer.color.green, layer.color.blue, 0.35), fill_bias)
try layer.lines.draw(&frame, mesh, layer.color, 2.5, bias, writes_depth = false)
try layer.crosses.draw(&frame, layer.color, 2.5, bias, writes_depth = false)
# The selection's lines are narrower than the marks, so a marked
# edge that is selected shows its mark's color at both sides.
try self.accent.draw(&frame, mesh, accent, selection_width, bias)
try self.strokes.draw(&frame, accent, selection_width, bias)
try self.preview.draw(&frame, mesh, preview else point, 2.5, near_bias)
try self.hover.draw(&frame, mesh, accent, 3.0 if self.mode == 2 else 2.5, near_bias)
# Point markers in point mode; in the others only the soft reach.
if self.mode == 0 or self.weights.count > 0:
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
let selected = self.selected.view() if self.mode == 0 else self.selected.view()[..<0]
try self.markers.draw(native, device, &self.screen, selected, self.weights.view(), self.mode == 0,
self.hovered if self.mode == 0 else -1, projection, size.width, size.height, point, accent)
if device.is_open(): self.arrays.trim()
## The GPU copies were made on another device: make them again.
func drop_copies():
self.wires.drop()
self.accent.drop()
self.strokes.drop()
self.faces.drop()
self.hover.drop()
self.preview.drop()
self.seams.drop()
self.sharps.drop()
for at in 0..<self.findings.count: self.findings.items[at].drop()
self.markers.drop()
self.device.destroy()
## 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.
## Points are told apart by a set as large as the selection's, not the
## mesh.
pub static func center(mesh: interop.Reference[Mesh], mode: i64, selection: const i64[]) -> Vector3!:
let geometry = try mesh.get()
let middle = geometry.bounds().minimum.add(geometry.bounds().maximum).multiply_scalar(0.5)
if selection.length == 0: return middle
var corners: usize = 0
for id in selection:
if mode == 2 and id >= 0 and id < geometry.face_count(): corners += (usize)(try geometry.face_size(id))
else: corners += 2
var slots: usize = 64
while slots < corners * 2: slots *= 2
let seen = try new u32[slots] --- in memory.heap
defer free(seen) in memory.heap
for at in 0..<slots: seen[at] = 0xFFFFFFFF
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)
var slot = ((usize)point *% 0x9E3779B1) & (slots - 1)
while seen[slot] != 0xFFFFFFFF and seen[slot] != (u32)point: slot = (slot + 1) & (slots - 1)
if seen[slot] == (u32)point: continue
seen[slot] = (u32)point
sum = sum.add(try geometry.point(point))
count += 1.0
if count == 0.0: return middle
return sum.multiply_scalar(1.0 / count)
## The edges marked `seam` and `sharp` (above 0), made again when the
## topology or either mark changes.
func marks_ready(mesh: const Mesh*) -> !:
let seam = mesh.attribute_on("seam", Domain.edge)
let sharp = mesh.attribute_on("sharp", Domain.edge)
var key = (self.topology *% 0x9E3779B97F4A7C15) | 1
if seam >= 0: key = (key ^ (try mesh.attribute_id(seam))) *% 0x100000001B3
if sharp >= 0: key = (key ^ ((try mesh.attribute_id(sharp)) << 1)) *% 0x100000001B3
if key == self.marks_key: return
self.marks_key = key
self.seams.clear()
self.sharps.clear()
let edges = try mesh.edge_span()
if seam >= 0: try marked_pairs(try mesh.attribute_entry(seam), edges, &self.seams)
if sharp >= 0: try marked_pairs(try mesh.attribute_entry(sharp), edges, &self.sharps)
## Edges drawn as seams and as hard, for tests: made as the next draw
## would make them.
pub func seam_segments() -> i64!:
let current = self.mesh else return 0
try self.marks_ready(try current.get())
return (i64)self.seams.segments()
pub func hard_segments() -> i64!:
let current = self.mesh else return 0
try self.marks_ready(try current.get())
return (i64)self.sharps.segments()
## Selection batch sizes, for tests and diagnostics.
pub func accent_segments() -> i64:
return (i64)(self.accent.segments() + self.strokes.segments())
pub func selected_triangles() -> i64:
return (i64)self.faces.count()
pub func hover_segments() -> i64:
return (i64)self.hover.segments()
## Whole projections of the points so far (the markers' and the
## pointer's): a drag reprojects only the moved ones (tests).
pub func projections() -> i64:
return (i64)self.screen.whole_version
## Selection rebuilds so far: hover, camera and point moves must not add
## any.
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()
var under = face
if mesh.face_count() > 0 and not (self.mode == 1 and self.groups_tagged(mesh)):
under = try screens.face_under(mesh, projection, x, y, width, height, face)
let found = try screens.pick(mesh, self.mode == 1, projection, x, y, width, height, under, &self.screen, &self.segments)
if self.mode != 3 or found < 0: return found
# A vertex: the picked face's corner at the picked point.
if under < 0: return -1
for at in 0..<(try mesh.face_size(under)):
if (try mesh.face_point(under, at)) == found: return (try mesh.face_offset(under)) + at
return -1
## Whether `mesh` has CAD boundary tags (edges are picked among them).
func groups_tagged(mesh: const Mesh*) -> bool:
return mesh.find_attribute("cad_edge", 4) >= 0
func rebuild_wires(mesh: const Mesh*) -> !:
self.wires.clear()
let edges = try mesh.edge_span()
try self.wires.ids.reserve(edges.length)
for end in edges: try self.wires.ids.push((u32)end)
self.wired = self.topology
## Selection geometry costs O(selection), never O(mesh): selected faces add
## their triangles (a contiguous range per face) and outlines; selected
## points light the edges whose ends are both selected, found by the
## mesh's point-edge adjacency (shared by its topology).
func rebuild_selection(mesh: const Mesh*, chosen: const i64[]) -> !:
self.accent.clear()
self.strokes.clear()
self.faces.clear()
# The marks of the last selection go one by one; the flags are as
# many as the mode's elements.
let elements = (usize)(mesh.point_count() if self.mode == 0 else (mesh.face_count() if self.mode == 2 else 0))
if self.selected.count != elements: try self.selected.fill(elements, false)
else:
for id in self.selection.view():
if id >= 0 and (usize)id < elements: self.selected.items[(usize)id] = false
try self.selection.assign(chosen)
for id in self.selection.view():
if id >= 0 and (usize)id < elements: self.selected.items[(usize)id] = true
if self.mode == 0:
if self.selection.count == 0: return
let around = try mesh.point_edge_span()
let ends = try mesh.edge_span()
for id in self.selection.view():
if id < 0 or (usize)id >= elements: continue
for at in (usize)around.0[(usize)id]..<(usize)around.0[(usize)id + 1]:
let edge = (usize)around.1[at]
let a = (i64)ends[edge * 2]
let b = (i64)ends[edge * 2 + 1]
# Reached from both selected ends; emit from the low one.
let other = b if a == id else a
if id < other and self.selected.items[(usize)other]: try self.accent.add(a, b)
return
if self.mode == 3:
try self.rebuild_strokes(mesh)
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
let edges = try mesh.edge_count()
for id in self.selection.view():
if self.mode == 1:
if id < 0 or id >= edges: continue
try self.accent.add(try mesh.edge_point(id, 0), try mesh.edge_point(id, 1))
continue
if id < 0 or id >= mesh.face_count(): continue
if outline: try face_outline(mesh, id, &self.accent)
if self.fill: try face_triangles(mesh, id, &self.faces)
## Vertices: a short stroke from each corner's point into its face.
func rebuild_strokes(mesh: const Mesh*) -> !:
self.strokes.clear()
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.strokes.add(p, p.add((try mesh.face_center(face)).subtract(p).multiply_scalar(0.3)))
## The hovered edge (a CAD boundary's whole 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()):
if self.groups_tagged(mesh) and mesh.find_attribute("cad_edge_model", 4) >= 0:
let ends = try mesh.edge_span()
for edge in try self.groups.segments(mesh, id):
try self.hover.add((i64)ends[(usize)edge * 2], (i64)ends[(usize)edge * 2 + 1])
else:
try self.hover.add(try mesh.edge_point(id, 0), try mesh.edge_point(id, 1))
if self.mode == 2 and id < mesh.face_count():
if (usize)id < self.selected.count and self.selected.items[(usize)id]: return
try face_outline(mesh, id, &self.hover)
pub func close():
let previous = self.mesh
self.mesh = none
if let current = previous: current.release()
self.wires.close()
self.selection.close()
self.accent.close()
self.strokes.close()
self.faces.close()
self.hover.close()
self.preview.close()
self.seams.close()
self.sharps.close()
self.clear_findings()
self.findings.close()
self.hidden_points.close()
self.selected.close()
self.listed.close()
self.screen.close()
self.segments.close()
self.groups.close()
self.markers.close()
self.weights.close()
self.arrays.close()
self.device.destroy()
func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
if let current = self.mesh: current.trace(visit, context)
## The edges whose mark `entry` is above 0, as point pairs into `lines`.
func marked_pairs(entry: const Attribute*, ends: const i32[], lines: PairLines*) -> !:
for edge in 0..<ends.length // 2:
if entry.number(edge, 0) <= 0.0: continue
try lines.add((i64)ends[edge * 2], (i64)ends[edge * 2 + 1])
pub let edit_overlay_type: interop.Type[EditOverlay] = interop.Type[EditOverlay]("EditOverlay", EditOverlay.close, EditOverlay.trace, closeable = true, main_thread = true)