Fonctions (TikZ et MP) inc pour Voronoi
[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             if(listPoints[i].type == "bbox") then
424                output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
425                j=j+1
426             else
427                output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
428             end
429          end
430       end
431       for i=1,#listCircumC do
432          output = output .. "\\draw[color="..colorVoronoi.."] (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\CircumPoint_{" .. i .. "}$};"
433       end
434    else
435       j=1
436       for i=1,#listPoints do
437          if(listPoints[i].type == "bbox") then
438             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
439             j=j+1
440          else
441             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
442          end
443       end
444       for i=1,#listCircumC do
445          output = output .. "\\draw[color="..colorVoronoi.."] (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ") node {$\\bullet$};"
446       end
447    end
448    return output
449 end
450
451
452
453 -- buildVoronoi with MP
454 function buildVoronoiMPBW(chaine,mode,points,bbox,scale,tri)
455    listPoints = buildList(chaine, mode)
456    triangulation = BowyerWatson(listPoints,bbox)
457    listVoronoi = buildVoronoi(listPoints, triangulation)
458    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri)
459    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
460    tex.sprint(output)
461 end
462
463
464 -- buildVoronoi with TikZ
465 function buildVoronoiTikZBW(chaine,mode,points,bbox,scale,tri,color,colorBbox,colorVoronoi)
466    listPoints = buildList(chaine, mode)
467    triangulation = BowyerWatson(listPoints,bbox)
468    listVoronoi = buildVoronoi(listPoints, triangulation)
469    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi)
470    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"   tex.sprint(output)
471 end
472
473
474 -- buildVoronoi with MP
475 function buildVoronoiMPBWinc(chaine,beginning, ending,mode,points,bbox,scale,tri)
476    listPoints = buildList(chaine, mode)
477    triangulation = BowyerWatson(listPoints,bbox)
478    listVoronoi = buildVoronoi(listPoints, triangulation)
479    output = traceVoronoiMP(listPoints,triangulation,listVoronoi,points,tri)
480    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
481    tex.sprint(output)
482 end
483
484
485 -- buildVoronoi with TikZ
486 function buildVoronoiTikZBWinc(chaine,beginning, ending,mode,points,bbox,scale,tri,color,colorBbox,colorVoronoi)
487    listPoints = buildList(chaine, mode)
488    triangulation = BowyerWatson(listPoints,bbox)
489    listVoronoi = buildVoronoi(listPoints, triangulation)
490    output = traceVoronoiTikZ(listPoints,triangulation,listVoronoi,points,tri,color,colorBbox,colorVoronoi)
491    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
492    tex.sprint(output)
493 end
494
495
496
497 -- trace a triangulation with TikZ
498 function traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
499    output = ""
500    for i=1,#listPoints do
501       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
502    end
503    for i=1,#triangulation do
504       PointI = listPoints[triangulation[i][1]]
505       PointJ = listPoints[triangulation[i][2]]
506       PointK = listPoints[triangulation[i][3]]
507       if(triangulation[i].type == "bbox") then
508          output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
509       else
510          output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
511       end
512    end
513    if(points=="points") then
514       j=1
515       for i=1,#listPoints do
516          if(listPoints[i].type == "bbox") then
517             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
518             j=j+1
519          else
520             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
521          end
522       end
523    end
524    return output
525 end
526
527
528 -- trace a triangulation with MP
529 function traceMeshMP(listPoints, triangulation,points)
530    output = "";
531    output = output .. " pair MeshPoints[];"
532    for i=1,#listPoints do
533       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
534    end
535
536    for i=1,#triangulation do
537       PointI = listPoints[triangulation[i][1]]
538       PointJ = listPoints[triangulation[i][2]]
539       PointK = listPoints[triangulation[i][3]]
540       if(triangulation[i].type == "bbox") then
541          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
542       else
543          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
544       end
545    end
546    if(points=="points") then
547       j=1
548       for i=1,#listPoints do
549          if(listPoints[i].type == "bbox") then
550             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
551             j=j+1
552          else
553             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
554          end
555       end
556    end
557    return output
558 end
559
560
561 -- buildMesh with MP
562 function buildMeshMPBW(chaine,mode,points,bbox,scale)
563    listPoints = buildList(chaine, mode)
564    triangulation = BowyerWatson(listPoints,bbox)
565    output = traceMeshMP(listPoints, triangulation,points)
566    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
567    tex.sprint(output)
568 end
569
570 -- buildMesh with MP include code
571 function buildMeshMPBWinc(chaine,beginning, ending,mode,points,bbox,scale)
572    listPoints = buildList(chaine, mode)
573    triangulation = BowyerWatson(listPoints,bbox)
574    output = traceMeshMP(listPoints, triangulation,points)
575    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
576    tex.sprint(output)
577 end
578
579 -- buildMesh with TikZ
580 function buildMeshTikZBW(chaine,mode,points,bbox,scale,color,colorBbox)
581    listPoints = buildList(chaine, mode)
582    triangulation = BowyerWatson(listPoints,bbox)
583    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
584    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
585    tex.sprint(output)
586 end
587
588 -- buildMesh with TikZ
589 function buildMeshTikZBWinc(chaine,beginning, ending,mode,points,bbox,scale,color,colorBbox)
590    listPoints = buildList(chaine, mode)
591    triangulation = BowyerWatson(listPoints,bbox)
592    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
593    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
594    tex.sprint(output)
595 end
596
597
598 -- print points of the mesh
599 function tracePointsMP(listPoints,points)
600    output = "";
601    output = output .. " pair MeshPoints[];"
602    for i=1,#listPoints do
603       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
604    end
605    if(points=="points") then
606       j=1
607       for i=1,#listPoints do
608          if(listPoints[i].type == "bbox") then
609             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
610             j=j+1
611          else
612             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
613          end
614       end
615    else
616       for i=1,#listPoints do
617          if(listPoints[i].type == "bbox") then
618             output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
619          else
620             output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolor withpen pencircle scaled 3;"
621          end
622       end
623    end
624    return output
625 end
626
627 -- print points of the mesh
628 function tracePointsTikZ(listPoints,points,color,colorBbox)
629    output = "";
630    for i=1,#listPoints do
631       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
632    end
633    if(points=="points") then
634       j=1
635       for i=1,#listPoints do
636          if(listPoints[i].type == "bbox") then
637             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
638             j = j+1
639          else
640             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
641          end
642       end
643    else
644       for i=1,#listPoints do
645          if(listPoints[i].type == "bbox") then
646             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
647          else
648             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
649          end
650       end
651    end
652    return output
653 end
654
655 -- print points to mesh
656 function printPointsMP(chaine,mode,points,bbox,scale)
657    listPoints = buildList(chaine, mode)
658    if(bbox == "bbox" ) then
659       listPoints = buildBoundingBox(listPoints)
660    end
661    output = tracePointsMP(listPoints,points)
662    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
663    tex.sprint(output)
664 end
665
666
667 -- print points to mesh
668 function printPointsMPinc(chaine,beginning, ending, mode,points,bbox,scale)
669    listPoints = buildList(chaine, mode)
670    if(bbox == "bbox" ) then
671       listPoints = buildBoundingBox(listPoints)
672    end
673    output = tracePointsMP(listPoints,points)
674    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
675    tex.sprint(output)
676 end
677
678 -- print points to mesh
679 function printPointsTikZ(chaine,mode,points,bbox,scale,color,colorBbox)
680    listPoints = buildList(chaine, mode)
681    if(bbox == "bbox" ) then
682       listPoints = buildBoundingBox(listPoints)
683    end
684    output = tracePointsTikZ(listPoints,points,color,colorBbox)
685    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
686    tex.sprint(output)
687 end
688
689
690 -- print points to mesh
691 function printPointsTikZinc(chaine,beginning, ending, mode,points,bbox,scale,color,colorBbox)
692    listPoints = buildList(chaine, mode)
693    if(bbox == "bbox" ) then
694       listPoints = buildBoundingBox(listPoints)
695    end
696    output = tracePointsTikZ(listPoints,points,color,colorBbox)
697    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
698    tex.sprint(output)
699 end
700
701
702 -- buildMesh
703 function buildRect(largeur,a,b,nbrA, nbrB)
704    listPoints = rectangleList(a,b,nbrA,nbrB)
705    triangulation = BowyerWatson(listPoints,"none")
706    traceTikZ(listPoints, triangulation,largeur,"none")
707 end
708
709 local function shallowCopy(original)
710    local copy = {}
711    for key, value in pairs(original) do
712       copy[key] = value
713    end
714    return copy
715 end
716
717 -- function give a real polygon without repeting points
718 function cleanPoly(polygon)
719    polyNew = {}
720    polyCopy = shallowCopy(polygon)
721    e1 = polyCopy[1][1]
722    e2 = polyCopy[1][2]
723    table.insert(polyNew, e1)
724    table.insert(polyNew, e2)
725    table.remove(polyCopy,1)
726    j = 2
727    while #polyCopy>1 do
728       i=1
729       find = false
730       while (i<=#polyCopy and find==false) do
731          bool1 = (polyCopy[i][1] == polyNew[j])
732          bool2 = (polyCopy[i][2] == polyNew[j])
733          if(bool1 or bool2) then -- the edge has a common point with polyNew[j]
734             if(not bool1) then
735                table.insert(polyNew, polyCopy[i][1])
736                find = true
737                table.remove(polyCopy,i)
738                j = j+1
739             elseif(not bool2) then
740                table.insert(polyNew, polyCopy[i][2])
741                find = true
742                table.remove(polyCopy,i)
743                j = j+1
744             end
745          end
746          i=i+1
747       end
748    end
749    return polyNew
750 end
751
752 --
753 function TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack, colorNew, colorCircle,colorBbox)
754    output = ""
755    -- build the triangulation
756    triangulation = BowyerWatson(listPoints,bbox)
757    badTriangles = buildBadTriangles(P,triangulation)
758    for i=1,#listPoints do
759       output = output .. "\\coordinate (MeshPoints".. i .. ") at  (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
760    end
761    if(step == "badT") then
762       -- draw all triangle
763       for i=1,#triangulation do
764          PointI = listPoints[triangulation[i][1]]
765          PointJ = listPoints[triangulation[i][2]]
766          PointK = listPoints[triangulation[i][3]]
767          if(triangulation[i].type == "bbox") then
768             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
769          else
770             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
771          end
772       end
773       -- draw and fill the bad triangle
774       for i=1,#badTriangles do
775          PointI = listPoints[triangulation[badTriangles[i]][1]]
776          PointJ = listPoints[triangulation[badTriangles[i]][2]]
777          PointK = listPoints[triangulation[badTriangles[i]][3]]
778          output = output .. "\\draw[fill="..colorBack.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
779       end
780       -- draw the circoncircle
781       for i=1,#badTriangles do
782          PointI = listPoints[triangulation[badTriangles[i]][1]]
783          PointJ = listPoints[triangulation[badTriangles[i]][2]]
784          PointK = listPoints[triangulation[badTriangles[i]][3]]
785          center, radius = circoncircle(PointI, PointJ, PointK)
786          output = output .. "\\draw[dashed, color="..colorCircle.."] ("..center.x .. "," .. center.y .. ") circle ("..radius ..");"
787       end
788       -- mark the points
789       j=1
790       for i=1,#listPoints do
791          if(listPoints[i].type == "bbox") then
792             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
793             j = j+1
794          else
795             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
796          end
797       end
798       -- mark the point to add
799       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
800    elseif(step == "cavity") then
801       polygon = buildCavity(badTriangles, triangulation)
802       polyNew = cleanPoly(polygon)
803       -- remove the bad triangles
804       for j=1,#badTriangles do
805          table.remove(triangulation,badTriangles[j]-(j-1))
806       end
807       -- draw the triangles
808       for i=1,#triangulation do
809          PointI = listPoints[triangulation[i][1]]
810          PointJ = listPoints[triangulation[i][2]]
811          PointK = listPoints[triangulation[i][3]]
812          if(triangulation[i].type == "bbox") then
813             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
814          else
815             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
816          end
817       end
818       -- fill and draw the cavity
819       path = ""
820       for i=1,#polyNew do
821          PointI = listPoints[polyNew[i]]
822          path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
823       end
824       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
825       -- mark the points of the mesh
826       j=1
827       for i=1,#listPoints do
828          if(listPoints[i].type == "bbox") then
829             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
830             j=j+1
831          else
832             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
833          end
834       end
835       -- mark the adding point
836       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
837    elseif(step == "newT") then
838       polygon = buildCavity(badTriangles, triangulation)
839       polyNew = cleanPoly(polygon)
840       -- remove the bad triangles
841       for j=1,#badTriangles do
842          table.remove(triangulation,badTriangles[j]-(j-1))
843       end
844       -- draw the triangle of the triangulation
845       for i=1,#triangulation do
846          PointI = listPoints[triangulation[i][1]]
847          PointJ = listPoints[triangulation[i][2]]
848          PointK = listPoints[triangulation[i][3]]
849          if(triangulation[i].type == "bbox") then
850             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
851          else
852             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
853          end
854       end
855       -- fill and draw the cavity
856       path = ""
857       for i=1,#polyNew do
858          PointI = listPoints[polyNew[i]]
859          path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
860       end
861       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
862       -- draw the new triangles composed by the edges of the polygon and the added point
863       for i=1,#polygon do
864          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 ..");"
865          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][1]].x .. "," .. listPoints[polygon[i][1]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
866          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][2]].x .. "," .. listPoints[polygon[i][2]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
867       end
868       -- mark points
869       j=1
870       for i=1,#listPoints do
871          if(listPoints[i].type == "bbox") then
872             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
873             j=j+1
874          else
875             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
876          end
877       end
878       -- mark the added point
879       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
880    end
881    return output
882 end
883
884 function TeXaddOnePointMPBW(listPoints,P,step,bbox)
885    output = "";
886    output = output .. "pair MeshPoints[];"
887    -- build the triangulation
888    triangulation = BowyerWatson(listPoints,bbox)
889    badTriangles = buildBadTriangles(P,triangulation)
890    for i=1,#listPoints do
891       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
892    end
893    if(step == "badT") then
894       -- draw all triangle
895       for i=1,#triangulation do
896          PointI = listPoints[triangulation[i][1]]
897          PointJ = listPoints[triangulation[i][2]]
898          PointK = listPoints[triangulation[i][3]]
899          if(triangulation[i].type == "bbox") then
900             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
901          else
902             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
903          end
904       end
905       -- draw and fill the bad triangle
906       for i=1,#badTriangles do
907          PointI = listPoints[triangulation[badTriangles[i]][1]]
908          PointJ = listPoints[triangulation[badTriangles[i]][2]]
909          PointK = listPoints[triangulation[badTriangles[i]][3]]
910          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
911          output = output .. "fill (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBack;"
912       end
913       -- draw the circoncircle
914       for i=1,#badTriangles do
915          PointI = listPoints[triangulation[badTriangles[i]][1]]
916          PointJ = listPoints[triangulation[badTriangles[i]][2]]
917          PointK = listPoints[triangulation[badTriangles[i]][3]]
918          center, radius = circoncircle(PointI, PointJ, PointK)
919          output = output .. "draw fullcircle scaled ("..radius .."*2u) shifted ("..center.x .. "*u," .. center.y .. "*u) dashed evenly withcolor \\luameshmpcolorCircle;"
920       end
921       -- mark the points
922       j=1
923       for i=1,#listPoints do
924          if(listPoints[i].type == "bbox") then
925             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
926             j=j+1
927          else
928             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
929          end
930       end
931       -- mark the point to add
932       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew;"
933    elseif(step == "cavity") then
934       polygon = buildCavity(badTriangles, triangulation)
935       polyNew = cleanPoly(polygon)
936       -- remove the bad triangles
937       for j=1,#badTriangles do
938          table.remove(triangulation,badTriangles[j]-(j-1))
939       end
940       -- draw the triangles
941       for i=1,#triangulation do
942          PointI = listPoints[triangulation[i][1]]
943          PointJ = listPoints[triangulation[i][2]]
944          PointK = listPoints[triangulation[i][3]]
945          if(triangulation[i].type == "bbox") then
946             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
947          else
948             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
949          end
950       end
951       -- fill and draw the cavity
952       path = ""
953       for i=1,#polyNew do
954          PointI = listPoints[polyNew[i]]
955          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
956       end
957       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
958       output = output .. "draw " .. path .. "cycle withcolor \\luameshmpcolorNew  withpen pencircle scaled 1pt;"
959       -- mark the points of the mesh
960       j=1
961       for i=1,#listPoints do
962          if(listPoints[i].type == "bbox") then
963             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
964             j=j+1
965          else
966             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
967          end
968       end
969       -- mark the adding point
970       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
971    elseif(step == "newT") then
972       polygon = buildCavity(badTriangles, triangulation)
973       polyNew = cleanPoly(polygon)
974       -- remove the bad triangles
975       for j=1,#badTriangles do
976          table.remove(triangulation,badTriangles[j]-(j-1))
977       end
978       -- draw the triangle of the triangulation
979       for i=1,#triangulation do
980          PointI = listPoints[triangulation[i][1]]
981          PointJ = listPoints[triangulation[i][2]]
982          PointK = listPoints[triangulation[i][3]]
983          if(triangulation[i].type == "bbox") then
984             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
985          else
986             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
987          end
988       end
989       -- fill  the cavity
990       path = ""
991       for i=1,#polyNew do
992          PointI = listPoints[polyNew[i]]
993          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
994       end
995       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
996       -- draw the new triangles composed by the edges of the polygon and the added point
997       for i=1,#polygon do
998          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;"
999          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;"
1000          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;"
1001       end
1002       -- mark points
1003       j=1
1004       for i=1,#listPoints do
1005          if(listPoints[i].type == "bbox") then
1006             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
1007             j=j+1
1008          else
1009             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
1010          end
1011       end
1012       -- mark the added point
1013       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
1014    end
1015    return output
1016 end
1017
1018
1019 -- build the list of points extern and stop at nbr
1020 function buildListExt(chaine, stop)
1021    listPoints = {}
1022    io.input(chaine) -- open the file
1023    text=io.read("*all")
1024    lines=string.explode(text,"\n+") -- all the lines
1025    for i=1,tonumber(stop) do
1026       xy=string.explode(lines[i]," +")
1027       table.insert(listPoints,{x=tonumber(xy[1]),y=tonumber(xy[2])})
1028    end
1029    xy=string.explode(lines[stop+1]," +")
1030    point={x=tonumber(xy[1]),y=tonumber(xy[2])}
1031    return point, listPoints
1032 end
1033
1034
1035 function TeXOnePointTikZBW(chaine,point,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
1036    if(mode=="int") then
1037       Sx,Sy=string.match(point,"%((.+),(.+)%)")
1038       P = {x=Sx, y=Sy}
1039       listPoints = buildList(chaine, mode)
1040    else
1041       -- point is a number
1042       P, listPoints = buildListExt(chaine,tonumber(point))
1043    end
1044    output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
1045    output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. output .. "\\end{tikzpicture}"
1046    tex.sprint(output)
1047 end
1048
1049 function TeXOnePointTikZBWinc(chaine,point,beginning, ending,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
1050    if(mode=="int") then
1051       Sx,Sy=string.match(point,"%((.+),(.+)%)")
1052       P = {x=Sx, y=Sy}
1053       listPoints = buildList(chaine, mode)
1054    else
1055       -- point is a number
1056       P, listPoints = buildListExt(chaine,tonumber(point))
1057    end
1058    output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
1059    output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. beginning..output ..ending.. "\\end{tikzpicture}"
1060    tex.sprint(output)
1061 end
1062
1063 function TeXOnePointMPBW(chaine,point,step,scale,mode,bbox)
1064    if(mode=="int") then
1065       Sx,Sy=string.match(point,"%((.+),(.+)%)")
1066       P = {x=Sx, y=Sy}
1067       listPoints = buildList(chaine, mode)
1068    else
1069       -- point is a number
1070       P, listPoints = buildListExt(chaine,tonumber(point))
1071    end
1072    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
1073    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale..";".. output .. "endfig;\\end{mplibcode}"
1074    tex.sprint(output)
1075 end
1076
1077 function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,bbox)
1078    if(mode=="int") then
1079       Sx,Sy=string.match(point,"%((.+),(.+)%)")
1080       P = {x=Sx, y=Sy}
1081       listPoints = buildList(chaine, mode)
1082    else
1083       -- point is a number
1084       P, listPoints = buildListExt(chaine,tonumber(point))
1085    end
1086    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
1087    output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
1088    tex.sprint(output)
1089 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.