barnsley 0.2.3

iterated function system image generator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! functions used in the IFS
//!
//! # Adding a new transform
//!
//! It's recommended to look at a simple transform's implementation before adding a new one.
//! For example, look at `LinearTransform` to understand what each part does.
//!
//! 1. Create a struct to store the transforms parameters. It should have a `base_color` and `weight` too.
//! 2. Derive `Serialize, Deserialize, Copy, Clone, Debug` for the new transform struct.
//! 3. Implement the `transform` trait for that struct.
//! 4. Add the transform to the `Transform` enum.

use crate::util::*;
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;

/// Use to map a point (x,y) to image space.
pub fn final_transform(x: f32, y: f32) -> (f32, f32) {
    let a = 0.5;
    let b = 0.0;
    let c = 0.0;
    let d = 1.0;

    let z = Complex32::new(x, y);
    let z2 = (a * z + b) / (c * z + d);
    (z2.re, z2.im)
}

#[enum_dispatch(Transformable)]
#[derive(Serialize, Deserialize, Copy, Clone, EnumIter, PartialEq)]
pub enum Transform {
    LinearTransform,
    AffineTransform,
    InverseJuliaTransform,
    MoebiusTransform
}

impl Transform {
    pub fn morph(&self, other: Transform, pct: f32) -> Transform {
        match (self, other) {
            (Transform::LinearTransform(t), Transform::LinearTransform(o)) => t.morph(&o, pct).into(),
            (Transform::MoebiusTransform(t), Transform::MoebiusTransform(o)) => t.morph(&o, pct).into(),
            (Transform::AffineTransform(t), Transform::AffineTransform(o)) => t.morph(&o, pct).into(),
            (Transform::InverseJuliaTransform(t), Transform::InverseJuliaTransform(o)) => t.morph(&o, pct).into(),
            _ => 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
#[enum_dispatch]
pub trait Transformable {
    /// Gets the transforms base color, i.e. the color of the transform that gets repeatedly mixed
    fn get_base_color(&self) -> Color;

    /// Transform a color using the `base_color` and the `current_color`.
    fn transform_color(&self, current_color: Color) -> Color {
        let base_color = self.get_base_color();
        Color {
            r: (base_color.r + current_color.r) / 2.0,
            g: (base_color.g + current_color.g) / 2.0,
            b: (base_color.b + current_color.b) / 2.0,      
        }
    }

    /// Applies the transformation to a point
    fn transform_point(&self, point: Point) -> Point;

    /// Retreives the transforms weight
    fn get_weight(&self) -> f32;

    /// Retrieves the name of the transformed
    fn get_name(&self) -> String;
}

pub trait Morphable<T: Transformable + ?Sized> {
    fn morph(&self, other: Box<&T>, pct: f32) -> Box<T>;
}

pub fn transform_from_str(name: String) -> Transform {
    match name.as_str() {
        "LinearTransform" => LinearTransform::random().into(),
        "AffineTransform" => AffineTransform::random().into(),
        "MoebiusTransform" => MoebiusTransform::random().into(),
        "InverseJuliaTransform" => InverseJuliaTransform::random().into(),
        _ => panic!("Not a supported transform kind")
    }
}

// LINEAR TRANSFORM
/// LinearTransform defined by the matrix:
/// [a b]
/// [c d]
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
pub struct LinearTransform {
    pub a: f32,
    pub b: f32,
    pub c: f32,
    pub d: f32,
    pub base_color: Color,
    pub weight: f32,
}

impl LinearTransform {
    pub fn new(a: f32, b: f32, c: f32, d: f32, base_color: Color, weight: f32) -> LinearTransform {
        LinearTransform {
            a,
            b,
            c,
            d,
            base_color,
            weight,
        }
    }

    pub fn random() -> LinearTransform {
        let mut rng = rand::thread_rng();
        let a: f32 = rng.gen::<f32>() * 2. - 1.;
        let b: f32 = rng.gen::<f32>() * 2. - 1.;
        let c: f32 = rng.gen::<f32>() * 2. - 1.;
        let d: f32 = rng.gen::<f32>() * 2. - 1.;
        let weight: f32 = rng.gen::<f32>();
        LinearTransform {
            a,
            b,
            c,
            d,
            base_color: Color::random(),
            weight,
        }
    }

    fn morph(&self, other:&Self, pct: f32) -> Self{
       LinearTransform::new(
                    lerp_f32(self.a, other.a, pct),
                    lerp_f32(self.b, other.b, pct),
                    lerp_f32(self.c, other.c, pct),
                    lerp_f32(self.d, other.d, pct),
                    lerp_color(self.base_color, other.base_color, pct),
                    lerp_f32(self.weight, other.weight, pct))
    }
}

impl Default for LinearTransform {
    fn default() -> Self {
        LinearTransform::random()
    }
}

impl Transformable for LinearTransform {
    fn transform_point(&self, point: Point) -> Point {
        Point {
            x: self.a * point.x + self.b * point.y,
            y: self.c * point.x + self.d * point.y,
        }
    }

    fn get_weight(&self) -> f32 {
        self.weight
    }

    fn get_base_color(&self) -> Color {
        self.base_color
    }

    fn get_name(&self) -> String {
        "LinearTransform".to_string()
    }
}
impl Morphable<LinearTransform> for LinearTransform {
    fn morph(&self, other: Box<&Self>, pct: f32) -> Box<LinearTransform> where Self: Sized {
        Box::new(LinearTransform::new(
                    lerp_f32(self.a, other.a, pct),
                    lerp_f32(self.b, other.b, pct),
                    lerp_f32(self.c, other.c, pct),
                    lerp_f32(self.d, other.d, pct),
                    lerp_color(self.base_color, other.base_color, pct),
                    lerp_f32(self.weight, other.weight, pct)))
    }

}

// AFFINE TRANSFORM
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
pub struct AffineTransform {
    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 {
    pub fn new(
        a: f32,
        b: f32,
        c: f32,
        d: f32,
        xshift: f32,
        yshift: f32,
        base_color: Color,
        weight: f32,
    ) -> AffineTransform {
        AffineTransform {
            a,
            b,
            c,
            d,
            xshift,
            yshift,
            base_color,
            weight,
        }
    }

    pub fn random() -> AffineTransform {
        let mut rng = rand::thread_rng();
        let a: f32 = rng.gen::<f32>() * 2. - 1.;
        let b: f32 = rng.gen::<f32>() * 2. - 1.;
        let c: f32 = rng.gen::<f32>() * 2. - 1.;
        let d: f32 = rng.gen::<f32>() * 2. - 1.;
        let xshift: f32 = rng.gen::<f32>() * 4. - 2.;
        let yshift: f32 = rng.gen::<f32>() * 4. - 2.;

        let normal: Normal<f64> = Normal::new(1.0, 0.15).unwrap();
        let weight: f32 = normal.sample(&mut rng) as f32;

        AffineTransform {
            a,
            b,
            c,
            d,
            xshift,
            yshift,
            base_color: Color::random(),
            weight,
        }
    }

   fn morph(&self, other: &Self, pct: f32) -> Self {
       AffineTransform::new(
                    lerp_f32(self.a, other.a, pct),
                    lerp_f32(self.b, other.b, pct),
                    lerp_f32(self.c, other.c, pct),
                    lerp_f32(self.d, other.d, pct),
                    lerp_f32(self.xshift, other.xshift, pct),
                    lerp_f32(self.yshift, other.yshift, pct),
                    lerp_color(self.base_color, other.base_color, pct),
                    lerp_f32(self.weight, other.weight, pct))
    }


}

impl Default for AffineTransform {
    fn default() -> Self {
        AffineTransform::random()
    }
}

impl Transformable for AffineTransform {
    fn transform_point(&self, point: Point) -> Point {
        Point {
            x: self.a * point.x + self.b * point.y + self.xshift,
            y: self.c * point.x + self.d * point.y + self.yshift,
        }
    }

    fn get_weight(&self) -> f32 {
        self.weight
    }

    fn get_base_color(&self) -> Color {
        self.base_color
    }

    fn get_name(&self) -> String {
        "AffineTransform".to_string()
    }
}

impl Morphable<AffineTransform> for AffineTransform {
    fn morph(&self, other: Box<&Self>, pct: f32) -> Box<Self> where Self: Sized {
        Box::new(AffineTransform::new(
                    lerp_f32(self.a, other.a, pct),
                    lerp_f32(self.b, other.b, pct),
                    lerp_f32(self.c, other.c, pct),
                    lerp_f32(self.d, other.d, pct),
                    lerp_f32(self.xshift, other.xshift, pct),
                    lerp_f32(self.yshift, other.yshift, pct),
                    lerp_color(self.base_color, other.base_color, pct),
                    lerp_f32(self.weight, other.weight, pct)))
    }
}

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

impl MoebiusTransform {
    pub fn new(
        a: Complex32,
        b: Complex32,
        c: Complex32,
        d: Complex32,
        base_color: Color,
        weight: f32,
    ) -> MoebiusTransform {
        MoebiusTransform {
            a,
            b,
            c,
            d,
            base_color,
            weight,
        }
    }

    pub fn random() -> MoebiusTransform {
        let a: Complex32 = random_complex_number();
        let b: Complex32 = random_complex_number();
        let c: Complex32 = random_complex_number();
        let d: Complex32 = random_complex_number();

        let mut rng = rand::thread_rng();

        let normal: Normal<f64> = Normal::new(1.0, 0.15).unwrap();
        let weight: f32 = normal.sample(&mut rng) as f32;

        MoebiusTransform {
            a,
            b,
            c,
            d,
            base_color: Color::random(),
            weight,
        }
    }

    fn morph(&self, other: &Self, pct: f32) -> Self{
       MoebiusTransform::new(
                    lerp_complex32(self.a, other.a, pct),
                    lerp_complex32(self.b, other.b, pct),
                    lerp_complex32(self.c, other.c, pct),
                    lerp_complex32(self.d, other.d, pct),
                    lerp_color(self.base_color, other.base_color, pct),
                    lerp_f32(self.weight, other.weight, pct))
    }
}

impl Default for MoebiusTransform {
    fn default() -> Self {
        MoebiusTransform::random()
    }
}

impl Transformable for MoebiusTransform {
    fn transform_point(&self, point: Point) -> Point {
        let z = Complex32 {
            re: point.x,
            im: point.y,
        };
        let z2 = (self.a * z + self.b) / (self.c * z + self.d);
        Point { x: z2.re, y: z2.im }
    }

    fn get_weight(&self) -> f32 {
        self.weight
    }

    fn get_base_color(&self) -> Color {
        self.base_color
    }

    fn get_name(&self) -> String {
        "MoebiusTransform".to_string()
    }
}

impl Morphable<MoebiusTransform> for MoebiusTransform {
    fn morph(&self, other: Box<&Self>, pct: f32) -> Box<Self> where Self: Sized {
        Box::new(MoebiusTransform::new(
                    lerp_complex32(self.a, other.a, pct),
                    lerp_complex32(self.b, other.b, pct),
                    lerp_complex32(self.c, other.c, pct),
                    lerp_complex32(self.d, other.d, pct),
                    lerp_color(self.base_color, other.base_color, pct),
                    lerp_f32(self.weight, other.weight, pct)))
    }
}

// INVERSE JULIA TRANSFORM

#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
pub struct InverseJuliaTransform {
    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 {
        InverseJuliaTransform {
            r,
            theta,
            base_color,
            weight,
        }
    }

    pub fn random() -> InverseJuliaTransform {
        let mut rng = rand::thread_rng();

        let r: f32 = rng.gen::<f32>().sqrt() * 0.4 + 0.8;
        let theta: f32 = 2.0 * PI * rng.gen::<f32>();

        let normal: Normal<f64> = Normal::new(1.0, 0.15).unwrap();
        let weight: f32 = normal.sample(&mut rng) as f32;

        InverseJuliaTransform::new(r, theta, Color::random(), weight)
    }

    fn morph(&self, other: &Self, pct: f32) -> Self {
        InverseJuliaTransform::new(
                    lerp_f32(self.r, other.r, pct),
                    lerp_f32(self.theta, other.theta, pct),
                    lerp_color(self.base_color, other.base_color, pct),
                    lerp_f32(self.weight, other.weight, pct))
    }
}


impl Default for InverseJuliaTransform {
    fn default() -> Self {
        InverseJuliaTransform::random()
    }
}

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 = 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));
        Point {
            x: sqrt_r * new_theta.cos(),
            y: sqrt_r * new_theta.sin(),
        }
    }

    fn get_weight(&self) -> f32 {
        self.weight
    }

    fn get_base_color(&self) -> Color {
        self.base_color
    }

    fn get_name(&self) -> String {
        "InverseJuliaTransform".to_string()
    }
}

impl Morphable<InverseJuliaTransform> for InverseJuliaTransform {
    fn morph(&self, other: Box<&Self>, pct: f32) -> Box<Self> {
        Box::new(InverseJuliaTransform::new(
                    lerp_f32(self.r, other.r, pct),
                    lerp_f32(self.theta, other.theta, pct),
                    lerp_color(self.base_color, other.base_color, pct),
                    lerp_f32(self.weight, other.weight, pct)))
    }
}