1 require "luamesh-polygon"
4 local function shallowCopy(original)
6 for key, value in pairs(original) do
12 -- Bowyer and Watson algorithm
14 function BowyerWatson (listPoints,bbox)
15 local triangulation = {}
17 -- add four points to listPoints to have a bounding box
18 listPoints = buildBoundingBox(listPoints)
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
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))
33 -- build the new triangles and add them to triangulation
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"})
38 table.insert(triangulation,{polygon[j][1],polygon[j][2],i,type="in"})
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)
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
62 for i=1,#listPoints do
63 if (listPoints[i].x < xmin) then
64 xmin = listPoints[i].x
66 if (listPoints[i].x > xmax) then
67 xmax = listPoints[i].x
69 if (listPoints[i].y < ymin) then
70 ymin = listPoints[i].y
72 if (listPoints[i].y > ymax) then
73 ymax = listPoints[i].y
76 eps = math.max(math.abs(xmax-xmin),math.abs(ymax-ymin))*0.15
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"})
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
97 for i=1,#listPoints do
98 if (listPoints[i].x < xmin) then
99 xmin = listPoints[i].x
101 if (listPoints[i].x > xmax) then
102 xmax = listPoints[i].x
104 if (listPoints[i].y < ymin) then
105 ymin = listPoints[i].y
107 if (listPoints[i].y > ymax) then
108 ymax = listPoints[i].y
111 eps = math.max(math.abs(xmax-xmin),math.abs(ymax-ymin))*0.15
116 return xmin, xmax, ymin, ymax
119 function removeBoundingBox(triangulation,lgth)
120 -- build the four bounding box edge
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])
136 return newTriangulation
140 function buildBadTriangles(point, triangulation,listPoints)
141 local badTriangles = {}
142 for j=1,#triangulation do -- for all triangles
143 A = listPoints[triangulation[j][1]]
144 B = listPoints[triangulation[j][2]]
145 C = listPoints[triangulation[j][3]]
146 center, radius = circoncircle(A,B,C)
147 CP = Vector(center,point)
148 if(VectorNorm(CP)<radius) then -- the point belongs to the circoncirle
149 table.insert(badTriangles,j)
155 -- construction of the cavity composed by the bad triangles around the point to add
156 function buildCavity(badTriangles, triangulation)
158 for j=1,#badTriangles do -- for all bad triangles
159 ind = badTriangles[j]
160 for k=1,3 do -- for all edges
161 edge = {triangulation[ind][k],triangulation[ind][k%3+1]}
163 for l = 1,#badTriangles do -- for all badtriangles
164 badInd = badTriangles[l]
165 if(badInd ~= ind) then -- if not the current one
166 edgeBord = edgeBord or edgeInTriangle(edge,triangulation[badInd])
169 -- if the edge does not belong to another bad triangle
170 if(edgeBord == false) then
171 -- insert the edge to the cavity
172 table.insert(polygon,edge)
179 function edgeInTriangle(e,t)
194 function pointInTriangle(e,t)
206 local out = {x = B.x - A.x, y = B.y - A.y}
210 function VectorNorm(NP)
211 return math.sqrt(NP.x*NP.x +NP.y*NP.y)
215 function circoncircle(M, N, P)
216 -- Compute center and radius of the circoncircle of the triangle M N P
218 -- return : (center [Point],radius [float])
220 local MN = Vector(M,N)
221 local NP = Vector(N,P)
222 local PM = Vector(P,M)
223 m = VectorNorm(NP) -- |NP|
224 n = VectorNorm(PM) -- |PM|
225 p = VectorNorm(MN) -- |MN|
227 d = (m + n + p) * (-m + n + p) * (m - n + p) * (m + n - p)
229 rad = m * n * p / math.sqrt(d)
233 d = -2 * (M.x * NP.y + N.x * PM.y + P.x * MN.y)
238 om2 = math.pow(VectorNorm(OM),2) -- |OM|**2
239 on2 = math.pow(VectorNorm(ON),2) -- |ON|**2
240 op2 = math.pow(VectorNorm(OP),2) -- |OP|**2
241 x0 = -(om2 * NP.y + on2 * PM.y + op2 * MN.y) / d
242 y0 = (om2 * NP.x + on2 * PM.x + op2 * MN.x) / d
248 return Out, rad -- (center [Point], R [float])
251 -- compute the list of the circumcircle of a triangulation
252 function listCircumCenter(listPoints,triangulation)
254 for j=1,#triangulation do
255 A = listPoints[triangulation[j][1]]
256 B = listPoints[triangulation[j][2]]
257 C = listPoints[triangulation[j][3]]
258 center, radius = circoncircle(A,B,C)
259 table.insert(list,{x=center.x,y=center.y,r=radius})
264 -- find the three neighbour triangles of T
265 function findNeighbour(T,i,triangulation)
267 -- i : index of T in triangualation
271 -- define the three edge
275 for j=1,#triangulation do
277 if(edgeInTriangle(e1,triangulation[j])) then
280 if(edgeInTriangle(e2,triangulation[j])) then
283 if(edgeInTriangle(e3,triangulation[j])) then
291 -- test if edge are the same (reverse)
292 function equalEdge(e1,e2)
293 if(((e1[1] == e2[1]) and (e1[2] == e2[2])) or ((e1[1] == e2[2]) and (e1[2] == e2[1]))) then
300 -- test if the edge belongs to the list
301 function edgeInList(e,listE)
304 if(equalEdge(e,listE[i])) then
311 -- build the edges of the Voronoi diagram with a given triangulation
312 function buildVoronoi(listPoints, triangulation)
313 local listCircumCircle = listCircumCenter(listPoints, triangulation)
314 local listVoronoi = {}
315 for i=1,#listCircumCircle do
316 listN = findNeighbour(triangulation[i],i,triangulation)
319 if( not edgeInList(edge, listVoronoi)) then
320 table.insert(listVoronoi, edge)
327 -- build the list of points
328 function buildList(chaine, mode)
329 -- if mode = int : the list is given in the chaine string (x1,y1);(x2,y2);...;(xn,yn)
330 -- if mode = ext : the list is given in a file line by line with space separation
331 local listPoints = {}
332 if mode == "int" then
333 local points = string.explode(chaine, ";")
336 Sx,Sy=string.match(points[i],"%((.+),(.+)%)")
337 listPoints[i]={x=tonumber(Sx),y=tonumber(Sy)}
339 elseif mode == "ext" then
340 io.input(chaine) -- open the file
342 lines=string.explode(text,"\n+") -- all the lines
345 xy=string.explode(lines[i]," +")
346 listPoints[i]={x=tonumber(xy[1]),y=tonumber(xy[2])}
349 print("Non existing mode")
355 -- function to add points on a polygon to respect
356 -- the size of unit mesh
357 function addPointsPolygon(polygon,h)
358 local newPolygon = shallowCopy(polygon)
359 k=0 -- to follow in the newPolygon
362 ip = (i)%(#polygon)+1
363 dist = math.sqrt(math.pow(polygon[i].x-polygon[ip].x,2) + math.pow(polygon[i].y-polygon[ip].y,2))
364 -- if the distance between two ponits of the polygon is greater than 1.5*h
366 n = math.floor(dist/h)
369 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}
370 table.insert(newPolygon,k+j,a)
378 -- function to build a gridpoints from the bounding box
380 function buildGrid(listPoints,h,random)
381 -- listPoints : list of the points of the polygon, ordered
382 -- h : parameter for the grid
383 xmin, xmax, ymin, ymax = BoundingBox(listPoints)
385 local grid = rectangleList(xmin,xmax,ymin,ymax,h,random)
389 -- function to build the list of points in the rectangle
390 function rectangleList(xmin,xmax,ymin,ymax,h,random)
392 math.randomseed( os.time() )
393 nbrX = math.floor(math.abs(xmax-xmin)/h)
394 nbrY = math.floor(math.abs(ymax-ymin)/h)
395 local listPoints = {}
400 if(random=="perturb") then
406 listPoints[k] = {x = xmin+(i-1)*h+rd*fact, y=ymin+(j-1)*h+rd*fact}
414 -- function to add points from a grid to the interior of a polygon
415 function addGridPoints(polygon, grid,h)
416 local listPoints = shallowCopy(polygon)
419 --print(grid[i].x,grid[i].y)
420 --print(isInside(polygon,grid[i]))
421 if(isInside(polygon,grid[i],h)) then
423 listPoints[k] = grid[i]
431 -- function give a real polygon without repeting points
432 function cleanPoly(polygon)
434 local polyCopy = shallowCopy(polygon)
437 table.insert(polyNew, e1)
438 table.insert(polyNew, e2)
439 table.remove(polyCopy,1)
444 while (i<=#polyCopy and find==false) do
445 bool1 = (polyCopy[i][1] == polyNew[j])
446 bool2 = (polyCopy[i][2] == polyNew[j])
447 if(bool1 or bool2) then -- the edge has a common point with polyNew[j]
449 table.insert(polyNew, polyCopy[i][1])
451 table.remove(polyCopy,i)
453 elseif(not bool2) then
454 table.insert(polyNew, polyCopy[i][2])
456 table.remove(polyCopy,i)
468 -- build the list of points extern and stop at nbr
469 function buildListExt(chaine, stop)
470 local listPoints = {}
471 io.input(chaine) -- open the file
473 lines=string.explode(text,"\n+") -- all the lines
474 for i=1,tonumber(stop) do
475 xy=string.explode(lines[i]," +")
476 table.insert(listPoints,{x=tonumber(xy[1]),y=tonumber(xy[2])})
478 xy=string.explode(lines[stop+1]," +")
479 point={x=tonumber(xy[1]),y=tonumber(xy[2])}
480 return point, listPoints
483 function split(pString, pPattern)
484 local Table = {} -- NOTE: use {n = 0} in Lua-5.0
485 local fpat = "(.-)" .. pPattern
487 local s, e, cap = pString:find(fpat, 1)
489 if s ~= 1 or cap ~= "" then
490 table.insert(Table,cap)
493 s, e, cap = pString:find(fpat, last_end)
495 if last_end <= #pString then
496 cap = pString:sub(last_end)
497 table.insert(Table, cap)
502 function readGmsh(file)
503 io.input(file) -- open the file
505 local lines = split(text,"\n+") -- all the lines
507 local triangulation ={}
514 if(lines[i+J] == "$EndNodes") then
516 -- go to the next line
518 if(boolNodes) then -- we are in the Nodes environment
519 xy=split(lines[i+J]," +")
520 table.insert(listPoints,{x=tonumber(xy[2]),y=tonumber(xy[3])})
522 if(lines[i+J] == "$Nodes") then
524 -- go to the next line
527 if(lines[i+J] == "$EndElements") then
529 -- go to the next line
531 if(boolElements) then -- we are in the Nodes environment
532 xy=split(lines[i+J]," +")
533 if(tonumber(xy[2]) == 2) then -- if the element is a triangle
535 table.insert(triangulation,{tonumber(xy[2+nbrTags+1]),tonumber(xy[2+nbrTags+2]),tonumber(xy[2+nbrTags+3])})
538 if(lines[i+J] == "$Elements") then
540 -- go to the next line
544 return listPoints, triangulation