summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/luamesh/luamesh-tex.lua
blob: 34755092ed4ef190e2376b66fe739d8874f89172 (plain)
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
-----------------------------------------------------------------------
--         FILE:  luamesh-tex.lua
--  DESCRIPTION:  functions of luamesh package to produce LaTeX code
--       AUTHOR:  Maxime Chupin
-----------------------------------------------------------------------
-- trace Voronoi with MP
function traceVoronoiMP(listPoints, triangulation,listVoronoi, points, tri,styleD,styleV)
   if(styleD == "dashed") then
      sDelaunay = "dashed evenly"
   else
      sDelaunay = ""
   end
   if(styleV == "dashed") then
      sVoronoi = "dashed evenly"
   else
      sVoronoi = ""
   end
   listCircumC = listCircumCenter(listPoints,triangulation)
   output = "";
   output = output .. " pair MeshPoints[];"
   for i=1,#listPoints do
      output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
   end
   output = output .. " pair CircumCenters[];"
   for i=1,#listCircumC do
      output = output .. "CircumCenters[".. i .. "] = (" .. listCircumC[i].x .. "," .. listCircumC[i].y .. ")*u;"
   end
   if(tri=="show") then
      for i=1,#triangulation do
         PointI = listPoints[triangulation[i][1]]
         PointJ = listPoints[triangulation[i][2]]
         PointK = listPoints[triangulation[i][3]]
         if(triangulation[i].type == "bbox") then
            output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle "..sDelaunay.." withcolor \\luameshmpcolorBbox;"
         else
            output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle "..sDelaunay.." withcolor \\luameshmpcolor;"
         end
      end
   end
   for i=1,#listVoronoi do
      PointI = listCircumC[listVoronoi[i][1]]
      PointJ = listCircumC[listVoronoi[i][2]]
      output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u "..sVoronoi.." withcolor \\luameshmpcolorVoronoi;"
   end
   if(points=="points") then
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
            j=j+1
         else
            output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
         end
      end
      for i=1,#listCircumC do
         output = output .. "dotlabel.llft (btex $\\CircumPoint_{" .. i .. "}$ etex, (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ")*u ) withcolor \\luameshmpcolorVoronoi ;"
      end
   else
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
            j=j+1
         else
            output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolor withpen pencircle scaled 3;"
         end
      end
      for i=1,#listCircumC do
         output = output .. "drawdot  (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ")*u  withcolor \\luameshmpcolorVoronoi withpen pencircle scaled 3;"
      end
   end

   return output
end


-- trace Voronoi with TikZ
function traceVoronoiTikZ(listPoints, triangulation,listVoronoi, points, tri,color,colorBbox,colorVoronoi,styleD,styleV)
   if(styleD == "dashed") then
      sDelaunay = ",dashed"
   else
      sDelaunay = ""
   end
   if(styleV == "dashed") then
      sVoronoi = ",dashed"
   else
      sVoronoi = ""
   end
   listCircumC = listCircumCenter(listPoints,triangulation)
    output = ""
   for i=1,#listPoints do
      output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
   end
   for i=1,#listCircumC do
      output = output .. "\\coordinate (CircumPoints".. i .. ") at  (" .. listCircumC[i].x .. "," .. listCircumC[i].y .. ");"
   end
   if(tri=="show") then
      for i=1,#triangulation do
         PointI = listPoints[triangulation[i][1]]
         PointJ = listPoints[triangulation[i][2]]
         PointK = listPoints[triangulation[i][3]]
         if(triangulation[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox..sDelaunay.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
         else
            output = output .. "\\draw[color="..color..sDelaunay.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
         end
      end
   end
   for i=1,#listVoronoi do
      PointI = listCircumC[listVoronoi[i][1]]
      PointJ = listCircumC[listVoronoi[i][2]]
      output = output .. "\\draw[color="..colorVoronoi..sVoronoi.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..");"
   end
   if(points=="points") then
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
            j=j+1
         else
            output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
         end
      end
      for i=1,#listCircumC do
         output = output .. "\\draw[color="..colorVoronoi.."] (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\CircumPoint_{" .. i .. "}$};"
      end
   else
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
            j=j+1
         else
            output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
         end
      end
      for i=1,#listCircumC do
         output = output .. "\\draw[color="..colorVoronoi.."] (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ") node {$\\bullet$};"
      end
   end
   return output
end



-- buildVoronoi with MP
function buildVoronoiMPBW(chaine,mode,points,bbox,scale,tri,styleD,styleV)
   local listPoints = buildList(chaine, mode)
   local triangulation = BowyerWatson(listPoints,bbox)
   local listVoronoi = buildVoronoi(listPoints, triangulation)
   output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri,styleD,styleV)
   output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
   tex.sprint(output)
