/* 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"nev_pointdata_.h"

void
nev_pointdata_init (struct POINT Point, struct POINTDATA *pPointData)
{
  double pointrad;

  (*pPointData).Col = ut_alloc_2d_int ((*pPointData).PointQty + 1, 3);
  (*pPointData).Rad = ut_alloc_1d ((*pPointData).PointQty + 1);
  (*pPointData).trs = ut_alloc_1d ((*pPointData).PointQty + 1);
  (*pPointData).Coo = ut_alloc_2d ((*pPointData).PointQty + 1, 3);

  pointrad = 0.0168 / pow ((*pPointData).PointQty, 0.25);
  ut_array_1d_set ((*pPointData).Rad + 1, (*pPointData).PointQty, pointrad);
  ut_array_2d_int_set ((*pPointData).Col + 1, (*pPointData).PointQty, 3, 128);
  ut_array_2d_memcpy ((*pPointData).Coo + 1, (*pPointData).PointQty, 3,
		      Point.PointCoo + 1);

  if ((*pPointData).ColData)
  {
    if ((*pPointData).ColDataType
	&& !strcmp ((*pPointData).ColDataType, "id"))
      nev_data_id_colour ((*pPointData).ColData, (*pPointData).PointQty,
			   (*pPointData).Col);
    else if ((*pPointData).ColDataType
	&& !strcmp ((*pPointData).ColDataType, "col"))
      nev_data_col_colour ((*pPointData).ColData, (*pPointData).PointQty,
			   (*pPointData).Col);
    else if ((*pPointData).ColDataType
	     && !strncmp ((*pPointData).ColDataType, "ori", 3))
      nev_data_ori_colour ((*pPointData).ColData, (*pPointData).PointQty,
			   (*pPointData).ColScheme,
			   (*pPointData).Col);
    else if ((*pPointData).ColDataType
	     && !strcmp ((*pPointData).ColDataType, "scal"))
      nev_data_scal_colour ((*pPointData).ColData,
			    (*pPointData).PointQty,
			    (*pPointData).Scale,
			    (*pPointData).ColScheme,
			    (*pPointData).Col,
			    &((*pPointData).Scale));

    else
    {
      printf ("(*pPointData).ColDataType = %s\n",
	  (*pPointData).ColDataType);
      ut_error_reportbug ();
    }
  }

  if (!(*pPointData).RadDataType)
    ut_string_string ("rad", &((*pPointData).RadDataType));
  if ((*pPointData).RadData)
    nev_data_rad_radius ((*pPointData).RadData, (*pPointData).PointQty,
			 (*pPointData).Rad);

  if ((*pPointData).trsdata)
    nev_data_tr_tr ((*pPointData).trsdata, (*pPointData).PointQty,
		    (*pPointData).trs);

  if ((*pPointData).CooDataType)
  {
    if  (!strcmp ((*pPointData).CooDataType, "coo"))
      nev_data_coo_coo (Point.PointCoo, (*pPointData).CooData,
			(*pPointData).CooFact, (*pPointData).PointQty,
			(*pPointData).Coo);
    else if (!strcmp ((*pPointData).CooDataType, "disp"))
      nev_data_disp_coo (Point.PointCoo, (*pPointData).CooData,
			 (*pPointData).CooFact, (*pPointData).PointQty,
			 (*pPointData).Coo);
    else
      abort ();
  }

  return;
}

void
nev_pointdata_fscanf (char *type, char *argument,
		     struct POINTDATA *pPointData)
{
  char **args = NULL;
  int i, argqty;
  char *value = ut_alloc_1d_char (1000);
  char *mod = ut_alloc_1d_char (1000);

  ut_string_separate (argument, NEUT_SEP_DEP, &args, &argqty);

  if (!strcmp (type, "col"))
  {
    (*pPointData).ColDataType = ut_alloc_1d_char (100);

    nev_data_typearg_args ("col", argument, (*pPointData).ColDataType,
			   value, mod);

    if (!strcmp ((*pPointData).ColDataType, "id"))
    {
      (*pPointData).ColData = ut_alloc_2d ((*pPointData).PointQty + 1, 3);

      for (i = 1; i <= (*pPointData).PointQty; i++)
	(*pPointData).ColData[i][0] = i;
    }
    else if (!strcmp ((*pPointData).ColDataType, "col"))
    {
      (*pPointData).ColData = ut_alloc_2d ((*pPointData).PointQty + 1, 3);
      ut_array_2d_fscanfn_wcard (value, (*pPointData).ColData + 1,
				 (*pPointData).PointQty, 3, "colour,size");
    }
    else if (!strncmp ((*pPointData).ColDataType, "ori", 3))
    {
      (*pPointData).ColData = ut_alloc_2d ((*pPointData).PointQty + 1, 3);
      int i;
      double *tmpd = ut_alloc_1d (4);
      int *tmpi = ut_alloc_1d_int (6);
      double **tmpdd = ut_alloc_2d (3, 3);
      FILE *file = ut_file_open (value, "r");

      int qty0;
      if (!strcmp ((*pPointData).ColDataType + 3, "e")
	  || !strcmp ((*pPointData).ColDataType + 3, "ek")
	  || !strcmp ((*pPointData).ColDataType + 3, "er")
	  || !strcmp ((*pPointData).ColDataType + 3, "R"))
	qty0 = 3;
      else if (!strcmp ((*pPointData).ColDataType + 3, "q"))
	qty0 = 4;
      else if (!strcmp ((*pPointData).ColDataType + 3, "m"))
	qty0 = 6;
      else if (!strcmp ((*pPointData).ColDataType + 3, "g"))
	qty0 = 9;
      else
      {
	printf ("(*pPointData).ColDataType = %s\n",
		(*pPointData).ColDataType);
	ut_error_reportbug ();
	abort ();
      }

      ut_file_nbwords_testwmessage (value, (*pPointData).PointQty * qty0);

      for (i = 1; i <= (*pPointData).PointQty; i++)
	if (!strcmp ((*pPointData).ColDataType + 3, "e"))
	  ol_e_fscanf (file, (*pPointData).ColData[i]);
	else if (!strcmp ((*pPointData).ColDataType + 3, "ek"))
	{
	  ol_e_fscanf (file, tmpd);
	  ol_ek_e (tmpd, (*pPointData).ColData[i]);
	}
	else if (!strcmp ((*pPointData).ColDataType + 3, "er"))
	{
	  ol_e_fscanf (file, tmpd);
	  ol_er_e (tmpd, (*pPointData).ColData[i]);
	}
	else if (!strcmp ((*pPointData).ColDataType + 3, "q"))
	{
	  ol_q_fscanf (file, tmpd);
	  ol_q_e (tmpd, (*pPointData).ColData[i]);
	}
	else if (!strcmp ((*pPointData).ColDataType + 3, "R"))
	{
	  ol_R_fscanf (file, tmpd);
	  ol_R_e (tmpd, (*pPointData).ColData[i]);
	}
	else if (!strcmp ((*pPointData).ColDataType + 3, "m"))
	{
	  ol_m_fscanf (file, tmpi);
	  ol_m_e (tmpi, (*pPointData).ColData[i]);
	}
	else if (!strcmp ((*pPointData).ColDataType + 3, "g"))
	{
	  ol_g_fscanf (file, tmpdd);
	  ol_g_e (tmpdd, (*pPointData).ColData[i]);
	}
	else
	  ut_error_reportbug ();
    }
    else if (!strcmp ((*pPointData).ColDataType, "scal"))
    {
      (*pPointData).ColData = ut_alloc_2d ((*pPointData).PointQty + 1, 1);
      ut_array_2d_fscanfn_wcard (value, (*pPointData).ColData + 1,
				 (*pPointData).PointQty, 1, "numeral,size");
    }
    else
      abort ();
  }
  else if (!strcmp (type, "trs"))
  {
    (*pPointData).trsdatatype = ut_alloc_1d_char (100);

    if (argqty == 1)
      strcpy ((*pPointData).trsdatatype, type);
    else
      strcpy ((*pPointData).trsdatatype, args[0]);

    if (!strcmp ((*pPointData).trsdatatype, "trs"))
    {
      (*pPointData).trsdata = ut_alloc_2d ((*pPointData).PointQty + 1, 3);
      ut_array_2d_fscanfn_wcard (args[0], (*pPointData).trsdata + 1,
				 (*pPointData).PointQty, 1, "numeral,size");
    }
    else if (!strcmp ((*pPointData).trsdatatype, "scal"))
    {
      (*pPointData).trsdata = ut_alloc_2d ((*pPointData).PointQty + 1, 3);
      ut_array_2d_fscanfn_wcard (args[0] + 5, (*pPointData).trsdata + 1,
				 (*pPointData).PointQty, 1, "numeral,size");
    }
    else
      abort ();
  }
  else if (!strcmp (type, "rad"))
  {
    (*pPointData).RadDataType = ut_alloc_1d_char (100);

    nev_data_typearg_args ("rad", argument, (*pPointData).RadDataType,
	                   value, mod);
    if (strlen (mod) > 0)
      ut_string_string (mod, &((*pPointData).Space));

    if (!strcmp ((*pPointData).RadDataType, "rad"))
    {
      (*pPointData).RadData = ut_alloc_2d ((*pPointData).PointQty + 1, 3);
      ut_array_2d_fscanfn_wcard (value, (*pPointData).RadData + 1,
				 (*pPointData).PointQty, 1, "numeral,size");
    }
    else if (!strcmp ((*pPointData).RadDataType, "cube"))
    {
      (*pPointData).RadData = ut_alloc_2d ((*pPointData).PointQty + 1, 10);
      ut_array_2d_fscanfn_wcard (value, (*pPointData).RadData + 1,
				 (*pPointData).PointQty, 10, "numeral,size");
    }
    else if (!strcmp ((*pPointData).RadDataType, "cyl"))
    {
      (*pPointData).RadData = ut_alloc_2d ((*pPointData).PointQty + 1, 5);
      ut_array_2d_fscanfn_wcard (value, (*pPointData).RadData + 1,
				 (*pPointData).PointQty, 5, "numeral,size");
    }
    else if (!strcmp ((*pPointData).RadDataType, "ell"))
    {
      (*pPointData).RadData = ut_alloc_2d ((*pPointData).PointQty + 1, 12);
      ut_array_2d_fscanfn_wcard (value, (*pPointData).RadData + 1,
				 (*pPointData).PointQty, 12, "numeral,size");
    }
    else
      abort ();
  }
  else if (!strcmp (type, "colscheme"))
  {
    (*pPointData).ColScheme = ut_alloc_1d_char (strlen (argument) + 1);
    strcpy ((*pPointData).ColScheme, argument);
  }
  else if (!strcmp (type, "scale"))
  {
    (*pPointData).Scale = ut_alloc_1d_char (strlen (argument) + 1);
    strcpy ((*pPointData).Scale, argument);
  }
  else if (!strcmp (type, "scaletitle"))
  {
    (*pPointData).ScaleTitle = ut_alloc_1d_char (strlen (argument) + 1);
    strcpy ((*pPointData).ScaleTitle, argument);
  }
  else if (!strcmp (type, "coo"))
  {
    if (argqty == 1)
    {
      (*pPointData).CooDataType = ut_alloc_1d_char (strlen (type) + 1);
      strcpy ((*pPointData).CooDataType, type);
    }
    else
    {
      (*pPointData).CooDataType = ut_alloc_1d_char (strlen (args[0]) + 1);
      strcpy ((*pPointData).CooDataType, args[0]);
    }

    if ((*pPointData).CooData == NULL)
      (*pPointData).CooData = ut_alloc_2d ((*pPointData).PointQty + 1, 3);

    if (!strcmp ((*pPointData).CooDataType, "coo"))
      ut_array_2d_fscanfn_wcard (args[0], (*pPointData).CooData + 1,
				 (*pPointData).PointQty, 3, "size");
    else if (!strcmp ((*pPointData).CooDataType, "disp"))
      ut_array_2d_fscanfn_wcard (args[1], (*pPointData).CooData + 1,
				 (*pPointData).PointQty, 3, "size");
    else
      abort ();
  }
  else if (!strcmp (type, "coofact"))
    sscanf (args[0], "%lf", &((*pPointData).CooFact));
  else
    ut_error_reportbug ();

  ut_free_1d_char (value);
  ut_free_1d_char (mod);

  return;
}
