Partially completed input conversion to matrix

This commit is contained in:
2024-02-18 01:44:14 -06:00
parent 6825f69118
commit b9e8521b19
4 changed files with 34 additions and 40 deletions

16
Cargo.lock generated
View File

@@ -541,14 +541,6 @@ version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd5256b483761cd23699d0da46cc6fd2ee3be420bbe6d020ae4a091e70b7e9fd" checksum = "bd5256b483761cd23699d0da46cc6fd2ee3be420bbe6d020ae4a091e70b7e9fd"
[[package]]
name = "hwocr"
version = "0.1.0"
dependencies = [
"relm4",
"rten-tensor",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.2.3" version = "2.2.3"
@@ -1107,6 +1099,14 @@ version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838"
[[package]]
name = "waywrite"
version = "0.1.0"
dependencies = [
"relm4",
"rten-tensor",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View File

@@ -1,5 +1,5 @@
[package] [package]
name = "hwocr" name = "waywrite"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View File

@@ -3,8 +3,8 @@ use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt, WidgetExt, Ge
use gtk::cairo::{Context, Operator}; use gtk::cairo::{Context, Operator};
use relm4::drawing::DrawHandler; use relm4::drawing::DrawHandler;
use hwocr::process_point::to_matrix; use waywrite::process_point::to_matrix;
use hwocr::Point; use waywrite::Point;
#[derive(Debug)] #[derive(Debug)]
enum AppInput { enum AppInput {
@@ -33,9 +33,9 @@ impl SimpleComponent for AppModel {
view! { view! {
gtk::Window { gtk::Window {
set_title: Some("Simple"), set_title: Some("Waywrite"),
set_default_width: 400, set_default_width: 600,
set_default_height: 100, set_default_height: 200,
gtk::Box{ gtk::Box{
set_orientation: gtk::Orientation::Horizontal, set_orientation: gtk::Orientation::Horizontal,
@@ -49,10 +49,6 @@ impl SimpleComponent for AppModel {
set_margin_all: 5, set_margin_all: 5,
set_vexpand: true, set_vexpand: true,
gtk::Label {
set_label: "Draw something",
},
#[local_ref] #[local_ref]
area -> gtk::DrawingArea { area -> gtk::DrawingArea {
set_vexpand: true, set_vexpand: true,
@@ -77,24 +73,18 @@ impl SimpleComponent for AppModel {
set_halign: gtk::Align::End, set_halign: gtk::Align::End,
gtk::Button { gtk::Button {
set_label: "Increment", set_label: "Enter",
connect_clicked[sender] => move |_| { connect_clicked[sender] => move |_| {
sender.input(AppInput::Input); sender.input(AppInput::Input);
} }
}, },
gtk::Button { gtk::Button {
set_label: "Decrement", set_label: "Erase",
connect_clicked[sender] => move |_| { connect_clicked[sender] => move |_| {
sender.input(AppInput::Reset); sender.input(AppInput::Reset);
} }
}, },
gtk::Label {
#[watch]
set_label: &format!("Counter: {}", model.counter),
set_margin_all: 5,
},
} }
} }
} }
@@ -123,6 +113,7 @@ impl SimpleComponent for AppModel {
match message { match message {
AppInput::Input => { AppInput::Input => {
println!("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
let mat = to_matrix(&self.points); let mat = to_matrix(&self.points);
for line in mat.iter() { for line in mat.iter() {
line.iter().for_each(|x| print!("{}", x)); line.iter().for_each(|x| print!("{}", x));
@@ -163,6 +154,6 @@ fn draw(cx: &Context, points: &[Point]) {
} }
fn main() { fn main() {
let app = RelmApp::new("relm4.test.simple"); let app = RelmApp::new("simmer505.waywrite");
app.run::<AppModel>(0); app.run::<AppModel>(0);
} }

View File

@@ -8,7 +8,9 @@ fn process(points: Vec<Point>) -> NdTensor<f32, 3> {
NdTensor::zeros([1, 1, 1]) NdTensor::zeros([1, 1, 1])
} }
pub fn to_matrix(points: &Vec<Point>) -> Vec<Box<[f64; 1000]>>{ pub fn to_matrix(points: &Vec<Point>) -> Vec<Box<[f64; 200]>>{
const MATRIX_SIZE: f64 = 200.0;
let min_x = points.iter().min_by_key(|p| p.x as i32).unwrap().x; 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 min_y = points.iter().min_by_key(|p| p.y as i32).unwrap().y;
@@ -18,27 +20,28 @@ pub fn to_matrix(points: &Vec<Point>) -> Vec<Box<[f64; 1000]>>{
let x_len = max_x - min_x; let x_len = max_x - min_x;
let y_len = max_y - min_y; let y_len = max_y - min_y;
let x_scale = 800.0 / x_len; let y_ratio = y_len / x_len;
let y_scale = (y_len / x_len) * x_scale; let x_scale = (0.8 * MATRIX_SIZE) / x_len;
let y_scale = ((0.8 * MATRIX_SIZE) * y_ratio) / y_len;
let scaled_points = points.iter().map(|point| { let scaled_points = points.iter().map(|point| {
let x_scaled = ((point.x - min_x) * x_scale) + 100.0; let x_scaled = ((point.x - min_x) * x_scale) + (0.1 * MATRIX_SIZE);
let y_scaled = ((point.y - min_y) * x_scale) + (100.0 * y_scale); let y_scaled = ((point.y - min_y) * y_scale) + ((0.1 * MATRIX_SIZE) * y_ratio);
((x_scaled, y_scaled), point.new_line) ((x_scaled, y_scaled), point.new_line)
}); });
let mut matrix: Vec<Box<[f64; 1000]>> = vec![Box::new([-0.0; 1000]); (y_len * y_scale) as u32 as usize]; let mut matrix: Vec<Box<[f64; MATRIX_SIZE as usize]>> = vec![Box::new([0.0; MATRIX_SIZE as u32 as usize]); (MATRIX_SIZE * y_ratio) as u64 as usize];
for ((x, y), newline) in scaled_points { for ((x, y), newline) in scaled_points {
let start_x = x - 1.5; let start_x = x - (MATRIX_SIZE / 100.0);
let end_x = x + 1.5; let end_x = x + (MATRIX_SIZE / 100.0);
let start_y = y - 1.5; let start_y = y - ((MATRIX_SIZE / 100.0) * y_ratio);
let end_y = y + 1.5; let end_y = y + ((MATRIX_SIZE / 100.0) * y_ratio);
matrix.iter_mut().enumerate().for_each(|(y, line)| line.iter_mut().for_each(|x| { matrix.iter_mut().enumerate().for_each(|(mat_y, line)| line.iter_mut().enumerate().for_each(|(mat_x, val)| {
if (start_x < *x && *x < end_x) && (start_y < (y as f64) && (y as f64) < end_y) { if (start_x < (mat_x as f64) && (mat_x as f64) < end_x) && (start_y < (mat_y as f64) && (mat_y as f64) < end_y) {
*x = 1.0; *val = 1.0;
} }
})); }));
} }