diff --git a/Cargo.lock b/Cargo.lock index 8f5c511..ac595f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -546,6 +546,7 @@ name = "hwocr" version = "0.1.0" dependencies = [ "relm4", + "rten-tensor", ] [[package]] @@ -791,6 +792,15 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "rten-tensor" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b994b2c4597280249d6beb99fe01dd0adf06b0f002c2049a07708f09aed55c3a" +dependencies = [ + "smallvec", +] + [[package]] name = "rustc-demangle" version = "0.1.23" diff --git a/Cargo.toml b/Cargo.toml index 77550a9..69fae56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] relm4="0.6.*" +rten-tensor = "0.4.*" diff --git a/process_point.rs b/process_point.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7bd0e5d --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,8 @@ +pub mod process_point; + +#[derive(Debug)] +pub struct Point { + pub x: f64, + pub y: f64, + pub new_line: bool, +} diff --git a/src/main.rs b/src/main.rs index 73f742d..80e86e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,21 +3,17 @@ use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt, WidgetExt, Ge use gtk::cairo::{Context, Operator}; use relm4::drawing::DrawHandler; +use hwocr::process_point::to_matrix; +use hwocr::Point; #[derive(Debug)] enum AppInput { - Increment, + Input, NewLine((f64, f64)), AddPoint((f64, f64)), Reset, } -#[derive(Debug)] -struct Point { - x: f64, - y: f64, - new_line: bool, -} #[derive(Debug)] struct AppModel { @@ -83,7 +79,7 @@ impl SimpleComponent for AppModel { gtk::Button { set_label: "Increment", connect_clicked[sender] => move |_| { - sender.input(AppInput::Increment); + sender.input(AppInput::Input); } }, @@ -126,8 +122,12 @@ impl SimpleComponent for AppModel { let cx = self.handler.get_context(); match message { - AppInput::Increment => { - self.counter = self.counter.wrapping_add(1); + AppInput::Input => { + let mat = to_matrix(&self.points); + for line in mat.iter() { + line.iter().for_each(|x| print!("{}", x)); + println!(); + } } AppInput::AddPoint((x, y)) => { self.points.push(Point { x, y, new_line: false }) diff --git a/src/process_point.rs b/src/process_point.rs new file mode 100644 index 0000000..1b07ddc --- /dev/null +++ b/src/process_point.rs @@ -0,0 +1,50 @@ +use rten_tensor::NdTensor; + +use crate::Point; + +fn process(points: Vec) -> NdTensor { + + + NdTensor::zeros([1, 1, 1]) +} + +pub fn to_matrix(points: &Vec) -> Vec>{ + + let min_x = points.iter().min_by_key(|p| p.x as i32).unwrap().x; + let min_y = points.iter().min_by_key(|p| p.y as i32).unwrap().y; + let max_x = points.iter().max_by_key(|p| p.x as i32).unwrap().x; + let max_y = points.iter().max_by_key(|p| p.y as i32).unwrap().y; + + + let x_len = max_x - min_x; + let y_len = max_y - min_y; + let x_scale = 800.0 / x_len; + let y_scale = (y_len / x_len) * x_scale; + + let scaled_points = points.iter().map(|point| { + let x_scaled = ((point.x - min_x) * x_scale) + 100.0; + let y_scaled = ((point.y - min_y) * x_scale) + (100.0 * y_scale); + + ((x_scaled, y_scaled), point.new_line) + }); + + let mut matrix: Vec> = vec![Box::new([-0.0; 1000]); (y_len * y_scale) as u32 as usize]; + + for ((x, y), newline) in scaled_points { + let start_x = x - 1.5; + let end_x = x + 1.5; + let start_y = y - 1.5; + let end_y = y + 1.5; + + matrix.iter_mut().enumerate().for_each(|(y, line)| line.iter_mut().for_each(|x| { + if (start_x < *x && *x < end_x) && (start_y < (y as f64) && (y as f64) < end_y) { + *x = 1.0; + } + })); + } + + + + matrix + +}