X-Git-Url: https://melusine.eu.org/syracuse/G/git/?a=blobdiff_plain;f=luamesh.lua;h=ad6ec6fcc11d96029fcfb132de9d4ee9cb98ac35;hb=091530a7b134b2955272ad2ab217cf195233dc39;hp=28e61db1a1e0ac6f465b50baecec4d28f3eeb107;hpb=f5b04999414b0f514d2fea252417c05ac2efeeac;p=delaunay.git diff --git a/luamesh.lua b/luamesh.lua index 28e61db..ad6ec6f 100644 --- a/luamesh.lua +++ b/luamesh.lua @@ -6,9 +6,9 @@ function BowyerWatson (listPoints,bbox) -- add four points to listPoints to have a bounding box listPoints = buildBoundingBox(listPoints) -- the first triangle - triangulation[1] = {lgth+1, lgth+2, lgth+3} + triangulation[1] = {lgth+1, lgth+2, lgth+3,type="bbox"} -- the second triangle - triangulation[2] = {lgth+1, lgth+3, lgth+4} + triangulation[2] = {lgth+1, lgth+3, lgth+4,type="bbox"} -- add points one by one for i=1,lgth do -- find the triangles which the circumcircle contained the point to add @@ -21,7 +21,11 @@ function BowyerWatson (listPoints,bbox) end -- build the new triangles and add them to triangulation for j=1,#polygon do - table.insert(triangulation,{polygon[j][1],polygon[j][2],i}) + if((polygon[j][1]>lgth) or (polygon[j][2]>lgth) or (i>lgth)) then + table.insert(triangulation,{polygon[j][1],polygon[j][2],i,type="bbox"}) + else + table.insert(triangulation,{polygon[j][1],polygon[j][2],i,type="in"}) + end end end -- end adding points of the listPoints -- remove bounding box @@ -64,10 +68,10 @@ function buildBoundingBox(listPoints) ymin = ymin - eps ymax = ymax + eps -- add points of the bounding box in last positions - table.insert(listPoints,{x=xmin,y=ymin}) - table.insert(listPoints,{x=xmin,y=ymax}) - table.insert(listPoints,{x=xmax,y=ymax}) - table.insert(listPoints,{x=xmax,y=ymin}) + table.insert(listPoints,{x=xmin,y=ymin,type="bbox"}) + table.insert(listPoints,{x=xmin,y=ymax,type="bbox"}) + table.insert(listPoints,{x=xmax,y=ymax,type="bbox"}) + table.insert(listPoints,{x=xmax,y=ymin,type="bbox"}) return listPoints end @@ -267,7 +271,7 @@ end -- trace a triangulation with MP -function traceMeshMP(listPoints, triangulation,points,color) +function traceMeshMP(listPoints, triangulation,points) output = ""; output = output .. " pair MeshPoints[];" for i=1,#listPoints do @@ -278,11 +282,51 @@ function traceMeshMP(listPoints, triangulation,points,color) PointI = listPoints[triangulation[i][1]] PointJ = listPoints[triangulation[i][2]] PointK = listPoints[triangulation[i][3]] - output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\mpcolor{"..color.. "};" + if(triangulation[i].type == "bbox") then + output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;" + else + output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;" + end end if(points=="points") then + j=1 for i=1,#listPoints do - output = output .. "dotlabel.llft( btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u) withcolor \\mpcolor{"..color.. "};" + if(listPoints[i].type == "bbox") then + output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{"..j.."}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;" + j=j+1 + else + output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;" + end + end + end + return output +end + + +-- print points of the mesh +function tracePointsMP(listPoints,points) + output = ""; + output = output .. " pair MeshPoints[];" + for i=1,#listPoints do + output = output .. "MeshPoints[".. i .. "] = (" .. listPoints[i].x .. "," .. listPoints[i].y .. ");" + end + if(points=="points") then + j=1 + for i=1,#listPoints do + if(listPoints[i].type == "bbox") then + output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;" + j=j+1 + else + output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;" + end + end + else + for i=1,#listPoints do + if(listPoints[i].type == "bbox") then + output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;" + else + output = output .. "drawdot (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;" + end end end return output @@ -303,15 +347,44 @@ end -- buildMesh with MP -function buildMeshMPBW(chaine,mode,points,bbox,full,scale,color) +function buildMeshMPBW(chaine,mode,points,bbox,scale) listPoints = buildList(chaine, mode) triangulation = BowyerWatson(listPoints,bbox) - output = traceMeshMP(listPoints, triangulation,points,color) - if(full=="full") then - output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}" - else - output = "u:="..scale.. ";".. output + output = traceMeshMP(listPoints, triangulation,points) + output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}" + tex.sprint(output) +end + +-- print points to mesh +function printPointsMP(chaine,mode,points,bbox,scale) + listPoints = buildList(chaine, mode) + if(bbox == "bbox" ) then + listPoints = buildBoundingBox(listPoints) + end + output = tracePointsMP(listPoints,points) + output = "\\leavevmode\\begin{mplibcode}beginfig(0);u:="..scale.. ";" .. output .."endfig;\\end{mplibcode}" + tex.sprint(output) +end + + +-- print points to mesh +function printPointsMPinc(chaine,beginning, ending, mode,points,bbox,scale) + listPoints = buildList(chaine, mode) + if(bbox == "bbox" ) then + listPoints = buildBoundingBox(listPoints) end + output = tracePointsMP(listPoints,points) + output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}" + tex.sprint(output) +end + + +-- buildMesh with MP include code +function buildMeshMPBWinc(chaine,beginning, ending,mode,points,bbox,scale) + listPoints = buildList(chaine, mode) + triangulation = BowyerWatson(listPoints,bbox) + output = traceMeshMP(listPoints, triangulation,points) + output = "\\begin{mplibcode}u:="..scale..";"..beginning .. output .. ending .. "\\end{mplibcode}" tex.sprint(output) end @@ -322,25 +395,43 @@ function buildRect(largeur,a,b,nbrA, nbrB) traceTikZ(listPoints, triangulation,largeur,"none") end +local function shallowCopy(original) + local copy = {} + for key, value in pairs(original) do + copy[key] = value + end + return copy +end -- function give a real polygon without repeting points function cleanPoly(polygon) polyNew = {} - e1 = polygon[1][1] - e2 = polygon[1][2] + polyCopy = shallowCopy(polygon) + e1 = polyCopy[1][1] + e2 = polyCopy[1][2] table.insert(polyNew, e1) table.insert(polyNew, e2) + table.remove(polyCopy,1) j = 2 - for i=2,#polygon do - bool1 = (polygon[i][1] == polyNew[j]) - bool2 = (polygon[i][2] == polyNew[j]) - if(bool1 or bool2) then -- the edge has a common point with polyNew[j] - if(not bool1) then - table.insert(polyNew, polygon[i][1]) - j = j+1 - elseif(not bool2) then - table.insert(polyNew, polygon[i][2]) - j = j+1 + while #polyCopy>1 do + i=1 + find = false + while (i<=#polyCopy and find==false) do + i = i+1 + bool1 = (polyCopy[i][1] == polyNew[j]) + bool2 = (polyCopy[i][2] == polyNew[j]) + if(bool1 or bool2) then -- the edge has a common point with polyNew[j] + if(not bool1) then + table.insert(polyNew, polyCopy[i][1]) + find = true + table.remove(polyCopy,i) + j = j+1 + elseif(not bool2) then + table.insert(polyNew, polyCopy[i][2]) + find = true + table.remove(polyCopy,i) + j = j+1 + end end end end @@ -464,7 +555,11 @@ function TeXaddOnePointMPBW(listPoints,P,step,bbox) PointI = listPoints[triangulation[i][1]] PointJ = listPoints[triangulation[i][2]] PointK = listPoints[triangulation[i][3]] - output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;" + if(triangulation[i].type == "bbox") then + output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;" + else + output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;" + end end -- draw and fill the bad triangle for i=1,#badTriangles do @@ -483,8 +578,14 @@ function TeXaddOnePointMPBW(listPoints,P,step,bbox) output = output .. "draw fullcircle scaled ("..radius .."*2u) shifted ("..center.x .. "*u," .. center.y .. "*u) dashed evenly withcolor \\luameshmpcolorCircle;" end -- mark the points + j=1 for i=1,#listPoints do - output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor;" + if(listPoints[i].type == "bbox") then + output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;" + j=j+1 + else + output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;" + end end -- mark the point to add output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew;" @@ -500,7 +601,11 @@ function TeXaddOnePointMPBW(listPoints,P,step,bbox) PointI = listPoints[triangulation[i][1]] PointJ = listPoints[triangulation[i][2]] PointK = listPoints[triangulation[i][3]] - output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;" + if(triangulation[i].type == "bbox") then + output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;" + else + output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;" + end end -- fill and draw the cavity path = "" @@ -511,8 +616,14 @@ function TeXaddOnePointMPBW(listPoints,P,step,bbox) output = output .. "fill " .. path .. "cycle withcolor \\luameshmpcolorBack;" output = output .. "draw " .. path .. "cycle withcolor \\luameshmpcolorNew withpen pencircle scaled 1pt;" -- mark the points of the mesh + j=1 for i=1,#listPoints do - output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;" + if(listPoints[i].type == "bbox") then + output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;" + j=j+1 + else + output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;" + end end -- mark the adding point output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;" @@ -528,7 +639,11 @@ function TeXaddOnePointMPBW(listPoints,P,step,bbox) PointI = listPoints[triangulation[i][1]] PointJ = listPoints[triangulation[i][2]] PointK = listPoints[triangulation[i][3]] - output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor ;" + if(triangulation[i].type == "bbox") then + output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolorBbox;" + else + output = output .. "draw (".. PointI.x ..",".. PointI.y ..")*u--("..PointJ.x..",".. PointJ.y ..")*u--("..PointK.x..",".. PointK.y ..")*u--cycle withcolor \\luameshmpcolor;" + end end -- fill the cavity path = "" @@ -544,8 +659,14 @@ function TeXaddOnePointMPBW(listPoints,P,step,bbox) 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;" end -- mark points + j=1 for i=1,#listPoints do - output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;" + if(listPoints[i].type == "bbox") then + output = output .. "dotlabel.llft (btex $\\MeshPoint^{*}_{" .. j .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolorBbox ;" + j=j+1 + else + output = output .. "dotlabel.llft (btex $\\MeshPoint_{" .. i .. "}$ etex, (" .. listPoints[i].x ..",".. listPoints[i].y .. ")*u ) withcolor \\luameshmpcolor ;" + end end -- mark the added point output = output .. "dotlabel.llft (btex $\\NewPoint$ etex,(" .. P.x ..",".. P.y .. ")*u) withcolor \\luameshmpcolorNew ;" @@ -576,7 +697,7 @@ function TeXOnePointTikZ(chaine,point,step,color,colorBack,colorNew,colorCircle, tex.sprint(output) end -function TeXOnePointMPBW(chaine,point,step,scale,mode,picture,bbox) +function TeXOnePointMPBW(chaine,point,step,scale,mode,bbox) if(mode=="int") then Sx,Sy=string.match(point,"%((.+),(.+)%)") P = {x=Sx, y=Sy} @@ -590,7 +711,7 @@ function TeXOnePointMPBW(chaine,point,step,scale,mode,picture,bbox) tex.sprint(output) end -function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,picture,bbox) +function TeXOnePointMPBWinc(chaine,point,beginning,ending,step,scale,mode,bbox) if(mode=="int") then Sx,Sy=string.match(point,"%((.+),(.+)%)") P = {x=Sx, y=Sy}