#include <stdio.h>
#include <stdlib.h>
#include "viewgen.h"
#include "viewpoints.h"

const char* msg1 = "Usage:\nviewgen {-s width height} [level] [3ds file] [imgname] {[outdir]}\n\n"; 
const char* msg2 = "[level] must be at least 1\n"; 

void EndMsg(const char* s)
{
  fprintf(stderr, s);
  exit(3);
}

int main(int argc, char* argv[])
{
  char fstr[255];
  float angle;
  int i; int n;
  char nooutdir[] = ".\0";
  char* outdir;
  bool setslash;
  int shift = 0;
  int width = 300;
  int height = 300;
    
  if(argc < 3) 
      EndMsg(msg1);
  else
  {
      if( (argv[1])[0] == '-' && (argv[1])[1] == 's' )
      {
          if( (width = atoi(argv[2])) < 1 ) EndMsg(msg1);
          if( (height = atoi(argv[3])) < 1 ) EndMsg(msg1);
          shift = 3;
      }
  }  

  width = width/10;
  height = height/10;

  if(argc < 3 + shift) EndMsg(msg1);
  if( (n = atoi(argv[1 + shift])) < 1 ) EndMsg(msg2);
  if( argc > 4 + shift ) outdir = argv[4 + shift]; else outdir = nooutdir;

  COctahedronPoints* pts = new COctahedronPoints(n);
  CViewGenerator* vg = new CViewGenerator(argv[2 + shift]);

  vg->SetSize(width, height);
  vg->SetMagnification(10);
  vg->SetupSilhouetteRendering();
  vg->Initialize();

  for(i=0; i < pts->Size(); i++){
     sprintf(fstr,"%s/%s%04i.tif", outdir, argv[3 + shift], i+1); printf("%s\n", fstr);
     vg->WriteViewToTIFF(pts->Phi(i), pts->Theta(i), fstr);
  }
 
  delete vg;
  delete pts;

  return 0;
}
