1 -- Bowyer and Watson algorithm
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)
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
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))
22 -- build the new triangles and add them to triangulation
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"})
27 table.insert(triangulation,{polygon[j][1],polygon[j][2],i,type="in"})
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)
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
51 for i=1,#listPoints do
52 if (listPoints[i].x < xmin) then
53 xmin = listPoints[i].x
55 if (listPoints[i].x > xmax) then
56 xmax = listPoints[i].x
58 if (listPoints[i].y < ymin) then
59 ymin = listPoints[i].y
61 if (listPoints[i].y > ymax) then
62 ymax = listPoints[i].y
65 eps = math.max(math.abs(xmax-xmin),math.abs(ymax-ymin))*0.15
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"})
78 function removeBoundingBox(triangulation,lgth)
79 -- build the four bounding box edge
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])
95 return newTriangulation
99 function buildBadTriangles(point, triangulation)
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)
114 -- construction of the cavity composed by the bad triangles around the point to add
115 function buildCavity(badTriangles, triangulation)
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]}
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])
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)
138 function edgeInTriangle(e,t)
153 function pointInTriangle(e,t)
165 local out = {x = B.x - A.x, y = B.y - A.y}
169 function VectorNorm(NP)
170 return math.sqrt(NP.x*NP.x +NP.y*NP.y)
174 function circoncircle(M, N, P)
175 -- Compute center and radius of the circoncircle of the triangle M N P
177 -- return : (center [Point],radius [float])
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|
186 d = (m + n + p) * (-m + n + p) * (m - n + p) * (m + n - p)
188 rad = m * n * p / math.sqrt(d)
192 d = -2 * (M.x * NP.y + N.x * PM.y + P.x * MN.y)
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
207 return Out, rad -- (center [Point], R [float])
210 -- compute the list of the circumcircle of a triangulation
211 function listCircumCenter(listPoints,triangulation)
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})
223 -- find the three neighbour triangles of T
224 function findNeighbour(T,i,triangulation)
226 -- i : index of T in triangualation
230 -- define the three edge
234 for j=1,#triangulation do
236 if(edgeInTriangle(e1,triangulation[j])) then
239 if(edgeInTriangle(e2,triangulation[j])) then
242 if(edgeInTriangle(e3,triangulation[j])) then
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
259 -- test if the edge belongs to the list
260 function edgeInList(e,listE)
263 if(equalEdge(e,listE[i])) then
270 -- build the edges of the Voronoi diagram with a given triangulation
271 function buildVoronoi(listPoints, triangulation)
272 listCircumCircle = listCircumCenter(listPoints, triangulation)
274 for i=1,#listCircumCircle do
275 listN = findNeighbour(triangulation[i],i,triangulation)
278 if( not edgeInList(edge, listVoronoi)) then
279 table.insert(listVoronoi, edge)
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
292 if mode == "int" then
293 local points = string.explode(chaine, ";")
296 Sx,Sy=string.match(points[i],"%((.+),(.+)%)")
297 listPoints[i]={x=tonumber(Sx),y=tonumber(Sy)}
299 elseif mode == "ext" then
300 io.input(chaine) -- open the file
302 lines=string.explode(text,"\n+") -- all the lines
305 xy=string.explode(lines[i]," +")
306 listPoints[i]={x=tonumber(xy[1]),y=tonumber(xy[2])}
309 print("Non existing mode")
316 function rectangleList(a,b,nbrA,nbrB)
323 listPoints[k] = {x = (i-1)*stepA, y=(j-1)*stepB}
331 -- trace Voronoi with MP
332 function traceVoronoiMP(listPoints, triangulation,listVoronoi, points, tri)
333 listCircumC = listCircumCenter(listPoints,triangulation)
335 output = output .. " pair MeshPoints[];"
336 for i=1,#listPoints do
337 output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
339 output = output .. " pair CircumCenters[];"
340 for i=1,#listCircumC do
341 output = output .. "CircumCenters[".. i .. "] = (" .. listCircumC[i].x .. "," .. listCircumC[i].y .. ")*u;"
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;"
351 output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
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;"
360 if(points=="points") then
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 ;"
367 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
370 for i=1,#listCircumC do
371 output = output .. "dotlabel.llft (btex $\\CircumPoint_{" .. i .. "}$ etex, (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ")*u ) withcolor \\luameshmpcolorVoronoi ;"
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;"
380 output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u withcolor \\luameshmpcolor withpen pencircle scaled 3;"
383 for i=1,#listCircumC do
384 output = output .. "drawdot (" .. listCircumC[i].x ..",".. listCircumC[i].y .. ")*u withcolor \\luameshmpcolorVoronoi withpen pencircle scaled 3;"
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}"
405 -- trace a triangulation with TikZ
406 function traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
408 for i=1,#listPoints do
409 output = output .. "\\coordinate (MeshPoints".. i .. ") at (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
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;"
418 output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
421 if(points=="points") then
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 .. "}$};"
428 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
436 -- trace a triangulation with MP
437 function traceMeshMP(listPoints, triangulation,points)
439 output = output .. " pair MeshPoints[];"
440 for i=1,#listPoints do
441 output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
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;"
451 output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
454 if(points=="points") then
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 ;"
461 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
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}"
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}"
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}"
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}"
506 -- print points of the mesh
507 function tracePointsMP(listPoints,points)
509 output = output .. " pair MeshPoints[];"
510 for i=1,#listPoints do
511 output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ")*u;"
513 if(points=="points") then
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 ;"
520 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
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;"
528 output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u withcolor \\luameshmpcolor withpen pencircle scaled 3;"
535 -- print points of the mesh
536 function tracePointsTikZ(listPoints,points,color,colorBbox)
538 for i=1,#listPoints do
539 output = output .. "\\coordinate (MeshPoints".. i .. ") at (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
541 if(points=="points") then
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 .. "}$};"
548 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
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$} ;"
556 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
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)
569 output = tracePointsMP(listPoints,points)
570 output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
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)
581 output = tracePointsMP(listPoints,points)
582 output = "\\leavevmode\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
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)
592 output = tracePointsTikZ(listPoints,points,color,colorBbox)
593 output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
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)
604 output = tracePointsTikZ(listPoints,points,color,colorBbox)
605 output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
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")
617 local function shallowCopy(original)
619 for key, value in pairs(original) do
625 -- function give a real polygon without repeting points
626 function cleanPoly(polygon)
628 polyCopy = shallowCopy(polygon)
631 table.insert(polyNew, e1)
632 table.insert(polyNew, e2)
633 table.remove(polyCopy,1)
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]
643 table.insert(polyNew, polyCopy[i][1])
645 table.remove(polyCopy,i)
647 elseif(not bool2) then
648 table.insert(polyNew, polyCopy[i][2])
650 table.remove(polyCopy,i)
661 function TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack, colorNew, colorCircle,colorBbox)
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 .. ");"
669 if(step == "badT") then
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;"
678 output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
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;"
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 ..");"
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 .. "}$};"
703 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
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))
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;"
723 output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
726 -- fill and draw the cavity
729 PointI = listPoints[polyNew[i]]
730 path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
732 output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
733 -- mark the points of the mesh
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 .. "}$};"
740 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
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))
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;"
760 output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
763 -- fill and draw the cavity
766 PointI = listPoints[polyNew[i]]
767 path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
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
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 ..");"
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 .. "}$};"
783 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
786 -- mark the added point
787 output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
792 function TeXaddOnePointMPBW(listPoints,P,step,bbox)
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;"
801 if(step == "badT") then
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;"
810 output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
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;"
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;"
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 ;"
836 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
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))
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;"
856 output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
859 -- fill and draw the cavity
862 PointI = listPoints[polyNew[i]]
863 path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
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
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 ;"
874 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
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))
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;"
894 output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
900 PointI = listPoints[polyNew[i]]
901 path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
903 output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
904 -- draw the new triangles composed by the edges of the polygon and the added point
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;"
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 ;"
917 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
920 -- mark the added point
921 output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
927 -- build the list of points extern and stop at nbr
928 function buildListExt(chaine, stop)
930 io.input(chaine) -- open the file
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])})
937 xy=string.explode(lines[stop+1]," +")
938 point={x=tonumber(xy[1]),y=tonumber(xy[2])}
939 return point, listPoints
943 function TeXOnePointTikZBW(chaine,point,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
945 Sx,Sy=string.match(point,"%((.+),(.+)%)")
947 listPoints = buildList(chaine, mode)
950 P, listPoints = buildListExt(chaine,tonumber(point))
952 output = TeXaddOnePointTikZ(listPoints,P,step,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
953 output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. output .. "\\end{tikzpicture}"
957 function TeXOnePointTikZBWinc(chaine,point,beginning, ending,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
959 Sx,Sy=string.match(point,"%((.+),(.+)%)")
961 listPoints = buildList(chaine, mode)
964 P, listPoints = buildListExt(chaine,tonumber(point))
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}"
971 function TeXOnePointMPBW(chaine,point,step,scale,mode,bbox)
973 Sx,Sy=string.match(point,"%((.+),(.+)%)")
975 listPoints = buildList(chaine, mode)
978 P, listPoints = buildListExt(chaine,tonumber(point))
980 output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
981 output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale..";".. output .. "endfig;\\end{mplibcode}"
985 function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,bbox)
987 Sx,Sy=string.match(point,"%((.+),(.+)%)")
989 listPoints = buildList(chaine, mode)
992 P, listPoints = buildListExt(chaine,tonumber(point))
994 output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
995 output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"