summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/luamesh/luamesh.lua
blob: e03703c6d726a13e823e77d821ae6f6d7a9aafa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
-----------------------------------------------------------------------
--         FILE:  luamesh.lua
--  DESCRIPTION:  core functions of luamesh package
-- REQUIREMENTS:  local scripts (luamesh-polygon and luamesh-tex)
--       AUTHOR:  Maxime Chupin
-----------------------------------------------------------------------
require "luamesh-polygon"
require "luamesh-tex"

local function shallowCopy(original)
   local copy = {}
   for key, value in pairs(original) do
      copy[key] = value
   end
   return copy
end

-- Bowyer and Watson algorithm
-- Delaunay meshing
function BowyerWatson (listPoints,bbox)
   local triangulation = {}
   lgth = #listPoints
   -- add four points to listPoints to have a bounding box
   listPoints = buildBoundingBox(listPoints)
   -- the first triangle
   triangulation[1] = {lgth+1, lgth+2, lgth+3,type="bbox"}
   -- the second triangle
   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
      badTriangles = buildBadTriangles(listPoints[i],triangulation,listPoints)
      -- build the polygon of the cavity containing the point to add
      polygon = buildCavity(badTriangles, triangulation)
      -- remove the bad triangles
      for j=1,#badTriangles do
         table.remove(triangulation,badTriangles[j]-(j-1))
      end
      -- build the new triangles and add them to triangulation
      for j=1,#polygon do
         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
   if(bbox ~= "bbox") then
      triangulation = removeBoundingBox(triangulation,lgth)
      table.remove(listPoints,lgth+1)
      table.remove(listPoints,lgth+1)
      table.remove(listPoints,lgth+1)
      table.remove(listPoints,lgth+1)
   end
   return triangulation
end


function buildBoundingBox(listPoints)
   -- listPoints : list of points
   -- epsV        : parameter for the distance of the bounding box
   local xmin, xmax, ymin, ymax, eps
   xmin = 1000
   ymin = 1000
   xmax = -1000
   ymax = -1000
   for i=1,#listPoints do
      if (listPoints[i].x < xmin) then
         xmin = listPoints[i].x
      end
      if (listPoints[i].x > xmax) then
         xmax = listPoints[i].x
      end
      if (listPoints[i].y < ymin) then
         ymin = listPoints[i].y
      end
      if (listPoints[i].y > ymax) then
         ymax = listPoints[i].y
      end
   end
   eps = math.max(math.abs(xmax-xmin),math.abs(ymax-ymin))*0.15
   xmin = xmin - eps
   xmax = xmax + eps
   ymin = ymin - eps
   ymax = ymax + eps
   -- add points of the bounding box in last positions
   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

function BoundingBox(listPoints)
   -- listPoints : list of points
   -- epsV        : parameter for the distance of the bounding box
   local xmin, xmax, ymin, ymax, eps
   xmin = 1000
   ymin = 1000
   xmax = -1000
   ymax = -1000
   for i=1,#listPoints do
      if (listPoints[i].x < xmin) then
         xmin = listPoints[i].x
      end
      if (listPoints[i].x > xmax) then
         xmax = listPoints[i].x
      end
      if (listPoints[i].y < ymin) then
         ymin = listPoints[i].y
      end
      if (listPoints[i].y > ymax) then
         ymax = listPoints[i].y
      end
   end
   eps = math.max(math.abs(xmax-xmin),math.abs(ymax-ymin))*0.15
   xmin = xmin - eps
   xmax = xmax + eps
   ymin = ymin - eps
   ymax = ymax + eps
   return xmin, xmax, ymin, ymax
end

function removeBoundingBox(triangulation,lgth)
   -- build the four bounding box edge
   point1 = lgth+1
   point2 = lgth+2
   point3 = lgth+3
   point4 = lgth+4
   -- for all triangle
   local newTriangulation = {}
   for i=1,#triangulation do
      boolE1 = pointInTriangle(point1,triangulation[i])
      boolE2 = pointInTriangle(point2,triangulation[i])
      boolE3 = pointInTriangle(point3,triangulation[i])
      boolE4 = pointInTriangle(point4,triangulation[i])
      if((not boolE1) and (not boolE2) and (not boolE3) and (not boolE4)) then
         table.insert(newTriangulation,triangulation[i])
      end
   end
   return newTriangulation
end


