euv-core 0.56.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Integer 2D cell coordinate `(x, y)` inside a grid or flow field.
#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, PartialEq)]
pub struct GridCoord {
    /// Column index along the X axis.
    #[get(type(copy))]
    pub(crate) x: usize,
    /// Row index along the Y axis.
    #[get(type(copy))]
    pub(crate) y: usize,
}

/// Shortest-path output produced by `GridPathfinder`.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct GridPathResult {
    /// Ordered grid cells from start to goal inclusive.
    pub(crate) path: Vec<GridCoord>,
    /// Exact accumulated traversal cost along `path`.
    #[get(type(copy))]
    pub(crate) total_cost: f32,
    /// Number of nodes expanded from the open priority queue.
    #[get(type(copy))]
    pub(crate) nodes_expanded: usize,
}

/// Reusable generation-stamped scratch buffers for zero-allocation grid pathfinding.
#[derive(Clone, Debug, Default)]
pub(crate) struct GridPathScratch {
    /// Active search generation stamp avoiding O(W*H) array clears.
    pub(crate) generation: u32,
    /// Generation stamp marking cells discovered in the active search.
    pub(crate) seen_gen: Vec<u32>,
    /// Generation stamp marking cells closed in the active search.
    pub(crate) closed_gen: Vec<u32>,
    /// Best known g-score per flat cell index.
    pub(crate) g_scores: Vec<f32>,
    /// Parent flat cell index per visited cell.
    pub(crate) came_from: Vec<usize>,
    /// Binary min-heap of `(f_score, cell_index)` entries.
    pub(crate) open_heap: Vec<(f32, usize)>,
    /// Generation stamp marking active multi-goal target cells.
    pub(crate) goal_flags: Vec<u32>,
    /// Active multi-goal stamp counter.
    pub(crate) goal_gen: u32,
}

/// Weighted 2D grid pathfinder supporting A*, multi-goal Dijkstra, and Jump Point Search.
#[derive(Clone, Data, Debug)]
pub struct GridPathfinder {
    /// Number of grid columns.
    #[get(type(copy))]
    pub(crate) width: usize,
    /// Number of grid rows.
    #[get(type(copy))]
    pub(crate) height: usize,
    /// Minimum positive traversal cost across walkable cells for admissible heuristics.
    #[get(type(copy))]
    pub(crate) min_walkable_cost: f32,
    /// Whether every walkable cell shares the same traversal cost (enabling JPS).
    #[get(type(copy))]
    pub(crate) uniform_walkable: bool,
    /// Flat row-major traversal costs (`<= 0.0` or non-finite marks blocked cells).
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) costs: Vec<f32>,
    /// Reusable scratch storage keeping repeated queries allocation-free.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) scratch: GridPathScratch,
}

/// Convex 2D navigation polygon stored in counter-clockwise winding order.
#[derive(Clone, Data, Debug, PartialEq)]
pub struct NavPolygon2D {
    /// Counter-clockwise ordered polygon perimeter vertices.
    pub(crate) vertices: Vec<Vec2>,
    /// Positive traversal cost multiplier for this polygon.
    #[get(type(copy))]
    pub(crate) cost_multiplier: f32,
    /// Precomputed polygon centroid.
    #[get(type(copy))]
    pub(crate) center: Vec2,
}

/// Directed portal edge connecting one navigation polygon to an adjacent polygon.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct NavPortal2D {
    /// Neighbor polygon index across this portal.
    #[get(type(copy))]
    pub(crate) target_polygon: usize,
    /// Left portal endpoint when traversing outward into `target_polygon`.
    #[get(type(copy))]
    pub(crate) left: Vec2,
    /// Right portal endpoint when traversing outward into `target_polygon`.
    #[get(type(copy))]
    pub(crate) right: Vec2,
    /// Portal segment midpoint used for A* graph edge distances.
    #[get(type(copy))]
    pub(crate) midpoint: Vec2,
}

/// Reusable scratch buffers for `NavMesh2D` portal A* and funnel string-pulling.
#[derive(Clone, Debug, Default)]
pub(crate) struct NavMeshScratch {
    /// Active search generation stamp.
    pub(crate) generation: u32,
    /// Discovery generation stamp per polygon.
    pub(crate) seen_gen: Vec<u32>,
    /// Closed generation stamp per polygon.
    pub(crate) closed_gen: Vec<u32>,
    /// Best accumulated g-score per polygon.
    pub(crate) g_scores: Vec<f32>,
    /// Parent polygon index per visited polygon.
    pub(crate) came_from_poly: Vec<usize>,
    /// Portal index within parent adjacency list used to enter each polygon.
    pub(crate) came_from_portal: Vec<usize>,
    /// Binary min-heap of `(f_score, polygon_index)` entries.
    pub(crate) open_heap: Vec<(f32, usize)>,
    /// Reusable portal corridor buffer `(left, right)` passed to the funnel solver.
    pub(crate) portal_corridor: Vec<(Vec2, Vec2)>,
}

