gemlab 2.0.0

Geometry and meshes laboratory for finite element analyses
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
use super::{Features, Mesh};
use crate::mesh::Triangulation;
use crate::shapes::{GeoClass, GeoKind, Scratchpad};
use crate::StrError;
use plotpy::{Canvas, Curve, InsetAxes, Plot, Text};
use russell_lab::math::PI;
use russell_lab::{sort2, sort4, Vector};
use std::ffi::OsStr;

/// Implements functions to draw cells, edges, and faces or the whole mesh
pub struct Draw<'a> {
    /// The plotpy structure to draw figures (plots)
    plot: Plot,

    /// Multiplier used to set the drawing area range
    m_range: f64,

    /// Tolerance to decide if an edge is horizontal or vertical
    tol_edge_marker: f64,

    /// Multiplier to scale the length of the normal vectors
    m_normal_vector: f64,

    /// Multiplier to scale the length of the normal vectors for markers
    m_normal_vector_marker: f64,

    /// Canvas to draw edges
    canvas_edges: Canvas,

    /// Canvas to draw points (dot indicators)
    canvas_point_dots: Curve,

    /// Canvas to draw point ids
    canvas_point_ids: Text,

    /// Canvas to draw cell ids
    canvas_cell_ids: Text,

    /// Canvas to draw cells
    canvas_cells: Canvas,

    /// Canvas to draw lin cells
    canvas_lin_cells: Canvas,

    /// Canvas to draw shells (triangles and quadrilaterals in 3D)
    canvas_shells: Canvas,

    /// Canvas to draw edge markers
    canvas_edge_markers: Text,

    /// Canvas to draw face markers lines
    canvas_edge_markers_lines: Canvas,

    /// Canvas to draw face markers
    canvas_face_markers: Text,

    /// Canvas to draw face markers lines
    canvas_face_markers_lines: Canvas,

    /// Canvas to draw normal vectors (2D)
    canvas_normals_2d: Canvas,

    /// Canvas to draw normal vectors (3D)
    canvas_normals_3d: Canvas,

    /// Canvas to draw boundary edges in 3D
    canvas_boundary_edges_3d: Canvas,

    /// Canvas to draw boundary faces (3D)
    canvas_boundary_faces: Canvas,

    /// Canvas to draw the glyph indicating the X-Y-Z directions in 3D plots
    canvas_glyph_3d: Canvas,

    /// Shows cells (wireframe in 3D)
    show_cells: bool,

    /// Shows cell ids
    show_cell_ids: bool,

    /// Shows cell marker within parenthesis
    show_cell_marker: bool,

    /// Shows point ids
    show_point_ids: bool,

    /// Shows point marker within parenthesis (if not zero)
    show_point_marker: bool,

    /// Shows point dots
    show_point_dots: bool,

    /// Shows edge markers
    show_edge_markers: bool,

    /// Shows face markers
    show_face_markers: bool,

    /// Shows normal vectors on boundaries
    show_normal_vectors: bool,

    /// Draws boundary edges (3D)
    show_boundary_edges_3d: bool,

    /// Draws boundary faces (3D)
    show_boundary_faces: bool,

    /// Shows the glyph indicating the X-Y-Z directions in 3D plots
    show_glyph_3d: bool,

    /// View the figure interactively in a window
    view: bool,

    /// Generates the plot without equal axes
    unequal_exes: bool,

    /// Specifies the camera elevation for 3D plots (default: 30 deg)
    camera_elevation: f64,

    /// Specifies the camera azimuth for 3D plots (default: 30 deg)
    camera_azimuth: f64,

    /// Hides the 3D grid and panes (make them transparent)
    hide_3d_grid: bool,

    /// Hides the axes (2D or 3D)
    hide_axes: bool,

    /// Origin point of the 3D glyph
    glyph_3d_origin: [f64; 3],

    /// Size of the 3D glyph
    glyph_3d_size: f64,

    /// Specifies the plot range (xmin, xmax, ymin, ymax)
    range_2d: Option<(f64, f64, f64, f64)>,

    /// Specifies the plot range (xmin, xmax, ymin, ymax, zmin, zmax)
    range_3d: Option<(f64, f64, f64, f64, f64, f64)>,

    /// Specifies the figure size in points
    size: Option<(f64, f64)>,

    /// Holds a (callback) function to perform some extra drawing on the plot area
    ///
    /// The function is `|plot, before| {}` where:
    ///
    /// * `plot` -- the `plot` reference that can be used perform some extra drawings.
    /// * `before` -- **true** indicates that the callback function is being called before all other drawing functions.
    extra: Option<Box<dyn Fn(&mut Plot, bool) + 'a>>,

    /// Enables zooming a region of the plot (2D meshes only)
    ///
    /// Holds `((xmin, xmax, ymin, ymax), (u0, u0, w, h))` for the zoomed region; where:
    ///
    /// * `(xmin, xmax, ymin, ymax)` -- the region to be zoomed
    /// * `(u0, v0, w, h)` -- the position (normalized coordinates) and size (width,height) of the zoomed region
    zoom_2d: Option<((f64, f64, f64, f64), (f64, f64, f64, f64))>,

    /// Configures some characteristics of the zoom indicator
    ///
    /// Holds `(color, alpha, linewidth)` for the zoom indicator.
    zoom_indicator_config: (String, f64, f64),

    /// Holds a (callback) function to perform some extra drawing on the zoom area
    ///
    /// The function is `|inset| {}` where:
    ///
    /// * `inset` -- Is the InsetAxes reference for further configuration
    zoom_extra: Option<Box<dyn Fn(&mut InsetAxes) + 'a>>,
}

