diff --git a/.rustfmt.toml b/.rustfmt.toml index f9cde57..448c59c 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -1,3 +1,4 @@ edition = "2021" fn_args_layout = "Compressed" -fn_single_line = true \ No newline at end of file +fn_single_line = true +hard_tabs = true \ No newline at end of file diff --git a/build.rs b/build.rs index fde04d0..ed26b31 100644 --- a/build.rs +++ b/build.rs @@ -1,9 +1,9 @@ fn main() { - let _ = command_run::Command::with_args("./pack_assets.sh", &[""]) - .enable_capture() - .run(); + let _ = command_run::Command::with_args("./pack_assets.sh", &[""]) + .enable_capture() + .run(); - println!("cargo:rerun-if-changed=.git/logs/HEAD"); // genius - println!("cargo:rerun-if-changed=assets"); // genius - shadow_rs::new().unwrap(); + println!("cargo:rerun-if-changed=.git/logs/HEAD"); // genius + println!("cargo:rerun-if-changed=assets"); // genius + shadow_rs::new().unwrap(); } diff --git a/src/egui_app.rs b/src/egui_app.rs index 181800c..bacd334 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -389,10 +389,8 @@ impl MathApp { .changed(); let configs_changed = max_x_changed - | min_x_changed - | integral_num_changed - | roots_toggled - | extrema_toggled; + | min_x_changed | integral_num_changed + | roots_toggled | extrema_toggled; let functions_len = self.functions.len(); let mut remove_i: Option = None; @@ -441,8 +439,7 @@ impl MathApp { let proc_func_str = process_func_str(self.func_strs[i].clone()); if configs_changed - | integral_toggle - | derivative_toggle + | integral_toggle | derivative_toggle | (proc_func_str != function.get_func_str()) | self.last_error.iter().any(|ele| ele.0 == i) { @@ -655,8 +652,7 @@ impl epi::App for MathApp { return f64::NAN; } - function.update_bounds(minx_bounds, maxx_bounds, available_width); - function.display(plot_ui) + function.display(plot_ui, minx_bounds, maxx_bounds, available_width) }) .collect(); self.last_info = (area_list, start.elapsed()); diff --git a/src/function.rs b/src/function.rs index a278d1e..95b2efc 100644 --- a/src/function.rs +++ b/src/function.rs @@ -100,61 +100,6 @@ impl FunctionEntry { } } - pub fn update_bounds(&mut self, min_x: f64, max_x: f64, pixel_width: usize) { - if pixel_width != self.pixel_width { - self.output.invalidate_back(); - self.output.invalidate_derivative(); - self.min_x = min_x; - self.max_x = max_x; - self.pixel_width = pixel_width; - } else if ((min_x != self.min_x) | (max_x != self.max_x)) && self.output.back.is_some() { - let resolution: f64 = self.pixel_width as f64 / (max_x.abs() + min_x.abs()); - let back_cache = self.output.back.as_ref().unwrap(); - - let x_data: SteppedVector = back_cache - .iter() - .map(|ele| ele.x) - .collect::>() - .into(); - - self.output.back = Some( - (0..self.pixel_width) - .map(|x| (x as f64 / resolution as f64) + min_x) - .map(|x| { - if let Some(i) = x_data.get_index(x) { - back_cache[i] - } else { - Value::new(x, self.function.get(x)) - } - }) - .collect(), - ); - // assert_eq!(self.output.back.as_ref().unwrap().len(), self.pixel_width); - - let derivative_cache = self.output.derivative.as_ref().unwrap(); - let new_data = (0..self.pixel_width) - .map(|x| (x as f64 / resolution as f64) + min_x) - .map(|x| { - if let Some(i) = x_data.get_index(x) { - derivative_cache[i] - } else { - Value::new(x, self.function.get_derivative_1(x)) - } - }) - .collect(); - - self.output.derivative = Some(new_data); - } else { - self.output.invalidate_back(); - self.output.invalidate_derivative(); - self.pixel_width = pixel_width; - } - - self.min_x = min_x; - self.max_x = max_x; - self.output.invalidate_points(); - } - pub fn run_back(&mut self) -> (Vec, Option<(Vec, f64)>, Option>) { let resolution: f64 = (self.pixel_width as f64 / (self.max_x - self.min_x).abs()) as f64; let back_values: Vec = { @@ -394,7 +339,77 @@ impl FunctionEntry { self.output.extrema = Some(extrama_list); } - pub fn display(&mut self, plot_ui: &mut PlotUi) -> f64 { + pub fn display( + &mut self, plot_ui: &mut PlotUi, min_x: f64, max_x: f64, pixel_width: usize, + ) -> f64 { + if pixel_width != self.pixel_width { + self.output.invalidate_back(); + self.output.invalidate_derivative(); + self.min_x = min_x; + self.max_x = max_x; + self.pixel_width = pixel_width; + } else if ((min_x != self.min_x) | (max_x != self.max_x)) && self.output.back.is_some() { + let resolution: f64 = self.pixel_width as f64 / (max_x.abs() + min_x.abs()); + let back_cache = self.output.back.as_ref().unwrap(); + + let x_data: SteppedVector = back_cache + .iter() + .map(|ele| ele.x) + .collect::>() + .into(); + + self.output.back = Some( + (0..self.pixel_width) + .map(|x| (x as f64 / resolution as f64) + min_x) + .map(|x| { + if let Some(i) = x_data.get_index(x) { + back_cache[i] + } else { + Value::new(x, self.function.get(x)) + } + }) + .collect(), + ); + // assert_eq!(self.output.back.as_ref().unwrap().len(), self.pixel_width); + + let derivative_cache = self.output.derivative.as_ref().unwrap(); + let new_data = (0..self.pixel_width) + .map(|x| (x as f64 / resolution as f64) + min_x) + .map(|x| { + if let Some(i) = x_data.get_index(x) { + derivative_cache[i] + } else { + Value::new(x, self.function.get_derivative_1(x)) + } + }) + .collect(); + + self.output.derivative = Some(new_data); + } else { + self.output.invalidate_back(); + self.output.invalidate_derivative(); + self.pixel_width = pixel_width; + } + + if self.extrema { + if (min_x != self.min_x) | (max_x != self.max_x) { + self.extrema(); + } + } else { + self.output.extrema = None; + } + + if self.roots { + if (min_x != self.min_x) | (max_x != self.max_x) { + self.roots(); + } + } else { + self.output.roots = None; + } + + self.min_x = min_x; + self.max_x = max_x; + let (back_values, integral, derivative) = self.run_back(); self.output.back = Some(back_values); self.output.integral = integral;