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

View File

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