warning pour l'index, et TODO list modifiée
[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
211 -------------------------- TeX
212 -- build the list of points
213 function buildList(chaine, mode)
214    -- if mode = int : the list is given in the chaine string (x1,y1);(x2,y2);...;(xn,yn)
215    -- if mode = ext : the list is given in a file line by line with space separation
216    listPoints = {}
217    if mode == "int" then
218       local points = string.explode(chaine, ";")
219       local lgth=#points
220       for i=1,lgth do
221          Sx,Sy=string.match(points[i],"%((.+),(.+)%)")
222          listPoints[i]={x=tonumber(Sx),y=tonumber(Sy)}
223       end
224    elseif mode == "ext" then
225       io.input(chaine) -- open the file
226       text=io.read("*all")
227       lines=string.explode(text,"\n+") -- all the lines
228       tablePoints={}
229       for i=1,#lines do
230          xy=string.explode(lines[i]," +")
231          listPoints[i]={x=tonumber(xy[1]),y=tonumber(xy[2])}
232       end
233    else
234       print("Non existing mode")
235    end
236    return listPoints
237 end
238
239
240 --
241 function rectangleList(a,b,nbrA,nbrB)
242    stepA = a/nbrA
243    stepB = b/nbrB
244    listPoints = {}
245    k=1
246    for i=1,(nbrA+1) do
247       for j=1,(nbrB+1) do
248          listPoints[k] = {x = (i-1)*stepA, y=(j-1)*stepB}
249          k=k+1
250       end
251    end
252    return listPoints
253 end
254
255 -- trace a triangulation with TikZ
256 function traceMeshTikZ(listPoints, triangulation,points,color)
257    output = ""
258    for i=1,#triangulation do
259       PointI = listPoints[triangulation[i][1]]
260       PointJ = listPoints[triangulation[i][2]]
261       PointK = listPoints[triangulation[i][3]]
262       output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
263    end
264    if(points=="points") then
265       for i=1,#listPoints do
266          output = output .. "\\draw[color=".."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
267       end
268    end
269    return output
270 end
271
272
273 -- trace a triangulation with MP
274 function traceMeshMP(listPoints, triangulation,points)
275    output = "";
276    output = output .. " pair MeshPoints[];"
277    for i=1,#listPoints do
278       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
279    end
280
281    for i=1,#triangulation do
282       PointI = listPoints[triangulation[i][1]]
283       PointJ = listPoints[triangulation[i][2]]
284       PointK = listPoints[triangulation[i][3]]
285       if(triangulation[i].type == "bbox") then
286          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
287       else
288          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
289       end
290    end
291    if(points=="points") then
292       j=1
293       for i=1,#listPoints do
294          if(listPoints[i].type == "bbox") then
295             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
296             j=j+1
297          else
298             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
299          end
300       end
301    end
302    return output
303 end
304
305
306 -- print points of the mesh
307 function tracePointsMP(listPoints,points)
308    output = "";
309    output = output .. " pair MeshPoints[];"
310    for i=1,#listPoints do
311       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
312    end
313    if(points=="points") then
314       j=1
315       for i=1,#listPoints do
316          if(listPoints[i].type == "bbox") then
317             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
318             j=j+1
319          else
320             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
321          end
322       end
323    else
324       for i=1,#listPoints do
325          if(listPoints[i].type == "bbox") then
326             output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
327          else
328             output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
329          end
330       end
331    end
332    return output
333 end
334
335
336
337 -- buildMesh with TikZ
338 function buildMeshTikZ(chaine,mode,points,bbox,full,scale,color)
339    listPoints = buildList(chaine, mode)
340    triangulation = BowyerWatson(listPoints,bbox)
341    output = traceMeshTikZ(listPoints, triangulation,points,color)
342    if(full=="full") then
343       output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
344    end
345    tex.sprint(output)
346 end
347
348
349 -- buildMesh with MP
350 function buildMeshMPBW(chaine,mode,points,bbox,scale)
351    listPoints = buildList(chaine, mode)
352    triangulation = BowyerWatson(listPoints,bbox)
353    output = traceMeshMP(listPoints, triangulation,points)
354    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
355    tex.sprint(output)
356 end
357
358 -- print points to mesh
359 function printPointsMP(chaine,mode,points,bbox,scale)
360    listPoints = buildList(chaine, mode)
361    if(bbox == "bbox" ) then
362       listPoints = buildBoundingBox(listPoints)
363    end
364    output = tracePointsMP(listPoints,points)
365    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
366    tex.sprint(output)
367 end
368
369
370 -- print points to mesh
371 function printPointsMPinc(chaine,beginning, ending, mode,points,bbox,scale)
372    listPoints = buildList(chaine, mode)
373    if(bbox == "bbox" ) then
374       listPoints = buildBoundingBox(listPoints)
375    end
376    output = tracePointsMP(listPoints,points)
377    output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
378    tex.sprint(output)
379 end
380
381
382 -- buildMesh with MP include code
383 function buildMeshMPBWinc(chaine,beginning, ending,mode,points,bbox,scale)
384    listPoints = buildList(chaine, mode)
385    triangulation = BowyerWatson(listPoints,bbox)
386    output = traceMeshMP(listPoints, triangulation,points)
387    output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
388    tex.sprint(output)
389 end
390
391 -- buildMesh
392 function buildRect(largeur,a,b,nbrA, nbrB)
393    listPoints = rectangleList(a,b,nbrA,nbrB)
394    triangulation = BowyerWatson(listPoints,"none")
395    traceTikZ(listPoints, triangulation,largeur,"none")
396 end
397
398 local function shallowCopy(original)
399    local copy = {}
400    for key, value in pairs(original) do
401       copy[key] = value
402    end
403    return copy
404 end
405
406 -- function give a real polygon without repeting points
407 function cleanPoly(polygon)
408    polyNew = {}
409    polyCopy = shallowCopy(polygon)
410    e1 = polyCopy[1][1]
411    e2 = polyCopy[1][2]
412    table.insert(polyNew, e1)
413    table.insert(polyNew, e2)
414    table.remove(polyCopy,1)
415    j = 2
416    while #polyCopy>1 do
417       i=1
418       find = false
419       while (i<=#polyCopy and find==false) do
420          i = i+1
421          bool1 = (polyCopy[i][1] == polyNew[j])
422          bool2 = (polyCopy[i][2] == polyNew[j])
423          if(bool1 or bool2) then -- the edge has a common point with polyNew[j]
424             if(not bool1) then
425                table.insert(polyNew, polyCopy[i][1])
426                find = true
427                table.remove(polyCopy,i)
428                j = j+1
429             elseif(not bool2) then
430                table.insert(polyNew, polyCopy[i][2])
431                find = true
432                table.remove(polyCopy,i)
433                j = j+1
434             end
435          end
436       end
437    end
438    return polyNew
439 end
440
441 --
442 function TeXaddOnePointTikZ(chaine,point,step,color,colorBack, colorNew, colorCircle)
443    Sx,Sy=string.match(point,"%((.+),(.+)%)")
444    P = {x=Sx, y=Sy}
445    output = ""
446    listPoints = buildList(chaine, "int")
447    -- build the triangulation
448    triangulation = BowyerWatson(listPoints,"none")
449    badTriangles = buildBadTriangles(P,triangulation)
450    if(step == "badT") then
451       -- draw all triangle
452       for i=1,#triangulation do
453          PointI = listPoints[triangulation[i][1]]
454          PointJ = listPoints[triangulation[i][2]]
455          PointK = listPoints[triangulation[i][3]]
456          output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
457       end
458       -- draw and fill the bad triangle
459       for i=1,#badTriangles do
460          PointI = listPoints[triangulation[badTriangles[i]][1]]
461          PointJ = listPoints[triangulation[badTriangles[i]][2]]
462          PointK = listPoints[triangulation[badTriangles[i]][3]]
463          output = output .. "\\draw[fill="..colorBack.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
464       end
465       -- draw the circoncircle
466       for i=1,#badTriangles do
467          PointI = listPoints[triangulation[badTriangles[i]][1]]
468          PointJ = listPoints[triangulation[badTriangles[i]][2]]
469          PointK = listPoints[triangulation[badTriangles[i]][3]]
470          center, radius = circoncircle(PointI, PointJ, PointK)
471          output = output .. "\\draw[dashed, color="..colorCircle.."] ("..center.x .. "," .. center.y .. ") circle ("..radius ..");"
472       end
473       -- mark the points
474       for i=1,#listPoints do
475          output = output .. "\\draw[color ="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
476       end
477       -- mark the point to add
478       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
479    elseif(step == "cavity") then
480       polygon = buildCavity(badTriangles, triangulation)
481       polyNew = cleanPoly(polygon)
482       -- remove the bad triangles
483       for j=1,#badTriangles do
484          table.remove(triangulation,badTriangles[j]-(j-1))
485       end
486       -- draw the triangles
487       for i=1,#triangulation do
488          PointI = listPoints[triangulation[i][1]]
489          PointJ = listPoints[triangulation[i][2]]
490          PointK = listPoints[triangulation[i][3]]
491          output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
492       end
493       -- fill and draw the cavity
494       path = ""
495       for i=1,#polyNew do
496          PointI = listPoints[polyNew[i]]
497          path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
498       end
499       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
500       -- mark the points of the mesh
501       for i=1,#listPoints do
502          output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
503       end
504       -- mark the adding point
505       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
506    elseif(step == "newT") then
507       polygon = buildCavity(badTriangles, triangulation)
508       polyNew = cleanPoly(polygon)
509       -- remove the bad triangles
510       for j=1,#badTriangles do
511          table.remove(triangulation,badTriangles[j]-(j-1))
512       end
513       -- draw the triangle of the triangulation
514       for i=1,#triangulation do
515          PointI = listPoints[triangulation[i][1]]
516          PointJ = listPoints[triangulation[i][2]]
517          PointK = listPoints[triangulation[i][3]]
518          output = output .. "\\draw[color ="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
519       end
520       -- fill and draw the cavity
521       path = ""
522       for i=1,#polyNew do
523          PointI = listPoints[polyNew[i]]
524          path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
525       end
526       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
527       -- draw the new triangles composed by the edges of the polygon and the added point
528       for i=1,#polygon do
529          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 ..");"
530          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][1]].x .. "," .. listPoints[polygon[i][1]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
531          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][2]].x .. "," .. listPoints[polygon[i][2]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
532       end
533       -- mark points
534       for i=1,#listPoints do
535          output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
536       end
537       -- mark the added point
538       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
539    end
540    return output
541 end
542
543 function TeXaddOnePointMPBW(listPoints,P,step,bbox)
544    output = "";
545    output = output .. "pair MeshPoints[];"
546    -- build the triangulation
547    triangulation = BowyerWatson(listPoints,bbox)
548    badTriangles = buildBadTriangles(P,triangulation)
549    for i=1,#listPoints do
550       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
551    end
552    if(step == "badT") then
553       -- draw all triangle
554       for i=1,#triangulation do
555          PointI = listPoints[triangulation[i][1]]
556          PointJ = listPoints[triangulation[i][2]]
557          PointK = listPoints[triangulation[i][3]]
558          if(triangulation[i].type == "bbox") then
559             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
560          else
561             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
562          end
563       end
564       -- draw and fill the bad triangle
565       for i=1,#badTriangles do
566          PointI = listPoints[triangulation[badTriangles[i]][1]]
567          PointJ = listPoints[triangulation[badTriangles[i]][2]]
568          PointK = listPoints[triangulation[badTriangles[i]][3]]
569          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
570          output = output .. "fill (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBack;"
571       end
572       -- draw the circoncircle
573       for i=1,#badTriangles do
574          PointI = listPoints[triangulation[badTriangles[i]][1]]
575          PointJ = listPoints[triangulation[badTriangles[i]][2]]
576          PointK = listPoints[triangulation[badTriangles[i]][3]]
577          center, radius = circoncircle(PointI, PointJ, PointK)
578          output = output .. "draw fullcircle scaled ("..radius .."*2u) shifted ("..center.x .. "*u," .. center.y .. "*u) dashed evenly withcolor \\luameshmpcolorCircle;"
579       end
580       -- mark the points
581       j=1
582       for i=1,#listPoints do
583          if(listPoints[i].type == "bbox") then
584             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
585             j=j+1
586          else
587             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
588          end
589       end
590       -- mark the point to add
591       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew;"
592    elseif(step == "cavity") then
593       polygon = buildCavity(badTriangles, triangulation)
594       polyNew = cleanPoly(polygon)
595       -- remove the bad triangles
596       for j=1,#badTriangles do
597          table.remove(triangulation,badTriangles[j]-(j-1))
598       end
599       -- draw the triangles
600       for i=1,#triangulation do
601          PointI = listPoints[triangulation[i][1]]
602          PointJ = listPoints[triangulation[i][2]]
603          PointK = listPoints[triangulation[i][3]]
604          if(triangulation[i].type == "bbox") then
605             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
606          else
607             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
608          end
609       end
610       -- fill and draw the cavity
611       path = ""
612       for i=1,#polyNew do
613          PointI = listPoints[polyNew[i]]
614          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
615       end
616       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
617       output = output .. "draw " .. path .. "cycle withcolor \\luameshmpcolorNew  withpen pencircle scaled 1pt;"
618       -- mark the points of the mesh
619       j=1
620       for i=1,#listPoints do
621          if(listPoints[i].type == "bbox") then
622             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
623             j=j+1
624          else
625             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
626          end
627       end
628       -- mark the adding point
629       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
630    elseif(step == "newT") then
631       polygon = buildCavity(badTriangles, triangulation)
632       polyNew = cleanPoly(polygon)
633       -- remove the bad triangles
634       for j=1,#badTriangles do
635          table.remove(triangulation,badTriangles[j]-(j-1))
636       end
637       -- draw the triangle of the triangulation
638       for i=1,#triangulation do
639          PointI = listPoints[triangulation[i][1]]
640          PointJ = listPoints[triangulation[i][2]]
641          PointK = listPoints[triangulation[i][3]]
642          if(triangulation[i].type == "bbox") then
643             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
644          else
645             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
646          end
647       end
648       -- fill  the cavity
649       path = ""
650       for i=1,#polyNew do
651          PointI = listPoints[polyNew[i]]
652          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
653       end
654       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
655       -- draw the new triangles composed by the edges of the polygon and the added point
656       for i=1,#polygon do
657          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;"
658          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;"
659          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;"
660       end
661       -- mark points
662       j=1
663       for i=1,#listPoints do
664          if(listPoints[i].type == "bbox") then
665             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
666             j=j+1
667          else
668             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
669          end
670       end
671       -- mark the added point
672       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
673    end
674    return output
675 end
676
677
678 -- build the list of points extern and stop at nbr
679 function buildListExt(chaine, stop)
680    listPoints = {}
681    io.input(chaine) -- open the file
682    text=io.read("*all")
683    lines=string.explode(text,"\n+") -- all the lines
684    for i=1,tonumber(stop) do
685       xy=string.explode(lines[i]," +")
686       table.insert(listPoints,{x=tonumber(xy[1]),y=tonumber(xy[2])})
687    end
688    xy=string.explode(lines[stop+1]," +")
689    point={x=tonumber(xy[1]),y=tonumber(xy[2])}
690    return point, listPoints
691 end
692
693
694 function TeXOnePointTikZ(chaine,point,step,color,colorBack,colorNew,colorCircle,scale)
695    output = TeXaddOnePointTikZ(chaine,point,step,color,colorBack,colorNew,colorCircle)
696    output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. output .. "\\end{tikzpicture}"
697    tex.sprint(output)
698 end
699
700 function TeXOnePointMPBW(chaine,point,step,scale,mode,bbox)
701    if(mode=="int") then
702       Sx,Sy=string.match(point,"%((.+),(.+)%)")
703       P = {x=Sx, y=Sy}
704       listPoints = buildList(chaine, mode)
705    else
706       -- point is a number
707       P, listPoints = buildListExt(chaine,tonumber(point))
708    end
709    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
710    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale..";".. output .. "endfig;\\end{mplibcode}"
711    tex.sprint(output)
712 end
713
714 function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,bbox)
715    if(mode=="int") then
716       Sx,Sy=string.match(point,"%((.+),(.+)%)")
717       P = {x=Sx, y=Sy}
718       listPoints = buildList(chaine, mode)
719    else
720       -- point is a number
721       P, listPoints = buildListExt(chaine,tonumber(point))
722    end
723    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
724    output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
725    tex.sprint(output)
726 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.