dcc-lsystem 0.6.3

An implementation of a Lindenmayer system together with some rendering tools
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use std::cmp::{max, min};
use std::collections::HashMap;
use std::f32::consts::FRAC_PI_2;

use rand::Rng;
use regex::Regex;

use dcc_lsystem_derive::TurtleContainer;
use lazy_static::lazy_static;

use crate::renderer::TurtleRenderer;
use crate::{ArenaId, LSystem, LSystemBuilder};

/// A simple trait for an integer-valued Turtle.
///
/// Any implementation of this trait should contain a `BaseTurtle` struct which
/// is referred to by the `inner` and `inner_mut` methods.  This BaseTurtle deals
/// with storing the turtle's current position, and drawing lines as appropriate.
///
/// The real meat and potatoes of this trait is the `forward` method, which is
/// how someone would actually move your turtle.  Your implementation should be responsible
/// for keeping track of the turtle's heading, and `forward` should move your turtle
/// in that direction (using `self.inner_mut().delta_move(dx, dy)`).
///
/// # Example
/// The following `DumbTurtle` only moves to the right.
///
/// ```rust
/// use dcc_lsystem::turtle::{BaseTurtle, MovingTurtle};
///
/// struct DumbTurtle {
///     inner: BaseTurtle,
/// }
///
/// impl MovingTurtle for DumbTurtle {
///     type Item = i32;
///
///     fn inner(&self) -> &BaseTurtle {
///         &self.inner
///     }
///
///     fn inner_mut(&mut self) -> &mut BaseTurtle {
///         &mut self.inner
///     }
///
///     fn forward(&mut self, distance: i32) {
///         self.inner_mut().delta_move(distance, 0);
///     }
/// }
/// ```
///
pub trait MovingTurtle {
    type Item;

    /// Returns a reference to the wrapped `BaseTurtle`.
    fn inner(&self) -> &BaseTurtle;

    /// Returns a mutable reference to the wrapped `BaseTurtle`.
    fn inner_mut(&mut self) -> &mut BaseTurtle;

    /// Moves the turtle forward by `distance`.
    fn forward(&mut self, distance: Self::Item);
}

/// This trait indicates that the implementor contains a turtle for us to play with.
///
/// It's a bit annoying to have to implement this trait everywhere, so using the `dcc-lsystem-derive`
/// crate you can do the following:
///
/// ```rust
/// use dcc_lsystem::turtle::{SimpleTurtle, TurtleContainer};
/// use dcc_lsystem_derive::TurtleContainer;
///
/// #[derive(TurtleContainer)]
/// struct BasicContainer {
///     #[turtle]
///     inner : SimpleTurtle,
///
///     /* <----- some other fields ----- */
/// }
/// ```
///
/// which is roughly equivalent to the following:
///
/// ```rust
/// use dcc_lsystem::turtle::{SimpleTurtle, TurtleContainer, MovingTurtle};
///
/// struct BasicContainer {
///     inner : SimpleTurtle,
///
///     /* <----- some other fields ----- */
/// }
///
/// impl TurtleContainer for BasicContainer {
///     type Item = <SimpleTurtle as MovingTurtle>::Item;
///
///     fn inner(&self) -> &MovingTurtle<Item = Self::Item> {
///         &self.inner
///     }
/// }
///```
pub trait TurtleContainer {
    type Item;

    fn inner(&self) -> &dyn MovingTurtle<Item = Self::Item>;
}

/// Every turtle contains a turtle.
impl<T> TurtleContainer for dyn MovingTurtle<Item = T> {
    type Item = T;

    fn inner(&self) -> &dyn MovingTurtle<Item = Self::Item> {
        self
    }
}

pub trait Stack: MovingTurtle {
    /// Push the current state of this turtle onto a stack.
    fn push(&mut self);

    /// Pop the current state of this turtle onto a stack.
    fn pop(&mut self);
}

#[derive(Clone, Debug)]
pub struct BaseTurtle {
    x: i32,
    y: i32,
    lines: Vec<(i32, i32, i32, i32)>,
    max_x: i32,
    max_y: i32,
    min_x: i32,
    min_y: i32,
    pen_down: bool,
}

