mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-05 19:12:08 -10:00
Add the charts reading gradient positions across their contours
This commit is contained in:
1 parent
a6a08c6f5f
commit
f7a62a4bad
3 files changed
+527
-37
No files matched your search
@@ -0,0 +1,471 @@
|
||||
//! Gradients read across their contours rather than along them, where each point's position `v` runs around or beside the contour through it.
|
||||
//!
|
||||
//! Everything here is in gradient space, laid out as in [`crate::gradient_standard`], and `v` increases toward +y (clockwise on screen for an
|
||||
//! unreflected placement). A winding chart measures an angle in turns within `[0, 1)`, wrapping at a seam on the +x side where the spread has no
|
||||
//! effect. The others measure from the gradient line, 0 on it and one unit per gradient length across its midpoint, which the spread then repeats.
|
||||
|
||||
use crate::gradient::{GradientForm, GradientModel};
|
||||
use crate::gradient_apollonian::{AxisCircle, CircleFamily, Homogeneous};
|
||||
use crate::gradient_standard::{Cone, GradientStandard, HalfPlane, NEAR_LINE_RADIUS, curvature_radius};
|
||||
use glam::DVec2;
|
||||
use std::f64::consts::TAU;
|
||||
|
||||
/// How far apart a cone's two centers may be, relative to its size, for it to be drawn as concentric circles.
|
||||
const CONCENTRIC_TOLERANCE: f64 = 1e-9;
|
||||
|
||||
/// How each point of a gradient read across its contours finds its position `v`, in gradient space.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum AcrossChart {
|
||||
/// The linear form's: `v = y`.
|
||||
Linear,
|
||||
/// Concentric circles': the angle around their shared center at `(center, 0)`, from the +x ray.
|
||||
Concentric { center: f64 },
|
||||
/// SVG 2's cone's: the angle of the point around the center of the cone's circle through it, unpainted where the cone is and past `keep`'s line.
|
||||
Cone { cone: Cone, keep: Option<HalfPlane> },
|
||||
/// Apollonian circles nesting around two limit points: the angle of `M(z) = (n₀ z + n₁) / (d₀ z + d₁)`, which sends the limit point the
|
||||
/// end contour encloses to 0 and the other to infinity, so each contour of constant `v` is an arc from one limit point to the other.
|
||||
Mobius { numerator: [f64; 2], denominator: [f64; 2] },
|
||||
/// Apollonian circles crossing at `(center_x, ±half_height)`: `scale · ln(|z - (center_x, -half_height)| / |z - (center_x, half_height)|)`,
|
||||
/// the bipolar coordinate whose contours are the circles around the two crossings.
|
||||
Bipolar { center_x: f64, half_height: f64, scale: f64 },
|
||||
/// Unpainted everywhere, as SVG 2 leaves identical end circles.
|
||||
Nothing,
|
||||
}
|
||||
|
||||
impl AcrossChart {
|
||||
/// The chart of a gradient with these curvatures, which must be finite and within -1 to 1, and model.
|
||||
pub fn new(start_curvature: f64, end_curvature: f64, model: GradientModel) -> Self {
|
||||
match (GradientForm::from_curvatures([start_curvature, end_curvature]), model) {
|
||||
(Some(GradientForm::Linear), _) => Self::Linear,
|
||||
(Some(GradientForm::Radial), _) => Self::Concentric { center: 0. },
|
||||
(None, GradientModel::Standard) => Self::standard(start_curvature, end_curvature),
|
||||
(None, GradientModel::Apollonian) => Self::apollonian(start_curvature, end_curvature),
|
||||
}
|
||||
}
|
||||
|
||||
/// The angle around SVG 2's circles, painting where the Standard model's gradient read along them paints.
|
||||
fn standard(start_curvature: f64, end_curvature: f64) -> Self {
|
||||
match GradientStandard::new(start_curvature, end_curvature) {
|
||||
GradientStandard::Cone(cone) => Self::cone(cone, None),
|
||||
// A line end and a nearly flat cone, drawn as a profile along the gradient line, measure around the cone that's drawn at the flattest radius
|
||||
GradientStandard::Ramp { keep, .. } | GradientStandard::Solid { keep, .. } => Self::cone(near_line_cone(start_curvature, end_curvature), keep),
|
||||
GradientStandard::Profile { .. } => Self::cone(near_line_cone(start_curvature, end_curvature), None),
|
||||
GradientStandard::Nothing => Self::Nothing,
|
||||
}
|
||||
}
|
||||
|
||||
fn cone(cone: Cone, keep: Option<HalfPlane>) -> Self {
|
||||
let size = cone.start_center.abs().max(cone.end_center.abs()).max(cone.start_radius).max(cone.end_radius);
|
||||
let concentric = (cone.end_center - cone.start_center).abs() <= CONCENTRIC_TOLERANCE * size;
|
||||
|
||||
match keep {
|
||||
None if concentric => Self::Concentric { center: cone.start_center },
|
||||
_ => Self::Cone { cone, keep },
|
||||
}
|
||||
}
|
||||
|
||||
/// The angle around the limit points of the Apollonian circles, or the bipolar coordinate beside their crossings.
|
||||
fn apollonian(start_curvature: f64, end_curvature: f64) -> Self {
|
||||
// Identical end circles span no family of circles, and draw the linear form's plain ramp
|
||||
let (start_radius, end_radius) = (curvature_radius(start_curvature), curvature_radius(end_curvature));
|
||||
if start_radius.is_finite() && end_radius.is_finite() && Cone::between(start_radius, end_radius).is_identical() {
|
||||
return Self::Linear;
|
||||
}
|
||||
|
||||
let (start, end) = (AxisCircle::start(start_curvature), AxisCircle::end(end_curvature));
|
||||
match CircleFamily::of(start, end) {
|
||||
CircleFamily::Crossing { radical_x } => Self::bipolar(start, end, radical_x),
|
||||
CircleFamily::Nesting { limit_points } => Self::mobius(start, end, limit_points),
|
||||
}
|
||||
}
|
||||
|
||||
fn bipolar(start: AxisCircle, end: AxisCircle, radical_x: f64) -> Self {
|
||||
// The crossings lie on whichever end contour is rounder, which places them most precisely
|
||||
let circle = if start.a.abs() >= end.a.abs() { start } else { end };
|
||||
let half_height = (-(circle.b * radical_x + circle.c) / circle.a - radical_x * radical_x).max(0.).sqrt().max(f64::MIN_POSITIVE);
|
||||
|
||||
// The coordinate's rate across the gradient line at its midpoint is `half_height / (half_height² + (1/2 - radical_x)²)` before this scale
|
||||
let midpoint_offset = 0.5 - radical_x;
|
||||
let scale = (half_height * half_height + midpoint_offset * midpoint_offset) / (2. * half_height);
|
||||
|
||||
Self::Bipolar {
|
||||
center_x: radical_x,
|
||||
half_height,
|
||||
scale,
|
||||
}
|
||||
}
|
||||
|
||||
fn mobius(start: AxisCircle, end: AxisCircle, limit_points: [Homogeneous; 2]) -> Self {
|
||||
// With nonnegative denominators, a point's sign on a contour doesn't depend on how it's scaled
|
||||
let oriented = |point: Homogeneous| {
|
||||
let sign = if point.denominator < 0. { -1. } else { 1. };
|
||||
Homogeneous {
|
||||
numerator: point.numerator * sign,
|
||||
denominator: point.denominator * sign,
|
||||
}
|
||||
};
|
||||
let [first, second] = limit_points.map(oriented);
|
||||
|
||||
// The limit point inside the end contour, or the start contour where the end is a line, is negative on it (or zero, for a point contour on it)
|
||||
let inside = |circle: AxisCircle, point: Homogeneous| {
|
||||
let Homogeneous { numerator, denominator } = point;
|
||||
circle.a * (circle.a * numerator * numerator + circle.b * numerator * denominator + circle.c * denominator * denominator)
|
||||
};
|
||||
let enclosing = if end.a != 0. { end } else { start };
|
||||
let (enclosed, other) = if inside(enclosing, first) <= inside(enclosing, second) {
|
||||
(first, second)
|
||||
} else {
|
||||
(second, first)
|
||||
};
|
||||
|
||||
// Concentric circles have their other limit point at infinity, about which the angle is around their shared center
|
||||
let Some(center) = enclosed.value() else { return Self::Linear };
|
||||
if other.denominator == 0. {
|
||||
return Self::Concentric { center };
|
||||
}
|
||||
|
||||
// `M(z) = ±(z - enclosed) / (other - z)`, signed so `M` is positive on the +x ray from the enclosed limit point as `other` passes through infinity
|
||||
let product = other.numerator * enclosed.denominator - enclosed.numerator * other.denominator;
|
||||
let sign = if product < 0. { -1. } else { 1. };
|
||||
Self::Mobius {
|
||||
numerator: [sign * enclosed.denominator, -sign * enclosed.numerator],
|
||||
denominator: [-other.denominator, other.numerator],
|
||||
}
|
||||
}
|
||||
|
||||
/// The position at `point`, or `None` where it's unpainted.
|
||||
pub fn v(&self, point: DVec2) -> Option<f64> {
|
||||
match *self {
|
||||
Self::Linear => Some(point.y),
|
||||
Self::Concentric { center } => Some(turns(point.y, point.x - center)),
|
||||
Self::Cone { cone, keep } => {
|
||||
if keep.is_some_and(|keep| !keep.keeps(point.x)) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let t = cone.t(point)?;
|
||||
let center = cone.start_center + t * (cone.end_center - cone.start_center);
|
||||
Some(turns(point.y, point.x - center))
|
||||
}
|
||||
Self::Mobius { numerator, denominator } => {
|
||||
// The angle of `numerator / denominator` is that of `numerator · conj(denominator)`
|
||||
let affine = |[slope, intercept]: [f64; 2]| DVec2::new(slope * point.x + intercept, slope * point.y);
|
||||
let (numerator, denominator) = (affine(numerator), affine(denominator));
|
||||
Some(turns(numerator.y * denominator.x - numerator.x * denominator.y, numerator.dot(denominator)))
|
||||
}
|
||||
Self::Bipolar { center_x, half_height, scale } => {
|
||||
// The squared distances to the crossings differ by `4 half_height y`, whose ratio's logarithm keeps its precision near the gradient line
|
||||
let to_upper = DVec2::new(point.x - center_x, point.y - half_height).length_squared();
|
||||
Some(scale * 0.5 * (4. * half_height * point.y / to_upper).ln_1p())
|
||||
}
|
||||
Self::Nothing => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether it measures an angle, wrapping from 1 back to 0 at a seam, rather than a distance from the gradient line.
|
||||
pub fn winds(&self) -> bool {
|
||||
matches!(self, Self::Concentric { .. } | Self::Cone { .. } | Self::Mobius { .. })
|
||||
}
|
||||
|
||||
/// Whether it paints every point, rather than leaving some unpainted.
|
||||
pub fn paints_everywhere(&self) -> bool {
|
||||
match self {
|
||||
Self::Linear | Self::Concentric { .. } | Self::Mobius { .. } | Self::Bipolar { .. } => true,
|
||||
Self::Cone { cone, keep } => keep.is_none() && cone.is_nested(),
|
||||
Self::Nothing => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether SVG draws it exactly, which only the linear form's ramp across the gradient line is (and nothing at all).
|
||||
pub fn svg_draws_exactly(&self) -> bool {
|
||||
matches!(self, Self::Linear | Self::Nothing)
|
||||
}
|
||||
}
|
||||
|
||||
/// The cone between the end contours with these curvatures, each a nearly flat circle or a line drawn at the flattest radius a cone is (a line bowing toward the other end).
|
||||
fn near_line_cone(start_curvature: f64, end_curvature: f64) -> Cone {
|
||||
let near_line_radius = |curvature: f64| {
|
||||
let radius = curvature_radius(curvature);
|
||||
if radius.is_infinite() {
|
||||
NEAR_LINE_RADIUS
|
||||
} else {
|
||||
radius.clamp(-NEAR_LINE_RADIUS, NEAR_LINE_RADIUS)
|
||||
}
|
||||
};
|
||||
Cone::between(near_line_radius(start_curvature), near_line_radius(end_curvature))
|
||||
}
|
||||
|
||||
/// The angle of `(x, y)` from the +x axis toward the +y axis, in turns within `[0, 1)`.
|
||||
fn turns(y: f64, x: f64) -> f64 {
|
||||
let turns = y.atan2(x).rem_euclid(TAU) / TAU;
|
||||
if turns >= 1. { 0. } else { turns }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::gradient::{LINEAR_CURVATURES, RADIAL_CURVATURES};
|
||||
use crate::gradient_apollonian::GradientApollonian;
|
||||
use crate::gradient_standard::radius_curvature;
|
||||
|
||||
/// Points spread over a view around the gradient line, kept off its exact rational coordinates.
|
||||
fn sample_points() -> impl Iterator<Item = DVec2> {
|
||||
(-30..=40).flat_map(|x| (-25..=25).map(move |y| DVec2::new(x as f64 * 0.1 + 0.0013, y as f64 * 0.1 + 0.0007)))
|
||||
}
|
||||
|
||||
/// The distance between two angles in turns, the short way around.
|
||||
fn turn_distance(first: f64, second: f64) -> f64 {
|
||||
let distance = (first - second).rem_euclid(1.);
|
||||
distance.min(1. - distance)
|
||||
}
|
||||
|
||||
/// Curvature pairs of every kind: circles crossing, nested, apart, and concentric, lines and points beside them, and nearly flat circles.
|
||||
fn curvature_pairs() -> Vec<[f64; 2]> {
|
||||
let mut pairs: Vec<[f64; 2]> = [
|
||||
(0., 0.75),
|
||||
(0.2, 1.5),
|
||||
(2., -0.5),
|
||||
(0.6, 0.6),
|
||||
(-0.3, -0.3),
|
||||
(0., -0.4),
|
||||
(2., -1.),
|
||||
(-0.5, 1.5),
|
||||
(0.8, 1e9),
|
||||
(-0.3, 1e9),
|
||||
(1e9, 0.25),
|
||||
(1e9, 2.),
|
||||
(3000., -2500.),
|
||||
(0.3, 2500.),
|
||||
]
|
||||
.into_iter()
|
||||
.map(|(start, end)| [radius_curvature(start), radius_curvature(end)])
|
||||
.collect();
|
||||
pairs.extend([[1., 0.3], [1., 1.], [0.8, -1.], [0.4, 0.], [0., -0.4], [-0.3, 0.]]);
|
||||
pairs
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_linear_form_reads_across_the_gradient_line() {
|
||||
for model in [GradientModel::Standard, GradientModel::Apollonian] {
|
||||
let chart = AcrossChart::new(LINEAR_CURVATURES[0], LINEAR_CURVATURES[1], model);
|
||||
assert_eq!(chart, AcrossChart::Linear);
|
||||
for point in sample_points() {
|
||||
assert_eq!(chart.v(point), Some(point.y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_radial_form_reads_the_angle_around_its_center() {
|
||||
for model in [GradientModel::Standard, GradientModel::Apollonian] {
|
||||
let chart = AcrossChart::new(RADIAL_CURVATURES[0], RADIAL_CURVATURES[1], model);
|
||||
for point in sample_points() {
|
||||
let expected = point.y.atan2(point.x).rem_euclid(TAU) / TAU;
|
||||
assert!(turn_distance(chart.v(point).unwrap(), expected) < 1e-12, "{model:?} at {point}");
|
||||
}
|
||||
|
||||
// Clockwise on screen, from the seam along the gradient line
|
||||
assert!((chart.v(DVec2::new(0., 1.)).unwrap() - 0.25).abs() < 1e-12);
|
||||
assert!(chart.v(DVec2::new(0.5, 1e-9)).unwrap() < 1e-6 && chart.v(DVec2::new(0.5, -1e-9)).unwrap() > 1. - 1e-6);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn both_models_read_concentric_circles_around_their_center() {
|
||||
// The start circle centered at its radius and the end at 1 minus its own share a center, with the end circle outside or inside the start circle
|
||||
for start_radius in [-1., -0.5, 0.25, 2., 3.] {
|
||||
let curvatures = [radius_curvature(start_radius), radius_curvature(1. - start_radius)];
|
||||
let [standard, apollonian] = [GradientModel::Standard, GradientModel::Apollonian].map(|model| AcrossChart::new(curvatures[0], curvatures[1], model));
|
||||
let cone = AcrossChart::Cone {
|
||||
cone: Cone::between(start_radius, 1. - start_radius),
|
||||
keep: None,
|
||||
};
|
||||
|
||||
assert!(matches!(standard, AcrossChart::Concentric { .. }), "{standard:?}");
|
||||
assert!(matches!(apollonian, AcrossChart::Concentric { .. }), "{apollonian:?}");
|
||||
for point in sample_points() {
|
||||
let expected = (point.y).atan2(point.x - start_radius).rem_euclid(TAU) / TAU;
|
||||
for (chart, name) in [(standard, "Standard"), (apollonian, "Apollonian"), (cone, "cone")] {
|
||||
let v = chart.v(point).unwrap_or_else(|| panic!("{name} leaves {point} unpainted with start radius {start_radius}"));
|
||||
assert!(turn_distance(v, expected) < 1e-9, "{name} with start radius {start_radius} at {point}: {v} rather than {expected}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn standard_contours_of_constant_position_are_rays_from_the_apex() {
|
||||
// Off-center pairs with an apex where the cone's circles shrink to a point
|
||||
for (start, end) in [(0., 0.75), (0.2, 1.5), (0.6, 0.3), (-0.3, -0.6), (2., -0.5)] {
|
||||
let cone = Cone::between(start, end);
|
||||
let chart = AcrossChart::new(radius_curvature(start), radius_curvature(end), GradientModel::Standard);
|
||||
assert!(matches!(chart, AcrossChart::Cone { .. }), "({start}, {end}) should be a cone, not {chart:?}");
|
||||
|
||||
let delta_radius = cone.end_radius - cone.start_radius;
|
||||
let apex_t = -cone.start_radius / delta_radius;
|
||||
let apex = DVec2::new(cone.start_center + apex_t * (cone.end_center - cone.start_center), 0.);
|
||||
|
||||
for angle in (0..24).map(|step| (step as f64 + 0.37) * TAU / 24.) {
|
||||
let direction = DVec2::from_angle(angle);
|
||||
let positions: Vec<f64> = [0.2, 0.5, 1., 2., 4.].into_iter().filter_map(|distance| chart.v(apex + direction * distance)).collect();
|
||||
for &v in &positions {
|
||||
assert!(turn_distance(v, positions[0]) < 1e-9, "({start}, {end}) along the ray at {angle}: {positions:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn across_paints_exactly_where_along_does() {
|
||||
for curvatures in curvature_pairs() {
|
||||
let standard = GradientStandard::new(curvatures[0], curvatures[1]);
|
||||
let chart = AcrossChart::new(curvatures[0], curvatures[1], GradientModel::Standard);
|
||||
|
||||
// A line end's cut and a nearly flat cone's split are drawn as straight lines across the gradient line, which the cone drawn at the flattest radius only nears
|
||||
let cut = match standard {
|
||||
GradientStandard::Ramp { keep, .. } | GradientStandard::Solid { keep, .. } => keep.map(|keep| keep.at),
|
||||
GradientStandard::Profile { split, .. } => Some(split),
|
||||
_ => None,
|
||||
};
|
||||
for point in sample_points() {
|
||||
if cut.is_some_and(|cut| (point.x - cut).abs() < 0.1 * point.y.abs().max(0.1)) {
|
||||
continue;
|
||||
}
|
||||
assert_eq!(chart.v(point).is_some(), standard.t(point).is_some(), "Standard curvatures {curvatures:?} at {point}: {chart:?}");
|
||||
}
|
||||
|
||||
// Apollonian circles paint everywhere, read along or across them
|
||||
let chart = AcrossChart::new(curvatures[0], curvatures[1], GradientModel::Apollonian);
|
||||
assert!(chart.paints_everywhere(), "Apollonian curvatures {curvatures:?}: {chart:?}");
|
||||
assert!(sample_points().all(|point| chart.v(point).is_some()), "Apollonian curvatures {curvatures:?}: {chart:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn positions_change_continuously_except_at_seams_and_where_contours_meet() {
|
||||
const STEP: f64 = 0.01;
|
||||
|
||||
for curvatures in curvature_pairs() {
|
||||
for model in [GradientModel::Standard, GradientModel::Apollonian] {
|
||||
let chart = AcrossChart::new(curvatures[0], curvatures[1], model);
|
||||
|
||||
// Where every contour of constant position meets, and a cut across the gradient line
|
||||
let mut singular_points = Vec::new();
|
||||
let mut cut = None;
|
||||
match chart {
|
||||
AcrossChart::Concentric { center } => singular_points.push(DVec2::new(center, 0.)),
|
||||
AcrossChart::Cone { cone, keep } => {
|
||||
let apex_t = -cone.start_radius / (cone.end_radius - cone.start_radius);
|
||||
singular_points.push(DVec2::new(cone.start_center + apex_t * (cone.end_center - cone.start_center), 0.));
|
||||
cut = keep.map(|keep| keep.at);
|
||||
}
|
||||
AcrossChart::Mobius { numerator, denominator } => {
|
||||
singular_points.push(DVec2::new(-numerator[1] / numerator[0], 0.));
|
||||
singular_points.push(DVec2::new(-denominator[1] / denominator[0], 0.));
|
||||
}
|
||||
AcrossChart::Bipolar { center_x, half_height, .. } => {
|
||||
singular_points.push(DVec2::new(center_x, half_height));
|
||||
singular_points.push(DVec2::new(center_x, -half_height));
|
||||
}
|
||||
AcrossChart::Linear | AcrossChart::Nothing => {}
|
||||
}
|
||||
let near_singular = |point: DVec2| singular_points.iter().any(|singular| (point - *singular).length() < 0.2) || cut.is_some_and(|cut| (point.x - cut).abs() < 0.02);
|
||||
|
||||
for point in sample_points().filter(|&point| !near_singular(point)) {
|
||||
for neighbor in [point + DVec2::new(STEP, 0.), point + DVec2::new(0., STEP)] {
|
||||
if near_singular(neighbor) {
|
||||
continue;
|
||||
}
|
||||
let (Some(v), Some(neighbor_v)) = (chart.v(point), chart.v(neighbor)) else { continue };
|
||||
|
||||
let change = if chart.winds() { turn_distance(v, neighbor_v) } else { (v - neighbor_v).abs() };
|
||||
assert!(change < 0.05, "{model:?} curvatures {curvatures:?} from {point} to {neighbor}: {v} to {neighbor_v} in {chart:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bipolar_chart_nears_the_linear_forms_as_crossing_curvatures_flatten() {
|
||||
// Over the view, the chart strays from `y` in proportion to the curvatures
|
||||
for curvature in [1e-3, 1e-4, 1e-6] {
|
||||
let tolerance = 300. * curvature;
|
||||
for [start, end] in [[curvature, curvature], [curvature, 2. * curvature], [3. * curvature, curvature]] {
|
||||
let chart = AcrossChart::new(start, end, GradientModel::Apollonian);
|
||||
assert!(matches!(chart, AcrossChart::Bipolar { .. }), "({start}, {end}) should cross, not {chart:?}");
|
||||
for point in sample_points() {
|
||||
let v = chart.v(point).unwrap();
|
||||
assert!((v - point.y).abs() < tolerance, "({start}, {end}) at {point}: {v}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Zero on the gradient line, and one unit per gradient length across its midpoint
|
||||
let chart = AcrossChart::new(0.6, 0.4, GradientModel::Apollonian);
|
||||
assert!(chart.v(DVec2::new(0.3, 0.)).unwrap().abs() < 1e-15);
|
||||
let rate = (chart.v(DVec2::new(0.5, 1e-6)).unwrap() - chart.v(DVec2::new(0.5, -1e-6)).unwrap()) / 2e-6;
|
||||
assert!((rate - 1.).abs() < 1e-6, "{rate}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apollonian_angles_run_around_the_limit_point_inside_the_end_contour() {
|
||||
// The start point inside the end circle, which encloses the start point as its limit point
|
||||
let start_point = AcrossChart::new(-1., 0.3, GradientModel::Apollonian);
|
||||
let AcrossChart::Mobius { numerator, .. } = start_point else { panic!("{start_point:?}") };
|
||||
assert!((-numerator[1] / numerator[0]).abs() < 1e-12, "{start_point:?}");
|
||||
|
||||
// The end a point, which is its own limit point
|
||||
let end_point = AcrossChart::new(0.8, 1., GradientModel::Apollonian);
|
||||
let AcrossChart::Mobius { numerator, .. } = end_point else { panic!("{end_point:?}") };
|
||||
assert!((-numerator[1] / numerator[0] - 1.).abs() < 1e-9, "{end_point:?}");
|
||||
|
||||
// Near the enclosed limit point, the position is the angle around it, from the seam on the +x side
|
||||
for angle in [0.1, 1., 2., 3., 4., 5., 6.] {
|
||||
let point = DVec2::from_angle(angle) * 1e-7;
|
||||
assert!(turn_distance(start_point.v(point).unwrap(), angle / TAU) < 1e-5, "at angle {angle}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_apollonian_chart_is_found_for_every_curvature_pair() {
|
||||
for start_step in -10..=10 {
|
||||
for end_step in -10..=10 {
|
||||
let (start, end) = (start_step as f64 / 10., end_step as f64 / 10.);
|
||||
let chart = AcrossChart::new(start, end, GradientModel::Apollonian);
|
||||
let along = GradientApollonian::new(start, end);
|
||||
for point in sample_points() {
|
||||
let v = chart.v(point);
|
||||
assert!(v.is_some_and(|v| !v.is_nan()), "({start}, {end}) at {point}: {v:?} in {chart:?}");
|
||||
assert!(!along.t(point).is_nan());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extreme_curvatures_compute_finitely() {
|
||||
let extremes = [4e-309, -4e-309, 1e-300, 6e-31, -6e-31, 7e-31, -7e-31, 1e-17, -1e-17];
|
||||
let curvatures = extremes.iter().flat_map(|&extreme| {
|
||||
[
|
||||
[extreme, 0.],
|
||||
[0., extreme],
|
||||
[extreme, 0.3],
|
||||
[-0.3, extreme],
|
||||
[extreme, extreme],
|
||||
[extreme, -extreme],
|
||||
[extreme, 1. - 1e-16],
|
||||
]
|
||||
});
|
||||
|
||||
for [start, end] in curvatures {
|
||||
for model in [GradientModel::Standard, GradientModel::Apollonian] {
|
||||
let chart = AcrossChart::new(start, end, model);
|
||||
for point in sample_points() {
|
||||
let v = chart.v(point);
|
||||
assert!(v.is_none_or(|v| !v.is_nan()), "{model:?} curvatures ({start}, {end}) at {point}: {chart:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,20 +60,20 @@ impl AxisCircle {
|
||||
|
||||
/// A point on the gradient line in homogeneous form, `numerator / denominator`, so the point at infinity is just a zero denominator.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
struct Homogeneous {
|
||||
numerator: f64,
|
||||
denominator: f64,
|
||||
pub(crate) struct Homogeneous {
|
||||
pub(crate) numerator: f64,
|
||||
pub(crate) denominator: f64,
|
||||
}
|
||||
|
||||
impl Homogeneous {
|
||||
const INFINITY: Self = Self { numerator: 1., denominator: 0. };
|
||||
pub(crate) const INFINITY: Self = Self { numerator: 1., denominator: 0. };
|
||||
|
||||
fn value(&self) -> Option<f64> {
|
||||
pub(crate) fn value(&self) -> Option<f64> {
|
||||
(self.denominator != 0.).then(|| self.numerator / self.denominator)
|
||||
}
|
||||
|
||||
/// Scaled so the larger of its two parts has a magnitude of 1, keeping products of points in range.
|
||||
fn normalized(self) -> Self {
|
||||
pub(crate) fn normalized(self) -> Self {
|
||||
let scale = self.numerator.abs().max(self.denominator.abs());
|
||||
if scale == 0. {
|
||||
return self;
|
||||
@@ -85,6 +85,51 @@ impl Homogeneous {
|
||||
}
|
||||
}
|
||||
|
||||
/// How the family of circles two end contours span is laid out.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub(crate) enum CircleFamily {
|
||||
/// The end contours cross, so every member passes through the two points where the line member at `radical_x` meets them.
|
||||
Crossing { radical_x: f64 },
|
||||
/// The members nest around two limit points (normalized), which coincide where the end contours touch.
|
||||
Nesting { limit_points: [Homogeneous; 2] },
|
||||
}
|
||||
|
||||
impl CircleFamily {
|
||||
pub(crate) fn of(start: AxisCircle, end: AxisCircle) -> Self {
|
||||
// The members of the family that are points have a zero discriminant, which as a quadratic form in the two members'
|
||||
// weights has these coefficients. Its determinant says whether the family has two such points (it's hyperbolic), none
|
||||
// (elliptic, where the end contours cross), or one (parabolic, where they touch).
|
||||
let point_quadratic_start = start.b * start.b - 4. * start.a * start.c;
|
||||
let point_quadratic_cross = start.b * end.b - 2. * (start.a * end.c + end.a * start.c);
|
||||
let point_quadratic_end = end.b * end.b - 4. * end.a * end.c;
|
||||
let determinant = point_quadratic_start * point_quadratic_end - point_quadratic_cross * point_quadratic_cross;
|
||||
let scale = point_quadratic_start.abs().max(point_quadratic_end.abs()).max(point_quadratic_cross.abs()).max(f64::MIN_POSITIVE);
|
||||
|
||||
if determinant > 1e-12 * scale * scale {
|
||||
// The line through the two points where the end contours cross is the member with no curvature
|
||||
let line = start.combine(end.a, &end, -start.a);
|
||||
return Self::Crossing { radical_x: -line.c / line.b };
|
||||
}
|
||||
|
||||
// The limit points are where the point members sit, with a point member of no curvature being the point at infinity
|
||||
let limit_points = point_member_weights(point_quadratic_start, point_quadratic_cross, point_quadratic_end).map(|(start_weight, end_weight)| {
|
||||
let member = start.combine(start_weight, &end, end_weight);
|
||||
let magnitude = member.a.abs().max(member.b.abs()).max(member.c.abs());
|
||||
if member.a.abs() <= 1e-12 * magnitude {
|
||||
return Homogeneous::INFINITY;
|
||||
}
|
||||
|
||||
Homogeneous {
|
||||
numerator: -member.b,
|
||||
denominator: 2. * member.a,
|
||||
}
|
||||
.normalized()
|
||||
});
|
||||
|
||||
Self::Nesting { limit_points }
|
||||
}
|
||||
}
|
||||
|
||||
/// The roots of `a y² + b y + c = 0`, in homogeneous form so a vanishing `a` puts a root at infinity rather than dividing by zero.
|
||||
fn solve_quadratic(a: f64, b: f64, c: f64) -> [Homogeneous; 2] {
|
||||
let discriminant = (b * b - 4. * a * c).max(0.);
|
||||
@@ -139,37 +184,10 @@ impl GradientApollonian {
|
||||
let start = AxisCircle::start(start_curvature);
|
||||
let end = AxisCircle::end(end_curvature);
|
||||
|
||||
// The members of the family that are points have a zero discriminant, which as a quadratic form in the two members'
|
||||
// weights has these coefficients. Its determinant says whether the family has two such points (it's hyperbolic), none
|
||||
// (elliptic, where the end contours cross), or one (parabolic, where they touch).
|
||||
let point_quadratic_start = start.b * start.b - 4. * start.a * start.c;
|
||||
let point_quadratic_cross = start.b * end.b - 2. * (start.a * end.c + end.a * start.c);
|
||||
let point_quadratic_end = end.b * end.b - 4. * end.a * end.c;
|
||||
let determinant = point_quadratic_start * point_quadratic_end - point_quadratic_cross * point_quadratic_cross;
|
||||
let scale = point_quadratic_start.abs().max(point_quadratic_end.abs()).max(point_quadratic_cross.abs()).max(f64::MIN_POSITIVE);
|
||||
|
||||
if determinant > 1e-12 * scale * scale {
|
||||
// The line through the two points where the end contours cross is the member with no curvature
|
||||
let line = start.combine(end.a, &end, -start.a);
|
||||
let radical_x = -line.c / line.b;
|
||||
|
||||
return Self::with_band(start, end, CrossingSelection::SameSideAs { radical_x }, None, 0., 1., svg_stand_in);
|
||||
}
|
||||
|
||||
// The limit points are where the point members sit, with a point member of no curvature being the point at infinity
|
||||
let limit_points = point_member_weights(point_quadratic_start, point_quadratic_cross, point_quadratic_end).map(|(start_weight, end_weight)| {
|
||||
let member = start.combine(start_weight, &end, end_weight);
|
||||
let magnitude = member.a.abs().max(member.b.abs()).max(member.c.abs());
|
||||
if member.a.abs() <= 1e-12 * magnitude {
|
||||
return Homogeneous::INFINITY;
|
||||
}
|
||||
|
||||
Homogeneous {
|
||||
numerator: -member.b,
|
||||
denominator: 2. * member.a,
|
||||
}
|
||||
.normalized()
|
||||
});
|
||||
let limit_points = match CircleFamily::of(start, end) {
|
||||
CircleFamily::Crossing { radical_x } => return Self::with_band(start, end, CrossingSelection::SameSideAs { radical_x }, None, 0., 1., svg_stand_in),
|
||||
CircleFamily::Nesting { limit_points } => limit_points,
|
||||
};
|
||||
|
||||
// Each crossing's place relative to the limit points, negative between them and positive beyond them
|
||||
let arc_quadratic = arc_quadratic(limit_points);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
extern crate log;
|
||||
|
||||
pub mod gradient;
|
||||
pub mod gradient_across;
|
||||
pub mod gradient_apollonian;
|
||||
pub mod gradient_standard;
|
||||
pub mod math;
|
||||
|
||||
Reference in new issue
Block a user