end


-- buildVoronoi with TikZ
function buildVoronoiTikZBW(chaine,mode,points,bbox,scale,tri,color,colorBbox,colorVoronoi,styleD,styleV)
   local listPoints = buildList(chaine, mode)
   local triangulation = BowyerWatson(listPoints,bbox)
   local listVoronoi = buildVoronoi(listPoints, triangulation)
   output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi,styleD,styleV)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"   tex.sprint(output)
end


-- buildVoronoi with MP
function buildVoronoiMPBWinc(chaine,beginning, ending,mode,points,bbox,scale,tri,styleD,styleV)
   local listPoints = buildList(chaine, mode)
   local triangulation = BowyerWatson(listPoints,bbox)
   local listVoronoi = buildVoronoi(listPoints, triangulation)
   output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri,styleD,styleV)
   output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
   tex.sprint(output)
end


-- buildVoronoi with TikZ
function buildVoronoiTikZBWinc(chaine,beginning, ending,mode,points,bbox,scale,tri,color,colorBbox,colorVoronoi)
   local listPoints = buildList(chaine, mode,styleD,styleV)
   local triangulation = BowyerWatson(listPoints,bbox)
   local listVoronoi = buildVoronoi(listPoints, triangulation)
   output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi,styleD,styleV)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
   tex.sprint(output)
end



-- trace a triangulation with TikZ
function traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
   output = ""
   for i=1,#listPoints do
      output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
   end
   for i=1,#triangulation do
      PointI = listPoints[triangulation[i][1]]
      PointJ = listPoints[triangulation[i][2]]
      PointK = listPoints[triangulation[i][3]]
      if(triangulation[i].type == "bbox") then
         output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
      else
         output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
      end
   end
   if(points=="points") then
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
            j=j+1
         else
            output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
         end
      end
   end
   if(points=="dotpoints") then
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$};"
            j=j+1
         else
            output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$};"
         end
      end
   end
   return output
end


-- trace a triangulation with MP
function traceMeshMP(listPoints, triangulation,points)
   output = "";
   output = output .. " pair MeshPoints[];"
   for i=1,#listPoints do
      output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
   end
   for i=1,#triangulation do
      PointI = listPoints[triangulation[i][1]]
      PointJ = listPoints[triangulation[i][2]]
      PointK = listPoints[triangulation[i][3]]
      if(triangulation[i].type == "bbox") then
         output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
      else
         output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
      end
   end
   if(points=="points") then
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
            j=j+1
         else
            output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
         end
      end
   end
   if(points=="dotpoints") then
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
            j=j+1
         else
            output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u withcolor \\luameshmpcolor withpen pencircle scaled 3;"
         end
      end
   end
   return output
end


-- buildMesh with MP
function buildMeshMPBW(chaine,mode,points,bbox,scale)
   local listPoints = buildList(chaine, mode)
   local triangulation = BowyerWatson(listPoints,bbox)
   output = traceMeshMP(listPoints, triangulation,points)
   output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
   tex.sprint(output)
end

-- buildMesh with MP include code
function buildMeshMPBWinc(chaine,beginning, ending,mode,points,bbox,scale)
   local listPoints = buildList(chaine, mode)
   local triangulation = BowyerWatson(listPoints,bbox)
   output = traceMeshMP(listPoints, triangulation,points)
   output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
   tex.sprint(output)
end

-- buildMesh with TikZ
function buildMeshTikZBW(chaine,mode,points,bbox,scale,color,colorBbox)
   local listPoints = buildList(chaine, mode)
   local triangulation = BowyerWatson(listPoints,bbox)
   output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
   tex.sprint(output)
end

-- buildMesh with TikZ
function buildMeshTikZBWinc(chaine,beginning, ending,mode,points,bbox,scale,color,colorBbox)
   local listPoints = buildList(chaine, mode)
   local triangulation = BowyerWatson(listPoints,bbox)
   output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
   tex.sprint(output)
