euv-core 0.57.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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use super::*;

/// Computes the sum of two 2D vectors.
///
/// # Arguments
///
/// - `Vec2` - Left 2D vector.
/// - `Vec2` - Right 2D vector.
///
/// # Returns
///
/// - `Vec2` - Component-wise sum.
pub(crate) fn pf_vec2_add(lhs: Vec2, rhs: Vec2) -> Vec2 {
    Vec2::new(lhs.get_x() + rhs.get_x(), lhs.get_y() + rhs.get_y())
}

/// Computes the difference `lhs - rhs` of two 2D vectors.
///
/// # Arguments
///
/// - `Vec2` - Left 2D vector.
/// - `Vec2` - Right 2D vector.
///
/// # Returns
///
/// - `Vec2` - Component-wise difference.
pub(crate) fn pf_vec2_sub(lhs: Vec2, rhs: Vec2) -> Vec2 {
    Vec2::new(lhs.get_x() - rhs.get_x(), lhs.get_y() - rhs.get_y())
}

/// Scales a 2D vector by scalar `factor`.
///
/// # Arguments
///
/// - `Vec2` - Input 2D vector.
/// - `f32` - Scalar multiplier.
///
/// # Returns
///
/// - `Vec2` - Scaled 2D vector.
pub(crate) fn pf_vec2_scale(vec: Vec2, factor: f32) -> Vec2 {
    Vec2::new(vec.get_x() * factor, vec.get_y() * factor)
}

/// Computes the 2D scalar cross product (determinant) `lhs.x * rhs.y - lhs.y * rhs.x`.
///
/// # Arguments
///
/// - `Vec2` - Left 2D vector.
/// - `Vec2` - Right 2D vector.
///
/// # Returns
///
/// - `f32` - Signed 2D determinant.
pub(crate) fn pf_vec2_det(lhs: Vec2, rhs: Vec2) -> f32 {
    lhs.get_x() * rhs.get_y() - lhs.get_y() * rhs.get_x()
}

/// Computes squared Euclidean length of a 2D vector.
///
/// # Arguments
///
/// - `Vec2` - Input 2D vector.
///
/// # Returns
///
/// - `f32` - Squared length.
pub(crate) fn pf_vec2_len_sq(vec: Vec2) -> f32 {
    vec.dot(vec)
}

/// Computes Euclidean length of a 2D vector.
///
/// # Arguments
///
/// - `Vec2` - Input 2D vector.
///
/// # Returns
///
/// - `f32` - Euclidean length.
pub(crate) fn pf_vec2_len(vec: Vec2) -> f32 {
    pf_vec2_len_sq(vec).sqrt()
}

/// Computes squared Euclidean distance between two 2D points.
///
/// # Arguments
///
/// - `Vec2` - First 2D point.
/// - `Vec2` - Second 2D point.
///
/// # Returns
///
/// - `f32` - Squared Euclidean distance.
pub(crate) fn pf_vec2_dist_sq(lhs: Vec2, rhs: Vec2) -> f32 {
    pf_vec2_len_sq(pf_vec2_sub(lhs, rhs))
}

/// Computes Euclidean distance between two 2D points.
///
/// # Arguments
///
/// - `Vec2` - First 2D point.
/// - `Vec2` - Second 2D point.
///
/// # Returns
///
/// - `f32` - Euclidean distance.
pub(crate) fn pf_vec2_dist(lhs: Vec2, rhs: Vec2) -> f32 {
    pf_vec2_dist_sq(lhs, rhs).sqrt()
}

/// Normalizes a 2D vector or returns zero if near-degenerate or non-finite.
///
/// # Arguments
///
/// - `Vec2` - Input 2D vector.
///
/// # Returns
///
/// - `Vec2` - Unit vector or `(0, 0)` when degenerate.
pub(crate) fn pf_vec2_normalize_or_zero(vec: Vec2) -> Vec2 {
    let len_sq: f32 = pf_vec2_len_sq(vec);
    if !len_sq.is_finite() || len_sq <= 1e-12 {
        return Vec2::zero();
    }
    let inv_len: f32 = 1.0 / len_sq.sqrt();
    pf_vec2_scale(vec, inv_len)
}