impl<'a> Draw<'a> {
    /// Allocates a new instance
    pub fn new() -> Self {
        let mut canvas_edges = Canvas::new();
        let mut canvas_point_dots = Curve::new();
        let mut canvas_point_ids = Text::new();
        let mut canvas_cell_ids = Text::new();
        let mut canvas_cells = Canvas::new();
        let mut canvas_lin_cells = Canvas::new();
        let mut canvas_shells = Canvas::new();
        let mut canvas_edge_markers = Text::new();
        let mut canvas_edge_markers_lines = Canvas::new();
        let mut canvas_face_markers = Text::new();
        let mut canvas_face_markers_lines = Canvas::new();
        let mut canvas_normals_2d = Canvas::new();
        let mut canvas_normals_3d = Canvas::new();
        let mut canvas_boundary_edges_3d = Canvas::new();
        let mut canvas_boundary_faces = Canvas::new();
        let canvas_glyph_3d = Canvas::new();
        canvas_edges
            .set_face_color("None")
            .set_line_width(1.0)
            .set_edge_color("#2440cd");
        canvas_point_dots
            .set_marker_color("black")
            .set_marker_line_color("white")
            .set_marker_style("o")
            .set_line_style("None");
        canvas_point_ids
            .set_extra("clip_on=True")
            .set_color("red")
            .set_fontsize(7.0)
            .set_align_horizontal("center")
            .set_align_vertical("center")
            .set_bbox(true)
            .set_bbox_facecolor("white")
            .set_bbox_edgecolor("None")
            .set_bbox_style("round,pad=0.15");
        canvas_cell_ids
            .set_extra("clip_on=True")
            .set_color("#22971f")
            .set_fontsize(8.0)
            .set_align_horizontal("center")
            .set_align_vertical("center")
            .set_bbox(true)
            .set_bbox_facecolor("white")
            .set_bbox_edgecolor("None")
            .set_bbox_style("square,pad=0.15");
        canvas_cells
            .set_face_color("#e3f3ff")
            .set_edge_color("#0055d4")
            .set_line_width(1.0);
        canvas_lin_cells
            .set_face_color("None")
            .set_edge_color("#cd0000")
            .set_line_width(2.0);
        canvas_shells
            .set_edge_color("None")
            .set_face_color("#32b31880")
            .set_line_width(1.0);
        canvas_edge_markers
            .set_color("#000000ff")
            .set_fontsize(9.0)
            .set_align_horizontal("center")
            .set_align_vertical("center")
            .set_bbox(true)
            .set_bbox_facecolor("#fff0a3ff")
            .set_bbox_edgecolor("black")
            .set_bbox_style("square,pad=0.15");
        canvas_edge_markers_lines
            .set_edge_color("#000000ff")
            .set_line_width(1.0);
        canvas_face_markers
            .set_color("#000000ff")
            .set_fontsize(9.0)
            .set_align_horizontal("center")
            .set_align_vertical("center")
            .set_bbox(true)
            .set_bbox_facecolor("#aaffb5ff")
            .set_bbox_edgecolor("black")
            .set_bbox_style("square,pad=0.15");
        canvas_face_markers_lines
            .set_face_color("None")
            .set_edge_color("#000000ff")
            .set_line_width(1.0);
        canvas_normals_2d
            .set_face_color("None")
            .set_edge_color("#f400f4ff")
            .set_line_width(2.0)
            .set_arrow_scale(5.0)
            .set_arrow_style("->");
        canvas_normals_3d
            .set_face_color("None")
            .set_edge_color("#f400f4ff")
            .set_line_width(2.0);
        canvas_boundary_edges_3d.set_edge_color("#001a9eff").set_line_width(2.0);
        canvas_boundary_faces.set_edge_color("None").set_face_color("#0095ff80");
        Draw {
            plot: Plot::new(),
            m_range: 0.2,
            tol_edge_marker: 1e-3,
            m_normal_vector: 0.05,
            m_normal_vector_marker: 0.1,
            canvas_edges,
            canvas_point_dots,
            canvas_point_ids,
            canvas_cell_ids,
            canvas_cells,
            canvas_lin_cells,
            canvas_shells,
            canvas_edge_markers,
            canvas_edge_markers_lines,
            canvas_face_markers,
            canvas_face_markers_lines,
            canvas_normals_2d,
            canvas_normals_3d,
            canvas_boundary_edges_3d,
            canvas_boundary_faces,
            canvas_glyph_3d,
            show_cells: true,
            show_cell_ids: false,
            show_cell_marker: true,
            show_point_ids: false,
            show_point_marker: false,
            show_point_dots: false,
            show_edge_markers: false,
            show_face_markers: false,
            show_normal_vectors: false,
            show_boundary_edges_3d: true,
            show_boundary_faces: true,
            show_glyph_3d: false,
            view: false,
            unequal_exes: false,
            camera_elevation: 30.0,
            camera_azimuth: 30.0,
            hide_3d_grid: true,
            hide_axes: false,
            glyph_3d_size: 1.0,
            glyph_3d_origin: [0.0, 0.0, 0.0],
            range_2d: None,
            range_3d: None,
            size: None,
            extra: None,
            zoom_2d: None,
            zoom_indicator_config: ("#f9d835".to_string(), 1.0, 2.0),
            zoom_extra: None,
        }
    }

    /// Get a mutable reference to the canvas to draw edges
    pub fn get_canvas_edges(&mut self) -> &mut Canvas {
        &mut self.canvas_edges
    }

    /// Get a mutable reference to the canvas to draw points
    pub fn get_canvas_points(&mut self) -> &mut Curve {
        &mut self.canvas_point_dots
    }

    /// Get a mutable reference to the canvas to draw point ids
    pub fn get_canvas_point_ids(&mut self) -> &mut Text {
        &mut self.canvas_point_ids
    }

    /// Get a mutable reference to the canvas to draw cell ids
    pub fn get_canvas_cell_ids(&mut self) -> &mut Text {
        &mut self.canvas_cell_ids
    }

    /// Get a mutable reference to the canvas to draw cells
    pub fn get_canvas_cells(&mut self) -> &mut Canvas {
        &mut self.canvas_cells
    }

    /// Get a mutable reference to the canvas to draw lin cells
    pub fn get_canvas_lin_cells(&mut self) -> &mut Canvas {
        &mut self.canvas_lin_cells
    }

    /// Get a mutable reference to the canvas to draw edge markers
    pub fn get_canvas_edge_markers(&mut self) -> &mut Text {
        &mut self.canvas_edge_markers
    }

    /// Get a mutable reference to the canvas to draw edge markers lines
    pub fn get_canvas_edge_markers_lines(&mut self) -> &mut Canvas {
        &mut self.canvas_edge_markers_lines
    }

    /// Get a mutable reference to the canvas to draw face markers
    pub fn get_canvas_face_markers(&mut self) -> &mut Text {
        &mut self.canvas_face_markers
    }

    /// Get a mutable reference to the canvas to draw face markers lines
    pub fn get_canvas_face_markers_lines(&mut self) -> &mut Canvas {
        &mut self.canvas_face_markers_lines
    }

    /// Get a mutable reference to the canvas to draw normals 2D
    pub fn get_canvas_normals_2d(&mut self) -> &mut Canvas {
        &mut self.canvas_normals_2d
    }

    /// Get a mutable reference to the canvas to draw normals 3D
    pub fn get_canvas_normals_3d(&mut self) -> &mut Canvas {
        &mut self.canvas_normals_3d
    }

    /// Get a mutable reference to the canvas tro draw boundary faces (3D)
    pub fn get_canvas_boundary_faces(&mut self) -> &mut Canvas {
        &mut self.canvas_boundary_faces
    }

    /// Sets the multiplier used to set the drawing area range
    ///
    /// Default: `0.2`
    pub fn set_m_range(&mut self, value: f64) -> &mut Self {
        self.m_range = value;
        self
    }

    /// Sets the tolerance to decide if an edge is horizontal or vertical
    ///
    /// Default: `1e-3`
    pub fn set_tol_edge_marker(&mut self, value: f64) -> &mut Self {
        self.tol_edge_marker = value;
        self
    }

    /// Sets the multiplier to scale the length of the normal vectors
    ///
    /// Default: `0.05`
    pub fn set_m_normal_vector(&mut self, value: f64) -> &mut Self {
        self.m_normal_vector = value;
        self
    }

    /// Sets the multiplier to scale the length of the normal vectors for markers
    ///
    /// Default: `0.1`
    pub fn set_m_normal_vector_marker(&mut self, value: f64) -> &mut Self {
        self.m_normal_vector_marker = value;
        self
    }

    /// Shows cells (wireframe in 3D)
    ///
    /// Default: `true`
    pub fn show_cells(&mut self, value: bool) -> &mut Self {
        self.show_cells = value;
        self
    }

    /// Shows cell ids
    ///
    /// Default: `false`
    pub fn show_cell_ids(&mut self, value: bool) -> &mut Self {
        self.show_cell_ids = value;
        self
    }

    /// Shows cell marker within parenthesis
    ///
    /// Default: `true`
    pub fn show_cell_marker(&mut self, value: bool) -> &mut Self {
        self.show_cell_marker = value;
        self
    }

    /// Shows point ids
    ///
    /// Default: `false`
    pub fn show_point_ids(&mut self, value: bool) -> &mut Self {
        self.show_point_ids = value;
        self
    }

    /// Shows point marker within parenthesis (if not zero)
    ///
    /// Default: `false`
    pub fn show_point_marker(&mut self, value: bool) -> &mut Self {
        self.show_point_marker = value;
        self
    }

    /// Shows point dots
    ///
    /// Default: `false`
    pub fn show_point_dots(&mut self, value: bool) -> &mut Self {
        self.show_point_dots = value;
        self
    }

    /// Shows edge markers
    ///
    /// Default: `false`
    pub fn show_edge_markers(&mut self, value: bool) -> &mut Self {
        self.show_edge_markers = value;
        self
    }

    /// Shows face markers
    ///
    /// Default: `false`
    pub fn show_face_markers(&mut self, value: bool) -> &mut Self {
        self.show_face_markers = value;
        self
    }

    /// Shows normal vectors on boundaries
    ///
    /// Default: `false`
    pub fn show_normal_vectors(&mut self, value: bool) -> &mut Self {
        self.show_normal_vectors = value;
        self
    }

    /// Shows boundary edges (3D)
    ///
    /// Default: `true`
    pub fn show_boundary_edges_3d(&mut self, value: bool) -> &mut Self {
        self.show_boundary_edges_3d = value;
        self
    }