function buildBadTriangles(point, triangulation,listPoints)
   local badTriangles = {}
   for j=1,#triangulation do -- for all triangles
      A = listPoints[triangulation[j][1]]
      B = listPoints[triangulation[j][2]]
      C = listPoints[triangulation[j][3]]
      center, radius = circoncircle(A,B,C)
      CP = Vector(center,point)
      if(VectorNorm(CP)<radius) then -- the point belongs to the circoncirle
         table.insert(badTriangles,j)
      end
   end
   return badTriangles
end

-- construction of the cavity composed by the bad triangles around the point to add
function buildCavity(badTriangles, triangulation)
   local polygon = {}
   for j=1,#badTriangles do -- for all bad triangles
      ind = badTriangles[j]
      for k=1,3 do -- for all edges
         edge = {triangulation[ind][k],triangulation[ind][k%3+1]}
         edgeBord = false
         for l = 1,#badTriangles do -- for all badtriangles
            badInd = badTriangles[l]
            if(badInd ~= ind) then -- if not the current one
               edgeBord = edgeBord or edgeInTriangle(edge,triangulation[badInd])
            end
         end --
         -- if the edge does not belong to another bad triangle
         if(edgeBord == false) then
            -- insert the edge to the cavity
            table.insert(polygon,edge)
         end
      end --
   end --
   return polygon
end

function edgeInTriangle(e,t)
   in1 = false
   in2 = false
   for i=1,3 do
      if e[1] == t[i] then
         in1 = true
      end
      if e[2] == t[i] then
         in2 = true
      end
   end
   out = (in1 and in2)
   return out
end

function pointInTriangle(e,t)
   in1 = false
   for i=1,3 do
      if e == t[i] then
         in1 = true
      end
   end
   return in1
end


function Vector(A,B)
   local out = {x = B.x - A.x, y = B.y - A.y}
   return out
end

function VectorNorm(NP)
   return math.sqrt(NP.x*NP.x +NP.y*NP.y)
end

-- circoncircle
function circoncircle(M, N, P)
   -- Compute center and radius of the circoncircle of the triangle M N P

   -- return : (center [Point],radius [float])

   local MN = Vector(M,N)
   local NP = Vector(N,P)
   local PM = Vector(P,M)
   m = VectorNorm(NP)  -- |NP|
   n = VectorNorm(PM)  -- |PM|
   p = VectorNorm(MN)  -- |MN|

   d = (m + n + p) * (-m + n + p) * (m - n + p) * (m + n - p)
   if d > 0 then
      rad = m * n * p / math.sqrt(d)
   else
      rad = 0
   end
   d = -2 * (M.x * NP.y + N.x * PM.y + P.x * MN.y)
   O = {x=0, y=0}
   OM = Vector(O, M)
   ON = Vector(O, N)
   OP = Vector(O, P)
   om2 = math.pow(VectorNorm(OM),2)  -- |OM|**2
   on2 = math.pow(VectorNorm(ON),2)  -- |ON|**2
   op2 = math.pow(VectorNorm(OP),2)  -- |OP|**2
   x0 = -(om2 * NP.y + on2 * PM.y + op2 * MN.y) / d
   y0 = (om2 * NP.x + on2 * PM.x + op2 * MN.x) / d
   if d == 0 then
      Out = {nil, nil}
   else
      Out = {x=x0, y=y0}
   end
   return Out, rad  -- (center [Point], R [float])
end

-- compute the list of the circumcircle of a triangulation
function listCircumCenter(listPoints,triangulation)
   local list = {}
   for j=1,#triangulation do
      A = listPoints[triangulation[j][1]]
      B = listPoints[triangulation[j][2]]
      C = listPoints[triangulation[j][3]]
      center, radius = circoncircle(A,B,C)
      table.insert(list,{x=center.x,y=center.y,r=radius})
   end
   return list
end

-- find the three neighbour triangles of T
function findNeighbour(T,i,triangulation)
   -- T : triangle
   -- i : index of T in triangualation
   -- triangulation

   list = {}
   -- define the three edge
   e1 = {T[1],T[2]}
   e2 = {T[2],T[3]}
   e3 = {T[3],T[1]}
   for j=1,#triangulation do
      if j~= i then
         if(edgeInTriangle(e1,triangulation[j])) then
            table.insert(list,j)
         end
         if(edgeInTriangle(e2,triangulation[j])) then
            table.insert(list,j)
         end
         if(edgeInTriangle(e3,triangulation[j])) then
            table.insert(list,j)
         end
      end
   end
   return list
