#! /bin/bash
# Last edited on 2021-03-08 11:41:48 by jstolfi

# Reads a data file as produced by {analyze_egg_shape.py}, plots
# outlines original and fitted. Optionally shows the radii of the
# osculating circles or of the incircles.

cmd="$0"; cmd="${cmd##*/}"

imname="$1"; shift  # Image name without directory, tag, extension.
rsmooth="$1"; shift # Smoothing window radius: "0300", "0600", etc.
kosc="$1"; shift    # Show osculating circle radii every this many points, or 0 to omit.
kinc="$1"; shift    # Show incircle radii every this many points, or 0 to omit.
show=$1; shift      # Display plot if 1, silent if 0

# END COMMAND LINE PARSING
# ----------------------------------------------------------------------

if [[ ( ${kosc} -ne 0 ) && ( ${kinc} -ne 0 ) ]]; then
  echo "only one of {kosc} or {kinc} should be nonzero." 1>&2; exit 1
fi

exfile="out/${imname}-pts.txt"               # File name with raw outlines.
anfile="out/${imname}-sm${rsmooth}-fit.txt"  # File name with fitted outlines.

ptag="contours"

# Prefix for temporary file names
tmp="/tmp/$$"

hPix=1200
vPix=1200

gfile="${tmp}.gpl"
tfile="${tmp}.png"

# Create the plot command:
ostyle="linespoints lt 1 lw 1 pt 6 ps 0.75"
vstyle="vectors nohead lt 1 lw 1"
printf "plot %s\n" "\\" > ${gfile}
printf "  \"${exfile}\" using 2:3 title \"raw\" with ${ostyle}  lc rgb '#0022cc' , %s\n" "\\" >> ${gfile}
printf "  \"${anfile}\" using 4:5 title \"fit\" with ${ostyle} lc rgb '#ff2200'" >> ${gfile}
if [[ ${kosc} -ne 0  ]]; then
  ptag=`printf "${ptag}-kosc%03d" "${kosc}"`
  printf ", %s\n" "\\" >> ${gfile}
  printf "  \"\" using 4:5:(sel(${kosc})*oscdx(0)):(oscdy(0)) notitle with ${vstyle} lc rgb '#ff2200'" >> ${gfile}
  printf ", %s\n" "\\" >> ${gfile}
  printf "  \"\" using (oscctrx(0)):(oscctry(0)) title \"inv\" with ${ostyle} lc rgb '#008800'" >> ${gfile}

fi
if [[ ${kinc} -ne 0  ]]; then
  ptag=`printf "${ptag}-kincc%03d" "${kinc}"`
  printf ", %s\n" "\\" >> ${gfile}
  printf "  \"\" using 4:5:(sel(${kinc})*incdx(0)):(incdy(0)) notitle with ${vstyle} lc rgb '#ff2200'" >> ${gfile}
  printf ", %s\n" "\\" >> ${gfile}
  printf "  \"\" using (incctrx(0)):(incctry(0)) title \"inv\" with ${ostyle} lc rgb '#008800'" >> ${gfile}

fi
printf "\n" >> ${gfile}

export GDFONTPATH="tt-fonts"

gnuplot <<EOF
  set terminal png truecolor size ${hPix},${vPix} font "arial,18"
  set size ratio -1
  set output "${tfile}"
  set nokey
  
  # set xrange[362:372]
  # set yrange[521:531]
  
  set xlabel "X (pixels)"
  set ylabel "Y (pixels)"
  
  sel(k) = ((int(column(0)) % k) == 0 ? 1 : 0/0)
  
  # Osculating circle parameters:
  oscrad(dum) = 1/(column(8))
  oscdx(dum) = -oscrad(dum)*(column(7))
  oscdy(dum) = +oscrad(dum)*(column(6))
  oscctrx(dum) = (column(4)) + oscdx(dum)
  oscctry(dum) = (column(5)) + oscdy(dum)

  # Incircle parameters:
  incctrx(dum) = (column(10))
  incctry(dum) = (column(11))
  incdx(dum) = incctrx(dum) - (column(4))
  incdy(dum) = incctry(dum) - (column(5))
  incrad(dum) = hypot(incdx(dum),incdy(dum))

  load "${gfile}"

EOF

if [[ -s ${tfile} ]]; then
  pfile="out/${imname}-sm${rsmooth}-${ptag}.png"
  convert ${tfile} -resize '50%' ${pfile}
  if [[ ${show} -ne 0 ]]; then display ${pfile}; fi
  rm ${tfile}
else
  echo "** plot failed" 1>&2 ; exit 1
fi
