forgot to enable something + better debug

This commit is contained in:
Simon Gardling 2022-02-28 11:05:11 -05:00
parent 8abeac37eb
commit 4e0c425fa0
3 changed files with 51 additions and 21 deletions

View File

@ -1,3 +1,4 @@
use crate::misc::debug_log;
use eframe::egui::{plot::Value, widgets::plot::Bar};
use meval::Expr;
@ -159,10 +160,8 @@ impl Function {
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.back_cache.is_some()
&& false
{
} else if ((min_x != self.min_x) | (max_x != self.max_x)) && self.back_cache.is_some() {
debug_log("back_cache: partial regen");
let range_new: f64 = max_x.abs() + min_x.abs();
let resolution: f64 = (self.pixel_width as f64 / range_new) as f64;
@ -207,8 +206,12 @@ impl Function {
pub fn run(&mut self) -> FunctionOutput {
let front_values: Vec<Value> = match self.back_cache.is_some() {
true => self.back_cache.as_ref().expect("").clone(),
true => {
debug_log("back_cache: using");
self.back_cache.as_ref().expect("").clone()
}
false => {
debug_log("back_cache: regen");
let absrange = (self.max_x - self.min_x).abs();
let resolution: f64 = (self.pixel_width as f64 / absrange) as f64;
let front_data: Vec<Value> = (1..=self.pixel_width)
@ -226,11 +229,13 @@ impl Function {
if self.integral {
let back_bars: (Vec<Bar>, f64) = match self.front_cache.is_some() {
true => {
debug_log("front_cache: using");
let cache = self.front_cache.as_ref().expect("");
let vec_bars: Vec<Bar> = cache.0.to_vec();
(vec_bars, cache.1)
}
false => {
debug_log("front_cache: regen");
let (data, area) = self.integral_rectangles();
let bars: Vec<Bar> = data.iter().map(|(x, y)| Bar::new(*x, *y)).collect();

View File

@ -5,6 +5,8 @@ mod egui_app;
mod function;
mod misc;
#[cfg(target_arch = "wasm32")]
use misc::log_helper;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
@ -12,21 +14,12 @@ use wasm_bindgen::prelude::*;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[cfg(target_arch = "wasm32")]
#[cfg(debug_assertions)]
fn init_tracing_wasm() {
log("Initializing tracing_wasm...");
log_helper("Initializing tracing_wasm...");
tracing_wasm::set_as_global_default();
log("Initialized tracing_wasm!");
log_helper("Initialized tracing_wasm!");
}
#[cfg(target_arch = "wasm32")]
@ -36,19 +29,19 @@ fn init_tracing_wasm() {}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn start(canvas_id: &str) -> Result<(), wasm_bindgen::JsValue> {
log("Initializing...");
log_helper("Initializing...");
// See performance in browser profiler!
init_tracing_wasm();
// Used in order to hook into `panic!()` to log in the browser's console
log("Initializing console_error_panic_hook...");
log_helper("Initializing console_error_panic_hook...");
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
log("Initialized console_error_panic_hook!");
log_helper("Initialized console_error_panic_hook!");
log("Finished initializing!");
log_helper("Finished initializing!");
log("Starting App...");
log_helper("Starting App...");
let app = egui_app::MathApp::default();
eframe::start_web(canvas_id, Box::new(app))
}

View File

@ -1,5 +1,37 @@
use meval::Expr;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[allow(dead_code)]
pub fn log_helper(s: &str) {
#[cfg(target_arch = "wasm32")]
log(s);
#[cfg(not(target_arch = "wasm32"))]
println!("{}", s);
}
#[cfg(debug_assertions)]
pub fn debug_log(s: &str) {
#[cfg(target_arch = "wasm32")]
log(s);
#[cfg(not(target_arch = "wasm32"))]
println!("{}", s);
}
#[cfg(not(debug_assertions))]
pub fn debug_log(_s: &str) {}
/*
EXTREMELY Janky function that tries to put asterisks in the proper places to be parsed. This is so cursed. But it works, and I hopefully won't ever have to touch it again.
One limitation though, variables with multiple characters like `pi` cannot be multiplied (like `pipipipi` won't result in `pi*pi*pi*pi`). But that's such a niche use case (and that same thing could be done by using exponents) that it doesn't really matter.