fonction meshPolygon pour MetaPost faite, ajout de dotpoints pour drawMesh (toujours...
[delaunay.git] / luamesh.lua
1 require "luamesh-polygon"
2 require "luamesh-tex"
3
4 local function shallowCopy(original)
5    local copy = {}
6    for key, value in pairs(original) do
7       copy[key] = value
8    end
9    return copy
10 end
11
12 -- Bowyer and Watson algorithm
13 -- Delaunay meshing
14 function BowyerWatson (listPoints,bbox)
15    local triangulation = {}
16    local lgth = #listPoints
17    -- add four points to listPoints to have a bounding box
18    listPoints = buildBoundingBox(listPoints)
19    -- the first triangle
20    triangulation[1] = {lgth+1, lgth+2, lgth+3,type="bbox"}
21    -- the second triangle
22    triangulation[2] = {lgth+1, lgth+3, lgth+4,type="bbox"}
23    -- add points one by one
24    for i=1,lgth do
25       -- find the triangles which the circumcircle contained the point to add
26       badTriangles = buildBadTriangles(listPoints[i],triangulation,listPoints)
27       -- build the polygon of the cavity containing the point to add
28       polygon = buildCavity(badTriangles, triangulation)
29       -- remove the bad triangles
30       for j=1,#badTriangles do
31          table.remove(triangulation,badTriangles[j]-(j-1))
32       end
33       -- build the new triangles and add them to triangulation
34       for j=1,#polygon do
35          if((polygon[j][1]>lgth) or (polygon[j][2]>lgth) or (i>lgth)) then
36             table.insert(triangulation,{polygon[j][1],polygon[j][2],i,type="bbox"})
37          else
38             table.insert(triangulation,{polygon[j][1],polygon[j][2],i,type="in"})
39          end
40       end
41    end  -- end adding points of the listPoints
42    -- remove bounding box
43    if(bbox ~= "bbox") then
44       triangulation = removeBoundingBox(triangulation,lgth)
45       table.remove(listPoints,lgth+1)
46       table.remove(listPoints,lgth+1)
47       table.remove(listPoints,lgth+1)
48       table.remove(listPoints,lgth+1)
49    end
50    return triangulation
51 end
52
53
54 function buildBoundingBox(listPoints)
55    -- listPoints : list of points
56    -- epsV        : parameter for the distance of the bounding box
57    local xmin, xmax, ymin, ymax, eps
58    xmin = 1000
59    ymin = 1000
60    xmax = -1000
61    ymax = -1000
62    for i=1,#listPoints do
63       if (listPoints[i].x < xmin) then
64          xmin = listPoints[i].x
65       end
66       if (listPoints[i].x > xmax) then
67          xmax = listPoints[i].x
68       end
69       if (listPoints[i].y < ymin) then
70          ymin = listPoints[i].y
71       end
72       if (listPoints[i].y > ymax) then
73          ymax = listPoints[i].y
74       end
75    end
76    eps = math.max(math.abs(xmax-xmin),math.abs(ymax-ymin))*0.15
77    xmin = xmin - eps
78    xmax = xmax + eps
79    ymin = ymin - eps
80    ymax = ymax + eps
81    -- add points of the bounding box in last positions
82    table.insert(listPoints,{x=xmin,y=ymin,type="bbox"})
83    table.insert(listPoints,{x=xmin,y=ymax,type="bbox"})
84    table.insert(listPoints,{x=xmax,y=ymax,type="bbox"})
85    table.insert(listPoints,{x=xmax,y=ymin,type="bbox"})
86    return listPoints
87 end
88
89 function BoundingBox(listPoints)
90    -- listPoints : list of points
91    -- epsV        : parameter for the distance of the bounding box
92    local xmin, xmax, ymin, ymax, eps
93    xmin = 1000
94    ymin = 1000
95    xmax = -1000
96    ymax = -1000
97    for i=1,#listPoints do
98       if (listPoints[i].x < xmin) then
99          xmin = listPoints[i].x
100       end
101       if (listPoints[i].x > xmax) then
102          xmax = listPoints[i].x
103       end
104       if (listPoints[i].y < ymin) then
105          ymin = listPoints[i].y
106       end
107       if (listPoints[i].y > ymax) then
108          ymax = listPoints[i].y
109       end
110    end
111    eps = math.max(math.abs(xmax-xmin),math.abs(ymax-ymin))*0.15
112    xmin = xmin - eps
113    xmax = xmax + eps
114    ymin = ymin - eps
115    ymax = ymax + eps
116    return xmin, xmax, ymin, ymax
117 end
118
119 function removeBoundingBox(triangulation,lgth)
120    -- build the four bounding box edge
121    point1 = lgth+1
122    point2 = lgth+2
123    point3 = lgth+3
124    point4 = lgth+4
125    -- for all triangle
126    local newTriangulation = {}
127    for i=1,#triangulation do
128       boolE1 = pointInTriangle(point1,triangulation[i])
129       boolE2 = pointInTriangle(point2,triangulation[i])
130       boolE3 = pointInTriangle(point3,triangulation[i])
131       boolE4 = pointInTriangle(point4,triangulation[i])
132       if((not boolE1) and (not boolE2) and (not boolE3) and (not boolE4)) then
133          table.insert(newTriangulation,triangulation[i])
134       end
135    end
136    return newTriangulation
137 end
138
139
140 function buildBadTriangles(point, triangulation,listPoints)
141    local badTriangles = {}
142    print(#triangulation)
143    for j=1,#triangulation do -- for all triangles
144       A = listPoints[triangulation[j][1]]
145       B = listPoints[triangulation[j][2]]
146       C = listPoints[triangulation[j][3]]
147       center, radius = circoncircle(A,B,C)
148       CP = Vector(center,point)
149       if(VectorNorm(CP)<radius) then -- the point belongs to the circoncirle
150          table.insert(badTriangles,j)
151       end
152    end
153    return badTriangles
154 end
155
156 -- construction of the cavity composed by the bad triangles around the point to add
157 function buildCavity(badTriangles, triangulation)
158    local polygon = {}
159    for j=1,#badTriangles do -- for all bad triangles
160       ind = badTriangles[j]
161       for k=1,3 do -- for all edges
162          edge = {triangulation[ind][k],triangulation[ind][k%3+1]}
163          edgeBord = false
164          for l = 1,#badTriangles do -- for all badtriangles
165             badInd = badTriangles[l]
166             if(badInd ~= ind) then -- if not the current one
167                edgeBord = edgeBord or edgeInTriangle(edge,triangulation[badInd])
168             end
169          end --
170          -- if the edge does not belong to another bad triangle
171          if(edgeBord == false) then
172             -- insert the edge to the cavity
173             table.insert(polygon,edge)
174          end
175       end --
176    end --
177    return polygon
178 end
179
180 function edgeInTriangle(e,t)
181    in1 = false
182    in2 = false
183    for i=1,3 do
184       if e[1] == t[i] then
185          in1 = true
186       end
187       if e[2] == t[i] then
188          in2 = true
189       end
190    end
191    out = (in1 and in2)
192    return out
193 end
194
195 function pointInTriangle(e,t)
196    in1 = false
197    for i=1,3 do
198       if e == t[i] then
199          in1 = true
200       end
201    end
202    return in1
203 end
204
205
206 function Vector(A,B)
207    local out = {x = B.x - A.x, y = B.y - A.y}
208    return out
209 end
210
211 function VectorNorm(NP)
212    return math.sqrt(NP.x*NP.x +NP.y*NP.y)
213 end
214
215 -- circoncircle
216 function circoncircle(M, N, P)
217    -- Compute center and radius of the circoncircle of the triangle M N P
218
219    -- return : (center [Point],radius [float])
220
221    local MN = Vector(M,N)
222    local NP = Vector(N,P)
223    local PM = Vector(P,M)
224    m = VectorNorm(NP)  -- |NP|
225    n = VectorNorm(PM)  -- |PM|
226    p = VectorNorm(MN)  -- |MN|
227
228    d = (m + n + p) * (-m + n + p) * (m - n + p) * (m + n - p)
229    if d > 0 then
230       rad = m * n * p / math.sqrt(d)
231    else
232       rad = 0
233    end
234    d = -2 * (M.x * NP.y + N.x * PM.y + P.x * MN.y)
235    O = {x=0, y=0}
236    OM = Vector(O, M)
237    ON = Vector(O, N)
238    OP = Vector(O, P)
239    om2 = math.pow(VectorNorm(OM),2)  -- |OM|**2
240    on2 = math.pow(VectorNorm(ON),2)  -- |ON|**2
241    op2 = math.pow(VectorNorm(OP),2)  -- |OP|**2
242    x0 = -(om2 * NP.y + on2 * PM.y + op2 * MN.y) / d
243    y0 = (om2 * NP.x + on2 * PM.x + op2 * MN.x) / d
244    if d == 0 then
245       Out = {nil, nil}
246    else
247       Out = {x=x0, y=y0}
248    end
249    return Out, rad  -- (center [Point], R [float])
250 end
251
252 -- compute the list of the circumcircle of a triangulation
253 function listCircumCenter(listPoints,triangulation)
254    local list = {}
255    for j=1,#triangulation do
256       A = listPoints[triangulation[j][1]]
257       B = listPoints[triangulation[j][2]]
258       C = listPoints[triangulation[j][3]]
259       center, radius = circoncircle(A,B,C)
260       table.insert(list,{x=center.x,y=center.y,r=radius})
261    end
262    return list
263 end
264
265 -- find the three neighbour triangles of T
266 function findNeighbour(T,i,triangulation)
267    -- T : triangle
268    -- i : index of T in triangualation
269    -- triangulation
270
271    list = {}
272    -- define the three edge
273    e1 = {T[1],T[2]}
274    e2 = {T[2],T[3]}
275    e3 = {T[3],T[1]}
276    for j=1,#triangulation do
277       if j~= i then
278          if(edgeInTriangle(e1,triangulation[j])) then
279             table.insert(list,j)
280          end
281          if(edgeInTriangle(e2,triangulation[j])) then
282             table.insert(list,j)
283          end
284          if(edgeInTriangle(e3,triangulation[j])) then
285             table.insert(list,j)
286          end
287       end
288    end
289    return list
290 end
291
292 -- test if edge are the same (reverse)
293 function equalEdge(e1,e2)
294    if(((e1[1] == e2[1]) and (e1[2] == e2[2])) or ((e1[1] == e2[2]) and (e1[2] == e2[1]))) then
295       return true
296    else
297       return false
298    end
299 end
300
301 -- test if the edge belongs to the list
302 function edgeInList(e,listE)
303    output = false
304    for i=1,#listE do
305       if(equalEdge(e,listE[i])) then
306          output = true
307       end
308    end
309    return output
310 end
311
312 -- build the edges of the Voronoi diagram with a given triangulation
313 function buildVoronoi(listPoints, triangulation)
314    local listCircumCircle = listCircumCenter(listPoints, triangulation)
315    local listVoronoi = {}
316    for i=1,#listCircumCircle do
317       listN = findNeighbour(triangulation[i],i,triangulation)
318       for j=1,#listN do
319          edge = {i,listN[j]}
320          if( not edgeInList(edge, listVoronoi)) then
321             table.insert(listVoronoi, edge)
322          end
323       end
324    end
325    return listVoronoi
326 end
327
328 -- build the list of points
329 function buildList(chaine, mode)
330    -- if mode = int : the list is given in the chaine string (x1,y1);(x2,y2);...;(xn,yn)
331    -- if mode = ext : the list is given in a file line by line with space separation
332    local listPoints = {}
333    if mode == "int" then
334       local points = string.explode(chaine, ";")
335       local lgth=#points
336       for i=1,lgth do
337          Sx,Sy=string.match(points[i],"%((.+),(.+)%)")
338          listPoints[i]={x=tonumber(Sx),y=tonumber(Sy)}
339       end
340    elseif mode == "ext" then
341       io.input(chaine) -- open the file
342       text=io.read("*all")
343       lines=string.explode(text,"\n+") -- all the lines
344       tablePoints={}
345       for i=1,#lines do
346          xy=string.explode(lines[i]," +")
347          listPoints[i]={x=tonumber(xy[1]),y=tonumber(xy[2])}
348       end
349    else
350       print("Non existing mode")
351    end
352    return listPoints
353 end
354
355
356 -- function to add points on a polygon to respect
357 -- the size of unit mesh
358 function addPointsPolygon(polygon,h)
359    local newPolygon = shallowCopy(polygon)
360    k=0 -- to follow in the newPolygon
361    for i=1,#polygon do
362       k = k+1
363       ip = (i)%(#polygon)+1
364       dist = math.sqrt(math.pow(polygon[i].x-polygon[ip].x,2) + math.pow(polygon[i].y-polygon[ip].y,2))
365       -- if the distance between two ponits of the polygon is greater than 1.5*h
366       if(dist>=2*h) then
367          n = math.floor(dist/h)
368          print(polygon[i].x,polygon[i].y,polygon[ip].x,polygon[ip].y)
369          step = dist/(n+1)
370          print("step="..step)
371          print("n="..n)
372          for j=1,n do
373             print(j*step)
374             a = {x=polygon[i].x+j*step*(polygon[ip].x-polygon[i].x)/dist,y=polygon[i].y+j*step*(polygon[ip].y-polygon[i].y)/dist}
375             print("new = "..a.x.."   "..a.y)
376             table.insert(newPolygon,k+j,a)
377          end
378          k=k+n
379       end
380    end
381    return newPolygon
382 end
383
384 -- function to build a gridpoints from the bounding box
385 -- with a prescribed
386 function buildGrid(listPoints,h)
387    -- listPoints : list of the points of the polygon, ordered
388    -- h : parameter for the grid
389    xmin, xmax, ymin, ymax = BoundingBox(listPoints)
390
391    local grid = rectangleList(xmin,xmax,ymin,ymax,h)
392    return grid
393 end
394
395 -- function to build the list of points in the rectangle
396 function rectangleList(xmin,xmax,ymin,ymax,h)
397    nbrX = math.floor(math.abs(xmax-xmin)/h)
398    nbrY = math.floor(math.abs(ymax-ymin)/h)
399    local listPoints = {}
400    k=1
401    for i=1,(nbrX+1) do
402       for j=1,(nbrY+1) do
403          listPoints[k] = {x = xmin+(i-1)*h, y=ymin+(j-1)*h}
404          k=k+1
405       end
406    end
407    return listPoints
408 end
409
410
411 -- function to add points from a grid to the interior of a polygon
412 function addGridPoints(polygon, grid,h)
413    local listPoints = shallowCopy(polygon)
414    k = #polygon
415    for i=1, #grid do
416       --print(grid[i].x,grid[i].y)
417       --print(isInside(polygon,grid[i]))
418       if(isInside(polygon,grid[i],h)) then
419          k=k+1
420          listPoints[k] = grid[i]
421       end
422    end
423    return listPoints
424 end
425
426
427
428 -- function give a real polygon without repeting points
429 function cleanPoly(polygon)
430    polyNew = {}
431    polyCopy = shallowCopy(polygon)
432    e1 = polyCopy[1][1]
433    e2 = polyCopy[1][2]
434    table.insert(polyNew, e1)
435    table.insert(polyNew, e2)
436    table.remove(polyCopy,1)
437    j = 2
438    while #polyCopy>1 do
439       i=1
440       find = false
441       while (i<=#polyCopy and find==false) do
442          bool1 = (polyCopy[i][1] == polyNew[j])
443          bool2 = (polyCopy[i][2] == polyNew[j])
444          if(bool1 or bool2) then -- the edge has a common point with polyNew[j]
445             if(not bool1) then
446                table.insert(polyNew, polyCopy[i][1])
447                find = true
448                table.remove(polyCopy,i)
449                j = j+1
450             elseif(not bool2) then
451                table.insert(polyNew, polyCopy[i][2])
452                find = true
453                table.remove(polyCopy,i)
454                j = j+1
455             end
456          end
457          i=i+1
458       end
459    end
460    return polyNew
461 end
462
463
464
465 -- build the list of points extern and stop at nbr
466 function buildListExt(chaine, stop)
467    listPoints = {}
468    io.input(chaine) -- open the file
469    text=io.read("*all")
470    lines=string.explode(text,"\n+") -- all the lines
471    for i=1,tonumber(stop) do
472       xy=string.explode(lines[i]," +")
473       table.insert(listPoints,{x=tonumber(xy[1]),y=tonumber(xy[2])})
474    end
475    xy=string.explode(lines[stop+1]," +")
476    point={x=tonumber(xy[1]),y=tonumber(xy[2])}
477    return point, listPoints
478 end
479
480 function split(pString, pPattern)
481    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
482    local fpat = "(.-)" .. pPattern
483    local last_end = 1
484    local s, e, cap = pString:find(fpat, 1)
485    while s do
486       if s ~= 1 or cap ~= "" then
487          table.insert(Table,cap)
488       end
489       last_end = e+1
490       s, e, cap = pString:find(fpat, last_end)
491    end
492    if last_end <= #pString then
493       cap = pString:sub(last_end)
494       table.insert(Table, cap)
495    end
496    return Table
497 end
498
499 function readGmsh(file)
500    io.input(file) -- open the file
501    text=io.read("*all")
502    local lines = split(text,"\n+") -- all the lines
503    listPoints={}
504    triangulation ={}
505    boolNodes = false
506    Jnodes = 0
507    boolElements = false
508    Jelements = 0
509    J=0
510    for i=1,#lines-J do
511       if(lines[i+J] == "$EndNodes") then
512          boolNodes = false
513          -- go to the next line
514       end
515       if(boolNodes) then -- we are in the Nodes environment
516          xy=split(lines[i+J]," +")
517          table.insert(listPoints,{x=tonumber(xy[2]),y=tonumber(xy[3])})
518       end
519       if(lines[i+J] == "$Nodes") then
520          boolNodes = true
521          -- go to the next line
522          J=J+1
523       end
524       if(lines[i+J] == "$EndElements") then
525          boolElements = false
526          -- go to the next line
527       end
528       if(boolElements) then -- we are in the Nodes environment
529          xy=split(lines[i+J]," +")
530          if(tonumber(xy[2]) == 2) then -- if the element is a triangle
531             nbrTags = xy[3]+1
532             table.insert(triangulation,{tonumber(xy[2+nbrTags+1]),tonumber(xy[2+nbrTags+2]),tonumber(xy[2+nbrTags+3])})
533          end
534       end
535       if(lines[i+J] == "$Elements") then
536          boolElements = true
537          -- go to the next line
538          J=J+1
539       end
540    end
541    return listPoints, triangulation
542 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.