]> granicus.if.org Git - poly2tri-c/commitdiff
Small fixes to the memory management of P2trCDT objects
authorBarak Itkin <lightningismyname@gmail.com>
Sun, 27 May 2012 09:46:13 +0000 (12:46 +0300)
committerBarak Itkin <lightningismyname@gmail.com>
Sun, 27 May 2012 09:46:13 +0000 (12:46 +0300)
bin/main.c
refine/cdt.c

index d537553d1d76c614f366f5562f800567d9278121..3ff6e9978be74c2160cd5bffa3e1f1b2142f41df 100755 (executable)
@@ -47,6 +47,8 @@ static gboolean debug_print = TRUE;
 static gboolean verbose = TRUE;
 static gchar *input_file = NULL;
 static gchar *output_file = NULL;
+static gboolean render_mesh = FALSE;
+static gboolean render_svg = FALSE;
 
 static GOptionEntry entries[] =
 {
@@ -55,6 +57,8 @@ static GOptionEntry entries[] =
   { "debug",            'd', 0, G_OPTION_ARG_NONE,     &debug_print,      "Enable debug printing",             NULL },
   { "input",            'i', 0, G_OPTION_ARG_FILENAME, &input_file,       "Use input file at FILE_IN",         "FILE_IN" },
   { "output",           'o', 0, G_OPTION_ARG_FILENAME, &output_file,      "Use output file at FILE_OUT",       "FILE_OUT" },
+  { "render-mesh",      'm', 0, G_OPTION_ARG_NONE,     &render_mesh,      "Render a color mesh of the result", NULL },
+  { "render-svg",       's', 0, G_OPTION_ARG_NONE,     &render_svg,       "Render an outline of the result",   NULL },
   { NULL }
 };
 
@@ -170,7 +174,9 @@ test_point_to_color (P2trPoint* point, gfloat *dest, gpointer user_data)
 
 gint main (int argc, char *argv[])
 {
-  FILE *out;
+  FILE *svg_out = NULL, *mesh_out = NULL;
+  gchar *svg_out_path, *mesh_out_path;
+
   GError *error = NULL;
   GOptionContext *context;
 
@@ -181,10 +187,6 @@ gint main (int argc, char *argv[])
   P2trCDT *rcdt;
   P2trDelaunayTerminator *dt;
 
-  gchar *buf;
-  gfloat *im;
-  P2trImageConfig imc;
-
   context = g_option_context_new ("- Create a fine mesh from a given PSLG");
   g_option_context_add_main_entries (context, entries, NULL);
 
@@ -206,16 +208,34 @@ gint main (int argc, char *argv[])
       exit (1);
     }
 
-  if (output_file == NULL)
+  if (output_file == NULL && (render_svg || render_mesh))
     {
       g_print ("No output file given. Stop.");
       exit (1);
     }
 
-  if ((out = fopen (output_file, "w")) == NULL)
+  if (render_svg)
     {
-      g_print ("Can't open the output file. Stop.");
-      exit (1);
+      svg_out_path = g_newa (gchar, strlen (output_file) + 4);
+      sprintf (svg_out_path, "%s.svg", output_file);
+
+      if ((svg_out = fopen (svg_out_path, "w")) == NULL)
+        {
+          g_print ("Can't open the svg output file. Stop.");
+          exit (1);
+        }
+    }
+
+  if (render_mesh)
+    {
+      mesh_out_path = g_newa (gchar, strlen (output_file) + 4);
+      sprintf (mesh_out_path, "%s.ppm", output_file);
+
+      if ((mesh_out = fopen (mesh_out_path, "w")) == NULL)
+        {
+          g_print ("Can't open the mesh output file. Stop.");
+          exit (1);
+        }
     }
 
   read_points_file (input_file, &pts, &colors);
@@ -229,41 +249,33 @@ gint main (int argc, char *argv[])
   dt = p2tr_dt_new (G_PI / 6, p2tr_dt_false_too_big, rcdt);
   p2tr_dt_refine (dt, refine_max_steps);
 
-  p2tr_plot_svg (dt->mesh->mesh,out);
-
-  fclose (out);
-
-  buf = g_newa(char, strlen(output_file) + 5);
-  sprintf (buf, "%s.ppm", output_file);
-
-  if ((out = fopen (buf, "w")) == NULL)
+  if (render_svg)
     {
-      g_print ("Can't open the output ppm file. Stop.");
-      exit (1);
+      p2tr_plot_svg (dt->mesh->mesh, svg_out);
+      fclose (svg_out);
     }
 
-  imc.cpp = 4;
-  imc.min_x = imc.min_y = 0;
-  imc.step_x = imc.step_y = 0.2;
-  imc.x_samples = imc.y_samples = 500;
-
-  im = g_new (gfloat, imc.cpp * imc.x_samples * imc.y_samples);
+  if (render_mesh)
+    {
+      P2trImageConfig imc;
+      gfloat *im;
 
-  p2tr_mesh_render_scanline (dt->mesh->mesh, im, &imc, test_point_to_color, NULL);
+      imc.cpp = 4;
+      imc.min_x = imc.min_y = 0;
+      imc.step_x = imc.step_y = 0.2;
+      imc.x_samples = imc.y_samples = 500;
 
-  p2tr_write_ppm (out, im, &imc);
-  fclose (out);
+      im = g_new (gfloat, imc.cpp * imc.x_samples * imc.y_samples);
 
-  g_free (im);
+      p2tr_mesh_render_scanline (dt->mesh->mesh, im, &imc, test_point_to_color, NULL);
 
-#if FALSE
-  p2tr_triangulation_free (T);
+      p2tr_write_ppm (mesh_out, im, &imc);
+      fclose (mesh_out);
 
-  for (i = 0; i < pts->len; i++)
-    {
-      p2tr_point_unref ((P2trPoint*) g_ptr_array_index (pts, i));
+      g_free (im);
     }
-#endif
+
+  p2tr_cdt_free (rcdt);
 
   g_ptr_array_free (pts, TRUE);
   g_array_free (colors, TRUE);
index 0cf46cf31fd0eb2aba62dbbc4765bccc9eccdcfd..70fbb517699c005d4854aec9433dcc06ae43a016 100644 (file)
@@ -129,9 +129,20 @@ p2tr_cdt_new (P2tCDT *cdt)
     p2tr_triangle_unref (new_tri);
   }
 
+  g_hash_table_destroy (point_map);
+
   return rmesh;
 }
 
+void
+p2tr_cdt_free (P2trCDT* self)
+{
+  p2tr_pslg_free (self->outline);
+  p2tr_mesh_unref (self->mesh);
+
+  g_slice_free (P2trCDT, self);
+}
+
 void
 p2tr_cdt_validate_edges (P2trCDT *self)
 {