forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_path.cpp
1767 lines (1526 loc) · 52.1 KB
/
_path.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c++; c-basic-offset: 4 -*- */
#include "agg_py_path_iterator.h"
#include "agg_py_transforms.h"
#include "path_converters.h"
#include <limits>
#include <math.h>
#include "CXX/Extensions.hxx"
#include "agg_conv_contour.h"
#include "agg_conv_curve.h"
#include "agg_conv_stroke.h"
#include "agg_conv_transform.h"
#include "agg_path_storage.h"
#include "agg_trans_affine.h"
struct XY
{
double x;
double y;
XY(double x_, double y_) : x(x_), y(y_) {}
};
// the extension module
class _path_module : public Py::ExtensionModule<_path_module>
{
public:
_path_module()
: Py::ExtensionModule<_path_module>("_path")
{
add_varargs_method("point_in_path", &_path_module::point_in_path,
"point_in_path(x, y, path, trans)");
add_varargs_method("points_in_path", &_path_module::points_in_path,
"points_in_path(points, path, trans)");
add_varargs_method("point_on_path", &_path_module::point_on_path,
"point_on_path(x, y, r, path, trans)");
add_varargs_method("get_path_extents", &_path_module::get_path_extents,
"get_path_extents(path, trans)");
add_varargs_method("update_path_extents", &_path_module::update_path_extents,
"update_path_extents(path, trans, bbox, minpos)");
add_varargs_method("get_path_collection_extents", &_path_module::get_path_collection_extents,
"get_path_collection_extents(trans, paths, transforms, offsets, offsetTrans)");
add_varargs_method("point_in_path_collection", &_path_module::point_in_path_collection,
"point_in_path_collection(x, y, r, trans, paths, transforms, offsets, offsetTrans, filled)");
add_varargs_method("path_in_path", &_path_module::path_in_path,
"path_in_path(a, atrans, b, btrans)");
add_varargs_method("clip_path_to_rect", &_path_module::clip_path_to_rect,
"clip_path_to_rect(path, bbox, inside)");
add_varargs_method("affine_transform", &_path_module::affine_transform,
"affine_transform(vertices, transform)");
add_varargs_method("count_bboxes_overlapping_bbox", &_path_module::count_bboxes_overlapping_bbox,
"count_bboxes_overlapping_bbox(bbox, bboxes)");
add_varargs_method("path_intersects_path", &_path_module::path_intersects_path,
"path_intersects_path(p1, p2)");
add_varargs_method("convert_path_to_polygons", &_path_module::convert_path_to_polygons,
"convert_path_to_polygons(path, trans, width, height)");
add_varargs_method("cleanup_path", &_path_module::cleanup_path,
"cleanup_path(path, trans, remove_nans, clip, snap, simplify, curves, sketch_params)");
add_varargs_method("convert_to_svg", &_path_module::convert_to_svg,
"convert_to_svg(path, trans, clip, simplify, precision)");
initialize("Helper functions for paths");
}
virtual ~_path_module() {}
private:
Py::Object point_in_path(const Py::Tuple& args);
Py::Object points_in_path(const Py::Tuple& args);
Py::Object point_on_path(const Py::Tuple& args);
Py::Object get_path_extents(const Py::Tuple& args);
Py::Object update_path_extents(const Py::Tuple& args);
Py::Object get_path_collection_extents(const Py::Tuple& args);
Py::Object point_in_path_collection(const Py::Tuple& args);
Py::Object path_in_path(const Py::Tuple& args);
Py::Object clip_path_to_rect(const Py::Tuple& args);
Py::Object affine_transform(const Py::Tuple& args);
Py::Object count_bboxes_overlapping_bbox(const Py::Tuple& args);
Py::Object path_intersects_path(const Py::Tuple& args);
Py::Object convert_path_to_polygons(const Py::Tuple& args);
Py::Object cleanup_path(const Py::Tuple& args);
Py::Object convert_to_svg(const Py::Tuple& args);
};
//
// The following function was found in the Agg 2.3 examples (interactive_polygon.cpp).
// It has been generalized to work on (possibly curved) polylines, rather than
// just polygons. The original comments have been kept intact.
// -- Michael Droettboom 2007-10-02
//
//======= Crossings Multiply algorithm of InsideTest ========================
//
// By Eric Haines, 3D/Eye Inc, [email protected]
//
// This version is usually somewhat faster than the original published in
// Graphics Gems IV; by turning the division for testing the X axis crossing
// into a tricky multiplication test this part of the test became faster,
// which had the additional effect of making the test for "both to left or
// both to right" a bit slower for triangles than simply computing the
// intersection each time. The main increase is in triangle testing speed,
// which was about 15% faster; all other polygon complexities were pretty much
// the same as before. On machines where division is very expensive (not the
// case on the HP 9000 series on which I tested) this test should be much
// faster overall than the old code. Your mileage may (in fact, will) vary,
// depending on the machine and the test data, but in general I believe this
// code is both shorter and faster. This test was inspired by unpublished
// Graphics Gems submitted by Joseph Samosky and Mark Haigh-Hutchinson.
// Related work by Samosky is in:
//
// Samosky, Joseph, "SectionView: A system for interactively specifying and
// visualizing sections through three-dimensional medical image data",
// M.S. Thesis, Department of Electrical Engineering and Computer Science,
// Massachusetts Institute of Technology, 1993.
//
// Shoot a test ray along +X axis. The strategy is to compare vertex Y values
// to the testing point's Y and quickly discard edges which are entirely to one
// side of the test ray. Note that CONVEX and WINDING code can be added as
// for the CrossingsTest() code; it is left out here for clarity.
//
// Input 2D polygon _pgon_ with _numverts_ number of vertices and test point
// _point_, returns 1 if inside, 0 if outside.
template<class T>
static void
point_in_path_impl(const void* const points_, const size_t s0,
const size_t s1, const size_t n, T& path,
npy_bool* const inside_flag)
{
int *yflag0;
int *subpath_flag;
int yflag1;
double vtx0, vty0, vtx1, vty1;
double tx, ty;
double sx, sy;
double x, y;
size_t i;
int all_done;
const char *const points = (const char * const)points_;
yflag0 = (int *)malloc(n * sizeof(int));
subpath_flag = (int *)malloc(n * sizeof(int));
path.rewind(0);
for (i = 0; i < n; ++i) {
inside_flag[i] = 0;
}
unsigned code = 0;
do
{
if (code != agg::path_cmd_move_to)
{
code = path.vertex(&x, &y);
if (code == agg::path_cmd_stop ||
(code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly) {
continue;
}
}
sx = vtx0 = vtx1 = x;
sy = vty0 = vty1 = y;
for (i = 0; i < n; ++i) {
ty = *(double *)(points + s0 * i + s1);
// get test bit for above/below X axis
yflag0[i] = (vty0 >= ty);
subpath_flag[i] = 0;
}
do
{
code = path.vertex(&x, &y);
// The following cases denote the beginning on a new subpath
if (code == agg::path_cmd_stop ||
(code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly)
{
x = sx;
y = sy;
}
else if (code == agg::path_cmd_move_to)
{
break;
}
for (i = 0; i < n; ++i) {
tx = *(double *)(points + s0 * i);
ty = *(double *)(points + s0 * i + s1);
yflag1 = (vty1 >= ty);
// Check if endpoints straddle (are on opposite sides) of
// X axis (i.e. the Y's differ); if so, +X ray could
// intersect this edge. The old test also checked whether
// the endpoints are both to the right or to the left of
// the test point. However, given the faster intersection
// point computation used below, this test was found to be
// a break-even proposition for most polygons and a loser
// for triangles (where 50% or more of the edges which
// survive this test will cross quadrants and so have to
// have the X intersection computed anyway). I credit
// Joseph Samosky with inspiring me to try dropping the
// "both left or both right" part of my code.
if (yflag0[i] != yflag1) {
// Check intersection of pgon segment with +X ray.
// Note if >= point's X; if so, the ray hits it. The
// division operation is avoided for the ">=" test by
// checking the sign of the first vertex wrto the test
// point; idea inspired by Joseph Samosky's and Mark
// Haigh-Hutchinson's different polygon inclusion
// tests.
if (((vty1 - ty) * (vtx0 - vtx1) >=
(vtx1 - tx) * (vty0 - vty1)) == yflag1) {
subpath_flag[i] ^= 1;
}
}
// Move to the next pair of vertices, retaining info as
// possible.
yflag0[i] = yflag1;
}
vtx0 = vtx1;
vty0 = vty1;
vtx1 = x;
vty1 = y;
}
while (code != agg::path_cmd_stop &&
(code & agg::path_cmd_end_poly) != agg::path_cmd_end_poly);
all_done = 1;
for (i = 0; i < n; ++i) {
tx = *(double *)(points + s0 * i);
ty = *(double *)(points + s0 * i + s1);
yflag1 = (vty1 >= ty);
if (yflag0[i] != yflag1) {
if (((vty1 - ty) * (vtx0 - vtx1) >=
(vtx1 - tx) * (vty0 - vty1)) == yflag1) {
subpath_flag[i] ^= 1;
}
}
inside_flag[i] |= subpath_flag[i];
if (inside_flag[i] == 0) {
all_done = 0;
}
}
if (all_done) {
goto exit;
}
}
while (code != agg::path_cmd_stop);
exit:
free(yflag0);
free(subpath_flag);
}
inline void
points_in_path(const void* const points, const size_t s0,
const size_t s1, const size_t n,
const double r, PathIterator& path,
const agg::trans_affine& trans,
npy_bool* result)
{
typedef agg::conv_transform<PathIterator> transformed_path_t;
typedef PathNanRemover<transformed_path_t> no_nans_t;
typedef agg::conv_curve<no_nans_t> curve_t;
typedef agg::conv_contour<curve_t> contour_t;
size_t i;
for (i = 0; i < n; ++i) {
result[i] = 0;
}
if (path.total_vertices() < 3)
{
return;
}
transformed_path_t trans_path(path, trans);
no_nans_t no_nans_path(trans_path, true, path.has_curves());
curve_t curved_path(no_nans_path);
contour_t contoured_path(curved_path);
contoured_path.width(fabs(r));
point_in_path_impl(points, s0, s1, n, contoured_path, result);
}
inline bool
point_in_path(const double x, const double y, const double r,
PathIterator& path, const agg::trans_affine& trans)
{
double points[2];
npy_bool result;
points[0] = x;
points[1] = y;
points_in_path(points, 0, sizeof(double), 1, r, path, trans, &result);
return result;
}
inline void
points_on_path(const void* const points, const size_t s0,
const size_t s1, const size_t n,
const double r, PathIterator& path,
const agg::trans_affine& trans,
npy_bool* result)
{
typedef agg::conv_transform<PathIterator> transformed_path_t;
typedef PathNanRemover<transformed_path_t> no_nans_t;
typedef agg::conv_curve<no_nans_t> curve_t;
typedef agg::conv_stroke<curve_t> stroke_t;
transformed_path_t trans_path(path, trans);
no_nans_t nan_removed_path(trans_path, true, path.has_curves());
curve_t curved_path(nan_removed_path);
stroke_t stroked_path(curved_path);
stroked_path.width(r * 2.0);
point_in_path_impl(points, s0, s1, n, stroked_path, result);
}
inline bool
point_on_path(const double x, const double y, const double r,
PathIterator& path, const agg::trans_affine& trans)
{
double points[2];
npy_bool result;
points[0] = x;
points[1] = y;
points_on_path(points, 0, sizeof(double), 1, r, path, trans, &result);
return result;
}
Py::Object
_path_module::point_in_path(const Py::Tuple& args)
{
double x = Py::Float(args[0]);
double y = Py::Float(args[1]);
double r = Py::Float(args[2]);
PathIterator path(args[3]);
agg::trans_affine trans = py_to_agg_transformation_matrix(args[4].ptr(), false);
if (::point_in_path(x, y, r, path, trans)) {
return Py::Int(1);
}
return Py::Int(0);
}
Py::Object
_path_module::points_in_path(const Py::Tuple& args)
{
args.verify_length(4);
npy_intp n;
PyArrayObject* points_array;
points_array = (PyArrayObject*)PyArray_FromObject(args[0].ptr(), PyArray_DOUBLE, 2, 2);
if (points_array == NULL || PyArray_DIM(points_array, 1) != 2) {
throw Py::TypeError(
"Argument 0 to points_in_path must be an Nx2 numpy array");
}
double r = Py::Float(args[1]);
PathIterator path(args[2]);
agg::trans_affine trans = py_to_agg_transformation_matrix(args[3].ptr(), false);
n = PyArray_DIM(points_array, 0);
PyObject* result = PyArray_ZEROS(1, &n, PyArray_BOOL, 0);
if (result == NULL) {
throw Py::MemoryError("Could not allocate memory for result");
}
::points_in_path(PyArray_DATA(points_array),
PyArray_STRIDE(points_array, 0),
PyArray_STRIDE(points_array, 1),
n, r, path, trans,
(npy_bool *)PyArray_DATA(result));
Py_DECREF(points_array);
return Py::Object(result, true);;
}
Py::Object
_path_module::point_on_path(const Py::Tuple& args)
{
double x = Py::Float(args[0]);
double y = Py::Float(args[1]);
double r = Py::Float(args[2]);
PathIterator path(args[3]);
agg::trans_affine trans = py_to_agg_transformation_matrix(args[4].ptr());
if (::point_on_path(x, y, r, path, trans))
{
return Py::Int(1);
}
return Py::Int(0);
}
void
get_path_extents(PathIterator& path, const agg::trans_affine& trans,
double* x0, double* y0, double* x1, double* y1,
double* xm, double* ym)
{
typedef agg::conv_transform<PathIterator> transformed_path_t;
typedef PathNanRemover<transformed_path_t> nan_removed_t;
typedef agg::conv_curve<nan_removed_t> curve_t;
double x, y;
unsigned code;
transformed_path_t tpath(path, trans);
nan_removed_t nan_removed(tpath, true, path.has_curves());
curve_t curved_path(nan_removed);
curved_path.rewind(0);
while ((code = curved_path.vertex(&x, &y)) != agg::path_cmd_stop)
{
if ((code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly)
{
continue;
}
if (x < *x0) *x0 = x;
if (y < *y0) *y0 = y;
if (x > *x1) *x1 = x;
if (y > *y1) *y1 = y;
/* xm and ym are the minimum positive values in the data, used
by log scaling */
if (x > 0.0 && x < *xm) *xm = x;
if (y > 0.0 && y < *ym) *ym = y;
}
}
Py::Object
_path_module::get_path_extents(const Py::Tuple& args)
{
args.verify_length(2);
PathIterator path(args[0]);
agg::trans_affine trans = py_to_agg_transformation_matrix(args[1].ptr(), false);
npy_intp extent_dims[] = { 2, 2, 0 };
double* extents_data = NULL;
double xm, ym;
PyArrayObject* extents = NULL;
try
{
extents = (PyArrayObject*)PyArray_SimpleNew
(2, extent_dims, PyArray_DOUBLE);
if (extents == NULL)
{
throw Py::MemoryError("Could not allocate result array");
}
extents_data = (double*)PyArray_DATA(extents);
extents_data[0] = std::numeric_limits<double>::infinity();
extents_data[1] = std::numeric_limits<double>::infinity();
extents_data[2] = -std::numeric_limits<double>::infinity();
extents_data[3] = -std::numeric_limits<double>::infinity();
/* xm and ym are the minimum positive values in the data, used
by log scaling */
xm = std::numeric_limits<double>::infinity();
ym = std::numeric_limits<double>::infinity();
::get_path_extents(path, trans, &extents_data[0], &extents_data[1],
&extents_data[2], &extents_data[3], &xm, &ym);
}
catch (...)
{
Py_XDECREF(extents);
throw;
}
return Py::Object((PyObject*)extents, true);
}
Py::Object
_path_module::update_path_extents(const Py::Tuple& args)
{
args.verify_length(5);
double x0, y0, x1, y1;
PathIterator path(args[0]);
agg::trans_affine trans = py_to_agg_transformation_matrix(
args[1].ptr(), false);
if (!py_convert_bbox(args[2].ptr(), x0, y0, x1, y1))
{
throw Py::ValueError(
"Must pass Bbox object as arg 3 of update_path_extents");
}
Py::Object minpos_obj = args[3];
bool ignore = Py::Boolean(args[4]);
double xm, ym;
PyArrayObject* input_minpos = NULL;
try
{
input_minpos = (PyArrayObject*)PyArray_FromObject(
minpos_obj.ptr(), PyArray_DOUBLE, 1, 1);
if (!input_minpos || PyArray_DIM(input_minpos, 0) != 2)
{
throw Py::TypeError(
"Argument 4 to update_path_extents must be a length-2 numpy array.");
}
xm = *(double*)PyArray_GETPTR1(input_minpos, 0);
ym = *(double*)PyArray_GETPTR1(input_minpos, 1);
}
catch (...)
{
Py_XDECREF(input_minpos);
throw;
}
Py_XDECREF(input_minpos);
npy_intp extent_dims[] = { 2, 2, 0 };
double* extents_data = NULL;
npy_intp minpos_dims[] = { 2, 0 };
double* minpos_data = NULL;
PyArrayObject* extents = NULL;
PyArrayObject* minpos = NULL;
bool changed = false;
try
{
extents = (PyArrayObject*)PyArray_SimpleNew
(2, extent_dims, PyArray_DOUBLE);
if (extents == NULL)
{
throw Py::MemoryError("Could not allocate result array");
}
minpos = (PyArrayObject*)PyArray_SimpleNew
(1, minpos_dims, PyArray_DOUBLE);
if (minpos == NULL)
{
throw Py::MemoryError("Could not allocate result array");
}
extents_data = (double*)PyArray_DATA(extents);
minpos_data = (double*)PyArray_DATA(minpos);
if (ignore)
{
extents_data[0] = std::numeric_limits<double>::infinity();
extents_data[1] = std::numeric_limits<double>::infinity();
extents_data[2] = -std::numeric_limits<double>::infinity();
extents_data[3] = -std::numeric_limits<double>::infinity();
minpos_data[0] = std::numeric_limits<double>::infinity();
minpos_data[1] = std::numeric_limits<double>::infinity();
}
else
{
if (x0 > x1)
{
extents_data[0] = std::numeric_limits<double>::infinity();
extents_data[2] = -std::numeric_limits<double>::infinity();
}
else
{
extents_data[0] = x0;
extents_data[2] = x1;
}
if (y0 > y1)
{
extents_data[1] = std::numeric_limits<double>::infinity();
extents_data[3] = -std::numeric_limits<double>::infinity();
}
else
{
extents_data[1] = y0;
extents_data[3] = y1;
}
minpos_data[0] = xm;
minpos_data[1] = ym;
}
::get_path_extents(path, trans, &extents_data[0], &extents_data[1],
&extents_data[2], &extents_data[3], &minpos_data[0],
&minpos_data[1]);
changed = (extents_data[0] != x0 ||
extents_data[1] != y0 ||
extents_data[2] != x1 ||
extents_data[3] != y1 ||
minpos_data[0] != xm ||
minpos_data[1] != ym);
}
catch (...)
{
Py_XDECREF(extents);
Py_XDECREF(minpos);
throw;
}
Py::Tuple result(3);
result[0] = Py::Object((PyObject*) extents);
result[1] = Py::Object((PyObject*) minpos);
result[2] = Py::Int(changed ? 1 : 0);
Py_XDECREF(extents);
Py_XDECREF(minpos);
return result;
}
Py::Object
_path_module::get_path_collection_extents(const Py::Tuple& args)
{
args.verify_length(5);
//segments, trans, clipbox, colors, linewidths, antialiaseds
agg::trans_affine master_transform = py_to_agg_transformation_matrix(args[0].ptr());
Py::SeqBase<Py::Object> paths = args[1];
Py::SeqBase<Py::Object> transforms_obj = args[2];
Py::Object offsets_obj = args[3];
agg::trans_affine offset_trans = py_to_agg_transformation_matrix(args[4].ptr(), false);
PyArrayObject* offsets = NULL;
double x0, y0, x1, y1, xm, ym;
try
{
offsets = (PyArrayObject*)PyArray_FromObject(
offsets_obj.ptr(), PyArray_DOUBLE, 0, 2);
if (!offsets ||
(PyArray_NDIM(offsets) == 2 && PyArray_DIM(offsets, 1) != 2) ||
(PyArray_NDIM(offsets) == 1 && PyArray_DIM(offsets, 0) != 0))
{
throw Py::ValueError("Offsets array must be Nx2");
}
size_t Npaths = paths.length();
size_t Noffsets = offsets->dimensions[0];
size_t N = std::max(Npaths, Noffsets);
size_t Ntransforms = std::min(transforms_obj.length(), N);
size_t i;
// Convert all of the transforms up front
typedef std::vector<agg::trans_affine> transforms_t;
transforms_t transforms;
transforms.reserve(Ntransforms);
for (i = 0; i < Ntransforms; ++i)
{
agg::trans_affine trans = py_to_agg_transformation_matrix
(transforms_obj[i].ptr(), false);
trans *= master_transform;
transforms.push_back(trans);
}
// The offset each of those and collect the mins/maxs
x0 = std::numeric_limits<double>::infinity();
y0 = std::numeric_limits<double>::infinity();
x1 = -std::numeric_limits<double>::infinity();
y1 = -std::numeric_limits<double>::infinity();
xm = std::numeric_limits<double>::infinity();
ym = std::numeric_limits<double>::infinity();
agg::trans_affine trans;
for (i = 0; i < N; ++i)
{
PathIterator path(paths[i % Npaths]);
if (Ntransforms)
{
trans = transforms[i % Ntransforms];
}
else
{
trans = master_transform;
}
if (Noffsets)
{
double xo = *(double*)PyArray_GETPTR2(offsets, i % Noffsets, 0);
double yo = *(double*)PyArray_GETPTR2(offsets, i % Noffsets, 1);
offset_trans.transform(&xo, &yo);
trans *= agg::trans_affine_translation(xo, yo);
}
::get_path_extents(path, trans, &x0, &y0, &x1, &y1, &xm, &ym);
}
}
catch (...)
{
Py_XDECREF(offsets);
throw;
}
Py_XDECREF(offsets);
Py::Tuple result(4);
result[0] = Py::Float(x0);
result[1] = Py::Float(y0);
result[2] = Py::Float(x1);
result[3] = Py::Float(y1);
return result;
}
Py::Object
_path_module::point_in_path_collection(const Py::Tuple& args)
{
args.verify_length(10);
//segments, trans, clipbox, colors, linewidths, antialiaseds
double x = Py::Float(args[0]);
double y = Py::Float(args[1]);
double radius = Py::Float(args[2]);
agg::trans_affine master_transform = py_to_agg_transformation_matrix(args[3].ptr());
Py::SeqBase<Py::Object> paths = args[4];
Py::SeqBase<Py::Object> transforms_obj = args[5];
Py::SeqBase<Py::Object> offsets_obj = args[6];
agg::trans_affine offset_trans = py_to_agg_transformation_matrix(args[7].ptr());
bool filled = Py::Boolean(args[8]);
std::string offset_position = Py::String(args[9]);
bool data_offsets = (offset_position == "data");
PyArrayObject* offsets = (PyArrayObject*)PyArray_FromObject(
offsets_obj.ptr(), PyArray_DOUBLE, 0, 2);
if (!offsets ||
(PyArray_NDIM(offsets) == 2 && PyArray_DIM(offsets, 1) != 2) ||
(PyArray_NDIM(offsets) == 1 && PyArray_DIM(offsets, 0) != 0))
{
Py_XDECREF(offsets);
throw Py::ValueError("Offsets array must be Nx2");
}
size_t Npaths = paths.length();
size_t Noffsets = offsets->dimensions[0];
size_t N = std::max(Npaths, Noffsets);
size_t Ntransforms = std::min(transforms_obj.length(), N);
size_t i;
// Convert all of the transforms up front
typedef std::vector<agg::trans_affine> transforms_t;
transforms_t transforms;
transforms.reserve(Ntransforms);
for (i = 0; i < Ntransforms; ++i)
{
agg::trans_affine trans = py_to_agg_transformation_matrix
(transforms_obj[i].ptr(), false);
trans *= master_transform;
transforms.push_back(trans);
}
Py::List result;
agg::trans_affine trans;
for (i = 0; i < N; ++i)
{
PathIterator path(paths[i % Npaths]);
if (Ntransforms)
{
trans = transforms[i % Ntransforms];
}
else
{
trans = master_transform;
}
if (Noffsets)
{
double xo = *(double*)PyArray_GETPTR2(offsets, i % Noffsets, 0);
double yo = *(double*)PyArray_GETPTR2(offsets, i % Noffsets, 1);
offset_trans.transform(&xo, &yo);
if (data_offsets) {
trans = agg::trans_affine_translation(xo, yo) * trans;
} else {
trans *= agg::trans_affine_translation(xo, yo);
}
}
if (filled)
{
if (::point_in_path(x, y, radius, path, trans))
result.append(Py::Int((int)i));
}
else
{
if (::point_on_path(x, y, radius, path, trans))
result.append(Py::Int((int)i));
}
}
return result;
}
bool
path_in_path(PathIterator& a, const agg::trans_affine& atrans,
PathIterator& b, const agg::trans_affine& btrans)
{
typedef agg::conv_transform<PathIterator> transformed_path_t;
typedef PathNanRemover<transformed_path_t> no_nans_t;
typedef agg::conv_curve<no_nans_t> curve_t;
if (a.total_vertices() < 3)
return false;
transformed_path_t b_path_trans(b, btrans);
no_nans_t b_no_nans(b_path_trans, true, b.has_curves());
curve_t b_curved(b_no_nans);
double x, y;
b_curved.rewind(0);
while (b_curved.vertex(&x, &y) != agg::path_cmd_stop)
{
if (!::point_in_path(x, y, 0.0, a, atrans))
return false;
}
return true;
}
Py::Object
_path_module::path_in_path(const Py::Tuple& args)
{
args.verify_length(4);
PathIterator a(args[0]);
agg::trans_affine atrans = py_to_agg_transformation_matrix(
args[1].ptr(), false);
PathIterator b(args[2]);
agg::trans_affine btrans = py_to_agg_transformation_matrix(
args[3].ptr(), false);
return Py::Int(::path_in_path(a, atrans, b, btrans));
}
/** The clip_path_to_rect code here is a clean-room implementation of
the Sutherland-Hodgman clipping algorithm described here:
http://en.wikipedia.org/wiki/Sutherland-Hodgman_clipping_algorithm
*/
typedef std::vector<XY> Polygon;
namespace clip_to_rect_filters
{
/* There are four different passes needed to create/remove
vertices (one for each side of the rectangle). The differences
between those passes are encapsulated in these functor classes.
*/
struct bisectx
{
double m_x;
bisectx(double x) : m_x(x) {}
inline void
bisect(double sx, double sy, double px, double py, double* bx,
double* by) const
{
*bx = m_x;
double dx = px - sx;
double dy = py - sy;
*by = sy + dy * ((m_x - sx) / dx);
}
};
struct xlt : public bisectx
{
xlt(double x) : bisectx(x) {}
inline bool
is_inside(double x, double y) const
{
return x <= m_x;
}
};
struct xgt : public bisectx
{
xgt(double x) : bisectx(x) {}
inline bool
is_inside(double x, double y) const
{
return x >= m_x;
}
};
struct bisecty
{
double m_y;
bisecty(double y) : m_y(y) {}
inline void
bisect(double sx, double sy, double px, double py, double* bx,
double* by) const
{
*by = m_y;
double dx = px - sx;
double dy = py - sy;
*bx = sx + dx * ((m_y - sy) / dy);
}
};
struct ylt : public bisecty
{
ylt(double y) : bisecty(y) {}
inline bool
is_inside(double x, double y) const
{
return y <= m_y;
}
};
struct ygt : public bisecty
{
ygt(double y) : bisecty(y) {}
inline bool
is_inside(double x, double y) const
{
return y >= m_y;
}
};
}
template<class Filter>
inline void
clip_to_rect_one_step(const Polygon& polygon, Polygon& result, const Filter& filter)
{
double sx, sy, px, py, bx, by;
bool sinside, pinside;
result.clear();
if (polygon.size() == 0)
{
return;
}
sx = polygon.back().x;
sy = polygon.back().y;
for (Polygon::const_iterator i = polygon.begin(); i != polygon.end(); ++i)
{
px = i->x;
py = i->y;
sinside = filter.is_inside(sx, sy);
pinside = filter.is_inside(px, py);
if (sinside ^ pinside)
{
filter.bisect(sx, sy, px, py, &bx, &by);
result.push_back(XY(bx, by));
}
if (pinside)
{
result.push_back(XY(px, py));
}
sx = px;
sy = py;
}
}
template<class Path>
void
clip_to_rect(Path& path,
double x0, double y0, double x1, double y1,
bool inside, std::vector<Polygon>& results)
{
double xmin, ymin, xmax, ymax;
if (x0 < x1)
{
xmin = x0;
xmax = x1;
}
else
{
xmin = x1;
xmax = x0;
}
if (y0 < y1)
{
ymin = y0;
ymax = y1;
}
else
{
ymin = y1;
ymax = y0;
}
if (!inside)
{
std::swap(xmin, xmax);