Skip to content

Commit

Permalink
Added coloring_function to MandelbrotModel
Browse files Browse the repository at this point in the history
  • Loading branch information
jortrr committed Sep 19, 2023
1 parent d9ac0bb commit 724d507
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 62 deletions.
63 changes: 34 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub use controller::config::Config;
use controller::key_bindings::KeyBindings;
use controller::user_input::{ask, pick_option};
use model::complex_plane::{ComplexPlane, View};
use model::mandelbrot_model::ColoringFunction;
pub use model::mandelbrot_model::MandelbrotModel;
use model::{pixel_buffer, pixel_plane, rendering};
use pixel_buffer::PixelBuffer;
Expand All @@ -48,7 +49,6 @@ use view::coloring::ColorChannelMapping;
use view::coloring::TrueColor;

//Coloring function
type ColoringFunction = fn(iterations: u32, max_iterations: u32) -> TrueColor;
static COLORING_FUNCTION: ColoringFunction = TrueColor::new_from_bernstein_polynomials;

//Color channel mapping
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Default for InteractionVariables {
}

// Handle any key events
fn handle_key_events(window: &Window, k: &KeyBindings, coloring_function: &mut ColoringFunction) {
fn handle_key_events(window: &Window, k: &KeyBindings) {
if let Some(key) = window.get_keys_pressed(minifb::KeyRepeat::No).first() {
print!("\nKey pressed: ");
k.print_key(key);
Expand All @@ -137,11 +137,11 @@ fn handle_key_events(window: &Window, k: &KeyBindings, coloring_function: &mut C
match key {
Key::K => k.print(),
Key::A => {
*coloring_function = pick_option(&[
mandelbrot_model.coloring_function = pick_option(&[
("HSV", TrueColor::new_from_hsv_colors),
("Bernstein polynomials", TrueColor::new_from_bernstein_polynomials),
]);
render(&mut mandelbrot_model, COLORING_FUNCTION);
render(&mut mandelbrot_model);
}
_ => (),
}
Expand All @@ -161,13 +161,13 @@ fn handle_left_mouse_clicked(mandelbrot_model: &MandelbrotModel, x: f32, y: f32)
println!();
}

fn handle_right_mouse_clicked(mandelbrot_model: &mut MandelbrotModel, x: f32, y: f32, coloring_function: ColoringFunction) {
fn handle_right_mouse_clicked(mandelbrot_model: &mut MandelbrotModel, x: f32, y: f32) {
println!("\nMouseButton::Right -> Move to ({x}, {y})");
let new_center = mandelbrot_model.c.complex_from_pixel_plane(x.into(), y.into());
println!("mandelbrot_model.c.center: {:?}", mandelbrot_model.c.center());
println!("new_center: {:?}", new_center);

rendering::translate_to_center_and_render_efficiently(mandelbrot_model, &new_center, coloring_function);
rendering::translate_to_center_and_render_efficiently(mandelbrot_model, &new_center);
mandelbrot_model.c.print();
println!();
}
Expand Down Expand Up @@ -199,7 +199,7 @@ impl MouseClickRecorder {
}
}

fn handle_mouse_events(window: &Window, coloring_function: ColoringFunction) {
fn handle_mouse_events(window: &Window) {
let mut mandelbrot_model = MandelbrotModel::get_instance();
static LEFT_MOUSE_RECORDER: MouseClickRecorder = MouseClickRecorder::new(MouseButton::Left); //Static variable with interior mutability to toggle mouse clicks; without such a variable, clicking the screen once would result in multiple actions

Check warning on line 204 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

adding items after statements is confusing, since items exist from the start of the scope

warning: adding items after statements is confusing, since items exist from the start of the scope --> src/lib.rs:204:5 | 204 | static LEFT_MOUSE_RECORDER: MouseClickRecorder = MouseClickRecorder::new(MouseButton::Left); //Static variable with interior mutabili... | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements = note: `#[warn(clippy::items_after_statements)]` implied by `#[warn(clippy::pedantic)]`
static RIGHT_MOUSE_RECORDER: MouseClickRecorder = MouseClickRecorder::new(MouseButton::Right);

Check warning on line 205 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

adding items after statements is confusing, since items exist from the start of the scope

warning: adding items after statements is confusing, since items exist from the start of the scope --> src/lib.rs:205:5 | 205 | static RIGHT_MOUSE_RECORDER: MouseClickRecorder = MouseClickRecorder::new(MouseButton::Right); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements
Expand All @@ -212,19 +212,20 @@ fn handle_mouse_events(window: &Window, coloring_function: ColoringFunction) {

//Right mouse actions
if RIGHT_MOUSE_RECORDER.was_clicked(window) {
handle_right_mouse_clicked(&mut mandelbrot_model, x, y, coloring_function);
handle_right_mouse_clicked(&mut mandelbrot_model, x, y);
}
}
}

fn render(mandelbrot_model: &mut MandelbrotModel, coloring_function: ColoringFunction) {
rendering::render_complex_plane_into_buffer(mandelbrot_model, coloring_function);
fn render(mandelbrot_model: &mut MandelbrotModel) {
//TODO: Observer pattern view -> model to update the view, instead of rendering manually
rendering::render_complex_plane_into_buffer(mandelbrot_model);
mandelbrot_model.c.print();
}

fn set_view(mandelbrot_model: &mut MandelbrotModel, view: &View) {
mandelbrot_model.c.set_view(view);
render(mandelbrot_model, COLORING_FUNCTION);
render(mandelbrot_model);
}

///Prints Mandelbrot ASCII art :) </br>
Expand Down Expand Up @@ -266,9 +267,9 @@ fn print_command_info() {
pub fn run() -> Result<(), Box<dyn Error>> {
let mut mandelbrot_model = MandelbrotModel::get_instance();
//Coloring function
let mut coloring_function = COLORING_FUNCTION;
mandelbrot_model.coloring_function = COLORING_FUNCTION;
//Color channel mapping
//MandelbrotModel::get_instance().p.color_channel_mapping = COLOR_CHANNEL_MAPPING;
mandelbrot_model.p.color_channel_mapping = COLOR_CHANNEL_MAPPING;
// Create a new window
let mut window = Window::new(
"Mandelbrot set viewer",
Expand All @@ -289,31 +290,31 @@ pub fn run() -> Result<(), Box<dyn Error>> {
key_bindings.add(Key::Up, "Move up translation_amount pixels", || {
let mut mandelbrot_model = MandelbrotModel::get_instance();
let rows = mandelbrot_model.vars.translation_amount;
rendering::translate_and_render_efficiently(&mut mandelbrot_model, rows.into(), 0, COLORING_FUNCTION);
rendering::translate_and_render_efficiently(&mut mandelbrot_model, rows.into(), 0);
mandelbrot_model.c.print();
});
key_bindings.add(Key::Down, "Move down translation_amount pixels", || {
let mut mandelbrot_model = MandelbrotModel::get_instance();
let rows = -i16::from(mandelbrot_model.vars.translation_amount);
rendering::translate_and_render_efficiently(&mut mandelbrot_model, rows, 0, COLORING_FUNCTION);
rendering::translate_and_render_efficiently(&mut mandelbrot_model, rows, 0);
mandelbrot_model.c.print();
});
key_bindings.add(Key::Left, "Move left translation_amount pixels", || {
let mut mandelbrot_model = MandelbrotModel::get_instance();
let columns = -i16::from(mandelbrot_model.vars.translation_amount);
rendering::translate_and_render_efficiently(&mut mandelbrot_model, 0, columns, COLORING_FUNCTION);
rendering::translate_and_render_efficiently(&mut mandelbrot_model, 0, columns);
mandelbrot_model.c.print();
});
key_bindings.add(Key::Right, "Move right translation_amount pixels", || {
let mut mandelbrot_model = MandelbrotModel::get_instance();
let columns = mandelbrot_model.vars.translation_amount.into();
rendering::translate_and_render_efficiently(&mut mandelbrot_model, 0, columns, COLORING_FUNCTION);
rendering::translate_and_render_efficiently(&mut mandelbrot_model, 0, columns);
mandelbrot_model.c.print();
});
key_bindings.add(Key::R, "Reset the Mandelbrot set view to the starting view", || {
let mut mandelbrot_model = MandelbrotModel::get_instance();

Check warning on line 315 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lib.rs:315:17 | 314 | key_bindings.add(Key::R, "Reset the Mandelbrot set view to the starting view", || { | _______________________________________________________________________________________- 315 | | let mut mandelbrot_model = MandelbrotModel::get_instance(); | | ^^^^^^^^^^^^^^^^ 316 | | mandelbrot_model.c.reset(); 317 | | render(&mut mandelbrot_model); 318 | | }); | |_____- temporary `mandelbrot_model` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: merge the temporary construction with its single usage | 315 ~ 316 + MandelbrotModel::get_instance().reset(); | help: remove separated single usage | 316 - mandelbrot_model.c.reset(); 316 + |
mandelbrot_model.c.reset();
render(&mut mandelbrot_model, COLORING_FUNCTION);
render(&mut mandelbrot_model);
});
key_bindings.add(Key::NumPadPlus, "Increment translation_amount", || {
let mut mandelbrot_model = MandelbrotModel::get_instance();
Expand Down Expand Up @@ -346,7 +347,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
let mut mandelbrot_model = MandelbrotModel::get_instance();

Check warning on line 347 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lib.rs:347:17 | 346 | key_bindings.add(Key::LeftBracket, "Scale the view by scaling_factor, effectively zooming in", || { | _______________________________________________________________________________________________________- 347 | | let mut mandelbrot_model = MandelbrotModel::get_instance(); | | ^^^^^^^^^^^^^^^^ 348 | | let scaling_factor = mandelbrot_model.vars.scaling_factor(); 349 | | mandelbrot_model.c.scale(scaling_factor); 350 | | render(&mut mandelbrot_model); 351 | | }); | |_____- temporary `mandelbrot_model` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: drop the temporary after the end of its last usage | 349 ~ mandelbrot_model.c.scale(scaling_factor); 350 + drop(mandelbrot_model); |
let scaling_factor = mandelbrot_model.vars.scaling_factor();
mandelbrot_model.c.scale(scaling_factor);
render(&mut mandelbrot_model, COLORING_FUNCTION);
render(&mut mandelbrot_model);
});
key_bindings.add(
Key::RightBracket,
Expand All @@ -355,7 +356,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
let mut mandelbrot_model = MandelbrotModel::get_instance();

Check warning on line 356 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lib.rs:356:21 | 355 | || { | ____________- 356 | | let mut mandelbrot_model = MandelbrotModel::get_instance(); | | ^^^^^^^^^^^^^^^^ 357 | | let inverse_scaling_factor = mandelbrot_model.vars.inverse_scaling_factor(); 358 | | mandelbrot_model.c.scale(inverse_scaling_factor); 359 | | render(&mut mandelbrot_model); 360 | | }, | |_________- temporary `mandelbrot_model` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: drop the temporary after the end of its last usage | 358 ~ mandelbrot_model.c.scale(inverse_scaling_factor); 359 + drop(mandelbrot_model); |
let inverse_scaling_factor = mandelbrot_model.vars.inverse_scaling_factor();
mandelbrot_model.c.scale(inverse_scaling_factor);
render(&mut mandelbrot_model, COLORING_FUNCTION);
render(&mut mandelbrot_model);
},
);
key_bindings.add(Key::V, "Prints the current Mandelbrot set view; the center and scale", || {
Expand Down Expand Up @@ -419,7 +420,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
ComplexPlane::new(mandelbrot_model.config.image_width, mandelbrot_model.config.image_height);
image_p.color_channel_mapping = mandelbrot_model.p.color_channel_mapping;
image_c.set_view(&mandelbrot_model.c.get_view());
rendering::render_complex_plane_into_buffer(&mut mandelbrot_model, COLORING_FUNCTION);
rendering::render_complex_plane_into_buffer(&mut mandelbrot_model);
image_p.save_as_png(
&time_stamp,
&mandelbrot_model.c.get_view(),
Expand All @@ -432,21 +433,21 @@ pub fn run() -> Result<(), Box<dyn Error>> {
key_bindings.add(Key::I, "Manually input a Mandelbrot set view", || {
let mut mandelbrot_model = MandelbrotModel::get_instance();

Check warning on line 434 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lib.rs:434:17 | 433 | key_bindings.add(Key::I, "Manually input a Mandelbrot set view", || { | _________________________________________________________________________- 434 | | let mut mandelbrot_model = MandelbrotModel::get_instance(); | | ^^^^^^^^^^^^^^^^ 435 | | mandelbrot_model.c.set_view(&View::new(ask("x"), ask("y"), ask("scale"))); 436 | | render(&mut mandelbrot_model); 437 | | }); | |_____- temporary `mandelbrot_model` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: merge the temporary construction with its single usage | 434 ~ 435 + MandelbrotModel::get_instance().set_view(&View::new(ask("x"), ask("y"), ask("scale"))); | help: remove separated single usage | 435 - mandelbrot_model.c.set_view(&View::new(ask("x"), ask("y"), ask("scale"))); 435 + |
mandelbrot_model.c.set_view(&View::new(ask("x"), ask("y"), ask("scale")));
render(&mut mandelbrot_model, COLORING_FUNCTION);
render(&mut mandelbrot_model);
});
key_bindings.add(Key::A, "Pick an algorithm to color the Mandelbrot set view", empty_closure);
key_bindings.add(Key::M, "Change the Mandelbrot set view max_iterations", || {
let mut mandelbrot_model = MandelbrotModel::get_instance();

Check warning on line 440 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lib.rs:440:17 | 439 | key_bindings.add(Key::M, "Change the Mandelbrot set view max_iterations", || { | __________________________________________________________________________________- 440 | | let mut mandelbrot_model = MandelbrotModel::get_instance(); | | ^^^^^^^^^^^^^^^^ 441 | | mandelbrot_model.m.max_iterations = ask("max_iterations"); 442 | | render(&mut mandelbrot_model); 443 | | }); | |_____- temporary `mandelbrot_model` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: merge the temporary construction with its single usage | 440 ~ 441 + MandelbrotModel::get_instance().; | help: remove separated single usage | 441 - mandelbrot_model.m.max_iterations = ask("max_iterations"); 441 + |
mandelbrot_model.m.max_iterations = ask("max_iterations");
render(&mut mandelbrot_model, COLORING_FUNCTION);
render(&mut mandelbrot_model);
});
key_bindings.add(
Key::O,
"Change the Mandelbrot set view color channel mapping, xyz -> RGB, where x,y,z ∈ {{'R','G','B'}} (case-insensitive)",
|| {
let mut mandelbrot_model = MandelbrotModel::get_instance();

Check warning on line 448 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lib.rs:448:21 | 447 | || { | ____________- 448 | | let mut mandelbrot_model = MandelbrotModel::get_instance(); | | ^^^^^^^^^^^^^^^^ 449 | | mandelbrot_model.p.color_channel_mapping = ask("color_channel_mapping"); 450 | | render(&mut mandelbrot_model); 451 | | }, | |_________- temporary `mandelbrot_model` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: merge the temporary construction with its single usage | 448 ~ 449 + MandelbrotModel::get_instance().; | help: remove separated single usage | 449 - mandelbrot_model.p.color_channel_mapping = ask("color_channel_mapping"); 449 + |
mandelbrot_model.p.color_channel_mapping = ask("color_channel_mapping");
render(&mut mandelbrot_model, COLORING_FUNCTION);
render(&mut mandelbrot_model);
},
);
key_bindings.add(
Expand All @@ -455,7 +456,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
|| {
let mut mandelbrot_model = MandelbrotModel::get_instance();

Check warning on line 457 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lib.rs:457:21 | 456 | || { | ____________- 457 | | let mut mandelbrot_model = MandelbrotModel::get_instance(); | | ^^^^^^^^^^^^^^^^ 458 | | mandelbrot_model.config.supersampling_amount = ask::<u8>("supersampling_amount").clamp(1, 64); 459 | | render(&mut mandelbrot_model); 460 | | }, | |_________- temporary `mandelbrot_model` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: merge the temporary construction with its single usage | 457 ~ 458 + MandelbrotModel::get_instance().; | help: remove separated single usage | 458 - mandelbrot_model.config.supersampling_amount = ask::<u8>("supersampling_amount").clamp(1, 64); 458 + |
mandelbrot_model.config.supersampling_amount = ask::<u8>("supersampling_amount").clamp(1, 64);
render(&mut mandelbrot_model, COLORING_FUNCTION);
render(&mut mandelbrot_model);
},
);
key_bindings.add(
Expand Down Expand Up @@ -485,16 +486,16 @@ pub fn run() -> Result<(), Box<dyn Error>> {
println!();

println!("Rendering Mandelbrot set default view");
rendering::render_complex_plane_into_buffer(&mut mandelbrot_model, coloring_function);
rendering::render_complex_plane_into_buffer(&mut mandelbrot_model);
drop(mandelbrot_model);

// Main loop
while window.is_open() && !window.is_key_down(Key::Escape) {
// Handle any window events
handle_key_events(&window, &key_bindings, &mut coloring_function);
handle_key_events(&window, &key_bindings);

//Handle any mouse events
handle_mouse_events(&window, coloring_function);
handle_mouse_events(&window);

let mandelbrot_model = MandelbrotModel::get_instance();
// Update the window with the new buffer
Expand All @@ -506,6 +507,10 @@ pub fn run() -> Result<(), Box<dyn Error>> {
)
.unwrap();
}

if window.is_key_down(Key::Escape) {
println!("Escape pressed, application exited gracefully.");
} else {
println!("Window closed, application exited gracefully.")

Check warning on line 513 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

consider adding a `;` to the last statement for consistent formatting

warning: consider adding a `;` to the last statement for consistent formatting --> src/lib.rs:513:9 | 513 | println!("Window closed, application exited gracefully.") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `println!("Window closed, application exited gracefully.");` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
}
Ok(())
}

Check warning on line 516 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many lines (232/100)

warning: this function has too many lines (232/100) --> src/lib.rs:267:1 | 267 | / pub fn run() -> Result<(), Box<dyn Error>> { 268 | | let mut mandelbrot_model = MandelbrotModel::get_instance(); 269 | | //Coloring function 270 | | mandelbrot_model.coloring_function = COLORING_FUNCTION; ... | 515 | | Ok(()) 516 | | } | |_^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_lines = note: `#[warn(clippy::too_many_lines)]` implied by `#[warn(clippy::pedantic)]`
6 changes: 5 additions & 1 deletion src/model/mandelbrot_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ use std::{
sync::{Mutex, MutexGuard},
};

use crate::{Config, InteractionVariables};
use crate::{view::coloring::TrueColor, Config, InteractionVariables};

use super::{complex_plane::ComplexPlane, mandelbrot_function::MandelbrotFunction, pixel_buffer::PixelBuffer, pixel_plane::PixelPlane};

lazy_static! {
static ref MANDELBROT_MODEL_INSTANCE: Mutex<MandelbrotModel> = Mutex::new(MandelbrotModel::new());
}

pub type ColoringFunction = fn(iterations: u32, max_iterations: u32) -> TrueColor;

pub struct MandelbrotModel {
pub config: Config,
pub c: ComplexPlane,
pub p: PixelBuffer,
pub vars: InteractionVariables,
pub amount_of_threads: usize,
pub m: MandelbrotFunction,
pub coloring_function: ColoringFunction,
}

impl MandelbrotModel {
Expand All @@ -34,6 +37,7 @@ impl MandelbrotModel {
vars: InteractionVariables::default(),
amount_of_threads: num_cpus::get(),
m: MandelbrotFunction::new(config.max_iterations, config.orbit_radius),
coloring_function: TrueColor::new_from_bernstein_polynomials,
}
}

Check warning on line 42 in src/model/mandelbrot_model.rs

View workflow job for this annotation

GitHub Actions / clippy

you should consider adding a `Default` implementation for `MandelbrotModel`

warning: you should consider adding a `Default` implementation for `MandelbrotModel` --> src/model/mandelbrot_model.rs:28:5 | 28 | / pub fn new() -> MandelbrotModel { 29 | | let config = Config::build(env::args()).unwrap_or_else(|err| { 30 | | eprintln!("Problem parsing arguments: {}", err); 31 | | process::exit(1); ... | 41 | | } 42 | | } | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default help: try adding this | 27 + impl Default for MandelbrotModel { 28 + fn default() -> Self { 29 + Self::new() 30 + } 31 + } |

Expand Down
44 changes: 12 additions & 32 deletions src/model/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,9 @@ impl RenderBox {
/// `orbit_radius` determines when Zn is considered to have gone to infinity.
/// `max_iterations` concerns the maximum amount of times the Mandelbrot formula will be applied to each Complex number.
/// Note: This function is computationally intensive, and should not be used for translations
pub fn render_complex_plane_into_buffer(
mandelbrot_model: &mut MandelbrotModel,
coloring_function: fn(iterations: u32, max_iterations: u32) -> TrueColor,
) {
pub fn render_complex_plane_into_buffer(mandelbrot_model: &mut MandelbrotModel) {
let render_box = RenderBox::new(0, mandelbrot_model.p.pixel_plane.width, 0, mandelbrot_model.p.pixel_plane.height);
render_box_render_complex_plane_into_buffer(mandelbrot_model, render_box, coloring_function);
render_box_render_complex_plane_into_buffer(mandelbrot_model, render_box);
}

/// Render the Complex plane c into the 32-bit pixel buffer by applying the Mandelbrot formula iteratively to every Complex point mapped to a pixel in the buffer.
Expand All @@ -77,11 +74,7 @@ pub fn render_complex_plane_into_buffer(
/// * `coloring_function` - e.g. `TrueColor::new_from_hsv`
/// # Panics
/// If `lock().unwrap()` panics
pub fn render_box_render_complex_plane_into_buffer(
mandelbrot_model: &mut MandelbrotModel,
render_box: RenderBox,
coloring_function: fn(iterations: u32, max_iterations: u32) -> TrueColor,
) {
pub fn render_box_render_complex_plane_into_buffer(mandelbrot_model: &mut MandelbrotModel, render_box: RenderBox) {
let time = benchmark_start();
let supersampling_amount = mandelbrot_model.config.supersampling_amount.clamp(1, 64); //Supersampling_amount should be at least 1 and atmost 64
render_box.print();
Expand All @@ -104,6 +97,7 @@ pub fn render_box_render_complex_plane_into_buffer(
let pixel_buffer = (mandelbrot_model.p).clone();
let ms = (mandelbrot_model.m).clone();
let atm = Arc::clone(&current_progress_atomic);
let coloring_function = (mandelbrot_model.coloring_function).clone();

Check warning on line 100 in src/model/rendering.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `fn(u32, u32) -> TrueColor` which implements the `Copy` trait

warning: using `clone` on type `fn(u32, u32) -> TrueColor` which implements the `Copy` trait --> src/model/rendering.rs:100:33 | 100 | let coloring_function = (mandelbrot_model.coloring_function).clone(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `(mandelbrot_model.coloring_function)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy

let handle = thread::spawn(move || {

Check warning on line 102 in src/model/rendering.rs

View workflow job for this annotation

GitHub Actions / clippy

the function has a cognitive complexity of (7/5)

warning: the function has a cognitive complexity of (7/5) --> src/model/rendering.rs:102:41 | 102 | let handle = thread::spawn(move || { | ^^ | = help: you could split it up into multiple smaller functions = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity = note: `#[warn(clippy::cognitive_complexity)]` implied by `#[warn(clippy::nursery)]`
let mut thread_chunks = Vec::new();
Expand Down Expand Up @@ -169,12 +163,7 @@ pub fn render_box_render_complex_plane_into_buffer(
benchmark("render_box_render_complex_plane_into_buffer()", time);
}

pub fn translate_and_render_complex_plane_buffer(
mandelbrot_model: &mut MandelbrotModel,
rows: i128,
columns: i128,
coloring_function: fn(iterations: u32, max_iterations: u32) -> TrueColor,
) {
pub fn translate_and_render_complex_plane_buffer(mandelbrot_model: &mut MandelbrotModel, rows: i128, columns: i128) {
println!("rows: {}, columns: {}", rows, columns);
let max_x: usize = if columns > 0 {
columns as usize
Expand All @@ -194,28 +183,23 @@ pub fn translate_and_render_complex_plane_buffer(
0,
mandelbrot_model.p.pixel_plane.height,
);
render_box_render_complex_plane_into_buffer(mandelbrot_model, render_box, coloring_function);
render_box_render_complex_plane_into_buffer(mandelbrot_model, render_box);
} else if columns == 0 {
let render_box = RenderBox::new(
0,
mandelbrot_model.p.pixel_plane.width,
(max_y as i128 - rows.abs()) as usize,
max_y,
);
render_box_render_complex_plane_into_buffer(mandelbrot_model, render_box, coloring_function);
render_box_render_complex_plane_into_buffer(mandelbrot_model, render_box);
} else {
println!("ERROR: translate_and_render_complex_plane_buffer() requires that rows == 0 || columns == 0");
}
}

///# Panics
/// If `rows_up` != 0 && `columns_right` != 0
pub fn translate_and_render_efficiently(
mandelbrot_model: &mut MandelbrotModel,
rows_up: i16,
columns_right: i16,
coloring_function: fn(iterations: u32, max_iterations: u32) -> TrueColor,
) {
pub fn translate_and_render_efficiently(mandelbrot_model: &mut MandelbrotModel, rows_up: i16, columns_right: i16) {
assert!(
rows_up == 0 || columns_right == 0,
"translate_and_render_efficiently: rows_up should be 0 or columns_right should be 0!"
Expand All @@ -227,14 +211,10 @@ pub fn translate_and_render_efficiently(
column_sign * mandelbrot_model.c.pixels_to_real(columns_right.unsigned_abs() as u8),
row_sign * mandelbrot_model.c.pixels_to_imaginary(rows_up.unsigned_abs() as u8),
);
translate_and_render_complex_plane_buffer(mandelbrot_model, rows_up.into(), (-columns_right).into(), coloring_function);
translate_and_render_complex_plane_buffer(mandelbrot_model, rows_up.into(), (-columns_right).into());
}

pub fn translate_to_center_and_render_efficiently(
mandelbrot_model: &mut MandelbrotModel,
new_center: &Complex,
coloring_function: fn(iterations: u32, max_iterations: u32) -> TrueColor,
) {
pub fn translate_to_center_and_render_efficiently(mandelbrot_model: &mut MandelbrotModel, new_center: &Complex) {
let mut translation: Complex = new_center.subtract(&mandelbrot_model.c.center());
//Mirror the y translation because the screen y is mirrored compared to the complex plane y axis
translation.y = -translation.y;
Expand All @@ -243,13 +223,13 @@ pub fn translate_to_center_and_render_efficiently(
mandelbrot_model.c.translate(translation.x, 0.0);
let columns_right = -mandelbrot_model.c.real_to_pixels(translation.x);
dbg!(columns_right);
translate_and_render_complex_plane_buffer(mandelbrot_model, 0, columns_right.into(), coloring_function);
translate_and_render_complex_plane_buffer(mandelbrot_model, 0, columns_right.into());

//Translate y, up
mandelbrot_model.c.translate(0.0, translation.y);
let rows_up = -mandelbrot_model.c.imaginary_to_pixels(translation.y);
dbg!(rows_up);
translate_and_render_complex_plane_buffer(mandelbrot_model, rows_up.into(), 0, coloring_function);
translate_and_render_complex_plane_buffer(mandelbrot_model, rows_up.into(), 0);
}

fn benchmark_start() -> Instant {
Expand Down

0 comments on commit 724d507

Please sign in to comment.