Skip to main content

Module geometry

Module geometry 

Source
Available on crate feature geometry only.
Expand description

Geometry for weighted sets and fixed placements.

This module draws the outline of a hyperedge from the numbers on its memberships, places nodes at a weighted mix of given positions, and spreads overlapping points apart. Everything is a pure function of its input.

§Determinism

Nothing here is random, reads a clock or iterates a hash map, and only + - * /, sqrt, abs, floor, min and max touch a float, so results are bit for bit the same on every machine. A test over every file of this module enforces the rule.

§Stability

Nothing is kept between calls: every result is a function of its input alone, a measurement and not a simulation. Stability from one call to the next comes from four rules, each pinned by a test:

  1. Results are keyed by id and listed in the input’s order (Positions keeps insertion order).
  2. The push that separates two points on one spot is chosen from the two ids, never from their indices, so adding or removing a far-away node changes no other node’s bits.
  3. A membrane’s grid sits on a lattice of cell fixed at the origin (0, 0), so the outline of a set does not shimmer when its extent changes.
  4. The same input gives the same bits on every run.

Easing between one call’s result and the next belongs to the caller.

§The two layers

The lower layer works on plain points and numbers: barycentre, corner_mix, spread, iso_lines and weighted_outline. The upper layer reads a Hypergraph through the reads the core already serves (node_ids and members_with_data): anchored_placement and edge_outline. The number on a membership is read through a closure the caller gives, so the membership value may be of any type.

§Example

Three edges: two anchors, top and bottom, and one set, club, of three weighted members, each of which is also in top or bottom, so every member is placed. The nodes are placed at the weighted mix of the anchors, spread apart, and the set is outlined.

use hypergraph_rs::geometry::{
    anchored_placement, edge_outline, spread, OutlineParams, Positions, SpreadParams,
};
use hypergraph_rs::Hypergraph;

let mut h: Hypergraph<(), (), f64> = Hypergraph::new();
for (node, edge, weight) in [
    ("a", "top", 3.0),
    ("a", "bottom", 1.0),
    ("b", "top", 1.0),
    ("b", "bottom", 3.0),
    ("c", "top", 2.0),
    ("c", "bottom", 2.0),
    ("a", "club", 5.0),
    ("b", "club", 4.0),
    ("c", "club", 6.0),
] {
    h.add_node_to_edge_with_data(edge, node, weight).unwrap();
}

let mut anchors = Positions::new();
anchors.insert("top", (0.0, 10.0));
anchors.insert("bottom", (0.0, 0.0));

let placement = anchored_placement(&h, &anchors, |w| *w).unwrap();
assert!(placement.unplaced.is_empty());
let mut positions = placement.positions;
spread(&mut positions, &[], &SpreadParams::new(1.0, 8)).unwrap();

let params = OutlineParams::new(4.0, 0.5, 0.9);
let outline = edge_outline(&h, "club", &positions, |w| *w, &params)
    .unwrap()
    .expect("the set has weight");
let drawn = &outline.outline;
assert!(drawn.held_weight >= 0.9 * drawn.total_weight);

Structs§

EdgeOutline
The outline of one hyperedge, and the members it was drawn from.
OutlineParams
The parameters of weighted_outline.
Placement
The result of placing items.
Positions
Positions keyed by id, in insertion order.
ScalarGrid
A regular grid of numbers.
SpreadParams
The parameters of spread.
SpreadReport
What a call of spread did.
WeightedOutline
An outline and what it holds.

Enums§

GeometryError
What can go wrong in a geometry function.
Outline
The shape of an outline.

Constants§

DEFAULT_LEVEL_FRACTION
The default fraction of a member’s level that the line of a membrane is drawn at.
DEFAULT_MAX_GRID_NODES
The default most nodes the outline’s grid may hold.
DEFAULT_MAX_NEIGHBOURS
The default cap on the points one point examines in a step.

Functions§

anchored_placement
Places each node of h at the weighted mean of the positions of the anchored hyperedges it belongs to.
barycentre
The weighted mean of anchors: (Σ wᵢ·xᵢ / Σ wᵢ, Σ wᵢ·yᵢ / Σ wᵢ), summed in index order.
contains
Whether at is inside the paths by the even-odd rule: a ray from at crosses an odd number of segments.
corner_mix
Places each item at the mix of corners given by its row of shares.
edge_outline
The outline of the hyperedge edge_id, from the numbers on its memberships and the positions of its members.
iso_lines
The closed paths along which the grid crosses threshold.
signed_area
The signed area of a closed path by the shoelace formula: positive when it runs counter-clockwise.
spread
Pushes points apart until none is closer than min_distance to another, within a fixed number of steps.
weighted_outline
The outline of a set of weighted points.

Type Aliases§

ClosedPath
A closed path: its first vertex is not repeated at the end.
Point
A point (x, y).
Rect
A rectangle (min_x, min_y, max_x, max_y), as layout::LayoutResult uses.