/* This file is part of the Neper software package. */
/* Copyright (C) 2003-2014, Romain Quey. */
/* See the COPYING file in the top-level directory. */

#include"net_input_.h"

/* SetDefaultOptions set the options to their default values */
void
net_input_options_default (struct IN *pIn)
{
  (*pIn).weightstring = ut_alloc_1d_char (10);
  strcpy ((*pIn).weightstring, "none");
  (*pIn).morphostring = ut_alloc_1d_char (10);
  strcpy ((*pIn).morphostring, "poisson");

  (*pIn).domain = ut_alloc_1d_char (10);
  strcpy ((*pIn).domain, "default");
  (*pIn).scalestring = NULL;
  (*pIn).scale = NULL;
  (*pIn).crop = NULL;

  (*pIn).tesrsizestring = ut_alloc_1d_char (3);
  strcpy ((*pIn).tesrsizestring, "20");
  (*pIn).filter = NULL;
  (*pIn).body = NULL;

  (*pIn).nstring = NULL;
  (*pIn).n = NULL;
  (*pIn).levelqty = 1;
  (*pIn).idstring = NULL;
  (*pIn).id = NULL;
  (*pIn).ttype = ut_alloc_1d_char (9);
  strcpy ((*pIn).ttype, "standard");
  (*pIn).dim = 3;
  (*pIn).randomizestring = NULL;

  ut_string_string ("rel", &((*pIn).seltype));
  ut_string_string ("1", &((*pIn).selstring));

  (*pIn).mode = NULL;
  (*pIn).input = NULL;
  (*pIn).tess = NULL;
  (*pIn).format = NULL;
  (*pIn).tesrformat = NULL;
  (*pIn).load = NULL;
  (*pIn).loadpoint = NULL;

  ut_string_string ("3D", &((*pIn).oridistrib));
  (*pIn).orides = ut_alloc_1d_char (100);
  strcpy ((*pIn).orides, "e");
  (*pIn).oricrysym = ut_alloc_1d_char (100);
  strcpy ((*pIn).oricrysym, "triclinic");
  (*pIn).oriformat = ut_alloc_1d_char (100);
  strcpy ((*pIn).oriformat, "plain");

  (*pIn).stc = NULL;
  (*pIn).stv = NULL;
  (*pIn).ste = NULL;
  (*pIn).stf = NULL;
  (*pIn).stp = NULL;
  (*pIn).stg = NULL;
  (*pIn).stpt = NULL;

  (*pIn).sortstring = NULL;

  (*pIn).centroidstring = NULL;
  (*pIn).centroid = NULL;
  (*pIn).centroiditermax = 1000;
  (*pIn).centroidconv = 0.02;
  (*pIn).centroidfact = 0.5;

  (*pIn).hardcorestring = NULL;
  (*pIn).hardcore = NULL;

  (*pIn).reg = 0;
  (*pIn).mloop = 2;
  (*pIn).maxedgedelqty = INT_MAX;
  (*pIn).fmax = 20;

  return;
}