    /// Shows boundary faces (3D)
    ///
    /// Default: `true`
    pub fn show_boundary_faces(&mut self, value: bool) -> &mut Self {
        self.show_boundary_faces = value;
        self
    }

    /// Shows the glyph indicating the X-Y-Z directions in 3D plots
    ///
    /// Default: `false`
    pub fn show_glyph_3d(&mut self, value: bool) -> &mut Self {
        self.show_glyph_3d = value;
        self
    }

    /// View the figure interactively in a window
    pub fn set_view_flag(&mut self, value: bool) -> &mut Self {
        self.view = value;
        self
    }

    /// Generates the plot without equal axes
    ///
    /// Default: `false`
    pub fn set_unequal_exes(&mut self, value: bool) -> &mut Self {
        self.unequal_exes = value;
        self
    }

    /// Sets the camera position for 3D plots (azimuth angles in degrees)
    pub fn set_camera(&mut self, elevation: f64, azimuth: f64) -> &mut Self {
        self.camera_elevation = elevation;
        self.camera_azimuth = azimuth;
        self
    }

    /// Sets an option to hide the 3D grid and panes (make them transparent)
    ///
    /// Default: `true`
    pub fn set_hide_3d_grid(&mut self, value: bool) -> &mut Self {
        self.hide_3d_grid = value;
        self
    }

    /// Sets whether to hide the axes (2D or 3D)
    pub fn set_hide_axes(&mut self, value: bool) -> &mut Self {
        self.hide_axes = value;
        self
    }

    /// Specifies the origin point of the 3D glyph and its size
    pub fn set_glyph_3d(&mut self, x: f64, y: f64, z: f64, size: f64) -> &mut Self {
        self.glyph_3d_origin = [x, y, z];
        self.glyph_3d_size = size;
        self
    }

    /// Specifies the plot range in 2D
    ///
    /// Default: `None` (automatic range)
    pub fn set_range_2d(&mut self, xmin: f64, xmax: f64, ymin: f64, ymax: f64) -> &mut Self {
        self.range_2d = Some((xmin, xmax, ymin, ymax));
        self
    }

    /// Specifies the plot range in 3D
    ///
    /// Default: `None` (automatic range)
    pub fn set_range_3d(&mut self, xmin: f64, xmax: f64, ymin: f64, ymax: f64, zmin: f64, zmax: f64) -> &mut Self {
        self.range_3d = Some((xmin, xmax, ymin, ymax, zmin, zmax));
        self
    }

    /// Specifies the figure size in points
    ///
    /// Default: `None` (automatic size)
    pub fn set_size(&mut self, width: f64, height: f64) -> &mut Self {
        self.size = Some((width, height));
        self
    }

    /// Sets a callback function to perform extra drawing on the plot area
    ///
    /// Default: `None`
    ///
    /// The function is `|plot, before| { ... }` where:
    ///
    /// * `plot` -- is the `plot` mutable reference that can be used perform some extra drawings.
    /// * `before` -- indicates if the function is being called before all other drawing functions.
    ///   The value `false` means that the function is being called `after` all other functions,
    ///   and just before the call to `plot.save()`.
    pub fn extra(&mut self, callback: impl Fn(&mut Plot, bool) + 'a) -> &mut Self {
        self.extra = Some(Box::new(callback));
        self
    }

    /// Enables zooming a region of the plot (2D meshes only)
    ///
    /// # Input
    ///
    /// * `xmin, xmax, ymin, ymax` - the region to be zoomed
    /// * `u0, v0, w, h` - the position (normalized coordinates) and size (width,height) of the zoomed region
    ///
    /// # Examples
    ///
    /// ```
    /// use gemlab::mesh::{Draw, Samples};
    /// use gemlab::StrError;
    /// use plotpy::Text;
    ///
    /// const SAVE_FIGURE: bool = false;
    ///
    /// fn main() -> Result<(), StrError> {
    ///     if SAVE_FIGURE {
    ///         let mesh = Samples::block_2d_four_qua12();
    ///         let mut draw = Draw::new();
    ///         draw.show_cell_ids(true)
    ///             .show_point_ids(true)
    ///             .show_point_dots(true)
    ///             .set_range_2d(-0.5, 6.0, -0.5, 6.0)
    ///             .zoom_2d(-0.05, 1.55, -0.05, 1.55, 0.6, 0.6, 0.3, 0.3)
    ///             .zoom_extra(|inset| {
    ///                 let mut text = Text::new();
    ///                 text.draw(0.3, 1.0, "HELLO");
    ///                 inset.add(&text);
    ///             })
    ///             .all(&mesh, "/tmp/gemlab/doc_draw_works_qua12.svg")?;
    ///     }
    ///     Ok(())
    /// }
    /// ```
    ///
    /// ![doc_draw_works_qua12](https://raw.githubusercontent.com/cpmech/gemlab/main/data/figures/doc_draw_works_qua12.svg)
    pub fn zoom_2d(
        &mut self,
        xmin: f64,
        xmax: f64,
        ymin: f64,
        ymax: f64,
        u0: f64,
        v0: f64,
        w: f64,
        h: f64,
    ) -> &mut Self {
        self.zoom_2d = Some(((xmin, xmax, ymin, ymax), (u0, v0, w, h)));
        self
    }

    /// Disables the mesh zooming
    ///
    /// Default: `None` (no zoom)
    pub fn disable_zoom_2d(&mut self) -> &mut Self {
        self.zoom_2d = None;
        self
    }

    /// Configures some characteristics of the zoom indicator
    ///
    /// Default: `("#f9d835", 1.0, 2.0)`
    ///
    /// # Input
    ///
    /// * `color` - color of the zoom indicator
    /// * `alpha` - transparency of the zoom indicator
    /// * `linewidth` - width of the zoom indicator line
    pub fn zoom_indicator(&mut self, color: &str, alpha: f64, linewidth: f64) -> &mut Self {
        self.zoom_indicator_config = (color.to_string(), alpha, linewidth);
        self
    }

    /// Sets a callback function to perform extra drawing on the zoom area
    ///
    /// Default: `None`
    ///
    /// The function is `|inset| {}` where:
    ///
    /// * `inset` -- Is the InsetAxes reference for further configuration
    ///
    /// # Examples
    ///
    /// See [zoom_2d](Self::zoom_2d) method for an example.
    pub fn zoom_extra(&mut self, callback: impl Fn(&mut InsetAxes) + 'a) -> &mut Self {
        self.zoom_extra = Some(Box::new(callback));
        self
    }

    /// Draws all points (dots)
    pub fn point_dots(&mut self, mesh: &Mesh) {
        if mesh.ndim == 2 {
            self.canvas_point_dots.points_begin();
            mesh.points.iter().for_each(|point| {
                self.canvas_point_dots.points_add(point.coords[0], point.coords[1]);
            });
            self.canvas_point_dots.points_end();
        } else {
            self.canvas_point_dots.points_3d_begin();
            mesh.points.iter().for_each(|point| {
                self.canvas_point_dots
                    .points_3d_add(point.coords[0], point.coords[1], point.coords[2]);
            });
            self.canvas_point_dots.points_3d_end();
        }
        self.plot.add(&self.canvas_point_dots);
    }

    /// Draws all point ids (labels)
    ///
    /// **Note:** Non-zero point markers are shown within parentheses.
    pub fn point_ids(&mut self, mesh: &Mesh) {
        if mesh.ndim == 2 {
            mesh.points.iter().for_each(|point| {
                let msg = if point.marker != 0 && self.show_point_marker {
                    format!("{}({})", point.id, point.marker)
                } else {
                    format!("{}", point.id)
                };
                self.canvas_point_ids.draw(point.coords[0], point.coords[1], &msg);
            });
        } else {
            mesh.points.iter().for_each(|point| {
                let msg = if point.marker != 0 && self.show_point_marker {
                    format!("{}({})", point.id, point.marker)
                } else {
                    format!("{}", point.id)
                };
                self.canvas_point_ids
                    .draw_3d(point.coords[0], point.coords[1], point.coords[2], &msg);
            });
        }
        self.plot.add(&self.canvas_point_ids);
    }

