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

void
net_flatten_cell_body (struct MTESS MTess, struct TESS *Tess,
		       struct TESS *pFTess, struct TESSE **pTessE,
		       struct FLATTEN *pFlatten)
{
  int i, j, k, l, face, poly, cell, domtess, dompoly;
  int *dom = ut_alloc_1d_int (2);

  for (i = 2; i <= MTess.TessQty; i++)
  {
    ut_array_1d_int_set_2 (dom, 3, MTess.TessDom[i][1]);

    for (j = 1; j <= Tess[i].PolyQty; j++)
    {
      poly = neut_tess_addpoly_alloc (pFTess);
      cell = neut_tess_addcell_alloc (pFTess);

      neut_flatten_addpoly (pFlatten, dom);

      neut_tess_cell_addcentre (pFTess, cell, Tess[i].SeedCoo[j],
					      Tess[i].SeedWeight[j]);
      ut_array_1d_memcpy ((*pFTess).CellOri[cell], 4, Tess[i].CellOri[j]);

      for (k = 1; k <= Tess[i].PolyFaceQty[j]; k++)
      {
	face = Tess[i].PolyFaceNb[j][k];
	for (l = 0; l < (*pTessE)[i].FaceFFaceQty[face]; l++)
	{
	  neut_tess_poly_addface (pFTess, poly,
				  (*pTessE)[i].FaceFFaceNb[face][l],
				  Tess[i].PolyFaceOri[j][k] *
				  (*pTessE)[i].FaceFFaceOri[face][l]);
	}
      }

      domtess = MTess.TessDom[i][0];
      dompoly = MTess.TessDom[i][1];
      (*pFTess).CellTrue[poly] = Tess[domtess].CellTrue[dompoly];
      (*pFTess).CellBody[poly] = Tess[domtess].CellBody[dompoly];

      neut_tesse_poly_addpoly (&(*pTessE)[i], j, poly);
    }
  }

  neut_tess_init_facepoly (pFTess);

  ut_free_1d_int (dom);

  return;
}
