forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_path_wrapper.cpp
720 lines (604 loc) · 22.2 KB
/
_path_wrapper.cpp
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include "numpy_cpp.h"
#include "_path.h"
#include "py_converters.h"
#include "py_adaptors.h"
PyObject *convert_polygon_vector(std::vector<Polygon> &polygons)
{
PyObject *pyresult = PyList_New(polygons.size());
for (size_t i = 0; i < polygons.size(); ++i) {
Polygon poly = polygons[i];
npy_intp dims[] = {(npy_intp)poly.size() + 1, 2 };
numpy::array_view<double, 2> subresult(dims);
/* Make last point same as first. */
memcpy(subresult.data(), &poly[0], sizeof(double) * poly.size() * 2);
subresult(poly.size(), 0) = poly[0].x;
subresult(poly.size(), 1) = poly[0].y;
if (PyList_SetItem(pyresult, i, subresult.pyobj())) {
Py_DECREF(pyresult);
return NULL;
}
}
return pyresult;
}
const char *Py_point_in_path__doc__ = "point_in_path(x, y, radius, path, trans)";
static PyObject *Py_point_in_path(PyObject *self, PyObject *args, PyObject *kwds)
{
double x, y, r;
py::PathIterator path;
agg::trans_affine trans;
bool result;
if (!PyArg_ParseTuple(args,
"dddO&O&:point_in_path",
&x,
&y,
&r,
&convert_path,
&path,
&convert_trans_affine,
&trans)) {
return NULL;
}
CALL_CPP("point_in_path", (result = point_in_path(x, y, r, path, trans)));
if (result) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
const char *Py_points_in_path__doc__ = "points_in_path(points, radius, path, trans)";
static PyObject *Py_points_in_path(PyObject *self, PyObject *args, PyObject *kwds)
{
numpy::array_view<const double, 2> points;
double r;
py::PathIterator path;
agg::trans_affine trans;
if (!PyArg_ParseTuple(args,
"O&dO&O&:points_in_path",
&points.converter,
&points,
&r,
&convert_path,
&path,
&convert_trans_affine,
&trans)) {
return NULL;
}
npy_intp dims[] = { points.dim(0) };
numpy::array_view<bool, 1> results(dims);
CALL_CPP("points_in_path", (points_in_path(points, r, path, trans, results)));
return results.pyobj();
}
const char *Py_point_on_path__doc__ = "point_on_path(x, y, radius, path, trans)";
static PyObject *Py_point_on_path(PyObject *self, PyObject *args, PyObject *kwds)
{
double x, y, r;
py::PathIterator path;
agg::trans_affine trans;
bool result;
if (!PyArg_ParseTuple(args,
"dddO&O&:point_on_path",
&x,
&y,
&r,
&convert_path,
&path,
&convert_trans_affine,
&trans)) {
return NULL;
}
CALL_CPP("point_on_path", (result = point_on_path(x, y, r, path, trans)));
if (result) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
const char *Py_points_on_path__doc__ = "points_on_path(points, radius, path, trans)";
static PyObject *Py_points_on_path(PyObject *self, PyObject *args, PyObject *kwds)
{
numpy::array_view<const double, 2> points;
double r;
py::PathIterator path;
agg::trans_affine trans;
if (!PyArg_ParseTuple(args,
"O&dO&O&:points_on_path",
&points.converter,
&points,
&r,
&convert_path,
&path,
&convert_trans_affine,
&trans)) {
return NULL;
}
npy_intp dims[] = { points.dim(0) };
numpy::array_view<bool, 1> results(dims);
CALL_CPP("points_on_path", (points_on_path(points, r, path, trans, results)));
return results.pyobj();
}
const char *Py_get_path_extents__doc__ = "get_path_extents(path, trans)";
static PyObject *Py_get_path_extents(PyObject *self, PyObject *args, PyObject *kwds)
{
py::PathIterator path;
agg::trans_affine trans;
if (!PyArg_ParseTuple(
args, "O&O&:get_path_extents", &convert_path, &path, &convert_trans_affine, &trans)) {
return NULL;
}
extent_limits e;
CALL_CPP("get_path_extents", (reset_limits(e)));
CALL_CPP("get_path_extents", (update_path_extents(path, trans, e)));
npy_intp dims[] = { 2, 2 };
numpy::array_view<double, 2> extents(dims);
extents(0, 0) = e.x0;
extents(0, 1) = e.y0;
extents(1, 0) = e.x1;
extents(1, 1) = e.y1;
return extents.pyobj();
}
const char *Py_update_path_extents__doc__ =
"update_path_extents(path, trans, rect, minpos, ignore)";
static PyObject *Py_update_path_extents(PyObject *self, PyObject *args, PyObject *kwds)
{
py::PathIterator path;
agg::trans_affine trans;
agg::rect_d rect;
numpy::array_view<double, 1> minpos;
int ignore;
int changed;
if (!PyArg_ParseTuple(args,
"O&O&O&O&i:update_path_extents",
&convert_path,
&path,
&convert_trans_affine,
&trans,
&convert_rect,
&rect,
&minpos.converter,
&minpos,
&ignore)) {
return NULL;
}
if (minpos.dim(0) != 2) {
PyErr_SetString(PyExc_ValueError, "minpos must be of length 2");
}
extent_limits e;
if (ignore) {
CALL_CPP("update_path_extents", reset_limits(e));
} else {
if (rect.x1 > rect.x2) {
e.x0 = std::numeric_limits<double>::infinity();
e.x1 = -std::numeric_limits<double>::infinity();
} else {
e.x0 = rect.x1;
e.x1 = rect.x2;
}
if (rect.y1 > rect.y2) {
e.y0 = std::numeric_limits<double>::infinity();
e.y1 = -std::numeric_limits<double>::infinity();
} else {
e.y0 = rect.y1;
e.y1 = rect.y2;
}
e.xm = minpos(0);
e.ym = minpos(1);
}
CALL_CPP("update_path_extents", (update_path_extents(path, trans, e)));
changed = (e.x0 != rect.x1 || e.y0 != rect.y1 || e.x1 != rect.x2 || e.y1 != rect.y2 ||
e.xm != minpos(0) || e.ym != minpos(1));
npy_intp extentsdims[] = { 2, 2 };
numpy::array_view<double, 2> outextents(extentsdims);
outextents(0, 0) = e.x0;
outextents(0, 1) = e.y0;
outextents(1, 0) = e.x1;
outextents(1, 1) = e.y1;
npy_intp minposdims[] = { 2 };
numpy::array_view<double, 1> outminpos(minposdims);
outminpos(0) = e.xm;
outminpos(1) = e.ym;
return Py_BuildValue(
"NNi", outextents.pyobj(), outminpos.pyobj(), changed);
}
const char *Py_get_path_collection_extents__doc__ = "get_path_collection_extents(";
static PyObject *Py_get_path_collection_extents(PyObject *self, PyObject *args, PyObject *kwds)
{
agg::trans_affine master_transform;
PyObject *pathsobj;
numpy::array_view<const double, 3> transforms;
numpy::array_view<const double, 2> offsets;
agg::trans_affine offset_trans;
extent_limits e;
if (!PyArg_ParseTuple(args,
"O&OO&O&O&:get_path_collection_extents",
&convert_trans_affine,
&master_transform,
&pathsobj,
&transforms.converter,
&transforms,
&offsets.converter,
&offsets,
&convert_trans_affine,
&offset_trans)) {
return NULL;
}
try
{
py::PathGenerator paths(pathsobj);
CALL_CPP("get_path_collection_extents",
(get_path_collection_extents(
master_transform, paths, transforms, offsets, offset_trans, e)));
}
catch (py::exception &e)
{
return NULL;
}
npy_intp dims[] = { 2, 2 };
numpy::array_view<double, 2> extents(dims);
extents(0, 0) = e.x0;
extents(0, 1) = e.y0;
extents(1, 0) = e.x1;
extents(1, 1) = e.y1;
return extents.pyobj();
}
const char *Py_point_in_path_collection__doc__ =
"point_in_path_collection(x, y, radius, master_transform, paths, transforms, offsets, "
"offset_trans, filled, offset_position)";
static PyObject *Py_point_in_path_collection(PyObject *self, PyObject *args, PyObject *kwds)
{
double x, y, radius;
agg::trans_affine master_transform;
PyObject *pathsobj;
numpy::array_view<const double, 3> transforms;
numpy::array_view<const double, 2> offsets;
agg::trans_affine offset_trans;
int filled;
e_offset_position offset_position;
std::vector<int> result;
if (!PyArg_ParseTuple(args,
"dddO&OO&O&O&iO&:point_in_path_collection",
&x,
&y,
&radius,
&convert_trans_affine,
&master_transform,
&pathsobj,
&transforms.converter,
&transforms,
&offsets.converter,
&offsets,
&convert_trans_affine,
&offset_trans,
&filled,
&convert_offset_position,
&offset_position)) {
return NULL;
}
try
{
py::PathGenerator paths(pathsobj);
CALL_CPP("point_in_path_collection",
(point_in_path_collection(x,
y,
radius,
master_transform,
paths,
transforms,
offsets,
offset_trans,
filled,
offset_position,
result)));
}
catch (py::exception &e)
{
return NULL;
}
npy_intp dims[] = {(npy_intp)result.size() };
numpy::array_view<int, 1> pyresult(dims);
memcpy(pyresult.data(), &result[0], result.size() * sizeof(int));
return pyresult.pyobj();
}
const char *Py_path_in_path__doc__ = "path_in_path(path_a, trans_a, path_b, trans_b)";
static PyObject *Py_path_in_path(PyObject *self, PyObject *args, PyObject *kwds)
{
py::PathIterator a;
agg::trans_affine atrans;
py::PathIterator b;
agg::trans_affine btrans;
bool result;
if (!PyArg_ParseTuple(args,
"O&O&O&O&:path_in_path",
&convert_path,
&a,
&convert_trans_affine,
&atrans,
&convert_path,
&b,
&convert_trans_affine,
&btrans)) {
return NULL;
}
CALL_CPP("path_in_path", (result = path_in_path(a, atrans, b, btrans)));
if (result) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
const char *Py_clip_path_to_rect__doc__ = "clip_path_to_rect(path, rect, inside)";
static PyObject *Py_clip_path_to_rect(PyObject *self, PyObject *args, PyObject *kwds)
{
py::PathIterator path;
agg::rect_d rect;
int inside;
std::vector<Polygon> result;
if (!PyArg_ParseTuple(args,
"O&O&i:clip_path_to_rect",
&convert_path,
&path,
&convert_rect,
&rect,
&inside)) {
return NULL;
}
CALL_CPP("clip_path_to_rect", (clip_path_to_rect(path, rect, inside, result)));
return convert_polygon_vector(result);
}
const char *Py_affine_transform__doc__ = "affine_transform(points, trans)";
static PyObject *Py_affine_transform(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *vertices_obj;
agg::trans_affine trans;
if (!PyArg_ParseTuple(args,
"OO&:affine_transform",
&vertices_obj,
&convert_trans_affine,
&trans)) {
return NULL;
}
try {
numpy::array_view<double, 2> vertices(vertices_obj);
npy_intp dims[] = { vertices.dim(0), 2 };
numpy::array_view<double, 2> result(dims);
CALL_CPP("affine_transform", (affine_transform_2d(vertices, trans, result)));
return result.pyobj();
} catch (py::exception) {
numpy::array_view<double, 1> vertices(vertices_obj);
npy_intp dims[] = { vertices.dim(0) };
numpy::array_view<double, 1> result(dims);
CALL_CPP("affine_transform", (affine_transform_1d(vertices, trans, result)));
return result.pyobj();
}
}
const char *Py_count_bboxes_overlapping_bbox__doc__ = "count_bboxes_overlapping_bbox(bbox, bboxes)";
static PyObject *Py_count_bboxes_overlapping_bbox(PyObject *self, PyObject *args, PyObject *kwds)
{
agg::rect_d bbox;
numpy::array_view<const double, 3> bboxes;
int result;
if (!PyArg_ParseTuple(args,
"O&O&:count_bboxes_overlapping_bbox",
&convert_rect,
&bbox,
&bboxes.converter,
&bboxes)) {
return NULL;
}
CALL_CPP("count_bboxes_overlapping_bbox",
(result = count_bboxes_overlapping_bbox(bbox, bboxes)));
return PyLong_FromLong(result);
}
const char *Py_path_intersects_path__doc__ = "path_intersects_path(path1, path2, filled=False)";
static PyObject *Py_path_intersects_path(PyObject *self, PyObject *args, PyObject *kwds)
{
py::PathIterator p1;
py::PathIterator p2;
agg::trans_affine t1;
agg::trans_affine t2;
int filled = 0;
const char *names[] = { "p1", "p2", "filled", NULL };
bool result;
if (!PyArg_ParseTupleAndKeywords(args,
kwds,
"O&O&i:path_intersects_path",
(char **)names,
&convert_path,
&p1,
&convert_path,
&p2,
&filled)) {
return NULL;
}
CALL_CPP("path_intersects_path", (result = path_intersects_path(p1, p2)));
if (filled) {
if (!result) {
CALL_CPP("path_intersects_path",
(result = path_in_path(p1, t1, p2, t2)));
}
if (!result) {
CALL_CPP("path_intersects_path",
(result = path_in_path(p2, t1, p1, t2)));
}
}
if (result) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
const char *Py_convert_path_to_polygons__doc__ =
"convert_path_to_polygons(path, trans, width=0, height=0)";
static PyObject *Py_convert_path_to_polygons(PyObject *self, PyObject *args, PyObject *kwds)
{
py::PathIterator path;
agg::trans_affine trans;
double width = 0.0, height = 0.0;
std::vector<Polygon> result;
if (!PyArg_ParseTuple(args,
"O&O&|dd:convert_path_to_polygons",
&convert_path,
&path,
&convert_trans_affine,
&trans,
&width,
&height)) {
return NULL;
}
CALL_CPP("convert_path_to_polygons",
(convert_path_to_polygons(path, trans, width, height, result)));
return convert_polygon_vector(result);
}
const char *Py_cleanup_path__doc__ =
"cleanup_path(path, trans, remove_nans, clip_rect, snap_mode, stroke_width, simplify, "
"return_curves, sketch)";
static PyObject *Py_cleanup_path(PyObject *self, PyObject *args, PyObject *kwds)
{
py::PathIterator path;
agg::trans_affine trans;
int remove_nans;
agg::rect_d clip_rect;
e_snap_mode snap_mode;
double stroke_width;
PyObject *simplifyobj;
bool simplify = false;
int return_curves;
SketchParams sketch;
if (!PyArg_ParseTuple(args,
"O&O&iO&O&dOiO&:cleanup_path",
&convert_path,
&path,
&convert_trans_affine,
&trans,
&remove_nans,
&convert_rect,
&clip_rect,
&convert_snap,
&snap_mode,
&stroke_width,
&simplifyobj,
&return_curves,
&convert_sketch_params,
&sketch)) {
return NULL;
}
if (simplifyobj == Py_None) {
simplify = path.should_simplify();
} else if (PyObject_IsTrue(simplifyobj)) {
simplify = true;
}
bool do_clip = (clip_rect.x1 < clip_rect.x2 && clip_rect.y1 < clip_rect.y2);
std::vector<double> vertices;
std::vector<npy_uint8> codes;
CALL_CPP("cleanup_path",
(cleanup_path(path,
trans,
remove_nans,
do_clip,
clip_rect,
snap_mode,
stroke_width,
simplify,
return_curves,
sketch,
vertices,
codes)));
size_t length = codes.size();
npy_intp vertices_dims[] = {(npy_intp)length, 2 };
numpy::array_view<double, 2> pyvertices(vertices_dims);
npy_intp codes_dims[] = {(npy_intp)length };
numpy::array_view<unsigned char, 1> pycodes(codes_dims);
memcpy(pyvertices.data(), &vertices[0], sizeof(double) * 2 * length);
memcpy(pycodes.data(), &codes[0], sizeof(unsigned char) * length);
return Py_BuildValue("NN", pyvertices.pyobj(), pycodes.pyobj());
}
const char *Py_convert_to_svg__doc__ = "convert_to_svg(path, trans, cliprect, simplify, precision)";
static PyObject *Py_convert_to_svg(PyObject *self, PyObject *args, PyObject *kwds)
{
py::PathIterator path;
agg::trans_affine trans;
agg::rect_d cliprect;
PyObject *simplifyobj;
bool simplify = false;
int precision;
if (!PyArg_ParseTuple(args,
"O&O&O&Oi:convert_to_svg",
&convert_path,
&path,
&convert_trans_affine,
&trans,
&convert_rect,
&cliprect,
&simplifyobj,
&precision)) {
return NULL;
}
if (simplifyobj == Py_None) {
simplify = path.should_simplify();
} else if (PyObject_IsTrue(simplifyobj)) {
simplify = true;
}
size_t buffersize = path.total_vertices() * (precision + 5) * 4;
std::string buffer;
buffer.reserve(buffersize);
CALL_CPP("convert_to_svg",
(convert_to_svg(path, trans, cliprect, simplify, precision, &buffer[0], &buffersize)));
return PyUnicode_DecodeASCII(&buffer[0], buffersize, "");
}
extern "C" {
static PyMethodDef module_functions[] = {
{"point_in_path", (PyCFunction)Py_point_in_path, METH_VARARGS, Py_point_in_path__doc__},
{"points_in_path", (PyCFunction)Py_points_in_path, METH_VARARGS, Py_points_in_path__doc__},
{"point_on_path", (PyCFunction)Py_point_on_path, METH_VARARGS, Py_point_on_path__doc__},
{"points_on_path", (PyCFunction)Py_points_on_path, METH_VARARGS, Py_points_on_path__doc__},
{"get_path_extents", (PyCFunction)Py_get_path_extents, METH_VARARGS, Py_get_path_extents__doc__},
{"update_path_extents", (PyCFunction)Py_update_path_extents, METH_VARARGS, Py_update_path_extents__doc__},
{"get_path_collection_extents", (PyCFunction)Py_get_path_collection_extents, METH_VARARGS, Py_get_path_collection_extents__doc__},
{"point_in_path_collection", (PyCFunction)Py_point_in_path_collection, METH_VARARGS, Py_point_in_path_collection__doc__},
{"path_in_path", (PyCFunction)Py_path_in_path, METH_VARARGS, Py_path_in_path__doc__},
{"clip_path_to_rect", (PyCFunction)Py_clip_path_to_rect, METH_VARARGS, Py_clip_path_to_rect__doc__},
{"affine_transform", (PyCFunction)Py_affine_transform, METH_VARARGS, Py_affine_transform__doc__},
{"count_bboxes_overlapping_bbox", (PyCFunction)Py_count_bboxes_overlapping_bbox, METH_VARARGS, Py_count_bboxes_overlapping_bbox__doc__},
{"path_intersects_path", (PyCFunction)Py_path_intersects_path, METH_VARARGS|METH_KEYWORDS, Py_path_intersects_path__doc__},
{"convert_path_to_polygons", (PyCFunction)Py_convert_path_to_polygons, METH_VARARGS, Py_convert_path_to_polygons__doc__},
{"cleanup_path", (PyCFunction)Py_cleanup_path, METH_VARARGS, Py_cleanup_path__doc__},
{"convert_to_svg", (PyCFunction)Py_convert_to_svg, METH_VARARGS, Py_convert_to_svg__doc__},
{NULL}
};
struct module_state
{
int _dummy;
};
#if PY3K
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_path",
NULL,
sizeof(struct module_state),
module_functions,
NULL,
NULL,
NULL,
NULL
};
#define INITERROR return NULL
PyMODINIT_FUNC PyInit__path(void)
#else
#define INITERROR return
PyMODINIT_FUNC init_path(void)
#endif
{
PyObject *m;
#if PY3K
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule3("_path", module_functions, NULL);
#endif
if (m == NULL) {
INITERROR;
}
import_array();
#if PY3K
return m;
#endif
}
}