/// Convex polygon 2D navigation mesh with portal graph A* and Simple Stupid Funnel smoothing.
#[derive(Clone, Data, Debug)]
pub struct NavMesh2D {
    /// Convex polygons normalized to counter-clockwise winding.
    pub(crate) polygons: Vec<NavPolygon2D>,
    /// Outgoing portals per polygon index.
    pub(crate) adjacency: Vec<Vec<NavPortal2D>>,
    /// Reusable scratch storage for path queries.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) scratch: NavMeshScratch,
}

/// Reusable scratch priority queue for `FlowFieldGrid` integration builds.
#[derive(Clone, Debug, Default)]
pub(crate) struct FlowFieldScratch {
    /// Binary min-heap of `(integration_cost, cell_index)` entries.
    pub(crate) open_heap: Vec<(u32, usize)>,
}

/// Integration and flow-vector grid enabling O(1) steering lookup for thousands of crowd units.
#[derive(Clone, Data, Debug)]
pub struct FlowFieldGrid {
    /// Number of columns.
    #[get(type(copy))]
    pub(crate) width: usize,
    /// Number of rows.
    #[get(type(copy))]
    pub(crate) height: usize,
    /// World-space size of each square grid cell.
    #[get(type(copy))]
    pub(crate) cell_size: f32,
    /// World-space origin `(min_x, min_y)` of cell `(0, 0)`.
    #[get(type(copy))]
    pub(crate) origin: Vec2,
    /// Per-cell movement cost (`1..=254` walkable, `255` impassable).
    pub(crate) costs: Vec<u8>,
    /// Dijkstra integration distance to the nearest goal (`u32::MAX` unreachable).
    pub(crate) integration: Vec<u32>,
    /// Unit flow direction vector per cell pointing downhill toward the nearest goal.
    pub(crate) flow_vectors: Vec<Vec2>,
    /// Reusable priority queue scratch buffer.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) scratch: FlowFieldScratch,
}

/// Crowd agent state for 2D Reciprocal Velocity Obstacles (ORCA / RVO2).
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct OrcaAgent2D {
    /// Current 2D world position.
    #[get(type(copy))]
    pub(crate) position: Vec2,
    /// Current 2D velocity vector.
    #[get(type(copy))]
    pub(crate) velocity: Vec2,
    /// Desired 2D preferred velocity toward the agent's waypoint or goal.
    #[get(type(copy))]
    pub(crate) preferred_velocity: Vec2,
    /// Agent collision disk radius.
    #[get(type(copy))]
    pub(crate) radius: f32,
    /// Maximum allowed speed.
    #[get(type(copy))]
    pub(crate) max_speed: f32,
    /// Maximum radius within which other agents are treated as local neighbors.
    #[get(type(copy))]
    pub(crate) neighbor_dist: f32,
    /// Collision anticipation lookahead horizon in seconds.
    #[get(type(copy))]
    pub(crate) time_horizon: f32,
}

/// Directed 2D half-plane constraint (`det(direction, v - point) >= 0` is feasible).
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct OrcaLine2D {
    /// Anchor point on the half-plane boundary.
    #[get(type(copy))]
    pub(crate) point: Vec2,
    /// Unit direction along the half-plane boundary (feasible region lies to the left).
    #[get(type(copy))]
    pub(crate) direction: Vec2,
}

/// Configuration defaults for `OrcaCrowdSolver`.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct OrcaCrowdConfig {
    /// Default neighbor detection radius for newly added agents.
    #[get(type(copy))]
    pub(crate) default_neighbor_dist: f32,
    /// Default collision lookahead horizon in seconds.
    #[get(type(copy))]
    pub(crate) default_time_horizon: f32,
    /// Maximum closest neighbors considered per agent during ORCA half-plane assembly.
    #[get(type(copy))]
    pub(crate) max_neighbors: usize,
}

/// Reusable linear-programming scratch buffers for zero-allocation ORCA crowd steps.
#[derive(Clone, Debug, Default)]
pub(crate) struct OrcaScratch {
    /// Per-agent active ORCA half-plane constraints.
    pub(crate) orca_lines: Vec<OrcaLine2D>,
    /// Projected half-planes used during infeasible LP fallback (`linear_program3`).
    pub(crate) proj_lines: Vec<OrcaLine2D>,
    /// Newly solved collision-free velocities staged before simultaneous commit.
    pub(crate) new_velocities: Vec<Vec2>,
    /// Per-agent sorted `(dist_sq, neighbor_index)` candidate scratch buffer.
    pub(crate) neighbor_indices: Vec<(f32, usize)>,
}

/// Multi-agent 2D ORCA / RVO2 collision-avoidance crowd solver with linear programming.
#[derive(Clone, Data, Debug)]
pub struct OrcaCrowdSolver {
    /// Solver default parameters.
    #[get(type(copy))]
    pub(crate) config: OrcaCrowdConfig,
    /// Registered crowd agents.
    pub(crate) agents: Vec<OrcaAgent2D>,
    /// Reusable LP and neighbor buffers.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) scratch: OrcaScratch,
}