/// Clamps a 2D vector's magnitude to `max_len` while sanitizing non-finite inputs.
///
/// # Arguments
///
/// - `Vec2` - Input 2D vector.
/// - `f32` - Maximum allowed length.
///
/// # Returns
///
/// - `Vec2` - Clamped 2D vector.
pub(crate) fn pf_vec2_clamp_len(vec: Vec2, max_len: f32) -> Vec2 {
    if !vec.get_x().is_finite()
        || !vec.get_y().is_finite()
        || !max_len.is_finite()
        || max_len <= 0.0
    {
        return Vec2::zero();
    }
    let len_sq: f32 = pf_vec2_len_sq(vec);
    let max_sq: f32 = max_len * max_len;
    if len_sq > max_sq && len_sq > 1e-12 {
        pf_vec2_scale(vec, max_len / len_sq.sqrt())
    } else {
        vec
    }
}

/// Twice the signed area of 2D triangle `(a, b, c)` (positive for counter-clockwise winding).
///
/// # Arguments
///
/// - `Vec2` - Triangle vertex A.
/// - `Vec2` - Triangle vertex B.
/// - `Vec2` - Triangle vertex C.
///
/// # Returns
///
/// - `f32` - Twice the signed triangle area.
pub(crate) fn pf_tri_area_2(point_a: Vec2, point_b: Vec2, point_c: Vec2) -> f32 {
    let ab: Vec2 = pf_vec2_sub(point_b, point_a);
    let ac: Vec2 = pf_vec2_sub(point_c, point_a);
    pf_vec2_det(ab, ac)
}

/// Pushes `(priority, index)` onto an in-place binary min-heap stored in `heap`.
///
/// # Arguments
///
/// - `&mut Vec<(f32, usize)>` - Reusable min-heap buffer.
/// - `f32` - Priority key (smaller values pop first).
/// - `usize` - Payload node index.
pub(crate) fn min_heap_f32_push(heap: &mut Vec<(f32, usize)>, priority: f32, index: usize) {
    let mut pos: usize = heap.len();
    heap.push((priority, index));
    while pos > 0 {
        let parent: usize = (pos - 1) >> 1;
        if heap[pos].0 < heap[parent].0 {
            heap.swap(pos, parent);
            pos = parent;
        } else {
            break;
        }
    }
}

/// Pops the smallest-priority `(priority, index)` pair from an in-place binary min-heap.
///
/// # Arguments
///
/// - `&mut Vec<(f32, usize)>` - Reusable min-heap buffer.
///
/// # Returns
///
/// - `Option<(f32, usize)>` - Minimum-priority entry if the heap is non-empty.
pub(crate) fn min_heap_f32_pop(heap: &mut Vec<(f32, usize)>) -> Option<(f32, usize)> {
    if heap.is_empty() {
        return None;
    }
    let top: (f32, usize) = heap.swap_remove(0);
    let len: usize = heap.len();
    let mut pos: usize = 0;
    loop {
        let left: usize = (pos << 1) + 1;
        if left >= len {
            break;
        }
        let right: usize = left + 1;
        let smallest: usize = if right < len && heap[right].0 < heap[left].0 {
            right
        } else {
            left
        };
        if heap[smallest].0 < heap[pos].0 {
            heap.swap(pos, smallest);
            pos = smallest;
        } else {
            break;
        }
    }
    Some(top)
}

/// Pushes `(priority, index)` onto an in-place integer binary min-heap stored in `heap`.
///
/// # Arguments
///
/// - `&mut Vec<(u32, usize)>` - Reusable integer min-heap buffer.
/// - `u32` - Priority key (smaller values pop first).
/// - `usize` - Payload cell index.
pub(crate) fn min_heap_u32_push(heap: &mut Vec<(u32, usize)>, priority: u32, index: usize) {
    let mut pos: usize = heap.len();
    heap.push((priority, index));
    while pos > 0 {
        let parent: usize = (pos - 1) >> 1;
        if heap[pos].0 < heap[parent].0 {
            heap.swap(pos, parent);
            pos = parent;
        } else {
            break;
        }
    }
}

