pcb 4.1.1
An interactive printed circuit board layout editor.

curve.c

Go to the documentation of this file.
00001 
00018 /* curve.c 147 2007-04-09 00:44:09Z selinger */
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 
00024 #include "potracelib.h"
00025 #include "lists.h"
00026 #include "curve.h"
00027 
00028 #define SAFE_MALLOC(var, n, typ) \
00029   if ((var = (typ *)malloc((n)*sizeof(typ))) == NULL) goto malloc_error
00030 
00031 /* allocate and free path objects */
00032 
00036 path_t *
00037 path_new (void)
00038 {
00039   path_t *p = NULL;
00040   privpath_t *priv = NULL;
00041 
00042   SAFE_MALLOC (p, 1, path_t);
00043   memset (p, 0, sizeof (path_t));
00044   SAFE_MALLOC (priv, 1, privpath_t);
00045   memset (priv, 0, sizeof (privpath_t));
00046   p->priv = priv;
00047   return p;
00048 
00049 malloc_error:
00050   free (p);
00051   free (priv);
00052   return NULL;
00053 }
00054 
00060 static void
00061 privcurve_free_members (privcurve_t * curve)
00062 {
00063   free (curve->tag);
00064   free (curve->c);
00065   free (curve->vertex);
00066   free (curve->alpha);
00067   free (curve->alpha0);
00068   free (curve->beta);
00069 }
00070 
00076 void
00077 path_free (path_t * p)
00078 {
00079   if (p)
00080     {
00081       if (p->priv)
00082         {
00083           free (p->priv->pt);
00084           free (p->priv->lon);
00085           free (p->priv->sums);
00086           free (p->priv->po);
00087           privcurve_free_members (&p->priv->curve);
00088           privcurve_free_members (&p->priv->ocurve);
00089         }
00090       free (p->priv);
00091       /* do not free p->fcurve ! */
00092     }
00093   free (p);
00094 }
00095 
00101 void
00102 pathlist_free (path_t * plist)
00103 {
00104   path_t *p;
00105 
00106   list_forall_unlink (p, plist)
00107   {
00108     path_free (p);
00109   }
00110 }
00111 
00112 /* initialize and finalize curve structures */
00113 
00114 typedef dpoint_t dpoint3_t[3];
00115 
00121 int
00122 privcurve_init (privcurve_t * curve, int n)
00123 {
00124   memset (curve, 0, sizeof (privcurve_t));
00125   curve->n = n;
00126   SAFE_MALLOC (curve->tag, n, int);
00127   SAFE_MALLOC (curve->c, n, dpoint3_t);
00128   SAFE_MALLOC (curve->vertex, n, dpoint_t);
00129   SAFE_MALLOC (curve->alpha, n, double);
00130   SAFE_MALLOC (curve->alpha0, n, double);
00131   SAFE_MALLOC (curve->beta, n, double);
00132   return 0;
00133 
00134 malloc_error:
00135   free (curve->tag);
00136   free (curve->c);
00137   free (curve->vertex);
00138   free (curve->alpha);
00139   free (curve->alpha0);
00140   free (curve->beta);
00141   return 1;
00142 }
00143 
00147 void
00148 privcurve_to_curve (privcurve_t * pc, potrace_curve_t * c)
00149 {
00150   c->n = pc->n;
00151   c->tag = pc->tag;
00152   c->c = pc->c;
00153 }