Skip to content

Commit

Permalink
Merge pull request #43 from nathiss/development
Browse files Browse the repository at this point in the history
[0.1.4] Fix off by one error
  • Loading branch information
nathiss authored Apr 28, 2023
2 parents f02f229 + 50f0906 commit edaa831
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 52 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "franklin"
description = "A utility for generating images through evolution."
version = "0.1.3"
version = "0.1.4"
edition = "2021"
rust-version = "1.58"
rust-version = "1.69"
authors = ["Kamil Rusin"]
readme = "README.md"
homepage = "https://github.com/nathiss/franklin"
Expand All @@ -20,9 +20,9 @@ include = [

[dependencies]
anyhow = "1.0.53"
show-image = "0.10.1"
show-image = "0.13.1"
rand = "0.8.4"
clap = { version = "3.1.0", features = ["cargo"] }
clap = { version = "3.2.25", features = ["cargo"] }
rayon = "1.5.1"
num-integer = "0.1.44"

Expand Down
18 changes: 9 additions & 9 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use franklin::{
LeftOrRightCloneCrossover,
},
fitness::{AbsoluteDistance, FitnessFunction, SquareDistance},
mutators::{Mutator, RectangleMutator, TriangleMutator, CircleMutator},
mutators::{CircleMutator, Mutator, RectangleMutator, TriangleMutator},
ArgParser, ColorMode, DisplayCondition, EnvironmentBuilder, ImageReader, SaveCondition,
};

Expand All @@ -62,28 +62,28 @@ fn get_color_mode_from_name(name: &str) -> Result<ColorMode> {
#[doc(hidden)]
fn get_mutator_from_name(name: &str) -> Result<Box<dyn Mutator + Send + Sync + 'static>> {
match name {
"Rectangle" => Ok(Box::new(RectangleMutator::default())),
"Triangle" => Ok(Box::new(TriangleMutator::default())),
"Circle" => Ok(Box::new(CircleMutator::default())),
"Rectangle" => Ok(Box::<RectangleMutator>::default()),
"Triangle" => Ok(Box::<TriangleMutator>::default()),
"Circle" => Ok(Box::<CircleMutator>::default()),
_ => Err(Error::msg("Unknown mutator.")),
}
}

#[doc(hidden)]
fn get_fitness_from_name(name: &str) -> Result<Box<dyn FitnessFunction + Send + Sync + 'static>> {
match name {
"SquareDistance" => Ok(Box::new(SquareDistance::default())),
"AbsoluteDistance" => Ok(Box::new(AbsoluteDistance::default())),
"SquareDistance" => Ok(Box::<SquareDistance>::default()),
"AbsoluteDistance" => Ok(Box::<AbsoluteDistance>::default()),
_ => Err(Error::msg("Unknown fitness function.")),
}
}

#[doc(hidden)]
fn get_crossover_from_name(name: &str) -> Result<Box<dyn CrossoverFunction + Send + 'static>> {
match name {
"LeftOrRight" => Ok(Box::new(LeftOrRightCloneCrossover::default())),
"EqualHalfs" => Ok(Box::new(EqualHalfsCrossover::default())),
"ArithmeticAverage" => Ok(Box::new(ArithmeticAverageCrossover::default())),
"LeftOrRight" => Ok(Box::<LeftOrRightCloneCrossover>::default()),
"EqualHalfs" => Ok(Box::<EqualHalfsCrossover>::default()),
"ArithmeticAverage" => Ok(Box::<ArithmeticAverageCrossover>::default()),
_ => Err(Error::msg("Unknown crossover function.")),
}
}
Expand Down
13 changes: 3 additions & 10 deletions src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,8 @@ fn get_first_generation(
image_height: usize,
image_width: usize,
) -> Vec<(Image, usize)> {
let mut vec = Vec::with_capacity(vec_len);

let pixel = Pixel::white();

vec.resize_with(vec_len, || {
(Image::blank(image_height, image_width, &pixel), usize::MAX)
});

vec
vec![(Image::blank(image_height, image_width, &pixel), usize::MAX); vec_len]
}

