Rectification des fichiers et de la version pour le CTAN
[delaunay.git] / luamesh.lua
index 402e33d..a1023aa 100644 (file)
@@ -1,11 +1,19 @@
 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 = {}
-   local lgth = #listPoints
+   lgth = #listPoints
    -- add four points to listPoints to have a bounding box
    listPoints = buildBoundingBox(listPoints)
    -- the first triangle
@@ -15,7 +23,7 @@ function BowyerWatson (listPoints,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)
+      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
@@ -129,7 +137,7 @@ function removeBoundingBox(triangulation,lgth)
 end
 
 
-function buildBadTriangles(point, triangulation)
+function buildBadTriangles(point, triangulation,listPoints)
    local badTriangles = {}
    for j=1,#triangulation do -- for all triangles
       A = listPoints[triangulation[j][1]]
@@ -316,7 +324,6 @@ function buildVoronoi(listPoints, triangulation)
    return listVoronoi
 end
 
--------------------------- TeX
 -- 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)
@@ -345,26 +352,58 @@ function buildList(chaine, mode)
 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)
+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)
+   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)
+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
-         listPoints[k] = {x = xmin+(i-1)*h, y=ymin+(j-1)*h}
+         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
@@ -373,13 +412,13 @@ end
 
 
 -- function to add points from a grid to the interior of a polygon
-function addGridPoints(polygon, grid)
-   local listPoints = 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])) then
+      if(isInside(polygon,grid[i],h)) then
          k=k+1
          listPoints[k] = grid[i]
       end
@@ -388,18 +427,11 @@ function addGridPoints(polygon, grid)
 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 = {}
-   polyCopy = shallowCopy(polygon)
+   local polyNew = {}
+   local polyCopy = shallowCopy(polygon)
    e1 = polyCopy[1][1]
    e2 = polyCopy[1][2]
    table.insert(polyNew, e1)
@@ -435,7 +467,7 @@ end
 
 -- build the list of points extern and stop at nbr
 function buildListExt(chaine, stop)
-   listPoints = {}
+   local listPoints = {}
    io.input(chaine) -- open the file
    text=io.read("*all")
    lines=string.explode(text,"\n+") -- all the lines
@@ -471,8 +503,8 @@ function readGmsh(file)
    io.input(file) -- open the file
    text=io.read("*all")
    local lines = split(text,"\n+") -- all the lines
-   listPoints={}
-   triangulation ={}
+   local listPoints={}
+   local triangulation ={}
    boolNodes = false
    Jnodes = 0
    boolElements = false

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.