parallelization
This commit is contained in:
parent
8f0add0e28
commit
74f80bcf9b
3 changed files with 38 additions and 10 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -219,6 +219,7 @@ dependencies = [
|
||||||
"image",
|
"image",
|
||||||
"indicatif",
|
"indicatif",
|
||||||
"num-complex",
|
"num-complex",
|
||||||
|
"rayon",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -10,4 +10,5 @@ edition = "2018"
|
||||||
image = "0.23.14"
|
image = "0.23.14"
|
||||||
num-complex = "0.4.0"
|
num-complex = "0.4.0"
|
||||||
# uuid = { version = "0.8", features = ["v4"] }
|
# uuid = { version = "0.8", features = ["v4"] }
|
||||||
indicatif = "0.15.0"
|
indicatif = "0.15.0"
|
||||||
|
rayon = "1.5.0"
|
||||||
44
src/main.rs
44
src/main.rs
|
|
@ -5,9 +5,16 @@ use image::{RgbImage, ImageBuffer, Rgb};
|
||||||
use std::fmt::Error;
|
use std::fmt::Error;
|
||||||
use num_complex::Complex;
|
use num_complex::Complex;
|
||||||
use indicatif::ProgressBar;
|
use indicatif::ProgressBar;
|
||||||
|
use rayon::prelude::*;
|
||||||
|
|
||||||
const THRESHHOLD: f64 = 200_i32.pow(2) as f64;
|
const THRESHHOLD: f64 = 200_i32.pow(2) as f64;
|
||||||
|
|
||||||
|
struct PixelInfo {
|
||||||
|
x: u32,
|
||||||
|
y: u32,
|
||||||
|
color: Rgb<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
fn calculate_color(number: Complex<f64>, iterations: u32) -> Result<Rgb<u8>, Error> {
|
fn calculate_color(number: Complex<f64>, iterations: u32) -> Result<Rgb<u8>, Error> {
|
||||||
let mut z: Complex<f64> = Complex::new(0.0, 0.0);
|
let mut z: Complex<f64> = Complex::new(0.0, 0.0);
|
||||||
let mut cycle = Vec::with_capacity(iterations as usize);
|
let mut cycle = Vec::with_capacity(iterations as usize);
|
||||||
|
|
@ -67,8 +74,12 @@ fn generate_mandelbrot(width: u32,
|
||||||
|
|
||||||
let bar = ProgressBar::new(width as u64);
|
let bar = ProgressBar::new(width as u64);
|
||||||
|
|
||||||
for x in 0..width {
|
let mut all_pixels: Vec<Vec<PixelInfo>> = vec![];
|
||||||
|
|
||||||
|
(0..width).into_par_iter().map(|x| {
|
||||||
bar.inc(1);
|
bar.inc(1);
|
||||||
|
let mut row: Vec<PixelInfo> = vec![];
|
||||||
|
|
||||||
for y in 0..height {
|
for y in 0..height {
|
||||||
let x_offset: f64 = (x as f64 / width as f64) * section_width;
|
let x_offset: f64 = (x as f64 / width as f64) * section_width;
|
||||||
let y_offset: f64 = (y as f64 / width as f64) * section_height;
|
let y_offset: f64 = (y as f64 / width as f64) * section_height;
|
||||||
|
|
@ -76,9 +87,22 @@ fn generate_mandelbrot(width: u32,
|
||||||
let color = calculate_color(
|
let color = calculate_color(
|
||||||
Complex::new(upper_left.re + x_offset, upper_left.im + y_offset),
|
Complex::new(upper_left.re + x_offset, upper_left.im + y_offset),
|
||||||
iterations).unwrap();
|
iterations).unwrap();
|
||||||
out_image.put_pixel(x, y, color);
|
row.push(PixelInfo {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
color,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
row
|
||||||
|
}).collect_into_vec(&mut all_pixels);
|
||||||
|
|
||||||
|
for vector in all_pixels {
|
||||||
|
for pixel in vector {
|
||||||
|
out_image.put_pixel(pixel.x, pixel.y, pixel.color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bar.finish_with_message("Rendered frame.");
|
bar.finish_with_message("Rendered frame.");
|
||||||
Ok(out_image)
|
Ok(out_image)
|
||||||
}
|
}
|
||||||
|
|
@ -100,16 +124,17 @@ fn render_around_point(image_size: u32,
|
||||||
lower_right)
|
lower_right)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_sequence(num_frames: u32,
|
fn render_sequence(end_frame: u32,
|
||||||
|
start_frame: u32,
|
||||||
image_size: u32,
|
image_size: u32,
|
||||||
iterations: u32,
|
iterations: u32,
|
||||||
initial_size: f64,
|
initial_size: f64,
|
||||||
zoom_point: Complex<f64>,
|
zoom_point: Complex<f64>,
|
||||||
scale_factor: f32,
|
scale_factor: f32,
|
||||||
path: String) {
|
path: String) {
|
||||||
let mut scale = initial_size;
|
let mut scale = initial_size * (1.0 - scale_factor).powi(start_frame as i32) as f64;
|
||||||
|
|
||||||
for frame in 0..num_frames {
|
for frame in start_frame..end_frame {
|
||||||
let fractal: RgbImage = render_around_point(image_size,
|
let fractal: RgbImage = render_around_point(image_size,
|
||||||
iterations,
|
iterations,
|
||||||
scale,
|
scale,
|
||||||
|
|
@ -125,12 +150,13 @@ fn render_sequence(num_frames: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
render_sequence(10,
|
render_sequence(240,
|
||||||
|
0,
|
||||||
500,
|
500,
|
||||||
200,
|
200,
|
||||||
1.0,
|
2.0,
|
||||||
Complex::new(-0.5, -0.5),
|
Complex::new(-0.761574, -0.0847596),
|
||||||
1.0 / 10.0,
|
1.0 / 10.0,
|
||||||
"./out/".parse().unwrap(),
|
"./out/".parse().unwrap(),
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue