-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpected_c.txt
executable file
·322 lines (288 loc) · 7.34 KB
/
expected_c.txt
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "INLINE.h"
//file: Polygon/functions.c
/*
Copyright 2004 Eric L. Wilhelm
GPL / Artistic License
See Polygon.pm for details
Version 0.04
*/
#include "gpc.h"
#include "gpc.c"
/*#define DEBUG_PRINT*/
#ifdef DEBUG_PRINT
#define dbg_p(x) printf x
#else
#define dbg_p(x)
#endif
SV* new (char* class);
void remove_add_polygon(SV* obj, SV* pg, int hole);
void DESTROY(SV* obj);
int from_file(SV* obj, char* filename, int want_hole);
void to_file(SV* obj, char* filename, int want_hole);
SV* clip_to(SV* obj, SV* clp, char* action);
void remove_add_polygon(SV* obj, SV* pg, int hole);
void get_polygons(SV* obj);
void pts_to_vertex_list(SV* pg, gpc_vertex_list* vl);
AV* vertex_list_to_pts(gpc_vertex_list* vl);
void gpc_free_polygon2(gpc_polygon *p);
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 remove_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");
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");
dbg_p(("setting hole\n"));
p->hole[0] = hole;
dbg_p(("making contour\n"));
MALLOC(p->contour, sizeof(gpc_vertex_list),
"contour creation\n");
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 get_polygons(SV* obj) {
Inline_Stack_Vars;
int c;
gpc_polygon* p = (gpc_polygon*) SvIV(SvRV(obj));
Inline_Stack_Reset;
if(p->num_contours < 1) {
dbg_p(("no contours\n"));
Inline_Stack_Done;
return;
}
for(c = 0; c < p->num_contours; c++) {
Inline_Stack_Push(newRV_noinc((SV*) vertex_list_to_pts(&(p->contour[c]))));
}
Inline_Stack_Done;
}
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");
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;
}
MODULE = Math::Geometry::Planar::GPC::Polygon PACKAGE = Math::Geometry::Planar::GPC::Polygon PREFIX = remove_
PROTOTYPES: DISABLE
SV *
new (class)
char * class
void
DESTROY (obj)
SV * obj
PREINIT:
I32* temp;
PPCODE:
temp = PL_markstack_ptr++;
DESTROY(obj);
if (PL_markstack_ptr != temp) {
/* truly void, because dXSARGS not invoked */
PL_markstack_ptr = temp;
XSRETURN_EMPTY; /* return empty stack */
}
/* must have used dXSARGS; list context implied */
return; /* assume stack size is correct */
int
from_file (obj, filename, want_hole)
SV * obj
char * filename
int want_hole
void
to_file (obj, filename, want_hole)
SV * obj
char * filename
int want_hole
PREINIT:
I32* temp;
PPCODE:
temp = PL_markstack_ptr++;
to_file(obj, filename, want_hole);
if (PL_markstack_ptr != temp) {
/* truly void, because dXSARGS not invoked */
PL_markstack_ptr = temp;
XSRETURN_EMPTY; /* return empty stack */
}
/* must have used dXSARGS; list context implied */
return; /* assume stack size is correct */
SV *
clip_to (obj, clp, action)
SV * obj
SV * clp
char * action
void
remove_add_polygon (obj, pg, hole)
SV * obj
SV * pg
int hole
PREINIT:
I32* temp;
PPCODE:
temp = PL_markstack_ptr++;
remove_add_polygon(obj, pg, hole);
if (PL_markstack_ptr != temp) {
/* truly void, because dXSARGS not invoked */
PL_markstack_ptr = temp;
XSRETURN_EMPTY; /* return empty stack */
}
/* must have used dXSARGS; list context implied */
return; /* assume stack size is correct */
void
get_polygons (obj)
SV * obj
PREINIT:
I32* temp;
PPCODE:
temp = PL_markstack_ptr++;
get_polygons(obj);
if (PL_markstack_ptr != temp) {
/* truly void, because dXSARGS not invoked */
PL_markstack_ptr = temp;
XSRETURN_EMPTY; /* return empty stack */
}
/* must have used dXSARGS; list context implied */
return; /* assume stack size is correct */
BOOT:
printf("Hi from bootstrap\n");