Skip to content

Commit 4799c33

Browse files
Format code
1 parent fcec7e9 commit 4799c33

File tree

11 files changed

+62
-40
lines changed

11 files changed

+62
-40
lines changed

benches/benchmarks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
44
use rand::SeedableRng;
55
use rand_xoshiro::Xoshiro256PlusPlus;
66

7-
use rust_random_logo::{Config, render, rand_sigma_factor_ifs};
7+
use rust_random_logo::{rand_sigma_factor_ifs, render, Config};
88

99
fn bench_generate_ifs(c: &mut Criterion) {
1010
c.bench_function("generate_ifs", |b| {

examples/basic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Basic example of using the rust-random-logo library
22
3-
use std::path::PathBuf;
43
use rand::SeedableRng;
54
use rand_xoshiro::Xoshiro256PlusPlus;
5+
use std::path::PathBuf;
66

7-
use rust_random_logo::{Config, render, rand_sigma_factor_ifs};
7+
use rust_random_logo::{rand_sigma_factor_ifs, render, Config};
88

99
fn main() -> Result<(), Box<dyn std::error::Error>> {
1010
// Create a random number generator with a seed

examples/grid.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
//! This example generates 25 different fractal images with different seeds
44
//! and arranges them in a 5x5 grid, saving the result as a single large image.
55
6-
use std::path::PathBuf;
6+
use image::ImageBuffer;
77
use rand::SeedableRng;
88
use rand_xoshiro::Xoshiro256PlusPlus;
9-
use image::ImageBuffer;
9+
use std::path::PathBuf;
1010

11-
use rust_random_logo::{Config, render, rand_sigma_factor_ifs};
11+
use rust_random_logo::{rand_sigma_factor_ifs, render, Config};
1212

1313
fn main() -> Result<(), Box<dyn std::error::Error>> {
1414
// Configuration for each fractal
@@ -31,7 +31,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3131
let grid_height = base_config.height * grid_rows;
3232
let mut grid_image = ImageBuffer::new(grid_width as u32, grid_height as u32);
3333

34-
println!("Generating {} fractals for a {}x{} grid...", grid_rows * grid_cols, grid_rows, grid_cols);
34+
println!(
35+
"Generating {} fractals for a {}x{} grid...",
36+
grid_rows * grid_cols,
37+
grid_rows,
38+
grid_cols
39+
);
3540

3641
// Generate fractals and place them in the grid
3742
for row in 0..grid_rows {
@@ -51,7 +56,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5156
let ifs = rand_sigma_factor_ifs(&mut rng);
5257

5358
// Render the fractal
54-
println!(" Rendering fractal {}/{} with seed {}...", index + 1, grid_rows * grid_cols, seed);
59+
println!(
60+
" Rendering fractal {}/{} with seed {}...",
61+
index + 1,
62+
grid_rows * grid_cols,
63+
seed
64+
);
5565
let fractal = render(rng, &ifs, &config);
5666

5767
// Calculate the position in the grid

src/core/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//! This module provides the Config struct and related functions for loading
44
//! configuration from TOML files.
55
6+
use serde::{Deserialize, Serialize};
67
use std::fs;
78
use std::path::Path;
8-
use serde::{Deserialize, Serialize};
99
use toml;
1010

1111
use crate::error::Result;

src/core/ifs.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
//! The implementation is based on the SVD approach proposed in the
55
//! [Improving Fractal Pre-training](http://catalys1.github.io/fractal-pretraining/) paper.
66
7-
use nalgebra::{Matrix2, Vector2, Rotation2};
8-
use rand::Rng;
7+
use nalgebra::{Matrix2, Rotation2, Vector2};
98
use rand::distributions::{Distribution, WeightedIndex};
9+
use rand::Rng;
1010

1111
use crate::core::affine::Affine;
1212
use crate::core::types::{Matrix2f, Vector2f, IFS};
@@ -36,8 +36,15 @@ impl SigmaFactorIFS {
3636
///
3737
/// A new SigmaFactorIFS
3838
pub fn new(transforms: Vec<Affine>, weights: Vec<f64>) -> Self {
39-
assert_eq!(transforms.len(), weights.len(), "Number of transforms must match number of weights");
40-
Self { transforms, weights }
39+
assert_eq!(
40+
transforms.len(),
41+
weights.len(),
42+
"Number of transforms must match number of weights"
43+
);
44+
Self {
45+
transforms,
46+
weights,
47+
}
4148
}
4249
}
4350

@@ -71,26 +78,26 @@ pub fn sample_svs<R: Rng>(rng: &mut R, alpha: f64, n: usize) -> Vec<(f64, f64)>
7178
let mut b_upper = alpha;
7279

7380
// Sample n-1 pairs
74-
for _ in 0..(n-1) {
81+
for _ in 0..(n - 1) {
7582
// Define sigma1
7683
let sigma1 = uniform(rng, f64::max(0.0, b_lower / 3.0), f64::min(1.0, b_upper));
7784
b_lower = b_lower - sigma1;
7885
b_upper = b_upper - sigma1;
7986

8087
// Define sigma2
81-
let sigma2 = uniform(rng, f64::max(0.0, 0.5 * b_lower), f64::min(sigma1, 0.5 * b_upper));
88+
let sigma2 = uniform(
89+
rng,
90+
f64::max(0.0, 0.5 * b_lower),
91+
f64::min(sigma1, 0.5 * b_upper),
92+
);
8293
b_lower = b_lower - 2.0 * sigma2 + 3.0;
8394
b_upper = b_upper - 2.0 * sigma2;
8495

8596
result.push((sigma1, sigma2));
8697
}
8798

8899
// Last pair
89-
let sigma2 = uniform(
90-
rng,
91-
f64::max(0.0, 0.5 * (b_upper - 1.0)),
92-
b_upper / 3.0,
93-
);
100+
let sigma2 = uniform(rng, f64::max(0.0, 0.5 * (b_upper - 1.0)), b_upper / 3.0);
94101
let sigma1 = b_upper - 2.0 * sigma2;
95102
result.push((sigma1, sigma2));
96103

@@ -173,9 +180,7 @@ pub fn rand_sigma_factor_ifs<R: Rng>(rng: &mut R) -> SigmaFactorIFS {
173180
}
174181

175182
// Create probability weights based on determinants
176-
let mut weights: Vec<f64> = transforms.iter()
177-
.map(|t| t.determinant().abs())
178-
.collect();
183+
let mut weights: Vec<f64> = transforms.iter().map(|t| t.determinant().abs()).collect();
179184

180185
// Normalize weights
181186
let sum: f64 = weights.iter().sum();

src/core/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// This module contains the core components for generating fractal images
33

44
pub mod affine;
5+
pub mod config;
56
pub mod ifs;
6-
pub mod types;
77
pub mod renderer;
8-
pub mod config;
8+
pub mod types;
99
pub mod utils;
1010

1111
// Re-export commonly used items
1212
pub use affine::Affine;
13-
pub use ifs::{SigmaFactorIFS, sample_svs, rand_sigma_factor_ifs};
14-
pub use types::*;
15-
pub use renderer::{generate_points, render};
1613
pub use config::Config;
14+
pub use ifs::{rand_sigma_factor_ifs, sample_svs, SigmaFactorIFS};
15+
pub use renderer::{generate_points, render};
16+
pub use types::*;

src/core/renderer.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ fn normalize_points(xs: &mut Vec<f64>, ys: &mut Vec<f64>, height: usize, width:
9696
/// # Returns
9797
///
9898
/// An RGB image
99-
pub fn render<R: Rng + Clone>(
100-
mut rng: R,
101-
ifs: &SigmaFactorIFS,
102-
config: &Config,
103-
) -> RgbImage {
99+
pub fn render<R: Rng + Clone>(mut rng: R, ifs: &SigmaFactorIFS, config: &Config) -> RgbImage {
104100
let height = config.height;
105101
let width = config.width;
106102
let npoints = config.npoints;
@@ -141,17 +137,28 @@ pub fn render_from_config(config: &Config) -> Result<RgbImage> {
141137

142138
// Validate IFS configuration
143139
if config.ifs_name != "SigmaFactorIFS" {
144-
return Err(Error::ConfigError(format!("Unknown IFS: {}", config.ifs_name)));
140+
return Err(Error::ConfigError(format!(
141+
"Unknown IFS: {}",
142+
config.ifs_name
143+
)));
145144
}
146145

147146
if config.ndims != 2 {
148-
return Err(Error::ConfigError(format!("Unsupported dimension: {}", config.ndims)));
147+
return Err(Error::ConfigError(format!(
148+
"Unsupported dimension: {}",
149+
config.ndims
150+
)));
149151
}
150152

151153
// Create RNG
152154
let mut rng = match config.rng_name.as_str() {
153155
"Xoshiro256PlusPlus" => Xoshiro256PlusPlus::seed_from_u64(config.seed),
154-
_ => return Err(Error::ConfigError(format!("Unknown RNG: {}", config.rng_name))),
156+
_ => {
157+
return Err(Error::ConfigError(format!(
158+
"Unknown RNG: {}",
159+
config.rng_name
160+
)))
161+
}
155162
};
156163

157164
// Create IFS

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Error types for the rust-random-logo library
22
3-
use thiserror::Error;
43
use std::io;
4+
use thiserror::Error;
55

66
/// Error type for the rust-random-logo library
77
#[derive(Error, Debug)]

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub mod error;
3232

3333
// Re-export commonly used items
3434
pub use core::affine::Affine;
35-
pub use core::ifs::{SigmaFactorIFS, sample_svs, rand_sigma_factor_ifs};
3635
pub use core::config::Config;
36+
pub use core::ifs::{rand_sigma_factor_ifs, sample_svs, SigmaFactorIFS};
3737
pub use core::renderer::{generate_points, render, render_from_config};
3838
pub use error::{Error, Result};

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::env;
44
use std::path::PathBuf;
55
use std::process;
66

7-
use rust_random_logo::{Config, render_from_config};
7+
use rust_random_logo::{render_from_config, Config};
88

99
fn main() {
1010
// Parse command-line arguments

0 commit comments

Comments
 (0)