First attempt at converting drawing into matrix
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -546,6 +546,7 @@ name = "hwocr"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"relm4",
|
"relm4",
|
||||||
|
"rten-tensor",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -791,6 +792,15 @@ dependencies = [
|
|||||||
"syn 2.0.48",
|
"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]]
|
[[package]]
|
||||||
name = "rustc-demangle"
|
name = "rustc-demangle"
|
||||||
version = "0.1.23"
|
version = "0.1.23"
|
||||||
|
|||||||
@@ -7,3 +7,4 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
relm4="0.6.*"
|
relm4="0.6.*"
|
||||||
|
rten-tensor = "0.4.*"
|
||||||
|
|||||||
0
process_point.rs
Normal file
0
process_point.rs
Normal file
8
src/lib.rs
Normal file
8
src/lib.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
pub mod process_point;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Point {
|
||||||
|
pub x: f64,
|
||||||
|
pub y: f64,
|
||||||
|
pub new_line: bool,
|
||||||
|
}
|
||||||
20
src/main.rs
20
src/main.rs
@@ -3,21 +3,17 @@ 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 hwocr::Point;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum AppInput {
|
enum AppInput {
|
||||||
Increment,
|
Input,
|
||||||
NewLine((f64, f64)),
|
NewLine((f64, f64)),
|
||||||
AddPoint((f64, f64)),
|
AddPoint((f64, f64)),
|
||||||
Reset,
|
Reset,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Point {
|
|
||||||
x: f64,
|
|
||||||
y: f64,
|
|
||||||
new_line: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct AppModel {
|
struct AppModel {
|
||||||
@@ -83,7 +79,7 @@ impl SimpleComponent for AppModel {
|
|||||||
gtk::Button {
|
gtk::Button {
|
||||||
set_label: "Increment",
|
set_label: "Increment",
|
||||||
connect_clicked[sender] => move |_| {
|
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();
|
let cx = self.handler.get_context();
|
||||||
|
|
||||||
match message {
|
match message {
|
||||||
AppInput::Increment => {
|
AppInput::Input => {
|
||||||
self.counter = self.counter.wrapping_add(1);
|
let mat = to_matrix(&self.points);
|
||||||
|
for line in mat.iter() {
|
||||||
|
line.iter().for_each(|x| print!("{}", x));
|
||||||
|
println!();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
AppInput::AddPoint((x, y)) => {
|
AppInput::AddPoint((x, y)) => {
|
||||||
self.points.push(Point { x, y, new_line: false })
|
self.points.push(Point { x, y, new_line: false })
|
||||||
|
|||||||
50
src/process_point.rs
Normal file
50
src/process_point.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
use rten_tensor::NdTensor;
|
||||||
|
|
||||||
|
use crate::Point;
|
||||||
|
|
||||||
|
fn process(points: Vec<Point>) -> NdTensor<f32, 3> {
|
||||||
|
|
||||||
|
|
||||||
|
NdTensor::zeros([1, 1, 1])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_matrix(points: &Vec<Point>) -> Vec<Box<[f64; 1000]>>{
|
||||||
|
|
||||||
|
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<Box<[f64; 1000]>> = 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
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user