impl BaseTurtle {
    /// Creates a new `BaseTurtle` instance.
    pub fn new() -> Self {
        Self {
            x: 0,
            y: 0,
            lines: Vec::new(),
            max_x: 0,
            max_y: 0,
            min_x: 0,
            min_y: 0,
            pen_down: true,
        }
    }

    /// Returns the current `x` coordinate of the turtle.
    pub fn x(&self) -> i32 {
        self.x
    }

    /// Returns the current `y` coordinate of the turtle.
    pub fn y(&self) -> i32 {
        self.y
    }

    /// Returns a slice containing all the lines `(x1, y1, x2, y2)` traversed by the turtle.
    pub fn lines(&self) -> &[(i32, i32, i32, i32)] {
        &self.lines
    }

    /// Set the current position of this turtle to `(x,y)`.
    pub fn set_position(&mut self, x: i32, y: i32) {
        self.x = x;
        self.y = y;
        self.update_bounds();
    }

    fn update_bounds(&mut self) {
        self.min_x = min(self.min_x, self.x);
        self.min_y = min(self.min_y, self.y);
        self.max_x = max(self.max_x, self.x);
        self.max_y = max(self.max_y, self.y);
    }

    /// Moves the turtle by `(dx,dy)`.
    pub fn delta_move(&mut self, dx: i32, dy: i32) {
        let x2 = self.x + dx;
        let y2 = self.y + dy;

        if self.pen_down {
            self.lines.push((self.x, self.y, x2, y2));
        }

        self.x = x2;
        self.y = y2;

        self.update_bounds();
    }

    /// Returns `(total_width, total_height, min_x, min_y)`, where
    /// `total_width` (respectively `total_height) is the largest horizontal (respectively vertical) distance between any two points
    /// that the turtle visited, `min_x` (respectively `min_y`) is the smallest horizontal (respectively vertical) position that
    /// the turtle visited.
    ///
    /// This is useful for converting from turtle coordinates to a new coordinate system starting at `(0,0)`
    /// with width `total_width`, height `total_height`, and all positions have positive `x` and `y` coordinates.
    pub fn bounds(&self) -> (u32, u32, i32, i32) {
        (
            (self.max_x + self.min_x.abs()) as u32,
            (self.max_y + self.min_y.abs()) as u32,
            self.min_x,
            self.min_y,
        )
    }

    /// Puts the turtles pen down.
    pub fn pen_down(&mut self) {
        self.pen_down = true;
    }

    /// Pulls the turtles pen up.
    pub fn pen_up(&mut self) {
        self.pen_down = false;
    }
}

impl Default for BaseTurtle {
    fn default() -> Self {
        Self::new()
    }
}

/// Represents the cardinal directions.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Heading {
    North,
    South,
    East,
    West,
}

impl Heading {
    /// Returns the `Heading` that is 90 degrees left of this one.
    pub fn left(self) -> Self {
        match self {
            Heading::North => Heading::West,
            Heading::West => Heading::South,
            Heading::South => Heading::East,
            Heading::East => Heading::North,
        }
    }

    /// Returns the `Heading` that is 90 degrees right of this one.
    pub fn right(self) -> Self {
        // Don't judge me...
        self.left().left().left()
    }

    pub fn dx(self) -> i32 {
        match self {
            Heading::West => -1,
            Heading::East => 1,
            _ => 0,
        }
    }

    pub fn dy(self) -> i32 {
        match self {
            Heading::North => 1,
            Heading::South => -1,
            _ => 0,
        }
    }
}

/// A simple turtle implementation.  It has the following features:
///
/// * You can change direction! (`turtle.set_heading(f32)`, `turtle.left(f32)`, and `turtle.right(f32)`)
/// * You can make it move! (`turtle.forward(i32)`)
/// * Stacks! (`turtle.push()` and `turtle.pop()`)
///
/// and some other features.
#[derive(Clone, Debug)]
pub struct SimpleTurtle {
    turtle: BaseTurtle,
    heading: f32,
    stack: Vec<(i32, i32, f32)>,
    pen_down: bool,
}

