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

void
net_tesr_vor3 (struct TESS Domain, struct SEEDSET GSet, struct TESR *pTesr)
{
  int i, j, k, l;
  double *coo = ut_alloc_1d (3);
  int *pos = ut_alloc_1d_int (3);
  double *SeedDist = ut_alloc_1d (GSet.N + GSet.nN + 1);

  (*pTesr).Dim = GSet.Dim;

  for (k = 1; k <= (*pTesr).size[2]; k++)
    for (j = 1; j <= (*pTesr).size[1]; j++)
      for (i = 1; i <= (*pTesr).size[0]; i++)
      {
	ut_array_1d_int_set_3 (pos, i, j, k);
	neut_tesr_coo (*pTesr, pos, coo);

	if ((*pTesr).Dim == 1 || neut_tess_point_incell (Domain, coo, 1))
	{
	  for (l = 1; l <= GSet.N; l++)
	    SeedDist[l] = ut_space_dist (coo, GSet.SeedCoo[l]);

	  for (l = 1; l <= GSet.nN; l++)
	    SeedDist[GSet.N + l] = ut_space_dist (coo, GSet.nSeedCoo[l]);

	  (*pTesr).VoxCell[i][j][k] =
	    1 + ut_array_1d_min_index (SeedDist + 1, GSet.N + GSet.nN);
	}
      }

  (*pTesr).SeedCoo = ut_alloc_2d ((*pTesr).CellQty + 1, 3);
  ut_array_2d_memcpy ((*pTesr).SeedCoo + 1, (*pTesr).CellQty, 3,
		      GSet.SeedCoo + 1);
  (*pTesr).SeedWeight = ut_alloc_1d ((*pTesr).CellQty + 1);
  ut_array_1d_memcpy ((*pTesr).SeedWeight + 1, (*pTesr).CellQty,
		      GSet.SeedWeight + 1);

  ut_free_1d (SeedDist);
  ut_free_1d (coo);
  ut_free_1d_int (pos);

  return;
}
