JFLib

render2d/pick.rs#

Source: lib/render2d/pick.rs

pick -- a scene's spatial index, picking, area selection and object snaps on the CPU in f64 (G6d, decisions 24 and 30;
docs/RENDER2D.md "Auswahl und Fang").  A child of scene.rs: it reads the scene's entities, layers and blocks.

The index: an R-tree (index.rs) of the entities on layers by their boxes (the centre line's, grown by how far a width in
drawing units reaches), and one per block of its entities in its own units.  An insert sits in the scene's tree by its
copies' boxes (its block's box through each transform, an array's corner copies), never once per entity of its block:
a query that meets it goes down into the block through the inverse transform (nested inserts likewise).  Texts in pixels
and inserts of blocks that hold them have no fixed box in the drawing: they are looked at by every query.  The tree is
built in one go by the first query (or build_index) and then kept in step: every change of the scene notes what it
touched, the next query applies it (a change of a block: its tree again, and its inserts' boxes when its box moved).

What a query decides is exact against the geometry in f64: a stroke's drawn region (its width, at least a pixel; caps,
joins, miters up to their limit; a dash pattern's gaps count as the stroke, as AutoCAD picks them), arcs and curves
themselves (not their flattening), a fill's inside (even-odd, holes) and a hatched one's, a text's quad.  Hits are in the
painter's order, topmost first.  Hidden layers are skipped; a filter can skip more (a layer lock of an app).
  • struct Target where a pick or a snap found something: the entity on a layer, its layer and, inside an insert, the way down to the entity

    • entity: EntityId
    • layer: LayerId
    • path: Vec<(u32, EntityId)> -- inside an insert, one step per level: the copy of the insert above (row * columns + column; 0 for a single insert) and the entity of its block the way goes on with (a nested insert, ..., last the entity itself). Empty for an entity on a layer
  • struct Hit a hit of Scene::pick

    • target: Target
    • distance: f64 -- pixels from the point to what is drawn of it (0: on it)
  • enum Region<'a> an area selection's region in drawing units (Scene::select)

    • Rect([f64; 2], [f64; 2]) -- two opposite corners of an axis-aligned rectangle
    • Polygon(&'a [[f64; 2]]) -- a closed polygon (even-odd; AutoCAD's WPolygon and CPolygon)
  • enum SelectMode AutoCAD's two area selections

    • Window -- what lies wholly inside
    • Crossing -- what lies inside or touches the boundary
  • enum SnapKind the kinds of object snap (decision 30, and AutoCAD's insertion point)

    • Endpoint -- a line's or arc's ends, a polyline's and a Bézier chain's vertices, a fill's corners
    • Midpoint -- the middle of a line, of each segment of a polyline, of an arc, of each Bézier piece and of an open ellipse or spline (by length)
    • Center -- a circle's, arc's or ellipse's centre (looked for when the pointer is over the curve)
    • Quadrant -- the points of a circle, arc or ellipse on its own axes (an ellipse made of a circle by an uneven insert: its axes' ends)
    • Intersection -- where two pieces near the pointer cross (also across inserts, fills' edges, a polyline with itself)
    • Perpendicular -- the foot of the perpendicular from the base point (SnapOptions::base)
    • Tangent -- where a line from the base point touches a circle, arc or curve
    • Nearest -- the nearest point of what is near the pointer
    • Insertion -- a text's alignment point, an insert's insertion point (each copy of an array)
SNAP_END: u32 = 1
SNAP_MID: u32 = 2
SNAP_CEN: u32 = 4
SNAP_QUA: u32 = 8
SNAP_INT: u32 = 16
SNAP_PER: u32 = 32
SNAP_TAN: u32 = 64
SNAP_NEA: u32 = 128
SNAP_INS: u32 = 256
SNAP_ALL: u32 = 511

impl SnapKind

  • const fn bit(self) -> u32 its bit of SnapOptions::kinds

  • const fn rank(self) -> u32 its rank among kinds equally near the pointer (lower first): endpoint, intersection, midpoint, centre, quadrant, insertion, perpendicular, tangent; nearest only when nothing else is there

  • struct SnapOptions what Scene::snap looks for. Made with new (it may grow)

    • aperture: f64 -- the aperture's radius in pixels: what lies within it of the pointer
    • kinds: u32 -- the kinds looked for (SNAP_* bits)
    • base: Option<[f64; 2]> -- the command's last point: perpendicular and tangent need it (None: they are not looked for)

impl SnapOptions

  • const fn new(aperture: f64, kinds: u32) -> SnapOptions

  • const fn with_base(mut self, base: [f64; 2]) -> SnapOptions the same with a base point for perpendicular and tangent

  • struct Snap an object snap (Scene::snap): its kind, the point (drawing units, f64), how far from the pointer (px), what it lies on (an intersection: both)

    • kind: SnapKind
    • point: [f64; 2]
    • distance: f64
    • target: Target
    • other: Option<Target>
  • struct IndexInfo what the spatial index holds (Scene::index_info). It may grow

    • entries: u32 -- entities on layers in the scene's tree, its nodes, levels, bytes
    • nodes: u32
    • height: u32
    • bytes: u64
    • block_trees: u32 -- blocks with a tree, their entries and bytes
    • block_entries: u32
    • block_bytes: u64
    • outside: u32 -- entities looked at by every query (texts in pixels, inserts of blocks holding them)
    • synced: u32 -- what the last sync applied: changes of entities on layers, blocks made again; the scene's tree made anew
    • blocks_synced: u32
    • rebuilt: bool

impl Scene

  • fn build_index(&mut self) builds the spatial index now (else the first pick, select or snap does)
  • fn index_info(&self) -> IndexInfo what the index holds
  • fn index_check(&mut self) -> Result<u32, &'static str> (tests) the trees' invariants hold

impl Scene

  • fn pick(&mut self, at: [f64; 2], tolerance: f64) -> Vec<Hit> what is drawn within tolerance pixels of a point of the drawing (f64), topmost first (the painter's order: an app cycles through overlapping ones); inside an insert the hit names the insert and the way down to the entity
  • fn pick_filtered(&mut self, at: [f64; 2], tolerance: f64, filter: &dyn Fn(LayerId) -> bool) -> Vec<Hit> pick with a filter of layers (false: skipped; hidden layers are skipped anyway): a layer lock of an app
  • fn pick_brute(&mut self, at: [f64; 2], tolerance: f64) -> Vec<Hit> (tests) pick without the index: every entity, every entity of every block

impl Scene

  • fn select(&mut self, region: Region, mode: SelectMode) -> Vec<EntityId> the entities on layers a region selects (an insert as a whole): Window what lies wholly inside, Crossing what lies inside or touches it (AutoCAD's); by the centre lines (widths do not count, as in AutoCAD), a fill by its rings (a crossing region inside a fill touches it), a text by its quad. In no particular order
  • fn select_filtered(&mut self, region: Region, mode: SelectMode, filter: &dyn Fn(LayerId) -> bool) -> Vec<EntityId> select with a filter of layers (false: skipped)
  • fn select_brute(&mut self, region: Region, mode: SelectMode) -> Vec<EntityId> (tests) select without the index

impl Scene

  • fn snap(&mut self, at: [f64; 2], opt: &SnapOptions) -> Option<Snap> the object snap at a point of the drawing (f64): of the kinds asked for within the aperture, the one nearest the pointer (equally near: endpoint, intersection, midpoint, centre, quadrant, insertion, perpendicular, tangent), nearest only where nothing else is
  • fn snaps(&mut self, at: [f64; 2], opt: &SnapOptions) -> Vec<Snap> every snap candidate in that order (an app cycles through them)
  • fn snaps_filtered(&mut self, at: [f64; 2], opt: &SnapOptions, filter: &dyn Fn(LayerId) -> bool) -> Vec<Snap> snaps with a filter of layers (false: skipped)
  • fn snaps_brute(&mut self, at: [f64; 2], opt: &SnapOptions) -> Vec<Snap> (tests) snaps without the index

impl Scene

  • fn grip_points(&self, id: EntityId) -> Vec<[f64; 2]> AutoCAD's grips of an entity on a layer (drawing units): a line's ends and middle, a polyline's vertices and its segments' middles, an arc's centre, ends and middle, a circle's centre and quadrants, an ellipse's centre and its axes' ends (an open one's ends too), a Bézier chain's and a spline's control points, a fill's corners, a text's alignment point, an insert's insertion point (each copy of an array). Empty for a dead id or a block's entity

Source: docs/API.md in the JFLib repository.