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