Skip to content

Commit

Permalink
cargo formatting - cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Aki Vänttinen committed Mar 22, 2020
1 parent a686871 commit db7a375
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 189 deletions.
5 changes: 5 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
max_width = 120
newline_style = "Unix"
fn_single_line = true
indent_style = "Visual"
use_small_heuristics = "Off"
73 changes: 33 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,103 +11,96 @@ fn sc2pathlib(_py: Python, m: &PyModule) -> PyResult<()> {
Ok(())
}
#[cfg(test)]
mod tests{
mod tests {
use super::*;
use test::bench::Bencher;
use std::fs::File;
use std::io::{BufRead, BufReader};
use test::bench::Bencher;

fn rotate90clockwise(vec: Vec<Vec<usize>>) -> Vec<Vec<usize>>{
fn rotate90clockwise(vec: Vec<Vec<usize>>) -> Vec<Vec<usize>> {
let N = vec[0].len();
let mut new_arr: Vec<Vec<usize>>= vec.clone();
let mut new_arr: Vec<Vec<usize>> = vec.clone();
// Traverse each cycle
for i in 0..(N / 2) {
for j in i..(N-i-1){
for j in i..(N - i - 1) {
let temp = vec[i][j];
new_arr[i][j] = vec[N-1-j][i];
new_arr[N-1-j][i] = vec[N-1-i][N-1-j];
new_arr[N-1-i][N-1-j]= vec[j][N-1-i];
new_arr[j][N-1-j] = temp;

new_arr[i][j] = vec[N - 1 - j][i];
new_arr[N - 1 - j][i] = vec[N - 1 - i][N - 1 - j];
new_arr[N - 1 - i][N - 1 - j] = vec[j][N - 1 - i];
new_arr[j][N - 1 - j] = temp;
}

}
new_arr
}

fn read_vec_from_file(file_path: &str)-> Vec<Vec<usize>>{

fn read_vec_from_file(file_path: &str) -> Vec<Vec<usize>> {
let f = BufReader::new(File::open(file_path).unwrap());
let mut arr = Vec::<Vec<usize>>::new();

for line in f.lines().map(|x| x.unwrap()){
for line in f.lines().map(|x| x.unwrap()) {
let mut maze_line = vec![];
for mini_line in line.chars().map(|n| n.to_digit(2).unwrap()){
for mini_line in line.chars().map(|n| n.to_digit(2).unwrap()) {
maze_line.push(mini_line as usize)
}

arr.push(maze_line);
}
rotate90clockwise(arr)

}

fn get_pathfind(file: &str)->path_find::PathFind{
fn get_pathfind(file: &str) -> path_find::PathFind {
let map = read_vec_from_file(file);
path_find::PathFind::bench_new(map)
}

#[test]
fn test_find_path_automaton_le(){
fn test_find_path_automaton_le() {
let path_find = get_pathfind("tests/AutomatonLE.txt");
let r = path_find.find_path((32, 51),(150, 118),Some(0));
let r = path_find.find_path((32, 51), (150, 118), Some(0));
let (_, distance) = r.unwrap();
assert!(distance == 147.1656);
}

#[test]
fn test_find_path_4x4(){
fn test_find_path_4x4() {
let path_find = get_pathfind("tests/maze4x4.txt");
let r = path_find.find_path((0, 0), (3, 3),Some(0));
let r = path_find.find_path((0, 0), (3, 3), Some(0));
let (_, distance) = r.unwrap();
assert!(distance == 6.0);
}

#[test]
fn test_find_path_10x10(){
fn test_find_path_10x10() {
let path_find = get_pathfind("tests/empty10x10.txt");
let r = path_find.find_path((0, 0),(8, 9),Some(0));
let r = path_find.find_path((0, 0), (8, 9), Some(0));
let (_, distance) = r.unwrap();
assert!(distance == 12.3136);
}

#[bench]
fn bench_find_path_automaton_le(b: &mut Bencher){
fn bench_find_path_automaton_le(b: &mut Bencher) {
let path_find = get_pathfind("tests/AutomatonLE.txt");
// Run bench
b.iter(|| {
path_find.find_path((32, 51),(150, 118),Some(0));
});

}
path_find.find_path((32, 51), (150, 118), Some(0));
});
}

#[bench]
fn bench_find_path_4x4(b: &mut Bencher){
fn bench_find_path_4x4(b: &mut Bencher) {
let path_find = get_pathfind("tests/maze4x4.txt");
// Run bench
b.iter(|| {
path_find.find_path((0,0), (0,2),Some(0));
});

}
b.iter(|| {
path_find.find_path((0, 0), (0, 2), Some(0));
});
}

#[bench]
fn bench_find_path_10x10(b: &mut Bencher){
fn bench_find_path_10x10(b: &mut Bencher) {
let path_find = get_pathfind("tests/empty10x10.txt");
// Run bench
b.iter(|| {
path_find.find_path((0, 0),(8, 9),Some(0));
});

}
b.iter(|| {
path_find.find_path((0, 0), (8, 9), Some(0));
});
}
}
32 changes: 13 additions & 19 deletions src/path_find/angles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,25 @@ pub fn angle_between(first: (usize, usize), other: (usize, usize)) -> f64 {

#[inline]
pub fn angle_between_f64(first: (f64, f64), other: (f64, f64)) -> f64 {
point_angle((other.0 - first.0 , other.1 - first.1))
point_angle((other.0 - first.0, other.1 - first.1))
}

// (x,y) = (1,0) => -pi /2
// (x,y) = (0,-1) => 0
// (x,y) = (0,1) => pi
// (x,y) = (-1,0) => pi / 2
#[inline]
pub fn point_angle(point: (f64, f64)) -> f64{
pub fn point_angle(point: (f64, f64)) -> f64 {
let mut angle: f64;
if point.1 == 0.0 {
if point.0 > 0.0 {
angle = std::f64::consts::FRAC_PI_2
}
else if point.1 < 0.0 {
} else if point.1 < 0.0 {
angle = -std::f64::consts::FRAC_PI_2
}
else {
} else {
angle = 0.0f64
}
}
else {
} else {
angle = (point.0 as f64 / point.1 as f64).atan();
if point.1 >= 0.0 {
angle += std::f64::consts::PI
Expand All @@ -38,31 +35,28 @@ pub fn point_angle(point: (f64, f64)) -> f64{
angle
}

pub fn wrap_angle(angle:f64) -> f64{
pub fn wrap_angle(angle: f64) -> f64 {
let mut angle = angle % PI2;

if angle < -std::f64::consts::PI{
if angle < -std::f64::consts::PI {
angle += PI2
}
else if angle > std::f64::consts::PI{
} else if angle > std::f64::consts::PI {
angle -= PI2
}
angle
}

pub fn angle_distance(angle1: f64, angle2:f64) -> f64 {
pub fn angle_distance(angle1: f64, angle2: f64) -> f64 {
let angle1 = wrap_angle(angle1);
let angle2 = wrap_angle(angle2);
let mut d = f64::abs(angle2 - angle1);

if d <= std::f64::consts::PI{
if d <= std::f64::consts::PI {
// Do nothing
}
else if angle1 < angle2{
} else if angle1 < angle2 {
d = f64::abs(angle2 - (angle1 + std::f64::consts::FRAC_PI_2))
}
else {
} else {
d = f64::abs(angle2 + std::f64::consts::FRAC_PI_2 - angle1)
}
d
}
}
Loading

0 comments on commit db7a375

Please sign in to comment.