pub struct Environment {
Expand Down Expand Up @@ -107,13 +100,13 @@ impl Environment {
context.get_mutator().mutate_rgb(&mut entry.0);
entry.1 = context
.get_fitness()
.calculate_fitness_rgb(&*context.get_image(), &entry.0);
.calculate_fitness_rgb(context.get_image(), &entry.0);
}
ColorMode::Grayscale => {
context.get_mutator().mutate_grayscale(&mut entry.0);
entry.1 = context
.get_fitness()
.calculate_fitness_grayscale(&*context.get_image(), &entry.0);
.calculate_fitness_grayscale(context.get_image(), &entry.0);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/environment_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ impl Default for EnvironmentBuilder {
Self {
image: None,
color_mode: ColorMode::Rgb,
mutator: Box::new(RectangleMutator::default()),
fitness: Box::new(SquareDistance::default()),
crossover: Box::new(EqualHalfsCrossover::default()),
mutator: Box::<RectangleMutator>::default(),
fitness: Box::<SquareDistance>::default(),
crossover: Box::<EqualHalfsCrossover>::default(),
generation_size: 100,
threads: 1,
display_condition: DisplayCondition::None,
Expand Down
8 changes: 4 additions & 4 deletions src/fitness/absolute_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ impl FitnessFunction for AbsoluteDistance {
let diff_g = isize::from(p1.get_g()) - isize::from(p2.get_g());
let diff_b = isize::from(p1.get_b()) - isize::from(p2.get_b());

sum += diff_r.abs() as usize;
sum += diff_g.abs() as usize;
sum += diff_b.abs() as usize;
sum += diff_r.unsigned_abs();
sum += diff_g.unsigned_abs();
sum += diff_b.unsigned_abs();

sum
};
Expand All @@ -37,7 +37,7 @@ impl FitnessFunction for AbsoluteDistance {
let diff_red = isize::from(p1.get_r()) - isize::from(p2.get_r());
let diff_grayscale = diff_red * 3;

sum += diff_grayscale.abs() as usize;
sum += diff_grayscale.unsigned_abs();

sum
};
Expand Down
12 changes: 2 additions & 10 deletions src/models/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,9 @@ impl Image {
#[must_use]
pub fn blank(height: usize, width: usize, pixel: &Pixel) -> Self {
let size = height * width;
let mut pixels = Vec::with_capacity(size as usize);
let pixels = vec![pixel.clone(); size];

for _ in 0..size {
pixels.push(pixel.clone());
}

Self {
height,
width,
pixels,
}
Self::new(height, width, pixels)
}

pub fn width(&self) -> usize {
Expand Down
17 changes: 8 additions & 9 deletions src/mutators/circle_mutator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use num_integer::Roots;

use crate::{util::Random, models::Image};
use crate::{models::Image, util::Random};

use super::Mutator;

Expand All @@ -19,18 +19,17 @@ impl CircleMutator {
let image_width = image.width() as i64;
let image_height = image.height() as i64;

let x = random.get_random(1, image_width) as i64;
let y = random.get_random(1, image_height) as i64;
let x = random.get_random(1, image_width);
let y = random.get_random(1, image_height);

let n = *[x, y, image_width - x, image_height - y].iter().min().unwrap();
let n = *[x, y, image_width - x, image_height - y]
.iter()
.min()
.unwrap();

let r = random.get_random(1, n + 1);

RandomCircle {
x,
y,
r,
}
RandomCircle { x, y, r }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/mutators/rectangle_mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl RectangleMutator {
let image_width = image.width();
let image_height = image.height();

let x = random.get_random(0usize, image_width - 1);
let y = random.get_random(0usize, image_height - 1);
let x = random.get_random(0usize, image_width);
let y = random.get_random(0usize, image_height);

let width = random.get_random(0usize, image_width - x) + 1;
let height = random.get_random(0usize, image_height - y) + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/mutators/triangle_mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl TriangleMutator {
let mut xf = vertices[0].x as f64;
let mut xt = vertices[0].x as f64 + dx_upper;

let mut y = vertices[0].y as isize;
let mut y = vertices[0].y;
while y
<= if vertices[2].y > image_height - 1 {
image_height - 1
Expand Down

0 comments on commit edaa831

Please sign in to comment.