// File         : main.cxx
// Author       : Paul A. Koshevoy
// Created      : Fri Apr 23 19:01:50 MDT 2004
// Copyright    : (C) 2004
// License      : GPL.
// Description  : 

// system includes:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>

// local includes:
#include "the_ltv.hxx"

using std::cout;
using std::cin;
using std::endl;
using std::setw;


//----------------------------------------------------------------
// main
// 
int
main(int argc, char ** argv)
{
  for (int i = 1; i < argc; i++)
  {
    if (strcmp(argv[i], "-h") == 0 ||
	strcmp(argv[i], "-help") == 0 ||
	strcmp(argv[i], "--help") == 0)
    {
      cout << "USAGE: " << argv[0]
	   << " a b diagonal gamma_1 {n | alpha_1 alpha | r1 omega_1}" << endl
	   << endl
	   << "a:        Numerator of the screen aspect ration (4 in 4/3)"
	   << endl
	   << "b:        Denominator of the screen aspect ration (3 in 4/3)"
	   << endl
	   << "diagonal: Screen diagonal size"
	   << endl
	   << "gamma_1:  Minimal angle of incidence at the center of the\n"
	   << "          reflection axis of the second mirror (in degrees)"
	   << endl
	   << "n:        Number of points per grid edge"
	   << endl
	   << "alpha_1:  Angle between the Y axis and the reflected ray"
	   << endl
	   << "alpha_2:  Current angle of incidence at the center of the\n"
	   << "          reflection axis of the second mirror (in degrees)"
	   << endl;
      exit(0);
    }
  }
  
  real_t aspect_ratio = 4.0 / 3.0;
  real_t diagonal = 20.0;
  real_t gamma_1 = 10.0 * M_PI / 180.0;
  
  if (argc > 5)
  {
    unsigned int a = strtoul(argv[1], NULL, 10);
    unsigned int b = strtoul(argv[2], NULL, 10);
    aspect_ratio = real_t(a) / real_t(b);
    
    diagonal = strtod(argv[3], NULL);
    gamma_1 = strtod(argv[4], NULL) * M_PI / 180.0;
  }
  
  the_fast_ltv_t ltv_fast(aspect_ratio, diagonal, gamma_1);
  the_slow_ltv_t ltv_slow(aspect_ratio, diagonal, gamma_1);
  
  cerr << "width:     " << ltv_fast.width() << endl
       << "height:    " << ltv_fast.height() << endl
       << "m2 length: " << ltv_fast.m2_length() << endl
       << "depth:     " << ltv_fast.d1() << endl << endl;
  
  if (argc == 8)
  {
    real_t a1 = strtod(argv[7], NULL) * M_PI / 180.0;
    real_t a2 = strtod(argv[8], NULL) * M_PI / 180.0;
    
    real_t u_slow = THE_REAL_MAX;
    real_t v_slow = THE_REAL_MAX;
    bool slow = ltv_slow.alpha_to_uv(a1, a2, u_slow, v_slow);
    
    real_t u_fast = THE_REAL_MAX;
    real_t v_fast = THE_REAL_MAX;
    bool fast = ltv_fast.alpha_to_uv(a1, a2, u_fast, v_fast);
    
    cout << "slow: " << slow << ", uv = "
	 << setw(13) << u_slow << ' '
	 << setw(13) << v_slow << endl
	 << "fast: " << fast << ", uv = "
	 << setw(13) << u_fast << ' '
	 << setw(13) << v_fast << endl;
    
    exit(0);
  }
  
  unsigned int n = 60;
  if (argc == 6) n = strtoul(argv[5], NULL, 10);
  
  for (unsigned int i = 0; i < n; i++)
  {
    real_t a1 = 2.0 * M_PI * real_t(i) / real_t(n);
    
    for (unsigned int j = 0; j < n; j++)
    {
      real_t a2 = 2.0 * M_PI * real_t(j) / real_t(n);
      
      real_t u_fast = 0.0;
      real_t v_fast = 0.0;
      bool fast = ltv_fast.alpha_to_uv(a1, a2, u_fast, v_fast);
      
      /*
      real_t u_slow = 0.0;
      real_t v_slow = 0.0;
      bool slow = ltv_slow.alpha_to_uv(a1, a2, u_slow, v_slow);
      
      if (slow != fast)
      {
	cerr << slow << fast
	     << " got different answers for a1 = " << a1 * 180.0 / M_PI
	     << ", a2 = " << a2 * 180.0 / M_PI << endl;
      }
      */
      if (fast)
      {
	cout << a1 << ' ' << a2 << ' ' << 1.0 << endl;
      }
      else
      {
	cout << a1 << ' ' << a2 << ' ' << 0.0 << endl;
      }
    }
    cout << endl;
  }
  
  return 0;
}