impl SimpleTurtle {
    /// Return a new `StackTurtle` instance.
    pub fn new() -> Self {
        Self {
            turtle: BaseTurtle::new(),
            heading: FRAC_PI_2,
            stack: Vec::new(),
            pen_down: true,
        }
    }

    /// Turns the turtle left by the given angle (in radians).
    pub fn left(&mut self, angle: f32) {
        self.heading += angle;
    }

    /// Turns the turtle right by the given angle (in radians).
    pub fn right(&mut self, angle: f32) {
        self.heading -= angle;
    }

    /// Set the current heading of the turtle (in radians).
    pub fn set_heading(&mut self, heading: f32) {
        self.heading = heading;
    }
}

impl Stack for SimpleTurtle {
    /// Pushes the current position and heading of the turtle onto the stack.
    fn push(&mut self) {
        self.stack
            .push((self.turtle.x(), self.turtle.y(), self.heading));
    }

    /// Pops the position and heading off the stack.
    fn pop(&mut self) {
        let (x, y, heading) = self.stack.pop().expect("Called pop on empty stack");

        self.turtle.set_position(x, y);
        self.heading = heading;
    }
}

impl MovingTurtle for SimpleTurtle {
    type Item = i32;

    fn inner(&self) -> &BaseTurtle {
        &self.turtle
    }

    fn inner_mut(&mut self) -> &mut BaseTurtle {
        &mut self.turtle
    }

    fn forward(&mut self, distance: i32) {
        let dx = self.heading.cos() * distance as f32;
        let dy = self.heading.sin() * distance as f32;

        if self.pen_down {
            self.turtle.delta_move(dx as i32, dy as i32);
        }
    }
}

impl Default for SimpleTurtle {
    fn default() -> Self {
        Self::new()
    }
}

/// The state modified by a `TurtleLSystemRenderer`.  Each `TurtleAction` corresponds
/// to a modifier of the form `Fn(&mut TurtleLSystemState)`.
#[derive(TurtleContainer)]
pub struct TurtleLSystemState {
    angle: i32,
    angle_stack: Vec<i32>,

    #[turtle]
    turtle: SimpleTurtle,
}

impl TurtleLSystemState {
    /// Create a new state.
    pub fn new() -> Self {
        Self {
            angle: 0,
            angle_stack: Vec::new(),
            turtle: SimpleTurtle::new(),
        }
    }
}

impl Default for TurtleLSystemState {
    fn default() -> Self {
        Self::new()
    }
}

/// A `TurtleLSystemBuilder` is used to generate an L-system and a turtle
/// based renderer based don this L-system.
#[derive(Clone)]
pub struct TurtleLSystemBuilder {
    builder: LSystemBuilder,
    actions: HashMap<ArenaId, TurtleAction>,
    tokens: HashMap<String, ArenaId>,
    global_rotate: i32,
}

impl TurtleLSystemBuilder {
    /// Create a new `TurtleLSystemBuilder` instance.
    pub fn new() -> Self {
        Self {
            builder: LSystemBuilder::new(),
            actions: HashMap::new(),
            tokens: HashMap::new(),
            global_rotate: 0,
        }
    }

    /// Apply a global rotation to the builder.  This is useful for modifying the orientation
    /// of the data passed to a `Renderer`.
    pub fn rotate(&mut self, angle: i32) -> &mut Self {
        self.global_rotate = angle;

        self
    }

    /// Associate a token and corresponding action to this builder.
    pub fn token<S: Into<String>>(&mut self, token: S, action: TurtleAction) -> &mut Self {
        let ident = token.into();

        let token = self.builder.token(ident.clone());

        self.tokens.insert(ident, token);
        self.actions.insert(token, action);

        self
    }

    /// Set the axiom  for this builder.
    pub fn axiom(&mut self, ident: &str) -> &mut Self {
        let mut axiom = Vec::new();

        for part in ident.split_whitespace() {
            let token = self.get_token(part).expect("Invalid axiom");

            axiom.push(token);
        }

        assert_ne!(axiom.len(), 0);

        self.builder.axiom(axiom);

        self
    }

    fn get_token(&self, token: &str) -> Option<ArenaId> {
        self.tokens.get(token).cloned()
    }

