terrainlib: compute current queue length instead of using '#' operator.

Important speedup.
This commit is contained in:
Gaël C 2024-01-20 20:02:11 +01:00
parent 0bc100030c
commit 2acefb2660
1 changed files with 12 additions and 6 deletions

View File

@ -134,15 +134,18 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
local queue = {singular[ib]} -- Start with the singular node, then this queue will be filled with water donors neighbours local queue = {singular[ib]} -- Start with the singular node, then this queue will be filled with water donors neighbours
basin_links = {} basin_links = {}
links[#links+1] = basin_links links[#links+1] = basin_links
while #queue > 0 do local cur = 1
local i = tremove(queue) while cur > 0 do
local i = queue[cur]
cur = cur - 1
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
-- Iterate through the 4 directions -- Iterate through the 4 directions
if d >= 8 then -- River coming from the East if d >= 8 then -- River coming from the East
d = d - 8 d = d - 8
queue[#queue+1] = i+1 cur = cur + 1
queue[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)
@ -152,7 +155,8 @@ 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
queue[#queue+1] = i+X cur = cur + 1
queue[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
@ -161,7 +165,8 @@ 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
queue[#queue+1] = i-1 cur = cur + 1
queue[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
@ -169,7 +174,8 @@ local function flow_routing(dem, dirs, lakes) -- 'dirs' and 'lakes' are optional
end end
if d >= 1 then -- River coming from the North if d >= 1 then -- River coming from the North
queue[#queue+1] = i-X cur = cur + 1
queue[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