Cavalier Contours

repository·master·Indexed 42 days ago

https://github.com/jbuckmccready/cavalier_contours

A Rust-based geometric library for high-performance 2D polyline operations. It provides tools for parallel offsetting, boolean operations (Union, Intersection, Difference, XOR), spatial indexing, and geometric analysis such as area and winding number calculations. The library supports polylines defined by line and arc segments and includes C FFI bindings for integration with C/C++ projects.

Tokens
27K
Snippets
80
Records
116
Agent score
53%

What's inside Cavalier Contours

  1. Overview of the Cavalier Contours UI

    master

    The cavalier_contours_ui package is a user interface application designed for visualizing, testing, and demonstrating the core cavalier_contours crate.

    It is built using the following technologies:

    • egui: An immediate mode GUI library.
    • egui_plot: A plotting widget for egui.
    • lyon: A path tessellation library used for filling concave and complex polygons.
  2. Core geometric features of Cavalier Contours

    master

    Cavalier Contours provides a suite of 2D geometric algorithms for polylines defined by line and arc segments (arcs are not approximated as line segments):

    • Parallel Offsetting: Works on open, closed, and self-intersecting polylines. Supports multi-polyline offsetting (shapes with islands).
    • Boolean Operations: Performs union, intersection, and difference between two closed polylines.
    • Containment & Intersection: Tests for polyline containment and segment intersection.
    • Spatial Indexing: Includes 2D spatial indexing to accelerate algorithms on high-vertex-count polylines.
    • Geometric Analysis: Calculates area, length, winding numbers (point-in-polyline), and provides redundant vertex removal.
  3. Understand the workspace structure

    master

    The project is organized into several crates depending on your integration needs:

    • cavalier_contours: The core Rust library containing the primary polyline algorithms and API.
    • cavalier_contours_ffi: C FFI bindings for integration with C, C++, and other languages.
    • cavalier_contours_ui: A WebAssembly-based UI demo built with egui.
    • examples: A collection of examples demonstrating library functionality.
  4. Run the Cavalier Contours UI in a web browser

    master

    To run the web version of the UI locally, you must have trunk installed and the wasm32-unknown-unknown Rust target added.

    1. Install trunk:
      cargo install trunk --locked
    2. Add the WASM target:
      rustup target add wasm32-unknown-unknown
    3. Start the development server:
      trunk serve
    4. Open your browser to http://localhost:8080/#dev.

    Note: The /#dev suffix is required to prevent the browser from loading a stale cached version of the application.

    cargo install trunk --locked
    rustup target add wasm32-unknown-unknown
    trunk serve
  5. Use the Cavalier Contours core geometry modules

    master

    Cavalier Contours is a 2D geometry library for polylines and shapes. It provides functionality for offsetting, combining shapes, computing areas, path lengths, and winding numbers. The library is organized into several key modules:

    • core: Contains the fundamental geometric primitives and types.
    • polyline: Provides tools specifically for working with polylines.
    • shape_algorithms: Contains advanced geometric algorithms for shape manipulation.

    Note: The library forbids unsafe_code to ensure memory safety.

  6. Analyze polyline intersections and slices

    master

    Beyond simple boolean results, you can analyze how two polylines interact by using internal processing functions (typically accessed via the process_for_boolean workflow). This is useful for debugging or custom geometric analysis.

    Key Analysis Capabilities

    • Intersection Points: Identify the exact coordinates where two polylines cross.
    • Overlapping Segments: Detect segments of one polyline that lie on or within the other.
    • Slicing: Break polylines into individual segments (slices) at every intersection point.

    Implementation Details

    Analysis often involves creating an AABB (Axis-Aligned Bounding Box) index for the first polyline to accelerate intersection lookups via create_approx_aabb_index() before calling process_for_boolean.

  7. How PlineView and PlineViewData work together

    master

    A PlineView is a lightweight, non-copying representation of a subpart or selection of a source polyline. It borrows the source polyline to provide access to vertex data and geometric operations without the overhead of duplicating the underlying data.

    To use a view, you follow a two-step process:

    1. Create PlineViewData: This structure holds the minimum metadata (indices, start/end points, bulge adjustments) required to define a contiguous subpart of a polyline. It is detached from the source and can be serialized.
    2. Create a PlineView: Call PlineViewData::view(&source) to create an active view that borrows the source polyline. The resulting PlineView implements the PlineSource trait, allowing you to use it in any function that accepts a polyline source.

    When you are finished with the view, you can call .detach() to consume the view and retrieve the underlying PlineViewData, releasing the borrow on the source polyline.

    // 1. Create view data (e.g., from slice points)
    let view_data = PlineViewData::from_slice_points(
        &polyline,
        start_point,
        start_index,
        end_point,
        end_index,
        1e-5
    ).expect("slice not collapsed");
    
    // 2. Create the active view by borrowing the source
    let view = view_data.view(&polyline);
    
    // 3. Use the view as a polyline source
    let length = view.path_length();
    
    // 4. Detach to release the borrow
    let detached_data = view.detach();
  8. Understand the `LineLineIntr` intersection result types

    master

    The LineLineIntr<T> enum categorizes the relationship between two line segments:

    VariantDescription
    NoIntersectThe segments are parallel and not collinear, or they are distinct points.
    TrueIntersectThe segments intersect at exactly one point within their bounds. Contains seg1_t and seg2_t (parametric values for both segments).
    FalseIntersectThe infinite lines intersect, but at least one segment must be extended to reach the intersection point. Contains seg1_t and seg2_t.
    OverlappingThe segments are collinear and overlap. Contains seg2_t0 and seg2_t1, representing the start and end parametric values of the coincidence along the second segment.
    #[derive(Debug, Copy, Clone)]
    pub enum LineLineIntr<T> where T: Real {
        NoIntersect,
        TrueIntersect {
            seg1_t: T,
            seg2_t: T,
        },
        Overlapping {
            seg2_t0: T,
            seg2_t1: T,
        },
        FalseIntersect {
            seg1_t: T,
            seg2_t: T,
        },
    }