terrainlib: loop only once for all singular nodes at step 2 of flow routing

This commit is contained in:
Gaël C 2024-01-21 22:00:23 +01:00
parent 2acefb2660
commit fe6e281130
1 changed files with 13 additions and 13 deletions

View File

@ -129,15 +129,16 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
basin_id[i] = 0
end
for ib=1, nbasins do
-- Here we will recursively search upstream from the singular node to determine its drainage basin
local queue = {singular[ib]} -- Start with the singular node, then this queue will be filled with water donors neighbours
basin_links = {}
links[#links+1] = basin_links
local cur = 1
while cur > 0 do
local i = queue[cur]
local cur = nbasins
local ib = 0
while cur > 0 do
local i = singular[cur]
cur = cur - 1
if dirs[i] == 0 then
basin_links = {}
links[#links+1] = basin_links
ib = ib + 1
end
basin_id[i] = ib
local d = dirs2[i] -- Get the directions water is coming from
@ -145,7 +146,7 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
if d >= 8 then -- River coming from the East
d = d - 8
cur = cur + 1
queue[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.
elseif i%X > 0 then
add_link(i, i+1, ib, false)
@ -156,7 +157,7 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
if d >= 4 then -- River coming from the South
d = d - 4
cur = cur + 1
queue[cur] = i+X
singular[cur] = i+X
elseif i <= X*(Y-1) then
add_link(i, i+X, ib, true)
else
@ -166,7 +167,7 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
if d >= 2 then -- River coming from the West
d = d - 2
cur = cur + 1
queue[cur] = i-1
singular[cur] = i-1
elseif i%X ~= 1 then
add_link(i, i-1, ib, false)
else
@ -175,13 +176,12 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
if d >= 1 then -- River coming from the North
cur = cur + 1
queue[cur] = i-X
singular[cur] = i-X
elseif i > X then
add_link(i, i-X, ib, true)
else
add_link(i, 0, ib, true)
end
end
end
dirs2 = nil