#! /bin/bash
# Last edited on 2023-09-25 12:43:15 by stolfi

# For each {SIDE} in "a","b", each image name {IMG} in ${rectdir}/{IMG}{SIDE}.jpg",
# and each key point {TAG} listed in "${refdir}/ref{SIDE}.pts",
# visually checks the correctness of the matching input image point by 
# displaying "${nhoodir}/{IMAGE}{SIDE}-{TAG}.png" magnified.
#
# Optionally the user may specifiy a set of tags and/or a set of images to check.
#
# If a second set of neighborhood images "${nhoodir2}/{IMAGE}{SIDE}-{TAG}.png"
# is given, the two images of each {TAG} are displayed one above the other.

refdir="$1"; shift   # Directory with ref images and ref points "ref{a,b}.pts".
rectdir="$1"; shift  # Directory with rectified images "{img}{side}.jpg".
nhoodir="$1"; shift  # Directory with extracted neighborhood images "{img}{side}-{TAG}.png".
nhoodir2="$1"; shift # Directory with alternative extracted neighborhood images or 'NONE'.
cmtags="$1"; shift   # Point tags to check, separated by commas; or 'ALL'.
images=( "$@" )      # Images to display; omitted to show all.

echo "refdir = ${refdir}" 1>&2
echo "rectdir = ${rectdir}" 1>&2
echo "nhoodir = ${nhoodir}" 1>&2
echo "nhoodir2 = ${nhoodir2}" 1>&2

if [[ "/${cmtags}" == "/ALL" ]]; then
  tags=()
else
  tags=( `echo "${cmtags}" | tr ',' '\012' | sort ` )
  echo "showing only tags ( ${tags[*]} )" 1>&2
fi

if [[ ${#images[@]} -ne 0 ]]; then
  echo "showing only images ( ${images[*]} )" 1>&2
fi

for side in a b ; do
  # Get the tags {stags} that should be available for this side:
  stgfile=".chtags-${side}"
  egrep -e '^[A-Z]' ${refdir}/ref${side}.pts | gawk '//{ print $1; }' | sort > ${stgfile}
  if [[ ${#tags[@]} -eq 0 ]]; then
    stags=( `cat ${stgfile}` )
    if [[ ${#stags[@]} -eq 0 ]]; then 
      echo "** no tags found for side ${side} ?!" 1>&2; exit 1
    fi
  else
    # Restrict to the user-specified tags:
    ctgfile=".tags-cmd"
    echo "${tags[@]}" | tr ',' '\012' | sort > ${ctgfile} 
    stags=( `bool 1.2 ${ctgfile} ${stgfile}` )
    if [[ ${#stags[@]} -eq 0 ]]; then
      echo "!! none of the specified tags apply to side ${side}" 1>&2
    fi
  fi
  echo "stags=( ${stags[*]} )" 1>&2 
  
  # Get the list {simgs} of image names "{IMG}{SIDE}" that have been rectified:
  if [[ ${#images[@]} -eq 0 ]]; then
    # All images that have been rectified:
    simgs=( \
      ` ( shopt -s nullglob; cd ${rectdir} && ls [a-z][a-z][0-9][0-9][0-9]${side}.png ) \
          | sed -e 's:[.].*$::g' \
          | sort \
      ` \
    )
    if [[ ${#simgs[@]} -eq 0 ]]; then echo "!! no images found for side ${side}" 1>&2; fi
  else
    # Filter the user-specified image names for this side:
    simgs=( `echo "${images[@]}" | tr ' ' '\012' | egrep -e "${side}"'$' | sort ` )
    if [[ ${#simgs[@]} -eq 0 ]]; then
      echo "!! none of the specified images are of side ${side}" 1>&2
    fi
  fi
  echo "simgs=( ${simgs[*]} )" 1>&2 
  
  if [[ ( ${#simgs[@]} -gt 0 ) && ( ${#stags[@]} -gt 0 ) ]]; then
    for simg in ${simgs[@]} ; do \
      check_ref_points_visually_img.sh \
        ${simg} ${nhoodir} ${nhoodir2} ${stags[@]}
    done
  fi
done
