Recompute the active leaf set by depth-first traversal from the roots. Traversal (rather than a scan of all nodes) is what makes orphaned nodes left behind by coarsening invisible.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(QuadTreeMesh2D), | intent(inout) | :: | this |
subroutine RebuildLeaves_QuadTreeMesh2D(this)
!! Recompute the active leaf set by depth-first traversal from the roots. Traversal (rather
!! than a scan of all nodes) is what makes orphaned nodes left behind by coarsening invisible.
implicit none
class(QuadTreeMesh2D),intent(inout) :: this
! Local
integer :: r,n
! First pass: count leaves.
n = 0
do r = 1,this%nRoots
call count_leaves(r,n)
enddo
this%nLeaves = n
if(allocated(this%leaf)) deallocate(this%leaf)
allocate(this%leaf(1:max(n,1)))
this%leaf = 0
! Second pass: collect leaves in root-major DFS order.
n = 0
do r = 1,this%nRoots
call collect_leaves(r,n)
enddo
contains
recursive subroutine count_leaves(node,cnt)
integer,intent(in) :: node
integer,intent(inout) :: cnt
integer :: k
if(this%child(1,node) == 0) then
cnt = cnt+1
else
do k = 1,4
call count_leaves(this%child(k,node),cnt)
enddo
endif
endsubroutine count_leaves
recursive subroutine collect_leaves(node,idx)
integer,intent(in) :: node
integer,intent(inout) :: idx
integer :: k
if(this%child(1,node) == 0) then
idx = idx+1
this%leaf(idx) = node
else
do k = 1,4
call collect_leaves(this%child(k,node),idx)
enddo
endif
endsubroutine collect_leaves
endsubroutine RebuildLeaves_QuadTreeMesh2D