From 0f36329811fe13b8d70c1542ba7546592cb9ed6b Mon Sep 17 00:00:00 2001 From: caleb <56044292+caleb-ad@users.noreply.github.com> Date: Tue, 10 May 2022 17:31:08 -0700 Subject: [PATCH] Shape boolean operation improvements (#547) * changed path_intersection structure * comment * Removed do_if! * Create project website with near-complete home page * Added support for undoing - i gotta say the undo system is quite nice * Website responsive resizing improvements * Add newsletter signup to website * Pen tool fixes (#563) Resolves 3 known bugs with the pen tool. * Fixed crash pointed out by @caleb-ad * Fixed issue with final path segment losing handle data * Replace curves with lines when under a drag threshold, improves usability. * Readability improvements, improved comments * Color Input (#565) * initial working prototype * clean up component * Fix alignment * Code review tweaks Co-authored-by: Keavon Chambers * Made the non-inclusive end of a pathseg less inclusive Fixed Bug: When doing a closepath it is possible that the current and beginning edge are both None Fixed Numerous other things * changed how closepath works modified how overlapping_curve_intersections is working * Add "New Folder"/"Delete Selected" buttons to layer panel Closes #532 * Update npm dependencies * Set text color based on its fill when it's being edited * Reorder tool icons, update favicon and logo, and other icon cleanup * Bug Fix: Line-Line intersect origin wasn't being preserved * Bug Fix: proper assignment of t_values in overlapping_curve_intersections * Honestly, i don't even know what I was thinking when i wrote the logic for splitting a subcurve at endpoints, but it was wrong. * Feature: overlapping rectangles behave properly, (except when intersections aren't found correctly) * Remake node type icons (closes #483); color picker cleanup * Change tool shelf icon colors to use classes not style * Add Image node icon and rename node from Path to Shape * Bug Fix: proper intersection construction in partial overlap case * cleaned up log statements * Add website revisions and many new pages * Add features page and fixes to website * Fix clippy lints and update packages (#568) * Fix type error in Brave browser (#569) * Small website text improvements * Various website fixes * Adjusted constants Rearranged intersection algorithm * Changed BooleanOperation::SubtractBack to use SubtractFront Added composite_boolean_operation for operations with more than one shape * Add node graph mockup to website * Differentiate between scale and dimensions (#570) * Differentiate between scale and dimensions * Fix layout and naming of properties * Add embedable images (#564) * Add embedable bitmaps * Initial work on blob urls * Finish implementing data url * Fix some bugs * Rename bitmap to image * Fix loading image on document load * Add transform properties for image * Remove some logging * Add image dimensions * Implement system copy and paste * Fix pasting images * Fix test * Address code review Co-authored-by: Keavon Chambers * Bump minimist from 1.2.5 to 1.2.6 in /frontend (#571) Bumps [minimist](https://github.com/substack/minimist) from 1.2.5 to 1.2.6. - [Release notes](https://github.com/substack/minimist/releases) - [Commits](https://github.com/substack/minimist/compare/1.2.5...1.2.6) --- updated-dependencies: - dependency-name: minimist dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Fix panic dialogue when handling a message (#573) * Fix panic dialogue when handling a message * Fix indents for github reporting * More whitespace improvements * Save: line_intersection * Add documentation to many parts of the Rust codebase (#552) * add lots of doccomments * add conversion traits from layerdatatypes to layers * add suggested doc improvements * Code review changes Co-authored-by: Keavon Chambers * Add additional stroke properties (#582) * Add aditional stroke properties * Add comment explaining clones for closure * Improve labels * Fix doc test Co-authored-by: Keavon Chambers * Bug Fix: .y not .x * Expand upon the "Announcing Graphite alpha" blog post * All shapes now have a Fill in the properties panel; color inputs are now optional (#583) * Add aditional stroke properties * Make the colour input optional * Fix fmt * Apply code review changes * Code review nitpicks * Fix recursion Co-authored-by: Keavon Chambers * New blog post: "Distributed computing with the Graphene runtime" * Fix gradient transformation (#588) * Fix with perfect circle * Actually fix rotated gradient * Gradient transform & fix on rotated canvas * Cleanup & remove logging * Add properties panel entries for artboards (#572) * Artboards can have properties * fix crash when renaming artboards * moved target document to utility types * moved import and added test for file version information * fixed missing import * fix error from merging * - typed properties message handler data - removed name from WidgetRow * clippy warnings * artboards have seperate properties section * - color input can be forced to have selection - crop tool shows on switch - select tool shows on switch * variable renamed * change to use PropType instead of PropType * Add an artboard icon * Add the "Delete Artboard" hint * fix unselect glitch * even better * Remove the Transform properties group Co-authored-by: Keavon Chambers * Bug Fix: boolean union on multiple shapes * Font selection for text layers (#585) * Add font dropdown * Add fonts * Font tool options * Fix tests * Replace http with https * Add variant selection * Do not embed default font * Use proxied font list API * Change default font to Merriweather * Remove outdated comment * Specify font once & load font into foreignobject * Fix tests * Rename variant to font_style * Change TextAreaInput to use FieldInput (WIP, breaks functionality) * Fix textarea functionality * Fix types * Add weight name mapping * Change labeling of "Italic" * Remove commented HTML node * Rename font "name" to "font_family" and "file" "font_file" * Fix errors * Fix fmt Co-authored-by: Keavon Chambers * Improvements to the layer transform cage UX (#589) * Allow input system to handle mousedown while dragging * Fix abort * Add apsect ratio * Make comment more explicit * Fix abort when dragging * Constrain when dragging edge * Rename Crop tool to Artboard tool * Stop pointerdown event from properties panel (#600) * Change stroke weight from ints to floats (#601) Also rename stroke "width" to "weight" in some places. Closes #587 * Change stroke weight from ints to floats * "miter_limit" -> "line_join_miter_limit" * Bump file format version * cargo update Prevent iterating beyond list bounds * Bug fix: proper composite intersection behavior * fix warnings * Improved ray casting and common intersection cases Finding intersections near path segment endpoints was previously unreliable because of imprecision, and the necessity of avoiding double counting any intersections. And, because of snapping, intersections on the endpoints of path segments are a common case. This also improved the ray casting use case, which previously used a "fudge factor" to mitigate the common problem of casting a ray into line endpoints. * fixed warnings * Fix properties deselect (#606) * Fix properties panel deselect * Fix arrow cursors on select tool * Fix drag from UI to document causing mouse down * Fix tests * Cleanup * cleanup messages * Draw the outlines of shapes on hover and selection (#609) * Add hover outline overlay * Increase selection tolerance * Increase weight * Only check if top intersection is selected * Outline selected paths * Reduce outline weight * Increase path tool outline thickness to match hover * Update to use unreachable! instead of panic! * Upgrade vue-cli to version 5 (#594) * Upgrade to Vue CLI 5 (fails to compile) * Upgrade versions with last few weeks of changes * Updated to fork-ts-checker-webpack-plugin 7.2.3 * Remove package.json overrides in lieu of the fixed fork-ts-checker-webpack-plugin@6.5.1 * Fix svg importing * Comments * For debugging only: added infrastructureLogging to vue.config.js * Now works on Windows, waiting on fork-ts-checker-webpack-plugin backport if possible * Switch to the fixed fork-ts-checker-webpack-plugin@6.5.2 * Fix license checker build compilation Co-authored-by: 0hypercube <0hypercube@gmail.com> * Tidy up the full frontend codebase and use optional chaining where possible (#620) * Tidy up the full frontend codebase and use optional chaining where possible * Code review changes * Add a hotkey to select a random primary color (#622) * Add shortcut to select a random primary color (#549) * Rename random primary color message and reduce the number of calls to generate_uuid * Add documentation for SelectRandomPrimaryColor message * Set the alpha value to 255 instead of a random value #622 Co-authored-by: Florent Collin * Move the Layer Tree panel's New Folder and Delete icons into the options bar * Migrate dialogs to Rust and add a New File dialog (#623) * Migrate coming soon and about dialog to Rust * Migrate confirm close and close all * Migrate dialog error * Improve keyboard navigation throughout UI * Cleanup and fix panic dialog * Reduce css spacing to better match old dialogs * Add new document modal * Fix crash when generating default name * Populate rust about graphite data on startup * Code review changes * Move one more :focus CSS rule into App.vue * Add a dialog message and move dialogs * Split out keyboard input navigation from this branch * Improvements including simplifying panic dialog code Co-authored-by: Keavon Chambers * Snapping system improvements and refactor (#621) * Snap to points and refactor * Improve dot position on bounds * Add snap matrix * Cleanup * Code review * Half axis fade rather than increase it * Fix fmt * Hide snap to point overlay when active Co-authored-by: Keavon Chambers * Add the File > Export dialog and PNG/JPG downloading (#629) * Add export dialog * Code review changes * More code review feedback * Fix compilation on stable Rust * Fixes to problems Co-authored-by: Keavon Chambers * Code review Co-authored-by: Keavon Chambers Co-authored-by: Oliver Davies Co-authored-by: mfish33 <32677537+mfish33@users.noreply.github.com> Co-authored-by: 0HyperCube <78500760+0HyperCube@users.noreply.github.com> Co-authored-by: TrueDoctor Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Alaska Co-authored-by: 0hypercube <0hypercube@gmail.com> Co-authored-by: FlorentCollin Co-authored-by: Florent Collin --- Cargo.lock | 79 +-- .../src/document/document_message_handler.rs | 2 + .../tools/shared/transformation_cage.rs | 2 +- graphene/src/boolean_ops.rs | 218 ++++--- graphene/src/consts.rs | 10 +- graphene/src/document.rs | 19 +- graphene/src/intersection.rs | 596 +++++++++++++----- 7 files changed, 615 insertions(+), 311 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cff85c00..54937bfd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "bitflags" version = "1.3.2" @@ -42,9 +48,9 @@ checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899" [[package]] name = "bytemuck" -version = "1.8.0" +version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e851ca7c24871e7336801608a4797d7376545b6928a10d32d75685687141ead" +checksum = "cdead85bdec19c194affaeeb670c0e41fe23de31459efd1c174d049269cf02cc" [[package]] name = "cfg-if" @@ -180,9 +186,9 @@ checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" [[package]] name = "js-sys" -version = "0.3.56" +version = "0.3.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a38fc24e30fd564ce974c02bf1d337caddff65be6cc4735a1f7eab22a7440f04" +checksum = "671a26f820db17c2a2750743f1dd03bafd15b98c9f30c7c2628c024c05d73397" dependencies = [ "wasm-bindgen", ] @@ -204,24 +210,25 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.120" +version = "0.2.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad5c14e80759d0939d013e6ca49930e59fc53dd8e5009132f76240c179380c09" +checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50" [[package]] name = "lock_api" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b" +checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53" dependencies = [ + "autocfg", "scopeguard", ] [[package]] name = "log" -version = "0.4.14" +version = "0.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8" dependencies = [ "cfg-if", ] @@ -240,18 +247,18 @@ checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" [[package]] name = "proc-macro2" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1" dependencies = [ "unicode-xid", ] [[package]] name = "quote" -version = "1.0.15" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1" dependencies = [ "proc-macro2", ] @@ -385,18 +392,18 @@ checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" [[package]] name = "spin" -version = "0.9.2" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "511254be0c5bcf062b019a6c89c01a664aa359ded62f78aa72c6fc137c0590e5" +checksum = "c530c2b0d0bf8b69304b39fe2001993e267461948b890cd037d8ad4293fa1a0d" dependencies = [ "lock_api", ] [[package]] name = "syn" -version = "1.0.87" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e59d925cf59d8151f25a3bedf97c9c157597c9df7324d32d68991cc399ed08b" +checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d" dependencies = [ "proc-macro2", "quote", @@ -470,9 +477,9 @@ checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" [[package]] name = "wasm-bindgen" -version = "0.2.79" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25f1af7423d8588a3d840681122e72e6a24ddbcb3f0ec385cac0d12d24256c06" +checksum = "27370197c907c55e3f1a9fbe26f44e937fe6451368324e009cba39e139dc08ad" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -480,9 +487,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.79" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b21c0df030f5a177f3cba22e9bc4322695ec43e7257d865302900290bcdedca" +checksum = "53e04185bfa3a779273da532f5025e33398409573f348985af9a1cbf3774d3f4" dependencies = [ "bumpalo", "lazy_static", @@ -495,9 +502,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.29" +version = "0.4.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2eb6ec270a31b1d3c7e266b999739109abce8b6c87e4b31fcfcd788b65267395" +checksum = "6f741de44b75e14c35df886aff5f1eb73aa114fa5d4d00dcd37b5e01259bf3b2" dependencies = [ "cfg-if", "js-sys", @@ -507,9 +514,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.79" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f4203d69e40a52ee523b2529a773d5ffc1dc0071801c87b3d270b471b80ed01" +checksum = "17cae7ff784d7e83a2fe7611cfe766ecf034111b49deb850a3dc7699c08251f5" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -517,9 +524,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.79" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa8a30d46208db204854cadbb5d4baf5fcf8071ba5bf48190c3e59937962ebc" +checksum = "99ec0dc7a4756fffc231aab1b9f2f578d23cd391390ab27f952ae0c9b3ece20b" dependencies = [ "proc-macro2", "quote", @@ -530,15 +537,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.79" +version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d958d035c4438e28c70e4321a2911302f10135ce78a9c7834c0cab4123d06a2" +checksum = "d554b7f530dee5964d9a9468d95c1f8b8acae4f282807e7d27d4b03099a46744" [[package]] name = "wasm-bindgen-test" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45c8d417d87eefa0087e62e3c75ad086be39433449e2961add9a5d9ce5acc2f1" +checksum = "d4464b3f74729a25f42b1a0cd9e6a515d2f25001f3535a6cfaf35d34a4de3bab" dependencies = [ "console_error_panic_hook", "js-sys", @@ -550,9 +557,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0e560d44db5e73b69a9757a15512fe7e1ef93ed2061c928871a4025798293dd" +checksum = "a77c5a6f82cc6093a321ca5fb3dc9327fe51675d477b3799b4a9375bac3b7b4c" dependencies = [ "proc-macro2", "quote", @@ -560,9 +567,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.56" +version = "0.3.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c060b319f29dd25724f09a2ba1418f142f539b2be99fbf4d2d5a8f7330afb8eb" +checksum = "7b17e741662c70c8bd24ac5c5b18de314a2c26c32bf8346ee1e6f53de919c283" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/editor/src/document/document_message_handler.rs b/editor/src/document/document_message_handler.rs index 50d57f4a..2352a62e 100644 --- a/editor/src/document/document_message_handler.rs +++ b/editor/src/document/document_message_handler.rs @@ -787,6 +787,7 @@ impl MessageHandler for Docum } BooleanOperation(op) => { // convert Vec<&[LayerId]> to Vec> because Vec<&[LayerId]> does not implement several traits (Debug, Serialize, Deserialize, ...) required by DocumentOperation enum + responses.push_back(StartTransaction.into()); responses.push_back( DocumentOperation::BooleanOperation { operation: op, @@ -794,6 +795,7 @@ impl MessageHandler for Docum } .into(), ); + responses.push_back(CommitTransaction.into()); } CommitTransaction => (), CreateEmptyFolder { mut container_path } => { diff --git a/editor/src/viewport_tools/tools/shared/transformation_cage.rs b/editor/src/viewport_tools/tools/shared/transformation_cage.rs index bf49faa2..a063b16c 100644 --- a/editor/src/viewport_tools/tools/shared/transformation_cage.rs +++ b/editor/src/viewport_tools/tools/shared/transformation_cage.rs @@ -10,7 +10,7 @@ use graphene::Operation; use glam::{DAffine2, DVec2}; -/// Contains the edges that are being dragged along with the origional bounds +/// Contains the edges that are being dragged along with the original bounds. #[derive(Clone, Debug, Default)] pub struct SelectedEdges { bounds: [DVec2; 2], diff --git a/graphene/src/boolean_ops.rs b/graphene/src/boolean_ops.rs index bcbfd0f6..ac5eaa46 100644 --- a/graphene/src/boolean_ops.rs +++ b/graphene/src/boolean_ops.rs @@ -1,11 +1,13 @@ -use crate::consts::{F64PRECISE, RAY_FUDGE_FACTOR}; +use crate::consts::F64PRECISE; use crate::intersection::{intersections, line_curve_intersections, valid_t, Intersect, Origin}; use crate::layers::shape_layer::ShapeLayer; use crate::layers::style::PathStyle; use kurbo::{BezPath, CubicBez, Line, ParamCurve, ParamCurveArclen, ParamCurveArea, ParamCurveExtrema, PathEl, PathSeg, Point, QuadBez, Rect}; use serde::{Deserialize, Serialize}; +use std::cell::RefCell; use std::fmt::{self, Debug, Formatter}; +use std::mem::swap; #[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq)] pub enum BooleanOperation { @@ -26,15 +28,6 @@ pub enum BooleanOperationError { Unexpected, // For debugging, when complete nothing should be unexpected } -/// A simple and idiomatic way to write short "if let Some(_)" statements which do nothing in the None case -macro_rules! do_if { - ($option:expr, $name:ident{$todo:expr}) => { - if let Some($name) = $option { - $todo - } - }; -} - struct Edge { pub from: Origin, pub destination: usize, @@ -55,7 +48,17 @@ struct Vertex { impl Debug for Vertex { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - f.write_str(format!("\n Intersect@ {:?}", self.intersect.point).as_str())?; + f.write_str( + format!( + "\n Intersect Point: {:?} Segment index of A: {:?}, Segment index of B: {:?} t value of A: {:?} t value of B: {:?}", + self.intersect.point, + self.intersect.segment_index(Origin::Alpha), + self.intersect.segment_index(Origin::Beta), + self.intersect.t_value(Origin::Alpha), + self.intersect.t_value(Origin::Beta), + ) + .as_str(), + )?; f.debug_list().entries(self.edges.iter()).finish() } } @@ -158,7 +161,6 @@ struct PathGraph { /// Has somewhat (totally?) undefined behavior when shapes have self intersections. impl PathGraph { pub fn from_paths(alpha: &BezPath, beta: &BezPath) -> Result { - // TODO: check for closed paths somewhere, maybe here? let mut new = PathGraph { vertices: intersections(alpha, beta).into_iter().map(|i| Vertex { intersect: i, edges: Vec::new() }).collect(), }; @@ -219,7 +221,7 @@ impl PathGraph { for (vertex_id, sub_seg) in vertex_ids.into_iter().zip(subdivided.iter()) { match self.current_start { Some(index) => { - do_if!(sub_seg, end_of_edge { self.current.push(*end_of_edge)}); + sub_seg.map(|end_of_edge| self.current.push(end_of_edge)); graph.add_edge(origin, index, vertex_id, self.current.clone()); self.current_start = Some(vertex_id); self.current = Vec::new(); @@ -227,11 +229,11 @@ impl PathGraph { None => { self.current_start = Some(vertex_id); self.start_index = Some(vertex_id); - do_if!(sub_seg, end_of_beginning {self.beginning.push(*end_of_beginning)}); + sub_seg.map(|end_of_beginning| self.beginning.push(end_of_beginning)); } } } - do_if!(subdivided.last().unwrap(), start_of_edge {self.current.push(*start_of_edge)}); + subdivided.last().unwrap().map(|start_of_edge| self.current.push(start_of_edge)); } else { match self.current_start { Some(_) => self.current.push(seg), @@ -242,28 +244,44 @@ impl PathGraph { } fn advance_by_closepath(&mut self, graph: &mut PathGraph, initial_point: &mut Point, origin: Origin) { - // *when a curve ends in a closepath and its start point does not equal its endpoint they should be connected with a line - let end_seg = match self.current.last() { - Some(seg) => seg, - None => self.beginning.last().unwrap(), // if both current and beginning are empty, the path is empty + // When a curve ends in a closepath and its start point does not equal its endpoint they should be connected with a line + let last_line = match self.current.last() { + Some(start_of_final_edge) => Line { + p0: start_of_final_edge.end(), + p1: *initial_point, + }, + None => { + // When None occurs the current edge has been connected to a vertex. + // Either self.beginning is Some or None, if self.beginning is Some there may be a dangling edge to connect + // if self.beginning is None, the end of the current edge may not have closed the path + match self.beginning.last() { + Some(end_of_first_edge) => Line { + p0: end_of_first_edge.end(), + p1: *initial_point, + }, + None => Line { + // should never panic, either a intersection has been encountered, so self.current_start is Some. + // or no vertex has been encountered so self.beginning.last() is Some + p0: graph.vertex(self.current_start.unwrap()).intersect.point, + p1: *initial_point, + }, + } + } }; - let temp_copy = end_seg.end(); - if temp_copy != *initial_point { - // a closepath implicitly defines a line which closes the path - self.advance_by_seg(graph, PathSeg::Line(Line { p0: temp_copy, p1: *initial_point }), origin); + if last_line.length() > F64PRECISE { + // A closepath implicitly defines a line which closes the path and the closepath line may contain intersections + self.advance_by_seg(graph, PathSeg::Line(last_line), origin); } - // when a closepath is not followed by moveto, the next path starts at the end of the current path - *initial_point = temp_copy; } fn finalize_sub_path(&mut self, graph: &mut PathGraph, origin: Origin) { if let (Some(current_start_), Some(start_index_)) = (self.current_start, self.start_index) { - //complete the current path + // Complete the current path self.current.append(&mut self.beginning); graph.add_edge(origin, current_start_, start_index_, self.current.clone()); } else { - //path has a subpath with no intersects - //create a dummy vertex with single edge which will be identified as cycle + // Path has a subpath with no intersects. + // Create a dummy vertex with single edge which will be identified as cycle. let dumb_id = graph.add_vertex(Intersect::new(self.beginning[0].start(), 0.0, 0.0, -1, -1)); graph.add_edge(origin, dumb_id, dumb_id, self.beginning.clone()); } @@ -272,6 +290,7 @@ impl PathGraph { let mut algorithm_state = AlgorithmState::new(); + // All valid SVG paths start with a moveto, so this will always be initialized let mut initial_point = Point::new(0.0, 0.0); for (el_index, el) in path.iter().enumerate() { @@ -306,7 +325,7 @@ impl PathGraph { } /// Returns the `Vertex` index and intersect `t_value` for all intersects in the segment identified by `seg_index` from `origin`. - /// sorts both lists for ascending t_value + /// Sorts both lists for ascending `t_value`. fn intersects_in_seg(&self, seg_index: i32, origin: Origin) -> (Vec, Vec) { let mut vertex_index = Vec::new(); let mut t_values = Vec::new(); @@ -323,7 +342,7 @@ impl PathGraph { (vertex_index, t_values) } - // Returns the number of vertices in the graph. This is equivalent to the number of intersections. + /// Returns the number of vertices in the graph. This is equivalent to the number of intersections. pub fn size(&self) -> usize { self.vertices.len() } @@ -391,18 +410,18 @@ impl PathGraph { /// If `t` is on `(0, 1)`, returns the split curve. /// If `t` is outside `[0, 1]`, returns `(None, None)` -/// If `t` is 0 returns (None, `p`). -/// If `t` is 1 returns (`p`, None). -// TODO: test values outside 1 +/// If `t` is 0 returns `(None, p)`. +/// If `t` is 1 returns `(p, None)`. pub fn split_path_seg(p: &PathSeg, t: f64) -> (Option, Option) { + if t <= -F64PRECISE || t >= 1.0 + F64PRECISE { + return (None, None); + } if t <= F64PRECISE { - if t >= 1.0 - F64PRECISE { - return (None, None); - } - return (Some(*p), None); - } else if t >= 1.0 - F64PRECISE { return (None, Some(*p)); } + if t >= 1.0 - F64PRECISE { + return (Some(*p), None); + } match p { PathSeg::Cubic(cubic) => { let a1 = Line::new(cubic.p0, cubic.p1).eval(t); @@ -453,12 +472,76 @@ pub fn subdivide_path_seg(p: &PathSeg, t_values: &mut [f64]) -> Vec>) -> Result, BooleanOperationError> { + if select == BooleanOperation::SubtractBack { + select = BooleanOperation::SubtractFront; + let temp_len = shapes.len(); + shapes.swap(0, temp_len - 1); + } + match select { + BooleanOperation::Union | BooleanOperation::Intersection => { + // We must attempt to union each shape with every other shape + let mut subject_idx = 0; + while subject_idx < shapes.len() { + let mut shape_idx = 0; + while shape_idx < shapes.len() && subject_idx < shapes.len() { + if shape_idx == subject_idx { + shape_idx += 1; + continue; + } + let partial_union = boolean_operation(select, &mut shapes[subject_idx].borrow_mut(), &mut shapes[shape_idx].borrow_mut()); + match partial_union { + Ok(temp_union) => { + // The result of a successful union will be exactly one shape + shapes.push(RefCell::new(temp_union.into_iter().next().unwrap())); + shapes.swap_remove(subject_idx); + shapes.swap_remove(shape_idx); + } + Err(BooleanOperationError::NothingDone) => shape_idx += 1, + Err(err) => return Err(err), + } + } + subject_idx += 1; + } + Ok(shapes.iter().map(|ref_shape_layer| ref_shape_layer.borrow().clone()).collect()) + } + BooleanOperation::SubtractFront => { + let mut result = vec![shapes[0].borrow().clone()]; + for shape_idx in 1..shapes.len() { + let mut temp = Vec::new(); + for mut partial in result { + match boolean_operation(select, &mut partial, &mut shapes[shape_idx].borrow_mut()) { + Ok(mut partial_result) => temp.append(&mut partial_result), + Err(BooleanOperationError::NothingDone) => temp.push(partial), + Err(err) => return Err(err), + } + } + result = temp; // This move should be done without copying + } + Ok(result) + } + BooleanOperation::Difference => { + let mut difference = Vec::new(); + for shape_idx in 0..shapes.len() { + shapes.swap(0, shape_idx); + difference.append(&mut composite_boolean_operation(BooleanOperation::SubtractFront, shapes)?); + } + Ok(difference) + } + BooleanOperation::SubtractBack => unreachable!("composite boolean operation: unreachable subtract from back"), + } +} + // TODO: check if shapes are filled // TODO: Bug: shape with at least two subpaths and comprised of many unions sometimes has erroneous movetos embedded in edges -pub fn boolean_operation(select: BooleanOperation, mut alpha: ShapeLayer, mut beta: ShapeLayer) -> Result, BooleanOperationError> { +pub fn boolean_operation(mut select: BooleanOperation, alpha: &mut ShapeLayer, beta: &mut ShapeLayer) -> Result, BooleanOperationError> { if alpha.path.is_empty() || beta.path.is_empty() { return Err(BooleanOperationError::InvalidSelection); } + if select == BooleanOperation::SubtractBack { + select = BooleanOperation::SubtractFront; + swap(alpha, beta); + } alpha.path = close_path(&alpha.path); beta.path = close_path(&beta.path); let beta_reverse = close_path(&reverse_path(&beta.path)); @@ -487,10 +570,10 @@ pub fn boolean_operation(select: BooleanOperation, mut alpha: ShapeLayer, mut be // If shape is inside the other the Union is just the larger // Check could also be done with area and single ray cast if cast_horizontal_ray(point_on_curve(&beta.path), &alpha.path) % 2 != 0 { - Ok(vec![alpha]) + Ok(vec![alpha.clone()]) } else if cast_horizontal_ray(point_on_curve(&alpha.path), &beta.path) % 2 != 0 { - beta.style = alpha.style; - Ok(vec![beta]) + beta.style = alpha.style.clone(); + Ok(vec![beta.clone()]) } else { Err(BooleanOperationError::NothingDone) } @@ -528,10 +611,10 @@ pub fn boolean_operation(select: BooleanOperation, mut alpha: ShapeLayer, mut be Err(BooleanOperationError::NoIntersections) => { // Check could also be done with area and single ray cast if cast_horizontal_ray(point_on_curve(&beta.path), &alpha.path) % 2 != 0 { - beta.style = alpha.style; - Ok(vec![beta]) + beta.style = alpha.style.clone(); + Ok(vec![beta.clone()]) } else if cast_horizontal_ray(point_on_curve(&alpha.path), &beta.path) % 2 != 0 { - Ok(vec![alpha]) + Ok(vec![alpha.clone()]) } else { Err(BooleanOperationError::NothingDone) } @@ -540,23 +623,7 @@ pub fn boolean_operation(select: BooleanOperation, mut alpha: ShapeLayer, mut be } } BooleanOperation::SubtractBack => { - match if beta_dir != alpha_dir { - PathGraph::from_paths(&alpha.path, &beta.path) - } else { - PathGraph::from_paths(&alpha.path, &beta_reverse) - } { - Ok(graph) => collect_shapes(&graph, &mut graph.get_cycles(), |dir| dir != alpha_dir, |_| &beta.style), - Err(BooleanOperationError::NoIntersections) => { - if cast_horizontal_ray(point_on_curve(&alpha.path), &beta.path) % 2 != 0 { - add_subpath(&mut beta.path, if beta_dir == alpha_dir { reverse_path(&alpha.path) } else { alpha.path }); - beta.style = alpha.style; - Ok(vec![beta]) - } else { - Err(BooleanOperationError::NothingDone) - } - } - Err(err) => Err(err), - } + unreachable!("Boolean operation: unreachable subtract from back"); } BooleanOperation::SubtractFront => { match if beta_dir != alpha_dir { @@ -567,8 +634,8 @@ pub fn boolean_operation(select: BooleanOperation, mut alpha: ShapeLayer, mut be Ok(graph) => collect_shapes(&graph, &mut graph.get_cycles(), |dir| dir == alpha_dir, |_| &alpha.style), Err(BooleanOperationError::NoIntersections) => { if cast_horizontal_ray(point_on_curve(&beta.path), &alpha.path) % 2 != 0 { - add_subpath(&mut alpha.path, if beta_dir == alpha_dir { reverse_path(&beta.path) } else { beta.path }); - Ok(vec![alpha]) + add_subpath(&mut alpha.path, if beta_dir == alpha_dir { reverse_path(&beta.path) } else { beta.path.clone() }); + Ok(vec![alpha.clone()]) } else { Err(BooleanOperationError::NothingDone) } @@ -579,23 +646,16 @@ pub fn boolean_operation(select: BooleanOperation, mut alpha: ShapeLayer, mut be } } -// TODO less hacky way to handle double counts on shared endpoints // TODO check bounding boxes more rigorously -pub fn cast_horizontal_ray(mut from: Point, into: &BezPath) -> usize { - // In practice, this makes it less likely that a ray will intersect with shared point between two curves - from.y += RAY_FUDGE_FACTOR; - - let ray = Line { +pub fn cast_horizontal_ray(from: Point, into: &BezPath) -> usize { + let mut ray = PathSeg::Line(Line { p0: from, - p1: Point { - x: from.x + 1.0, - y: from.y + RAY_FUDGE_FACTOR, - }, - }; + p1: Point { x: from.x + 1.0, y: from.y }, + }); let mut intersects = Vec::new(); - for ref seg in into.segments() { + for ref mut seg in into.segments() { if seg.bounding_box().x1 > from.x { - line_curve_intersections(&ray, seg, true, |_, b| valid_t(b), &mut intersects); + line_curve_intersections((&mut ray, seg), |_, b| valid_t(b), &mut intersects); } } intersects.len() @@ -636,7 +696,8 @@ where shapes.push(graph.get_shape(cycle, style(dir))); } } - Err(err) => return Err(err), + // Exclude cycles with 0.0 area + Err(_err) => (), } } Ok(shapes) @@ -675,7 +736,6 @@ pub fn reverse_path(path: &BezPath) -> BezPath { } } curve.append(&mut temp.into_iter().rev().collect()); - log::debug!("{:?}", BezPath::from_path_segments(curve.clone().into_iter())); BezPath::from_path_segments(curve.into_iter()) } diff --git a/graphene/src/consts.rs b/graphene/src/consts.rs index 81373ba1..f3d0d66a 100644 --- a/graphene/src/consts.rs +++ b/graphene/src/consts.rs @@ -7,11 +7,5 @@ pub const LAYER_OUTLINE_STROKE_WEIGHT: f64 = 1.; // BOOLEAN OPERATIONS // Bezier curve intersection algorithm -pub const F64PRECISE: f64 = f64::EPSILON * 100.0; // for f64 comparisons, to allow for rounding error -pub const F64LOOSE: f64 = f64::EPSILON * 1000000.0; // == 0.0000000002220446049250313 - -// A bezier curve whose `available_precision()` is greater than CURVE_FIDELITY can be evaluated at least 10000 "unique" locations -pub const CURVE_FIDELITY: f64 = F64PRECISE * 100.0; - -// In practice, this makes it less likely that a ray will intersect with a common anchor point between two curves -pub const RAY_FUDGE_FACTOR: f64 = 0.00001; +pub const F64PRECISE: f64 = f64::EPSILON * ((1 << 7) as f64); // ~= 2^(-45) - For f64 comparisons to allow for rounding error; note that f64::EPSILON ~= 2^(-52) +pub const F64LOOSE: f64 = f64::EPSILON * ((1 << 20) as f64); // ~= 2^(-32) - For comparisons between values that are a result of complex computations where error accumulates diff --git a/graphene/src/document.rs b/graphene/src/document.rs index 922faf84..66d10d96 100644 --- a/graphene/src/document.rs +++ b/graphene/src/document.rs @@ -1,4 +1,4 @@ -use crate::boolean_ops::boolean_operation; +use crate::boolean_ops::composite_boolean_operation; use crate::intersection::Quad; use crate::layers; use crate::layers::folder_layer::FolderLayer; @@ -12,6 +12,7 @@ use crate::{DocumentError, DocumentResponse, Operation}; use glam::{DAffine2, DVec2}; use kurbo::Affine; use serde::{Deserialize, Serialize}; +use std::cell::RefCell; use std::cmp::max; use std::collections::hash_map::DefaultHasher; use std::collections::HashMap; @@ -626,21 +627,9 @@ impl Document { Some([vec![DocumentChanged, CreatedLayer { path: path.clone() }], update_thumbnails_upstream(&path)].concat()) } Operation::BooleanOperation { operation, selected } => { - // TODO: proper difference - // TODO: proper style selection (done?) - // TODO: should generate symmetrical code - // TODO: Operations on any number of shapes - // TODO: boolean ops on any number of shapes - // TODO: handle overlapping identical curve case - // TODO: precision reached without intersection bug (maybe caused by separating a closed path, or dragging handles) - // TODO: click on shape should drag the shape - // TODO: add ability to undo let mut responses = Vec::new(); - if selected.len() > 1 && selected.len() < 3 { - // ? apparently `selected` should be reversed - let mut shapes = self.transformed_shapes(&selected)?; - let mut shape_drain = shapes.drain(..).rev(); - let new_shapes = boolean_operation(operation, shape_drain.next().unwrap(), shape_drain.next().unwrap())?; + if selected.len() > 1 { + let new_shapes = composite_boolean_operation(operation, &mut self.transformed_shapes(&selected)?.into_iter().rev().map(RefCell::new).collect())?; for path in selected { self.delete(&path)?; diff --git a/graphene/src/intersection.rs b/graphene/src/intersection.rs index c7320471..5ffc586b 100644 --- a/graphene/src/intersection.rs +++ b/graphene/src/intersection.rs @@ -1,13 +1,10 @@ -use core::panic; -use std::ops::Mul; +use crate::boolean_ops::{split_path_seg, subdivide_path_seg}; +use crate::consts::{F64LOOSE, F64PRECISE}; -use crate::{ - boolean_ops::split_path_seg, - boolean_ops::subdivide_path_seg, - consts::{CURVE_FIDELITY, F64LOOSE, F64PRECISE}, -}; use glam::{DAffine2, DMat2, DVec2}; -use kurbo::{BezPath, CubicBez, Line, ParamCurve, ParamCurveExtrema, PathSeg, Point, QuadBez, Rect, Shape, Vec2}; +use kurbo::{BezPath, CubicBez, Line, ParamCurve, ParamCurveDeriv, ParamCurveExtrema, PathSeg, Point, QuadBez, Rect, Shape, Vec2}; +use std::collections::VecDeque; +use std::ops::Mul; #[derive(Debug, Clone, Default, Copy)] /// A quad defined by four vertices. @@ -60,26 +57,27 @@ fn to_point(vec: DVec2) -> Point { /// Return `true` if `quad` intersects `shape`. /// This is the case if any of the following conditions are true: -/// * the edges of `quad` and `shape` intersect -/// * `shape` is entirely contained within `quad` -/// * `filled` is `true` and `quad` is entirely contained within `shape`. +/// - the edges of `quad` and `shape` intersect +/// - `shape` is entirely contained within `quad` +/// - `filled` is `true` and `quad` is entirely contained within `shape`. pub fn intersect_quad_bez_path(quad: Quad, shape: &BezPath, filled: bool) -> bool { let mut shape = shape.clone(); - // for filled shapes act like shape was closed even if it isn't + + // For filled shapes act like shape was closed even if it isn't if filled && shape.elements().last() != Some(&kurbo::PathEl::ClosePath) { shape.close_path(); } - // check if outlines intersect + // Check if outlines intersect if shape.segments().any(|path_segment| quad.lines().iter().any(|line| !path_segment.intersect_line(*line).is_empty())) { return true; } - // check if selection is entirely within the shape + // Check if selection is entirely within the shape if filled && shape.contains(to_point(quad.0[0])) { return true; } - // check if shape is entirely within selection + // Check if shape is entirely within selection get_arbitrary_point_on_path(&shape).map(|shape_point| quad.path().contains(shape_point)).unwrap_or_default() } @@ -93,12 +91,12 @@ pub fn get_arbitrary_point_on_path(path: &BezPath) -> Option { }) } -/// \/ \/ -/// Bezier Curve Intersection algorithm -/// \/ \/ +// +// Bezier Curve Intersection algorithm +// /// Each intersection has two curves. This enum helps distinguished between the two. -// TODO: refactor so actual curve data and Origin aren't separate +// TODO: refactor so actual curve data and `Origin` aren't separate #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum Origin { Alpha, @@ -122,7 +120,6 @@ pub struct Intersect { pub t_b: f64, pub a_seg_index: i32, pub b_seg_index: i32, - // TODO: remove the `quality` field pub quality: f64, } @@ -171,16 +168,18 @@ impl From<(Point, f64, f64)> for Intersect { } } +#[derive(Clone, Copy)] struct SubCurve<'a> { pub curve: &'a PathSeg, pub start_t: f64, pub end_t: f64, - local: [Point; 2], // local endpoints + /// Local endpoints + local: [Point; 2], pub extrema: &'a Vec<(Point, f64)>, } impl<'a> SubCurve<'a> { - // TODO: Fix this Clippy lint error + /// Extrema given by [SubCurve::subcurve_extrema], they are stored externally so they don't have to recalculated pub fn new(parent: &'a PathSeg, extrema: &'a Vec<(Point, f64)>) -> Self { SubCurve { curve: parent, @@ -191,6 +190,16 @@ impl<'a> SubCurve<'a> { } } + pub fn subcurve_extrema(parent: &PathSeg) -> Vec<(Point, f64)> { + // Extrema at endpoints should not be included here as they must be calculated for each subcurve + // Note: below filtering may filter out extrema near the endpoints + parent + .extrema() + .iter() + .filter_map(|t| if *t > F64PRECISE && *t < 1.0 - F64PRECISE { Some((parent.eval(*t), *t)) } else { None }) + .collect() + } + fn bounding_box(&self) -> Rect { let mut bound = Rect { x0: self.start().x, @@ -203,6 +212,7 @@ impl<'a> SubCurve<'a> { .chain( self.extrema .iter() + // Filter out "internal extrema which are not contained within this subcurve" .filter_map(|place_time| if place_time.1 > self.start_t && place_time.1 < self.end_t { Some(&place_time.0) } else { None }), ) .for_each(|p| { @@ -227,7 +237,7 @@ impl<'a> SubCurve<'a> { } /// Split subcurve at `t`, as though the subcurve is a bezier curve, where `t` is a value between `0.0` and `1.0`. - fn split(&self, t: f64) -> (SubCurve, SubCurve) { + fn split<'sub_life>(self: &'sub_life SubCurve<'a>, t: f64) -> (SubCurve<'a>, SubCurve<'a>) { let split_t = self.start_t + t * (self.end_t - self.start_t); ( SubCurve { @@ -257,42 +267,54 @@ impl<'a> SubCurve<'a> { } // TODO: use the cool algorithm described in: https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.99.9678&rep=rep1&type=pdf -// Bezier Curve Intersection Algorithm -// TODO: how does f64 precision affect the algorithm? -// Error correction schemes? -// TODO: profile algorithm -// TODO: intersections of overlapping curve -// If the algorithm is rewritten to be non-recursive it can be restructured to be more breadth first then depth first -// Test for overlapping curves by splitting the curves -// Behavior: deep recursion could result in stack overflow -// Improvement: intersections on the end of segments -// Improvement: more adaptive way to decide when "close enough" -// Optimization: any extra copying happening? +/// Bezier Curve Intersection Algorithm +fn path_intersections(a: &SubCurve, b: &SubCurve, intersections: &mut Vec) { + // At recursion depth 10: + // - maximum recursive execution paths = 4^10 = 1048576 + // - minimum recursive execution paths = 1 + // - up to 9 cubic Bezier intersections + // - `SubCurve` is 1/2^10 of the original curve + // - conservatively: there should never be more than 9 * 100 = 1000 total recursive execution paths + // TODO: Can probably be much less, should find a better capacity + const MAX_CALL_NUM: usize = 1000; + let mut call_buffer: VecDeque<(SubCurve, SubCurve)> = VecDeque::with_capacity(MAX_CALL_NUM); + let mut recursion = 1.0; -fn path_intersections(a: &SubCurve, b: &SubCurve, mut recursion: f64, intersections: &mut Vec) { - if overlap(&a.bounding_box(), &b.bounding_box()) { - if let (PathSeg::Line(line), _) = (a.curve, b) { - line_curve_intersections(line, b.curve, true, |a, b| valid_t(a) && valid_t(b), intersections); - return; - } - if let (_, PathSeg::Line(line)) = (a, b.curve) { - line_curve_intersections(line, a.curve, false, |a, b| valid_t(a) && valid_t(b), intersections); + fn helper<'a, 'b: 'a>(a: &'a SubCurve<'b>, b: &'a SubCurve<'b>, recursion: f64, intersections: &mut Vec, call_buffer: &'a mut VecDeque<(SubCurve<'b>, SubCurve<'b>)>) { + if let (PathSeg::Line(_), _) | (_, PathSeg::Line(_)) = (a.curve, b.curve) { + line_curve_intersections((&mut a.curve.clone(), &mut b.curve.clone()), |a, b| valid_t(a) && valid_t(b), intersections); return; } // We are close enough to try linear approximation if recursion < (1 << 10) as f64 { + // If the number of sub-curves being checked could exceed the threshold, check for overlap + if call_buffer.len() >= MAX_CALL_NUM - 4 { + overlapping_curve_intersections(a.curve, b.curve) + .into_iter() + .flatten() + .for_each(|intersect| intersections.push(intersect)); + // Regardless of whether intersections were found, need to return to prevent crashing the editor + // If no intersections are found above the curves are very close to overlapping but not quite + return; + } if let Some(mut cross) = line_intersection(&Line { p0: a.start(), p1: a.end() }, &Line { p0: b.start(), p1: b.end() }) { // Intersection `t_value` equals the recursive `t_value` + interpolated intersection value - cross.t_a = a.start_t + cross.t_a * recursion; cross.t_b = b.start_t + cross.t_b * recursion; + cross.t_a = a.start_t + cross.t_a * recursion; cross.quality = guess_quality(a.curve, b.curve, &cross); // log::debug!("checking: {:?}", cross.quality); - if cross.quality <= CURVE_FIDELITY { - intersections.push(cross); + if cross.quality <= F64LOOSE { + // Invalid intersections should still be rejected + // Rejects "valid" intersections on the non-inclusive end of a `PathSeg` + if valid_t(cross.t_a) && valid_t(cross.t_b) { + intersections.push(cross); + } return; } + // Eventually the points in the curve become too close together to split the curve meaningfully + // Return the best estimate of intersection regardless of quality // Also provides a base case and prevents infinite recursion if a.available_precision() <= F64PRECISE || b.available_precision() <= F64PRECISE { log::debug!("precision reached"); @@ -300,7 +322,6 @@ fn path_intersections(a: &SubCurve, b: &SubCurve, mut recursion: f64, intersecti return; } } - // Alternate base case // Note: may occur for the less forgiving side of a `PathSeg` endpoint intersect if a.available_precision() <= F64PRECISE || b.available_precision() <= F64PRECISE { @@ -308,28 +329,71 @@ fn path_intersections(a: &SubCurve, b: &SubCurve, mut recursion: f64, intersecti return; } } - recursion /= 2.0; let (a1, a2) = a.split(0.5); let (b1, b2) = b.split(0.5); - path_intersections(&a1, &b1, recursion, intersections); - path_intersections(&a1, &b2, recursion, intersections); - path_intersections(&a2, &b1, recursion, intersections); - path_intersections(&a2, &b2, recursion, intersections); + + if overlap(&a1.bounding_box(), &b1.bounding_box()) { + call_buffer.push_back((a1, b1)); + } + if overlap(&a1.bounding_box(), &b2.bounding_box()) { + call_buffer.push_back((a1, b2)); + } + if overlap(&a2.bounding_box(), &b1.bounding_box()) { + call_buffer.push_back((a2, b1)); + } + if overlap(&a2.bounding_box(), &b2.bounding_box()) { + call_buffer.push_back((a2, b2)); + } + } + + call_buffer.push_back((*a, *b)); + while !call_buffer.is_empty() { + let mut current_level = call_buffer.len(); + while current_level > 0 { + let (a, b) = call_buffer.pop_front().unwrap(); + helper(&a, &b, recursion, intersections, &mut call_buffer); + recursion /= 2.0; + current_level -= 1; + } } } -pub fn line_curve_intersections(line: &Line, curve: &PathSeg, is_line_a: bool, t_validate: F, intersections: &mut Vec) +/// Does nothing when neither PathSeg in `line_curve` is a line. +/// Closure `t_validate` takes the two t_values of an Intersect as arguments. +/// The order of the t_values corresponds with the order of the PathSegs in `line_curve`, +/// `t_validate` should return true for allowable intersection t_values, valid intersections will be added to `intersections`. +pub fn line_curve_intersections(line_curve: (&mut PathSeg, &mut PathSeg), t_validate: F, intersections: &mut Vec) where F: Fn(f64, f64) -> bool, { - if let (line, PathSeg::Line(line2)) = (line, curve) { + extend_curve(line_curve.0, F64PRECISE); + extend_curve(line_curve.1, F64PRECISE); + + if let (PathSeg::Line(line), PathSeg::Line(line2)) = line_curve { if let Some(cross) = line_intersection(line, line2) { if t_validate(cross.t_a, cross.t_b) { intersections.push(cross); } + } else { + //the lines may be overlapping + overlapping_curve_intersections(line_curve.0, line_curve.1) + .into_iter() + .flatten() + .for_each(|intersect| intersections.push(intersect)) } } else { - // Forced to construct a `Vec` here because match arms must return same type, and E0716 + let is_line_a; + let (line, curve) = match line_curve { + (PathSeg::Line(line), curve) => { + is_line_a = true; + (line, curve) + } + (curve, PathSeg::Line(line)) => { + is_line_a = false; + (line, curve) + } + _ => return, + }; let roots = match curve { PathSeg::Quad(quad) => Vec::from(quad_line_intersect(line, quad)), PathSeg::Cubic(cubic) => Vec::from(cubic_line_intersect(line, cubic)), @@ -345,7 +409,7 @@ where PathSeg::Quad(quad) => quad.eval(*time), _ => Point::new(0.0, 0.0), // Should never occur }; - // the intersection point should be on the line, unless FP math error produces bad results + // The intersection point should be on the line, unless floating point math error produces bad results let line_time = projection_on_line(line, &point); if !t_validate(line_time, *time) { return None; @@ -364,6 +428,23 @@ where } } +/// Extend the starting point of `curve` backwards along its derivative. +/// Used to make finding intersections near endpoints reliable. +pub fn extend_curve(curve: &mut PathSeg, distance: f64) { + fn extended_start(c: &mut C, d: f64) -> Point { + let mut c_prime = c.deriv().eval(0.0); + c_prime.x *= d / c_prime.distance(Point::ORIGIN); + c_prime.y *= d / c_prime.distance(Point::ORIGIN); + let es_vec = c.eval(0.0) - c_prime; + Point { x: es_vec.x, y: es_vec.y } + } + match curve { + PathSeg::Line(line) => line.p0 = extended_start(line, distance), + PathSeg::Quad(quad) => quad.p0 = extended_start(quad, distance), + PathSeg::Cubic(cubic) => cubic.p0 = extended_start(cubic, distance), + }; +} + /// For quality Q in the worst case, the point on curve `a` corresponding to `guess` is distance Q from the point on curve `b`. // TODO: Optimization: inline? maybe.. fn guess_quality(a: &PathSeg, b: &PathSeg, guess: &Intersect) -> f64 { @@ -372,15 +453,17 @@ fn guess_quality(a: &PathSeg, b: &PathSeg, guess: &Intersect) -> f64 { at_a.distance(guess.point) + at_b.distance(guess.point) } -/// Returns either [None, None] or [Some(_), Some(_)], -/// The above may not be true when either a or b is very small (their endpoints are close together), so that when the algorithm does curve splitting -/// TODO: test this -/// TODO: use this +/// If curves overlap, returns intersections corresponding to the endpoints of the overlapping section +/// +/// # Panics +/// May panic if either curve is very short, or has endpoints which are close together +// TODO: test the case where a and b are identical +// TODO: test this, especially the overlapping curve cases which are more complex pub fn overlapping_curve_intersections(a: &PathSeg, b: &PathSeg) -> [Option; 2] { // To check if two curves overlap we find if the endpoints of either curve are on the other curve. // Then, the curves are split at these points, if the resulting control polygons match the curves are the same - let mut b_on_a: Vec> = [point_t_value(a, &b.start()), point_t_value(a, &b.end())].into_iter().collect(); - let mut a_on_b: Vec> = [point_t_value(b, &a.start()), point_t_value(b, &a.end())].into_iter().collect(); + let b_on_a: Vec> = [point_t_value(a, &b.start()), point_t_value(a, &b.end())].into_iter().collect(); + let a_on_b: Vec> = [point_t_value(b, &a.start()), point_t_value(b, &a.end())].into_iter().collect(); // I think, but have not mathematically shown, that if a and b are parts of the same curve then b_on_a and a_on_b should together have no more than three non-None elements. Which occurs when a or b is a cubic bezier which crosses itself let b_on_a_not_none = b_on_a.iter().filter_map(|o| *o).count(); let a_on_b_not_none = a_on_b.iter().filter_map(|o| *o).count(); @@ -388,66 +471,73 @@ pub fn overlapping_curve_intersections(a: &PathSeg, b: &PathSeg) -> [Option { let (t1a, t1b, t2a, t2b): (f64, f64, f64, f64); let to_compare = if b_on_a_not_none == 2 { - b_on_a.sort_by(|val1, val2| (val1).partial_cmp(val2).unwrap_or(std::cmp::Ordering::Less)); - let mut a_ts = b_on_a.iter_mut().filter_map(|o| *o).collect::>(); - t1a = a_ts[0]; + t1a = b_on_a[0].unwrap(); t1b = 0.0; - t2a = a_ts[1]; + t2a = b_on_a[1].unwrap(); t2b = 1.0; - (*b, subdivide_path_seg(a, a_ts.as_mut_slice())[1].unwrap()) + let mut split_at = if t1a > t2a { [t2a, t1a] } else { [t1a, t2a] }; + (*b, subdivide_path_seg(a, &mut split_at)[1].unwrap()) } else if a_on_b_not_none == 2 { - a_on_b.sort_by(|val1, val2| (val1).partial_cmp(val2).unwrap_or(std::cmp::Ordering::Less)); - let mut b_ts = a_on_b.iter_mut().filter_map(|o| *o).collect::>(); t1a = 0.0; - t1b = b_ts[0]; + t1b = a_on_b[0].unwrap(); t2a = 1.0; - t2b = b_ts[1]; - (*a, subdivide_path_seg(a, b_ts.as_mut_slice())[1].unwrap()) + t2b = a_on_b[1].unwrap(); + let mut split_at = if t1b > t2b { [t2b, t1b] } else { [t1b, t2b] }; + (*a, subdivide_path_seg(b, &mut split_at)[1].unwrap()) } else { - ( - match (b_on_a[0], b_on_a[1], a_on_b[0], a_on_b[1]) { - (None, Some(_), _, Some(t_val)) | (None, Some(_), Some(t_val), _) => { - t1b = t_val; - t2b = 1.0; - split_path_seg(b, t_val).1.unwrap() - } - (Some(_), None, _, Some(t_val)) | (Some(_), None, Some(t_val), _) => { - t1b = 0.0; - t2b = t_val; - split_path_seg(b, t_val).0.unwrap() - } - _ => panic!(), - }, - match (a_on_b[0], a_on_b[1], b_on_a[0], b_on_a[1]) { - (None, Some(_), _, Some(t_val)) | (None, Some(_), Some(t_val), _) => { - t1a = t_val; - t2a = 1.0; - split_path_seg(a, t_val).1.unwrap() - } - (Some(_), None, _, Some(t_val)) | (Some(_), None, Some(t_val), _) => { - t1a = 0.0; - t2a = t_val; - split_path_seg(a, t_val).0.unwrap() - } - _ => panic!(), - }, - ) + match (b_on_a[0], b_on_a[1], a_on_b[0], a_on_b[1]) { + (None, Some(a_val), None, Some(b_val)) => { + t1b = b_val; + t2b = 1.0; + t1a = 1.0; + t2a = a_val; + (split_path_seg(b, b_val).1.unwrap(), split_path_seg(a, a_val).1.unwrap()) + } + (None, Some(a_val), Some(b_val), None) => { + t1b = b_val; + t2b = 1.0; + t1a = 0.0; + t2a = a_val; + (split_path_seg(b, b_val).1.unwrap(), split_path_seg(a, a_val).0.unwrap()) + } + (Some(a_val), None, None, Some(b_val)) => { + t1b = 0.0; + t2b = b_val; + t1a = a_val; + t2a = 1.0; + (split_path_seg(b, b_val).0.unwrap(), split_path_seg(a, a_val).1.unwrap()) + } + (Some(a_val), None, Some(b_val), None) => { + t1b = 0.0; + t2b = b_val; + t1a = a_val; + t2a = 0.0; + (split_path_seg(b, b_val).0.unwrap(), split_path_seg(a, a_val).0.unwrap()) + } + _ => unreachable!("Overlapping curve intersections: too many intersections for match arm"), + } }; + let mut to_return = [None, None]; if match_control_polygon(&to_compare.0, &to_compare.1) { - [Some(Intersect::from((to_compare.0.start(), t1a, t1b))), Some(Intersect::from((to_compare.0.end(), t2a, t2b)))] - } else { - [None, None] + if valid_t(t1a) && valid_t(t1b) { + to_return[0] = Some(Intersect::from((to_compare.0.start(), t1a, t1b))); + } + if valid_t(t2a) && valid_t(t2b) { + to_return[1] = Some(Intersect::from((to_compare.0.end(), t2a, t2b))); + } } + to_return } _ => [None, None], } } -/// Returns true if the Bezier curves described by A and B have the same control polygon -/// TODO: test this +/// Returns true if the Bezier curves described by `a` and `b` have the same control polygon. +/// The order of the polygon does not effect the result, pub fn match_control_polygon(a: &PathSeg, b: &PathSeg) -> bool { let mut a_polygon = get_control_polygon(a); let mut b_polygon = get_control_polygon(b); + // Allow matching of polygons whose points are reverse ordered if a_polygon.first().unwrap() == b_polygon.last().unwrap() && a_polygon.last().unwrap() == b_polygon.first().unwrap() { b_polygon.reverse() } @@ -486,7 +576,7 @@ pub fn match_control_polygon(a: &PathSeg, b: &PathSeg) -> bool { pub fn colinear(points: &[&Point]) -> bool { let ray = Line { p0: *points[0], p1: *points[1] }; for p in points.iter().skip(2) { - if line_t_value(&ray, p).is_none() { + if point_t_value(&PathSeg::Line(ray), p).is_none() { return false; } } @@ -501,18 +591,34 @@ pub fn get_control_polygon(a: &PathSeg) -> Vec { } } -/// if p in on pathseg a, returns Some(t_value) for p -/// in the edge case where the path crosses itself, and p is at the cross, the first t_value found (but not necessarily the smallest) is returned +/// If `p` in on `PathSeg` `a`, returns `Some(t_value)` for `p` in the edge case where the path crosses itself, +/// and `p` is at the cross, the first t_value found (but not necessarily the smallest `t_value`) is returned. +// TODO: create a trait or something for roots to remove duplicate code pub fn point_t_value(a: &PathSeg, p: &Point) -> Option { match a { - PathSeg::Line(line) => line_t_value(line, p), + PathSeg::Line(line) => { + let [mut p0, p1] = linear_bezier_coefficients(line); + p0 -= p.to_vec2(); + let x_root = linear_root(p0.x, p1.x); + let y_root = linear_root(p0.y, p1.y); + if let (Some(x_root_val), Some(y_root_val)) = (x_root, y_root) { + if (y_root_val - x_root_val).abs() < F64LOOSE { + return Some(x_root_val); + } + } + return None; + } PathSeg::Quad(quad) => { let [mut p0, p1, p2] = quadratic_bezier_coefficients(quad); p0 -= p.to_vec2(); let x_roots = quadratic_real_roots(p0.x, p1.x, p2.x); quadratic_real_roots(p0.y, p1.y, p2.y) .into_iter() - .find(|yt_option| x_roots.iter().any(|xt_option| yt_option.is_some() && xt_option.is_some() && (yt_option.unwrap() == xt_option.unwrap()))) + .find(|yt_option| { + x_roots + .iter() + .any(|xt_option| yt_option.is_some() && xt_option.is_some() && ((yt_option.unwrap() - xt_option.unwrap()).abs() < F64LOOSE)) + }) .flatten() } PathSeg::Cubic(cubic) => { @@ -521,10 +627,15 @@ pub fn point_t_value(a: &PathSeg, p: &Point) -> Option { let x_roots = cubic_real_roots(p0.x, p1.x, p2.x, p3.x); cubic_real_roots(p0.y, p1.y, p2.y, p3.y) .into_iter() - .find(|yt_option| x_roots.iter().any(|xt_option| yt_option.is_some() && xt_option.is_some() && (yt_option.unwrap() == xt_option.unwrap()))) + .find(|yt_option| { + x_roots + .iter() + .any(|xt_option| yt_option.is_some() && xt_option.is_some() && ((yt_option.unwrap() - xt_option.unwrap()).abs() < F64LOOSE)) + }) .flatten() } } + .and_then(|t| if valid_t(t) { Some(t) } else { None }) } pub fn intersections(a: &BezPath, b: &BezPath) -> Vec { @@ -534,26 +645,14 @@ pub fn intersections(a: &BezPath, b: &BezPath) -> Vec { let mut intersections: Vec = Vec::new(); // There is some duplicate computation of b_extrema here, but I doubt it's significant a.segments().enumerate().for_each(|(a_index, a_seg)| { - // Extrema at endpoints should not be included here as they must be calculated for each subcurve - // Note: below filtering may filter out extrema near the endpoints - let a_extrema = a_seg - .extrema() - .iter() - .filter_map(|t| if *t > F64PRECISE && *t < 1.0 - F64PRECISE { Some((a_seg.eval(*t), *t)) } else { None }) - .collect(); + let a_extrema = SubCurve::subcurve_extrema(&a_seg); b.segments().enumerate().for_each(|(b_index, b_seg)| { - let b_extrema = b_seg - .extrema() - .iter() - .filter_map(|t| if *t > F64PRECISE && *t < 1.0 - F64PRECISE { Some((b_seg.eval(*t), *t)) } else { None }) - .collect(); + let b_extrema = SubCurve::subcurve_extrema(&b_seg); let mut intersects = Vec::new(); - path_intersections(&SubCurve::new(&a_seg, &a_extrema), &SubCurve::new(&b_seg, &b_extrema), 1.0, &mut intersects); + path_intersections(&SubCurve::new(&a_seg, &a_extrema), &SubCurve::new(&b_seg, &b_extrema), &mut intersects); for mut path_intersection in intersects { - intersections.push({ - path_intersection.add_index(a_index.try_into().unwrap(), b_index.try_into().unwrap()); - path_intersection - }); + path_intersection.add_index(a_index.try_into().unwrap(), b_index.try_into().unwrap()); + intersections.push(path_intersection); } }) }); @@ -570,14 +669,15 @@ pub fn line_intersect_point(a: &Line, b: &Line) -> Option { /// Returns intersection point and `t` values, treating lines as Bezier curves. pub fn line_intersection(a: &Line, b: &Line) -> Option { - if let Some(intersect) = line_intersection_unchecked(a, b) { - if valid_t(intersect.t_a) && valid_t(intersect.t_b) { - Some(intersect) - } else { - None + match line_intersection_unchecked(a, b) { + Some(intersect) => { + if valid_t(intersect.t_a) && valid_t(intersect.t_b) { + Some(intersect) + } else { + None + } } - } else { - None + None => None, } } @@ -587,31 +687,11 @@ pub fn line_intersection_unchecked(a: &Line, b: &Line) -> Option { if slopes.determinant() == 0.0 { return None; } - let t_values = slopes.inverse() * DVec2::new((a.p0 - b.p0).x, (a.p0 - b.p0).y); + let t_values = slopes.inverse() * DVec2::new(a.p0.x - b.p0.x, a.p0.y - b.p0.y); Some(Intersect::from((b.eval(t_values[0]), t_values[1], t_values[0]))) } -/// if p in on line a, returns Some(t_value) for p -/// t_values seem to be accurate to roughly 7-10 decimal places -pub fn line_t_value(a: &Line, p: &Point) -> Option { - let from_x = (p.x - a.p0.x) / (a.p1.x - a.p0.x); - let from_y = (p.y - a.p0.y) / (a.p1.y - a.p0.y); - if !from_x.is_normal() { - if !from_y.is_normal() { - None - } else { - Some(from_y) - } - } else if !from_y.is_normal() { - Some(from_x) - } else if (from_x - from_y).abs() < F64LOOSE { - Some(0.5 * (from_x + from_y)) - } else { - None - } -} - -/// return the t_value of the point nearest to p on a +/// Returns the `t_value` of the point nearest to `p` on `a`. pub fn projection_on_line(a: &Line, p: &Point) -> f64 { let ray = a.p1.to_vec2() - a.p0.to_vec2(); ray.dot(p.to_vec2() - a.p0.to_vec2()) / ((ray.to_point().distance(Point::ORIGIN)) * (ray.to_point().distance(Point::ORIGIN))) @@ -651,7 +731,7 @@ pub fn quad_line_intersect(a: &Line, b: &QuadBez) -> [Option; 2] { } /// Returns real roots to cubic equation: `f(t) = a0 + t*a1 + t^2*a2 + t^3*a3`. -/// This function uses the Cardano-Viete and Numerical Recipes algorithm, found here: +/// This function uses the Cardano-Viete and Numerical Recipes algorithm, found here: https://quarticequations.com/Cubic.pdf pub fn cubic_real_roots(mut a0: f64, mut a1: f64, mut a2: f64, a3: f64) -> [Option; 3] { use std::f64::consts::FRAC_PI_3 as PI_3; @@ -688,8 +768,8 @@ pub fn cubic_real_roots(mut a0: f64, mut a1: f64, mut a2: f64, a3: f64) -> [Opti } } -/// a quadratic bezier can be written x = p0 + t*p1 + t^2*p2 + t^3*p3, where x, p0, p1, p2, and p3 are vectors -/// this function returns [p0, p1, p2, p3] +/// A quadratic bezier can be written `x = p0 + t*p1 + t^2*p2 + t^3*p3`, where `x`, `p0`, `p1`, `p2`, and `p3` are vectors. +/// This function returns `[p0, p1, p2, p3]`. pub fn cubic_bezier_coefficients(cubic: &CubicBez) -> [Vec2; 4] { let p0 = cubic.p0.to_vec2(); let p1 = cubic.p1.to_vec2(); @@ -702,7 +782,8 @@ pub fn cubic_bezier_coefficients(cubic: &CubicBez) -> [Vec2; 4] { [c0, c1, c2, c3] } -/// Returns real roots to quadratic equation: `f(t) = a0 + t*a1 + t^2*a2`. +/// Returns real roots to the quadratic equation: `f(t) = a0 + t*a1 + t^2*a2`. +// TODO: make numerically stable pub fn quadratic_real_roots(a0: f64, a1: f64, a2: f64) -> [Option; 2] { let radicand = a1 * a1 - 4.0 * a2 * a0; if radicand < 0.0 { @@ -711,8 +792,8 @@ pub fn quadratic_real_roots(a0: f64, a1: f64, a2: f64) -> [Option; 2] { [Some((-a1 + radicand.sqrt()) / (2.0 * a2)), Some((-a1 - radicand.sqrt()) / (2.0 * a2))] } -/// a quadratic bezier can be written x = p0 + t*p1 + t^2*p2, where x, p0, p1, and p2 are vectors -/// this function returns [p0, p1, p2] +/// A quadratic bezier can be written `x = p0 + t*p1 + t^2*p2`, where `x`, `p0`, `p1`, and `p2` are vectors. +/// This function returns `[p0, p1, p2]`. pub fn quadratic_bezier_coefficients(quad: &QuadBez) -> [Vec2; 3] { let p0 = quad.p0.to_vec2(); let p1 = quad.p1.to_vec2(); @@ -723,12 +804,23 @@ pub fn quadratic_bezier_coefficients(quad: &QuadBez) -> [Vec2; 3] { [c0, c1, c2] } -/// Returns root to linear equation: `f(t) = a0 + t*a1`. -pub fn linear_root(a0: f64, a1: f64) -> [Option; 1] { +/// Returns the root to the linear equation: `f(t) = a0 + t*a1`. +pub fn linear_root(a0: f64, a1: f64) -> Option { if a1 == 0.0 { - return [None]; + return None; } - [Some(-a0 / a1)] + if a1.is_infinite() { + return Some(a0); + } + Some(-a0 / a1) +} + +/// A line can be written `x = p0 + t*p1`, where `x`, `p0` and `p1` are vectors. +/// Returns `[p0, p1]`. +pub fn linear_bezier_coefficients(line: &Line) -> [Vec2; 2] { + let p0 = line.p0.to_vec2(); + let p1 = line.p1.to_vec2(); + [p0, p1 - p0] } /// Returns `true` if rectangles overlap, even if either rectangle has 0 area. @@ -738,16 +830,21 @@ pub fn overlap(a: &Rect, b: &Rect) -> bool { } /// Tests if a `t` value belongs to `[0.0, 1.0)`. -/// Uses F64PRECISE to allow a slightly larger range of values. +/// Uses [crate::consts::F64PRECISE] to allow a slightly larger range of values. pub fn valid_t(t: f64) -> bool { - t > -F64PRECISE && t < 1.0 + t > -F64PRECISE && t < (1.0 - F64PRECISE) } -/// Each of these tests has been visually, but not mathematically verified. -/// These tests are all ignored because each test looks for exact floating point comparisons, so isn't flexible to small adjustments in the algorithm. +/// Each of these tests have been visually, but not mathematically, verified. +/// These tests are all ignored because each test looks for exact floating point comparisons, so isn't tolerant to small adjustments in the algorithm. mod tests { - #[allow(unused_imports)] // This import is used + // These imports are used in the tests which are #[ignore] + #[allow(unused_imports)] use super::*; + #[allow(unused_imports)] + use crate::boolean_ops::point_on_curve; + #[allow(unused_imports)] + use std::{fs::File, io::Write}; /// Two intersect points, on different `PathSegs`. #[ignore] @@ -830,7 +927,7 @@ mod tests { assert!(expected.iter().zip(result.iter()).fold(true, |equal, (a, b)| equal && a == b)); } - /// Intersect points at ends of `PathSegs`. + /// Intersect points at ends of `PathSeg`s. #[ignore] #[test] fn curve_intersection_seg_edges() { @@ -892,4 +989,159 @@ mod tests { let p4 = Point { x: 720.297, y: 1443.594 }; assert!(colinear(&[&p1, &p2, &p3, &p4])); } + + #[test] + #[ignore] + fn test_point_t_value() { + let vertical_line = Line::new(Point::new(0.0, -10.0), Point::new(0.0, 10.0)); + let t_value = point_t_value(&PathSeg::Line(vertical_line), &Point::new(0.0, 1.0)); + assert_eq!(t_value.unwrap(), 0.55); + } + + #[test] + #[ignore] + fn test_kurbo_eval_stability() { + let mut test_results = File::create("..\\target\\debug\\test_kurbo_eval_results.txt").expect(""); + let test_curve = BezPath::from_svg("M-355.41190151646936 -204.93220299904385C-355.41190151646936 -164.32790664074417 -389.9224217662629 -131.4116207799262 -432.4933059063151 -131.4116207799262C-475.06419004636723 -131.4116207799262 -509.5747102961608 -164.32790664074417 -509.5747102961608 -204.93220299904382C-509.5747102961608 -245.53649935734347 -475.06419004636723 -278.45278521816147 -432.4933059063151 -278.45278521816147C-389.9224217662629 -278.45278521816147 -355.41190151646936 -245.5364993573435 -355.41190151646936 -204.93220299904385").expect("").segments().next().unwrap(); + let mut val = 0.0; + while val < 0.0 + 1000000.0 * f64::EPSILON { + writeln!(&mut test_results, "{:?}, {:?}", val, test_curve.eval(val).x).expect(""); + val += f64::EPSILON; + } + } + + #[test] + #[ignore] + fn test_quality_stability() { + let mut test_results = File::create("..\\target\\debug\\test_quality_results.txt").expect(""); + let mut val = 0.0; + + while val < 0.0 + 1000000.0 * f64::EPSILON { + let a = Line::new(Point::new(0.0, 0.0), Point::new(1.0 + val + val, 1.0 + val + val)); + let b = Line::new(Point::new(0.0, 1.0 + val + val), Point::new(1.0 + val + val, 0.0)); + let guess = Intersect::from((Point::new(0.5 + val, 0.5 + val), 0.5, 0.5)); + + writeln!(&mut test_results, "{:?}, {:?}", val, guess_quality(&PathSeg::Line(a), &PathSeg::Line(b), &guess)).expect(""); + val += f64::EPSILON; + } + } + + #[test] + #[ignore] + fn test_quality_cubic_stability() { + let mut test_results = File::create("..\\target\\debug\\test_quality_cubic_results.txt").expect(""); + let mut val = 0.0; + + while val < 0.0 + 1000000.0 * f64::EPSILON { + let a = PathSeg::Cubic(CubicBez::new( + Point::new(0.0 + val, 0.0), + Point::new(0.25 + val, 0.661437827766), + Point::new(0.75 + val, 0.968245836552), + Point::new(1.0 + val, 1.0), + )); + let b = PathSeg::Cubic(CubicBez::new( + Point::new(0.0, 1.0), + Point::new(0.25, 0.968245836552), + Point::new(0.75, 0.661437827766), + Point::new(1.0, 0.0), + )); + let guess = Intersect::from((Point::new(0.5 + val, 0.5 + val), 0.5 + val, 0.5 + val)); + + writeln!(&mut test_results, "{:?}, {:?}", val, guess_quality(&a, &b, &guess)).expect(""); + val += f64::EPSILON; + } + } + + #[test] + #[ignore] + fn test_line_intersection_stability() { + let mut test_results = File::create("..\\target\\debug\\test_line_intersect_results.txt").expect(""); + let mut val = 0.0; + + while val < 0.0 + 1000000.0 * f64::EPSILON { + let a = Line::new(Point::new(0.0 + val, 0.0), Point::new(1.0 + val, 1.0)); + let b = Line::new(Point::new(0.0, 1.0), Point::new(1.0, 0.0)); + + let line_intersection = line_intersection(&a, &b).unwrap(); + writeln!( + &mut test_results, + "{:?}, {:?}, {:?}, {:?}", + val, + line_intersection.t_a, + line_intersection.point.x, + guess_quality(&PathSeg::Line(a), &PathSeg::Line(b), &line_intersection) + ) + .expect(""); + val += f64::EPSILON; + } + } + + #[test] + #[ignore] + fn test_line_intersection_cancellation() { + let mut test_results = File::create("..\\target\\debug\\test_line_intersection_cancellation_results.txt").expect(""); + let val = 1.0; + let mut theta = F64PRECISE; + + while theta < std::f64::consts::FRAC_PI_2 - 0.1 { + let a = Line::new(Point::new(1.0, 1.0), Point::new(1.0 + val, 1.0 + val)); + let b = Line::new(Point::new(1.0, 1.0 + val * f64::cos(theta)), Point::new(1.0 + val, 1.0 + val * f64::sin(theta))); + + let line_intersection = line_intersection(&a, &b).unwrap(); + writeln!( + &mut test_results, + "{:?}, {:?}, {:?}, {:?}", + theta, line_intersection.t_a, line_intersection.t_b, line_intersection.point.x, + ) + .expect(""); + theta += f64::powf(2.0, 20.0) * F64LOOSE; + } + } + + #[test] + #[ignore] + fn test_intersections_stability() { + let mut test_results_intersection = File::create("..\\target\\debug\\test_curve_intersections_multi_results.txt").expect(""); + let mut val = 0.0; + + while val < 0.0 + 1000000.0 * f64::EPSILON { + let a = PathSeg::Cubic(CubicBez::new( + Point::new(0.0 + val, 0.0), + Point::new(0.25 + val, 0.661437827766), + Point::new(0.75 + val, 0.968245836552), + Point::new(1.0 + val, 1.0), + )); + let b = PathSeg::Cubic(CubicBez::new( + Point::new(0.0, 1.0), + Point::new(0.25, 0.968245836552), + Point::new(0.75, 0.661437827766), + Point::new(1.0, 0.0), + )); + let aex = SubCurve::subcurve_extrema(&a); + let bex = SubCurve::subcurve_extrema(&b); + let a_sub = SubCurve::new(&a, &aex); + let b_sub = SubCurve::new(&b, &bex); + let mut intersections = Vec::new(); + path_intersections(&a_sub, &b_sub, &mut intersections); + + writeln!( + &mut test_results_intersection, + "{:?}, {:?}, {:?}, {:?}", + val, + intersections.first().unwrap().point.x, + intersections.first().unwrap().quality, + intersections.first().unwrap().t_a + ) + .expect(""); + + val += f64::EPSILON; + } + } + + #[ignore] + #[test] + fn test_test_dir() { + use std::env::current_dir; + println!("{:?}", current_dir()); + } }