hypergraph_rs/geometry/mod.rs
1//! Geometry for weighted sets and fixed placements.
2//!
3//! This module draws the outline of a hyperedge from the numbers on its memberships, places nodes at a weighted
4//! mix of given positions, and spreads overlapping points apart. Everything is a pure function of its input.
5//!
6//! # Determinism
7//!
8//! Nothing here is random, reads a clock or iterates a hash map, and only `+ - * /`, `sqrt`, `abs`, `floor`, `min`
9//! and `max` touch a float, so results are bit for bit the same on every machine. A test over every file of this
10//! module enforces the rule.
11//!
12//! # Stability
13//!
14//! Nothing is kept between calls: every result is a function of its input alone, a measurement and not a
15//! simulation. Stability from one call to the next comes from four rules, each pinned by a test:
16//!
17//! 1. Results are keyed by id and listed in the input's order ([`Positions`] keeps insertion order).
18//! 2. The push that separates two points on one spot is chosen from the two ids, never from their indices, so
19//! adding or removing a far-away node changes no other node's bits.
20//! 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
21//! shimmer when its extent changes.
22//! 4. The same input gives the same bits on every run.
23//!
24//! Easing between one call's result and the next belongs to the caller.
25//!
26//! # The two layers
27//!
28//! The lower layer works on plain points and numbers: [`barycentre`], [`corner_mix`], [`spread`], [`iso_lines`]
29//! and [`weighted_outline`]. The upper layer reads a [`Hypergraph`](crate::Hypergraph) through the reads the core
30//! already serves (`node_ids` and `members_with_data`): [`anchored_placement`] and [`edge_outline`]. The number on
31//! a membership is read through a closure the caller gives, so the membership value may be of any type.
32//!
33//! # Example
34//!
35//! Three edges: two anchors, `top` and `bottom`, and one set, `club`, of three weighted members, each of which is
36//! also in `top` or `bottom`, so every member is placed. The nodes are placed at the weighted mix of the anchors,
37//! spread apart, and the set is outlined.
38//!
39//! ```
40//! use hypergraph_rs::geometry::{
41//! anchored_placement, edge_outline, spread, OutlineParams, Positions, SpreadParams,
42//! };
43//! use hypergraph_rs::Hypergraph;
44//!
45//! let mut h: Hypergraph<(), (), f64> = Hypergraph::new();
46//! for (node, edge, weight) in [
47//! ("a", "top", 3.0),
48//! ("a", "bottom", 1.0),
49//! ("b", "top", 1.0),
50//! ("b", "bottom", 3.0),
51//! ("c", "top", 2.0),
52//! ("c", "bottom", 2.0),
53//! ("a", "club", 5.0),
54//! ("b", "club", 4.0),
55//! ("c", "club", 6.0),
56//! ] {
57//! h.add_node_to_edge_with_data(edge, node, weight).unwrap();
58//! }
59//!
60//! let mut anchors = Positions::new();
61//! anchors.insert("top", (0.0, 10.0));
62//! anchors.insert("bottom", (0.0, 0.0));
63//!
64//! let placement = anchored_placement(&h, &anchors, |w| *w).unwrap();
65//! assert!(placement.unplaced.is_empty());
66//! let mut positions = placement.positions;
67//! spread(&mut positions, &[], &SpreadParams::new(1.0, 8)).unwrap();
68//!
69//! let params = OutlineParams::new(4.0, 0.5, 0.9);
70//! let outline = edge_outline(&h, "club", &positions, |w| *w, ¶ms)
71//! .unwrap()
72//! .expect("the set has weight");
73//! let drawn = &outline.outline;
74//! assert!(drawn.held_weight >= 0.9 * drawn.total_weight);
75//! ```
76
77mod contour;
78mod error;
79mod outline;
80mod place;
81mod positions;
82mod spread;
83
84pub use contour::{contains, iso_lines, signed_area, ClosedPath, ScalarGrid};
85pub use error::GeometryError;
86pub use outline::{
87 edge_outline, weighted_outline, EdgeOutline, Outline, OutlineParams, WeightedOutline,
88 DEFAULT_LEVEL_FRACTION, DEFAULT_MAX_GRID_NODES,
89};
90pub use place::{anchored_placement, barycentre, corner_mix};
91pub use positions::{Placement, Positions};
92pub use spread::{spread, SpreadParams, SpreadReport, DEFAULT_MAX_NEIGHBOURS};
93
94/// A point `(x, y)`.
95pub type Point = (f64, f64);
96
97/// A rectangle `(min_x, min_y, max_x, max_y)`, as `layout::LayoutResult` uses.
98pub type Rect = (f64, f64, f64, f64);