end

-- test if edge are the same (reverse)
function equalEdge(e1,e2)
   if(((e1[1] == e2[1]) and (e1[2] == e2[2])) or ((e1[1] == e2[2]) and (e1[2] == e2[1]))) then
      return true
   else
      return false
   end
end

-- test if the edge belongs to the list
function edgeInList(e,listE)
   output = false
   for i=1,#listE do
      if(equalEdge(e,listE[i])) then
         output = true
      end
   end
   return output
end

-- build the edges of the Voronoi diagram with a given triangulation
function buildVoronoi(listPoints, triangulation)
   local listCircumCircle = listCircumCenter(listPoints, triangulation)
   local listVoronoi = {}
   for i=1,#listCircumCircle do
      listN = findNeighbour(triangulation[i],i,triangulation)
      for j=1,#listN do
         edge = {i,listN[j]}
         if( not edgeInList(edge, listVoronoi)) then
            table.insert(listVoronoi, edge)
         end
      end
   end
   return listVoronoi
end

-- build the list of points
function buildList(chaine, mode)
   -- if mode = int : the list is given in the chaine string (x1,y1);(x2,y2);...;(xn,yn)
   -- if mode = ext : the list is given in a file line by line with space separation
   local listPoints = {}
   if mode == "int" then
      local points = string.explode(chaine, ";")
      local lgth=#points
      for i=1,lgth do
         Sx,Sy=string.match(points[i],"%((.+),(.+)%)")
         listPoints[i]={x=tonumber(Sx),y=tonumber(Sy)}
      end
   elseif mode == "ext" then
      io.input(chaine) -- open the file
      text=io.read("*all")
      lines=string.explode(text,"\n+") -- all the lines
      tablePoints={}
      for i=1,#lines do
	 xy=string.explode(lines[i]," +")
	 listPoints[i]={x=tonumber(xy[1]),y=tonumber(xy[2])}
      end
   else
      print("Non existing mode")
   end
   return listPoints
end


-- function to add points on a polygon to respect
-- the size of unit mesh
function addPointsPolygon(polygon,h)
   local newPolygon = shallowCopy(polygon)
   k=0 -- to follow in the newPolygon
   for i=1,#polygon do
      k = k+1
      ip = (i)%(#polygon)+1
      dist = math.sqrt(math.pow(polygon[i].x-polygon[ip].x,2) + math.pow(polygon[i].y-polygon[ip].y,2))
      -- if the distance between two ponits of the polygon is greater than 1.5*h
      if(dist>=2*h) then
         n = math.floor(dist/h)
         step = dist/(n+1)
         for j=1,n do
            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}
            table.insert(newPolygon,k+j,a)
         end
         k=k+n
      end
   end
   return newPolygon
end

-- function to build a gridpoints from the bounding box
-- with a prescribed
function buildGrid(listPoints,h,random)
   -- listPoints : list of the points of the polygon, ordered
   -- h : parameter for the grid
   xmin, xmax, ymin, ymax = BoundingBox(listPoints)

   local grid = rectangleList(xmin,xmax,ymin,ymax,h,random)
   return grid
end

-- function to build the list of points in the rectangle
function rectangleList(xmin,xmax,ymin,ymax,h,random)
   -- for the random
   math.randomseed( os.time() )
   nbrX = math.floor(math.abs(xmax-xmin)/h)
   nbrY = math.floor(math.abs(ymax-ymin)/h)
   local listPoints = {}
   k=1
   for i=1,(nbrX+1) do
      for j=1,(nbrY+1) do
         rd = math.random()
         if(random=="perturb") then
            fact = 0.3*h
            --print(fact)
         else
            fact = 0.0
         end
         listPoints[k] = {x = xmin+(i-1)*h+rd*fact, y=ymin+(j-1)*h+rd*fact}
         k=k+1
      end
   end
   return listPoints
end


-- function to add points from a grid to the interior of a polygon
function addGridPoints(polygon, grid,h)
   local listPoints = shallowCopy(polygon)
   k = #polygon
   for i=1, #grid do
      --print(grid[i].x,grid[i].y)
      --print(isInside(polygon,grid[i]))
      if(isInside(polygon,grid[i],h)) then
         k=k+1
         listPoints[k] = grid[i]
      end
   end
   return listPoints
end