end


-- print points of the mesh
function tracePointsMP(listPoints,points)
   output = "";
   output = output .. " pair MeshPoints[];"
   for i=1,#listPoints do
      output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
   end
   if(points=="points") then
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
            j=j+1
         else
            output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
         end
      end
   else
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
         else
            output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolor withpen pencircle scaled 3;"
         end
      end
   end
   return output
end

-- print points of the mesh
function tracePointsTikZ(listPoints,points,color,colorBbox)
   output = "";
   for i=1,#listPoints do
      output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
   end
   if(points=="points") then
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
            j = j+1
         else
            output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
         end
      end
   else
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
         else
            output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
         end
      end
   end
   return output
end

-- print points to mesh
function printPointsMP(chaine,mode,points,bbox,scale)
   local listPoints = buildList(chaine, mode)
   if(bbox == "bbox" ) then
      listPoints = buildBoundingBox(listPoints)
   end
   output = tracePointsMP(listPoints,points)
   output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
   tex.sprint(output)
end


-- print points to mesh
function printPointsMPinc(chaine,beginning, ending, mode,points,bbox,scale)
   local listPoints = buildList(chaine, mode)
   if(bbox == "bbox" ) then
      listPoints = buildBoundingBox(listPoints)
   end
   output = tracePointsMP(listPoints,points)
   output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
   tex.sprint(output)
end

-- print points to mesh
function printPointsTikZ(chaine,mode,points,bbox,scale,color,colorBbox)
   local listPoints = buildList(chaine, mode)
   if(bbox == "bbox" ) then
      listPoints = buildBoundingBox(listPoints)
   end
   output = tracePointsTikZ(listPoints,points,color,colorBbox)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
   tex.sprint(output)
end


-- print points to mesh
function printPointsTikZinc(chaine,beginning, ending, mode,points,bbox,scale,color,colorBbox)
   local listPoints = buildList(chaine, mode)
   if(bbox == "bbox" ) then
      listPoints = buildBoundingBox(listPoints)
   end
   output = tracePointsTikZ(listPoints,points,color,colorBbox)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
   tex.sprint(output)
end


-- buildMesh
function buildRect(largeur,a,b,nbrA, nbrB)
   local listPoints = rectangleList(a,b,nbrA,nbrB)
   local triangulation = BowyerWatson(listPoints,"none")
   traceTikZ(listPoints, triangulation,largeur,"none")
end


