#! /bin/bash -e
# Last edited on 2025-10-18 12:36:10 by stolfi

# Reads from {stdin} or given file a list of 3d point coordinates and
# displays them as little spheres, with an interactive 3D interface.
# Each point is defined by a line
# 
#   "-point {X} {Y} {X} {RADIUS} {R} {G} {B}"
# 
# where {X,Y,Z} are coordinates of the point, typically in {[-0.5 _ +0.5]};
# {RADIUS} is the radius of the sphere; and {R,G,B} are its color.
# The {R}, {G}, and {B} parts can be omitted.
#
# The input file may also contain the specs of zero or more 
# wireframe tetrahedra or hexahedra.  Each of these is is specified 
# by one of these lines
#
#   "-segment {RE} {GE} {BE}"
#   "-tetra {RE} {GE} {BE}"
#   "-box {RE} {GE} {BE}"
#
# followed by two, four, or eight lines, respectively, each line having
# the three coordinates and the radius "{XV[k]} {YV[k]} {ZV[k]} {RV[k]}"
# of a vertex.  The radius may be zero, in which case a small sphere 
# will be provided to smooth out the joints between the edges.
# The coordinates of the "-box" vertices are assumed to be
# in binary order. The fields {RE,GE,BE} are the colors of the wires
# (edges) and may be omitted.
#
# Ignores blank lines and '#'-comments.

usage="$0 [<] ${data_file}"

if [[ $# -ge 1 ]]; then data_file="$1"; shift; else data_file="-"; fi
if [[ $# -gt 1 ]]; then arg_error "spurious argument \"$1\""; fi

temp="/tmp/$$"

export PATH="${HOME}/lib:${HOME}/lib/platform:${PATH}"

whereis data_to_geomview.py 1>&2

# echo "@----------------------------------------------------------------------" 1>&2
# cat ${data_file} 1>&2
# echo "@----------------------------------------------------------------------" 1>&2

# Create geomview file with points as small spheres:
geom_file="${temp}.off"
echo "data_file = ${data_file}" 1>&2
echo "geom_file = ${geom_file}" 1>&2
data_to_geomview.py ${data_file} ${geom_file}

# echo "@----------------------------------------------------------------------" 1>&2
# cat ${geom_file} 1>&2
# echo "@----------------------------------------------------------------------" 1>&2

# Run geomview:
geomview -b 0.9 0.9 0.9 -c '(bbox-draw allgeoms no)' ${geom_file}

# Cleanup:
rm ${geom_file}

function arg_error() {
  msg="$1"; shift
  echo "** ${msg}" 1>&2; 
  echo "usage: ${usage}"
  exit 1
}
