Skip to content

Commit 2425852

Browse files
committed
137 bad next, 48115 bad distances
1 parent 0086bef commit 2425852

File tree

3 files changed

+195
-23
lines changed

3 files changed

+195
-23
lines changed

navpointmatrixcli/src/main.rs

+50-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use std::fs::{self};
22

3+
use ordered_f32::OrderedF32;
34
use p3_api::data::{navigation_matrix::NavigationMatrix, navigation_vector::NavigationVector, navpoint_matrix::NavpointMatrix};
45
use pathfinding::prelude::{build_path, dijkstra_all};
56

7+
pub mod ordered_f32;
8+
69
pub struct DirectlyConnectedNodes {
710
pub connected_nodes: Vec<(u16, u16)>,
811
}
@@ -38,35 +41,38 @@ fn main() {
3841
for target_index in 0..navigation_vector.points.len() as u16 {
3942
if source_index != target_index {
4043
let path = build_path(&(target_index), &parents);
41-
let distance = 0;
42-
//println!("{source_index} -> {target_index} {path:?}");
44+
let distance = navigation_vector.get_path_length(&path);
4345
new_navpoint_matrix.set_next(source_index, target_index, path[1], distance, navigation_vector.length)
4446
} else {
4547
new_navpoint_matrix.set_next(source_index, source_index, source_index, 0, navigation_vector.length)
4648
}
4749
}
4850
}
4951

50-
{
51-
println!(
52-
"{} {} {}",
53-
navigation_vector.get_distance(0, 14),
54-
navigation_vector.get_distance(0, 9),
55-
navigation_vector.get_distance(9, 14)
56-
);
57-
}
58-
59-
//assert_eq!(original_navpoint_matrix, new_navpoint_matrix)
6052
assert_eq!(original_navpoint_matrix.matrix.len(), new_navpoint_matrix.matrix.len());
53+
6154
println!("Asserting {} cells", original_navpoint_matrix.matrix.len());
62-
//println!("{:?}", &original_navpoint_matrix.matrix[0..10]);
63-
//println!("{:?}", &new_navpoint_matrix.matrix[0..10]);
55+
let mut bad_next_cells = 0;
6456
for i in 0..original_navpoint_matrix.matrix.len() {
6557
let orig_next = original_navpoint_matrix.matrix[i].next;
6658
let calculated_next = new_navpoint_matrix.matrix[i].next;
67-
println!("cell {i}: {orig_next} == {calculated_next}");
68-
assert_eq!(orig_next, calculated_next);
59+
if orig_next != calculated_next {
60+
println!("cell {i}: next {orig_next} != {calculated_next}");
61+
bad_next_cells += 1;
62+
}
6963
}
64+
println!("{bad_next_cells} bad next entries");
65+
66+
let mut bad_distance_cells = 0;
67+
for i in 0..original_navpoint_matrix.matrix.len() {
68+
let orig_distance = original_navpoint_matrix.matrix[i].distance;
69+
let calculated_distance = new_navpoint_matrix.matrix[i].distance;
70+
if orig_distance != calculated_distance {
71+
println!("cell {i}: distance {orig_distance} != {calculated_distance}");
72+
bad_distance_cells += 1;
73+
}
74+
}
75+
println!("{bad_distance_cells} bad distances");
7076
}
7177