    /// Draws cells
    pub fn cells(&mut self, mesh: &Mesh) -> Result<(), StrError> {
        // holds the IDs of shell cells
        let mut shell_cell_ids = Vec::new();

        // loop over cells (draws the wireframe if 3D)
        for cell_id in 0..mesh.cells.len() {
            if mesh.ndim == 3 {
                let class = mesh.cells[cell_id].kind.class();
                if class == GeoClass::Tri || class == GeoClass::Qua {
                    shell_cell_ids.push(cell_id);
                }
            };
            let canvas = if mesh.cells[cell_id].kind.is_lin() {
                &mut self.canvas_lin_cells
            } else {
                &mut self.canvas_cells
            };
            mesh.draw_cell(canvas, cell_id)?;
        }

        // loop over shell cells to draw the 3D surface (the wireframe is already drawn)
        if shell_cell_ids.len() > 0 {
            let surface: Vec<_> = shell_cell_ids.iter().map(|&id| &mesh.cells[id]).collect();
            let res = Triangulation::from_surface(mesh, &surface);
            self.canvas_shells
                .draw_triangles_3d(&res.xx, &res.yy, &res.zz, &res.triangles);
            self.plot.add(&self.canvas_shells);
        }

        // add (wireframe) to plot
        self.plot.add(&self.canvas_cells);
        self.plot.add(&self.canvas_lin_cells);
        Ok(())
    }

    /// Draws ids and markers of cells
    ///
    /// **Note:** Cell markers are shown within parentheses.
    pub fn cell_ids(&mut self, mesh: &Mesh) -> Result<(), StrError> {
        // auxiliary
        let mut x = Vector::new(mesh.ndim);

        // loop over all cells
        for cell_id in 0..mesh.cells.len() {
            // compute coordinates of the label
            let cell = &mesh.cells[cell_id];
            x.fill(0.0);
            if cell.kind == GeoKind::Qua9 || cell.kind == GeoKind::Qua17 {
                for i in 0..mesh.ndim {
                    x[i] = (3.0 * mesh.points[cell.points[0]].coords[i] + mesh.points[cell.points[2]].coords[i]) / 4.0;
                }
            } else if cell.kind == GeoKind::Tri10 {
                for i in 0..mesh.ndim {
                    x[i] = (mesh.points[cell.points[0]].coords[i] + mesh.points[cell.points[9]].coords[i]) / 2.0;
                }
            } else {
                for m in 0..cell.points.len() {
                    for i in 0..mesh.ndim {
                        x[i] += mesh.points[cell.points[m]].coords[i];
                    }
                }
                for i in 0..mesh.ndim {
                    x[i] /= cell.points.len() as f64;
                }
            }

            // add label
            let msg = if self.show_cell_marker {
                format!("{}({})", cell.id, cell.marker)
            } else {
                format!("{}", cell.id)
            };
            if mesh.ndim == 2 {
                self.canvas_cell_ids.draw(x[0], x[1], msg.as_str());
            } else {
                self.canvas_cell_ids.draw_3d(x[0], x[1], x[2], msg.as_str());
            }
        }

        // add to plot
        self.plot.add(&self.canvas_cell_ids);
        Ok(())
    }

    /// Draws edge markers
    pub fn edge_markers(&mut self, features: &Features) -> Result<(), StrError> {
        let mesh = &features.mesh;
        let ndim = mesh.ndim;
        let ksi = &[0.0, 0.0];
        let mut un = Vector::new(ndim);
        let mut x = Vector::new(ndim);
        let mut sum = 0.0;
        for i in 0..ndim {
            sum += f64::abs(features.max[i] - features.min[i]) * f64::abs(features.max[i] - features.min[i]);
        }
        let s = f64::sqrt(sum) * self.m_normal_vector_marker;
        for (marker, p1, p2) in &mesh.marked_edges {
            if ndim == 2 {
                let mut key = (*p1, *p2);
                sort2(&mut key);
                if let Some(edge) = features.edges.get(&key) {
                    let mut pad = Scratchpad::new(ndim, edge.kind)?;
                    mesh.set_pad(&mut pad, &edge.points);
                    pad.calc_normal_vector(&mut un, ksi)?;
                    pad.calc_coords(&mut x, ksi)?;
                    let alpha = if f64::abs(un[1]) < self.tol_edge_marker {
                        0.0
                    } else if f64::abs(un[0]) < self.tol_edge_marker {
                        90.0
                    } else {
                        f64::atan(un[1] / un[0]) * 180.0 / PI
                    };
                    self.canvas_edge_markers.set_rotation(alpha).draw(
                        x[0] + 0.5 * s * un[0],
                        x[1] + 0.5 * s * un[1],
                        &format!("{}", marker),
                    );
                    self.canvas_edge_markers_lines
                        .draw_polyline(&[[x[0], x[1]], [x[0] + s * un[0], x[1] + s * un[1]]], false);
                }
            } else {
                for i in 0..ndim {
                    x[i] = 0.5 * (mesh.points[*p1].coords[i] + mesh.points[*p2].coords[i]);
                }
                self.canvas_edge_markers
                    .draw_3d(x[0], x[1], x[2], &format!("{}", marker));
            }
        }
        self.plot.add(&self.canvas_edge_markers_lines);
        self.plot.add(&self.canvas_edge_markers);
        Ok(())
    }

    /// Draws face markers
    pub fn face_markers(&mut self, features: &Features) -> Result<(), StrError> {
        let mesh = &features.mesh;
        let ndim = mesh.ndim;
        if ndim == 2 {
            return Ok(());
        }
        let ksi3 = &[1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0];
        let ksi4 = &[0.0, 0.0, 0.0];
        let mut un = Vector::new(ndim);
        let mut x = Vector::new(ndim);
        let mut sum = 0.0;
        for i in 0..ndim {
            sum += f64::abs(features.max[i] - features.min[i]) * f64::abs(features.max[i] - features.min[i]);
        }
        let s = f64::sqrt(sum) * self.m_normal_vector_marker;
        for (marker, p1, p2, p3, p4) in &mesh.marked_faces {
            let mut key = (*p1, *p2, *p3, *p4);
            sort4(&mut key);
            if let Some(face) = features.faces.get(&key) {
                let mut pad = Scratchpad::new(ndim, face.kind)?;
                mesh.set_pad(&mut pad, &face.points);
                if face.kind.class() == GeoClass::Tri {
                    pad.calc_normal_vector(&mut un, ksi3)?;
                    pad.calc_coords(&mut x, ksi3)?;
                } else {
                    pad.calc_normal_vector(&mut un, ksi4)?;
                    pad.calc_coords(&mut x, ksi4)?;
                }
                self.canvas_face_markers.draw_3d(
                    x[0] + 0.5 * s * un[0],
                    x[1] + 0.5 * s * un[1],
                    x[2] + 0.5 * s * un[2],
                    &format!("{}", marker),
                );
                self.canvas_face_markers_lines.polyline_3d_begin();
                self.canvas_face_markers_lines.polyline_3d_add(x[0], x[1], x[2]);
                self.canvas_face_markers_lines
                    .polyline_3d_add(x[0] + s * un[0], x[1] + s * un[1], x[2] + s * un[2]);
                self.canvas_face_markers_lines.polyline_3d_end();
            }
        }
        self.plot.add(&self.canvas_face_markers_lines);
        self.plot.add(&self.canvas_face_markers);
        Ok(())
    }

