56b635c5e3cfe9dde2d26dcf2b0755745eec0ee4
[delaunay.git] / luamesh-tex.lua
1 -- trace Voronoi with MP
2 function traceVoronoiMP(listPoints, triangulation,listVoronoi, points, tri,styleD,styleV)
3    if(styleD == "dashed") then
4       sDelaunay = "dashed evenly"
5    else
6       sDelaunay = ""
7    end
8    if(styleV == "dashed") then
9       sVoronoi = "dashed evenly"
10    else
11       sVoronoi = ""
12    end
13    listCircumC = listCircumCenter(listPoints,triangulation)
14    output = "";
15    output = output .. " pair MeshPoints[];"
16    for i=1,#listPoints do
17       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
18    end
19    output = output .. " pair CircumCenters[];"
20    for i=1,#listCircumC do
21       output = output .. "CircumCenters[".. i .. "] = (" .. listCircumC[i].x .. "," .. listCircumC[i].y .. ")*u;"
22    end
23    if(tri=="show") then
24       for i=1,#triangulation do
25          PointI = listPoints[triangulation[i][1]]
26          PointJ = listPoints[triangulation[i][2]]
27          PointK = listPoints[triangulation[i][3]]
28          if(triangulation[i].type == "bbox") then
29             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle "..sDelaunay.." withcolor \\luameshmpcolorBbox;"
30          else
31             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle "..sDelaunay.." withcolor \\luameshmpcolor;"
32          end
33       end
34    end
35    for i=1,#listVoronoi do
36       PointI = listCircumC[listVoronoi[i][1]]
37       PointJ = listCircumC[listVoronoi[i][2]]
38       output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u "..sVoronoi.." withcolor \\luameshmpcolorVoronoi;"
39    end
40    if(points=="points") then
41       j=1
42       for i=1,#listPoints do
43          if(listPoints[i].type == "bbox") then
44             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
45             j=j+1
46          else
47             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
48          end
49       end
50       for i=1,#listCircumC do
51          output = output .. "dotlabel.llft (btex $\\CircumPoint_{" .. i .. "}$ etex, (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ")*u ) withcolor \\luameshmpcolorVoronoi ;"
52       end
53    else
54       j=1
55       for i=1,#listPoints do
56          if(listPoints[i].type == "bbox") then
57             output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
58             j=j+1
59          else
60             output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolor withpen pencircle scaled 3;"
61          end
62       end
63       for i=1,#listCircumC do
64          output = output .. "drawdot  (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ")*u  withcolor \\luameshmpcolorVoronoi withpen pencircle scaled 3;"
65       end
66    end
67
68    return output
69 end
70
71
72 -- trace Voronoi with TikZ
73 function traceVoronoiTikZ(listPoints, triangulation,listVoronoi, points, tri,color,colorBbox,colorVoronoi,styleD,styleV)
74    if(styleD == "dashed") then
75       sDelaunay = ",dashed"
76    else
77       sDelaunay = ""
78    end
79    if(styleV == "dashed") then
80       sVoronoi = ",dashed"
81    else
82       sVoronoi = ""
83    end
84    listCircumC = listCircumCenter(listPoints,triangulation)
85     output = ""
86    for i=1,#listPoints do
87       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
88    end
89    for i=1,#listCircumC do
90       output = output .. "\\coordinate (CircumPoints".. i .. ") at  (" .. listCircumC[i].x .. "," .. listCircumC[i].y .. ");"
91    end
92    if(tri=="show") then
93       for i=1,#triangulation do
94          PointI = listPoints[triangulation[i][1]]
95          PointJ = listPoints[triangulation[i][2]]
96          PointK = listPoints[triangulation[i][3]]
97          if(triangulation[i].type == "bbox") then
98             output = output .. "\\draw[color="..colorBbox..sDelaunay.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
99          else
100             output = output .. "\\draw[color="..color..sDelaunay.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
101          end
102       end
103    end
104    for i=1,#listVoronoi do
105       PointI = listCircumC[listVoronoi[i][1]]
106       PointJ = listCircumC[listVoronoi[i][2]]
107       output = output .. "\\draw[color="..colorVoronoi..sVoronoi.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..");"
108    end
109    if(points=="points") then
110       j=1
111       for i=1,#listPoints do
112          if(listPoints[i].type == "bbox") then
113             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
114             j=j+1
115          else
116             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
117          end
118       end
119       for i=1,#listCircumC do
120          output = output .. "\\draw[color="..colorVoronoi.."] (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\CircumPoint_{" .. i .. "}$};"
121       end
122    else
123       j=1
124       for i=1,#listPoints do
125          if(listPoints[i].type == "bbox") then
126             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
127             j=j+1
128          else
129             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
130          end
131       end
132       for i=1,#listCircumC do
133          output = output .. "\\draw[color="..colorVoronoi.."] (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ") node {$\\bullet$};"
134       end
135    end
136    return output
137 end
138
139
140
141 -- buildVoronoi with MP
142 function buildVoronoiMPBW(chaine,mode,points,bbox,scale,tri,styleD,styleV)
143    local listPoints = buildList(chaine, mode)
144    local triangulation = BowyerWatson(listPoints,bbox)
145    local listVoronoi = buildVoronoi(listPoints, triangulation)
146    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri,styleD,styleV)
147    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
148    tex.sprint(output)
149 end
150
151
152 -- buildVoronoi with TikZ
153 function buildVoronoiTikZBW(chaine,mode,points,bbox,scale,tri,color,colorBbox,colorVoronoi,styleD,styleV)
154    local listPoints = buildList(chaine, mode)
155    local triangulation = BowyerWatson(listPoints,bbox)
156    local listVoronoi = buildVoronoi(listPoints, triangulation)
157    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi,styleD,styleV)
158    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"   tex.sprint(output)
159 end
160
161
162 -- buildVoronoi with MP
163 function buildVoronoiMPBWinc(chaine,beginning, ending,mode,points,bbox,scale,tri,styleD,styleV)
164    local listPoints = buildList(chaine, mode)
165    local triangulation = BowyerWatson(listPoints,bbox)
166    local listVoronoi = buildVoronoi(listPoints, triangulation)
167    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri,styleD,styleV)
168    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
169    tex.sprint(output)
170 end
171
172
173 -- buildVoronoi with TikZ
174 function buildVoronoiTikZBWinc(chaine,beginning, ending,mode,points,bbox,scale,tri,color,colorBbox,colorVoronoi)
175    local listPoints = buildList(chaine, mode,styleD,styleV)
176    local triangulation = BowyerWatson(listPoints,bbox)
177    local listVoronoi = buildVoronoi(listPoints, triangulation)
178    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi,styleD,styleV)
179    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
180    tex.sprint(output)
181 end
182
183
184
185 -- trace a triangulation with TikZ
186 function traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
187    output = ""
188    for i=1,#listPoints do
189       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
190    end
191    for i=1,#triangulation do
192       PointI = listPoints[triangulation[i][1]]
193       PointJ = listPoints[triangulation[i][2]]
194       PointK = listPoints[triangulation[i][3]]
195       if(triangulation[i].type == "bbox") then
196          output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
197       else
198          output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
199       end
200    end
201    if(points=="points") then
202       j=1
203       for i=1,#listPoints do
204          if(listPoints[i].type == "bbox") then
205             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
206             j=j+1
207          else
208             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
209          end
210       end
211    end
212    return output
213 end
214
215
216 -- trace a triangulation with MP
217 function traceMeshMP(listPoints, triangulation,points)
218    output = "";
219    output = output .. " pair MeshPoints[];"
220    for i=1,#listPoints do
221       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
222    end
223    for i=1,#triangulation do
224       PointI = listPoints[triangulation[i][1]]
225       PointJ = listPoints[triangulation[i][2]]
226       PointK = listPoints[triangulation[i][3]]
227       if(triangulation[i].type == "bbox") then
228          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
229       else
230          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
231       end
232    end
233    if(points=="points") then
234       j=1
235       for i=1,#listPoints do
236          if(listPoints[i].type == "bbox") then
237             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
238             j=j+1
239          else
240             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
241          end
242       end
243    end
244    if(points=="dotpoints") then
245       j=1
246       for i=1,#listPoints do
247          if(listPoints[i].type == "bbox") then
248             output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
249             j=j+1
250          else
251             output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u withcolor \\luameshmpcolor withpen pencircle scaled 3;"
252          end
253       end
254    end
255    return output
256 end
257
258
259 -- buildMesh with MP
260 function buildMeshMPBW(chaine,mode,points,bbox,scale)
261    local listPoints = buildList(chaine, mode)
262    local triangulation = BowyerWatson(listPoints,bbox)
263    output = traceMeshMP(listPoints, triangulation,points)
264    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
265    tex.sprint(output)
266 end
267
268 -- buildMesh with MP include code
269 function buildMeshMPBWinc(chaine,beginning, ending,mode,points,bbox,scale)
270    local listPoints = buildList(chaine, mode)
271    local triangulation = BowyerWatson(listPoints,bbox)
272    output = traceMeshMP(listPoints, triangulation,points)
273    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
274    tex.sprint(output)
275 end
276
277 -- buildMesh with TikZ
278 function buildMeshTikZBW(chaine,mode,points,bbox,scale,color,colorBbox)
279    local listPoints = buildList(chaine, mode)
280    local triangulation = BowyerWatson(listPoints,bbox)
281    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
282    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
283    tex.sprint(output)
284 end
285
286 -- buildMesh with TikZ
287 function buildMeshTikZBWinc(chaine,beginning, ending,mode,points,bbox,scale,color,colorBbox)
288    local listPoints = buildList(chaine, mode)
289    local triangulation = BowyerWatson(listPoints,bbox)
290    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
291    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
292    tex.sprint(output)
293 end
294
295
296 -- print points of the mesh
297 function tracePointsMP(listPoints,points)
298    output = "";
299    output = output .. " pair MeshPoints[];"
300    for i=1,#listPoints do
301       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
302    end
303    if(points=="points") then
304       j=1
305       for i=1,#listPoints do
306          if(listPoints[i].type == "bbox") then
307             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
308             j=j+1
309          else
310             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
311          end
312       end
313    else
314       for i=1,#listPoints do
315          if(listPoints[i].type == "bbox") then
316             output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
317          else
318             output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolor withpen pencircle scaled 3;"
319          end
320       end
321    end
322    return output
323 end
324
325 -- print points of the mesh
326 function tracePointsTikZ(listPoints,points,color,colorBbox)
327    output = "";
328    for i=1,#listPoints do
329       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
330    end
331    if(points=="points") then
332       j=1
333       for i=1,#listPoints do
334          if(listPoints[i].type == "bbox") then
335             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
336             j = j+1
337          else
338             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
339          end
340       end
341    else
342       for i=1,#listPoints do
343          if(listPoints[i].type == "bbox") then
344             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
345          else
346             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
347          end
348       end
349    end
350    return output
351 end
352
353 -- print points to mesh
354 function printPointsMP(chaine,mode,points,bbox,scale)
355    local listPoints = buildList(chaine, mode)
356    if(bbox == "bbox" ) then
357       listPoints = buildBoundingBox(listPoints)
358    end
359    output = tracePointsMP(listPoints,points)
360    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
361    tex.sprint(output)
362 end
363
364
365 -- print points to mesh
366 function printPointsMPinc(chaine,beginning, ending, mode,points,bbox,scale)
367    local listPoints = buildList(chaine, mode)
368    if(bbox == "bbox" ) then
369       listPoints = buildBoundingBox(listPoints)
370    end
371    output = tracePointsMP(listPoints,points)
372    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
373    tex.sprint(output)
374 end
375
376 -- print points to mesh
377 function printPointsTikZ(chaine,mode,points,bbox,scale,color,colorBbox)
378    local listPoints = buildList(chaine, mode)
379    if(bbox == "bbox" ) then
380       listPoints = buildBoundingBox(listPoints)
381    end
382    output = tracePointsTikZ(listPoints,points,color,colorBbox)
383    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
384    tex.sprint(output)
385 end
386
387
388 -- print points to mesh
389 function printPointsTikZinc(chaine,beginning, ending, mode,points,bbox,scale,color,colorBbox)
390    local listPoints = buildList(chaine, mode)
391    if(bbox == "bbox" ) then
392       listPoints = buildBoundingBox(listPoints)
393    end
394    output = tracePointsTikZ(listPoints,points,color,colorBbox)
395    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
396    tex.sprint(output)
397 end
398
399
400 -- buildMesh
401 function buildRect(largeur,a,b,nbrA, nbrB)
402    local listPoints = rectangleList(a,b,nbrA,nbrB)
403    local triangulation = BowyerWatson(listPoints,"none")
404    traceTikZ(listPoints, triangulation,largeur,"none")
405 end
406
407
408 --
409 function TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack, colorNew, colorCircle,colorBbox)
410    output = ""
411    -- build the triangulation
412    local triangulation = BowyerWatson(listPoints,bbox)
413    local badTriangles = buildBadTriangles(P,triangulation,listPoints)
414    for i=1,#listPoints do
415       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
416    end
417    if(step == "badT") then
418       -- draw all triangle
419       for i=1,#triangulation do
420          PointI = listPoints[triangulation[i][1]]
421          PointJ = listPoints[triangulation[i][2]]
422          PointK = listPoints[triangulation[i][3]]
423          if(triangulation[i].type == "bbox") then
424             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
425          else
426             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
427          end
428       end
429       -- draw and fill the bad triangle
430       for i=1,#badTriangles do
431          PointI = listPoints[triangulation[badTriangles[i]][1]]
432          PointJ = listPoints[triangulation[badTriangles[i]][2]]
433          PointK = listPoints[triangulation[badTriangles[i]][3]]
434          output = output .. "\\draw[fill="..colorBack.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
435       end
436       -- draw the circoncircle
437       for i=1,#badTriangles do
438          PointI = listPoints[triangulation[badTriangles[i]][1]]
439          PointJ = listPoints[triangulation[badTriangles[i]][2]]
440          PointK = listPoints[triangulation[badTriangles[i]][3]]
441          center, radius = circoncircle(PointI, PointJ, PointK)
442          output = output .. "\\draw[dashed, color="..colorCircle.."] ("..center.x .. "," .. center.y .. ") circle ("..radius ..");"
443       end
444       -- mark the points
445       j=1
446       for i=1,#listPoints do
447          if(listPoints[i].type == "bbox") then
448             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
449             j = j+1
450          else
451             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
452          end
453       end
454       -- mark the point to add
455       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
456    elseif(step == "cavity") then
457       polygon = buildCavity(badTriangles, triangulation)
458       polyNew = cleanPoly(polygon)
459       -- remove the bad triangles
460       for j=1,#badTriangles do
461          table.remove(triangulation,badTriangles[j]-(j-1))
462       end
463       -- draw the triangles
464       for i=1,#triangulation do
465          PointI = listPoints[triangulation[i][1]]
466          PointJ = listPoints[triangulation[i][2]]
467          PointK = listPoints[triangulation[i][3]]
468          if(triangulation[i].type == "bbox") then
469             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
470          else
471             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
472          end
473       end
474       -- fill and draw the cavity
475       path = ""
476       for i=1,#polyNew do
477          PointI = listPoints[polyNew[i]]
478          path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
479       end
480       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
481       -- mark the points of the mesh
482       j=1
483       for i=1,#listPoints do
484          if(listPoints[i].type == "bbox") then
485             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
486             j=j+1
487          else
488             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
489          end
490       end
491       -- mark the adding point
492       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
493    elseif(step == "newT") then
494       polygon = buildCavity(badTriangles, triangulation)
495       polyNew = cleanPoly(polygon)
496       -- remove the bad triangles
497       for j=1,#badTriangles do
498          table.remove(triangulation,badTriangles[j]-(j-1))
499       end
500       -- draw the triangle of the triangulation
501       for i=1,#triangulation do
502          PointI = listPoints[triangulation[i][1]]
503          PointJ = listPoints[triangulation[i][2]]
504          PointK = listPoints[triangulation[i][3]]
505          if(triangulation[i].type == "bbox") then
506             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
507          else
508             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
509          end
510       end
511       -- fill and draw the cavity
512       path = ""
513       for i=1,#polyNew do
514          PointI = listPoints[polyNew[i]]
515          path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
516       end
517       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
518       -- draw the new triangles composed by the edges of the polygon and the added point
519       for i=1,#polygon do
520          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 ..");"
521          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][1]].x .. "," .. listPoints[polygon[i][1]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
522          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][2]].x .. "," .. listPoints[polygon[i][2]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
523       end
524       -- mark points
525       j=1
526       for i=1,#listPoints do
527          if(listPoints[i].type == "bbox") then
528             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
529             j=j+1
530          else
531             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
532          end
533       end
534       -- mark the added point
535       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
536    end
537    return output
538 end
539
540 function TeXaddOnePointMPBW(listPoints,P,step,bbox)
541    output = "";
542    output = output .. "pair MeshPoints[];"
543    -- build the triangulation
544    local triangulation = {}
545    local badTriangles = {}
546    triangulation = BowyerWatson(listPoints,bbox)
547    badTriangles = buildBadTriangles(P,triangulation,listPoints)
548    for i=1,#listPoints do
549       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
550    end
551    if(step == "badT") then
552       -- draw all triangle
553       for i=1,#triangulation do
554          PointI = listPoints[triangulation[i][1]]
555          PointJ = listPoints[triangulation[i][2]]
556          PointK = listPoints[triangulation[i][3]]
557          if(triangulation[i].type == "bbox") then
558             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
559          else
560             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
561          end
562       end
563       -- draw and fill the bad triangle
564       for i=1,#badTriangles do
565          PointI = listPoints[triangulation[badTriangles[i]][1]]
566          PointJ = listPoints[triangulation[badTriangles[i]][2]]
567          PointK = listPoints[triangulation[badTriangles[i]][3]]
568          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
569          output = output .. "fill (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBack;"
570       end
571       -- draw the circoncircle
572       for i=1,#badTriangles do
573          PointI = listPoints[triangulation[badTriangles[i]][1]]
574          PointJ = listPoints[triangulation[badTriangles[i]][2]]
575          PointK = listPoints[triangulation[badTriangles[i]][3]]
576          center, radius = circoncircle(PointI, PointJ, PointK)
577          output = output .. "draw fullcircle scaled ("..radius .."*2u) shifted ("..center.x .. "*u," .. center.y .. "*u) dashed evenly withcolor \\luameshmpcolorCircle;"
578       end
579       -- mark the points
580       j=1
581       for i=1,#listPoints do
582          if(listPoints[i].type == "bbox") then
583             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
584             j=j+1
585          else
586             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
587          end
588       end
589       -- mark the point to add
590       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew;"
591    elseif(step == "cavity") then
592       polygon = buildCavity(badTriangles, triangulation)
593       polyNew = cleanPoly(polygon)
594       -- remove the bad triangles
595       for j=1,#badTriangles do
596          table.remove(triangulation,badTriangles[j]-(j-1))
597       end
598       -- draw the triangles
599       for i=1,#triangulation do
600          PointI = listPoints[triangulation[i][1]]
601          PointJ = listPoints[triangulation[i][2]]
602          PointK = listPoints[triangulation[i][3]]
603          if(triangulation[i].type == "bbox") then
604             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
605          else
606             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
607          end
608       end
609       -- fill and draw the cavity
610       path = ""
611       for i=1,#polyNew do
612          PointI = listPoints[polyNew[i]]
613          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
614       end
615       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
616       output = output .. "draw " .. path .. "cycle withcolor \\luameshmpcolorNew  withpen pencircle scaled 1pt;"
617       -- mark the points of the mesh
618       j=1
619       for i=1,#listPoints do
620          if(listPoints[i].type == "bbox") then
621             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
622             j=j+1
623          else
624             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
625          end
626       end
627       -- mark the adding point
628       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
629    elseif(step == "newT") then
630       polygon = buildCavity(badTriangles, triangulation)
631       polyNew = cleanPoly(polygon)
632       -- remove the bad triangles
633       for j=1,#badTriangles do
634          table.remove(triangulation,badTriangles[j]-(j-1))
635       end
636       -- draw the triangle of the triangulation
637       for i=1,#triangulation do
638          PointI = listPoints[triangulation[i][1]]
639          PointJ = listPoints[triangulation[i][2]]
640          PointK = listPoints[triangulation[i][3]]
641          if(triangulation[i].type == "bbox") then
642             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
643          else
644             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
645          end
646       end
647       -- fill  the cavity
648       path = ""
649       for i=1,#polyNew do
650          PointI = listPoints[polyNew[i]]
651          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
652       end
653       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
654       -- draw the new triangles composed by the edges of the polygon and the added point
655       for i=1,#polygon do
656          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;"
657          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;"
658          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;"
659       end
660       -- mark points
661       j=1
662       for i=1,#listPoints do
663          if(listPoints[i].type == "bbox") then
664             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
665             j=j+1
666          else
667             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
668          end
669       end
670       -- mark the added point
671       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
672    end
673    return output
674 end
675
676
677 function TeXOnePointTikZBW(chaine,point,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
678    local listPoints = {}
679    if(mode=="int") then
680       Sx,Sy=string.match(point,"%((.+),(.+)%)")
681       P = {x=Sx, y=Sy}
682       listPoints = buildList(chaine, mode)
683    else
684       -- point is a number
685       P, listPoints = buildListExt(chaine,tonumber(point))
686    end
687    output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
688    output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. output .. "\\end{tikzpicture}"
689    tex.sprint(output)
690 end
691
692 function TeXOnePointTikZBWinc(chaine,point,beginning, ending,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
693    local listPoints = {}
694    if(mode=="int") then
695       Sx,Sy=string.match(point,"%((.+),(.+)%)")
696       P = {x=Sx, y=Sy}
697       listPoints = buildList(chaine, mode)
698    else
699       -- point is a number
700       P, listPoints = buildListExt(chaine,tonumber(point))
701    end
702    output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
703    output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. beginning..output ..ending.. "\\end{tikzpicture}"
704    tex.sprint(output)
705 end
706
707 function TeXOnePointMPBW(chaine,point,step,scale,mode,bbox)
708    local listPoints = {}
709    if(mode=="int") then
710       Sx,Sy=string.match(point,"%((.+),(.+)%)")
711       P = {x=Sx, y=Sy}
712       listPoints = buildList(chaine, mode)
713    else
714       -- point is a number
715       P, listPoints = buildListExt(chaine,tonumber(point))
716    end
717    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
718    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale..";".. output .. "endfig;\\end{mplibcode}"
719    tex.sprint(output)
720 end
721
722 function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,bbox)
723    local listPoints = {}
724    if(mode=="int") then
725       Sx,Sy=string.match(point,"%((.+),(.+)%)")
726       P = {x=Sx, y=Sy}
727       listPoints = buildList(chaine, mode)
728    else
729       -- point is a number
730       P, listPoints = buildListExt(chaine,tonumber(point))
731    end
732    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
733    output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
734    tex.sprint(output)
735 end
736
737
738 function drawGmshMP(file,points,scale)
739    local listPoints,triangulation = readGmsh(file)
740    output = traceMeshMP(listPoints,triangulation,points)
741    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
742    tex.sprint(output)
743 end
744
745 function drawGmshMPinc(file,beginning,ending,points,scale)
746    local listPoints,triangulation = readGmsh(file)
747    output = traceMeshMP(listPoints,triangulation,points)
748    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
749    tex.sprint(output)
750 end
751
752
753
754 --
755 function drawGmshTikZ(file,points,scale,color)
756    local listPoints,triangulation = readGmsh(file)
757    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
758    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
759    tex.sprint(output)
760 end
761
762 --
763 function drawGmshTikZinc(file,beginning, ending,points,scale,color)
764    local listPoints,triangulation = readGmsh(file)
765    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
766    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
767    tex.sprint(output)
768 end
769
770
771 -- buildVoronoi with MP
772 function gmshVoronoiMP(file,points,scale,tri,styleD,styleV)
773    local listPoints,triangulation = readGmsh(file)
774    local listVoronoi = buildVoronoi(listPoints, triangulation)
775    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri,styleD,styleV)
776    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
777    tex.sprint(output)
778 end
779
780
781 -- buildVoronoi with TikZ
782 function gmshVoronoiTikZ(file,points,scale,tri,color,colorVoronoi,styleD,styleV)
783    local listPoints,triangulation = readGmsh(file)
784    local listVoronoi = buildVoronoi(listPoints, triangulation)
785    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi,styleD,styleV)
786    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"   tex.sprint(output)
787 end
788
789
790 -- buildVoronoi with MP
791 function gmshVoronoiMPinc(file,beginning, ending,points,scale,tri,styleD,styleV)
792    local listPoints,triangulation = readGmsh(file)
793    local listVoronoi = buildVoronoi(listPoints, triangulation)
794    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri,styleD,styleV)
795    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
796    tex.sprint(output)
797 end
798
799
800 -- buildVoronoi with TikZ
801 function gmshVoronoiTikZinc(file,beginning, ending,points,scale,tri,color,colorVoronoi,styleD,styleV)
802    local listPoints,triangulation = readGmsh(file)
803    local listVoronoi = buildVoronoi(listPoints, triangulation)
804    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi,styleD,styleV)
805    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
806    tex.sprint(output)
807 end
808
809
810 --------------------------------------------------
811 --         Meshing of a polygon                 --
812 --------------------------------------------------
813
814
815 function TeXMeshPolygonMP(polygon,listPoints, grid, step)
816
817 end
818
819 function  tracePolygonMP(polygon,points)
820    output = "";
821    output = output .. "pair polygon[];"
822    for i=1,#polygon do
823       output = output .. "polygon[".. i .. "] = (" .. polygon[i].x .. "," .. polygon[i].y .. ")*u;"
824    end
825    output = output .. "draw "
826    for i=1,#polygon do
827       output = output .. "(" .. polygon[i].x .. "," .. polygon[i].y .. ")*u -- "
828    end
829    output = output .. "cycle withcolor \\luameshmpcolorPoly withpen pencircle scaled 1pt;"
830    if(points=="points") then
831       for i=1,#polygon do
832          output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. polygon[i].x ..",".. polygon[i].y .. ")*u ) withcolor \\luameshmpcolorPoly ;"
833       end
834    end
835    if(points=="dotpoints") then
836          for i=1,#polygon do
837          output = output .. "drawdot  (" .. polygon[i].x ..",".. polygon[i].y .. ")*u  withcolor \\luameshmpcolorPoly withpen pencircle scaled 3;"
838       end
839    end
840    return output
841 end
842
843
844
845 function drawMeshPolygonMP(chaine,mode,h,step,
846                              points,scale)
847    local polygon = buildList(chaine, mode)
848    polygon = addPointsPolygon(polygon,h)
849    local grid = buildGrid(polygon,h)
850    local listPoints = addGridPoints(polygon,grid,h)
851    if(step=="polygon") then
852       -- the polygon
853       output = tracePolygonMP(polygon,points)
854    end
855    if(step=="grid") then
856       -- polygon + grid
857       output = tracePointsMP(grid,points)
858       output = output .. tracePolygonMP(polygon,points)
859    end
860    if(step=="points") then
861       -- polygon + only grid points inside the polygon
862       output = tracePointsMP(listPoints,points)
863       output = output .. tracePolygonMP(polygon,points)
864    end
865    if(step=="mesh") then
866       -- polygon + mesh
867       triangulation = BowyerWatson(listPoints,"none") -- no bbox
868       output = traceMeshMP(listPoints,triangulation,points)
869       output = output .. tracePolygonMP(polygon,points)
870    end
871
872    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
873    tex.sprint(output)
874 end

Licence Creative Commons Les fichiers de Syracuse sont mis à disposition (sauf mention contraire) selon les termes de la
Licence Creative Commons Attribution - Pas d’Utilisation Commerciale - Partage dans les Mêmes Conditions 4.0 International.