void
net_input_options_set (struct IN *pIn, int argc, char **argv)
{
  int i, ArgQty, Res;
  char **ArgList = ut_alloc_2d_char (101, 101);
  char *Arg = NULL;

  // Argument list =====================================================

  ArgQty = 0;
  // Input data --------------------------------------------------------
  strcpy (ArgList[++ArgQty], "-n");
  strcpy (ArgList[++ArgQty], "-id");
  strcpy (ArgList[++ArgQty], "-morpho");
  strcpy (ArgList[++ArgQty], "-weight");
  strcpy (ArgList[++ArgQty], "-dim");
  strcpy (ArgList[++ArgQty], "-ttype");
  strcpy (ArgList[++ArgQty], "-centroid");
  strcpy (ArgList[++ArgQty], "-hardcore");
  // General options ---------------------------------------------------
  strcpy (ArgList[++ArgQty], "-o");
  strcpy (ArgList[++ArgQty], "-v");
  strcpy (ArgList[++ArgQty], "-libpath");
  // Tessellation options ----------------------------------------------
  strcpy (ArgList[++ArgQty], "-domain");
  strcpy (ArgList[++ArgQty], "-domainfacet");
  strcpy (ArgList[++ArgQty], "-scale");
  strcpy (ArgList[++ArgQty], "-crop");
  strcpy (ArgList[++ArgQty], "-randomize");
  strcpy (ArgList[++ArgQty], "-centroidfact");
  strcpy (ArgList[++ArgQty], "-centroidconv");
  strcpy (ArgList[++ArgQty], "-centroiditermax");
  // Regularization options --------------------------------------------
  strcpy (ArgList[++ArgQty], "-regularization");
  strcpy (ArgList[++ArgQty], "-fmax");
  strcpy (ArgList[++ArgQty], "-sel");
  strcpy (ArgList[++ArgQty], "-rsel");
  strcpy (ArgList[++ArgQty], "-mloop");
  strcpy (ArgList[++ArgQty], "-maxedgedelqty");
  // Filtering options -------------------------------------------------
  strcpy (ArgList[++ArgQty], "-filter");
  // Output options ----------------------------------------------------
  strcpy (ArgList[++ArgQty], "-format");
  strcpy (ArgList[++ArgQty], "-tesrformat");
  strcpy (ArgList[++ArgQty], "-tesrsize");
  strcpy (ArgList[++ArgQty], "-ori");
  strcpy (ArgList[++ArgQty], "-oridescriptor");
  strcpy (ArgList[++ArgQty], "-oricrysym");
  strcpy (ArgList[++ArgQty], "-oriformat");
  // Post-processing ---------------------------------------------------
  strcpy (ArgList[++ArgQty], "-statver");
  strcpy (ArgList[++ArgQty], "-statedge");
  strcpy (ArgList[++ArgQty], "-statface");
  strcpy (ArgList[++ArgQty], "-statpoly");
  strcpy (ArgList[++ArgQty], "-statcell");
  strcpy (ArgList[++ArgQty], "-statseed");
  strcpy (ArgList[++ArgQty], "-statpoint");
  strcpy (ArgList[++ArgQty], "-sort");
  // Restart a job -----------------------------------------------------
  strcpy (ArgList[++ArgQty], "-loadtess");
  strcpy (ArgList[++ArgQty], "-loadtesr");
  strcpy (ArgList[++ArgQty], "-loadpoint");
  // Debugging ---------------------------------------------------------
  strcpy (ArgList[++ArgQty], "-checktess");

  // Treating arguments ================================================

  for (i = 1; i < argc; i++)
  {
    if (argv[i][0] != '-')
      ut_arg_badarg ();

    if (i == argc - 1)
      ut_arg_badarg ();

    /* Searching option name (string completion stuff) */
    Res = ut_string_comp (argv[i], ArgList, ArgQty, &Arg);
    if (Res == 1)
    {
      ut_print_lineheader (2);
      printf ("Several possibilities for option `%s'.\n", argv[i]);
      ut_arg_badarg ();
    }
    else if (Res == -1)
    {
      ut_print_lineheader (2);
      printf ("Unknown option `%s'.\n", argv[i]);
      ut_arg_badarg ();
    }

/*---------------------------------------------------------------------- 
* input data */
    if (!strcmp (Arg, "-n") && i < argc - 1)
    {
      if ((*pIn).input == NULL)
      {
	(*pIn).input = ut_alloc_1d_char (5);
	strcpy ((*pIn).input, "n");
	ut_arg_nextasstring (argv, &i, Arg, &((*pIn).nstring));

	if ((strncmp ((*pIn).morphostring, "cube", 4) == 0)
	    || (strncmp ((*pIn).morphostring, "dodeca", 6) == 0)
	    || (strncmp ((*pIn).morphostring, "tocta", 5) == 0)
	    || (strncmp ((*pIn).morphostring, "square", 6) == 0))
	  strcpy ((*pIn).input, "n_reg");
      }
      else
	ut_arg_badarg ();
    }
    else if (!strcmp (Arg, "-id") && i < argc - 1)
    {
      /*
         if ((*pIn).id == 0)
         {
         ut_print_message (2, 0,
         "Functions `-id' and `-morpho' are mutually exclusive!");
         abort ();
         }
       */
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).idstring));
    }
    else if (!strcmp (Arg, "-morpho"))
    {
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).morphostring));

      if ((*pIn).n != NULL)
	if ((!strncmp ((*pIn).morphostring, "cube", 4))
	    || (!strncmp ((*pIn).morphostring, "dodeca", 6))
	    || (!strncmp ((*pIn).morphostring, "tocta", 5))
	    || (!strncmp ((*pIn).morphostring, "square", 6)))
	{
	  (*pIn).input = ut_alloc_1d_char (6);
	  strcpy ((*pIn).input, "n_reg");
	}
	if (!strncmp ((*pIn).morphostring, "square", 6))
	  (*pIn).dim = 2;
    }
    else if (!strcmp (Arg, "-weight"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).weightstring));
    else if (!strcmp (Arg, "-dim"))
      ut_arg_nextasint (argv, &i, Arg, 1, 3, &((*pIn).dim));
    else if (!strcmp (Arg, "-ttype"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).ttype));