    /// Draws normal vectors on boundary edges, faces, and shells
    pub fn normal_vectors(&mut self, features: &Features) -> Result<(), StrError> {
        let ndim = features.mesh.ndim;
        let mut sum = 0.0;
        for i in 0..ndim {
            sum += f64::abs(features.max[i] - features.min[i]) * f64::abs(features.max[i] - features.min[i]);
        }
        let s = f64::sqrt(sum) * self.m_normal_vector;
        let ksi3 = &[1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0];
        let ksi4 = &[0.0, 0.0, 0.0];
        let mut un = Vector::new(ndim);
        let mut x = Vector::new(ndim);
        if ndim == 2 {
            for (edge_key, edge) in &features.edges {
                let ncell = features.all_2d_edges.get(edge_key).unwrap().len();
                if ncell == 1 {
                    // only boundary edges
                    let mut pad = Scratchpad::new(ndim, edge.kind)?;
                    features.mesh.set_pad(&mut pad, &edge.points);
                    pad.calc_normal_vector(&mut un, ksi4)?;
                    pad.calc_coords(&mut x, ksi4)?;
                    self.canvas_normals_2d
                        .draw_arrow(x[0], x[1], x[0] + s * un[0], x[1] + s * un[1]);
                }
            }
            self.plot.add(&self.canvas_normals_2d);
        } else {
            for (face_key, face) in &features.faces {
                let ncell = features.all_faces.get(face_key).unwrap().len();
                if ncell == 1 {
                    // boundary faces only
                    let mut pad = Scratchpad::new(ndim, face.kind)?;
                    features.mesh.set_pad(&mut pad, &face.points);
                    if face.kind.class() == GeoClass::Tri {
                        pad.calc_normal_vector(&mut un, ksi3)?;
                        pad.calc_coords(&mut x, ksi3)?;
                    } else {
                        pad.calc_normal_vector(&mut un, ksi4)?;
                        pad.calc_coords(&mut x, ksi4)?;
                    }
                    self.canvas_normals_3d.polyline_3d_begin();
                    self.canvas_normals_3d.polyline_3d_add(x[0], x[1], x[2]);
                    self.canvas_normals_3d
                        .polyline_3d_add(x[0] + s * un[0], x[1] + s * un[1], x[2] + s * un[2]);
                    self.canvas_normals_3d.polyline_3d_end();
                }
            }
            for cell_id in &features.shells {
                let cell = &features.mesh.cells[*cell_id];
                let mut pad = Scratchpad::new(ndim, cell.kind)?;
                features.mesh.set_pad(&mut pad, &cell.points);
                if cell.kind.class() == GeoClass::Tri {
                    pad.calc_normal_vector(&mut un, ksi3)?;
                    pad.calc_coords(&mut x, ksi3)?;
                } else {
                    pad.calc_normal_vector(&mut un, ksi4)?;
                    pad.calc_coords(&mut x, ksi4)?;
                }
                self.canvas_normals_3d.polyline_3d_begin();
                self.canvas_normals_3d.polyline_3d_add(x[0], x[1], x[2]);
                self.canvas_normals_3d
                    .polyline_3d_add(x[0] + s * un[0], x[1] + s * un[1], x[2] + s * un[2]);
                self.canvas_normals_3d.polyline_3d_end();
            }
            self.plot.add(&self.canvas_normals_3d);
        }
        Ok(())
    }

    /// Draw boundary edges (3D)
    pub fn boundary_edges_3d(&mut self, features: &Features) {
        if features.mesh.ndim == 3 {
            let edge_keys = features.get_boundary_edges();
            if edge_keys.len() > 0 {
                for (i, j) in &edge_keys {
                    let a = &features.mesh.points[*i].coords;
                    let b = &features.mesh.points[*j].coords;
                    self.canvas_boundary_edges_3d.polyline_3d_begin();
                    self.canvas_boundary_edges_3d.polyline_3d_add(a[0], a[1], a[2]);
                    self.canvas_boundary_edges_3d.polyline_3d_add(b[0], b[1], b[2]);
                    self.canvas_boundary_edges_3d.polyline_3d_end();
                }
                self.plot.add(&self.canvas_boundary_edges_3d);
            }
        }
    }

    /// Draw boundary faces (3D)
    pub fn boundary_faces(&mut self, features: &Features) {
        if features.mesh.ndim == 3 {
            let res = features.triangulate_3d_boundary();
            if res.triangles.len() > 0 {
                self.canvas_boundary_faces
                    .draw_triangles_3d(&res.xx, &res.yy, &res.zz, &res.triangles);
                self.plot.add(&self.canvas_boundary_faces);
            }
        }
    }

    /// Configures the plot range
    fn config_plot_range(&mut self, mesh: &Mesh) {
        let (xmin, xmax) = mesh.get_limits();
        let dx = xmax[0] - xmin[0];
        let dy = xmax[1] - xmin[1];
        if dx > 0.0 && dy > 0.0 {
            let gx = self.m_range * dx;
            let gy = self.m_range * dy;
            self.plot
                .set_range(xmin[0] - gx, xmax[0] + gx, xmin[1] - gy, xmax[1] + gy);
        }
    }