/// Pops the smallest-priority `(priority, index)` pair from an in-place integer binary min-heap.
///
/// # Arguments
///
/// - `&mut Vec<(u32, usize)>` - Reusable integer min-heap buffer.
///
/// # Returns
///
/// - `Option<(u32, usize)>` - Minimum-priority entry if the heap is non-empty.
pub(crate) fn min_heap_u32_pop(heap: &mut Vec<(u32, usize)>) -> Option<(u32, usize)> {
    if heap.is_empty() {
        return None;
    }
    let top: (u32, usize) = heap.swap_remove(0);
    let len: usize = heap.len();
    let mut pos: usize = 0;
    loop {
        let left: usize = (pos << 1) + 1;
        if left >= len {
            break;
        }
        let right: usize = left + 1;
        let smallest: usize = if right < len && heap[right].0 < heap[left].0 {
            right
        } else {
            left
        };
        if heap[smallest].0 < heap[pos].0 {
            heap.swap(pos, smallest);
            pos = smallest;
        } else {
            break;
        }
    }
    Some(top)
}

/// Computes an admissible heuristic distance between two grid coordinates scaled by `min_cost`.
///
/// # Arguments
///
/// - `GridCoord` - Source grid coordinate.
/// - `GridCoord` - Goal grid coordinate.
/// - `GridConnectivity` - Grid neighbor connectivity mode.
/// - `f32` - Minimum walkable cell cost across the grid.
///
/// # Returns
///
/// - `f32` - Admissible lower-bound path cost.
pub(crate) fn grid_heuristic(
    from: GridCoord,
    to: GridCoord,
    connectivity: GridConnectivity,
    min_cost: f32,
) -> f32 {
    let dx: f32 = from.get_x().abs_diff(to.get_x()) as f32;
    let dy: f32 = from.get_y().abs_diff(to.get_y()) as f32;
    let raw: f32 = match connectivity {
        GridConnectivity::FourWay => dx + dy,
        GridConnectivity::EightWay => {
            let diag: f32 = dx.min(dy);
            let straight: f32 = dx.max(dy) - diag;
            diag * SQRT_2 + straight
        }
    };
    raw * min_cost
}

/// Runs the Simple Stupid Funnel Algorithm to string-pull a taut path through 2D portals.
///
/// # Arguments
///
/// - `Vec2` - Start point inside the first corridor polygon.
/// - `Vec2` - Goal point inside the final corridor polygon.
/// - `&[(Vec2, Vec2)]` - Ordered `(left, right)` portal segments connecting corridor polygons.
/// - `&mut Vec<Vec2>` - Output waypoint buffer populated in start-to-goal order.
pub(crate) fn funnel_string_pull(
    start: Vec2,
    goal: Vec2,
    portals: &[(Vec2, Vec2)],
    out_path: &mut Vec<Vec2>,
) {
    out_path.clear();
    out_path.push(start);
    if pf_vec2_dist_sq(start, goal) <= 1e-10 {
        return;
    }
    let total_portals: usize = portals.len() + 1;
    let mut portal_apex: Vec2 = start;
    let mut portal_left: Vec2 = start;
    let mut portal_right: Vec2 = start;
    let mut left_index: usize = 0;
    let mut right_index: usize = 0;
    let mut step: usize = 0;
    while step < total_portals {
        let (left, right): (Vec2, Vec2) = if step < portals.len() {
            portals[step]
        } else {
            (goal, goal)
        };
        if pf_tri_area_2(portal_apex, portal_right, right) >= 0.0 {
            if pf_vec2_dist_sq(portal_apex, portal_right) <= 1e-10
                || pf_tri_area_2(portal_apex, portal_left, right) <= 0.0
            {
                portal_right = right;
                right_index = step;
            } else {
                if let Some(last) = out_path.last().copied()
                    && pf_vec2_dist_sq(last, portal_left) > 1e-10
                {
                    out_path.push(portal_left);
                }
                let apex_index: usize = left_index;
                portal_apex = portal_left;
                portal_right = portal_apex;
                left_index = apex_index;
                right_index = apex_index;
                step = apex_index + 1;
                continue;
            }
        }
        if pf_tri_area_2(portal_apex, portal_left, left) <= 0.0 {
            if pf_vec2_dist_sq(portal_apex, portal_left) <= 1e-10
                || pf_tri_area_2(portal_apex, portal_right, left) >= 0.0
            {
                portal_left = left;
                left_index = step;
            } else {
                if let Some(last) = out_path.last().copied()
                    && pf_vec2_dist_sq(last, portal_right) > 1e-10
                {
                    out_path.push(portal_right);
                }
                let apex_index: usize = right_index;
                portal_apex = portal_right;
                portal_left = portal_apex;
                left_index = apex_index;
                right_index = apex_index;
                step = apex_index + 1;
                continue;
            }
        }
        step += 1;
    }
    if let Some(last) = out_path.last().copied()
        && pf_vec2_dist_sq(last, goal) > 1e-10
    {
        out_path.push(goal);
    }
}

