| 1 |
|
| //file: Polygon/functions.c
|
| 2 |
|
| /*
|
| 3 |
|
|
|
| 4 |
|
| Copyright 2004 Eric L. Wilhelm
|
| 5 |
|
|
|
| 6 |
|
| GPL / Artistic License
|
| 7 |
|
| See Polygon.pm for details
|
| 8 |
|
|
|
| 9 |
|
| Version 0.04
|
| 10 |
|
|
|
| 11 |
|
| */
|
| 12 |
|
| #include "functions.h"
|
| 13 |
|
| #include "gpc.h"
|
| 14 |
|
|
|
| 15 |
7 |
| SV* new (char* class) {
|
| 16 |
7 |
| gpc_polygon* p = malloc(sizeof(gpc_polygon));
|
| 17 |
7 |
| SV* obj_ref = newSViv(0);
|
| 18 |
7 |
| SV* obj = newSVrv(obj_ref, class); // bless it
|
| 19 |
7 |
| p->num_contours = 0;
|
| 20 |
7 |
| sv_setiv(obj, (IV)p);
|
| 21 |
7 |
| SvREADONLY_on(obj);
|
| 22 |
7 |
| return(obj_ref);
|
| 23 |
|
| }
|
| 24 |
|
|
|
| 25 |
7 |
| void DESTROY(SV* obj) {
|
| 26 |
7 |
| gpc_polygon* p = (gpc_polygon*) SvIV(SvRV(obj));
|
| 27 |
|
| dbg_p(("running destroy for %d contours\n", p->num_contours));
|
| 28 |
7 |
▶ | if(p->num_contours > 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if(p->num_contours > 0) {
|
7 |
0 |
7 |
|
| 29 |
|
| dbg_p(("free contents now\n"));
|
| 30 |
7 |
| gpc_free_polygon2(p);
|
| 31 |
|
| }
|
| 32 |
|
| dbg_p(("that's done now\n"));
|
| 33 |
7 |
| free(p);
|
| 34 |
|
| dbg_p(("p free... DESTROY complete\n"));
|
| 35 |
7 |
| }
|
| 36 |
|
|
|
| 37 |
6 |
| int from_file(SV* obj, char* filename, int want_hole) {
|
| 38 |
6 |
| gpc_polygon* p = (gpc_polygon*) SvIV(SvRV(obj));
|
| 39 |
|
| FILE* sfp;
|
| 40 |
|
| dbg_p(("from %s file\n", filename));
|
| 41 |
6 |
| sfp = fopen(filename, "r");
|
| 42 |
6 |
▶ | if(! sfp) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if(! sfp) {
|
0 |
6 |
6 |
|
| 43 |
|
| dbg_p(("file open failed\n"));
|
| 44 |
0 |
| return(0);
|
| 45 |
|
| }
|
| 46 |
6 |
| gpc_read_polygon(sfp, want_hole, p);
|
| 47 |
|
| dbg_p(("read %d contours\n", p->num_contours));
|
| 48 |
6 |
| return(p->num_contours);
|
| 49 |
|
| }
|
| 50 |
|
|
|
| 51 |
0 |
| void to_file(SV* obj, char* filename, int want_hole) {
|
| 52 |
0 |
| gpc_polygon* p = (gpc_polygon*) SvIV(SvRV(obj));
|
| 53 |
|
| FILE* ofp;
|
| 54 |
0 |
| ofp = fopen(filename, "w");
|
| 55 |
0 |
| gpc_write_polygon(ofp, want_hole, p);
|
| 56 |
0 |
| }
|
| 57 |
|
|
|
| 58 |
4 |
| SV* clip_to(SV* obj, SV* clp, char* action) {
|
| 59 |
4 |
| gpc_polygon* p = (gpc_polygon*) SvIV(SvRV(obj));
|
| 60 |
4 |
| HV* stash = SvSTASH(SvRV(obj));
|
| 61 |
|
| // we need the classname to make a new object and to check on clp
|
| 62 |
4 |
▶ | char * classname = HvNAME(stash);
|
Branch: 6 branches
| Branch | True | False | Total |
| char * classname = HvNAME(stash);
|
4 |
0 |
4 |
| char * classname = HvNAME(stash);
|
4 |
0 |
4 |
| char * classname = HvNAME(stash);
|
0 |
4 |
4 |
| char * classname = HvNAME(stash);
|
0 |
0 |
0 |
| char * classname = HvNAME(stash);
|
4 |
0 |
4 |
| char * classname = HvNAME(stash);
|
0 |
4 |
4 |
|
| 63 |
4 |
| SV* result = new(classname);
|
| 64 |
4 |
| gpc_polygon* r = (gpc_polygon*) SvIV(SvRV(result));
|
| 65 |
|
| gpc_op op;
|
| 66 |
|
| gpc_polygon* c;
|
| 67 |
4 |
▶ | if(! sv_isobject(clp)) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if(! sv_isobject(clp)) {
|
0 |
4 |
4 |
|
| 68 |
0 |
| croak("not an object");
|
| 69 |
|
| }
|
| 70 |
4 |
▶ | if(! sv_isa(clp, classname)) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if(! sv_isa(clp, classname)) {
|
0 |
4 |
4 |
|
| 71 |
0 |
| croak("not a member of %s", classname);
|
| 72 |
|
| }
|
| 73 |
4 |
| c = (gpc_polygon*) SvIV(SvRV(clp));
|
| 74 |
4 |
▶ | if(! strcmp(action, "INTERSECT")) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if(! strcmp(action, "INTERSECT")) {
|
2 |
2 |
4 |
|
| 75 |
|
| dbg_p(("performing INTERSECT\n"));
|
| 76 |
2 |
| op = GPC_INT;
|
| 77 |
|
| }
|
| 78 |
4 |
▶ | if(! strcmp(action, "DIFFERENCE")) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if(! strcmp(action, "DIFFERENCE")) {
|
1 |
3 |
4 |
|
| 79 |
|
| dbg_p(("performing DIFFERENCE\n"));
|
| 80 |
1 |
| op = GPC_DIFF;
|
| 81 |
|
| }
|
| 82 |
4 |
▶ | if(! strcmp(action, "UNION")) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if(! strcmp(action, "UNION")) {
|
1 |
3 |
4 |
|
| 83 |
|
| dbg_p(("performing UNION\n"));
|
| 84 |
1 |
| op = GPC_UNION;
|
| 85 |
|
| }
|
| 86 |
|
| /* FIXME: need some way to integrate this:
|
| 87 |
|
| printf("%s is not an operation".
|
| 88 |
|
| " (INTERSECT|DIFFERENCE|UNION)\n", action
|
| 89 |
|
| );
|
| 90 |
|
| */
|
| 91 |
4 |
| gpc_polygon_clip(op, p, c, r);
|
| 92 |
4 |
| return(result);
|
| 93 |
|
| }
|
| 94 |
|
|
|
| 95 |
|
|
|
| 96 |
5 |
| void add_polygon(SV* obj, SV* pg, int hole) {
|
| 97 |
5 |
| gpc_polygon* p = (gpc_polygon*) SvIV(SvRV(obj));
|
| 98 |
|
| dbg_p(("got my vl\n"));
|
| 99 |
5 |
▶ | if(p->num_contours > 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if(p->num_contours > 0) {
|
4 |
1 |
5 |
|
| 100 |
|
| gpc_vertex_list* c;
|
| 101 |
4 |
▶ | MALLOC(c, sizeof(gpc_vertex_list),
|
Branch: 1 branch
| Branch | True | False | Total |
| MALLOC(c, sizeof(gpc_vertex_list),
|
0 |
4 |
4 |
|
| 102 |
|
| "addable contour creation\n",gpc_vertex_list);
|
| 103 |
4 |
| pts_to_vertex_list(pg, c);
|
| 104 |
|
| dbg_p(("adding to existing\n"));
|
| 105 |
4 |
| gpc_add_contour(p, c, hole);
|
| 106 |
|
| }
|
| 107 |
|
| else {
|
| 108 |
|
| dbg_p(("adding as new\n"));
|
| 109 |
1 |
▶ | MALLOC(p->hole, sizeof(int), "hole flag array\n",int);
|
Branch: 1 branch
| Branch | True | False | Total |
| MALLOC(p->hole, sizeof(int), "hole flag array\n",int);
|
0 |
1 |
1 |
|
| 110 |
|
| dbg_p(("setting hole\n"));
|
| 111 |
1 |
| p->hole[0] = hole;
|
| 112 |
|
| dbg_p(("making contour\n"));
|
| 113 |
1 |
▶ | MALLOC(p->contour, sizeof(gpc_vertex_list),
|
Branch: 1 branch
| Branch | True | False | Total |
| MALLOC(p->contour, sizeof(gpc_vertex_list),
|
0 |
1 |
1 |
|
| 114 |
|
| "contour creation\n",gpc_vertex_list);
|
| 115 |
1 |
| pts_to_vertex_list(pg, &(p->contour[0]) );
|
| 116 |
|
| dbg_p(("got %d vertices\n", p->contour[0].num_vertices));
|
| 117 |
1 |
| p->num_contours = 1;
|
| 118 |
|
| }
|
| 119 |
|
| dbg_p(("added\n"));
|
| 120 |
5 |
| }
|
| 121 |
|
|
|
| 122 |
5 |
| void pts_to_vertex_list(SV* pg, gpc_vertex_list* vl) {
|
| 123 |
|
| SV** psv;
|
| 124 |
|
| SV* val;
|
| 125 |
|
| AV* pt;
|
| 126 |
|
| AV* pts;
|
| 127 |
|
| I32 p;
|
| 128 |
|
| I32 num;
|
| 129 |
5 |
▶ | if(!SvROK(pg))
|
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvROK(pg))
|
0 |
5 |
5 |
|
| 130 |
0 |
| croak("polygon must be reference\n");
|
| 131 |
5 |
| pts = (AV*)SvRV(pg);
|
| 132 |
5 |
| num = av_len(pts) + 1;
|
| 133 |
|
| dbg_p(("going to allocate for %d pts\n", num));
|
| 134 |
5 |
▶ | MALLOC(vl->vertex, num * sizeof(gpc_vertex), "vertex creation",gpc_vertex);
|
Branch: 2 branches
| Branch | True | False | Total |
| MALLOC(vl->vertex, num * sizeof(gpc_vertex), "vertex creation",gpc_vertex);
|
4 |
1 |
5 |
| MALLOC(vl->vertex, num * sizeof(gpc_vertex), "vertex creation",gpc_vertex);
|
0 |
4 |
4 |
|
| 135 |
5 |
| vl->num_vertices = num;
|
| 136 |
|
| dbg_p(("MALLOC okay (%d vertices)\n", vl->num_vertices));
|
| 137 |
26 |
▶ | for(p = 0; p < num; p++) {
|
Branch: 1 branch
| Branch | True | False | Total |
| for(p = 0; p < num; p++) {
|
21 |
5 |
26 |
|
| 138 |
21 |
| psv = av_fetch(pts, p, 0);
|
| 139 |
21 |
| val = *psv;
|
| 140 |
21 |
▶ | if(! SvROK(val))
|
Branch: 1 branch
| Branch | True | False | Total |
| if(! SvROK(val))
|
0 |
21 |
21 |
|
| 141 |
0 |
| croak("point %d not a ref", p);
|
| 142 |
21 |
| pt = (AV*)SvRV(val);
|
| 143 |
21 |
| psv = av_fetch(pt, 0, 0);
|
| 144 |
21 |
| val = *psv;
|
| 145 |
21 |
| vl->vertex[p].x = SvNV(val);
|
| 146 |
21 |
| psv = av_fetch(pt, 1, 0);
|
| 147 |
21 |
| val = *psv;
|
| 148 |
21 |
| vl->vertex[p].y = SvNV(val);
|
| 149 |
|
| dbg_p(("added %0.2f, %0.2f\n",
|
| 150 |
|
| vl->vertex[p].x, vl->vertex[p].y
|
| 151 |
|
| ));
|
| 152 |
|
| }
|
| 153 |
|
| dbg_p(("returning\n"));
|
| 154 |
5 |
| }
|
| 155 |
|
|
|
| 156 |
10 |
| AV* vertex_list_to_pts(gpc_vertex_list* vl) {
|
| 157 |
|
| AV* pts;
|
| 158 |
|
| AV* pt;
|
| 159 |
|
| int p;
|
| 160 |
|
| dbg_p(("%d vertices\n", vl->num_vertices));
|
| 161 |
10 |
| pts = newAV();
|
| 162 |
68 |
▶ | for(p = 0; p < vl->num_vertices; p++) {
|
Branch: 1 branch
| Branch | True | False | Total |
| for(p = 0; p < vl->num_vertices; p++) {
|
58 |
10 |
68 |
|
| 163 |
58 |
| pt = newAV();
|
| 164 |
58 |
| av_push(pts, newRV_noinc((SV*) pt));
|
| 165 |
58 |
| av_push(pt, newSVnv(vl->vertex[p].x));
|
| 166 |
58 |
| av_push(pt, newSVnv(vl->vertex[p].y));
|
| 167 |
|
| dbg_p(("point %d: %0.2f, %0.2f\n",
|
| 168 |
|
| p, vl->vertex[p].x, vl->vertex[p].y
|
| 169 |
|
| ));
|
| 170 |
|
| }
|
| 171 |
10 |
| return(pts);
|
| 172 |
|
| }
|
| 173 |
|
|
|
| 174 |
|
|
|
| 175 |
7 |
| void gpc_free_polygon2(gpc_polygon *p) {
|
| 176 |
|
| int c;
|
| 177 |
27 |
▶ | for (c= 0; c < p->num_contours; c++) {
|
Branch: 1 branch
| Branch | True | False | Total |
| for (c= 0; c < p->num_contours; c++) {
|
20 |
7 |
27 |
|
| 178 |
|
| dbg_p(("free contour %d\n", c));
|
| 179 |
20 |
▶ | FREE(p->contour[c].vertex);
|
Branch: 1 branch
| Branch | True | False | Total |
| FREE(p->contour[c].vertex);
|
19 |
1 |
20 |
|
| 180 |
|
| }
|
| 181 |
|
| dbg_p(("free hole\n"));
|
| 182 |
7 |
▶ | FREE(p->hole);
|
Branch: 1 branch
| Branch | True | False | Total |
| FREE(p->hole);
|
7 |
0 |
7 |
|
| 183 |
|
| dbg_p(("free contour\n"));
|
| 184 |
7 |
▶ | FREE(p->contour);
|
Branch: 1 branch
| Branch | True | False | Total |
| FREE(p->contour);
|
7 |
0 |
7 |
|
| 185 |
7 |
| p->num_contours= 0;
|
| 186 |
7 |
| }
|
| 187 |
|
|
|
| 188 |
|
|
|