    /// Draws the mesh
    ///
    /// # Input
    ///
    /// * `mesh` -- the mesh to be drawn
    /// * `filepath` -- may be a String, &str, or Path
    ///
    /// # Examples
    ///
    /// ```
    /// use gemlab::mesh::{Draw, Samples};
    /// use gemlab::StrError;
    /// use plotpy::Canvas;
    ///
    /// const SAVE_FIGURE: bool = false;
    ///
    /// fn main() -> Result<(), StrError> {
    ///     if SAVE_FIGURE {
    ///         // use sample mesh
    ///         let mesh = Samples::ring_eight_qua8_rad1_thick1();
    ///
    ///         // draw circles
    ///         let mut circle_in = Canvas::new();
    ///         let mut circle_out = Canvas::new();
    ///         circle_in
    ///             .set_face_color("None")
    ///             .set_edge_color("#ff000080")
    ///             .set_line_width(7.0)
    ///             .draw_circle(0.0, 0.0, 1.0);
    ///         circle_out
    ///             .set_face_color("None")
    ///             .set_edge_color("#0000ff80")
    ///             .set_line_width(7.0)
    ///             .draw_circle(0.0, 0.0, 2.0);
    ///
    ///         // draw mesh
    ///         let mut draw = Draw::new();
    ///         draw.extra(|plot, before| {
    ///             if !before {
    ///                 plot.add(&circle_in);
    ///                 plot.add(&circle_out);
    ///             }
    ///         })
    ///         .show_cell_ids(true)
    ///         .all(&mesh, "/tmp/gemlab/doc_example_mesh_draw.svg")?;
    ///     }
    ///     Ok(())
    /// }
    /// ```
    ///
    /// ![doc_example_mesh_draw](https://raw.githubusercontent.com/cpmech/gemlab/main/data/figures/doc_example_mesh_draw.svg)
    pub fn all<P>(&mut self, mesh: &Mesh, filepath: &P) -> Result<(), StrError>
    where
        P: AsRef<OsStr> + ?Sized,
    {
        if let Some(callback) = self.extra.as_ref() {
            callback(&mut self.plot, true);
        }
        if self.show_cells {
            self.cells(mesh)?;
        }
        if self.show_cell_ids {
            self.cell_ids(mesh)?;
        }
        if self.show_point_dots {
            self.point_dots(mesh);
        }
        if self.show_point_ids {
            self.point_ids(mesh);
        }
        if self.show_edge_markers
            || self.show_face_markers
            || self.show_normal_vectors
            || self.show_boundary_edges_3d
            || self.show_boundary_faces
        {
            let features = Features::new(mesh, false);
            if self.show_edge_markers {
                self.edge_markers(&features)?;
            }
            if self.show_face_markers {
                self.face_markers(&features)?;
            }
            if self.show_normal_vectors {
                self.normal_vectors(&features)?;
            }
            if self.show_boundary_faces {
                self.boundary_faces(&features);
            }
            if self.show_boundary_edges_3d {
                self.boundary_edges_3d(&features);
            }
        }
        if mesh.ndim == 2 {
            self.plot.grid_and_labels("x", "y");
        }
        if mesh.ndim == 2 {
            if let Some((xmin, xmax, ymin, ymax)) = self.range_2d {
                self.plot.set_range(xmin, xmax, ymin, ymax);
            } else {
                self.config_plot_range(mesh);
            }
        } else {
            if let Some((xmin, xmax, ymin, ymax, zmin, zmax)) = self.range_3d {
                self.plot.set_range_3d(xmin, xmax, ymin, ymax, zmin, zmax);
            } else {
                self.config_plot_range(mesh);
            }
        }
        if !self.unequal_exes {
            self.plot.set_equal_axes(true);
        }
        if let Some((width, height)) = self.size {
            self.plot.set_figure_size_points(width, height);
        }
        if let Some(callback) = self.extra.as_ref() {
            callback(&mut self.plot, false);
        }
        if let Some(((xmin, xmax, ymin, ymax), (u0, v0, w, h))) = self.zoom_2d {
            let mut inset = InsetAxes::new();
            inset
                .set_indicator_line_color(&self.zoom_indicator_config.0)
                .set_indicator_alpha(self.zoom_indicator_config.1)
                .set_indicator_line_width(self.zoom_indicator_config.2);
            inset.add(&self.canvas_cells);
            if self.show_cell_ids {
                inset.add(&self.canvas_cell_ids);
            }
            if self.show_point_dots {
                inset.add(&self.canvas_point_dots);
            }
            if self.show_point_ids {
                inset.add(&self.canvas_point_ids);
            }
            inset.set_range(xmin, xmax, ymin, ymax).draw(u0, v0, w, h);
            if let Some(callback) = self.zoom_extra.as_ref() {
                callback(&mut inset);
            }
            self.plot.add(&inset);
        }
        if mesh.ndim == 3 {
            self.plot.set_camera(self.camera_elevation, self.camera_azimuth);
        }
        if self.hide_3d_grid && mesh.ndim == 3 {
            self.plot.set_hide_3d_grid(true);
        }
        if self.hide_axes {
            self.plot.set_hide_axes(true);
        }
        if self.show_glyph_3d && mesh.ndim == 3 {
            self.canvas_glyph_3d.set_glyph_size(self.glyph_3d_size);
            self.canvas_glyph_3d.draw_glyph_3d(
                self.glyph_3d_origin[0],
                self.glyph_3d_origin[1],
                self.glyph_3d_origin[2],
            );
            self.plot.add(&self.canvas_glyph_3d);
        }
        if self.view {
            self.plot.show(filepath)
        } else {
            self.plot.save(filepath)
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::Draw;
    use crate::mesh::{Features, Mesh, Samples};
    use plotpy::{Canvas, Plot, Text};

    const SAVE_FIGURE: bool = false;

    fn labels_and_caption() -> (Text, Text) {
        // labels for cell local ids
        let mut labels = Text::new();
        labels
            .set_color("#17af14")
            .set_align_vertical("bottom")
            .set_align_horizontal("center");

        // captions such as Lin3, Lin4, Tri3, ...
        let mut caption = Text::new();
        caption
            .set_fontsize(12.0)
            .set_align_horizontal("center")
            .set_align_vertical("top")
            .set_bbox(true)
            .set_bbox_style("round4,pad=0.3,rounding_size=0.15")
            .set_bbox_edgecolor("None")
            .set_bbox_facecolor("#57e986");
        (labels, caption)
    }

    fn draw_cell_local_ids(plot: &mut Plot, labels: &mut Text, mesh: &Mesh, dx: f64, dy: f64, dz: f64) {
        for cell in &mesh.cells {
            for m in 0..cell.points.len() {
                let (x, y) = (
                    mesh.points[cell.points[m]].coords[0],
                    mesh.points[cell.points[m]].coords[1],
                );
                if mesh.ndim == 2 {
                    labels.draw(x + dx, y + dy, format!("{}", m).as_str());
                } else {
                    let z = mesh.points[cell.points[m]].coords[2];
                    labels.draw_3d(x + dx, y + dy, z + dz, format!("{}", m).as_str());
                }
            }
        }
        plot.add(labels);
    }

    #[test]
    fn draw_cells_and_points_work() {
        // lin cells ---------------------------------------------------------------------------
        let mesh = Samples::lin_cells();
        let mut draw = Draw::new();
        draw.cells(&mesh).unwrap();
        draw.point_dots(&mesh);

        if SAVE_FIGURE {
            let (mut labels, mut caption) = labels_and_caption();
            draw_cell_local_ids(&mut draw.plot, &mut labels, &mesh, 0.0, 0.05, 0.0);
            caption.draw(0.6, -0.05, "Lin2");
            caption.draw(1.4 + 0.6, -0.05, "Lin3");
            caption.draw(2.8 + 0.6, -0.05, "Lin4");
            caption.draw(4.2 + 0.6, -0.05, "Lin5");
            draw.plot.add(&caption);
            draw.plot
                .set_figure_size_points(600.0, 600.0)
                .set_frame_borders(false)
                .set_hide_axes(true)
                .set_equal_axes(true)
                .set_range(-0.1, 5.6, -0.3, 1.5)
                .save("/tmp/gemlab/test_draw_cells_and_points_work_1_lin.svg")
                .unwrap();
        }

        // lin cells in 3d ---------------------------------------------------------------------
        let mesh = Samples::lin_cells_3d();
        let mut draw = Draw::new();
        draw.cells(&mesh).unwrap();
        draw.point_dots(&mesh);

        if SAVE_FIGURE {
            let (mut labels, mut caption) = labels_and_caption();
            draw_cell_local_ids(&mut draw.plot, &mut labels, &mesh, 0.0, 0.05, 0.0);
            caption.draw_3d(1.2, 1.33, 1.33, "Lin2");
            caption.draw_3d(1.4 + 1.2, 1.33, 1.33, "Lin3");
            caption.draw_3d(2.8 + 1.2, 1.33, 1.33, "Lin4");
            caption.draw_3d(4.2 + 1.2, 1.33, 1.33, "Lin5");
            draw.plot.add(&caption);
            draw.plot
                .set_figure_size_points(600.0, 600.0)
                .set_equal_axes(true)
                .set_range_3d(0.0, 5.3, 0.0, 1.2, 0.0, 1.2)
                .save("/tmp/gemlab/test_draw_cells_and_points_work_1_lin_3d.svg")
                .unwrap();
        }

        // tri cells ---------------------------------------------------------------------------
        let mesh = Samples::tri_cells();
        let mut draw = Draw::new();
        draw.cells(&mesh).unwrap();
        draw.point_dots(&mesh);

        if SAVE_FIGURE {
            let (mut labels, mut caption) = labels_and_caption();
            draw_cell_local_ids(&mut draw.plot, &mut labels, &mesh, 0.0, 0.02, 0.0);
            caption.draw(0.5, -0.1, "Tri3");
            caption.draw(1.7, -0.1, "Tri6");
            caption.draw(0.5, 1.1, "Tri10");
            caption.draw(1.7, 1.1, "Tri15");
            draw.plot.add(&caption);
            draw.plot
                .set_figure_size_points(600.0, 600.0)
                .set_frame_borders(false)
                .set_hide_axes(true)
                .set_equal_axes(true)
                .set_range(-0.1, 2.3, -0.2, 2.2)
                .save("/tmp/gemlab/test_draw_cells_and_points_work_2_tri.svg")
                .unwrap();
        }

        // qua cells ---------------------------------------------------------------------------
        let mesh = Samples::qua_cells();
        let mut draw = Draw::new();
        draw.cells(&mesh).unwrap();
        draw.point_dots(&mesh);

        if SAVE_FIGURE {
            let (mut labels, mut caption) = labels_and_caption();
            draw_cell_local_ids(&mut draw.plot, &mut labels, &mesh, -0.02, 0.02, 0.0);
            caption.draw(0.4, -0.06, "Qua4");
            caption.draw(1.5, -0.06, "Qua8");
            caption.draw(2.6, -0.06, "Qua9");
            caption.draw(0.4, 1.1, "Qua12");
            caption.draw(1.5, 1.1, "Qua16");
            caption.draw(2.6, 1.1, "Qua17");
            draw.plot.add(&caption);
            draw.plot
                .set_figure_size_points(600.0, 600.0)
                .set_frame_borders(false)
                .set_hide_axes(true)
                .set_equal_axes(true)
                .set_range(-0.15, 3.1, -0.25, 2.15)
                .save("/tmp/gemlab/test_draw_cells_and_points_work_3_qua.svg")
                .unwrap();
        }

        // tet cells ---------------------------------------------------------------------------
        let mesh = Samples::tet_cells();
        let mut draw = Draw::new();
        draw.cells(&mesh).unwrap();
        draw.point_dots(&mesh);

        if SAVE_FIGURE {
            let (mut labels, mut caption) = labels_and_caption();
            draw_cell_local_ids(&mut draw.plot, &mut labels, &mesh, 0.03, 0.0, 0.03);
            caption.draw_3d(0.5, -0.15, 0.0, "Tet4");
            caption.draw_3d(1.7, -0.15, 0.0, "Tet10");
            caption.draw_3d(1.1, 1.05, 0.8, "Tet20");
            draw.plot.add(&caption);
            draw.plot
                .set_figure_size_points(600.0, 600.0)
                .set_equal_axes(true)
                .set_range_3d(0.0, 2.2, 0.0, 2.2, 0.0, 2.2)
                .save("/tmp/gemlab/test_draw_cells_and_points_work_4_tet.svg")
                .unwrap();
        }

        // hex cells ---------------------------------------------------------------------------
        let mesh = Samples::hex_cells();
        let mut draw = Draw::new();
        draw.cells(&mesh).unwrap();
        draw.point_dots(&mesh);

        if SAVE_FIGURE {
            let (mut labels, mut caption) = labels_and_caption();
            draw_cell_local_ids(&mut draw.plot, &mut labels, &mesh, 0.03, 0.0, 0.03);
            caption.draw_3d(0.5, -0.25, 0.0, "Hex8");
            caption.draw_3d(2.3, -0.25, 0.0, "Hex20");
            caption.draw_3d(0.3, 1.1, 2.0, "Hex32");
            draw.plot.add(&caption);
            draw.plot
                .set_figure_size_points(600.0, 600.0)
                .set_equal_axes(true)
                .set_range_3d(0.0, 3.0, 0.0, 3.0, 0.0, 2.5)
                .save("/tmp/gemlab/test_draw_cells_and_points_work_5_hex.svg")
                .unwrap();
        }

        // ring --------------------------------------------------------------------------------
        let mesh = Samples::ring_eight_qua8_rad1_thick1();
        let mut draw = Draw::new();
        draw.cells(&mesh).unwrap();
        draw.point_dots(&mesh);

        if SAVE_FIGURE {
            let mut circle_in = Canvas::new();
            let mut circle_mi = Canvas::new();
            let mut circle_ou = Canvas::new();
            circle_in
                .set_face_color("None")
                .set_edge_color("#bfbfbfbb")
                .set_line_width(7.0)
                .draw_circle(0.0, 0.0, 1.0);
            circle_mi
                .set_face_color("None")
                .set_edge_color("#bfbfbfbb")
                .set_line_width(7.0)
                .draw_circle(0.0, 0.0, 1.5);
            circle_ou
                .set_face_color("None")
                .set_edge_color("#bfbfbfbb")
                .set_line_width(7.0)
                .draw_circle(0.0, 0.0, 2.0);
            draw.plot.add(&circle_in);
            draw.plot.add(&circle_mi);
            draw.plot.add(&circle_ou);
            draw.plot
                .set_figure_size_points(600.0, 600.0)
                .set_equal_axes(true)
                .set_range(-0.1, 2.1, -0.1, 2.1)
                .save("/tmp/gemlab/test_draw_cells_and_points_work_6_ring.svg")
                .unwrap();
        }
    }

    #[test]
    fn draw_works_qua12() {
        if SAVE_FIGURE {
            let mesh = Samples::block_2d_four_qua12();
            let mut draw = Draw::new();
            draw.show_cell_ids(true)
                .show_point_ids(true)
                .show_point_dots(true)
                .set_range_2d(-0.5, 6.0, -0.5, 6.0)
                .zoom_2d(-0.05, 1.55, -0.05, 1.55, 0.6, 0.6, 0.3, 0.3)
                .zoom_extra(|inset| {
                    let mut text = Text::new();
                    text.draw(0.3, 1.0, "HELLO");
                    inset.add(&text);
                })
                .all(&mesh, "/tmp/gemlab/test_draw_works_qua12.svg")
                .unwrap();
        }
    }

    #[test]
    fn draw_works_qua16() {
        if SAVE_FIGURE {
            let mesh = Samples::block_2d_four_qua16();
            let mut draw = Draw::new();
            draw.show_cell_ids(true)
                .show_point_ids(true)
                .show_point_dots(true)
                .all(&mesh, "/tmp/gemlab/test_draw_works_qua16.svg")
                .unwrap();
        }
    }

    #[test]
    fn draw_works_qua17() {
        if SAVE_FIGURE {
            let mesh = Samples::block_2d_four_qua17();
            let mut draw = Draw::new();
            draw.show_cell_ids(true)
                .show_point_ids(true)
                .show_point_dots(true)
                .all(&mesh, "/tmp/gemlab/test_draw_works_qua17.svg")
                .unwrap();
        }
    }

    #[test]
    fn draw_works_mixed_2d() {
        if SAVE_FIGURE {
            let mesh = Samples::mixed_shapes_2d();
            let mut draw = Draw::new();
            draw.show_cell_ids(true)
                .show_point_ids(true)
                .show_point_dots(true)
                .all(&mesh, "/tmp/gemlab/test_draw_works_mixed_2d.svg")
                .unwrap();
        }
    }

    #[test]
    fn draw_works_hex8() {
        if SAVE_FIGURE {
            let mesh = Samples::two_hex8();
            let mut draw = Draw::new();
            draw.show_cell_ids(true)
                .show_point_ids(true)
                .show_point_dots(true)
                .set_size(600.0, 600.0);
            draw.get_canvas_point_ids()
                .set_align_horizontal("left")
                .set_align_vertical("bottom")
                .set_color("black")
                .set_fontsize(10.0)
                .set_bbox_facecolor("gold")
                .set_bbox_alpha(0.5);
            draw.all(&mesh, "/tmp/gemlab/test_draw_works_hex8.svg").unwrap();
        }
    }

    #[test]
    fn draw_works_hex20() {
        if SAVE_FIGURE {
            let mesh = Samples::block_3d_eight_hex20();
            let mut draw = Draw::new();
            draw.show_cell_ids(true)
                .show_point_ids(true)
                .show_point_dots(true)
                .set_size(600.0, 600.0);
            draw.get_canvas_point_ids()
                .set_align_horizontal("left")
                .set_align_vertical("bottom")
                .set_color("black")
                .set_fontsize(10.0)
                .set_bbox_facecolor("gold")
                .set_bbox_alpha(0.5);
            draw.all(&mesh, "/tmp/gemlab/test_draw_works_hex20.svg").unwrap();
        }
    }

    #[test]
    fn draw_works_mixed_3d() {
        if SAVE_FIGURE {
            let mesh = Samples::mixed_shapes_3d();
            let mut draw = Draw::new();
            draw.show_cell_ids(true)
                .show_point_ids(true)
                .show_point_dots(true)
                .show_normal_vectors(true)
                .set_m_normal_vector(0.08)
                .set_m_range(0.0)
                .set_glyph_3d(1.8, 0.2, 0.0, 0.25)
                .set_hide_axes(true)
                .set_hide_3d_grid(true)
                .set_range_3d(0.0, 1.0, -0.5, 2.0, 0.0, 1.0)
                .set_size(800.0, 800.0)
                .set_camera(30.0, 30.0)
                .set_view_flag(false);
            draw.get_canvas_point_ids()
                .set_align_horizontal("left")
                .set_align_vertical("bottom")
                .set_color("black")
                .set_fontsize(10.0)
                .set_bbox_facecolor("gold")
                .set_bbox_alpha(0.5);
            draw.all(&mesh, "/tmp/gemlab/test_draw_works_mixed_3d.svg").unwrap();
        }
    }

    #[test]
    fn test_figure_setters() {
        let mut draw = Draw::new();

        // Test show_cell_ids
        assert!(!draw.show_cell_ids);
        draw.show_cell_ids(true);
        assert!(draw.show_cell_ids);

        // Test show_cell_att
        assert!(draw.show_cell_marker);
        draw.show_cell_marker(false);
        assert!(!draw.show_cell_marker);

        // Test show_point_ids
        assert!(!draw.show_point_ids);
        draw.show_point_ids(true);
        assert!(draw.show_point_ids);

        // Test show_point_marker
        assert!(!draw.show_point_marker);
        draw.show_point_marker(true);
        assert!(draw.show_point_marker);

        // Test show_point_dots
        assert!(!draw.show_point_dots);
        draw.show_point_dots(true);
        assert!(draw.show_point_dots);

        // Test unequal_axes
        assert!(!draw.unequal_exes);
        draw.set_unequal_exes(true);
        assert!(draw.unequal_exes);

        // Test range_2d
        assert!(draw.range_2d.is_none());
        draw.set_range_2d(-1.0, 1.0, -2.0, 2.0);
        assert_eq!(draw.range_2d, Some((-1.0, 1.0, -2.0, 2.0)));

        // Test range_3d
        assert!(draw.range_3d.is_none());
        draw.set_range_3d(-1.0, 1.0, -2.0, 2.0, -3.0, 3.0);
        assert_eq!(draw.range_3d, Some((-1.0, 1.0, -2.0, 2.0, -3.0, 3.0)));

        // Test size
        assert!(draw.size.is_none());
        draw.set_size(800.0, 600.0);
        assert_eq!(draw.size, Some((800.0, 600.0)));

        // Test extra
        assert!(draw.extra.is_none());
        draw.extra(|_, _| {});
        assert!(draw.extra.is_some());

        // Test zoom_2d
        assert!(draw.zoom_2d.is_none());
        draw.zoom_2d(-0.5, 0.5, -1.0, 1.0, 0.1, 0.2, 0.3, 0.4);
        assert_eq!(draw.zoom_2d, Some(((-0.5, 0.5, -1.0, 1.0), (0.1, 0.2, 0.3, 0.4))));

        // Test disable_zoom_2d
        draw.disable_zoom_2d();
        assert!(draw.zoom_2d.is_none());

        // Test zoom_indicator
        draw.zoom_indicator("#ff0000", 0.8, 2.5);
        assert_eq!(draw.zoom_indicator_config, ("#ff0000".to_string(), 0.8, 2.5));

        // Test zoom_extra
        assert!(draw.zoom_extra.is_none());
        draw.zoom_extra(|_| {});
        assert!(draw.zoom_extra.is_some());

        // Test method chaining
        let mut draw = Draw::new();
        draw.show_cell_ids(true)
            .show_point_ids(true)
            .show_point_dots(true)
            .set_size(800.0, 600.0)
            .set_range_2d(-1.0, 1.0, -1.0, 1.0);
        assert!(draw.show_cell_ids);
        assert!(draw.show_point_ids);
        assert!(draw.show_point_dots);
        assert_eq!(draw.size, Some((800.0, 600.0)));
        assert_eq!(draw.range_2d, Some((-1.0, 1.0, -1.0, 1.0)));
    }

    #[test]
    fn draw_edge_markers_works() {
        if SAVE_FIGURE {
            let mesh = Samples::two_qua4();
            let features = Features::new(&mesh, false);
            let mut draw = Draw::new();
            draw.cells(&mesh).unwrap();
            draw.point_dots(&mesh);
            draw.edge_markers(&features).unwrap();
            draw.plot
                .set_equal_axes(true)
                .save("/tmp/gemlab/test_draw_edge_markers_works_1.svg")
                .unwrap();

            let mesh = Samples::ring_eight_qua8_rad1_thick1();
            let features = Features::new(&mesh, false);
            let mut draw = Draw::new();
            draw.cells(&mesh).unwrap();
            draw.point_dots(&mesh);
            draw.edge_markers(&features).unwrap();
            draw.plot
                .set_equal_axes(true)
                .set_figure_size_points(600.0, 600.0)
                .save("/tmp/gemlab/test_draw_edge_markers_works_2.svg")
                .unwrap();

            let mesh = Samples::two_hex8();
            let features = Features::new(&mesh, true);
            let mut draw = Draw::new();
            draw.cells(&mesh).unwrap();
            draw.point_dots(&mesh);
            draw.edge_markers(&features).unwrap();
            draw.plot
                .set_equal_axes(true)
                .set_figure_size_points(600.0, 600.0)
                .save("/tmp/gemlab/test_draw_edge_markers_works_3.svg")
                .unwrap();
        }
    }

    #[test]
    fn draw_normals_works() {
        if SAVE_FIGURE {
            let mesh = Samples::two_qua4();
            let features = Features::new(&mesh, false);
            let mut draw = Draw::new();
            draw.cells(&mesh).unwrap();
            draw.point_dots(&mesh);
            draw.normal_vectors(&features).unwrap();
            draw.plot
                .set_equal_axes(true)
                .save("/tmp/gemlab/test_draw_normals_works_1.svg")
                .unwrap();

            let mesh = Samples::ring_eight_qua8_rad1_thick1();
            let features = Features::new(&mesh, false);
            let mut draw = Draw::new();
            draw.cells(&mesh).unwrap();
            draw.point_dots(&mesh);
            draw.normal_vectors(&features).unwrap();
            draw.plot
                .set_equal_axes(true)
                .set_figure_size_points(600.0, 600.0)
                .save("/tmp/gemlab/test_draw_normals_works_2.svg")
                .unwrap();

            let mesh = Samples::two_hex8();
            let features = Features::new(&mesh, true);
            let mut draw = Draw::new();
            draw.cells(&mesh).unwrap();
            draw.point_dots(&mesh);
            draw.normal_vectors(&features).unwrap();
            draw.plot
                .set_equal_axes(true)
                .set_figure_size_points(600.0, 600.0)
                // .show("/tmp/gemlab/test_draw_normals_works_3.svg")
                .save("/tmp/gemlab/test_draw_normals_works_3.svg")
                .unwrap();
        }
    }

    #[test]
    fn draw_face_markers_works() {
        if SAVE_FIGURE {
            let mesh = Samples::two_hex8();
            let features = Features::new(&mesh, true);
            let mut draw = Draw::new();
            draw.cells(&mesh).unwrap();
            draw.point_dots(&mesh);
            draw.face_markers(&features).unwrap();
            draw.plot
                .set_equal_axes(true)
                .set_figure_size_points(600.0, 600.0)
                // .show("/tmp/gemlab/test_draw_face_markers_works.svg")
                .save("/tmp/gemlab/test_draw_face_markers_works.svg")
                .unwrap();
        }
    }

    #[test]
    fn draw_boundary_edges_works() {
        if SAVE_FIGURE {
            // let mesh = Samples::two_hex8();
            let mesh = Samples::block_3d_eight_hex20();
            let features = Features::new(&mesh, true);
            let mut draw = Draw::new();
            draw.boundary_edges_3d(&features);
            draw.point_dots(&mesh);
            draw.plot
                .set_equal_axes(true)
                .set_figure_size_points(800.0, 800.0)
                // .show("/tmp/gemlab/test_draw_boundary_edges_works.svg")
                .save("/tmp/gemlab/test_draw_boundary_edges_works.svg")
                .unwrap();
        }
    }

    #[test]
    fn draw_boundary_faces_works() {
        if SAVE_FIGURE {
            // let mesh = Samples::two_hex8();
            let mesh = Samples::block_3d_eight_hex20();
            let features = Features::new(&mesh, true);
            let mut draw = Draw::new();
            // draw.cells(&mesh).unwrap();
            // draw.point_dots(&mesh);
            draw.boundary_faces(&features);
            draw.boundary_edges_3d(&features);
            draw.plot
                .set_equal_axes(true)
                .set_figure_size_points(800.0, 800.0)
                // .show("/tmp/gemlab/test_draw_boundary_faces_works.svg")
                .save("/tmp/gemlab/test_draw_boundary_faces_works.svg")
                .unwrap();
        }
    }
}