--
function TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack, colorNew, colorCircle,colorBbox)
   output = ""
   -- build the triangulation
   local triangulation = BowyerWatson(listPoints,bbox)
   local badTriangles = buildBadTriangles(P,triangulation,listPoints)
   for i=1,#listPoints do
      output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
   end
   if(step == "badT") then
      -- draw all triangle
      for i=1,#triangulation do
         PointI = listPoints[triangulation[i][1]]
         PointJ = listPoints[triangulation[i][2]]
         PointK = listPoints[triangulation[i][3]]
         if(triangulation[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
         else
            output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
         end
      end
      -- draw and fill the bad triangle
      for i=1,#badTriangles do
         PointI = listPoints[triangulation[badTriangles[i]][1]]
         PointJ = listPoints[triangulation[badTriangles[i]][2]]
         PointK = listPoints[triangulation[badTriangles[i]][3]]
         output = output .. "\\draw[fill="..colorBack.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
      end
      -- draw the circoncircle
      for i=1,#badTriangles do
         PointI = listPoints[triangulation[badTriangles[i]][1]]
         PointJ = listPoints[triangulation[badTriangles[i]][2]]
         PointK = listPoints[triangulation[badTriangles[i]][3]]
         center, radius = circoncircle(PointI, PointJ, PointK)
         output = output .. "\\draw[dashed, color="..colorCircle.."] ("..center.x .. "," .. center.y .. ") circle ("..radius ..");"
      end
      -- mark the points
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
            j = j+1
         else
            output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
         end
      end
      -- mark the point to add
      output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
   elseif(step == "cavity") then
      polygon = buildCavity(badTriangles, triangulation)
      polyNew = cleanPoly(polygon)
      -- remove the bad triangles
      for j=1,#badTriangles do
         table.remove(triangulation,badTriangles[j]-(j-1))
      end
      -- draw the triangles
      for i=1,#triangulation do
         PointI = listPoints[triangulation[i][1]]
         PointJ = listPoints[triangulation[i][2]]
         PointK = listPoints[triangulation[i][3]]
         if(triangulation[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
         else
            output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
         end
      end
      -- fill and draw the cavity
      path = ""
      for i=1,#polyNew do
         PointI = listPoints[polyNew[i]]
         path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
      end
      output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
      -- mark the points of the mesh
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
            j=j+1
         else
            output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
         end
      end
      -- mark the adding point
      output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
   elseif(step == "newT") then
      polygon = buildCavity(badTriangles, triangulation)
      polyNew = cleanPoly(polygon)
      -- remove the bad triangles
      for j=1,#badTriangles do
         table.remove(triangulation,badTriangles[j]-(j-1))
      end
      -- draw the triangle of the triangulation
      for i=1,#triangulation do
         PointI = listPoints[triangulation[i][1]]
         PointJ = listPoints[triangulation[i][2]]
         PointK = listPoints[triangulation[i][3]]
         if(triangulation[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
         else
            output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
         end
      end
      -- fill and draw the cavity
      path = ""
      for i=1,#polyNew do
         PointI = listPoints[polyNew[i]]
         path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
      end
      output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
      -- draw the new triangles composed by the edges of the polygon and the added point
      for i=1,#polygon do
         output = output .. "\\draw[color=TeXCluaMeshNewTikZ, thick]".."(".. listPoints[polygon[i][1]].x .. "," .. listPoints[polygon[i][1]].y .. ") -- (" .. listPoints[polygon[i][2]].x .. "," .. listPoints[polygon[i][2]].y ..");"
         output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][1]].x .. "," .. listPoints[polygon[i][1]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
         output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][2]].x .. "," .. listPoints[polygon[i][2]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
      end
      -- mark points
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
            j=j+1
         else
            output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
         end
      end
      -- mark the added point
      output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
   end
   return output
end

function TeXaddOnePointMPBW(listPoints,P,step,bbox)
   output = "";
   output = output .. "pair MeshPoints[];"
   -- build the triangulation
   local triangulation = {}
   local badTriangles = {}
   triangulation = BowyerWatson(listPoints,bbox)
   badTriangles = buildBadTriangles(P,triangulation,listPoints)
   for i=1,#listPoints do
      output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
   end
   if(step == "badT") then
      -- draw all triangle
      for i=1,#triangulation do
         PointI = listPoints[triangulation[i][1]]
         PointJ = listPoints[triangulation[i][2]]
         PointK = listPoints[triangulation[i][3]]
         if(triangulation[i].type == "bbox") then
            output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
         else
            output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
         end
      end
      -- draw and fill the bad triangle
      for i=1,#badTriangles do
         PointI = listPoints[triangulation[badTriangles[i]][1]]
         PointJ = listPoints[triangulation[badTriangles[i]][2]]
         PointK = listPoints[triangulation[badTriangles[i]][3]]
         output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
         output = output .. "fill (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBack;"
      end
      -- draw the circoncircle
      for i=1,#badTriangles do
         PointI = listPoints[triangulation[badTriangles[i]][1]]
         PointJ = listPoints[triangulation[badTriangles[i]][2]]
         PointK = listPoints[triangulation[badTriangles[i]][3]]
         center, radius = circoncircle(PointI, PointJ, PointK)
         output = output .. "draw fullcircle scaled ("..radius .."*2u) shifted ("..center.x .. "*u," .. center.y .. "*u) dashed evenly withcolor \\luameshmpcolorCircle;"
      end
      -- mark the points
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
            j=j+1
         else
            output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
         end
      end
      -- mark the point to add
      output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew;"
   elseif(step == "cavity") then
      polygon = buildCavity(badTriangles, triangulation)
      polyNew = cleanPoly(polygon)
      -- remove the bad triangles
      for j=1,#badTriangles do
         table.remove(triangulation,badTriangles[j]-(j-1))
      end
      -- draw the triangles
      for i=1,#triangulation do
         PointI = listPoints[triangulation[i][1]]
         PointJ = listPoints[triangulation[i][2]]
         PointK = listPoints[triangulation[i][3]]
         if(triangulation[i].type == "bbox") then
            output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
         else
            output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
         end
      end
      -- fill and draw the cavity
      path = ""
      for i=1,#polyNew do
         PointI = listPoints[polyNew[i]]
         path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
      end
      output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
      output = output .. "draw " .. path .. "cycle withcolor \\luameshmpcolorNew  withpen pencircle scaled 1pt;"
      -- mark the points of the mesh
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
            j=j+1
         else
            output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
         end
      end
      -- mark the adding point
      output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
   elseif(step == "newT") then
      polygon = buildCavity(badTriangles, triangulation)
      polyNew = cleanPoly(polygon)
      -- remove the bad triangles
      for j=1,#badTriangles do
         table.remove(triangulation,badTriangles[j]-(j-1))
      end
      -- draw the triangle of the triangulation
      for i=1,#triangulation do
         PointI = listPoints[triangulation[i][1]]
         PointJ = listPoints[triangulation[i][2]]
         PointK = listPoints[triangulation[i][3]]
         if(triangulation[i].type == "bbox") then
            output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
         else
            output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
         end
      end
      -- fill  the cavity
      path = ""
      for i=1,#polyNew do
         PointI = listPoints[polyNew[i]]
         path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
      end
      output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
      -- draw the new triangles composed by the edges of the polygon and the added point
      for i=1,#polygon do
         output = output .. "draw".."(".. listPoints[polygon[i][1]].x .. "," .. listPoints[polygon[i][1]].y .. ")*u -- (" .. listPoints[polygon[i][2]].x .. "," .. listPoints[polygon[i][2]].y ..")*u withcolor \\luameshmpcolorNew  withpen pencircle scaled 1pt;"
         output = output .. "draw".."(".. listPoints[polygon[i][1]].x .. "," .. listPoints[polygon[i][1]].y .. ")*u -- (" .. P.x .. "," .. P.y ..")*u withcolor \\luameshmpcolorNew withpen pencircle scaled 1pt;"
         output = output .. "draw".."(".. listPoints[polygon[i][2]].x .. "," .. listPoints[polygon[i][2]].y .. ")*u -- (" .. P.x .. "," .. P.y ..")*u withcolor \\luameshmpcolorNew withpen pencircle scaled 1pt;"
      end
      -- mark points
      j=1
      for i=1,#listPoints do
         if(listPoints[i].type == "bbox") then
            output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
            j=j+1
         else
            output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
         end
      end
      -- mark the added point
      output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
   end
   return output
end


function TeXOnePointTikZBW(chaine,point,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
   local listPoints = {}
   if(mode=="int") then
      Sx,Sy=string.match(point,"%((.+),(.+)%)")
      P = {x=Sx, y=Sy}
      listPoints = buildList(chaine, mode)
   else
      -- point is a number
      P, listPoints = buildListExt(chaine,tonumber(point))
   end
   output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
   output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. output .. "\\end{tikzpicture}"
   tex.sprint(output)
end

function TeXOnePointTikZBWinc(chaine,point,beginning, ending,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
   local listPoints = {}
   if(mode=="int") then
      Sx,Sy=string.match(point,"%((.+),(.+)%)")
      P = {x=Sx, y=Sy}
      listPoints = buildList(chaine, mode)
   else
      -- point is a number
      P, listPoints = buildListExt(chaine,tonumber(point))
   end
   output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
   output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. beginning..output ..ending.. "\\end{tikzpicture}"
   tex.sprint(output)
end

function TeXOnePointMPBW(chaine,point,step,scale,mode,bbox)
   local listPoints = {}
   if(mode=="int") then
      Sx,Sy=string.match(point,"%((.+),(.+)%)")
      P = {x=Sx, y=Sy}
      listPoints = buildList(chaine, mode)
   else
      -- point is a number
      P, listPoints = buildListExt(chaine,tonumber(point))
   end
   output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
   output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale..";".. output .. "endfig;\\end{mplibcode}"
   tex.sprint(output)
end

function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,bbox)
   local listPoints = {}
   if(mode=="int") then
      Sx,Sy=string.match(point,"%((.+),(.+)%)")
      P = {x=Sx, y=Sy}
      listPoints = buildList(chaine, mode)
   else
      -- point is a number
      P, listPoints = buildListExt(chaine,tonumber(point))
   end
   output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
   output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
   tex.sprint(output)
end


function drawGmshMP(file,points,scale)
   local listPoints,triangulation = readGmsh(file)
   output = traceMeshMP(listPoints,triangulation,points)
   output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
   tex.sprint(output)
end

function drawGmshMPinc(file,beginning,ending,points,scale)
   local listPoints,triangulation = readGmsh(file)
   output = traceMeshMP(listPoints,triangulation,points)
   output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
   tex.sprint(output)
end



--
function drawGmshTikZ(file,points,scale,color)
   local listPoints,triangulation = readGmsh(file)
   output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
   tex.sprint(output)
end

--
function drawGmshTikZinc(file,beginning, ending,points,scale,color)
   local listPoints,triangulation = readGmsh(file)
   output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
   tex.sprint(output)
end


-- buildVoronoi with MP
function gmshVoronoiMP(file,points,scale,tri,styleD,styleV)
   local listPoints,triangulation = readGmsh(file)
   local listVoronoi = buildVoronoi(listPoints, triangulation)
   output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri,styleD,styleV)
   output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
   tex.sprint(output)
end


-- buildVoronoi with TikZ
function gmshVoronoiTikZ(file,points,scale,tri,color,colorVoronoi,styleD,styleV)
   local listPoints,triangulation = readGmsh(file)
   local listVoronoi = buildVoronoi(listPoints, triangulation)
   output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi,styleD,styleV)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"   tex.sprint(output)
end


-- buildVoronoi with MP
function gmshVoronoiMPinc(file,beginning, ending,points,scale,tri,styleD,styleV)
   local listPoints,triangulation = readGmsh(file)
   local listVoronoi = buildVoronoi(listPoints, triangulation)
   output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri,styleD,styleV)
   output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
   tex.sprint(output)
end


-- buildVoronoi with TikZ
function gmshVoronoiTikZinc(file,beginning, ending,points,scale,tri,color,colorVoronoi,styleD,styleV)
   local listPoints,triangulation = readGmsh(file)
   local listVoronoi = buildVoronoi(listPoints, triangulation)
   output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi,styleD,styleV)
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
   tex.sprint(output)
end


--------------------------------------------------
--         Meshing of a polygon                 --
--------------------------------------------------

function  tracePolygonMP(polygon,points)
   output = "";
   output = output .. "pair polygon[];"
   for i=1,#polygon do
      output = output .. "polygon[".. i .. "] = (" .. polygon[i].x .. "," .. polygon[i].y .. ")*u;"
   end
   output = output .. "draw "
   for i=1,#polygon do
      output = output .. "(" .. polygon[i].x .. "," .. polygon[i].y .. ")*u -- "
   end
   output = output .. "cycle withcolor \\luameshmpcolorPoly withpen pencircle scaled 1pt;"
   if(points=="points") then
      for i=1,#polygon do
         output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. polygon[i].x ..",".. polygon[i].y .. ")*u ) withcolor \\luameshmpcolorPoly ;"
      end
   end
   if(points=="dotpoints") then
         for i=1,#polygon do
         output = output .. "drawdot  (" .. polygon[i].x ..",".. polygon[i].y .. ")*u  withcolor \\luameshmpcolorPoly withpen pencircle scaled 3;"
      end
   end
   return output
end


function  tracePolygonTikZ(polygon,points, colorPoly)
   output = "";
   for i=1,#polygon do
      output = output .. "\\coordinate (polygon".. i .. ") at  (" .. polygon[i].x .. "," .. polygon[i].y .. ");"
   end
   output = output .. "\\draw[color=".. colorPoly .. ", thick]"
   for i=1,#polygon do
      output = output .. "(" .. polygon[i].x .. "," .. polygon[i].y .. ") -- "
   end
   output = output .. "cycle;"
   if(points=="points") then
      for i=1,#polygon do
         output = output .. "\\draw[color="..colorPoly.."] (" .. polygon[i].x ..",".. polygon[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
      end
   end
   if(points=="dotpoints") then
      for i=1,#polygon do
         output = output .. "\\draw[color="..colorPoly.."] (" .. polygon[i].x ..",".. polygon[i].y .. ") node {$\\bullet$};"
      end
   end
   return output
end



function drawMeshPolygonMP(chaine,mode,h,step,
                             points,scale,random)
   local polygon = buildList(chaine, mode)
   polygon = addPointsPolygon(polygon,h)
   local grid = buildGrid(polygon,h,random)
   local listPoints = addGridPoints(polygon,grid,h)
   if(step=="polygon") then
      -- the polygon
      output = tracePolygonMP(polygon,points)
   end
   if(step=="grid") then
      -- polygon + grid
      output = tracePointsMP(grid,points)
      output = output .. tracePolygonMP(polygon,points)
   end
   if(step=="points") then
      -- polygon + only grid points inside the polygon
      output = tracePointsMP(listPoints,points)
      output = output .. tracePolygonMP(polygon,points)
   end
   if(step=="mesh") then
      -- polygon + mesh
      triangulation = BowyerWatson(listPoints,"none") -- no bbox
      output = traceMeshMP(listPoints,triangulation,points)
      output = output .. tracePolygonMP(polygon,points)
   end

   output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
   tex.sprint(output)
end



function drawMeshPolygonTikZ(chaine,mode,h,step,
                             points,scale,color,colorPoly,random)
   local polygon = buildList(chaine, mode)
   polygon = addPointsPolygon(polygon,h)
   local grid = buildGrid(polygon,h,random)
   local listPoints = addGridPoints(polygon,grid,h)
   if(step=="polygon") then
      -- the polygon
      output = tracePolygonTikZ(polygon,points,colorPoly)
   end
   if(step=="grid") then
      -- polygon + grid
      output = tracePointsTikZ(grid,points,color,"none") -- none for colorBbox
      output = output .. tracePolygonTikZ(polygon,points,colorPoly)
   end
   if(step=="points") then
      -- polygon + only grid points inside the polygon
      output = tracePointsTikZ(listPoints,points,color,"none")
      output = output .. tracePolygonTikZ(polygon,points,colorPoly)
   end
   if(step=="mesh") then
      -- polygon + mesh
      triangulation = BowyerWatson(listPoints,"none") -- no bbox
      output = traceMeshTikZ(listPoints,triangulation,points,color,"none")
      output = output .. tracePolygonTikZ(polygon,points,colorPoly)
   end
   output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
   tex.sprint(output)
end

function drawMeshPolygonMPinc(chaine,beginning,ending,mode,h,step,
                             points,scale,random)
   local polygon = buildList(chaine, mode)
   polygon = addPointsPolygon(polygon,h)
   local grid = buildGrid(polygon,h,random)
   local listPoints = addGridPoints(polygon,grid,h)
   if(step=="polygon") then
      -- the polygon
      output = tracePolygonMP(polygon,points)
   end
   if(step=="grid") then
      -- polygon + grid
      output = tracePointsMP(grid,points)
      output = output .. tracePolygonMP(polygon,points)
   end
   if(step=="points") then
      -- polygon + only grid points inside the polygon
      output = tracePointsMP(listPoints,points)
      output = output .. tracePolygonMP(polygon,points)
   end
   if(step=="mesh") then
      -- polygon + mesh
      triangulation = BowyerWatson(listPoints,"none") -- no bbox
      output = traceMeshMP(listPoints,triangulation,points)
      output = output .. tracePolygonMP(polygon,points)
   end
   output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
   tex.sprint(output)
end



function drawMeshPolygonTikZinc(chaine,beginning,ending,mode,h,step,
                             points,scale,color,colorPoly,random)
   local polygon = buildList(chaine, mode)
   polygon = addPointsPolygon(polygon,h)
   local grid = buildGrid(polygon,h,random)
   local listPoints = addGridPoints(polygon,grid,h)
   if(step=="polygon") then
      -- the polygon
      output = tracePolygonTikZ(polygon,points,colorPoly)
   end
   if(step=="grid") then
      -- polygon + grid
      output = tracePointsTikZ(grid,points,color,"none") -- none for colorBbox
      output = output .. tracePolygonTikZ(polygon,points,colorPoly)
   end
   if(step=="points") then
      -- polygon + only grid points inside the polygon
      output = tracePointsTikZ(listPoints,points,color,"none")
      output = output .. tracePolygonTikZ(polygon,points,colorPoly)
   end
   if(step=="mesh") then
      -- polygon + mesh
      triangulation = BowyerWatson(listPoints,"none") -- no bbox
      output = traceMeshTikZ(listPoints,triangulation,points,color,"none")
      output = output .. tracePolygonTikZ(polygon,points,colorPoly)
   end
   output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. beginning..output ..ending.. "\\end{tikzpicture}"
   tex.sprint(output)
end