/* 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"

void
net_input (struct IN *pIn, int fargc, char **fargv, int argc, char **argv)
{
  int i, tess_out, tesr_out, ori_out, qty;
  char** val = NULL;

  net_input_treatargs (fargc, fargv, argc, argv, pIn);

  if ((*pIn).scalestring)
  {
    (*pIn).scale = ut_alloc_1d (3);
    ut_array_1d_set ((*pIn).scale, 3, 1);
    ut_string_separate ((*pIn).scalestring, NEUT_SEP_DEP, &val, &qty);
    for (i = 0; i < qty; i++)
      if (strlen (val[0]) > 0)
	ut_string_real (val[i], &((*pIn).scale[i]));
  }

  if (!(*pIn).format)
  {
    (*pIn).format = ut_alloc_1d_char (5);

    if ((*pIn).load)
      strcpy ((*pIn).format, (*pIn).input);
    else
      strcpy ((*pIn).format, "tess");
  }

  tess_out = 0;
  tesr_out = 0;
  ori_out = 0;
  if (ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "tess") == 1
      || ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "mtess") == 1
      || ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "geo") == 1
      || ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "mgeo") == 1
      || ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "3dec") == 1
      || ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "ply") == 1
      || ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "obj") == 1)
    tess_out = 1;
  if (ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "tesr") == 1
      || ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "raw") == 1)
    tesr_out = 1;
  if (tess_out == 0 && tesr_out == 0
      && ut_string_inlist ((*pIn).format, NEUT_SEP_NODEP, "ori") == 1)
    ori_out = 1;

  // Setting mode
  (*pIn).mode = ut_alloc_1d_char (30);

  if (tess_out == 1)
    strcpy ((*pIn).mode, "tess");
  else if (tesr_out == 1)
  {
    // centroid and reg can be done only on tess
    if ((*pIn).centroidstring || (*pIn).reg)
      strcpy ((*pIn).mode, "tess");
    else
      strcpy ((*pIn).mode, "tesr");
  }
  else if (ori_out == 1)
    strcpy ((*pIn).mode, "ori");
  else if ((*pIn).load)
    strcpy ((*pIn).mode, (*pIn).format);

  if (!strcmp ((*pIn).mode, "tess"))
  {
    if (!strcmp ((*pIn).input, "tesr"))
    {
      ut_print_message (2, 0,
			"tess-type output not available with tesr input.");
      abort ();
    }
    else if (strcmp ((*pIn).ttype, "standard") != 0)
    {
      ut_print_message (2, 2,
			"tess-type output not available with ttype not `standard'.\n");
      abort ();
    }
    else if (!strcmp ((*pIn).input, "n_reg")
	     && strcmp ((*pIn).morpho[1], "tocta") != 0)
    {
      ut_print_message (2, 0,
			"tess-type output not available for morpho=%s\n",
			(*pIn).morpho[1]);
      abort ();
    }
  }

  ut_free_2d_char (val, qty);

  return;
}

void
net_in_set_zero (struct IN *pIn)
{
  (*pIn).mode = NULL;
  (*pIn).nstring = NULL;
  (*pIn).n = NULL;
  (*pIn).idstring = NULL;
  (*pIn).id = NULL;
  (*pIn).morphostring = NULL;
  (*pIn).morpho = NULL;
  (*pIn).weightstring = NULL;
  (*pIn).weight = NULL;
  (*pIn).ttype = NULL;
  (*pIn).reg = 0;
  (*pIn).fmax = 0;
  (*pIn).selstring = NULL;
  (*pIn).mloop = 0;
  (*pIn).seltype = NULL;

  (*pIn).levelqty = 0;

  (*pIn).randomizestring = NULL;

  (*pIn).mode = NULL;
  (*pIn).input = 0;

  (*pIn).domain = NULL;
  (*pIn).scalestring = NULL;
  (*pIn).scale = NULL;
  (*pIn).crop = NULL;

  (*pIn).tesrsizestring = NULL;

  (*pIn).checktess = 0;
  (*pIn).filter = NULL;

  (*pIn).format = NULL;
  (*pIn).tesrformat = NULL;
  (*pIn).oridistrib = NULL;
  (*pIn).orides = NULL;
  (*pIn).oricrysym = NULL;
  (*pIn).oriformat = NULL;
  (*pIn).body = NULL;
  (*pIn).load = NULL;
  (*pIn).loadpoint = NULL;
  (*pIn).tess = NULL;
  (*pIn).mtess = NULL;
  (*pIn).tesr = NULL;
  (*pIn).raw = NULL;
  (*pIn).geo = NULL;
  (*pIn).mgeo = NULL;
  (*pIn).ply = NULL;
  (*pIn).stc = NULL;
  (*pIn).stv = NULL;
  (*pIn).ste = NULL;
  (*pIn).stf = NULL;
  (*pIn).stp = NULL;
  (*pIn).stg = NULL;
  (*pIn).dec = NULL;
  (*pIn).obj = NULL;
  (*pIn).ori = NULL;
  (*pIn).debug = NULL;

  (*pIn).sortstring = NULL;
  (*pIn).stpt = NULL;

  (*pIn).centroidstring = NULL;
  (*pIn).centroid = NULL;
  (*pIn).hardcorestring = NULL;
  (*pIn).hardcore = NULL;
  (*pIn).centroiditermax = 0;
  (*pIn).centroidfact = 0;
  (*pIn).centroidconv = 0;

  return;
}

void
net_in_free (struct IN In)
{
  ut_free_1d_char (In.domain);
  ut_free_1d_char (In.scalestring);
  ut_free_1d_char (In.crop);
  ut_free_1d (In.scale);
  ut_free_1d_char (In.format);
  ut_free_1d_char (In.tesrformat);
  ut_free_1d_char (In.oridistrib);
  ut_free_1d_char (In.orides);
  ut_free_1d_char (In.oricrysym);
  ut_free_1d_char (In.oriformat);
  ut_free_1d_char (In.body);
  ut_free_1d_char (In.load);
  ut_free_1d_char (In.loadpoint);
  ut_free_1d_char (In.tess);
  ut_free_1d_char (In.mtess);
  ut_free_1d_char (In.tesr);
  ut_free_1d_char (In.raw);
  ut_free_1d_char (In.geo);
  ut_free_1d_char (In.mgeo);
  ut_free_1d_char (In.ply);
  ut_free_1d_char (In.dec);
  ut_free_1d_char (In.obj);
  ut_free_1d_char (In.ori);
  ut_free_1d_char (In.debug);
  ut_free_1d_char (In.sortstring);
  ut_free_1d_char (In.stpt);
  ut_free_1d_char (In.tesrsizestring);
  ut_free_1d_char (In.filter);

  ut_free_1d_char (In.nstring);
  ut_free_1d_char (In.centroidstring);

  ut_free_1d_char (In.hardcorestring);
  if (In.hardcore)
    ut_free_2d_char (In.hardcore, In.levelqty + 1);

  ut_free_1d_char (In.input);
  ut_free_1d_int (In.centroid);
  ut_free_1d_char (In.ttype);
  ut_free_1d_char (In.seltype);
  ut_free_1d_char (In.selstring);
  ut_free_1d_char (In.morphostring);
  ut_free_2d_char (In.morpho, In.levelqty + 1);
  ut_free_1d_char (In.weightstring);
  ut_free_2d_char (In.weight, In.levelqty + 1);
  ut_free_1d_char (In.randomizestring);
  ut_free_2d_char (In.n, In.levelqty + 1);
  ut_free_1d_char (In.idstring);
  ut_free_1d_int (In.id);
  ut_free_1d_char (In.mode);

  return;
}
