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