
/* ptomap.c  (C) R.E.Wolff@BitWizard.nl
 * This is a quick hack. It worked for me, it probably doens't
 * work without modification on any other project. Beware. 
 * It is mostly intended as a demonstration of WHAT information
 * is useful for some users. The intent is that hugin can be 
 * modified to provide this information in the future. 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct image {
  double pitch, roll, yaw, fov;
};

struct image images[256];
int n=0;
int pano_width, pano_height;
double pano_vfov, pano_hfov;
double hsize;


int istag (char c)
{
  if ((c >= 'a') && (c <= 'z')) return 1;
  if ((c >= 'A') && (c <= 'Z')) return 1;
  return 0;
}


int parse_tv (char *p, char *tag, double *value)
{
  while (istag (*p)) {
    *tag++ = *p++;
  }
  *tag++ = 0;

  if (*p == '=') {
    p++;
    sscanf (p, "%lf", value);
    return 1;
  }

  sscanf (p, "%lf", value);
  return 0;
}



int main (int argc, char **argv)
{
  char buf[1024];
  char *p;
  char tag[10];
  double value; 
  int i, j, c;
  struct image *ii, *jj;

  while (fgets (buf, 1024, stdin)) {
    switch (buf[0]) {
    case '#':
    case '\n': 
    case ' ':
      continue;
      break;
    case 'p': 
      strtok (buf, " ");
      while ( (p = strtok (NULL, " "))) {
	parse_tv (p, tag, &value);
	switch (tag[0]) {
	case 'w': pano_width=value;break; 
	case 'h': pano_height=value;break; 
	case 'v': pano_hfov=value;break; 
	default: 
	  //printf ("parse-p: tag=%s, value=%lf\n", tag, value);
	  break;
	}
      }
      break;
    case 'i':
      strtok (buf, " ");
      while ( (p = strtok (NULL, " "))) {
	c = parse_tv (p, tag, &value);
	switch (tag[0]) {
	case 'p': images[n].pitch = value;break;
	case 'r': images[n].roll  = value;break;
	case 'y': images[n].yaw   = value;break;
	case 'v': 
	  if (c) 
	    images[n].fov = images[(int)value].fov;
	  else
	    images[n].fov = value;
	  break;

	default: 
	  //printf ("parse-i%d: tag=%s, value=%lf\n", n, tag, value);
	  break;
	}
      }
      n++;
      break;
    }
  }
  pano_vfov = pano_hfov * pano_height / pano_width;
  printf  ("%%Pano = %lf x %lf degrees.\n", pano_hfov, pano_vfov);

  printf ("300 400 translate 90 rotate\n");

  hsize = 550;
  printf ("%lf %lf scale\n", hsize/pano_hfov, hsize/pano_hfov);
  printf ("%lf setlinewidth\n", pano_hfov / hsize); 
  printf  ("(Times-Roman) findfont %lf scalefont setfont\n", 
	   10 / hsize * pano_hfov);

  for (i=0;i<n;i++) {
    ii = &images[i];
    printf ("%lf %lf moveto (%d) show\n", images[i].yaw, images[i].pitch, i);
    
    printf ("%lf %lf moveto %lf %lf lineto  %lf %lf lineto  %lf %lf lineto  %lf %lf lineto stroke\n", 
	    ii->yaw - ii->fov/2, ii->pitch - ii->fov/3,
	    ii->yaw - ii->fov/2, ii->pitch + ii->fov/3,
	    ii->yaw + ii->fov/2, ii->pitch + ii->fov/3,
	    ii->yaw + ii->fov/2, ii->pitch - ii->fov/3,
	    ii->yaw - ii->fov/2, ii->pitch - ii->fov/3);
	    

  }

  printf ("showpage\n");

  for (i=0;i<n;i++) {
    ii = &images[i];
    printf ("%%image %d overlaps with:", i);
    for (j=0;j<n;j++) {
      if (j == i) continue;
      jj = &images[j];
      if ((abs (ii->yaw   - jj->yaw  ) < ii->fov) &&
	  (abs (ii->pitch - jj->pitch) < ii->fov*2/3))
	printf (" %d", j);
    }
    printf ("\n");
  }
  exit (0);
}

// This crosses a full page in postscript. 
// 0 0 moveto 610 790 lineto stroke
