@@ -518,3 +518,54 @@ def test_clipping_full():
518
518
simplified = list (p .iter_segments (clip = [0 , 0 , 100 , 100 ]))
519
519
assert ([(list (x ), y ) for x , y in simplified ] ==
520
520
[([50 , 40 ], 1 )])
521
+
522
+
523
+ def test_simplify_closepoly ():
524
+ # The values of the vertices in a CLOSEPOLY should always be ignored,
525
+ # in favor of the most recent MOVETO's vertex values
526
+ paths = [Path ([(1 , 1 ), (2 , 1 ), (2 , 2 ), (np .nan , np .nan )],
527
+ [Path .MOVETO , Path .LINETO , Path .LINETO , Path .CLOSEPOLY ]),
528
+ Path ([(1 , 1 ), (2 , 1 ), (2 , 2 ), (40 , 50 )],
529
+ [Path .MOVETO , Path .LINETO , Path .LINETO , Path .CLOSEPOLY ])]
530
+ expected_path = Path ([(1 , 1 ), (2 , 1 ), (2 , 2 ), (1 , 1 ), (1 , 1 ), (0 , 0 )],
531
+ [Path .MOVETO , Path .LINETO , Path .LINETO , Path .LINETO ,
532
+ Path .LINETO , Path .STOP ])
533
+
534
+ for path in paths :
535
+ simplified_path = path .cleaned (simplify = True )
536
+ assert_array_equal (expected_path .vertices , simplified_path .vertices )
537
+ assert_array_equal (expected_path .codes , simplified_path .codes )
538
+
539
+ # test that a compound path also works
540
+ path = Path ([(1 , 1 ), (2 , 1 ), (2 , 2 ), (np .nan , np .nan ),
541
+ (- 1 , 0 ), (- 2 , 0 ), (- 2 , 1 ), (np .nan , np .nan )],
542
+ [Path .MOVETO , Path .LINETO , Path .LINETO , Path .CLOSEPOLY ,
543
+ Path .MOVETO , Path .LINETO , Path .LINETO , Path .CLOSEPOLY ])
544
+ expected_path = Path ([(1 , 1 ), (2 , 1 ), (2 , 2 ), (1 , 1 ),
545
+ (- 1 , 0 ), (- 2 , 0 ), (- 2 , 1 ), (- 1 , 0 ), (- 1 , 0 ), (0 , 0 )],
546
+ [Path .MOVETO , Path .LINETO , Path .LINETO , Path .LINETO ,
547
+ Path .MOVETO , Path .LINETO , Path .LINETO , Path .LINETO ,
548
+ Path .LINETO , Path .STOP ])
549
+
550
+ simplified_path = path .cleaned (simplify = True )
551
+ assert_array_equal (expected_path .vertices , simplified_path .vertices )
552
+ assert_array_equal (expected_path .codes , simplified_path .codes )
553
+
554
+ # test for a path with an invalid MOVETO
555
+ # CLOSEPOLY with an invalid MOVETO should be ignored
556
+ path = Path ([(1 , 0 ), (1 , - 1 ), (2 , - 1 ),
557
+ (np .nan , np .nan ), (- 1 , - 1 ), (- 2 , 1 ), (- 1 , 1 ),
558
+ (2 , 2 ), (0 , - 1 )],
559
+ [Path .MOVETO , Path .LINETO , Path .LINETO ,
560
+ Path .MOVETO , Path .LINETO , Path .LINETO , Path .LINETO ,
561
+ Path .CLOSEPOLY , Path .LINETO ])
562
+ expected_path = Path ([(1 , 0 ), (1 , - 1 ), (2 , - 1 ),
563
+ (np .nan , np .nan ), (- 1 , - 1 ), (- 2 , 1 ), (- 1 , 1 ),
564
+ (0 , - 1 ), (0 , - 1 ), (0 , 0 )],
565
+ [Path .MOVETO , Path .LINETO , Path .LINETO ,
566
+ Path .MOVETO , Path .LINETO , Path .LINETO , Path .LINETO ,
567
+ Path .LINETO , Path .LINETO , Path .STOP ])
568
+
569
+ simplified_path = path .cleaned (simplify = True )
570
+ assert_array_equal (expected_path .vertices , simplified_path .vertices )
571
+ assert_array_equal (expected_path .codes , simplified_path .codes )
0 commit comments