use super::*;
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())
}
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())
}
pub(crate) fn pf_vec2_scale(vec: Vec2, factor: f32) -> Vec2 {
Vec2::new(vec.get_x() * factor, vec.get_y() * factor)
}
pub(crate) fn pf_vec2_det(lhs: Vec2, rhs: Vec2) -> f32 {
lhs.get_x() * rhs.get_y() - lhs.get_y() * rhs.get_x()
}
pub(crate) fn pf_vec2_len_sq(vec: Vec2) -> f32 {
vec.dot(vec)
}
pub(crate) fn pf_vec2_len(vec: Vec2) -> f32 {
pf_vec2_len_sq(vec).sqrt()
}
pub(crate) fn pf_vec2_dist_sq(lhs: Vec2, rhs: Vec2) -> f32 {
pf_vec2_len_sq(pf_vec2_sub(lhs, rhs))
}
pub(crate) fn pf_vec2_dist(lhs: Vec2, rhs: Vec2) -> f32 {
pf_vec2_dist_sq(lhs, rhs).sqrt()
}
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)
}
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
}
}
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)
}
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;
}
}
}
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)
}
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;
}
}
}
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)
}
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
}
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);
}
}
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
}
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()
}
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),
);
}
}
}