terrainlib/rivermapper.lua: Move checks out of the nested function

This commit is contained in:
Gaël C 2024-01-21 22:34:53 +01:00
parent fe6e281130
commit c723b28ec6

View File

@ -98,17 +98,8 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
-- i1, i2: coordinates of two nodes -- i1, i2: coordinates of two nodes
-- b1: basin that contains i1 -- b1: basin that contains i1
-- isY: whether the link is in Y direction -- isY: whether the link is in Y direction
local b2
-- Note that basin number #0 represents the outside of the map; or if the coordinate is inside the map, means that the basin number is uninitialized. -- Note that basin number #0 represents the outside of the map; or if the coordinate is inside the map, means that the basin number is uninitialized.
if i2 == 0 then -- If outside the map local b2 = i2 == 0 and 0 or basin_id[i2]
b2 = 0
else
b2 = basin_id[i2]
if b2 == 0 then -- If basin of i2 is not already computed, skip
return
end
end
if b2 ~= b1 then -- If these two nodes don't belong to the same basin, we have found a link between two adjacent basins
local elev = i2 == 0 and dem[i1] or mmax(dem[i1], dem[i2]) -- Elevation of the highest of the two sides of the link (or only i1 if b2 is map outside) local elev = i2 == 0 and dem[i1] or mmax(dem[i1], dem[i2]) -- Elevation of the highest of the two sides of the link (or only i1 if b2 is map outside)
local l2 = basin_links[b2] local l2 = basin_links[b2]
if not l2 then if not l2 then
@ -123,7 +114,6 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
l2[2] = b1 l2[2] = b1
end end
end end
end
for i=1, X*Y do for i=1, X*Y do
basin_id[i] = 0 basin_id[i] = 0
@ -149,7 +139,9 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
singular[cur] = i+1 singular[cur] = i+1
-- If no river is coming from the East, we might be at the limit of two basins, thus we need to test adjacency. -- If no river is coming from the East, we might be at the limit of two basins, thus we need to test adjacency.
elseif i%X > 0 then elseif i%X > 0 then
if basin_id[i+1] ~= ib and basin_id[i+1] ~= 0 then
add_link(i, i+1, ib, false) add_link(i, i+1, ib, false)
end
else -- If the eastern neighbour is outside the map else -- If the eastern neighbour is outside the map
add_link(i, 0, ib, false) add_link(i, 0, ib, false)
end end
@ -159,7 +151,9 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
cur = cur + 1 cur = cur + 1
singular[cur] = i+X singular[cur] = i+X
elseif i <= X*(Y-1) then elseif i <= X*(Y-1) then
if basin_id[i+X] ~= ib and basin_id[i+X] ~= 0 then
add_link(i, i+X, ib, true) add_link(i, i+X, ib, true)
end
else else
add_link(i, 0, ib, true) add_link(i, 0, ib, true)
end end
@ -169,7 +163,9 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
cur = cur + 1 cur = cur + 1
singular[cur] = i-1 singular[cur] = i-1
elseif i%X ~= 1 then elseif i%X ~= 1 then
if basin_id[i-1] ~= ib and basin_id[i-1] ~= 0 then
add_link(i, i-1, ib, false) add_link(i, i-1, ib, false)
end
else else
add_link(i, 0, ib, false) add_link(i, 0, ib, false)
end end
@ -178,7 +174,9 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
cur = cur + 1 cur = cur + 1
singular[cur] = i-X singular[cur] = i-X
elseif i > X then elseif i > X then
if basin_id[i-X] ~= ib and basin_id[i-X] ~= 0 then
add_link(i, i-X, ib, true) add_link(i, i-X, ib, true)
end
else else
add_link(i, 0, ib, true) add_link(i, 0, ib, true)
end end