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

void
net_flatten_ver (struct MTESS MTess, struct TESS *Tess,
		 struct TESS *pFTess, struct TESSE *TessE,
		 struct FLATTEN *pFlatten)
{
  int i, j;
  int *dom = ut_alloc_1d_int (2);

  // Vertices
  (*pFTess).VerQty = 0;
  (*pFTess).VerCoo = ut_alloc_1d_pdouble (1);

  TessE[1].VerFVer = ut_alloc_1d_int (Tess[1].VerQty + 1);

  // adding vertices of tessellation L1
  for (j = 1; j <= Tess[1].VerQty; j++)
  {
    ut_array_1d_int_set_2 (dom, 0, j);
    neut_tess_addver (pFTess, Tess[1].VerCoo[j]);
    TessE[1].VerFVer[j] = (*pFTess).VerQty;
    neut_flatten_addver (pFlatten, dom);
  }

  // adding vertices of tessellations L2
  for (i = 2; i <= MTess.TessQty; i++)
  {
    TessE[i].VerFVer = ut_alloc_1d_int (Tess[i].VerQty + 1);

    for (j = 1; j <= Tess[i].VerQty; j++)
      if (Tess[i].VerDom[j][0] != 0)
      {
	dom[0] = Tess[i].VerDom[j][0];
	if (dom[0] == 1)
	  dom[1] = MTess.TessDomEdgeNb[i][Tess[i].VerDom[j][1]];
	else if (dom[0] == 2)
	  dom[1] = MTess.TessDomFaceNb[i][Tess[i].VerDom[j][1]];
	else if (dom[0] == -1)
	{
	  dom[0] = 3;
	  dom[1] = MTess.TessDom[i][1];
	}
	else
	  abort ();

	neut_tess_addver (pFTess, Tess[i].VerCoo[j]);
	TessE[i].VerFVer[j] = (*pFTess).VerQty;
	neut_flatten_addver (pFlatten, dom);
      }
      else
      {
	int domver = Tess[i].VerDom[j][1];
	int domverid = MTess.TessDomVerNb[i][domver];
	TessE[i].VerFVer[j] = TessE[1].VerFVer[domverid];
      }
  }

  // filling pFVerTessVerQty and pFVerTessVerNb
  net_flatten_ver_init_fvertessver (MTess, Tess, pFTess, TessE, pFlatten);

  ut_free_1d_int (dom);

  return;
}
