|
30 | 30 | } |
31 | 31 |
|
32 | 32 |
|
| 33 | +def _addContour(clipperPath, contour, fillType, contourCount): |
| 34 | + if pyclipper.Area(contour) == 0: |
| 35 | + # skip paths with no area, |
| 36 | + # BUT self intersecting paths could have no area... |
| 37 | + dummy = pyclipper.Pyclipper() |
| 38 | + try: |
| 39 | + dummy.AddPath(contour, fillType) |
| 40 | + shouldBeAValidPath = True |
| 41 | + except pyclipper.ClipperException: |
| 42 | + shouldBeAValidPath = False |
| 43 | + if not shouldBeAValidPath: |
| 44 | + return |
| 45 | + |
| 46 | + try: |
| 47 | + clipperPath.AddPath(contour, pyclipper.PT_SUBJECT) |
| 48 | + except pyclipper.ClipperException: |
| 49 | + raise InvalidSubjectContourError("contour %d is invalid for clipping" % contourCount) |
| 50 | + |
| 51 | + |
33 | 52 | def clipExecute(subjectContours, clipContours, operation, subjectFillType="nonZero", |
34 | 53 | clipFillType="nonZero"): |
35 | 54 | pc = pyclipper.Pyclipper() |
36 | 55 |
|
37 | 56 | for i, subjectContour in enumerate(subjectContours): |
38 | | - try: |
39 | | - pc.AddPath(subjectContour, pyclipper.PT_SUBJECT) |
40 | | - except pyclipper.ClipperException: |
41 | | - raise InvalidSubjectContourError("contour %d is invalid for clipping" % i) |
| 57 | + _addContour(pc, subjectContour, pyclipper.PT_SUBJECT, i) |
| 58 | + |
42 | 59 | for j, clipContour in enumerate(clipContours): |
43 | | - try: |
44 | | - pc.AddPath(clipContour, pyclipper.PT_CLIP) |
45 | | - except pyclipper.ClipperException: |
46 | | - raise InvalidClippingContourError("contour %d is invalid for clipping" % j) |
| 60 | + _addContour(pc, subjectContour, pyclipper.PT_CLIP, i) |
47 | 61 |
|
48 | 62 | try: |
49 | 63 | solution = pc.Execute(_operationMap[operation], |
|
0 commit comments