Doc: documentation de la partie inc pour MetaPost
[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,colorBbox)
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       if(triangulation[i].type == "bbox") then
263          output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
264       else
265          output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
266       end
267    end
268    if(points=="points") then
269       j=1
270       for i=1,#listPoints do
271          if(listPoints[i].type == "bbox") then
272             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
273             j=j+1
274          else
275             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
276          end
277       end
278    end
279    return output
280 end
281
282
283 -- trace a triangulation with MP
284 function traceMeshMP(listPoints, triangulation,points)
285    output = "";
286    output = output .. " pair MeshPoints[];"
287    for i=1,#listPoints do
288       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
289    end
290
291    for i=1,#triangulation do
292       PointI = listPoints[triangulation[i][1]]
293       PointJ = listPoints[triangulation[i][2]]
294       PointK = listPoints[triangulation[i][3]]
295       if(triangulation[i].type == "bbox") then
296          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
297       else
298          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
299       end
300    end
301    if(points=="points") then
302       j=1
303       for i=1,#listPoints do
304          if(listPoints[i].type == "bbox") then
305             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
306             j=j+1
307          else
308             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
309          end
310       end
311    end
312    return output
313 end
314
315
316 -- buildMesh with MP
317 function buildMeshMPBW(chaine,mode,points,bbox,scale)
318    listPoints = buildList(chaine, mode)
319    triangulation = BowyerWatson(listPoints,bbox)
320    output = traceMeshMP(listPoints, triangulation,points)
321    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
322    tex.sprint(output)
323 end
324
325 -- buildMesh with MP include code
326 function buildMeshMPBWinc(chaine,beginning, ending,mode,points,bbox,scale)
327    listPoints = buildList(chaine, mode)
328    triangulation = BowyerWatson(listPoints,bbox)
329    output = traceMeshMP(listPoints, triangulation,points)
330    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
331    tex.sprint(output)
332 end
333
334 -- buildMesh with TikZ
335 function buildMeshTikZBW(chaine,mode,points,bbox,scale,color,colorBbox)
336    listPoints = buildList(chaine, mode)
337    triangulation = BowyerWatson(listPoints,bbox)
338    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
339    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
340    tex.sprint(output)
341 end
342
343 -- buildMesh with TikZ
344 function buildMeshTikZBWinc(chaine,beginning, ending,mode,points,bbox,scale,color,colorBbox)
345    listPoints = buildList(chaine, mode)
346    triangulation = BowyerWatson(listPoints,bbox)
347    output = traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
348    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
349    tex.sprint(output)
350 end
351
352
353 -- print points of the mesh
354 function tracePointsMP(listPoints,points)
355    output = "";
356    output = output .. " pair MeshPoints[];"
357    for i=1,#listPoints do
358       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
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    else
371       for i=1,#listPoints do
372          if(listPoints[i].type == "bbox") then
373             output = output .. "drawdot  (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolorBbox withpen pencircle scaled 3;"
374          else
375             output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u  withcolor \\luameshmpcolor withpen pencircle scaled 3;"
376          end
377       end
378    end
379    return output
380 end
381
382 -- print points of the mesh
383 function tracePointsTikZ(listPoints,points,color,colorBbox)
384    output = "";
385    if(points=="points") then
386       j=1
387       for i=1,#listPoints do
388          if(listPoints[i].type == "bbox") then
389             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
390             j = j+1
391          else
392             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
393          end
394       end
395    else
396       for i=1,#listPoints do
397          if(listPoints[i].type == "bbox") then
398             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
399          else
400             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
401          end
402       end
403    end
404    return output
405 end
406
407 -- print points to mesh
408 function printPointsMP(chaine,mode,points,bbox,scale)
409    listPoints = buildList(chaine, mode)
410    if(bbox == "bbox" ) then
411       listPoints = buildBoundingBox(listPoints)
412    end
413    output = tracePointsMP(listPoints,points)
414    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
415    tex.sprint(output)
416 end
417
418
419 -- print points to mesh
420 function printPointsMPinc(chaine,beginning, ending, mode,points,bbox,scale)
421    listPoints = buildList(chaine, mode)
422    if(bbox == "bbox" ) then
423       listPoints = buildBoundingBox(listPoints)
424    end
425    output = tracePointsMP(listPoints,points)
426    output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
427    tex.sprint(output)
428 end
429
430 -- print points to mesh
431 function printPointsTikZ(chaine,mode,points,bbox,scale,color,colorBbox)
432    listPoints = buildList(chaine, mode)
433    if(bbox == "bbox" ) then
434       listPoints = buildBoundingBox(listPoints)
435    end
436    output = tracePointsTikZ(listPoints,points,color,colorBbox)
437    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
438    tex.sprint(output)
439 end
440
441
442 -- print points to mesh
443 function printPointsTikZinc(chaine,beginning, ending, mode,points,bbox,scale,color,colorBbox)
444    listPoints = buildList(chaine, mode)
445    if(bbox == "bbox" ) then
446       listPoints = buildBoundingBox(listPoints)
447    end
448    output = tracePointsTikZ(listPoints,points,color,colorBbox)
449    output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
450    tex.sprint(output)
451 end
452
453
454 -- buildMesh
455 function buildRect(largeur,a,b,nbrA, nbrB)
456    listPoints = rectangleList(a,b,nbrA,nbrB)
457    triangulation = BowyerWatson(listPoints,"none")
458    traceTikZ(listPoints, triangulation,largeur,"none")
459 end
460
461 local function shallowCopy(original)
462    local copy = {}
463    for key, value in pairs(original) do
464       copy[key] = value
465    end
466    return copy
467 end
468
469 -- function give a real polygon without repeting points
470 function cleanPoly(polygon)
471    polyNew = {}
472    polyCopy = shallowCopy(polygon)
473    e1 = polyCopy[1][1]
474    e2 = polyCopy[1][2]
475    table.insert(polyNew, e1)
476    table.insert(polyNew, e2)
477    table.remove(polyCopy,1)
478    j = 2
479    while #polyCopy>1 do
480       i=1
481       find = false
482       while (i<=#polyCopy and find==false) do
483          bool1 = (polyCopy[i][1] == polyNew[j])
484          bool2 = (polyCopy[i][2] == polyNew[j])
485          if(bool1 or bool2) then -- the edge has a common point with polyNew[j]
486             if(not bool1) then
487                table.insert(polyNew, polyCopy[i][1])
488                find = true
489                table.remove(polyCopy,i)
490                j = j+1
491             elseif(not bool2) then
492                table.insert(polyNew, polyCopy[i][2])
493                find = true
494                table.remove(polyCopy,i)
495                j = j+1
496             end
497          end
498          i=i+1
499       end
500    end
501    return polyNew
502 end
503
504 --
505 function TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack, colorNew, colorCircle,colorBbox)
506    output = ""
507    -- build the triangulation
508    triangulation = BowyerWatson(listPoints,bbox)
509    badTriangles = buildBadTriangles(P,triangulation)
510    if(step == "badT") then
511       -- draw all triangle
512       for i=1,#triangulation do
513          PointI = listPoints[triangulation[i][1]]
514          PointJ = listPoints[triangulation[i][2]]
515          PointK = listPoints[triangulation[i][3]]
516          if(triangulation[i].type == "bbox") then
517             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
518          else
519             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
520          end
521       end
522       -- draw and fill the bad triangle
523       for i=1,#badTriangles do
524          PointI = listPoints[triangulation[badTriangles[i]][1]]
525          PointJ = listPoints[triangulation[badTriangles[i]][2]]
526          PointK = listPoints[triangulation[badTriangles[i]][3]]
527          output = output .. "\\draw[fill="..colorBack.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
528       end
529       -- draw the circoncircle
530       for i=1,#badTriangles do
531          PointI = listPoints[triangulation[badTriangles[i]][1]]
532          PointJ = listPoints[triangulation[badTriangles[i]][2]]
533          PointK = listPoints[triangulation[badTriangles[i]][3]]
534          center, radius = circoncircle(PointI, PointJ, PointK)
535          output = output .. "\\draw[dashed, color="..colorCircle.."] ("..center.x .. "," .. center.y .. ") circle ("..radius ..");"
536       end
537       -- mark the points
538       j=1
539       for i=1,#listPoints do
540          if(listPoints[i].type == "bbox") then
541             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
542             j = j+1
543          else
544             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
545          end
546       end
547       -- mark the point to add
548       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
549    elseif(step == "cavity") then
550       polygon = buildCavity(badTriangles, triangulation)
551       polyNew = cleanPoly(polygon)
552       -- remove the bad triangles
553       for j=1,#badTriangles do
554          table.remove(triangulation,badTriangles[j]-(j-1))
555       end
556       -- draw the triangles
557       for i=1,#triangulation do
558          PointI = listPoints[triangulation[i][1]]
559          PointJ = listPoints[triangulation[i][2]]
560          PointK = listPoints[triangulation[i][3]]
561          if(triangulation[i].type == "bbox") then
562             output = output .. "\\draw[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
563          else
564             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
565          end
566       end
567       -- fill and draw the cavity
568       path = ""
569       for i=1,#polyNew do
570          PointI = listPoints[polyNew[i]]
571          path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
572       end
573       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
574       -- mark the points of the mesh
575       j=1
576       for i=1,#listPoints do
577          if(listPoints[i].type == "bbox") then
578             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
579             j=j+1
580          else
581             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
582          end
583       end
584       -- mark the adding point
585       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
586    elseif(step == "newT") 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 triangle of the triangulation
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[color="..colorBbox.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
600          else
601             output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
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 ..")--"
609       end
610       output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
611       -- draw the new triangles composed by the edges of the polygon and the added point
612       for i=1,#polygon do
613          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 ..");"
614          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][1]].x .. "," .. listPoints[polygon[i][1]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
615          output = output .. "\\draw[color="..colorNew..", thick]".."(".. listPoints[polygon[i][2]].x .. "," .. listPoints[polygon[i][2]].y .. ") -- (" .. P.x .. "," .. P.y ..");"
616       end
617       -- mark points
618       j=1
619       for i=1,#listPoints do
620          if(listPoints[i].type == "bbox") then
621             output = output .. "\\draw[color="..colorBbox.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint^*_{" .. j .. "}$};"
622             j=j+1
623          else
624             output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
625          end
626       end
627       -- mark the added point
628       output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
629    end
630    return output
631 end
632
633 function TeXaddOnePointMPBW(listPoints,P,step,bbox)
634    output = "";
635    output = output .. "pair MeshPoints[];"
636    -- build the triangulation
637    triangulation = BowyerWatson(listPoints,bbox)
638    badTriangles = buildBadTriangles(P,triangulation)
639    for i=1,#listPoints do
640       output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
641    end
642    if(step == "badT") then
643       -- draw all triangle
644       for i=1,#triangulation do
645          PointI = listPoints[triangulation[i][1]]
646          PointJ = listPoints[triangulation[i][2]]
647          PointK = listPoints[triangulation[i][3]]
648          if(triangulation[i].type == "bbox") then
649             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
650          else
651             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
652          end
653       end
654       -- draw and fill the bad triangle
655       for i=1,#badTriangles do
656          PointI = listPoints[triangulation[badTriangles[i]][1]]
657          PointJ = listPoints[triangulation[badTriangles[i]][2]]
658          PointK = listPoints[triangulation[badTriangles[i]][3]]
659          output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
660          output = output .. "fill (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBack;"
661       end
662       -- draw the circoncircle
663       for i=1,#badTriangles do
664          PointI = listPoints[triangulation[badTriangles[i]][1]]
665          PointJ = listPoints[triangulation[badTriangles[i]][2]]
666          PointK = listPoints[triangulation[badTriangles[i]][3]]
667          center, radius = circoncircle(PointI, PointJ, PointK)
668          output = output .. "draw fullcircle scaled ("..radius .."*2u) shifted ("..center.x .. "*u," .. center.y .. "*u) dashed evenly withcolor \\luameshmpcolorCircle;"
669       end
670       -- mark the points
671       j=1
672       for i=1,#listPoints do
673          if(listPoints[i].type == "bbox") then
674             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
675             j=j+1
676          else
677             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
678          end
679       end
680       -- mark the point to add
681       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew;"
682    elseif(step == "cavity") then
683       polygon = buildCavity(badTriangles, triangulation)
684       polyNew = cleanPoly(polygon)
685       -- remove the bad triangles
686       for j=1,#badTriangles do
687          table.remove(triangulation,badTriangles[j]-(j-1))
688       end
689       -- draw the triangles
690       for i=1,#triangulation do
691          PointI = listPoints[triangulation[i][1]]
692          PointJ = listPoints[triangulation[i][2]]
693          PointK = listPoints[triangulation[i][3]]
694          if(triangulation[i].type == "bbox") then
695             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
696          else
697             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
698          end
699       end
700       -- fill and draw the cavity
701       path = ""
702       for i=1,#polyNew do
703          PointI = listPoints[polyNew[i]]
704          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
705       end
706       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
707       output = output .. "draw " .. path .. "cycle withcolor \\luameshmpcolorNew  withpen pencircle scaled 1pt;"
708       -- mark the points of the mesh
709       j=1
710       for i=1,#listPoints do
711          if(listPoints[i].type == "bbox") then
712             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
713             j=j+1
714          else
715             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
716          end
717       end
718       -- mark the adding point
719       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
720    elseif(step == "newT") then
721       polygon = buildCavity(badTriangles, triangulation)
722       polyNew = cleanPoly(polygon)
723       -- remove the bad triangles
724       for j=1,#badTriangles do
725          table.remove(triangulation,badTriangles[j]-(j-1))
726       end
727       -- draw the triangle of the triangulation
728       for i=1,#triangulation do
729          PointI = listPoints[triangulation[i][1]]
730          PointJ = listPoints[triangulation[i][2]]
731          PointK = listPoints[triangulation[i][3]]
732          if(triangulation[i].type == "bbox") then
733             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;"
734          else
735             output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
736          end
737       end
738       -- fill  the cavity
739       path = ""
740       for i=1,#polyNew do
741          PointI = listPoints[polyNew[i]]
742          path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
743       end
744       output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
745       -- draw the new triangles composed by the edges of the polygon and the added point
746       for i=1,#polygon do
747          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;"
748          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;"
749          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;"
750       end
751       -- mark points
752       j=1
753       for i=1,#listPoints do
754          if(listPoints[i].type == "bbox") then
755             output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;"
756             j=j+1
757          else
758             output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
759          end
760       end
761       -- mark the added point
762       output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
763    end
764    return output
765 end
766
767
768 -- build the list of points extern and stop at nbr
769 function buildListExt(chaine, stop)
770    listPoints = {}
771    io.input(chaine) -- open the file
772    text=io.read("*all")
773    lines=string.explode(text,"\n+") -- all the lines
774    for i=1,tonumber(stop) do
775       xy=string.explode(lines[i]," +")
776       table.insert(listPoints,{x=tonumber(xy[1]),y=tonumber(xy[2])})
777    end
778    xy=string.explode(lines[stop+1]," +")
779    point={x=tonumber(xy[1]),y=tonumber(xy[2])}
780    return point, listPoints
781 end
782
783
784 function TeXOnePointTikZBW(chaine,point,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
785    if(mode=="int") then
786       Sx,Sy=string.match(point,"%((.+),(.+)%)")
787       P = {x=Sx, y=Sy}
788       listPoints = buildList(chaine, mode)
789    else
790       -- point is a number
791       P, listPoints = buildListExt(chaine,tonumber(point))
792    end
793    output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
794    output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. output .. "\\end{tikzpicture}"
795    tex.sprint(output)
796 end
797
798 function TeXOnePointTikZBWinc(chaine,point,beginning, ending,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
799    if(mode=="int") then
800       Sx,Sy=string.match(point,"%((.+),(.+)%)")
801       P = {x=Sx, y=Sy}
802       listPoints = buildList(chaine, mode)
803    else
804       -- point is a number
805       P, listPoints = buildListExt(chaine,tonumber(point))
806    end
807    output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
808    output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. beginning..output ..ending.. "\\end{tikzpicture}"
809    tex.sprint(output)
810 end
811
812 function TeXOnePointMPBW(chaine,point,step,scale,mode,bbox)
813    if(mode=="int") then
814       Sx,Sy=string.match(point,"%((.+),(.+)%)")
815       P = {x=Sx, y=Sy}
816       listPoints = buildList(chaine, mode)
817    else
818       -- point is a number
819       P, listPoints = buildListExt(chaine,tonumber(point))
820    end
821    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
822    output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale..";".. output .. "endfig;\\end{mplibcode}"
823    tex.sprint(output)
824 end
825
826 function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,bbox)
827    if(mode=="int") then
828       Sx,Sy=string.match(point,"%((.+),(.+)%)")
829       P = {x=Sx, y=Sy}
830       listPoints = buildList(chaine, mode)
831    else
832       -- point is a number
833       P, listPoints = buildListExt(chaine,tonumber(point))
834    end
835    output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
836    output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
837    tex.sprint(output)
838 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.