-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
133 additions
and
48 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use std::collections::HashSet; | ||
|
||
fn dfs(node: usize, visited: &mut HashSet<usize>, subgraph: &Vec<Vec<u8>>) { | ||
visited.insert(node); | ||
for (neighbor, &is_connected) in subgraph[node].iter().enumerate() { | ||
if is_connected == 1 && !visited.contains(&neighbor) { | ||
dfs(neighbor, visited, subgraph); | ||
} | ||
} | ||
} | ||
|
||
pub fn are_strokes_linked(strokes: &Vec<usize>, intersection_matrix: &Vec<Vec<u8>>) -> bool { | ||
if strokes.is_empty() { | ||
return false; | ||
} | ||
|
||
// Create a subgraph for only the strokes of interest | ||
let mut subgraph: Vec<Vec<u8>> = vec![vec![0; strokes.len()]; strokes.len()]; | ||
for (i, &stroke1) in strokes.iter().enumerate() { | ||
for (j, &stroke2) in strokes.iter().enumerate() { | ||
subgraph[i][j] = intersection_matrix[stroke1][stroke2]; | ||
} | ||
} | ||
|
||
let mut visited: HashSet<usize> = HashSet::new(); | ||
dfs(0, &mut visited, &subgraph); | ||
|
||
visited.len() == strokes.len() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_are_strokes_linked_single() { | ||
assert!(are_strokes_linked(&vec![0], &vec![vec![1]])); | ||
} | ||
|
||
#[test] | ||
fn test_are_strokes_linked_pair_connected() { | ||
assert!(are_strokes_linked( | ||
&vec![0, 1], | ||
&vec![vec![1, 1], vec![1, 1]] | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_are_strokes_linked_pair_disconnected() { | ||
assert!(!are_strokes_linked( | ||
&vec![0, 1], | ||
&vec![vec![1, 0], vec![0, 1]] | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_are_strokes_linked_group_disconnected() { | ||
assert!(!are_strokes_linked( | ||
&vec![0, 1, 2, 3], | ||
&vec![ | ||
vec![1, 1, 0, 0], | ||
vec![1, 1, 0, 0], | ||
vec![0, 0, 1, 1], | ||
vec![0, 0, 1, 1], | ||
] | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_are_strokes_linked_specific_case() { | ||
assert!(!are_strokes_linked( | ||
&vec![0, 3], | ||
&vec![ | ||
vec![1, 1, 1, 0], | ||
vec![1, 1, 0, 1], | ||
vec![1, 0, 1, 1], | ||
vec![0, 1, 1, 1], | ||
] | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod generate; | ||
mod glyph; | ||
mod intersect; | ||
mod parameters; | ||
mod stroke; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use glyphs_generator::{compute, initialize}; | ||
use std::fs; | ||
|
||
use relative_path::RelativePath; | ||
|
||
fn setup() { | ||
let tests_dir = RelativePath::new("tests"); | ||
let parameters_path = tests_dir.join("parameters_9ap.json").to_string(); | ||
let parameters_json = fs::read_to_string(parameters_path) | ||
.expect("Failed to read parameters file") | ||
.to_string(); | ||
initialize(parameters_json); | ||
} | ||
|
||
#[test] | ||
fn test_computable_example() { | ||
setup(); | ||
let tests_dir = RelativePath::new("tests"); | ||
let computable_path = tests_dir.join("computable.json").to_string(); | ||
let computable_json = fs::read_to_string(computable_path) | ||
.expect("Failed to read computable file") | ||
.to_string(); | ||
let result = compute(computable_json); | ||
assert!(result.is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"parent_strokes":[{"x0":-1.0,"y0":-1.0,"x1":-1.0,"y1":1.0},{"x0":-1.0,"y0":-1.0,"x1":1.0,"y1":-1.0},{"x0":-1.0,"y0":-1.0,"x1":1.0,"y1":1.0},{"x0":-1.0,"y0":1.0,"x1":1.0,"y1":-1.0},{"x0":-1.0,"y0":1.0,"x1":1.0,"y1":1.0},{"x0":1.0,"y0":-1.0,"x1":1.0,"y1":1.0}],"intersection_matrix":[[1,1,1,1,1,0],[1,1,1,1,0,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,0,1,1,1,1],[0,1,1,1,1,1]],"transformation_matrix":[[0,5,4,1,4,5,1],[4,1,0,0,5,4,5],[3,3,3,2,2,2,3],[2,2,2,3,3,3,2],[1,4,5,5,0,1,0],[5,0,1,4,1,0,4]]} | ||
{"parent_strokes":[{"x0":-1.0,"y0":-1.0,"x1":-1.0,"y1":1.0},{"x0":-1.0,"y0":-1.0,"x1":1.0,"y1":-1.0},{"x0":-1.0,"y0":-1.0,"x1":1.0,"y1":1.0},{"x0":-1.0,"y0":1.0,"x1":1.0,"y1":-1.0},{"x0":-1.0,"y0":1.0,"x1":1.0,"y1":1.0},{"x0":1.0,"y0":-1.0,"x1":1.0,"y1":1.0}],"intersection_matrix":[[0,1,1,1,1,0],[1,0,1,1,0,1],[1,1,0,1,1,1],[1,1,1,0,1,1],[1,0,1,1,0,1],[0,1,1,1,1,0]],"transformation_matrix":[[0,5,4,1,4,5,1],[4,1,0,0,5,4,5],[3,3,3,2,2,2,3],[2,2,2,3,3,3,2],[1,4,5,5,0,1,0],[5,0,1,4,1,0,4]]} |
Oops, something went wrong.