build_chains Subroutine

public subroutine build_chains(cs, se0, se1, sy0, sy1, ns)

Link outline segments into polyline chains by walking shared endpoints.

Endpoints match by exact equality (see symdiff_append); where more than two segments meet, any continuation is picked, each segment drawn exactly once either way. Reads nmodels from kipp_state.f90.

Arguments

Type IntentOptional Attributes Name
type(chainset_t), intent(inout) :: cs

Receives chains.

integer(kind=int32), intent(in) :: se0(:)

Strip edge of each segment's first and second end.

integer(kind=int32), intent(in) :: se1(:)

Strip edge of each segment's first and second end.

real(kind=real64), intent(in) :: sy0(:)

y of each segment's first and second end.

real(kind=real64), intent(in) :: sy1(:)

y of each segment's first and second end.

integer(kind=int32), intent(in) :: ns

Number of segments.


Source Code

   subroutine build_chains(cs, se0, se1, sy0, sy1, ns)
      !! Link outline segments into polyline chains by walking shared endpoints.
      !!
      !! Endpoints match by exact equality (see `symdiff_append`); where more than two segments
      !! meet, any continuation is picked, each segment drawn exactly once either way. Reads
      !! `nmodels` from kipp_state.f90.
      ! Arguments
      type(chainset_t), intent(inout) :: cs
         !! Receives chains.
      integer(int32), intent(in) :: ns
         !! Number of segments.
      integer(int32), intent(in) :: se0(:), se1(:)
         !! Strip edge of each segment's first and second end.
      real(real64), intent(in) :: sy0(:), sy1(:)
         !! y of each segment's first and second end.
      logical, allocatable :: used(:)
      integer(int32), allocatable :: cnt(:), off(:), ids(:), fil(:)
      integer(int32), allocatable :: cei(:)
      real(real64), allocatable :: cy(:)
      integer(int32) :: nedge, s, e, k, cn, tmpe
      real(real64) :: tmpy

      if (ns == 0) return
      ! bucket the segments by the strip edges they touch (CSR layout)
      nedge = nmodels + 1
      allocate (used(ns)); used = .false.
      allocate (cnt(nedge), off(nedge + 1), fil(nedge))
      cnt = 0
      do s = 1, ns
         cnt(se0(s)) = cnt(se0(s)) + 1
         if (se1(s) /= se0(s)) cnt(se1(s)) = cnt(se1(s)) + 1
      end do
      off(1) = 1
      do e = 1, nedge
         off(e + 1) = off(e) + cnt(e)
      end do
      allocate (ids(off(nedge + 1) - 1))
      fil = off(1:nedge)
      do s = 1, ns
         ids(fil(se0(s))) = s; fil(se0(s)) = fil(se0(s)) + 1
         if (se1(s) /= se0(s)) then
            ids(fil(se1(s))) = s; fil(se1(s)) = fil(se1(s)) + 1
         end if
      end do

      do s = 1, ns
         if (used(s)) cycle
         used(s) = .true.
         cn = 0
         call chain_push(cei, cy, cn, se0(s), sy0(s))
         call chain_push(cei, cy, cn, se1(s), sy1(s))
         call chain_extend()
         ! reverse and extend from the other end too
         do k = 1, cn/2
            tmpe = cei(k); cei(k) = cei(cn + 1 - k); cei(cn + 1 - k) = tmpe
            tmpy = cy(k); cy(k) = cy(cn + 1 - k); cy(cn + 1 - k) = tmpy
         end do
         call chain_extend()
         call chainset_append(cs, cei, cy, cn)
      end do

   contains

      subroutine chain_extend()
         !! Extend chain from its tail while an unused segment connects.
         integer(int32) :: et, kk, s2
         real(real64) :: yt
         logical :: found
         do
            et = cei(cn); yt = cy(cn)
            found = .false.
            do kk = off(et), off(et + 1) - 1
               s2 = ids(kk)
               if (used(s2)) cycle
               if (se0(s2) == et .and. sy0(s2) == yt) then
                  used(s2) = .true.
                  call chain_push(cei, cy, cn, se1(s2), sy1(s2))
                  found = .true.
                  exit
               else if (se1(s2) == et .and. sy1(s2) == yt) then
                  used(s2) = .true.
                  call chain_push(cei, cy, cn, se0(s2), sy0(s2))
                  found = .true.
                  exit
               end if
            end do
            if (.not. found) return
         end do
      end subroutine chain_extend
   end subroutine build_chains