Skip to content

Commit

Permalink
Guard lookup curve from panics
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuuuube committed Jul 25, 2024
1 parent 2e02357 commit 1880d52
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
28 changes: 15 additions & 13 deletions src/accel_curves/lookup.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::{
types::{AccelArgs, Vec2},
utility::{maxsd, minsd, LUT_POINTS_CAPACITY},
types::{AccelArgs, Vec2}, unwrap_option_or_return_none, utility::{maxsd, minsd, LUT_POINTS_CAPACITY}
};

pub fn lookup(x: f64, args: &AccelArgs) -> f64 {
pub fn lookup(x: f64, args: &AccelArgs) -> Option<f64> {
if args.lookup_data.len() < 2 {
return 0.0;
return None;
}
let capacity = LUT_POINTS_CAPACITY;

Expand All @@ -19,13 +18,13 @@ pub fn lookup(x: f64, args: &AccelArgs) -> f64 {
let mut hi: i32 = size - 2;

if x <= 0.0 {
return 0.0;
return Some(0.0);
}

if (hi as i64) < ((capacity - 1) as i64) {
while lo <= hi {
let mid: i32 = (lo + hi) / 2;
let p: Vec2 = points[mid as usize];
let p: Vec2 = unwrap_option_or_return_none!(points.get(mid as usize)).to_owned();

if x < p.x {
hi = mid - 1;
Expand All @@ -36,27 +35,30 @@ pub fn lookup(x: f64, args: &AccelArgs) -> f64 {
if velocity {
y /= x;
}
return y;
return Some(y);
}
}

if lo > 0 {
let a: Vec2 = points[(lo - 1) as usize];
let b: Vec2 = points[lo as usize];
let a: Vec2 = unwrap_option_or_return_none!(points.get((lo - 1) as usize)).to_owned();
let b: Vec2 = unwrap_option_or_return_none!(points.get(lo as usize)).to_owned();
let t: f64 = (x - a.x) / (b.x - a.x);
let mut y: f64 = lerp(a.y, b.y, t);
if velocity {
y /= x;
}
return y;
return Some(y);
}
}

let mut y: f64 = points[0].y;
if points.len() < 1 {
return None;
}
let mut y: f64 = unwrap_option_or_return_none!(points.get(0)).y;
if velocity {
y /= points[0].x;
y /= unwrap_option_or_return_none!(points.get(0)).x;
}
return y;
return Some(y);
}

fn lerp(a: f64, b: f64, t: f64) -> f64 {
Expand Down
11 changes: 10 additions & 1 deletion src/generate_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn generate_curve(args: &AccelArgs) -> CurvegenResult {
_ => step_maker(args.point_count * 100, 0.0, (args.dpi / 20) as f64),
};
let mut curve_outputs: Vec<Point> = vec![];
let mut previous_point_y: f64 = Default::default();
for curve_step in curve_steps.x_values {
let output_sens = match args.mode {
crate::types::AccelMode::Classic | crate::types::AccelMode::Linear => {
Expand All @@ -22,7 +23,15 @@ pub fn generate_curve(args: &AccelArgs) -> CurvegenResult {
crate::types::AccelMode::Synchronous => crate::synchronous(curve_step, args),
crate::types::AccelMode::Power => crate::power(curve_step, args),
crate::types::AccelMode::Motivity => crate::motivity(curve_step, args),
crate::types::AccelMode::Lookup => crate::lookup(curve_step, args),
crate::types::AccelMode::Lookup => {
match crate::lookup(curve_step, args) {
Some(some) => {
previous_point_y = some;
some
},
None => previous_point_y,
}
},
crate::types::AccelMode::Noaccel => crate::noaccel(curve_step, args),
};
curve_outputs.push(Point {
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl FpRepRange {
}
}

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Default)]
pub struct Point {
pub x: f64,
pub y: f64,
Expand Down

0 comments on commit 1880d52

Please sign in to comment.