Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge @raphlinus's upstream offset experiment for MFEK project testing/early alpha/beta use #1

Merged
merged 18 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions benches/quartic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![feature(test)]
extern crate test;
use kurbo::common::solve_quartic;
use test::Bencher;

#[bench]
fn bench_quartic(bb: &mut Bencher) {
let (x1, x2, x3, x4) = (1.0, 2.0, 3.0, 4.0);
let a = -(x1 + x2 + x3 + x4);
let b = x1 * (x2 + x3) + x2 * (x3 + x4) + x4 * (x1 + x3);
let c = -x1 * x2 * (x3 + x4) - x3 * x4 * (x1 + x2);
let d = x1 * x2 * x3 * x4;

bb.iter(|| {
solve_quartic(
test::black_box(d),
test::black_box(c),
test::black_box(b),
test::black_box(a),
1.0,
)
})
}
68 changes: 68 additions & 0 deletions examples/fit_poly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::{io::Write, ops::Range};

use kurbo::{BezPath, CurveFitSample, ParamCurveFit, PathEl, Point, Vec2};

struct MyPoly;

fn eval_mypoly(x: f64) -> f64 {
(x + 4.) * (x + 1.) * (x - 1.) * (x - 3.) / 14. + 0.5
}

fn eval_mypoly_deriv(x: f64) -> f64 {
(((4. * x + 3.) * x - 26.) * x - 1.) / 14.
}

impl ParamCurveFit for MyPoly {
fn sample_pt_deriv(&self, t: f64) -> (Point, Vec2) {
const S: f64 = 336.;
let x = 37. + t * S;
let math_x = (x - 220.) / 40.;
let y = -eval_mypoly(math_x) * 40. + 260.;
let dx = S;
let dy = -dx * eval_mypoly_deriv(math_x);
(Point::new(x, y), Vec2::new(dx, dy))
}

fn sample_pt_tangent(&self, t: f64, _: f64) -> CurveFitSample {
let (p, tangent) = self.sample_pt_deriv(t);
CurveFitSample { p, tangent }
}

fn break_cusp(&self, _: Range<f64>) -> Option<f64> {
None
}
}

pub fn to_svg_economical(path: &BezPath) -> String {
let mut buffer = Vec::new();
write_to(path, &mut buffer).unwrap();
String::from_utf8(buffer).unwrap()
}

/// Write the SVG representation of this path to the provided buffer.
pub fn write_to<W: Write>(path: &BezPath, mut writer: W) -> std::io::Result<()> {
for el in path.elements() {
match *el {
PathEl::MoveTo(p) => write!(writer, "M{:.2} {:.2}", p.x, p.y)?,
PathEl::LineTo(p) => write!(writer, "L{} {}", p.x, p.y)?,
PathEl::QuadTo(p1, p2) => write!(writer, "Q{} {} {} {}", p1.x, p1.y, p2.x, p2.y)?,
PathEl::CurveTo(p1, p2, p3) => write!(
writer,
"C{:.2} {:.2} {:.2} {:.2} {:.2} {:.2}",
p1.x, p1.y, p2.x, p2.y, p3.x, p3.y
)?,
PathEl::ClosePath => write!(writer, "Z")?,
}
}
Ok(())
}

fn main() {
println!("<svg width='800' height='600' xmlns='http://www.w3.org/2000/svg'>");
let path = kurbo::fit_to_bezpath_opt(&MyPoly, 0.1);
println!(
" <path d='{}' stroke='#008' fill='none' stroke-width='2.0'/>",
to_svg_economical(&path)
);
println!("</svg>");
}
19 changes: 19 additions & 0 deletions examples/offset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use kurbo::{offset::CubicOffset, CubicBez, Shape};

fn main() {
println!("<svg width='800' height='600' xmlns='http://www.w3.org/2000/svg'>");
let c = CubicBez::new((100., 100.), (150., 75.), (300., 50.), (400., 200.));
println!(
" <path d='{}' stroke='#000' fill='none' />",
c.to_path(1e-9).to_svg()
);
for i in 1..=10 {
let co = CubicOffset::new(c, i as f64 * 4.0);
let path = kurbo::fit_to_bezpath_opt(&co, 1e-3);
println!(
" <path d='{}' stroke='#008' fill='none' stroke-width='0.2'/>",
path.to_svg()
);
}
println!("</svg>");
}
86 changes: 86 additions & 0 deletions examples/quad_intersect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use arrayvec::ArrayVec;
use kurbo::{common::solve_quartic, ParamCurve, Point, QuadBez, Shape};
use rand::{thread_rng, Rng};

fn rand_point() -> Point {
let mut rng = thread_rng();
Point::new(rng.gen_range(0.0..500.0), rng.gen_range(0.0..500.0))
}

fn rand_quad() -> QuadBez {
QuadBez::new(rand_point(), rand_point(), rand_point())
}

struct ImplicitQuad {
x2: f64,
xy: f64,
y2: f64,
x: f64,
y: f64,
c: f64,
}

