Пример #1
0
void gts_polygon_triangulate_test() 
{
	int i; 
	gdouble v[12][3] = {
		{0.0, 0.0, 0.0},
		{0.0, 3.0, 0.0},
		{3.0, 3.0, 0.0},
		{3.0, 1.0, 0.0},
		{4.0, 1.0, 0.0},
		{4.0, 3.0, 0.0},
		{5.0, 3.0, 0.0},
		{5.0, 0.0, 0.0},
		{2.0, 0.0, 0.0},
		{2.0, 2.0, 0.0},
		{1.0, 2.0, 0.0},
		{1.0, 0.0, 0.0},
	};

	GtsVector normal = {0,0,0}; 
	GtsVertex *vtx[12]; 
	GCList *polygon = NULL;
	GCList *triangle = NULL;
	GtsSurface *s, *s2; 
	
	GtsEdgePool *pool = gts_edge_pool_new(gts_edge_pool_class());
	
	gts_triangulate_test_stuff(); 
		
	for (i = 0; i < 12; ++i) {
		vtx[i] = gts_vertex_new(gts_vertex_class(), v[i][0],v[i][1],v[i][2]); 
		polygon = g_clist_append(polygon, vtx[i]);
	}

	for (i = 0; i < 3; ++i) {
		triangle = g_clist_prepend(triangle, vtx[i]); 
	}
	
	
	s = gts_surface_new(gts_surface_class(), 
			    gts_face_class(), 
			    gts_edge_class(),
			    gts_vertex_class());
	
	s2 = gts_surface_new(gts_surface_class(), 
			     gts_face_class(), 
			     gts_edge_class(),
			     gts_vertex_class());
	
	
	
	// triangulate the polygon by using its own orientation 
	polygon = gts_surface_add_polygon(s, pool, polygon, normal);
	// do some test ?
	g_assert(gts_surface_face_number(s) == 10); 
	g_debug("area  = %f", gts_surface_area(s)); 
	g_assert(gts_surface_area(s) == 11.0);
	
	
	
	gts_object_destroy(GTS_OBJECT(pool)); 
	// cleanup

	g_clist_free(triangle); 
	g_clist_free(polygon); 
	gts_object_destroy(GTS_OBJECT(s));
	gts_object_destroy(GTS_OBJECT(s2));
	g_message("Triangulate PASSED"); 
}
Пример #2
0
/* coarsen - produce a coarsened version of the input */
int main (int argc, char * argv[])
{
  GtsSurface * s;
  GtsPSurface * ps = NULL;
  gboolean verbose = FALSE;
  gboolean progressive = FALSE;
  gboolean log_cost = FALSE;
  guint number = 0;
  gdouble cmax = 0.0;
  StopOptions stop = NUMBER;
  CostOptions cost = COST_OPTIMIZED;
  MidvertexOptions mid = OPTIMIZED;
  GtsKeyFunc cost_func = NULL;
  GtsCoarsenFunc coarsen_func = NULL;
  GtsStopFunc stop_func = NULL;
  gpointer stop_data = NULL;
  int c = 0;
  GtsFile * fp;
  gdouble fold = PI/180.;
  GtsVolumeOptimizedParams params = { 0.5, 0.5, 0. };
  gpointer coarsen_data = NULL, cost_data = NULL;

  if (!setlocale (LC_ALL, "POSIX"))
    g_warning ("cannot set locale to POSIX");

  /* parse options using getopt */
  while (c != EOF) {
#ifdef HAVE_GETOPT_LONG
    static struct option long_options[] = {
      {"angle", no_argument, NULL, 'a'},
      {"progressive", no_argument, NULL, 'p'},
      {"help", no_argument, NULL, 'h'},
      {"verbose", no_argument, NULL, 'v'},
      {"number", required_argument, NULL, 'n'},
      {"length", no_argument, NULL, 'l'},
      {"midvertex", no_argument, NULL, 'm'},
      {"cost", required_argument, NULL, 'c'},
      {"fold", required_argument, NULL, 'f'},
      {"vweight", required_argument, NULL, 'w'},
      {"bweight", required_argument, NULL, 'b'},
      {"sweight", required_argument, NULL, 's'},
      {"log", no_argument, NULL, 'L'}
    };
    int option_index = 0;
    switch ((c = getopt_long (argc, argv, "hvmc:n:lpf:w:b:s:La",
			      long_options, &option_index))) {
#else /* not HAVE_GETOPT_LONG */
    switch ((c = getopt (argc, argv, "hvmc:n:lpf:w:b:s:La"))) {
#endif /* not HAVE_GETOPT_LONG */
    case 'a': /* angle */
      cost = COST_ANGLE;
      break;
    case 'L': /* log */
      log_cost = TRUE;
      break;
    case 'p': /* write progressive surface */
      progressive = TRUE;
      break;
    case 'n': /* stop by number */
      stop = NUMBER;
      number = atoi (optarg);
      break;
    case 'c': /* stop by cost */
      stop = COST;
      cmax = atof (optarg);
      break;
    case 'v': /* verbose */
      verbose = TRUE;
      break;
    case 'm': /* midvertex */
      mid = MIDVERTEX;
      break;
    case 'l': /* cost is length */
      cost = COST_LENGTH;
      break;
    case 'f': /* fold angle */
      fold = atof (optarg)*PI/180.;
      break;
    case 'w': /* volume optimized weight */
      params.volume_weight = atof (optarg);
      break;
    case 'b': /* boundary optimized weight */
      params.boundary_weight = atof (optarg);
      break;
    case 's': /* shape optimized weight */
      params.shape_weight = atof (optarg);
      break;
    case 'h': /* help */
      fprintf (stderr,
             "Usage: coarsen [OPTION] < file.gts\n"
	     "Construct a coarsened version of the input.\n"
	     "\n"
	     "  -n N, --number=N    stop the coarsening process if the number of\n"
	     "                      edges was to fall below N\n"
	     "  -c C, --cost=C      stop the coarsening process if the cost of collapsing\n"
	     "                      an edge is larger than C\n"
	     "  -m    --midvertex   use midvertex as replacement vertex\n"
	     "                      default is volume optimized point\n"
	     "  -l    --length      use length^2 as cost function\n"
	     "                      default is optimized point cost\n"
	     "  -f F, --fold=F      set maximum fold angle to F degrees\n"
	     "                      default is one degree\n"
	     "  -w W, --vweight=W   set weight used for volume optimization\n"
	     "                      default is 0.5\n"
	     "  -b W, --bweight=W   set weight used for boundary optimization\n"
	     "                      default is 0.5\n"
	     "  -s W, --sweight=W   set weight used for shape optimization\n"
	     "                      default is 0.0\n"
	     "  -p    --progressive write progressive surface file\n"
	     "  -L    --log         logs the evolution of the cost\n"
	     "  -v    --verbose     print statistics about the surface\n"
	     "  -h    --help        display this help and exit\n"
	     "\n"
	     "Reports bugs to %s\n",
	     GTS_MAINTAINER);
      return 0; /* success */
      break;
    case '?': /* wrong options */
      fprintf (stderr, "Try `coarsen --help' for more information.\n");
      return 1; /* failure */
    }
  }
  
  /* read surface in */
  s = gts_surface_new (gts_surface_class (),
		       gts_face_class (),
		       gts_edge_class (),
		       gts_vertex_class ());
  fp = gts_file_new (stdin);
  if (gts_surface_read (s, fp)) {
    fputs ("coarsen: the file on standard input is not a valid GTS file\n", 
	   stderr);
    fprintf (stderr, "stdin:%d:%d: %s\n", fp->line, fp->pos, fp->error);
    return 1; /* failure */
  }

  /* if verbose on print stats */
  if (verbose) {
    gts_surface_print_stats (s, stderr);
    fprintf (stderr, "# volume: %g area: %g\n", 
	     gts_surface_volume (s), gts_surface_area (s));
  }

  /* select the right coarsening process */
  switch (cost) {
  case COST_OPTIMIZED: 
    cost_func = (GtsKeyFunc) gts_volume_optimized_cost; 
    cost_data = &params;
    break;
  case COST_LENGTH:
    cost_func = NULL; break;
  case COST_ANGLE:
    cost_func = (GtsKeyFunc) cost_angle; break;
  default:
    g_assert_not_reached ();
  }
  switch (mid) {
  case MIDVERTEX:
    coarsen_func = NULL; 
    break;
  case OPTIMIZED:
    coarsen_func = (GtsCoarsenFunc) gts_volume_optimized_vertex; 
    coarsen_data = &params;
    break;
  default:
    g_assert_not_reached ();
  }
  if (log_cost)
    stop_func = (GtsStopFunc) stop_log_cost;
  else {
    switch (stop) {
    case NUMBER:
      if (verbose)
	stop_func = (GtsStopFunc) stop_number_verbose;
      else
	stop_func = (GtsStopFunc) gts_coarsen_stop_number; 
      stop_data = &number;
      break;
    case COST:
      if (verbose)
	stop_func = (GtsStopFunc) stop_cost_verbose;
      else
	stop_func = (GtsStopFunc) gts_coarsen_stop_cost; 
      stop_data = &cmax;
      break;
    default:
      g_assert_not_reached ();
    }
  }
  if (progressive)
    ps = gts_psurface_new (gts_psurface_class (),
			   s, gts_split_class (),
			   cost_func, cost_data,
			   coarsen_func, coarsen_data,
			   stop_func, stop_data, 
			   fold);
  else
    gts_surface_coarsen (s, 
			 cost_func, cost_data, 
			 coarsen_func, coarsen_data, 
			 stop_func, stop_data, fold);

  /* if verbose on print stats */
  if (verbose) {
    fputc ('\n', stderr);
    gts_surface_print_stats (s, stderr);
    fprintf (stderr, "# volume: %g area: %g\n", 
	     gts_surface_volume (s), gts_surface_area (s));
  }

  /* write resulting surface to standard output */
  if (progressive)
    gts_psurface_write (ps, stdout);
  else
    gts_surface_write (s, stdout);

  return 0; /* success */
}