/// Solves a 1D linear program on `lines[line_no]` bounded by prior lines and disk `radius`.
///
/// # Arguments
///
/// - `&[OrcaLine2D]` - Ordered half-plane constraints.
/// - `usize` - Active line index being solved along.
/// - `f32` - Maximum velocity disk radius.
/// - `Vec2` - Target velocity or optimization direction.
/// - `bool` - Whether `opt_vel` represents a unit direction rather than a target point.
/// - `&mut Vec2` - Output optimal feasible velocity if solvable.
///
/// # Returns
///
/// - `bool` - Whether a feasible point exists on `lines[line_no]`.
pub(crate) fn orca_linear_program1(
    lines: &[OrcaLine2D],
    line_no: usize,
    radius: f32,
    opt_vel: Vec2,
    direction_opt: bool,
    result: &mut Vec2,
) -> bool {
    let active: OrcaLine2D = lines[line_no];
    let dot_product: f32 = active.get_point().dot(active.get_direction());
    let discriminant: f32 =
        dot_product * dot_product + radius * radius - pf_vec2_len_sq(active.get_point());
    if discriminant < 0.0 {
        return false;
    }
    let sqrt_disc: f32 = discriminant.sqrt();
    let mut t_left: f32 = -dot_product - sqrt_disc;
    let mut t_right: f32 = -dot_product + sqrt_disc;
    for prior in &lines[..line_no] {
        let denominator: f32 = pf_vec2_det(active.get_direction(), prior.get_direction());
        let numerator: f32 = pf_vec2_det(
            prior.get_direction(),
            pf_vec2_sub(active.get_point(), prior.get_point()),
        );
        if denominator.abs() <= ORCA_EPSILON {
            if numerator < 0.0 {
                return false;
            }
            continue;
        }
        let t_hit: f32 = numerator / denominator;
        if denominator >= 0.0 {
            t_right = t_right.min(t_hit);
        } else {
            t_left = t_left.max(t_hit);
        }
        if t_left > t_right {
            return false;
        }
    }
    let chosen_t: f32 = if direction_opt {
        if opt_vel.dot(active.get_direction()) > 0.0 {
            t_right
        } else {
            t_left
        }
    } else {
        let proj_t: f32 = active
            .get_direction()
            .dot(pf_vec2_sub(opt_vel, active.get_point()));
        proj_t.clamp(t_left, t_right)
    };
    *result = pf_vec2_add(
        active.get_point(),
        pf_vec2_scale(active.get_direction(), chosen_t),
    );
    true
}

