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

void
net_flatten_dom_gen (struct TESS Tess, struct TESS *pFTess)
{
  (*pFTess).DomType = ut_alloc_1d_char (strlen (Tess.DomType) + 1);
  strcpy ((*pFTess).DomType, Tess.DomType);

  return;
}

void
net_flatten_dom_ver (struct TESS *Tess,
		     struct TESS *pFTess, struct TESSE *TessE)
{
  int i, ver;

  (*pFTess).DomVerQty = Tess[1].DomVerQty;
  (*pFTess).DomVerLabel = ut_alloc_1d_pchar ((*pFTess).DomVerQty + 1);
  for (i = 1; i <= (*pFTess).DomVerQty; i++)
  {
    (*pFTess).DomVerLabel[i] = ut_alloc_1d_char (strlen
						 (Tess[1].DomVerLabel[i]) +
						 1);
    strcpy ((*pFTess).DomVerLabel[i], Tess[1].DomVerLabel[i]);
  }

  (*pFTess).DomVerCoo = ut_alloc_2d ((*pFTess).DomVerQty + 1, 3);
  for (i = 1; i <= (*pFTess).DomVerQty; i++)
    ut_array_1d_memcpy ((*pFTess).DomVerCoo[i], 3,
			Tess[1].DomVerCoo[i]);

  (*pFTess).DomVerEdgeQty = ut_alloc_1d_int ((*pFTess).DomVerQty + 1);
  (*pFTess).DomVerEdgeNb = ut_alloc_1d_pint ((*pFTess).DomVerQty + 1);
  for (i = 1; i <= (*pFTess).DomVerQty; i++)
  {
    (*pFTess).DomVerEdgeQty[i] = Tess[1].DomVerEdgeQty[i];
    (*pFTess).DomVerEdgeNb[i] = ut_alloc_1d_int ((*pFTess).DomVerEdgeQty[i]);
    ut_array_1d_int_memcpy ((*pFTess).DomVerEdgeNb[i],
			    (*pFTess).DomVerEdgeQty[i],
			    Tess[1].DomVerEdgeNb[i]);
  }

  (*pFTess).DomTessVerNb = ut_alloc_1d_int ((*pFTess).DomVerQty + 1);
  for (i = 1; i <= (*pFTess).DomVerQty; i++)
  {
    ver = TessE[1].VerFVer[Tess[1].DomTessVerNb[i]];
    (*pFTess).DomTessVerNb[i] = ver;
  }

  return;
}

void
net_flatten_dom_edge (struct TESS *Tess,
		      struct TESS *pFTess, struct TESSE *TessE)
{
  int i, j, edge;

  (*pFTess).DomEdgeQty = Tess[1].DomEdgeQty;
  (*pFTess).DomEdgeLabel = ut_alloc_1d_pchar ((*pFTess).DomEdgeQty + 1);
  for (i = 1; i <= (*pFTess).DomEdgeQty; i++)
  {
    (*pFTess).DomEdgeLabel[i] = ut_alloc_1d_char (strlen
						  (Tess[1].DomEdgeLabel[i]) +
						  1);
    strcpy ((*pFTess).DomEdgeLabel[i], Tess[1].DomEdgeLabel[i]);
  }

  (*pFTess).DomEdgeVerNb = ut_alloc_2d_int ((*pFTess).DomEdgeQty + 1, 2);
  ut_array_2d_int_memcpy ((*pFTess).DomEdgeVerNb + 1,
			  (*pFTess).DomEdgeQty, 2, Tess[1].DomEdgeVerNb + 1);

  (*pFTess).DomEdgeFaceNb = ut_alloc_2d_int ((*pFTess).DomEdgeQty + 1, 2);
  ut_array_2d_int_memcpy ((*pFTess).DomEdgeFaceNb + 1,
			  (*pFTess).DomEdgeQty, 2, Tess[1].DomEdgeFaceNb + 1);

  (*pFTess).DomTessEdgeQty = ut_alloc_1d_int ((*pFTess).DomEdgeQty + 1);
  (*pFTess).DomTessEdgeNb = ut_alloc_1d_pint ((*pFTess).DomEdgeQty + 1);