-- function give a real polygon without repeting points
function cleanPoly(polygon)
   local polyNew = {}
   local 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
   while #polyCopy>1 do
      i=1
      find = false
      while (i<=#polyCopy and find==false) do
         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
         i=i+1
      end
   end
   return polyNew
end



-- build the list of points extern and stop at nbr
function buildListExt(chaine, stop)
   local listPoints = {}
   io.input(chaine) -- open the file
   text=io.read("*all")
   lines=string.explode(text,"\n+") -- all the lines
   for i=1,tonumber(stop) do
      xy=string.explode(lines[i]," +")
      table.insert(listPoints,{x=tonumber(xy[1]),y=tonumber(xy[2])})
   end
   xy=string.explode(lines[stop+1]," +")
   point={x=tonumber(xy[1]),y=tonumber(xy[2])}
   return point, listPoints
end

function split(pString, pPattern)
   local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pPattern
   local last_end = 1
   local s, e, cap = pString:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
         table.insert(Table,cap)
      end
      last_end = e+1
      s, e, cap = pString:find(fpat, last_end)
   end
   if last_end <= #pString then
      cap = pString:sub(last_end)
      table.insert(Table, cap)
   end
   return Table
end

function readGmsh(file)
   io.input(file) -- open the file
   text=io.read("*all")
   local lines = split(text,"\n+") -- all the lines
   local listPoints={}
   local triangulation ={}
   boolNodes = false
   Jnodes = 0
   boolElements = false
   Jelements = 0

   J=0
   format=split(lines[2]," +")
   if((tonumber(format[1])>=2.0) and (tonumber(format[1])<3.0)) then -- Format 2
      for i=4,#lines-J do
         if(lines[i+J] == "$EndNodes") then
            boolNodes = false
            -- go to the next line
         end
         if(boolNodes) then -- we are in the Nodes environment
            xy=split(lines[i+J]," +")
            table.insert(listPoints,{x=tonumber(xy[2]),y=tonumber(xy[3])})
         end
         if(lines[i+J] == "$Nodes") then
            boolNodes = true
            -- go to the next line
            J=J+1
         end
         if(lines[i+J] == "$EndElements") then
            boolElements = false
            -- go to the next line
         end
         if(boolElements) then -- we are in the Elements environment
            xy=split(lines[i+J]," +")
            if(tonumber(xy[2]) == 2) then -- if the element is a triangle
               nbrTags = xy[3]+1
               table.insert(triangulation,{tonumber(xy[2+nbrTags+1]),tonumber(xy[2+nbrTags+2]),tonumber(xy[2+nbrTags+3])})
            end
         end
         if(lines[i+J] == "$Elements") then
            boolElements = true
            -- go to the next line
            J=J+1
         end
      end
   end


   if(tonumber(format[1]) >= 4.0) then -- format 4
      for i=4,#lines-J do
         if(lines[i+J] == "$EndNodes") then
            boolNodes = false
            -- go to the next line
         end
         if(boolNodes) then -- we are in the Nodes environment
            xy=split(lines[i+J]," +") -- bloc 4 number
            nbrBloc = tonumber(xy[4])
            for k=1,nbrBloc do
               xyz=split(lines[i+J+nbrBloc+k]," +") -- x y z
               table.insert(listPoints,{x=tonumber(xyz[1]),y=tonumber(xyz[2])})
            end
            J =J+ 2*tonumber(xy[4])
         end
         if(lines[i+J] == "$Nodes") then
            boolNodes = true
            -- go to the next line
            J=J+1
         end
         if(lines[i+J] == "$EndElements") then
            boolElements = false
            -- go to the next line
         end
         if(boolElements) then -- we are in the Elements environment
            xy=split(lines[i+J]," +")
            if(tonumber(xy[1]) < 2) then -- if the elements block is not a triangle
               J = J+tonumber(xy[4]) -- we jump to the new block
            end
            if(tonumber(xy[1]) == 2) then -- if the elements block is a triangle
               nbrElements = tonumber(xy[4])
               for k=1,nbrElements do
                  xyz=split(lines[i+J+k]," +") -- line tag node1 node2 node3
                  table.insert(triangulation,{tonumber(xyz[2]),tonumber(xyz[3]),tonumber(xyz[4])})
               end
               J = J+nbrElements
            end
         end
         if(lines[i+J] == "$Elements") then
            boolElements = true
            -- go to the next line
            J=J+1
         end

      end
   end
   return listPoints, triangulation
end