-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_symmetry.py
executable file
·6014 lines (5329 loc) · 186 KB
/
create_symmetry.py
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
###
# imports
# <<<1
# misc functions
import copy
import getopt
import sys
import os
import os.path
from itertools import product
import re
import json
# math
from cmath import exp
from math import sqrt, pi, sin, cos, asin, atan2, ceil
from random import randrange, uniform, shuffle, seed
# multiprocessing
from multiprocessing import Process, Queue
import queue
# Tkinter for GUI
from tkinter import Tk, Canvas, Label, Frame, LabelFrame, Entry, Text, Menu
from tkinter import Button, Checkbutton, Radiobutton, Listbox, Scrollbar, Toplevel
from tkinter import StringVar, BooleanVar
import tkinter as tk
from tkinter.ttk import Combobox, Notebook, Style
import tkinter.font
from tkinter import filedialog, messagebox
# vectorized arrays
import numpy as np
import numexpr as ne
# image manipulation (Pillow)
import PIL
from PIL import ImageDraw
import PIL.ImageTk
from PIL.ImageColor import getrgb
# >>>1
###
# default configuration options
# <<<1
# DEVELOPPER MODE
DEVEL = False
# colorwheel
COLOR_GEOMETRY = (-1, 1, -1, 1)
DEFAULT_COLOR = "black"
# output
OUTPUT_GEOMETRY = (-1.5, 1.5, -1.5, 1.5)
SPHERE_ROTATIONS = (15, 0, 0)
TRANSLATION_INVERSION_DELTA = 0.1
TRANSLATION_DELTA = 0.1
ROTATION_DELTA = 1
ZOOM_FACTOR = 2**(1/4)
INVERSION_CENTER = complex(-1/2, sqrt(3)/2)
OUTPUT_SIZE = (800, 800)
FILENAME_TEMPLATE = "output-{type:}-{name:}-{nb:}"
###
# misc options
# color of background for sphere / hyperbolic patterns
DEFAULT_BACKGROUND = "#000066"
# color of random pixels ("stars") for sphere patterns
STAR_COLOR = "#FFC"
NB_STARS = 500
FADE_COEFF = 200
# misc GUI options
COLOR_SIZE = 180 # size of colorwheel image in GUI
PREVIEW_SIZE = 400 # size of preview image in GUI
STRETCH_DISPLAY_RADIUS = 5 # how much of the "stretched" colorwheel to display
UNDO_SIZE = 100 # size of undo stack
# process images using blocks of that many pixels (0 => process everything at
# once)
BLOCK_SIZE = 2000
# keep a random seed to display random pixels in sphere images. The pixels
# should always be at the same place during a run of the program to prevent
# "jumps" during translatiosn / rotations of the image
RANDOM_SEED = uniform(0, 1)
# >>>1
###
# recipes and related informations
# <<<1
PATTERN = { # <<<2
# the 17 wallpaper groups <<<3
'o': {
"alt_name": "p1",
"recipe": "",
"parity": "",
"type": "plane group",
"description": "general lattice",
# OK
},
'2222': {
"alt_name": "p2",
"recipe": "n,m = -n, -m",
"parity": "",
"type": "plane group",
"description": "general lattice",
# OK
},
'*×': {
"alt_name": "cm",
"recipe": "n,m = m,n",
"parity": "",
"type": "plane group",
"description": "rhombic lattice",
# OK
},
'2*22': {
"alt_name": "cmm",
"recipe": "n,m = m,n = -n,-m = -m,-n",
"parity": "",
"type": "plane group",
"description": "rhombic lattice",
# OK
},
'**': {
"alt_name": "pm",
"recipe": "n,m = n,-m",
"parity": "",
"type": "plane group",
"description": "rectangular lattice",
# OK
},
'**₁': {
"alt_name": "pm",
"type": "plane group",
},
'**₂': {
"alt_name": "pm",
"type": "plane group",
},
'××': {
"alt_name": "pg",
"recipe": "n,m = -{n}(n,-m)",
"parity": "",
"type": "plane group",
"description": "rectangular lattice",
# OK
},
'*2222': {
"alt_name": "pmm",
"recipe": "n,m = -n,-m = -n,m = n,-m",
"parity": "",
"type": "plane group",
"description": "rectangular lattice",
# OK
},
'22*': {
"alt_name": "pmg",
"recipe": "n,m = -n,-m = -{n}(n,-m) = -{n}(-n,m)",
"parity": "",
"type": "plane group",
"description": "rectangular lattice",
# OK
},
'22×': {
"alt_name": "pgg",
"recipe": "n,m = -n,-m = -{n+m}(n,-m) = -{n+m}(-n,m)",
"parity": "",
"type": "plane group",
"description": "rectangular lattice",
# OK
},
'442': {
"alt_name": "p4",
"recipe": "n,m = m,-n = -n,-m = -m,n",
"parity": "",
"type": "plane group",
"description": "square lattice",
# OK
},
'*442': {
"alt_name": "p4m",
"recipe": "n,m = m,-n = -n,-m = -m,n ; n,m = m,n",
"parity": "",
"type": "plane group",
"description": "square lattice",
# OK
},
'4*2': {
"alt_name": "p4g",
"recipe": "n,m = m,-n = -n,-m = -m,n ; n,m = -{n+m}(m,n)",
"parity": "",
"type": "plane group",
"description": "square lattice",
# OK
},
'333': {
"alt_name": "p3",
"recipe": "n,m = m,-n-m = -n-m,n",
"parity": "",
"type": "plane group",
"description": "hexagonal lattice",
# OK
},
'3*3': {
"alt_name": "p31m",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = m,n",
"parity": "",
"type": "plane group",
"description": "hexagonal lattice",
# OK
},
'*333': {
"alt_name": "p3m1",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = -m,-n",
"parity": "",
"type": "plane group",
"description": "hexagonal lattice",
# OK
},
'632': {
"alt_name": "p6",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = -n,-m",
"parity": "",
"type": "plane group",
"description": "hexagonal lattice",
# OK
},
'*632': {
"alt_name": "p6m",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = m,n = -n,-m = -m,-n",
"parity": "",
"type": "plane group",
"description": "hexagonal lattice",
# OK
}, # >>>3
# the 46 color reversing wallpaper groups <<<3
('o', 'o'): {
"alt_name": "",
"recipe": "",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "general lattice",
# OK
},
('2222', 'o'): {
"alt_name": "",
"recipe": "n,m = -(-n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "general lattice",
# OK
},
('*×', 'o'): {
"alt_name": "",
"recipe": "n,m = -(m,n)",
"parity": "",
"type": "color reversing plane group",
"description": "rhombic lattice",
# OK
},
('**', 'o'): {
"alt_name": "",
"recipe": "n,m = -(n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('××', 'o'): {
"alt_name": "",
"recipe": "n,m = -{n+1}(n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('2222', '2222'): {
"alt_name": "",
"recipe": "n,m = -n,-m",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "general lattice",
# OK
},
('2*22', '2222'): {
"alt_name": "",
"recipe": "n,m = -(m,n) ; n,m = -n,-m",
"parity": "",
"type": "color reversing plane group",
"description": "rhombic lattice",
# OK
},
('*2222', '2222'): {
"alt_name": "",
"recipe": "n,m = -(-n,m) ; n,m = -n,-m",
"parity": "",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK
},
('22*', '2222'): {
"alt_name": "",
"recipe": "n,m = -{m+1}(-n,m) ; n,m = -n,-m",
"parity": "",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK NOTE: there is a typo in Farris' book
},
('22×', '2222'): {
"alt_name": "",
"recipe": "n,m = -{1+n+m}(-n, m) ; n,m = -n,-m",
"parity": "",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK NOTE: there is a typo in Farris' book
},
('442', '2222'): {
"alt_name": "",
"recipe": "n,m = -(-m,n)",
"parity": "",
"type": "color reversing plane group",
"description": "square lattice with negating center",
# OK
},
('2*22', '*×'): {
"alt_name": "",
"recipe": "n,m = -(m,n) ; n,m = -(-n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "rhombic lattice",
# OK
},
('**', '*×'): {
"alt_name": "",
"recipe": "n,m = m,n",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "rhombic lattice",
# OK
},
('*2222', '2*22'): {
"alt_name": "",
"recipe": "n,m = m,n ; n,m = -n,-m",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "rhombic lattice",
# OK
},
('*442', '2*22'): {
"alt_name": "",
"recipe": "n,m = -(-m,n) ; n,m = m,n",
"parity": "",
"type": "color reversing plane group",
"description": "square lattice with negating center",
# OK
},
('4*2', '2*22'): {
"alt_name": "",
"recipe": "n,m = -(-m,n) ; n,m = -{n+m}(m,n)",
"parity": "",
"type": "color reversing plane group",
"description": "square lattice with negative center",
# OK
},
('*×', '**'): {
"alt_name": "",
"recipe": "n,m = n,-m",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('**₁', '**'): {
"alt_name": "",
"recipe": "n,m = n,-m",
"parity": "m = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('**₂', '**'): {
"alt_name": "",
"recipe": "n,m = -n,m",
"parity": "m = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK (the axis of symmetry are the same, only the size of the basic
# tile changes
},
('*2222', '**'): {
"alt_name": "",
"recipe": "n,m = n,-m ; n,m = -(-n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('22*', '**'): {
"alt_name": "",
"recipe": "n,m = -{m+1}(-n,m) ; n,m = -(-n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('*×', '××'): {
"alt_name": "",
"recipe": "n,m = -{n}(n,-m)",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('**', '××'): {
"alt_name": "",
"recipe": "n,m = -{n}(n,-m)",
"parity": "n = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('××', '××'): {
"alt_name": "",
"recipe": "n,m = -{m}(-n,m)",
"parity": "n = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('22*', '××'): {
"alt_name": "",
"recipe": "n,m = -{n}(n,-m) ; n,m = -(-n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('22×', '××'): {
"alt_name": "",
"recipe": "n,m = -{n+m}(-n,m) ; n,m = -(-n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "rectangular lattice without positive half turn",
# OK
},
('2*22', '*2222'): {
"alt_name": "",
"recipe": "n,m = -n,m ; n,m = -n,-m",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK
},
('*2222', '*2222'): {
"alt_name": "",
"recipe": "n,m = -n,m ; n,m = -n,-m",
"parity": "n = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK
},
('*442', '*2222'): {
"alt_name": "",
"recipe": "n,m = -(-m,n) ; n,m = -(m,n)",
"parity": "",
"type": "color reversing plane group",
"description": "square lattice with negating center",
# OK
},
('2*22', '22*'): {
"alt_name": "",
"recipe": "n,m = -{n}(n,-m) ; n,m = -n,-m",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK
},
('*2222', '22*'): {
"alt_name": "",
"recipe": "n,m = -{n}(n,-m) ; n,m = -n,-m",
"parity": "n = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK
},
('22*', '22*'): {
"alt_name": "",
"recipe": "n,m = -{n}(n,-m) ; n,m = -n,-m",
"parity": "m = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK
},
('2*22', '22×'): {
"alt_name": "",
"recipe": "n,m = -{n+m}(-n,m) ; n,m = -n,-m",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK
},
('22*', '22×'): {
"alt_name": "",
"recipe": "n,m = -{n+m}(-n,m) ; n,m = -n,-m",
"parity": "n = 1 mod 2",
"type": "color reversing plane group",
"description": "rectangular lattice with positive half turn",
# OK
},
('4*2', '22×'): {
"alt_name": "",
"recipe": "n,m = -(-m,n) ; n,m = -{1+n+m}(m,n)",
"parity": "",
"type": "color reversing plane group",
"description": "square lattice with negative center",
# OK
},
('442', '442'): {
"alt_name": "",
"recipe": "n,m = -m,n",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "square lattice with positive center",
# OK
},
('*442', '442'): {
"alt_name": "",
"recipe": "n,m = -m,n ; n,m = -(m,n)",
"parity": "",
"type": "color reversing plane group",
"description": "square lattice with positive center",
# OK
},
('4*2', '442'): {
"alt_name": "",
"recipe": "n,m = -m,n ; n,m = -{n+m+1}(m,n)",
"parity": "",
"type": "color reversing plane group",
"description": "square lattice with positive center",
# OK
},
('*442', '*442'): {
"alt_name": "",
"recipe": "n,m = -m,n ; n,m = m,n",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "square lattice with positive center",
# OK
},
('*442', '4*2'): {
"alt_name": "",
"recipe": "n,m = -m,n ; n,m = -{n+m}(m,n)",
"parity": "n+m = 1 mod 2",
"type": "color reversing plane group",
"description": "square lattice with positive center",
# OK
},
('3*3', '333'): {
"alt_name": "",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = -(m,n)",
"parity": "",
"type": "color reversing plane group",
"description": "hexagonal lattice, 3-fold symmetry",
# OK
},
('*333', '333'): {
"alt_name": "",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = -(-m,-n)",
"parity": "",
"type": "color reversing plane group",
"description": "hexagonal lattice, 3-fold symmetry",
# OK
},
('632', '333'): {
"alt_name": "",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = -(-n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "hexagonal lattice, 3-fold symmetry",
# OK
},
('*632', '3*3'): {
"alt_name": "",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = m,n ; n,m = -(-n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "hexagonal lattice, 6-fold symmetry",
# OK
},
('*632', '*333'): {
"alt_name": "",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = -m,-n ; n,m = -(-n,-m)",
"parity": "",
"type": "color reversing plane group",
"description": "hexagonal lattice, 6-fold symmetry",
# OK
},
('*632', '632'): {
"alt_name": "",
"recipe": "n,m = m,-n-m = -n-m,n ; n,m = -n,-m ; n,m = -(m,n)",
"parity": "",
"type": "color reversing plane group",
"description": "hexagonal lattice, 6-fold symmetry",
# OK
}, # >>>3
# the 14 spherical symmetry groups <<<3
'332': {
"alt_name": "T",
"recipe": "n,m = -n,-m",
"parity": "n-m = 0 mod 2",
"type": "sphere group",
"description": "tetrahedral symmetry",
},
'432': {
"alt_name": "O",
"recipe": "n,m = -n,-m",
"parity": "n-m = 0 mod 4",
"type": "sphere group",
"description": "octahedral symmetry",
},
'532': {
"alt_name": "I",
"recipe": "n,m = -n,-m",
"parity": "n-m = 0 mod 2",
"type": "sphere group",
"description": "icosahedral symmetry",
},
'3*2': {
"alt_name": "Th",
"recipe": "n,m = -n,-m ; n,m = -m,-n",
"parity": "n-m = 0 mod 2",
"type": "sphere group",
"description": "tetrahedral symmetry",
},
'*332': {
"alt_name": "Td",
"recipe": "n,m = -n,-m ; n,m = i{n-m}(m,n)",
"parity": "n-m = 0 mod 2",
"type": "sphere group",
"description": "tetrahedral symmetry",
},
'*432': {
"alt_name": "Oh",
"recipe": "n,m = -n,-m ; n,m = -m,-n",
"parity": "n-m = 0 mod 4",
"type": "sphere group",
"description": "octahedral symmetry",
},
'*532': {
"alt_name": "Ih",
"recipe": "n,m = -n,-m ; n,m = -m,-n",
"parity": "n-m = 0 mod 2",
"type": "sphere group",
"description": "icosahedral symmetry",
},
'NN': {
"alt_name": "CN p111",
"recipe": "",
"parity": "n-m = 0 mod N",
"type": "sphere group",
"description": "cyclic symmetry",
},
'22N': {
"alt_name": "DN p211",
"recipe": "n,m = -n,-m",
"parity": "n-m = 0 mod N",
"type": "sphere group",
"description": "dihedral symmetry",
},
'*NN': {
"alt_name": "CNv p1m1",
"recipe": "n,m = m,n",
"parity": "n-m = 0 mod N",
"type": "sphere group",
"description": "cyclic symmetry",
},
'N*': {
"alt_name": "CNh p11m",
"recipe": "n,m = -m,-n",
"parity": "n-m = 0 mod N",
"type": "sphere group",
"description": "cyclic symmetry",
},
'*22N': {
"alt_name": "DNh p2mm",
"recipe": "n,m = m,n = -n,-m = -m,-n",
"parity": "n-m = 0 mod N",
"type": "sphere group",
"description": "dihedral symmetry",
},
'N×': {
"alt_name": "S2N p11g",
"recipe": "n,m = -{n+m}(-m,-n)",
"parity": "n-m = 0 mod N",
"type": "sphere group",
"description": "cyclic symmetry",
},
'2*N': {
"alt_name": "DNd p2mg",
"recipe": "n,m = -n,-m = -{n+m}(-m,-n) = -{n+m}(m,n)",
"parity": "n-m = 0 mod N",
"type": "sphere group",
"description": "dihedral symmetry",
}, # >>>3
# the 7 frieze groups are generated from the corresponding cyclic spherical
# groups
}
# transform all the appropriate cyclic sphere groups into frieze groups
_F = {}
for p in PATTERN:
if PATTERN[p]["type"] == "sphere group" and "N" in p:
fp = p.replace("N", "∞")
_F[fp] = copy.deepcopy(PATTERN[p])
_F[fp]["type"] = "frieze"
_F[fp]["description"] = ""
alt_name1, alt_name2 = PATTERN[p]["alt_name"].split()
_F[fp]["alt_name"] = alt_name2
PATTERN[p]["alt_name"] = alt_name1
PATTERN.update(_F)
del _F
# >>>2
# order of the groups in menus
NAMES = [ # <<<2
# wallpaper groups
"o", # general lattice
"2222",
"*×", # rhombic lattice
"2*22",
"**", # rectangular lattice
"××",
"*2222",
"22*",
"22×",
"442", # square lattice
"*442",
"4*2",
"333", # hexagonal lattice
"3*3",
"*333",
"632",
"*632",
# spherical groups
"332",
"*332",
"3*2",
"432",
"*432",
"532",
"*532",
"NN",
"*NN",
"N*",
"N×",
"22N",
"*22N",
"2*N",
]
# add names for frieze patterns
NAMES = NAMES + [p.replace("N", "∞") for p in NAMES if "N" in p]
# full names, with alternative names, for wallpaper groups
W_NAMES = ["{} ({})".format(p, PATTERN[p]["alt_name"])
for p in NAMES
if PATTERN[p]["type"] == "plane group"]
_t = None
for i in range(len(W_NAMES)):
p = W_NAMES[i].split()[0]
t = PATTERN[p]["description"].split()[0]
if _t != t:
W_NAMES[i] += " -- {}".format(t)
_t = t
del p
# full names, with alternative names, for sphere groups
S_NAMES = ["{} ({})".format(p, PATTERN[p]["alt_name"])
for p in NAMES
if PATTERN[p]["type"] == "sphere group"]
_t = None
for i in range(len(S_NAMES)):
p = S_NAMES[i].split()[0]
t = PATTERN[p]["description"].split()[0]
if _t != t:
S_NAMES[i] += " -- {}".format(t)
_t = t
del p
del _t
# full names, with alternative names, for frieze groups
F_NAMES = ["{} ({})".format(p, PATTERN[p]["alt_name"])
for p in NAMES
if PATTERN[p]["type"] == "frieze"]
# full names, with alternative names, for color reversing groups, as a function
# of the symmetry groups
def C_NAMES(s):
r = []
# we need to deal with the two groups for **/**
names = copy.deepcopy(NAMES)
k = NAMES.index("**")
names.insert(k+1, "**₁")
names.insert(k+2, "**₂")
for p in names:
if (p, s) in PATTERN:
if p in ["**₁", "**₂"]:
q = p[2]
p = p[:2]
else:
q = ""
r.append("{}{} ({}{})".format(p, q, PATTERN[p]["alt_name"], q))
return r
# >>>2
# >>>1
###
# utility functions
# <<<1
class Error(Exception): # <<<2
"""generic class for errors"""
pass
# >>>2
def error(*args, **kwargs): # <<<2
"""print message on stderr"""
print("***", *args, file=sys.stderr, **kwargs)
# >>>2
def message(*args, **kwargs): # <<<2
"""print message if verbosity is greater than 1"""
print(*args, **kwargs)
# >>>2
def sequence(*fs): # <<<2
"""return a function calling all the argument functions in sequence
useful for running several functions in a callback
"""
def res(*args):
r = []
for f in fs:
r.append(f())
if "break" in r:
return "break"
return res
# >>>2
def invert22(M): # <<<2
"""invert a 2x2 matrix"""
d = M[0][0] * M[1][1] - M[1][0] * M[0][1]
I = [[M[1][1]/d, -M[0][1]/d],
[-M[1][0]/d, M[0][0]/d]]
return I
# >>>2
def matrix_mult(M1, M2): # <<<2
"""matrix multiplication"""
assert len(M1[0]) == len(M2)
R = [[0]*len(M2[0]) for i in range(len(M1))]
for i in range(len(M1)):
for j in range(len(M2[0])):
for k in range(len(M2)):
R[i][j] += M1[i][k] * M2[k][j]
return R
# >>>2
def rotation_matrix(x, y, z): # <<<2
"""rotation matrix corresponding to the Euler-Tait angles
("yaw", "pitch" and "roll")
see https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
"""
a = cos(x)*cos(y)
b = cos(x)*sin(y)*sin(z) - cos(z)*sin(x)
c = sin(x)*sin(z) + cos(x)*cos(z)*sin(y)
d = cos(y)*sin(x)
e = cos(x)*cos(z) + sin(x)*sin(y)*sin(z)
f = cos(z)*sin(x)*sin(y) - cos(x)*sin(z)
g = -sin(y)
h = cos(y)*sin(z)
i = cos(y)*cos(z)
return [[a, b, c], [d, e, f], [g, h, i]]
# >>>2
def tait_angle(R): # <<<2
"""compute the Euler-Tait angles from a rotation matrix
see http://stackoverflow.com/questions/18433801/converting-a-3x3-matrix-to-euler-tait-bryan-angles-pitch-yaw-roll
"""
theta_z = atan2(R[2][1], R[2][2])
theta_y = -asin(R[2][0])
theta_x = atan2(R[1][0], R[0][0])
return theta_x, theta_y, theta_z
# >>>2
def str_to_floats(s): # <<<2
"""transform a string into a list of floats"""
try:
if s.strip() == "":
return []
else:
s = re.sub(r"[,;]", " ", s)
return list(map(float, s.split()))
except:
raise ValueError("str_to_floats: '{}' is not a list of floats"
.format(s))
# >>>2
def float_to_str(x): # <<<2
"""transform a float into a string, removing trailing decimal 0s
and decimal points if possible
"""
s = str(x)
if "." in s:
s = re.sub(r"0*\s*$", "", s)
s = s.rstrip(".")
if s == "":
s = "0"
return s
# >>>2
def floats_to_str(l): # <<<2
"""transform a list of floats into a string"""
return ", ".join(map(float_to_str, l))
# >>>2
def str_to_complex(s): # <<<2
"""transform a string into a complex number"""
s = re.sub(r"\s*", "", s)
s = s.replace("i", "j")
return complex(s)
# >>>2
def complex_to_str(z, precision=16): # <<<2
"""transform a complex number into a string"""
if z == 0:
return "0"
elif z == z.real:
x = "{x:.{prec:}f}".format(x=z.real, prec=precision)
x = float_to_str(x)
return x
elif z == z - z.real:
y = "{y:.{prec:}f}".format(y=z.imag, prec=precision)
y = float_to_str(y)
y = "" if y == "1" else y
return y + "i"
else:
sign = "+" if z.imag > 0 else "-"
x = "{x:.{prec:}f}".format(x=z.real, prec=precision)
x = float_to_str(x)
# x = x.rjust(precision + 3)
y = "{y:.{prec:}f}".format(y=abs(z.imag), prec=precision)
y = float_to_str(y)
y = "" if y == "1" else y
return "{} {} {}i".format(x, sign, y)
# >>>2
def matrix_to_list(M): # <<<2
"""transform a "matrix" (ie a dictionnary with pairs of integers as keys
and complex numbers as values) into a list of pairs of pairs:
- the pair of integers of the key
- the real and imaginary parts of the value
This is used as preprocessing before transforming the matrix to JSON.
"""
try:
return [((n, m), (z.real, z.imag)) for (n, m), z in M.items()]
except:
return M
# >>>2
def list_to_matrix(L): # <<<2
"""transform a list of pairs of pairs into a matrix:
- the first pairs are integers
- the second pairs are floats (real and imaginary parts)