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

extern void
net_res_ori (struct IN In, struct SEEDSET SeedSet)
{
  FILE *file = NULL;
  int i, j;
  char *des = ut_alloc_1d_char (10);
  char *crysym = ut_alloc_1d_char (10);
  char *format = ut_alloc_1d_char (10);

  file = ut_file_open (In.ori, "w");

  strcpy (des, In.orides);
  strcpy (crysym, In.oricrysym);
  strcpy (format, In.oriformat);

  // plain format
  if (strcmp (format, "plain") == 0)
  {
    if (strcmp (des, "e") == 0)
    {
      double *e = ol_e_alloc ();
      for (i = 1; i <= SeedSet.N; i++)
      {
	ol_q_e (SeedSet.q[i], e);
	if (strcmp (crysym, "triclinic") != 0)
	  ol_e_ecrysym (e, crysym, e);
	ol_e_fprintf (file, e, "%17.12f");
      }
      ol_e_free (e);
    }
    else if (strcmp (des, "ek") == 0)
    {
      double *e = ol_e_alloc ();
      for (i = 1; i <= SeedSet.N; i++)
      {
	ol_q_e (SeedSet.q[i], e);
	ol_e_ek (e, e);
	ol_e_fprintf (file, e, "%17.12f");
      }
      ol_e_free (e);
    }
    else if (strcmp (des, "er") == 0)
    {
      double *e = ol_e_alloc ();
      for (i = 1; i <= SeedSet.N; i++)
      {
	ol_q_e (SeedSet.q[i], e);
	ol_e_er (e, e);
	ol_e_fprintf (file, e, "%17.12f");
      }
      ol_e_free (e);
    }
    else if (strcmp (des, "g") == 0)
    {
      double **g = ol_g_alloc ();
      for (i = 1; i <= SeedSet.N; i++)
      {
	ol_q_g (SeedSet.q[i], g);
	if (strcmp (crysym, "triclinic") != 0)
	  ol_g_gcrysym (g, crysym, g);
	ol_g_fprintf (file, g, "%17.12f");
      }
      ol_g_free (g);
    }
    else if (strcmp (des, "rtheta") == 0)
    {
      double *r = ol_r_alloc ();
      double theta;
      for (i = 1; i <= SeedSet.N; i++)
      {
	ol_q_rtheta (SeedSet.q[i], r, &theta);
	if (strcmp (crysym, "triclinic") != 0)
	  ol_rtheta_rthetacrysym (r, theta, crysym, r, &theta);
	ol_rtheta_fprintf (file, r, theta, "%17.12f");
      }
      ol_r_free (r);
    }
    else if (strcmp (des, "R") == 0)
    {
      double *R = ol_R_alloc ();
      for (i = 1; i <= SeedSet.N; i++)
      {
	ol_q_R (SeedSet.q[i], R);
	if (strcmp (crysym, "triclinic") != 0)
	  ol_R_Rcrysym (R, crysym, R);
	ol_R_fprintf (file, R, "%17.12f");
      }
      ol_R_free (R);
    }
    else if (strcmp (des, "q") == 0)
    {
      for (i = 1; i <= SeedSet.N; i++)
      {
	if (strcmp (crysym, "triclinic") != 0)
	  ol_R_Rcrysym (SeedSet.q[i], crysym, SeedSet.q[i]);
	ol_q_fprintf (file, SeedSet.q[i], "%17.12f");
      }
    }
    else if (strcmp (des, "Rcol") == 0)
    {
      double *R = ol_R_alloc ();
      int *rgb = ut_alloc_1d_int (3);
      double SQRT2 = 1.41421356237309504880;
      for (i = 1; i <= SeedSet.N; i++)
      {
	if (strcmp (crysym, "cubic"))
	  ut_print_message (2, 1, "Rcol requires `-oricrysym cubic'.\n");

	ol_q_R (SeedSet.q[i], R);
	ol_R_Rcrysym (R, crysym, R);
	for (j = 0; j < 3; j++)
	  rgb[j] =
	    ut_num_d2ri (255 * (R[j] + (SQRT2 - 1)) / (2 * (SQRT2 - 1)));

	ut_array_1d_int_fprintf (file, rgb, 3, "%3d");
      }
      ol_R_free (R);
      ut_free_1d_int (rgb);
    }
    else
      ut_print_message (2, 1, "Format %s not available.\n", des);
  }

  // fepx format
  else if (strcmp (format, "fepx") == 0)
  {
    fprintf (file, "grain-orientations\n%d\n", SeedSet.N);
    double *e = ol_e_alloc ();
    for (i = 1; i <= SeedSet.N; i++)
    {
      ol_q_e (SeedSet.q[i], e);
      ol_e_ek (e, e);
      for (j = 0; j < 3; j++)
	fprintf (file, "%17.12f ", e[j]);
      fprintf (file, "%d\n", i);
    }
    ol_e_free (e);
    fprintf (file, "EOF\n");
  }

  // geof format
  else if (strcmp (format, "geof") == 0)
  {
    double *e = ol_e_alloc ();
    for (i = 1; i <= SeedSet.N; i++)
    {
      fprintf (file, "**elset poly%d  *rotation ", i);
      ol_q_e (SeedSet.q[i], e);
      ol_e_e (e, e);
      for (j = 0; j < 3; j++)
	fprintf (file, "%17.12f ", e[j]);
      fprintf (file, "\n");
    }
    ol_e_free (e);
  }

  ut_file_close (file, In.ori, "w");

  ut_free_1d_char (crysym);
  ut_free_1d_char (des);
  ut_free_1d_char (format);

  return;
}
