/* This file is part of the Neper software package. */
/* Copyright (C) 2003-2015, Romain Quey. */
/* See the COPYING file in the top-level directory. */

#include"net_clip_.h"

void
net_clip (struct IN In, struct SEEDSET SeedSet, struct TESS *pTess)
{
  int i, qty, status;
  FILE *fp = NULL;
  char *tmp = NULL;
  double *eq = ut_alloc_1d (4);
  char *input = ut_alloc_1d_char (strlen (In.clip) + 1);
  char *file = ut_alloc_1d_char (strlen (In.clip) + 1);

  ut_string_string (In.clip, &tmp);

  ut_string_fnr (tmp, '(', ' ');
  ut_string_fnr (tmp, ')', ' ');
  ut_string_fnr (tmp, ',', ' ');
  if (sscanf (tmp, "%s%s", input, file) != 2)
    ut_print_message (2, 2, "Failed to read argument `%s'.\n", In.clip);

  fp = ut_file_open (file, "r");
  if (fscanf (fp, "%d", &qty) != 1)
    abort ();
  for (i = 0; i < qty; i++)
  {
    status = ut_array_1d_fscanf (fp, eq, 4);
    if (status != 1)
      abort ();
    ut_array_1d_scale (eq, 4, 1. / ut_vector_norm (eq + 1));
    net_tess_clip (SeedSet, pTess, eq);
  }
  ut_file_close (fp, file, "r");

  ut_free_1d_char (tmp);
  ut_free_1d_char (input);
  ut_free_1d_char (file);
  ut_free_1d (eq);

  return;
}