7278
fn is_connected(p0: (i16, i16), p1: (i16, i16), navigation_matrix: &NavigationMatrix) -> bool {
@@ -116,11 +122,11 @@ impl DirectlyConnectedNodes {
116122
buf
117123
}
118124

119-
pub fn get_neighbours(&self, node_index: u16, nav_vec: &NavigationVector) -> Vec<(u16, i128)> {
125+
pub fn get_neighbours(&self, node_index: u16, nav_vec: &NavigationVector) -> Vec<(u16, OrderedF32)> {
120126
let mut neighbours = vec![];
121127
for n in &self.connected_nodes {
122128
if n.0 == node_index {
123-
neighbours.push((n.1, nav_vec.get_distance(n.0 as _, n.1 as _)));
129+
neighbours.push((n.1, nav_vec.get_distance(n.0 as _, n.1 as _).into()));
124130
}
125131
}
126132

@@ -167,3 +173,29 @@ impl DirectlyConnectedNodes {
167173
DirectlyConnectedNodes { connected_nodes: nodes }
168174
}
169175
}
176+
177+
#[test]
178+
fn test1() {
179+
let _navigation_matrix = NavigationMatrix::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\nav_matrix.dat").unwrap());
180+
let navigation_vector = NavigationVector::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\nav_vec.dat").unwrap());
181+
let original_navpoint_matrix = NavpointMatrix::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\matrix_int.dat").unwrap());
182+
let cell4 = &original_navpoint_matrix.matrix[6];
183+
let cell4_distance = cell4.distance;
184+
185+
let (x1, y1) = navigation_vector.points[0];
186+
let (x2, y2) = navigation_vector.points[6];
187+
let dx = (x2 - x1) as f32;
188+
let dy = (y2 - y1) as f32;
189+
190+
let calculated_distance = ((dx * dx + dy * dy).sqrt() * 65536.0) as i32;
191+
192+
println!("{cell4_distance} {calculated_distance}");
193+
assert_eq!(cell4.distance, calculated_distance)
194+
}
195+
196+
#[test]
197+
fn test2() {
198+
let navigation_vector = NavigationVector::deserialize(&fs::read(r"C:\Users\Benni\Patrician 3_workbench\navdata\nav_vec.dat").unwrap());
199+
let path_0_2 = [0, 9, 20, 34, 33, 25, 24, 29, 31, 59, 65, 64, 44, 2];
200+
assert_eq!(19936074, navigation_vector.get_path_length(&path_0_2))
201+
}

navpointmatrixcli/src/ordered_f32.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+
Some(self.cmp(other))
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

+89-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,94 @@ 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()
27+
}
28+
29+
pub fn get_path_length2(&self, path: &[u16]) -> i32 {
30+
let mut distance = 0;
31+
let mut i = 0;
32+
while i < path.len() - 1 {
33+
let x1 = self.points[path[i] as usize].0 as i64;
34+
let y1 = self.points[path[i] as usize].1 as i64;
35+
let x2 = self.points[path[i + 1] as usize].0 as i64;
36+
let y2 = self.points[path[i + 1] as usize].1 as i64;
37+
let i_square = ((x2 << 16) - (x1 << 16)).pow(2) + ((y2 << 16) - (y1 << 16)).pow(2);
38+
distance += i_square.isqrt() as i32;
39+
i += 1;
40+
}
41+
42+
distance
43+
}
44+
45+
pub fn get_path_length3(&self, path: &[u16]) -> i32 {
46+
let mut distance = 0;
47+
let mut i = 0;
48+
while i < path.len() - 1 {
49+
let x1 = self.points[path[i] as usize].0 as i32;
50+
let y1 = self.points[path[i] as usize].1 as i32;
51+
let x2 = self.points[path[i + 1] as usize].0 as i32;
52+
let y2 = self.points[path[i + 1] as usize].1 as i32;
53+
let i_square = (x2 - x1).pow(2) + (y2 - y1).pow(2);
54+
distance += i_square.isqrt() << 16;
55+
i += 1;
56+
}
57+
58+
distance
59+
}
60+
61+
/// F64, multiplication and rounding at the end
62+
pub fn get_path_length_48k(&self, path: &[u16]) -> i32 {
63+
let mut distance = 0.0;
64+
let mut i = 0;
65+
while i < path.len() - 1 {
66+
let x1 = self.points[path[i] as usize].0 as i32;
67+
let y1 = self.points[path[i] as usize].1 as i32;
68+
let x2 = self.points[path[i + 1] as usize].0 as i32;
69+
let y2 = self.points[path[i + 1] as usize].1 as i32;
70+
let dx = (x2 - x1) as f64;
71+
let dy = (y2 - y1) as f64;
72+
distance += (dx * dx + dy * dy).sqrt();
73+
i += 1;
74+
}
75+
76+
(distance * 65536.0).round() as i32
77+
}
78+
79+
pub fn get_path_length_66k(&self, path: &[u16]) -> i32 {
80+
let mut distance = 0.0;
81+
let mut i = 0;
82+
while i < path.len() - 1 {
83+
let x1 = self.points[path[i] as usize].0 as i32;
84+
let y1 = self.points[path[i] as usize].1 as i32;
85+
let x2 = self.points[path[i + 1] as usize].0 as i32;
86+
let y2 = self.points[path[i + 1] as usize].1 as i32;
87+
let dx = (x2 - x1) as f32;
88+
let dy = (y2 - y1) as f32;
89+
distance += (dx * dx + dy * dy).sqrt();
90+
i += 1;
91+
}
92+
93+
(distance * 65536.0).round() as i32
94+
}
95+
96+
pub fn get_path_length(&self, path: &[u16]) -> i32 {
97+
let mut distance = 0.0;
98+
for i in 0..path.len() - 1 {
99+
let x1 = self.points[path[i] as usize].0 as i32;
100+
let y1 = self.points[path[i] as usize].1 as i32;
101+
let x2 = self.points[path[i + 1] as usize].0 as i32;
102+
let y2 = self.points[path[i + 1] as usize].1 as i32;
103+
let dx = (x2 - x1) as f64;
104+
let dy = (y2 - y1) as f64;
105+
distance += (dx * dx + dy * dy).sqrt();
106+
}
107+
108+
(distance * 65536.0).round() as i32
25109
}
26110
}

0 commit comments

Comments
 (0)