/*---------------------------------------------------------------------- 
* general options */
    else if (!strcmp (Arg, "-o"))
    {
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).body));
      ut_string_body ((*pIn).body, ".", &((*pIn).body));
    }

/*---------------------------------------------------------------------- 
* tessellation options */
    else if (!strcmp (Arg, "-domain"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).domain));
    else if (!strcmp (Arg, "-scale"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).scalestring));
    else if (!strcmp (Arg, "-crop"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).crop));
    else if (!strcmp (Arg, "-randomize"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).randomizestring));
    else if (!strcmp (Arg, "-centroid"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).centroidstring));
    else if (!strcmp (Arg, "-centroidfact"))
      ut_arg_nextasreal (argv, &i, Arg, 0, 1, &((*pIn).centroidfact));
    else if (!strcmp (Arg, "-centroidconv"))
      ut_arg_nextasreal (argv, &i, Arg, 0, DBL_MAX, &((*pIn).centroidconv));
    else if (!strcmp (Arg, "-centroiditermax"))
      ut_arg_nextasint (argv, &i, Arg, 1, INT_MAX, &((*pIn).centroiditermax));
    else if (!strcmp (Arg, "-hardcore"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).hardcorestring));

/*---------------------------------------------------------------------- 
* regularization options */
    else if (!strcmp (Arg, "-regularization"))
      ut_arg_nextasint (argv, &i, Arg, 0, 1, &(*pIn).reg);
    else if (!strcmp (Arg, "-fmax"))
      ut_arg_nextasreal (argv, &i, Arg, 0, 180, &(*pIn).fmax);
    else if (!strcmp (Arg, "-sel"))
    {
      strcpy ((*pIn).seltype, "abs");
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).selstring));
    }
    else if (!strcmp (Arg, "-rsel"))
    {
      strcpy ((*pIn).seltype, "rel");
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).selstring));
    }
    else if (!strcmp (Arg, "-mloop"))
      ut_arg_nextasint (argv, &i, Arg, 0, INT_MAX, &((*pIn).mloop));
    else if (!strcmp (Arg, "-maxedgedelqty"))
      ut_arg_nextasint (argv, &i, Arg, 0, INT_MAX, &((*pIn).maxedgedelqty));

/*---------------------------------------------------------------------- 
* filtering options */
    else if (!strcmp (Arg, "-filter"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).filter));

/*----------------------------------------------------------------------
* post-processing options */
    else if (!strcmp (Arg, "-format"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).format));
    else if (!strcmp (Arg, "-tesrformat"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).tesrformat));
    else if (!strcmp (Arg, "-tesrsize"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).tesrsizestring));
    else if (!strcmp (Arg, "-ori"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).oridistrib));
    else if (!strcmp (Arg, "-oridescriptor"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).orides));
    else if (!strcmp (Arg, "-oricrysym"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).oricrysym));
    else if (!strcmp (Arg, "-oriformat"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).oriformat));
    else if (!strcmp (Arg, "-statcell"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).stc));
    else if (!strcmp (Arg, "-statver"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).stv));
    else if (!strcmp (Arg, "-statedge"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).ste));
    else if (!strcmp (Arg, "-statface"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).stf));
    else if (!strcmp (Arg, "-statpoly"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).stp));
    else if (!strcmp (Arg, "-statseed"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).stg));
    else if (!strcmp (Arg, "-statpoint"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).stpt));
    else if (!strcmp (Arg, "-sort"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).sortstring));

/*---------------------------------------------------------------------- 
* load capability */
    else if ((!strcmp (Arg, "-loadtess"))
	     || (!strcmp (Arg, "-checktess")))
    {
      ut_string_string ("tess", &(*pIn).input);
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).load));

      if (!strcmp (Arg, "-checktess"))
	(*pIn).checktess = 1;
    }
    else if (!strcmp (Arg, "-loadtesr"))
    {
      ut_string_string ("tesr", &(*pIn).input);
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).load));
    }
    else if (!strcmp (Arg, "-loadpoint"))
      ut_arg_nextasstring (argv, &i, Arg, &((*pIn).loadpoint));

    else
      ut_arg_badarg ();
  }

  ut_free_1d_char (Arg);
  ut_free_2d_char (ArgList, 101);

  return;
}