/// Solves a 2D linear program over all ORCA half-plane constraints within disk `radius`.
///
/// # Arguments
///
/// - `&[OrcaLine2D]` - Ordered half-plane constraints.
/// - `f32` - Maximum velocity disk radius.
/// - `Vec2` - Target velocity or optimization direction.
/// - `bool` - Whether `opt_vel` represents a unit direction rather than a target point.
/// - `&mut Vec2` - Output optimal velocity updated incrementally.
///
/// # Returns
///
/// - `usize` - First failing constraint index, or `lines.len()` when fully feasible.
pub(crate) fn orca_linear_program2(
    lines: &[OrcaLine2D],
    radius: f32,
    opt_vel: Vec2,
    direction_opt: bool,
    result: &mut Vec2,
) -> usize {
    if direction_opt {
        *result = pf_vec2_scale(opt_vel, radius);
    } else if pf_vec2_len_sq(opt_vel) > radius * radius {
        *result = pf_vec2_scale(pf_vec2_normalize_or_zero(opt_vel), radius);
    } else {
        *result = opt_vel;
    }
    for (idx, line) in lines.iter().enumerate() {
        if pf_vec2_det(line.get_direction(), pf_vec2_sub(line.get_point(), *result)) > 0.0 {
            let previous: Vec2 = *result;
            if !orca_linear_program1(lines, idx, radius, opt_vel, direction_opt, result) {
                *result = previous;
                return idx;
            }
        }
    }
    lines.len()
}

/// Minimizes maximum half-plane violation when the 2D ORCA linear program is infeasible.
///
/// # Arguments
///
/// - `&[OrcaLine2D]` - Full set of ORCA half-plane constraints.
/// - `usize` - Index of the first constraint where `orca_linear_program2` failed.
/// - `f32` - Maximum velocity disk radius.
/// - `&mut Vec<OrcaLine2D>` - Reusable projected-line scratch buffer.
/// - `&mut Vec2` - Velocity updated in place to the minimum-penetration fallback solution.
pub(crate) fn orca_linear_program3(
    lines: &[OrcaLine2D],
    begin_line: usize,
    radius: f32,
    proj_lines: &mut Vec<OrcaLine2D>,
    result: &mut Vec2,
) {
    let mut distance: f32 = 0.0;
    for i in begin_line..lines.len() {
        let line_i: OrcaLine2D = lines[i];
        if pf_vec2_det(
            line_i.get_direction(),
            pf_vec2_sub(line_i.get_point(), *result),
        ) > distance
        {
            proj_lines.clear();
            for line_j in &lines[..i] {
                let determinant: f32 = pf_vec2_det(line_i.get_direction(), line_j.get_direction());
                let point: Vec2 = if determinant.abs() <= ORCA_EPSILON {
                    if line_i.get_direction().dot(line_j.get_direction()) > 0.0 {
                        continue;
                    }
                    pf_vec2_scale(pf_vec2_add(line_i.get_point(), line_j.get_point()), 0.5)
                } else {
                    let numer: f32 = pf_vec2_det(
                        line_j.get_direction(),
                        pf_vec2_sub(line_i.get_point(), line_j.get_point()),
                    );
                    pf_vec2_add(
                        line_i.get_point(),
                        pf_vec2_scale(line_i.get_direction(), numer / determinant),
                    )
                };
                let diff_dir: Vec2 = pf_vec2_sub(line_j.get_direction(), line_i.get_direction());
                let direction: Vec2 = pf_vec2_normalize_or_zero(diff_dir);
                if pf_vec2_len_sq(direction) > 1e-12 {
                    proj_lines.push(OrcaLine2D::new(point, direction));
                }
            }
            let previous: Vec2 = *result;
            let opt_dir: Vec2 = Vec2::new(
                -line_i.get_direction().get_y(),
                line_i.get_direction().get_x(),
            );
            if orca_linear_program2(proj_lines, radius, opt_dir, true, result) < proj_lines.len() {
                *result = previous;
            }
            distance = pf_vec2_det(
                line_i.get_direction(),
                pf_vec2_sub(line_i.get_point(), *result),
            );
        }
    }
}