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