    /// Add a transformation rule to the builder.
    pub fn rule<'a, S: Into<&'a str>>(&mut self, rule: S) -> &mut Self {
        let rule = rule.into();

        lazy_static! {
            static ref RE: Regex = Regex::new(r"\s*(\w)\s*=>\s*((?:\s*\S+\s*)*)\s*").unwrap();
        }

        let cap = RE.captures(rule).expect("Invalid rule");

        // The LHS of our rule
        let lhs = self
            .get_token(&cap[1])
            .unwrap_or_else(|| panic!("Invalid token: {}", &cap[1]));

        // Construct the RHS of our rule
        let mut rule = Vec::new();

        for token in cap[2].split_whitespace() {
            let token = self
                .get_token(token)
                .unwrap_or_else(|| panic!("Invalid token: {}", token));

            rule.push(token);
        }

        // Add the rule to our builder
        self.builder.transformation_rule(lhs, rule);

        self
    }

    /// Consumes the builder, returning the generated `LSystem` and a `Renderer`
    /// which can associate tokens in the `LSystem` to turtle actions.
    pub fn finish(self) -> (LSystem, TurtleRenderer<TurtleLSystemState>) {
        let mut renderer = TurtleRenderer::new(TurtleLSystemState::new());

        // Register the processing functions for each action
        for (id, action) in self.actions.into_iter() {
            match action {
                TurtleAction::Push => {
                    renderer.register(id, |state| {
                        state.turtle.push();
                        state.angle_stack.push(state.angle);
                    });
                }
                TurtleAction::Pop => {
                    renderer.register(id, |state| {
                        state.turtle.pop();
                        state.angle = state.angle_stack.pop().expect("Popped with empty stack");
                    });
                }
                TurtleAction::Forward(distance) => {
                    let current_global_rotate = self.global_rotate;

                    renderer.register(id, move |state| {
                        state.turtle.set_heading(
                            ((current_global_rotate + state.angle) as f32).to_radians(),
                        );
                        state.turtle.forward(distance);
                    });
                }
                TurtleAction::Rotate(angle) => {
                    renderer.register(id, move |state| {
                        state.angle = (state.angle + angle) % 360;
                    });
                }
                TurtleAction::StochasticRotate(distribution) => {
                    renderer.register(id, move |state| {
                        state.angle = (state.angle + distribution.sample()) % 360;
                    });
                }
                TurtleAction::StochasticForward(distribution) => {
                    let current_global_rotate = self.global_rotate;

                    renderer.register(id, move |state| {
                        state.turtle.set_heading(
                            ((current_global_rotate + state.angle) as f32).to_radians(),
                        );
                        state.turtle.forward(distribution.sample());
                    });
                }
                TurtleAction::Nothing => {}
            }
        }

        (self.builder.finish(), renderer)
    }
}

impl Default for TurtleLSystemBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// An integer-valued probability distribution.
///
/// We need to be able to clone Box<dyn Distribution>, so we use the wonderful
/// `dyn_clone` crate to allow for this.
pub trait Distribution: dyn_clone::DynClone {
    /// Take a sample from this distribution.
    fn sample(&self) -> i32;
}

dyn_clone::clone_trait_object!(Distribution);

/// A uniform distribution on a closed interval.
#[derive(Clone)]
pub struct Uniform {
    lower: i32,
    upper: i32,
}

impl Uniform {
    /// Creates a new uniform distribution on the interval [lower, upper].
    ///
    /// # Panics
    /// Will panic is `lower` > `upper`
    pub fn new(lower: i32, upper: i32) -> Self {
        assert!(lower <= upper);
        Self { lower, upper }
    }
}

impl Distribution for Uniform {
    fn sample(&self) -> i32 {
        let mut rng = rand::thread_rng();
        rng.gen_range(self.lower..=self.upper)
    }
}

/// A constant distribution
impl Distribution for i32 {
    fn sample(&self) -> i32 {
        *self
    }
}

/// The possible actions we can associate to tokens in our `LSystem`.
#[derive(Clone)]
pub enum TurtleAction {
    Nothing,
    Rotate(i32),
    Forward(i32),
    StochasticRotate(Box<dyn Distribution>),
    StochasticForward(Box<dyn Distribution>),
    Push,
    Pop,
}