feat: add some point to respect angle position

This commit is contained in:
Lapin Raving 2023-08-23 23:46:16 +02:00
parent a006bd87a6
commit e409071cc1
3 changed files with 101 additions and 49 deletions

View File

@ -35,3 +35,7 @@ Until = 48
[[transformers]]
[transformers.intensity]
[[transformers]]
[transformers.angle_correction]
coef = 2000.0

View File

@ -3,13 +3,15 @@ use crate::transformer::Transformers;
use crate::worldstate::WorldState;
use serde::{Deserialize, Serialize};
use std::f32::consts::PI;
use std::f64::consts::PI;
//use std::cmp::min;
/// Angle Optimisation
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct AngleOptimisation {
coef: f32,
coef: f64,
//pps: u16,
}
fn color_not_zero(p1: &Point, p2: &Point, p3: &Point) -> bool {
@ -29,58 +31,93 @@ fn pos_different(p1: &Point, p2: &Point) -> bool {
}
}
/*
* Le temps a attendre a un angle est toujours le meme, peu importe les kpps.
* Donc le nombre de point a rajouter est proportionelle a la vitesse du laser.
*
* kpps /
* */
fn color_same(p1: &Point, p2: &Point) -> bool {
if p1.color.r == p2.color.r && p1.color.g == p2.color.g && p1.color.b == p2.color.b {
true
} else {
false
}
}
// quand on rajoute un point, on le rejoute ou et avec quelle couleur
// -> j'ai l'impression qu'on le rajoute surtout a l'arriver
fn get_prev(pl: &[Point], id: usize) -> (Option<&Point>, usize) {
for i in (0..id).rev() {
if pos_different(&pl[id], &pl[i]) {
return (Some(&pl[i]), id - i);
}
}
(None, id - 0)
}
fn get_next(pl: &[Point], id: usize) -> (Option<&Point>, usize) {
for i in id..(pl.len() - 1) {
if pos_different(&pl[id], &pl[i]) {
return (Some(&pl[i]), i - id);
}
}
(None, pl.len() - id - 1)
}
fn max(v1: f64, v2: f64) -> f64 {
if v1 > v2 {
v1
} else {
v2
}
}
impl Transformers for AngleOptimisation {
fn apply(&self, point_list: &[Point], _ws: &WorldState) -> Vec<Point> {
fn apply(&self, pl: &[Point], _ws: &WorldState) -> Vec<Point> {
let c0 = Color { r: 0, g: 0, b: 0 };
let mut v = vec![];
//println!();
//println!("====================");
//println!("= next Frame =");
//println!("====================");
v.push(point_list[0]);
for i in 1..(point_list.len() - 1) {
let prev = point_list[i - 1];
let node = point_list[i];
let next = point_list[i + 1];
let to_add_max = _ws.kpps as f64 / self.coef; // for 180 deg
let to_add_min = max(to_add_max / 1.5, 2.); // for 180 deg
if color_not_zero(&prev, &node, &next)
&& pos_different(&prev, &node)
&& pos_different(&node, &next)
v.push(pl[0]); // push first
for i in 1..(pl.len() - 1) {
let node = &pl[i];
if let ((Some(prev), dist_prev), (Some(next), dist_next)) =
(get_prev(pl, i), get_next(pl, i))
{
//println!("=========");
let mut d1 = Point::diff(&node, &prev);
let mut d2 = Point::diff(&next, &next);
//dbg!(d1);
//dbg!(d2);
d1.normalize();
d2.normalize();
let cross = d1.cross(&d2);
let angle = d1.cross(&d2).acos();
let deg = angle * 180. / PI;
//dbg!(prev);
//dbg!(node);
//dbg!(next);
//dbg!(d1);
//dbg!(d2);
//dbg!(cross);
//dbg!(angle);
//dbg!(deg);
//println!("deg: {deg}");
let nb_add = (self.coef * (1. - angle / PI)) as usize;
for _ in 0..nb_add {}
// on push plusisieur fois le point que l'on regarde
// - on calcul l'angle
// - on en deduis le ratio de nombre a appliquer
// - on regarde si il y a suffisement de point avant
// - on rajoute les point manquant
let d1 = Point::diff(node, prev).normalize();
let d2 = Point::diff(next, node).normalize();
let angle = (d1.cross(&d2) as f64).acos();
let to_add = ((to_add_max - to_add_min) * (angle / PI) + to_add_min) as usize;
println!("\nangle: {}", (angle / PI * 180.));
dbg!(to_add);
dbg!(to_add_max);
for _ in 0..to_add {
v.push(*node);
}
v.push(node); // push node
} else {
v.push(*node);
};
//v.push(*node); // push node
}
v.push(point_list[point_list.len() - 1]); // push last
v.push(pl[pl.len() - 1]); // push last
v
}
}
/*
* |\
* | \
* | \
* | |
* */

View File

@ -1,4 +1,4 @@
use crate::point::{Point, Color};
use crate::point::{Color, Point};
use crate::transformer::Transformers;
use crate::worldstate::WorldState;
@ -20,8 +20,19 @@ impl Transformers for Replicate {
match self {
Replicate::Until(n) => {
while point_list2.len() < *n {
if point_list.len() == 0 { // to prevent infinit loop in case of empty frame
point_list2.append(&mut vec![Point{x:0., y:0., color: Color{r:0, g:0, b:0}}; *n].to_vec());
if point_list.len() == 0 {
// to prevent infinit loop in case of empty frame
point_list2.append(
&mut vec![
Point {
x: 0.,
y: 0.,
color: Color { r: 0, g: 0, b: 0 }
};
*n
]
.to_vec(),
);
} else {
point_list2.append(&mut point_list.to_vec());
}