KiCad PCB EDA Suite
curve.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2017 Peter Selinger.
2  * This file is part of Potrace. It is free software and it is covered
3  * by the GNU General Public License. See the file COPYING for details. */
4 
5 /* private part of the path and curve data structures */
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "curve.h"
16 #include "lists.h"
17 #include "potracelib.h"
18 
19 #define SAFE_CALLOC( var, n, typ ) \
20  if( ( var = (typ*) calloc( n, sizeof( typ ) ) ) == NULL ) \
21  goto calloc_error
22 
23 /* ---------------------------------------------------------------------- */
24 /* allocate and free path objects */
25 
26 path_t* path_new( void )
27 {
28  path_t* p = NULL;
29  privpath_t* priv = NULL;
30 
31  SAFE_CALLOC( p, 1, path_t );
32  memset( p, 0, sizeof( path_t ) );
33  SAFE_CALLOC( priv, 1, privpath_t );
34  memset( priv, 0, sizeof( privpath_t ) );
35  p->priv = priv;
36  return p;
37 
38 calloc_error:
39  free( p );
40  free( priv );
41  return NULL;
42 }
43 
44 
45 /* free the members of the given curve structure. Leave errno unchanged. */
46 static void privcurve_free_members( privcurve_t* curve )
47 {
48  free( curve->tag );
49  free( curve->c );
50  free( curve->vertex );
51  free( curve->alpha );
52  free( curve->alpha0 );
53  free( curve->beta );
54 }
55 
56 
57 /* free a path. Leave errno untouched. */
58 void path_free( path_t* p )
59 {
60  if( p )
61  {
62  if( p->priv )
63  {
64  free( p->priv->pt );
65  free( p->priv->lon );
66  free( p->priv->sums );
67  free( p->priv->po );
70  }
71 
72  free( p->priv );
73  /* do not free p->fcurve ! */
74  }
75 
76  free( p );
77 }
78 
79 
80 /* free a pathlist, leaving errno untouched. */
81 void pathlist_free( path_t* plist )
82 {
83  path_t* p;
84 
85  list_forall_unlink( p, plist ) {
86  path_free( p );
87  }
88 }
89 
90 
91 /* ---------------------------------------------------------------------- */
92 /* initialize and finalize curve structures */
93 
94 typedef dpoint_t dpoint3_t[3];
95 
96 /* initialize the members of the given curve structure to size m.
97  * Return 0 on success, 1 on error with errno set. */
98 int privcurve_init( privcurve_t* curve, int n )
99 {
100  memset( curve, 0, sizeof( privcurve_t ) );
101  curve->n = n;
102  SAFE_CALLOC( curve->tag, n, int );
103  SAFE_CALLOC( curve->c, n, dpoint3_t );
104  SAFE_CALLOC( curve->vertex, n, dpoint_t );
105  SAFE_CALLOC( curve->alpha, n, double );
106  SAFE_CALLOC( curve->alpha0, n, double );
107  SAFE_CALLOC( curve->beta, n, double );
108  return 0;
109 
110 calloc_error:
111  free( curve->tag );
112  free( curve->c );
113  free( curve->vertex );
114  free( curve->alpha );
115  free( curve->alpha0 );
116  free( curve->beta );
117  return 1;
118 }
119 
120 
121 /* copy private to public curve structure */
123 {
124  c->n = pc->n;
125  c->tag = pc->tag;
126  c->c = pc->c;
127 }
void pathlist_free(path_t *plist)
Definition: curve.cpp:81
static void privcurve_free_members(privcurve_t *curve)
Definition: curve.cpp:46
path_t * path_new(void)
Definition: curve.cpp:26
int privcurve_init(privcurve_t *curve, int n)
Definition: curve.cpp:98
#define list_forall_unlink(elt, list)
Definition: lists.h:186
double * alpha
Definition: curve.h:29
#define SAFE_CALLOC(var, n, typ)
Definition: curve.cpp:19
privcurve_t curve
Definition: curve.h:62
dpoint_t * vertex
Definition: curve.h:28
void path_free(path_t *p)
Definition: curve.cpp:58
privcurve_t ocurve
Definition: curve.h:63
struct potrace_privpath_s * priv
Definition: potracelib.h:103
potrace_dpoint_t(* c)[3]
Definition: potracelib.h:86
void privcurve_to_curve(privcurve_t *pc, potrace_curve_t *c)
Definition: curve.cpp:122
double * beta
Definition: curve.h:31
point_t * pt
Definition: curve.h:53
sums_t * sums
Definition: curve.h:57
double * alpha0
Definition: curve.h:30
int * tag
Definition: curve.h:21
int n
Definition: curve.h:20
dpoint_t dpoint3_t[3]
Definition: curve.cpp:94
dpoint_t(* c)[3]
Definition: curve.h:22