impl ImplicitQuad {
fn from_quad(q: QuadBez) -> Self {
let Point { x: ax, y: ay } = q.p0;
let Point { x: bx, y: by } = q.p1;
let Point { x: cx, y: cy } = q.p2;
let (u0, u1, u2) = (by - cy, cx - bx, bx * cy - by * cx);
let (v0, v1, v2) = (cy - ay, ax - cx, cx * ay - cy * ax);
let (w0, w1, w2) = (ay - by, bx - ax, ax * by - ay * bx);
ImplicitQuad {
x2: 4. * u0 * w0 - v0 * v0,
xy: 4. * (u0 * w1 + u1 * w0) - 2. * v0 * v1,
y2: 4. * u1 * w1 - v1 * v1,
x: 4. * (u0 * w2 + u2 * w0) - 2. * v0 * v2,
y: 4. * (u1 * w2 + u2 * w1) - 2. * v1 * v2,
c: 4. * u2 * w2 - v2 * v2,
}
}

fn eval(&self, x: f64, y: f64) -> f64 {
self.x2 * x * x + self.xy * x * y + self.y2 * y * y + self.x * x + self.y * y + self.c
}
}

fn intersect_quads(q0: QuadBez, q1: QuadBez) -> ArrayVec<f64, 4> {
let iq = ImplicitQuad::from_quad(q0);
let c = q1.p0.to_vec2();
let b = (q1.p1 - q1.p0) * 2.0;
let a = c - q1.p1.to_vec2() * 2.0 + q1.p2.to_vec2();
let c0 = iq.eval(c.x, c.y);
let c1 = iq.x * b.x
+ iq.y * b.y
+ 2. * iq.x2 * (b.x * c.x)
+ 2. * iq.y2 * (b.y * c.y)
+ iq.xy * (b.x * c.y + b.y * c.x);
let c2 = iq.x * a.x
+ iq.y * a.y
+ iq.x2 * (2. * a.x * c.x + b.x * b.x)
+ iq.xy * (a.x * c.y + b.x * b.y + a.y * c.x)
+ iq.y2 * (2. * a.y * c.y + b.y * b.y);
let c3 = iq.x2 * 2. * a.x * b.x + iq.xy * (a.x * b.y + b.x * a.y) + iq.y2 * 2. * a.y * b.y;
let c4 = iq.x2 * a.x * a.x + iq.xy * a.x * a.y + iq.y2 * a.y * a.y;
let ts = solve_quartic(c0, c1, c2, c3, c4);
println!("ts: {:?}", ts);
ts
}

fn main() {
let q0 = rand_quad();
let q1 = rand_quad();
println!(
" <path d='{}' stroke='#000' fill='none' />",
q0.to_path(1e-9).to_svg()
);
println!(
" <path d='{}' stroke='#008' fill='none' />",
q1.to_path(1e-9).to_svg()
);
for t in intersect_quads(q0, q1) {
if (0.0..=1.0).contains(&t) {
let p = q1.eval(t);
println!(" <circle cx='{}' cy='{}' r='3' />", p.x, p.y);
}
}
}
45 changes: 45 additions & 0 deletions examples/simplify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use kurbo::{BezPath, Point};

fn plot_fn(f: &dyn Fn(f64) -> f64, d: &dyn Fn(f64) -> f64, xa: f64, xb: f64, n: usize) -> BezPath {
let width = 800.;
let dx = (xb - xa) / n as f64;
let xs = width / (xb - xa);
let ys = 250.;
let y_origin = 300.;
let plot = |x, y| Point::new((x - xa) * xs, y_origin - y * ys);
let mut x0 = xa;
let mut y0 = f(xa);
let mut d0 = d(xa);
let mut path = BezPath::new();
path.move_to(plot(x0, y0));
for i in 0..n {
let x3 = xa + dx * (i + 1) as f64;
let y3 = f(x3);
let d3 = d(x3);
let x1 = x0 + (1. / 3.) * dx;
let x2 = x3 - (1. / 3.) * dx;
let y1 = y0 + d0 * (1. / 3.) * dx;
let y2 = y3 - d3 * (1. / 3.) * dx;
path.curve_to(plot(x1, y1), plot(x2, y2), plot(x3, y3));
x0 = x3;
y0 = y3;
d0 = d3;
}
path
}

fn main() {
println!("<svg width='800' height='600' xmlns='http://www.w3.org/2000/svg'>");
let path = plot_fn(&|x| x.sin(), &|x| x.cos(), -8., 8., 20);
println!(
" <path d='{}' stroke='#8c8' fill='none' stroke-width='5'/>",
path.to_svg()
);
let simpl = kurbo::simplify::SimplifyBezPath::new(path);
let simplified_path = kurbo::fit_to_bezpath_opt(&simpl, 0.1);
println!(
" <path d='{}' stroke='#000' fill='none' stroke-width='1'/>",
simplified_path.to_svg()
);
println!("</svg>");
}
Loading