  for (i = 1; i <= (*pFTess).DomEdgeQty; i++)
  {
    (*pFTess).DomTessEdgeNb[i] = NULL;
    for (j = 1; j <= Tess[1].DomTessEdgeQty[i]; j++)
    {
      edge = Tess[1].DomTessEdgeNb[i][j];
      (*pFTess).DomTessEdgeNb[i]
	= ut_realloc_1d_int ((*pFTess).DomTessEdgeNb[i],
			     (*pFTess).DomTessEdgeQty[i] +
			     TessE[1].EdgeFEdgeQty[edge] + 1);
      ut_array_1d_int_memcpy ((*pFTess).DomTessEdgeNb[i] +
			      (*pFTess).DomTessEdgeQty[i] + 1,
			      TessE[1].EdgeFEdgeQty[edge],
			      TessE[1].EdgeFEdgeNb[edge]);
      (*pFTess).DomTessEdgeQty[i] += TessE[1].EdgeFEdgeQty[edge];
    }
  }

  return;
}

void
net_flatten_dom_face (struct TESS *Tess,
		      struct TESS *pFTess, struct TESSE *TessE)
{
  int i, j, face;

  (*pFTess).DomFaceQty = Tess[1].DomFaceQty;
  (*pFTess).DomFaceLabel = ut_alloc_1d_pchar ((*pFTess).DomFaceQty + 1);
  for (i = 1; i <= (*pFTess).DomFaceQty; i++)
  {
    (*pFTess).DomFaceLabel[i] = ut_alloc_1d_char (strlen
						  (Tess[1].DomFaceLabel[i]) +
						  1);
    strcpy ((*pFTess).DomFaceLabel[i], Tess[1].DomFaceLabel[i]);
  }

  (*pFTess).DomFaceEq = ut_alloc_2d ((*pFTess).DomFaceQty + 1, 4);
  ut_array_2d_memcpy ((*pFTess).DomFaceEq + 1, (*pFTess).DomFaceQty,
		      4, Tess[1].DomFaceEq + 1);

  (*pFTess).DomFaceVerQty = ut_alloc_1d_int ((*pFTess).DomFaceQty + 1);
  (*pFTess).DomFaceVerNb = ut_alloc_1d_pint ((*pFTess).DomFaceQty + 1);
  (*pFTess).DomFaceEdgeNb = ut_alloc_1d_pint ((*pFTess).DomFaceQty + 1);
  for (i = 1; i <= (*pFTess).DomFaceQty; i++)
  {
    (*pFTess).DomFaceVerQty[i] = Tess[1].DomFaceVerQty[i];
    (*pFTess).DomFaceVerNb[i] = ut_alloc_1d_int
      ((*pFTess).DomFaceVerQty[i] + 1);
    ut_array_1d_int_memcpy ((*pFTess).DomFaceVerNb[i] + 1,
			    (*pFTess).DomFaceVerQty[i],
			    Tess[1].DomFaceVerNb[i] + 1);
    (*pFTess).DomFaceEdgeNb[i] = ut_alloc_1d_int
      ((*pFTess).DomFaceVerQty[i] + 1);
    ut_array_1d_int_memcpy ((*pFTess).DomFaceEdgeNb[i] + 1,
			    (*pFTess).DomFaceVerQty[i],
			    Tess[1].DomFaceEdgeNb[i] + 1);
  }

  (*pFTess).DomTessFaceQty = ut_alloc_1d_int ((*pFTess).DomFaceQty + 1);
  (*pFTess).DomTessFaceNb = ut_alloc_1d_pint ((*pFTess).DomFaceQty + 1);

  for (i = 1; i <= (*pFTess).DomFaceQty; i++)
  {
    (*pFTess).DomTessFaceNb[i] = NULL;
    for (j = 1; j <= Tess[1].DomTessFaceQty[i]; j++)
    {
      face = Tess[1].DomTessFaceNb[i][j];
      (*pFTess).DomTessFaceNb[i]
	= ut_realloc_1d_int ((*pFTess).DomTessFaceNb[i],
			     (*pFTess).DomTessFaceQty[i] +
			     TessE[1].FaceFFaceQty[face] + 1);
      ut_array_1d_int_memcpy ((*pFTess).DomTessFaceNb[i] +
			      (*pFTess).DomTessFaceQty[i] + 1,
			      TessE[1].FaceFFaceQty[face],
			      TessE[1].FaceFFaceNb[face]);
      (*pFTess).DomTessFaceQty[i] += TessE[1].FaceFFaceQty[face];
    }
  }

  return;
}
