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])
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
217 if mode == "int" then
218 local points = string.explode(chaine, ";")
221 Sx,Sy=string.match(points[i],"%((.+),(.+)%)")
222 listPoints[i]={x=tonumber(Sx),y=tonumber(Sy)}
224 elseif mode == "ext" then
225 io.input(chaine) -- open the file
227 lines=string.explode(text,"\n+") -- all the lines
230 xy=string.explode(lines[i]," +")
231 listPoints[i]={x=tonumber(xy[1]),y=tonumber(xy[2])}
234 print("Non existing mode")
241 function rectangleList(a,b,nbrA,nbrB)
248 listPoints[k] = {x = (i-1)*stepA, y=(j-1)*stepB}
255 -- trace a triangulation with TikZ
256 function traceMeshTikZ(listPoints, triangulation,points,color,colorBbox)
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;"
265 output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
268 if(points=="points") then
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 .. "}$};"
275 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
283 -- trace a triangulation with MP
284 function traceMeshMP(listPoints, triangulation,points)
286 output = output .. " pair MeshPoints[];"
287 for i=1,#listPoints do
288 output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
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;"
298 output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
301 if(points=="points") then
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 ;"
308 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
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}"
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}"
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}"
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}"
353 -- print points of the mesh
354 function tracePointsMP(listPoints,points)
356 output = output .. " pair MeshPoints[];"
357 for i=1,#listPoints do
358 output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");"
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 ;"
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;"
375 output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u withcolor \\luameshmpcolor withpen pencircle scaled 3;"
382 -- print points of the mesh
383 function tracePointsTikZ(listPoints,points,color,colorBbox)
385 if(points=="points") then
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 .. "}$};"
392 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
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$} ;"
400 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} ;"
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)
413 output = tracePointsMP(listPoints,points)
414 output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}"
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)
425 output = tracePointsMP(listPoints,points)
426 output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"
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)
436 output = tracePointsTikZ(listPoints,points,color,colorBbox)
437 output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" .. output .."\\end{tikzpicture}"
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)
448 output = tracePointsTikZ(listPoints,points,color,colorBbox)
449 output = "\\noindent\\begin{tikzpicture}[x=" .. scale .. ",y=" .. scale .."]" ..beginning.. output..ending .."\\end{tikzpicture}"
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")
461 local function shallowCopy(original)
463 for key, value in pairs(original) do
469 -- function give a real polygon without repeting points
470 function cleanPoly(polygon)
472 polyCopy = shallowCopy(polygon)
475 table.insert(polyNew, e1)
476 table.insert(polyNew, e2)
477 table.remove(polyCopy,1)
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]
487 table.insert(polyNew, polyCopy[i][1])
489 table.remove(polyCopy,i)
491 elseif(not bool2) then
492 table.insert(polyNew, polyCopy[i][2])
494 table.remove(polyCopy,i)
505 function TeXaddOnePointTikZ(chaine,point,step,color,colorBack, colorNew, colorCircle,colorBbox)
507 -- build the triangulation
508 triangulation = BowyerWatson(listPoints,"none")
509 badTriangles = buildBadTriangles(P,triangulation)
510 if(step == "badT") then
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;"
519 output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
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;"
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 ..");"
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 .. "}$};"
544 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
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))
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;"
564 output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
567 -- fill and draw the cavity
570 PointI = listPoints[polyNew[i]]
571 path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
573 output = output .. "\\draw[color="..colorNew..",fill ="..colorBack..", thick] " .. path .. "cycle;"
574 -- mark the points of the mesh
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 .. "}$};"
581 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
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))
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;"
601 output = output .. "\\draw[color="..color.."] (".. PointI.x ..",".. PointI.y ..")--("..PointJ.x..",".. PointJ.y ..")--("..PointK.x..",".. PointK.y ..")--cycle;"
604 -- fill and draw the cavity
607 PointI = listPoints[polyNew[i]]
608 path = path .. "(".. PointI.x ..",".. PointI.y ..")--"
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
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 ..");"
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 .. "}$};"
624 output = output .. "\\draw[color="..color.."] (" .. listPoints[i].x ..",".. listPoints[i].y .. ") node {$\\bullet$} node[anchor=north east] {$\\MeshPoint_{" .. i .. "}$};"
627 -- mark the added point
628 output = output .. "\\draw[color="..colorNew.."] (" .. P.x ..",".. P.y .. ") node {$\\bullet$} node[anchor=north east] {$\\NewPoint$};"
633 function TeXaddOnePointMPBW(listPoints,P,step,bbox)
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 .. ");"
642 if(step == "badT") then
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;"
651 output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
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;"
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;"
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 ;"
677 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
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))
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;"
697 output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
700 -- fill and draw the cavity
703 PointI = listPoints[polyNew[i]]
704 path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
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
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 ;"
715 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
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))
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;"
735 output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;"
741 PointI = listPoints[polyNew[i]]
742 path = path .. "(".. PointI.x ..",".. PointI.y ..")*u--"
744 output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;"
745 -- draw the new triangles composed by the edges of the polygon and the added point
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;"
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 ;"
758 output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;"
761 -- mark the added point
762 output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;"
768 -- build the list of points extern and stop at nbr
769 function buildListExt(chaine, stop)
771 io.input(chaine) -- open the file
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])})
778 xy=string.explode(lines[stop+1]," +")
779 point={x=tonumber(xy[1]),y=tonumber(xy[2])}
780 return point, listPoints
784 function TeXOnePointTikZBW(chaine,point,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
786 Sx,Sy=string.match(point,"%((.+),(.+)%)")
788 listPoints = buildList(chaine, mode)
791 P, listPoints = buildListExt(chaine,tonumber(point))
793 output = TeXaddOnePointTikZ(chaine,point,step,color,colorBack,colorNew,colorCircle,colorBbox)
794 output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. output .. "\\end{tikzpicture}"
798 function TeXOnePointTikZBWinc(chaine,point,beginning, ending,step,scale,mode,bbox,color,colorBack,colorNew,colorCircle,colorBbox)
800 Sx,Sy=string.match(point,"%((.+),(.+)%)")
802 listPoints = buildList(chaine, mode)
805 P, listPoints = buildListExt(chaine,tonumber(point))
807 output = TeXaddOnePointTikZ(chaine,point,step,color,colorBack,colorNew,colorCircle,colorBbox)
808 output = "\\noindent\\begin{tikzpicture}[x="..scale..",y="..scale.."]".. beginning..output ..ending.. "\\end{tikzpicture}"
812 function TeXOnePointMPBW(chaine,point,step,scale,mode,bbox)
814 Sx,Sy=string.match(point,"%((.+),(.+)%)")
816 listPoints = buildList(chaine, mode)
819 P, listPoints = buildListExt(chaine,tonumber(point))
821 output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
822 output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale..";".. output .. "endfig;\\end{mplibcode}"
826 function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,bbox)
828 Sx,Sy=string.match(point,"%((.+),(.+)%)")
830 listPoints = buildList(chaine, mode)
833 P, listPoints = buildListExt(chaine,tonumber(point))
835 output = TeXaddOnePointMPBW(listPoints,P,step,bbox)
836 output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}"