Mise à jour archive v0.2 et liens index.md
[delaunay.git] / luamesh.lua
1 -- Bowyer and Watson algorithm
2 -- Delaunay meshing
3 function BowyerWatson (listPoints,bbox)
4    local triangulation = {}
5    local lgth = #listPoints
6    -- add four points to listPoints to have a bounding box
7    listPoints = buildBoundingBox(listPoints)
8    -- the first triangle
9    triangulation[1] = {lgth+1, lgth+2, lgth+3,type="bbox"}
10    -- the second triangle
11    triangulation[2] = {lgth+1, lgth+3, lgth+4,type="bbox"}
12    -- add points one by one
13    for i=1,lgth do
14       -- find the triangles which the circumcircle contained the point to add
15       badTriangles = buildBadTriangles(listPoints[i],triangulation)
16       -- build the polygon of the cavity containing the point to add
17       polygon = buildCavity(badTriangles, triangulation)
18       -- remove the bad triangles
19       for j=1,#badTriangles do
20          table.remove(triangulation,badTriangles[j]-(j-1))
21       end
22       -- build the new triangles and add them to triangulation
23       for j=1,#polygon do
24          if((polygon[j][1]>lgth) or (polygon[j][2]>lgth) or (i>lgth)) then
25             table.insert(triangulation,{polygon[j][1],polygon[j][2],i,type="bbox"})
26          else
27             table.insert(triangulation,{polygon[j][1],polygon[j][2],i,type="in"})
28          end
29       end
30    end  -- end adding points of the listPoints
31    -- remove bounding box
32    if(bbox ~= "bbox") then
33       triangulation = removeBoundingBox(triangulation,lgth)
34       table.remove(listPoints,lgth+1)
35       table.remove(listPoints,lgth+1)
36       table.remove(listPoints,lgth+1)
37       table.remove(listPoints,lgth+1)
38    end
39    return triangulation
40 end
41
42
43 function buildBoundingBox(listPoints)
44    -- listPoints : list of points
45    -- epsV        : parameter for the distance of the bounding box
46    local xmin, xmax, ymin, ymax, eps
47    xmin = 1000
48    ymin = 1000
49    xmax = -1000
50    ymax = -1000
51    for i=1,#listPoints do
52       if (listPoints[i].x < xmin) then
53          xmin = listPoints[i].x
54       end
55       if (listPoints[i].x > xmax) then
56          xmax = listPoints[i].x
57       end
58       if (listPoints[i].y < ymin) then
59          ymin = listPoints[i].y
60       end
61       if (listPoints[i].y > ymax) then
62          ymax = listPoints[i].y
63       end
64    end
65    eps = math.max(math.abs(xmax-xmin),math.abs(ymax-ymin))*0.15
66    xmin = xmin - eps
67    xmax = xmax + eps
68    ymin = ymin - eps
69    ymax = ymax + eps
70    -- add points of the bounding box in last positions
71    table.insert(listPoints,{x=xmin,y=ymin,type="bbox"})
72    table.insert(listPoints,{x=xmin,y=ymax,type="bbox"})
73    table.insert(listPoints,{x=xmax,y=ymax,type="bbox"})
74    table.insert(listPoints,{x=xmax,y=ymin,type="bbox"})
75    return listPoints
76 end
77
78 function removeBoundingBox(triangulation,lgth)
79    -- build the four bounding box edge
80    point1 = lgth+1
81    point2 = lgth+2
82    point3 = lgth+3
83    point4 = lgth+4
84    -- for all triangle
85    newTriangulation = {}
86    for i=1,#triangulation do
87       boolE1 = pointInTriangle(point1,triangulation[i])
88       boolE2 = pointInTriangle(point2,triangulation[i])
89       boolE3 = pointInTriangle(point3,triangulation[i])
90       boolE4 = pointInTriangle(point4,triangulation[i])
91       if((not boolE1) and (not boolE2) and (not boolE3) and (not boolE4)) then
92          table.insert(newTriangulation,triangulation[i])
93       end
94    end
95    return newTriangulation
96 end
97
98
99 function buildBadTriangles(point, triangulation)
100    badTriangles = {}
101    for j=1,#triangulation do -- for all triangles
102       A = listPoints[triangulation[j][1]]
103       B = listPoints[triangulation[j][2]]
104       C = listPoints[triangulation[j][3]]
105       center, radius = circoncircle(A,B,C)
106       CP = Vector(center,point)
107       if(VectorNorm(CP)<radius) then -- the point belongs to the circoncirle
108          table.insert(badTriangles,j)
109       end
110    end
111    return badTriangles
112 end
113
114 -- construction of the cavity composed by the bad triangles around the point to add
115 function buildCavity(badTriangles, triangulation)
116    polygon = {}
117    for j=1,#badTriangles do -- for all bad triangles
118       ind = badTriangles[j]
119       for k=1,3 do -- for all edges
120          edge = {triangulation[ind][k],triangulation[ind][k%3+1]}
121          edgeBord = false
122          for l = 1,#badTriangles do -- for all badtriangles
123             badInd = badTriangles[l]
124             if(badInd ~= ind) then -- if not the current one
125                edgeBord = edgeBord or edgeInTriangle(edge,triangulation[badInd])
126             end
127          end --
128          -- if the edge does not belong to another bad triangle
129          if(edgeBord == false) then
130             -- insert the edge to the cavity
131             table.insert(polygon,edge)
132          end
133       end --
134    end --
135    return polygon
136 end
137
138 function edgeInTriangle(e,t)
139    in1 = false
140    in2 = false
141    for i=1,3 do
142       if e[1] == t[i] then
143          in1 = true
144       end
145       if e[2] == t[i] then
146          in2 = true
147       end
148    end
149    out = (in1 and in2)
150    return out
151 end
152
153 function pointInTriangle(e,t)
154    in1 = false
155    for i=1,3 do
156       if e == t[i] then
157          in1 = true
158       end
159    end
160    return in1
161 end
162
163
164 function Vector(A,B)
165    local out = {x = B.x - A.x, y = B.y - A.y}
166    return out
167 end
168
169 function VectorNorm(NP)
170    return math.sqrt(NP.x*NP.x +NP.y*NP.y)
171 end
172
173 -- circoncircle
174 function circoncircle(M, N, P)
175    -- Compute center and radius of the circoncircle of the triangle M N P
176
177    -- return : (center [Point],radius [float])
178
179    local MN = Vector(M,N)
180    local NP = Vector(N,P)
181    local PM = Vector(P,M)
182    m = VectorNorm(NP)  -- |NP|
183    n = VectorNorm(PM)  -- |PM|
184    p = VectorNorm(MN)  -- |MN|
185
186    d = (m + n + p) * (-m + n + p) * (m - n + p) * (m + n - p)
187    if d > 0 then
188       rad = m * n * p / math.sqrt(d)
189    else
190       rad = 0
191    end
192    d = -2 * (M.x * NP.y + N.x * PM.y + P.x * MN.y)
193    O = {x=0, y=0}
194    OM = Vector(O, M)
195    ON = Vector(O, N)
196    OP = Vector(O, P)
197    om2 = math.pow(VectorNorm(OM),2)  -- |OM|**2
198    on2 = math.pow(VectorNorm(ON),2)  -- |ON|**2
199    op2 = math.pow(VectorNorm(OP),2)  -- |OP|**2
200    x0 = -(om2 * NP.y + on2 * PM.y + op2 * MN.y) / d
201    y0 = (om2 * NP.x + on2 * PM.x + op2 * MN.x) / d
202    if d == 0 then
203       Out = {nil, nil}
204    else
205       Out = {x=x0, y=y0}
206    end
207    return Out, rad  -- (center [Point], R [float])
208 end
209
210 -- compute the list of the circumcircle of a triangulation
211 function listCircumCenter(listPoints,triangulation)
212    list = {}
213    for j=1,#triangulation do
214       A = listPoints[triangulation[j][1]]
215       B = listPoints[triangulation[j][2]]
216       C = listPoints[triangulation[j][3]]
217       center, radius = circoncircle(A,B,C)
218       table.insert(list,{x=center.x,y=center.y,r=radius})
219    end
220    return list
221 end
222
223 -- find the three neighbour triangles of T
224 function findNeighbour(T,i,triangulation)
225    -- T : triangle
226    -- i : index of T in triangualation
227    -- triangulation
228
229    list = {}
230    -- define the three edge
231    e1 = {T[1],T[2]}
232    e2 = {T[2],T[3]}
233    e3 = {T[3],T[1]}
234    for j=1,#triangulation do
235       if j~= i then
236          if(edgeInTriangle(e1,triangulation[j])) then
237             table.insert(list,j)
238          end
239          if(edgeInTriangle(e2,triangulation[j])) then
240             table.insert(list,j)
241          end
242          if(edgeInTriangle(e3,triangulation[j])) then
243             table.insert(list,j)
244          end
245       end
246    end
247    return list
248 end
249
250 -- test if edge are the same (reverse)
251 function equalEdge(e1,e2)
252    if(((e1[1] == e2[1]) and (e1[2] == e2[2])) or ((e1[1] == e2[2]) and (e1[2] == e2[1]))) then
253       return true
254    else
255       return false
256    end
257 end
258
259 -- test if the edge belongs to the list
260 function edgeInList(e,listE)
261    output = false
262    for i=1,#listE do
263       if(equalEdge(e,listE[i])) then
264          output = true
265       end
266    end
267    return output
268 end
269
270 -- build the edges of the Voronoi diagram with a given triangulation
271 function buildVoronoi(listPoints, triangulation)
272    listCircumCircle = listCircumCenter(listPoints, triangulation)
273    listVoronoi = {}
274    for i=1,#listCircumCircle do
275       listN = findNeighbour(triangulation[i],i,triangulation)
276       for j=1,#listN do
277          edge = {i,listN[j]}
278          if( not edgeInList(edge, listVoronoi)) then
279             table.insert(listVoronoi, edge)
280          end
281       end
282    end
283    return listVoronoi
284 end
285
286 -------------------------- TeX
287 -- build the list of points
288 function buildList(chaine, mode)
289    -- if mode = int : the list is given in the chaine string (x1,y1);(x2,y2);...;(xn,yn)
290    -- if mode = ext : the list is given in a file line by line with space separation
291    listPoints = {}
292    if mode == "int" then
293       local points = string.explode(chaine, ";")
294       local lgth=#points
295       for i=1,lgth do
296          Sx,Sy=string.match(points[i],"%((.+),(.+)%)")
297          listPoints[i]={x=tonumber(Sx),y=tonumber(Sy)}
298       end
299    elseif mode == "ext" then
300       io.input(chaine) -- open the file
301       text=io.read("*all")
302       lines=string.explode(text,"\n+") -- all the lines
303       tablePoints={}
304       for i=1,#lines do
305          xy=string.explode(lines[i]," +")
306          listPoints[i]={x=tonumber(xy[1]),y=tonumber(xy[2])}
307       end
308    else
309       print("Non existing mode")
310    end
311    return listPoints
312 end
313
314
315 --
316 function rectangleList(a,b,nbrA,nbrB)
317    stepA = a/nbrA
318    stepB = b/nbrB
319    listPoints = {}
320    k=1
321    for i=1,(nbrA+1) do
322       for j=1,(nbrB+1) do
323          listPoints[k] = {x = (i-1)*stepA, y=(j-1)*stepB}
324          k=k+1
325       end
326    end
327    return listPoints
328 end
329
330
331 -- trace Voronoi with MP
332 function traceVoronoiMP(listPoints, triangulation,listVoronoi, points, tri)
333    listCircumC = listCircumCenter(listPoints,triangulation)
334    output = "";
335    output = output .. " pair MeshPoints[];"
336    for i=1,#listPoints do
337       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
338    end
339    output = output .. " pair CircumCenters[];"
340    for i=1,#listCircumC do
341       output = output .. "CircumCenters[".. i .. "] = (" .. listCircumC[i].x .. "," .. listCircumC[i].y .. ")*u;"
342    end
343    if(tri=="show") then
344       for i=1,#triangulation do
345          PointI = listPoints[triangulation[i][1]]
346          PointJ = listPoints[triangulation[i][2]]
347          PointK = listPoints[triangulation[i][3]]
348          if(triangulation[i].type == "bbox") then
349             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
350          else
351             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
352          end
353       end
354    end
355    for i=1,#listVoronoi do
356       PointI = listCircumC[listVoronoi[i][1]]
357       PointJ = listCircumC[listVoronoi[i][2]]
358       output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u withcolor \\luameshmpcolorVoronoi;"
359    end
360    if(points=="points") then
361       j=1
362       for i=1,#listPoints do
363          if(listPoints[i].type == "bbox") then
364             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
365             j=j+1
366          else
367             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
368          end
369       end
370       for i=1,#listCircumC do
371          output = output .. "dotlabel.llft (btex $\\CircumPoint_{" .. i .. "}$ etex, (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ")*u ) withcolor \\luameshmpcolorVoronoi ;"
372       end
373    else
374       j=1
375       for i=1,#listPoints do
376          if(listPoints[i].type == "bbox") then
377             output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
378             j=j+1
379          else
380             output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolor withpen pencircle scaled 3;"
381          end
382       end
383       for i=1,#listCircumC do
384          output = output .. "drawdot  (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ")*u  withcolor \\luameshmpcolorVoronoi withpen pencircle scaled 3;"
385       end
386    end
387
388    return output
389 end
390
391
392 -- trace Voronoi with TikZ
393 function traceVoronoiTikZ(listPoints, triangulation,listVoronoi, points, tri,color,colorBbox,colorVoronoi)
394    listCircumC = listCircumCenter(listPoints,triangulation)
395     output = ""
396    for i=1,#listPoints do
397       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
398    end
399    for i=1,#listCircumC do
400       output = output .. "\\coordinate (CircumPoints".. i .. ") at  (" .. listCircumC[i].x .. "," .. listCircumC[i].y .. ");"
401    end
402    if(tri=="show") then
403       for i=1,#triangulation do
404          PointI = listPoints[triangulation[i][1]]
405          PointJ = listPoints[triangulation[i][2]]
406          PointK = listPoints[triangulation[i][3]]
407          if(triangulation[i].type == "bbox") then
408             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
409          else
410             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
411          end
412       end
413    end
414    for i=1,#listVoronoi do
415       PointI = listCircumC[listVoronoi[i][1]]
416       PointJ = listCircumC[listVoronoi[i][2]]
417       output = output .. "\\draw[color="..colorVoronoi.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..");"
418    end
419    if(points=="points") then
420       j=1
421       for i=1,#listPoints do
422          if(listPoints[i].type == "bbox") then
423             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
424             j=j+1
425          else
426             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
427          end
428       end
429       for i=1,#listCircumC do
430          output = output .. "\\draw[color="..colorVoronoi.."] (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\CircumPoint_{" .. i .. "}$};"
431       end
432    else
433       j=1
434       for i=1,#listPoints do
435          if(listPoints[i].type == "bbox") then
436             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
437             j=j+1
438          else
439             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
440          end
441       end
442       for i=1,#listCircumC do
443          output = output .. "\\draw[color="..colorVoronoi.."] (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ") node {$\\bullet$};"
444       end
445    end
446    return output
447 end
448
449
450
451 -- buildVoronoi with MP
452 function buildVoronoiMPBW(chaine,mode,points,bbox,scale,tri)
453    listPoints = buildList(chaine, mode)
454    triangulation = BowyerWatson(listPoints,bbox)
455    listVoronoi = buildVoronoi(listPoints, triangulation)
456    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri)
457    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
458    tex.sprint(output)
459 end
460
461
462 -- buildVoronoi with TikZ
463 function buildVoronoiTikZBW(chaine,mode,points,bbox,scale,tri,color,colorBbox,colorVoronoi)
464    listPoints = buildList(chaine, mode)
465    triangulation = BowyerWatson(listPoints,bbox)
466    listVoronoi = buildVoronoi(listPoints, triangulation)
467    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi)
468    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"   tex.sprint(output)
469 end
470
471
472 -- buildVoronoi with MP
473 function buildVoronoiMPBWinc(chaine,beginning, ending,mode,points,bbox,scale,tri)
474    listPoints = buildList(chaine, mode)
475    triangulation = BowyerWatson(listPoints,bbox)
476    listVoronoi = buildVoronoi(listPoints, triangulation)
477    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri)
478    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
479    tex.sprint(output)
480 end
481
482
483 -- buildVoronoi with TikZ
484 function buildVoronoiTikZBWinc(chaine,beginning, ending,mode,points,bbox,scale,tri,color,colorBbox,colorVoronoi)
485    listPoints = buildList(chaine, mode)
486    triangulation = BowyerWatson(listPoints,bbox)
487    listVoronoi = buildVoronoi(listPoints, triangulation)
488    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi)
489    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
490    tex.sprint(output)
491 end
492
493
494
495 -- trace a triangulation with TikZ
496 function traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
497    output = ""
498    for i=1,#listPoints do
499       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
500    end
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    if(points=="points") then
512       j=1
513       for i=1,#listPoints do
514          if(listPoints[i].type == "bbox") then
515             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
516             j=j+1
517          else
518             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
519          end
520       end
521    end
522    return output
523 end
524
525
526 -- trace a triangulation with MP
527 function traceMeshMP(listPoints, triangulation,points)
528    output = "";
529    output = output .. " pair MeshPoints[];"
530    for i=1,#listPoints do
531       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
532    end
533    for i=1,#triangulation do
534       PointI = listPoints[triangulation[i][1]]
535       PointJ = listPoints[triangulation[i][2]]
536       PointK = listPoints[triangulation[i][3]]
537       if(triangulation[i].type == "bbox") then
538          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
539       else
540          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
541       end
542    end
543    if(points=="points") then
544       j=1
545       for i=1,#listPoints do
546          if(listPoints[i].type == "bbox") then
547             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
548             j=j+1
549          else
550             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
551          end
552       end
553    end
554    return output
555 end
556
557
558 -- buildMesh with MP
559 function buildMeshMPBW(chaine,mode,points,bbox,scale)
560    listPoints = buildList(chaine, mode)
561    triangulation = BowyerWatson(listPoints,bbox)
562    output = traceMeshMP(listPoints, triangulation,points)
563    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
564    tex.sprint(output)
565 end
566
567 -- buildMesh with MP include code
568 function buildMeshMPBWinc(chaine,beginning, ending,mode,points,bbox,scale)
569    listPoints = buildList(chaine, mode)
570    triangulation = BowyerWatson(listPoints,bbox)
571    output = traceMeshMP(listPoints, triangulation,points)
572    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
573    tex.sprint(output)
574 end
575
576 -- buildMesh with TikZ
577 function buildMeshTikZBW(chaine,mode,points,bbox,scale,color,colorBbox)
578    listPoints = buildList(chaine, mode)
579    triangulation = BowyerWatson(listPoints,bbox)
580    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
581    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
582    tex.sprint(output)
583 end
584
585 -- buildMesh with TikZ
586 function buildMeshTikZBWinc(chaine,beginning, ending,mode,points,bbox,scale,color,colorBbox)
587    listPoints = buildList(chaine, mode)
588    triangulation = BowyerWatson(listPoints,bbox)
589    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
590    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
591    tex.sprint(output)
592 end
593
594
595 -- print points of the mesh
596 function tracePointsMP(listPoints,points)
597    output = "";
598    output = output .. " pair MeshPoints[];"
599    for i=1,#listPoints do
600       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
601    end
602    if(points=="points") then
603       j=1
604       for i=1,#listPoints do
605          if(listPoints[i].type == "bbox") then
606             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
607             j=j+1
608          else
609             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
610          end
611       end
612    else
613       for i=1,#listPoints do
614          if(listPoints[i].type == "bbox") then
615             output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
616          else
617             output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolor withpen pencircle scaled 3;"
618          end
619       end
620    end
621    return output
622 end
623
624 -- print points of the mesh
625 function tracePointsTikZ(listPoints,points,color,colorBbox)
626    output = "";
627    for i=1,#listPoints do
628       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
629    end
630    if(points=="points") then
631       j=1
632       for i=1,#listPoints do
633          if(listPoints[i].type == "bbox") then
634             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
635             j = j+1
636          else
637             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
638          end
639       end
640    else
641       for i=1,#listPoints do
642          if(listPoints[i].type == "bbox") then
643             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
644          else
645             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
646          end
647       end
648    end
649    return output
650 end
651
652 -- print points to mesh
653 function printPointsMP(chaine,mode,points,bbox,scale)
654    listPoints = buildList(chaine, mode)
655    if(bbox == "bbox" ) then
656       listPoints = buildBoundingBox(listPoints)
657    end
658    output = tracePointsMP(listPoints,points)
659    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
660    tex.sprint(output)
661 end
662
663
664 -- print points to mesh
665 function printPointsMPinc(chaine,beginning, ending, mode,points,bbox,scale)
666    listPoints = buildList(chaine, mode)
667    if(bbox == "bbox" ) then
668       listPoints = buildBoundingBox(listPoints)
669    end
670    output = tracePointsMP(listPoints,points)
671    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
672    tex.sprint(output)
673 end
674
675 -- print points to mesh
676 function printPointsTikZ(chaine,mode,points,bbox,scale,color,colorBbox)
677    listPoints = buildList(chaine, mode)
678    if(bbox == "bbox" ) then
679       listPoints = buildBoundingBox(listPoints)
680    end
681    output = tracePointsTikZ(listPoints,points,color,colorBbox)
682    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
683    tex.sprint(output)
684 end
685
686
687 -- print points to mesh
688 function printPointsTikZinc(chaine,beginning, ending, mode,points,bbox,scale,color,colorBbox)
689    listPoints = buildList(chaine, mode)
690    if(bbox == "bbox" ) then
691       listPoints = buildBoundingBox(listPoints)
692    end
693    output = tracePointsTikZ(listPoints,points,color,colorBbox)
694    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
695    tex.sprint(output)
696 end
697
698
699 -- buildMesh
700 function buildRect(largeur,a,b,nbrA, nbrB)
701    listPoints = rectangleList(a,b,nbrA,nbrB)
702    triangulation = BowyerWatson(listPoints,"none")
703    traceTikZ(listPoints, triangulation,largeur,"none")
704 end
705
706 local function shallowCopy(original)
707    local copy = {}
708    for key, value in pairs(original) do
709       copy[key] = value
710    end
711    return copy
712 end
713
714 -- function give a real polygon without repeting points
715 function cleanPoly(polygon)
716    polyNew = {}
717    polyCopy = shallowCopy(polygon)
718    e1 = polyCopy[1][1]
719    e2 = polyCopy[1][2]
720    table.insert(polyNew, e1)
721    table.insert(polyNew, e2)
722    table.remove(polyCopy,1)
723    j = 2
724    while #polyCopy>1 do
725       i=1
726       find = false
727       while (i<=#polyCopy and find==false) do
728          bool1 = (polyCopy[i][1] == polyNew[j])
729          bool2 = (polyCopy[i][2] == polyNew[j])
730          if(bool1 or bool2) then -- the edge has a common point with polyNew[j]
731             if(not bool1) then
732                table.insert(polyNew, polyCopy[i][1])
733                find = true
734                table.remove(polyCopy,i)
735                j = j+1
736             elseif(not bool2) then
737                table.insert(polyNew, polyCopy[i][2])
738                find = true
739                table.remove(polyCopy,i)
740                j = j+1
741             end
742          end
743          i=i+1
744       end
745    end
746    return polyNew
747 end
748
749 --
750 function TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack, colorNew, colorCircle,colorBbox)
751    output = ""
752    -- build the triangulation
753    triangulation = BowyerWatson(listPoints,bbox)
754    badTriangles = buildBadTriangles(P,triangulation)
755    for i=1,#listPoints do
756       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
757    end
758    if(step == "badT") then
759       -- draw all triangle
760       for i=1,#triangulation do
761          PointI = listPoints[triangulation[i][1]]
762          PointJ = listPoints[triangulation[i][2]]
763          PointK = listPoints[triangulation[i][3]]
764          if(triangulation[i].type == "bbox") then
765             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
766          else
767             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
768          end
769       end
770       -- draw and fill the bad triangle
771       for i=1,#badTriangles do
772          PointI = listPoints[triangulation[badTriangles[i]][1]]
773          PointJ = listPoints[triangulation[badTriangles[i]][2]]
774          PointK = listPoints[triangulation[badTriangles[i]][3]]
775          output = output .. "\\draw[fill="..colorBack.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
776       end
777       -- draw the circoncircle
778       for i=1,#badTriangles do
779          PointI = listPoints[triangulation[badTriangles[i]][1]]
780          PointJ = listPoints[triangulation[badTriangles[i]][2]]
781          PointK = listPoints[triangulation[badTriangles[i]][3]]
782          center, radius = circoncircle(PointI, PointJ, PointK)
783          output = output .. "\\draw[dashed, color="..colorCircle.."] ("..center.x .. "," .. center.y .. ") circle ("..radius ..");"
784       end
785       -- mark the points
786       j=1
787       for i=1,#listPoints do
788          if(listPoints[i].type == "bbox") then
789             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
790             j = j+1
791          else
792             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
793          end
794       end
795       -- mark the point to add
796       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
797    elseif(step == "cavity") then
798       polygon = buildCavity(badTriangles, triangulation)
799       polyNew = cleanPoly(polygon)
800       -- remove the bad triangles
801       for j=1,#badTriangles do
802          table.remove(triangulation,badTriangles[j]-(j-1))
803       end
804       -- draw the triangles
805       for i=1,#triangulation do
806          PointI = listPoints[triangulation[i][1]]
807          PointJ = listPoints[triangulation[i][2]]
808          PointK = listPoints[triangulation[i][3]]
809          if(triangulation[i].type == "bbox") then
810             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
811          else
812             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
813          end
814       end
815       -- fill and draw the cavity
816       path = ""
817       for i=1,#polyNew do
818          PointI = listPoints[polyNew[i]]
819          path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
820       end
821       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
822       -- mark the points of the mesh
823       j=1
824       for i=1,#listPoints do
825          if(listPoints[i].type == "bbox") then
826             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
827             j=j+1
828          else
829             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
830          end
831       end
832       -- mark the adding point
833       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
834    elseif(step == "newT") then
835       polygon = buildCavity(badTriangles, triangulation)
836       polyNew = cleanPoly(polygon)
837       -- remove the bad triangles
838       for j=1,#badTriangles do
839          table.remove(triangulation,badTriangles[j]-(j-1))
840       end
841       -- draw the triangle of the triangulation
842       for i=1,#triangulation do
843          PointI = listPoints[triangulation[i][1]]
844          PointJ = listPoints[triangulation[i][2]]
845          PointK = listPoints[triangulation[i][3]]
846          if(triangulation[i].type == "bbox") then
847             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
848          else
849             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
850          end
851       end
852       -- fill and draw the cavity
853       path = ""
854       for i=1,#polyNew do
855          PointI = listPoints[polyNew[i]]
856          path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
857       end
858       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
859       -- draw the new triangles composed by the edges of the polygon and the added point
860       for i=1,#polygon do
861          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 ..");"
862          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][1]].x .. "," .. listPoints[polygon[i][1]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
863          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][2]].x .. "," .. listPoints[polygon[i][2]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
864       end
865       -- mark points
866       j=1
867       for i=1,#listPoints do
868          if(listPoints[i].type == "bbox") then
869             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
870             j=j+1
871          else
872             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
873          end
874       end
875       -- mark the added point
876       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
877    end
878    return output
879 end
880
881 function TeXaddOnePointMPBW(listPoints,P,step,bbox)
882    output = "";
883    output = output .. "pair MeshPoints[];"
884    -- build the triangulation
885    triangulation = BowyerWatson(listPoints,bbox)
886    badTriangles = buildBadTriangles(P,triangulation)
887    for i=1,#listPoints do
888       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
889    end
890    if(step == "badT") then
891       -- draw all triangle
892       for i=1,#triangulation do
893          PointI = listPoints[triangulation[i][1]]
894          PointJ = listPoints[triangulation[i][2]]
895          PointK = listPoints[triangulation[i][3]]
896          if(triangulation[i].type == "bbox") then
897             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
898          else
899             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
900          end
901       end
902       -- draw and fill the bad triangle
903       for i=1,#badTriangles do
904          PointI = listPoints[triangulation[badTriangles[i]][1]]
905          PointJ = listPoints[triangulation[badTriangles[i]][2]]
906          PointK = listPoints[triangulation[badTriangles[i]][3]]
907          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
908          output = output .. "fill (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBack;"
909       end
910       -- draw the circoncircle
911       for i=1,#badTriangles do
912          PointI = listPoints[triangulation[badTriangles[i]][1]]
913          PointJ = listPoints[triangulation[badTriangles[i]][2]]
914          PointK = listPoints[triangulation[badTriangles[i]][3]]
915          center, radius = circoncircle(PointI, PointJ, PointK)
916          output = output .. "draw fullcircle scaled ("..radius .."*2u) shifted ("..center.x .. "*u," .. center.y .. "*u) dashed evenly withcolor \\luameshmpcolorCircle;"
917       end
918       -- mark the points
919       j=1
920       for i=1,#listPoints do
921          if(listPoints[i].type == "bbox") then
922             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
923             j=j+1
924          else
925             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
926          end
927       end
928       -- mark the point to add
929       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew;"
930    elseif(step == "cavity") then
931       polygon = buildCavity(badTriangles, triangulation)
932       polyNew = cleanPoly(polygon)
933       -- remove the bad triangles
934       for j=1,#badTriangles do
935          table.remove(triangulation,badTriangles[j]-(j-1))
936       end
937       -- draw the triangles
938       for i=1,#triangulation do
939          PointI = listPoints[triangulation[i][1]]
940          PointJ = listPoints[triangulation[i][2]]
941          PointK = listPoints[triangulation[i][3]]
942          if(triangulation[i].type == "bbox") then
943             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
944          else
945             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
946          end
947       end
948       -- fill and draw the cavity
949       path = ""
950       for i=1,#polyNew do
951          PointI = listPoints[polyNew[i]]
952          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
953       end
954       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
955       output = output .. "draw " .. path .. "cycle withcolor \\luameshmpcolorNew  withpen pencircle scaled 1pt;"
956       -- mark the points of the mesh
957       j=1
958       for i=1,#listPoints do
959          if(listPoints[i].type == "bbox") then
960             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
961             j=j+1
962          else
963             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
964          end
965       end
966       -- mark the adding point
967       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
968    elseif(step == "newT") then
969       polygon = buildCavity(badTriangles, triangulation)
970       polyNew = cleanPoly(polygon)
971       -- remove the bad triangles
972       for j=1,#badTriangles do
973          table.remove(triangulation,badTriangles[j]-(j-1))
974       end
975       -- draw the triangle of the triangulation
976       for i=1,#triangulation do
977          PointI = listPoints[triangulation[i][1]]
978          PointJ = listPoints[triangulation[i][2]]
979          PointK = listPoints[triangulation[i][3]]
980          if(triangulation[i].type == "bbox") then
981             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
982          else
983             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
984          end
985       end
986       -- fill  the cavity
987       path = ""
988       for i=1,#polyNew do
989          PointI = listPoints[polyNew[i]]
990          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
991       end
992       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
993       -- draw the new triangles composed by the edges of the polygon and the added point
994       for i=1,#polygon do
995          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;"
996          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;"
997          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;"
998       end
999       -- mark points
1000       j=1
1001       for i=1,#listPoints do
1002          if(listPoints[i].type == "bbox") then
1003             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
1004             j=j+1
1005          else
1006             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
1007          end
1008       end
1009       -- mark the added point
1010       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
1011    end
1012    return output
1013 end
1014
1015
1016 -- build the list of points extern and stop at nbr
1017 function buildListExt(chaine, stop)
1018    listPoints = {}
1019    io.input(chaine) -- open the file
1020    text=io.read("*all")
1021    lines=string.explode(text,"\n+") -- all the lines
1022    for i=1,tonumber(stop) do
1023       xy=string.explode(lines[i]," +")
1024       table.insert(listPoints,{x=tonumber(xy[1]),y=tonumber(xy[2])})
1025    end
1026    xy=string.explode(lines[stop+1]," +")
1027    point={x=tonumber(xy[1]),y=tonumber(xy[2])}
1028    return point, listPoints
1029 end
1030
1031
1032 function TeXOnePointTikZBW(chaine,point,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
1033    if(mode=="int") then
1034       Sx,Sy=string.match(point,"%((.+),(.+)%)")
1035       P = {x=Sx, y=Sy}
1036       listPoints = buildList(chaine, mode)
1037    else
1038       -- point is a number
1039       P, listPoints = buildListExt(chaine,tonumber(point))
1040    end
1041    output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
1042    output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. output .. "\\end{tikzpicture}"
1043    tex.sprint(output)
1044 end
1045
1046 function TeXOnePointTikZBWinc(chaine,point,beginning, ending,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
1047    if(mode=="int") then
1048       Sx,Sy=string.match(point,"%((.+),(.+)%)")
1049       P = {x=Sx, y=Sy}
1050       listPoints = buildList(chaine, mode)
1051    else
1052       -- point is a number
1053       P, listPoints = buildListExt(chaine,tonumber(point))
1054    end
1055    output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
1056    output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. beginning..output ..ending.. "\\end{tikzpicture}"
1057    tex.sprint(output)
1058 end
1059
1060 function TeXOnePointMPBW(chaine,point,step,scale,mode,bbox)
1061    if(mode=="int") then
1062       Sx,Sy=string.match(point,"%((.+),(.+)%)")
1063       P = {x=Sx, y=Sy}
1064       listPoints = buildList(chaine, mode)
1065    else
1066       -- point is a number
1067       P, listPoints = buildListExt(chaine,tonumber(point))
1068    end
1069    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
1070    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale..";".. output .. "endfig;\\end{mplibcode}"
1071    tex.sprint(output)
1072 end
1073
1074 function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,bbox)
1075    if(mode=="int") then
1076       Sx,Sy=string.match(point,"%((.+),(.+)%)")
1077       P = {x=Sx, y=Sy}
1078       listPoints = buildList(chaine, mode)
1079    else
1080       -- point is a number
1081       P, listPoints = buildListExt(chaine,tonumber(point))
1082    end
1083    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
1084    output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
1085    tex.sprint(output)
1086 end
1087
1088
1089 function split(pString, pPattern)
1090    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
1091    local fpat = "(.-)" .. pPattern
1092    local last_end = 1
1093    local s, e, cap = pString:find(fpat, 1)
1094    while s do
1095       if s ~= 1 or cap ~= "" then
1096          table.insert(Table,cap)
1097       end
1098       last_end = e+1
1099       s, e, cap = pString:find(fpat, last_end)
1100    end
1101    if last_end <= #pString then
1102       cap = pString:sub(last_end)
1103       table.insert(Table, cap)
1104    end
1105    return Table
1106 end
1107
1108 function readGmsh(file)
1109    io.input(file) -- open the file
1110    text=io.read("*all")
1111    local lines = split(text,"\n+") -- all the lines
1112    listPoints={}
1113    triangulation ={}
1114    boolNodes = false
1115    Jnodes = 0
1116    boolElements = false
1117    Jelements = 0
1118    J=0
1119    for i=1,#lines-J do
1120       if(lines[i+J] == "$EndNodes") then
1121          boolNodes = false
1122          -- go to the next line
1123       end
1124       if(boolNodes) then -- we are in the Nodes environment
1125          xy=split(lines[i+J]," +")
1126          table.insert(listPoints,{x=tonumber(xy[2]),y=tonumber(xy[3])})
1127       end
1128       if(lines[i+J] == "$Nodes") then
1129          boolNodes = true
1130          -- go to the next line
1131          J=J+1
1132       end
1133       if(lines[i+J] == "$EndElements") then
1134          boolElements = false
1135          -- go to the next line
1136       end
1137       if(boolElements) then -- we are in the Nodes environment
1138          xy=split(lines[i+J]," +")
1139          if(tonumber(xy[2]) == 2) then -- if the element is a triangle
1140             nbrTags = xy[3]+1
1141             table.insert(triangulation,{tonumber(xy[2+nbrTags+1]),tonumber(xy[2+nbrTags+2]),tonumber(xy[2+nbrTags+3])})
1142          end
1143       end
1144       if(lines[i+J] == "$Elements") then
1145          boolElements = true
1146          -- go to the next line
1147          J=J+1
1148       end
1149    end
1150    return listPoints, triangulation
1151 end
1152
1153 function drawGmshMP(file,points,scale)
1154    listPoints,triangulation = readGmsh(file)
1155    output = traceMeshMP(listPoints,triangulation,points)
1156    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
1157    tex.sprint(output)
1158 end
1159
1160 function drawGmshMPinc(file,beginning,ending,points,scale)
1161    listPoints,triangulation = readGmsh(file)
1162    output = traceMeshMP(listPoints,triangulation,points)
1163    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
1164    tex.sprint(output)
1165 end
1166
1167
1168
1169 --
1170 function drawGmshTikZ(file,points,scale,color)
1171    listPoints,triangulation = readGmsh(file)
1172    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
1173    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
1174    tex.sprint(output)
1175 end
1176
1177 --
1178 function drawGmshTikZinc(file,beginning, ending,points,scale,color)
1179    listPoints,triangulation = readGmsh(file)
1180    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
1181    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
1182    tex.sprint(output)
1183 end
1184
1185
1186 -- buildVoronoi with MP
1187 function gmshVoronoiMP(file,points,scale,tri)
1188    listPoints,triangulation = readGmsh(file)
1189    listVoronoi = buildVoronoi(listPoints, triangulation)
1190    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri)
1191    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
1192    tex.sprint(output)
1193 end
1194
1195
1196 -- buildVoronoi with TikZ
1197 function gmshVoronoiTikZ(file,points,scale,tri,color,colorVoronoi)
1198    listPoints,triangulation = readGmsh(file)
1199    listVoronoi = buildVoronoi(listPoints, triangulation)
1200    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi)
1201    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"   tex.sprint(output)
1202 end
1203
1204
1205 -- buildVoronoi with MP
1206 function gmshVoronoiMPinc(file,beginning, ending,points,scale,tri)
1207    listPoints,triangulation = readGmsh(file)
1208    listVoronoi = buildVoronoi(listPoints, triangulation)
1209    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri)
1210    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
1211    tex.sprint(output)
1212 end
1213
1214
1215 -- buildVoronoi with TikZ
1216 function gmshVoronoiTikZinc(file,beginning, ending,points,scale,tri,color,colorVoronoi)
1217    listPoints,triangulation = readGmsh(file)
1218    listVoronoi = buildVoronoi(listPoints, triangulation)
1219    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi)
1220    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
1221    tex.sprint(output)
1222 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.