Skip to content

Commit

Permalink
Merge pull request #27 from jmbhughes/initial-gui
Browse files Browse the repository at this point in the history
Prepares for initial version of barnsley_gui
  • Loading branch information
jmbhughes authored Oct 27, 2023
2 parents 3dde82f + 0013b8f commit d8f9518
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "barnsley"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Marcus Hughes <hughes.jmb@gmail.com>"]
license = "MIT OR Apache-2.0"
Expand All @@ -26,4 +26,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.104"
strum = "0.25.0"
strum_macros = "0.25.1"
toml = "0.8.0"
toml = "0.8.6"
10 changes: 9 additions & 1 deletion src/ifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::image::*;
/// Iterated function system
pub struct IFS {
/// transforms used in the iterated function system
transforms: Vec<Transform>,
pub transforms: Vec<Transform>,
/// the number of transforms in the IFS, stored for efficiency
num_transforms: usize,
/// the total weight of all the transforms in the IFS, stored for efficiency
Expand Down Expand Up @@ -72,6 +72,14 @@ impl IFS{
self.distribution = WeightedIndex::new(self.transforms.iter().map(|t| t.get_weight())).unwrap();
}

pub fn delete_transform(&mut self, index: usize) {
let transform = self.transforms.get(index).unwrap();
self.total_weight -= transform.get_weight();
self.num_transforms -= 1;
self.transforms.remove(index);
self.distribution = WeightedIndex::new(self.transforms.iter().map(|t| t.get_weight())).unwrap();
}

/// Select a transform at random according to the weighting
fn choose_transform(&self) -> &Transform {
let mut rng = thread_rng();
Expand Down
75 changes: 42 additions & 33 deletions src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use num::complex::{Complex, Complex32};
use rand::prelude::*;
use rand_distr::{Distribution, Normal};
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;
use std::default::Default;
use std::f32::consts::PI;
use enum_dispatch::enum_dispatch;
Expand All @@ -32,7 +33,7 @@ pub fn final_transform(x: f32, y: f32) -> (f32, f32) {
}

#[enum_dispatch(Transformable)]
#[derive(Serialize, Deserialize, Copy, Clone)]
#[derive(Serialize, Deserialize, Copy, Clone, EnumIter, PartialEq)]
pub enum Transform {
LinearTransform,
AffineTransform,
Expand All @@ -50,6 +51,15 @@ impl Transform {
_ => panic!("self and other must be the same transform type")
}
}

pub fn index(&self) -> usize {
match self {
Transform::LinearTransform(_) => 0,
Transform::AffineTransform(_) => 1,
Transform::MoebiusTransform(_) => 2,
Transform::InverseJuliaTransform(_) => 3
}
}
}

/// All transforms must have this trait
Expand Down Expand Up @@ -96,14 +106,14 @@ pub fn transform_from_str(name: String) -> Transform {
/// LinearTransform defined by the matrix:
/// [a b]
/// [c d]
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
pub struct LinearTransform {
a: f32,
b: f32,
c: f32,
d: f32,
base_color: Color,
weight: f32,
pub a: f32,
pub b: f32,
pub c: f32,
pub d: f32,
pub base_color: Color,
pub weight: f32,
}

impl LinearTransform {
Expand Down Expand Up @@ -186,16 +196,16 @@ impl Morphable<LinearTransform> for LinearTransform {
}

// AFFINE TRANSFORM
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
pub struct AffineTransform {
a: f32,
b: f32,
c: f32,
d: f32,
xshift: f32,
yshift: f32,
base_color: Color,
weight: f32,
pub a: f32,
pub b: f32,
pub c: f32,
pub d: f32,
pub xshift: f32,
pub yshift: f32,
pub base_color: Color,
pub weight: f32,
}

impl AffineTransform {
Expand Down Expand Up @@ -302,14 +312,14 @@ impl Morphable<AffineTransform> for AffineTransform {
}

// MOEBIUS TRANSFORM
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
pub struct MoebiusTransform {
a: Complex<f32>,
b: Complex32,
c: Complex32,
d: Complex32,
base_color: Color,
weight: f32,
pub a: Complex<f32>,
pub b: Complex32,
pub c: Complex32,
pub d: Complex32,
pub base_color: Color,
pub weight: f32,
}

impl MoebiusTransform {
Expand Down Expand Up @@ -406,24 +416,21 @@ impl Morphable<MoebiusTransform> for MoebiusTransform {

// INVERSE JULIA TRANSFORM

#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
pub struct InverseJuliaTransform {
r: f32,
theta: f32,
base_color: Color,
weight: f32,
c: Complex32,
pub r: f32,
pub theta: f32,
pub base_color: Color,
pub weight: f32
}

impl InverseJuliaTransform {
pub fn new(r: f32, theta: f32, base_color: Color, weight: f32) -> InverseJuliaTransform {
let c = Complex32::new(r * theta.cos(), r * theta.sin());
InverseJuliaTransform {
r,
theta,
base_color,
weight,
c,
}
}

Expand Down Expand Up @@ -458,11 +465,13 @@ impl Default for InverseJuliaTransform {
impl Transformable for InverseJuliaTransform {

fn transform_point(&self, point: Point) -> Point {
let c = Complex32::new(self.r * self.theta.cos(), self.r * self.theta.sin());

let z = Complex32 {
re: point.x,
im: point.y,
};
let z2 = self.c - z;
let z2 = c - z;
let new_theta = z2.im.atan2(z2.re) * 0.5;
let sqrt_r = vec![1., -1.].choose(&mut rand::thread_rng()).unwrap()
* ((z2.im * z2.im + z2.re * z2.re).powf(0.25));
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn lerp_color(a: Color, b: Color, pct: f32) -> Color {
}

/// representation of an RGB color
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct Color {
pub r: f32,
pub g: f32,
Expand Down

0 comments on commit d8f9518

Please sign in to comment.