Skip to content

Commit 114d9df

Browse files
committed
hm
1 parent 0086bef commit 114d9df

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

navpointmatrixcli/src/main.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use std::fs::{self};
2-
1+
use ordered_f64::OrderedF32;
32
use p3_api::data::{navigation_matrix::NavigationMatrix, navigation_vector::NavigationVector, navpoint_matrix::NavpointMatrix};
43
use pathfinding::prelude::{build_path, dijkstra_all};
4+
use std::fs::{self};
5+
6+
pub mod ordered_f64;
57

68
pub struct DirectlyConnectedNodes {
79
pub connected_nodes: Vec<(u16, u16)>,
@@ -116,11 +118,11 @@ impl DirectlyConnectedNodes {
116118
buf
117119
}
118120

119-
pub fn get_neighbours(&self, node_index: u16, nav_vec: &NavigationVector) -> Vec<(u16, i128)> {
121+
pub fn get_neighbours(&self, node_index: u16, nav_vec: &NavigationVector) -> Vec<(u16, OrderedF32)> {
120122
let mut neighbours = vec![];
121123
for n in &self.connected_nodes {
122124
if n.0 == node_index {
123-
neighbours.push((n.1, nav_vec.get_distance(n.0 as _, n.1 as _)));
125+
neighbours.push((n.1, nav_vec.get_distance(n.0 as _, n.1 as _).into()));
124126
}
125127
}
126128

navpointmatrixcli/src/ordered_f64.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::{cmp::Ordering, ops::Add};
2+
3+
#[derive(Copy, Clone)]
4+
pub struct OrderedF32 {
5+
value: f32,
6+
}
7+
8+
impl From<f32> for OrderedF32 {
9+
fn from(value: f32) -> Self {
10+
Self { value }
11+
}
12+
}
13+
14+
impl pathfinding::num_traits::Zero for OrderedF32 {
15+
fn zero() -> Self {
16+
Self { value: 0.0 }
17+
}
18+
19+
fn is_zero(&self) -> bool {
20+
self.value == 0.0
21+
}
22+
}
23+
24+
impl Add for OrderedF32 {
25+
type Output = OrderedF32;
26+
27+
fn add(self, rhs: Self) -> Self::Output {
28+
Self { value: self.value + rhs.value }
29+
}
30+
}
31+
32+
impl Ord for OrderedF32 {
33+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
34+
if self.value < other.value {
35+
Ordering::Less
36+
} else if self.value > other.value {
37+
Ordering::Greater
38+
} else {
39+
Ordering::Equal
40+
}
41+
}
42+
}
43+
44+
impl PartialOrd for OrderedF32 {
45+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
46+
self.value.partial_cmp(&other.value)
47+
}
48+
}
49+
50+
impl PartialEq for OrderedF32 {
51+
fn eq(&self, other: &Self) -> bool {
52+
self.value == other.value
53+
}
54+
}
55+
56+
impl Eq for OrderedF32 {}

p3-api/src/data/navigation_vector.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ impl NavigationVector {
1717
NavigationVector { length, points }
1818
}
1919

20-
pub fn get_distance(&self, source_index: usize, destination_index: usize) -> i128 {
21-
let s = self.points[source_index];
22-
let d = self.points[destination_index];
23-
let i_square = (s.0 as i64 - d.0 as i64).pow(2) + (s.1 as i64 - d.1 as i64).pow(2);
24-
((i_square as f64).sqrt()) as i128
20+
pub fn get_distance(&self, source_index: usize, destination_index: usize) -> f32 {
21+
let x1 = self.points[source_index].0 as f32;
22+
let y1 = self.points[source_index].1 as f32;
23+
let x2 = self.points[destination_index].0 as f32;
24+
let y2 = self.points[destination_index].1 as f32;
25+
let i_square = (x2 - x1).powf(2.0) + (y2 - y1).powf(2.0);
26+
i_square.sqrt()
2527
}
2628
}

0 commit comments

Comments
 (0)