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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Lindenmayer System (L-system) with personal customizations

See also

    https://en.wikipedia.org/wiki/L-system

    https://onlinemathtools.com/l-system-generator
"""

import io
import math
from collections import deque
from bokeh.plotting import figure, show, output_file
from bokeh.io import export_png, export_svgs
from bokeh.io.export import get_screenshot_as_png
from loguru import logger
from scipy.spatial.transform import Rotation
import attrs as at
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objects as go
import PIL.Image as pimage


@at.define
class Config:  # pylint: disable=too-few-public-methods
    """
    Configuration of the important active characters
    """
    reserved: str = ':; '  # reserved characters

    color: str = '.'
    move_lifted_pen: str = 'UVW'
    move_angle_init: str = '_'
    move: str = 'ABCDEFGHIJKLNOQRST'  # M and P are reserved for 3d rotations

    move_up_3d: str = '⇧'
    move_down_3d: str = '⇩'

    r3d_1_plus: str = 'p'   # Axis of rotation : "X"
    r3d_1_minus: str = 'm'

    r3d_2_plus: str = 'P'   # Axis of rotation : "Y"
    r3d_2_minus: str = 'M'

    r3d_all: str = at.field(init=False)

    move_multi: str = at.field(init=False)
    move_all: str = at.field(init=False)

    delta_add: str = 'u'
    delta_sub: str = 'v'

    outer_repetition: str = '#'
    outer_repetition_max: int = 100

    skipped: str = ''
    total_skipped: str = at.field(init=False)  # all skipped characters

    def __attrs_post_init__(self):
        self.move_multi = self.move.lower()
        self.move_all = self.color + self.move_lifted_pen + self.move_angle_init + self.move_multi + self.move
        self.total_skipped = ' ' + self.skipped
        self.r3d_all = self.r3d_1_plus + self.r3d_1_minus + self.r3d_2_plus + self.r3d_2_minus


class Lsystc:
    """
    Classic L-System with few customizations
    """
    def __init__(self, config: Config, axiom: str, rules: list[tuple[str, str]], nbiter: int,
                 dev_ini: bool = True, verbose: bool = False) -> None:
        self.config = config
        self.axiom = axiom
        self.rules = rules
        self.nbiter = nbiter
        self.dev_ini = dev_ini
        self.verbose = verbose

        self.dev = ''
        self.turt = []

        self.dimension = 2

        self.rotation_3d_done = False
        self.mobile_vectors: list[np.array] = [np.array([1.0, 0.0, 0.0]),
                                               np.array([0.0, 1.0, 0.0]),
                                               np.array([0.0, 0.0, 1.0])]

        self.log('info', f"Axiom: {self.axiom:.50} ; Rules : {self.rules} ; Nb iterations : {self.nbiter}")

        if self.dev_ini:
            self.develop()
            self.log('info', f"Axiom: {self.axiom:.50} ; Rules : {self.rules} ; "
                             f"Nb iterations : {self.nbiter} Expanded value : {self.dev[:50]+'...'} ; After")

    @staticmethod
    def apply_rot(rot: Rotation, vec: np.array) -> np.array:
        """
        Apply a rotation on a vector

        :param rot:  rotation to apply
        :param vec:  concerned vector
        :return: rotated vector
        """
        return rot.apply(vec).round(decimals=6)

    @staticmethod
    def dev_unit(source: str, rules: list[tuple[str, str]]) -> str:
        """
        Develop source with rules
        """
        result = source
        position = 0
        lreg = None

        while True:
            # The leftmost usable rule is applied
            newpos = None
            for lr, regle in enumerate(rules):
                lpos = result.find(regle[0], position)
                if lpos >= 0:
                    if newpos is None or lpos < newpos:
                        newpos = lpos
                        lreg = lr

            if newpos is None:
                break

            result = result[0:newpos] + result[newpos:].replace(rules[lreg][0], rules[lreg][1], 1)
            position = newpos + len(rules[lreg][1])

        return result

    @staticmethod
    def color_from_map(name: str, index: int) -> tuple[int, int, int]:
        """
        :param name: name of the discrete colormap (matplotlib source) to be used
        :param index: index of the color in the map
        :return: tuple (red, green, blue)
        """
        r, g, b = mpl.colormaps[name].colors[index]
        r, g, b = int(r * 255), int(g * 255), int(b * 255)

        return r, g, b

    def log(self, ltype: str, message: str, *args, **kwargs):
        """
        Log a message with consideration for the verbosity property

        :param ltype: type of log
        :param message: message
        :return: None
        """
        if self.verbose:
            func_dict = {'info': logger.info, 'debug': logger.debug, 'warning': logger.warning,
                         'error': logger.error, 'exception': logger.exception}

            func = func_dict.get(ltype)
            if not func:
                raise ValueError(f"This type of log is unknown : {ltype}")

            func(message, *args, **kwargs)

    def new_pos(self, ax: float, ay: float, az: float, astep: float, aangle: float) -> tuple[float, float, float]:
        """
        New position from (ax, ay, az) with the use of astep and aangle

        The angle is not used if a 3D rotation has been done

        :param ax: 1st coordinate of starting point
        :param ay: 2nd coordinate of starting point
        :param az: 3rd coordinate of starting point
        :param astep: step size
        :param aangle: step angle
        """
        if self.rotation_3d_done:
            forward_vector = self.mobile_vectors[0]
            lx = ax + astep * forward_vector[0]
            ly = ay + astep * forward_vector[1]
            lz = az + astep * forward_vector[2]

            return lx, ly, lz
        else:
            if aangle == 0.0:
                lx = ax + astep
                ly = ay
            elif aangle == 90.0:
                lx = ax
                ly = ay + astep
            elif aangle == 180.0:
                lx = ax - astep
                ly = ay
            elif aangle == 270.0:
                lx = ax
                ly = ay - astep
            else:
                lx = ax + astep * math.cos(math.radians(aangle))
                ly = ay + astep * math.sin(math.radians(aangle))

            return lx, ly, az

    def develop(self) -> None:
        """
        Develop self.axiom from the list of rules (self.rules) with nbiter iterations

        A rule is a couple (source, target) where source can be replaced by target

        Example of Koch :
            axiom = 'F'
            rules = [('F','F+F-F-F+F')]

        Example with 2 rules :
            axiom = 'A'
            rules = [('A','AB'),('B','A')]
        """
        result = self.axiom

        if self.rules:
            for _ in range(self.nbiter):
                result = self.dev_unit(result, self.rules)

        self.dev = result

    def init_3d(self, angle: float) -> None:
        """
        Initialization of the 3D and of the mobile vectors

        :param angle: current angle
        :return: None
        """
        self.rotation_3d_done = True
        self.dimension = 3

        vec_x = self.mobile_vectors[0]
        vec_y = self.mobile_vectors[1]
        axis = self.mobile_vectors[2]

        rot = Rotation.from_rotvec(angle * axis, degrees=True)
        new_vec_x = self.apply_rot(rot, vec_x)
        new_vec_y = self.apply_rot(rot, vec_y)

        self.mobile_vectors[0] = new_vec_x
        self.mobile_vectors[1] = new_vec_y

    def rotate_3d(self, rtype: str, rangle: float) -> None:
        """
        Apply a 3D rotation on the mobile vectors

        :param rtype: type of rotation
        :param rangle: angle of rotation
        :return: None
        """
        vec_x = self.mobile_vectors[0]
        vec_y = self.mobile_vectors[1]
        vec_z = self.mobile_vectors[2]
        rsign = 1 if rtype in self.config.r3d_1_plus + self.config.r3d_2_plus + '+>' else -1
        if rtype in self.config.r3d_1_minus + self.config.r3d_1_plus:
            # Axis of rotation is "X"
            axis = vec_x
            rot = Rotation.from_rotvec(rsign * rangle * axis, degrees=True)
            new_vec_x = axis
            new_vec_y = self.apply_rot(rot, vec_y)
            new_vec_z = self.apply_rot(rot, vec_z)
        elif rtype in self.config.r3d_2_minus + self.config.r3d_2_plus:
            # Axis of rotation is "Y"
            axis = vec_y
            rot = Rotation.from_rotvec(rsign * rangle * axis, degrees=True)
            new_vec_x = self.apply_rot(rot, vec_x)
            new_vec_y = axis
            new_vec_z = self.apply_rot(rot, vec_z)
        else:
            # Axis of rotation is "Z" ( +->< )
            axis = vec_z
            rot = Rotation.from_rotvec(rsign * rangle * axis, degrees=True)
            new_vec_x = self.apply_rot(rot, vec_x)
            new_vec_y = self.apply_rot(rot, vec_y)
            new_vec_z = axis

        self.mobile_vectors[0] = new_vec_x
        self.mobile_vectors[1] = new_vec_y
        self.mobile_vectors[2] = new_vec_z

    def turtle(self, step: float = 10.0, angle: float = 90.0, angleinit: float = 0.0, coeff: float = 1.1,
               angle2: float = 10.0, color_length: int = 3, color_map: str = "Set1",
               delta: float = 0.1) -> None:
        """
        Develop self.dev in [(lx, ly, lz, color),...] where lx, ly, lz are lists of positions
        The result goes to self.turt

        :param step: the turtle step size
        :param angle: angle of rotation (in degrees)     ( + - )
        :param angleinit: initial angle
        :param coeff: magnification or reduction factor for the step ( * / ) and factor for "lowered" characters
        :param angle2: 2nd usable angle ( < > )
        :param color_length: maximal number of colours
        :param color_map: color map to use (matplotlib name)
        :param delta: value to add to the step
        """
        res = []
        stock: list = []  # List of ("point", angle, ...) kept for [] et ()

        lix: list[float] = []
        liy: list[float] = []
        liz: list[float] = []

        tx = 0.0
        ty = 0.0
        tz = 0.0
        tstep = step
        tangle = angleinit
        tsens = 1
        color_index = 0
        tcouleur = self.color_from_map(color_map, color_index)

        nb_iterations: int = 0
        stock_outer: deque = deque([(tx, ty, tz, tangle, tcouleur, tstep, self.mobile_vectors)])

        while stock_outer:
            nb_iterations += 1
            if len(lix) > 1:
                res.append((lix, liy, liz, tcouleur))

            tx, ty, tz, tangle, tcouleur, tstep, self.mobile_vectors = stock_outer.popleft()
            lix = [tx]
            liy = [ty]
            liz = [tz]

            for car in self.dev:

                if car in self.config.total_skipped:
                    continue

                npos = False
                npospos = False
                nliste = False
                ncolor = False

                if car in self.config.move_all:
                    if car in self.config.color:
                        ncolor = True  # Change of color
                    else:
                        ltstep = tstep

                        if car in self.config.move_multi:
                            ltstep = tstep * coeff
                        elif car in self.config.move_angle_init:
                            tangle = angleinit

                        ltangle = tangle

                        tx, ty, tz = self.new_pos(tx, ty, tz, ltstep, ltangle)

                    # npos true <-> new position with the pen down
                    npos = car in self.config.move + self.config.move_multi + self.config.move_angle_init

                    # nliste true <-> new list because of a change of color or a raised pen
                    nliste = car in self.config.color + self.config.move_lifted_pen
                elif car in self.config.move_up_3d or car in self.config.move_down_3d:
                    npos = True
                    self.dimension = 3
                    if car in self.config.move_up_3d:
                        tz += tstep
                    else:
                        tz -= tstep
                elif car in '+-><' + self.config.r3d_all:
                    if self.rotation_3d_done:
                        if car in '+-' + self.config.r3d_all:
                            langle = angle
                        else:
                            langle = angle2

                        self.rotate_3d(car, langle * tsens)
                    else:
                        if car in '+':
                            tangle = (tangle + angle * tsens) % 360.0
                        elif car in '-':
                            tangle = (tangle - angle * tsens) % 360.0
                        elif car in '>':
                            tangle = (tangle + angle2 * tsens) % 360.0
                        elif car in '<':
                            tangle = (tangle - angle2 * tsens) % 360.0
                        else:
                            # There is a not trivial 3D rotation ( PMpm )
                            self.init_3d(tangle)
                            self.rotate_3d(car, angle * tsens)

                elif car == '*':
                    tstep *= coeff
                elif car == '/':
                    tstep /= coeff
                elif car in self.config.delta_add:
                    tstep += delta
                elif car in self.config.delta_sub:
                    tstep -= delta
                elif car in '[(':
                    stock.append((tx, ty, tz, tangle, tcouleur, tstep, self.mobile_vectors))
                elif car in '])':
                    if stock:
                        tx, ty, tz, tangle, tcouleur, tstep, self.mobile_vectors = stock.pop()
                        nliste = True  # the pen is raised to go back to the stocked position
                elif car == '|':
                    # Single "return" ("round-trip")
                    npospos = True
                elif car == '!':
                    # Change the sens of rotation
                    tsens = tsens * -1
                elif car in self.config.outer_repetition:
                    # A new possible item in the "outer stock"
                    if nb_iterations <= self.config.outer_repetition_max:
                        stock_outer.append((tx, ty, tz, tangle, tcouleur, tstep, self.mobile_vectors))
                else:
                    continue

                # Take into account the read character
                # ------------------------------------
                if nliste:
                    # New list because of new color or lifted pen
                    if len(lix) > 1:
                        res.append((lix, liy, liz, tcouleur))

                    if ncolor:
                        # Change of color
                        color_index = (color_index + 1) % color_length
                        tcouleur = self.color_from_map(color_map, color_index)

                    lix = [tx]
                    liy = [ty]
                    liz = [tz]
                elif npos:
                    # New position and no new list
                    lix.append(tx)
                    liy.append(ty)
                    liz.append(tz)
                elif npospos:
                    # 2 new positions for a "round-trip"
                    tnx, tny, tnz = self.new_pos(tx, ty, tz, tstep, tangle)

                    lix.append(tnx)
                    liy.append(tny)
                    liz.append(tnz)

                    lix.append(tx)
                    liy.append(ty)
                    liz.append(tz)

        if len(lix) > 1:
            # Finally, append the last points
            res.append((lix, liy, liz, tcouleur))

        self.turt = res

    def render(self, show_type: str = 'matplot', image_destination: str = 'images_out/',
               save_files: bool = True, show_more: bool = False, show_3d: bool = False,
               return_type: str = ''):
        """
        Render self.turt using a specific show type

        :param show_type: 'matplot' or 'bokeh'
        :param image_destination: folder for images backup
        :param save_files: True to save files
        :param show_more: True to show with specific show_type
        :param show_3d: True to show 3D (implemented with plotly only)
        :param return_type: '', 'image' or 'figure'
        :return: None or an image if return_type is 'image' or a figure if return_type is 'figure'
        """
        if show_type == 'matplot':
            fig, ax = plt.subplots()

            for (lx, ly, _, coul) in self.turt:
                r, g, b = coul
                ax.plot(lx, ly, color=(r / 255., g / 255., b / 255., 1.0))

            ax.set_axis_off()
            ax.grid(visible=False)

            if show_more:
                plt.show()

            if save_files:
                fig.savefig(f'{image_destination}plot_{show_type}.png', bbox_inches='tight')
                fig.savefig(f'{image_destination}plot_{show_type}.svg', bbox_inches='tight')

            if return_type == 'image':
                fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
                fig.canvas.draw()

                # Return an image : PIL.Image
                return pimage.frombytes('RGB', fig.canvas.get_width_height(), fig.canvas.tostring_rgb())

            if return_type == 'figure':
                return fig

        elif show_type == 'bokeh':
            if save_files:
                output_file(f'{image_destination}lines_{show_type}.html')

            fig = figure(title="LSyst", x_axis_label='x', y_axis_label='y', width=800, height=800)

            for (lx, ly, _, coul) in self.turt:
                cr, cg, cb = coul
                fig.line(lx, ly, line_color=(cr, cg, cb))

            fig.xgrid.grid_line_color = None
            fig.ygrid.grid_line_color = None

            if show_more:
                _ = show(fig)

            if save_files:
                export_png(fig, filename=f'{image_destination}plot_{show_type}.png')

                fig.output_backend = "svg"
                export_svgs(fig, filename=f'{image_destination}plot_{show_type}.svg')

            if return_type == 'image':
                fig.toolbar_location = None
                fig.axis.visible = False
                fig.title = ""

                # Return an image : PIL.Image
                return get_screenshot_as_png(fig)

            if return_type == 'figure':
                return fig

        elif show_type == 'plotly':
            fig = go.Figure()

            axis_dict = {
                "showline": True,
                "showgrid": False,
                "showticklabels": True,
                "zeroline": False,
                "ticks": 'outside',
            }

            index = 0

            if self.dimension == 2 or not show_3d:
                fig.update_yaxes(
                    scaleanchor="x",
                    scaleratio=1,
                )
                for (lx, ly, lz, coul) in self.turt:
                    index += 1
                    cr, cg, cb = coul
                    fig.add_trace(go.Scatter(x=lx, y=ly, mode='lines',
                                             name=f"t{index}", line={"color": f'rgb({cr},{cg},{cb})', "width": 1}))

            else:
                # 3D
                for (lx, ly, lz, coul) in self.turt:
                    index += 1
                    cr, cg, cb = coul
                    fig.add_trace(go.Scatter3d(x=lx, y=ly, z=lz, mode='lines',
                                               name=f"t{index}", line={"color": f'rgb({cr},{cg},{cb})', "width": 1}))

            fig.update_layout(
                xaxis=axis_dict,
                yaxis=axis_dict,
                autosize=True,
                showlegend=False
            )

            if show_more:
                fig.show()

            if save_files:
                fig.write_image(f'{image_destination}plot_{show_type}.png')
                fig.write_image(f'{image_destination}plot_{show_type}.svg')

            if return_type == 'image':
                fig_bytes = fig.to_image(format="png")
                buf = io.BytesIO(fig_bytes)

                # Return an image : PIL.Image
                return pimage.open(buf)

            if return_type == 'figure':
                return fig

        else:
            raise ValueError("The given show_type is not correct")