use bladvak::{
ErrorManager,
eframe::egui::{self, Align2, Color32, FontId, Pos2, Rect, Stroke, Vec2},
};
use crate::utils::ui_settings;
#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct WaveApp {
reflection: f32,
length: f32,
incident: bool,
reflected: bool,
total: bool,
stationary: bool,
progressive: bool,
time: f32,
}
impl Default for WaveApp {
fn default() -> Self {
Self {
reflection: -0.60,
length: 3.0,
incident: true,
reflected: true,
total: true,
stationary: false,
progressive: true,
time: 0.0,
}
}
}
#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_lines)]
#[allow(clippy::fn_params_excessive_bools)]
fn draw_wave_canvas(
painter: &egui::Painter,
rect: Rect,
time: f32,
reflection: f32,
length: f32,
show_incident: bool,
show_reflected: bool,
show_total: bool,
show_stationary: bool,
show_progressive: bool,
) {
let plot = rect;
let axis_y = plot.center().y + 10.0;
painter.line_segment(
[
Pos2::new(plot.left(), axis_y),
Pos2::new(plot.right(), axis_y),
],
Stroke::new(1.0, Color32::LIGHT_GRAY),
);
painter.text(
Pos2::new(plot.left() + 6.0, plot.top() + 8.0),
Align2::LEFT_TOP,
"Emetteur",
FontId::proportional(16.0),
Color32::GRAY,
);
let fond_id = FontId::proportional(13.0);
painter.text(
plot.center_top(),
Align2::CENTER_TOP,
format!("Coefficient de Réflexion = {reflection:.2}"),
fond_id.clone(),
Color32::BLACK,
);
painter.text(
plot.center_top() + Vec2::new(0.0, 15.0),
Align2::CENTER_TOP,
format!("R / Z₁ = {:.4}", (1.0 + reflection) / (1.0 - reflection)),
fond_id,
Color32::BLACK,
);
painter.text(
Pos2::new(plot.right() - 80.0, plot.top() + 8.0),
Align2::LEFT_TOP,
"Récepteur",
FontId::proportional(16.0),
Color32::GRAY,
);
let wavelength = egui::lerp(80.0..=500.0, length / 3.0);
let amplitude = 50.0;
let omega = 2.0;
if show_incident {
draw_incident_wave(
painter,
plot,
axis_y,
wavelength,
amplitude,
omega,
time,
Color32::RED,
);
}
if show_reflected {
draw_reflected_wave(
painter,
plot,
axis_y,
wavelength,
amplitude,
reflection,
omega,
time,
Color32::GREEN,
);
}
if show_total {
draw_total_wave(
painter,
plot,
axis_y,
wavelength,
amplitude,
reflection,
omega,
time,
Color32::BLACK,
);
}
if show_stationary {
draw_stationary_wave(
painter,
plot,
axis_y,
wavelength,
amplitude,
reflection,
omega,
time,
Color32::BLUE,
);
}
if show_progressive {
draw_progressive_wave(
painter,
plot,
axis_y,
wavelength,
amplitude,
reflection,
omega,
time,
Color32::MAGENTA,
);
}
}
#[allow(clippy::too_many_arguments)]
fn draw_incident_wave(
painter: &egui::Painter,
rect: Rect,
axis_y: f32,
wavelength: f32,
amplitude: f32,
omega: f32,
time: f32,
color: Color32,
) {
let mut points = Vec::new();
let k = std::f32::consts::TAU / wavelength;
let mut x = rect.left();
while x <= rect.right() {
let local_x = x - rect.left();
let value = (k * local_x - omega * time).sin();
let y = axis_y - amplitude * value;
points.push(Pos2::new(x, y));
x += 2.0;
}
painter.add(egui::Shape::line(points, Stroke::new(1.3, color)));
}
#[allow(clippy::too_many_arguments)]
fn draw_reflected_wave(
painter: &egui::Painter,
rect: Rect,
axis_y: f32,
wavelength: f32,
amplitude: f32,
reflection: f32,
omega: f32,
time: f32,
color: Color32,
) {
let mut points = Vec::new();
let k = std::f32::consts::TAU / wavelength;
let mut x = rect.left();
while x <= rect.right() {
let local_x = x - rect.left();
let value = reflection * (k * local_x + omega * time).sin();
let y = axis_y - amplitude * value;
points.push(Pos2::new(x, y));
x += 2.0;
}
painter.add(egui::Shape::line(points, Stroke::new(1.3, color)));
}
#[allow(clippy::too_many_arguments)]
fn draw_total_wave(
painter: &egui::Painter,
rect: Rect,
axis_y: f32,
wavelength: f32,
amplitude: f32,
reflection: f32,
omega: f32,
time: f32,
color: Color32,
) {
let mut points = Vec::new();
let k = std::f32::consts::TAU / wavelength;
let mut x = rect.left();
while x <= rect.right() {
let local_x = x - rect.left();
let incident = (k * local_x - omega * time).sin();
let reflected = reflection * (k * local_x + omega * time).sin();
let total = incident + reflected;
let y = axis_y - amplitude * total;
points.push(Pos2::new(x, y));
x += 2.0;
}
painter.add(egui::Shape::line(points, Stroke::new(1.6, color)));
}
#[allow(clippy::too_many_arguments)]
fn draw_stationary_wave(
painter: &egui::Painter,
rect: Rect,
axis_y: f32,
wavelength: f32,
amplitude: f32,
reflection: f32,
omega: f32,
time: f32,
color: Color32,
) {
let mut points = Vec::new();
let k = std::f32::consts::TAU / wavelength;
let r = reflection.abs();
let mut x = rect.left();
while x <= rect.right() {
let local_x = x - rect.left();
let value = if reflection >= 0.0 {
let spatial = (k * local_x).sin();
let temporal = (omega * time).cos();
2.0 * r * spatial * temporal
} else {
let spatial = (k * local_x).cos();
let temporal = (omega * time).sin();
-2.0 * r * spatial * temporal
};
let y = axis_y - amplitude * value;
points.push(Pos2::new(x, y));
x += 2.0;
}
painter.add(egui::Shape::line(points, Stroke::new(1.5, color)));
}
#[allow(clippy::too_many_arguments)]
fn draw_progressive_wave(
painter: &egui::Painter,
rect: Rect,
axis_y: f32,
wavelength: f32,
amplitude: f32,
reflection: f32,
omega: f32,
time: f32,
color: Color32,
) {
let mut points = Vec::new();
let k = std::f32::consts::TAU / wavelength;
let progressive_amplitude = amplitude * (1.0 - reflection.abs());
let mut x = rect.left();
while x <= rect.right() {
let local_x = x - rect.left();
let value = (k * local_x - omega * time).sin();
let y = axis_y - progressive_amplitude * value;
points.push(Pos2::new(x, y));
x += 2.0;
}
painter.add(egui::Shape::line(points, Stroke::new(1.4, color)));
}
fn draw_controls(ui: &mut egui::Ui, app: &mut WaveApp) {
ui.horizontal(|ui| {
ui.label(egui::RichText::new("Coeff de Réflexion").color(Color32::WHITE));
ui.add(egui::Slider::new(&mut app.reflection, -1.0..=1.0).show_value(false));
ui.label(egui::RichText::new(format!("{:.2}", app.reflection)).color(Color32::LIGHT_BLUE));
});
checkbox(ui, "Onde Incidente", &mut app.incident, Color32::RED);
checkbox(ui, "Onde Réfléchie", &mut app.reflected, Color32::GREEN);
checkbox(ui, "Onde Totale", &mut app.total, Color32::BLACK);
checkbox(ui, "Onde Stationnaire", &mut app.stationary, Color32::BLUE);
checkbox(
ui,
"Composante Progressive",
&mut app.progressive,
Color32::MAGENTA,
);
ui.horizontal(|ui| {
ui.label(egui::RichText::new("Longueur").color(Color32::WHITE));
ui.add(egui::Slider::new(&mut app.length, 0.25..=3.0).show_value(false));
ui.label(egui::RichText::new(format!("{:.2}", app.length)).color(Color32::LIGHT_BLUE));
});
}
fn checkbox(ui: &mut egui::Ui, text: &str, value: &mut bool, color: Color32) {
ui.horizontal(|ui| {
let size = egui::vec2(10.0, 10.0);
let (rect, _) = ui.allocate_exact_size(size, egui::Sense::hover());
ui.painter().rect_filled(rect, 2.0, color);
ui.checkbox(value, egui::RichText::new(text).color(Color32::WHITE));
});
}
impl WaveApp {
pub(crate) fn show(&mut self, ui: &mut egui::Ui, _error_manager: &mut ErrorManager) {
egui::ScrollArea::vertical()
.id_salt("animation_viewer")
.show(ui, |ui| self.inner_show(ui));
ui_settings(ui, |ui| {
ui.collapsing("Menu", |ui| {
draw_controls(ui, self);
});
});
}
pub(crate) fn inner_show(&mut self, ui: &mut egui::Ui) {
let ctx = ui.ctx();
self.time += ctx.input(|i| i.stable_dt);
ctx.request_repaint();
ui.vertical_centered(|ui| {
ui.heading(
egui::RichText::new("Réflexion et Onde Stationnaire")
.size(34.0)
.strong(),
);
});
ui.add_space(10.0);
ui.horizontal(|ui| {
let available = ui.available_size();
let canvas_width = available.x;
let canvas_height = 300.0;
egui::Frame::canvas(ui.style())
.fill(Color32::WHITE)
.show(ui, |ui| {
ui.set_min_size(Vec2::new(canvas_width, canvas_height));
let rect = ui.max_rect();
let painter = ui.painter_at(rect);
draw_wave_canvas(
&painter,
rect,
self.time,
self.reflection,
self.length,
self.incident,
self.reflected,
self.total,
self.stationary,
self.progressive,
);
});
});
}
}