Skip to content

Commit

Permalink
Merge pull request #30 from revarbat/nogil
Browse files Browse the repository at this point in the history
Release the GIL while performing long processing.
  • Loading branch information
anthrotype authored Jul 1, 2020
2 parents edf8bfb + b089ab9 commit fc2772a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 45 deletions.
117 changes: 73 additions & 44 deletions pyclipper/pyclipper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ cdef extern from "clipper.hpp" namespace "ClipperLib":
Clipper()
#~Clipper()
void Clear()
bool Execute(ClipType clipType, Paths & solution, PolyFillType subjFillType, PolyFillType clipFillType)
bool Execute(ClipType clipType, PolyTree & solution, PolyFillType subjFillType, PolyFillType clipFillType)
bool Execute(ClipType clipType, Paths & solution, PolyFillType subjFillType, PolyFillType clipFillType) nogil
bool Execute(ClipType clipType, PolyTree & solution, PolyFillType subjFillType, PolyFillType clipFillType) nogil
bool ReverseSolution()
void ReverseSolution(bool value)
bool StrictlySimple()
Expand All @@ -180,7 +180,7 @@ cdef extern from "clipper.hpp" namespace "ClipperLib":
void PreserveCollinear(bool value)
bool AddPath(Path & path, PolyType polyType, bool closed)
bool AddPaths(Paths & paths, PolyType polyType, bool closed)
IntRect GetBounds()
IntRect GetBounds() nogil

cdef cppclass ClipperOffset:
ClipperOffset(double miterLimit, double roundPrecision)
Expand All @@ -189,35 +189,35 @@ cdef extern from "clipper.hpp" namespace "ClipperLib":
#~ClipperOffset()
void AddPath(Path & path, JoinType joinType, EndType endType)
void AddPaths(Paths & paths, JoinType joinType, EndType endType)
void Execute(Paths & solution, double delta)
void Execute(PolyTree & solution, double delta)
void Execute(Paths & solution, double delta) nogil
void Execute(PolyTree & solution, double delta) nogil
void Clear()
double MiterLimit
double ArcTolerance

# prefixes are added to original functions to prevent naming collisions
bool c_Orientation "Orientation"(const Path & poly)
double c_Area "Area"(const Path & poly)
int c_PointInPolygon "PointInPolygon"(const IntPoint & pt, const Path & path)
bool c_Orientation "Orientation"(const Path & poly) nogil
double c_Area "Area"(const Path & poly) nogil
int c_PointInPolygon "PointInPolygon"(const IntPoint & pt, const Path & path) nogil

# In the following 4 functions default values for fillType and distance were removed
# because it caused a bug in C++ code generated by Cython.
# See Cython bug report: http://trac.cython.org/ticket/816
void c_SimplifyPolygon "SimplifyPolygon"(const Path & in_poly, Paths & out_polys, PolyFillType fillType)
void c_SimplifyPolygons "SimplifyPolygons"(const Paths & in_polys, Paths & out_polys, PolyFillType fillType)
void c_CleanPolygon "CleanPolygon"(const Path& in_poly, Path& out_poly, double distance)
void c_CleanPolygons "CleanPolygons"(Paths& polys, double distance)
void c_SimplifyPolygon "SimplifyPolygon"(const Path & in_poly, Paths & out_polys, PolyFillType fillType) nogil
void c_SimplifyPolygons "SimplifyPolygons"(const Paths & in_polys, Paths & out_polys, PolyFillType fillType) nogil
void c_CleanPolygon "CleanPolygon"(const Path& in_poly, Path& out_poly, double distance) nogil
void c_CleanPolygons "CleanPolygons"(Paths& polys, double distance) nogil

void c_MinkowskiSum "MinkowskiSum"(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed)
void c_MinkowskiSum "MinkowskiSum"(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed)
void c_MinkowskiDiff "MinkowskiDiff"(const Path& poly1, const Path& poly2, Paths& solution)
void c_MinkowskiSum "MinkowskiSum"(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed) nogil
void c_MinkowskiSum "MinkowskiSum"(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed) nogil
void c_MinkowskiDiff "MinkowskiDiff"(const Path& poly1, const Path& poly2, Paths& solution) nogil

void c_PolyTreeToPaths "PolyTreeToPaths"(const PolyTree& polytree, Paths& paths)
void c_ClosedPathsFromPolyTree "ClosedPathsFromPolyTree"(const PolyTree& polytree, Paths& paths)
void c_OpenPathsFromPolyTree "OpenPathsFromPolyTree"(PolyTree& polytree, Paths& paths)

void c_ReversePath "ReversePath"(Path& p)
void c_ReversePaths "ReversePaths"(Paths& p)
void c_ReversePath "ReversePath"(Path& p) nogil
void c_ReversePaths "ReversePaths"(Paths& p) nogil

#============================= Enum mapping ================

Expand Down Expand Up @@ -276,7 +276,11 @@ def Orientation(poly):
True -- counter-clockwise orientation
False -- clockwise orientation
"""
return <bint>c_Orientation(_to_clipper_path(poly))
cdef Path c_path = _to_clipper_path(poly)
cdef bint result
with nogil:
result = <bint>c_Orientation(c_path)
return result


def Area(poly):
Expand All @@ -291,7 +295,11 @@ def Area(poly):
Negative number if orientation is False
"""

return <double>c_Area(_to_clipper_path(poly))
cdef Path c_path = _to_clipper_path(poly)
cdef double result
with nogil:
result = <double>c_Area(c_path)
return result


def PointInPolygon(point, poly):
Expand All @@ -308,8 +316,12 @@ def PointInPolygon(point, poly):
1 -- point is in polygon
"""

return <int>c_PointInPolygon(_to_clipper_point(point),
_to_clipper_path(poly))
cdef IntPoint c_point = _to_clipper_point(point)
cdef Path c_path = _to_clipper_path(poly)
cdef int result
with nogil:
result = <int>c_PointInPolygon(c_point, c_path)
return result


def SimplifyPolygon(poly, PolyFillType fill_type=pftEvenOdd):
Expand All @@ -324,7 +336,9 @@ def SimplifyPolygon(poly, PolyFillType fill_type=pftEvenOdd):
list of simplified polygons (containing one or more polygons)
"""
cdef Paths out_polys
c_SimplifyPolygon(_to_clipper_path(poly), out_polys, fill_type)
cdef Path c_path = _to_clipper_path(poly)
with nogil:
c_SimplifyPolygon(c_path, out_polys, fill_type)
return _from_clipper_paths(out_polys)


Expand All @@ -340,7 +354,9 @@ def SimplifyPolygons(polys, PolyFillType fill_type=pftEvenOdd):
list of simplified polygons
"""
cdef Paths out_polys
c_SimplifyPolygons(_to_clipper_paths(polys), out_polys, fill_type)
cdef Paths c_polys = _to_clipper_paths(polys)
with nogil:
c_SimplifyPolygons(c_polys, out_polys, fill_type)
return _from_clipper_paths(out_polys)


Expand All @@ -356,7 +372,9 @@ def CleanPolygon(poly, double distance=1.415):
cleaned polygon
"""
cdef Path out_poly
c_CleanPolygon(_to_clipper_path(poly), out_poly, distance)
cdef Path c_path = _to_clipper_path(poly)
with nogil:
c_CleanPolygon(c_path, out_poly, distance)
return _from_clipper_path(out_poly)


Expand All @@ -372,7 +390,8 @@ def CleanPolygons(polys, double distance=1.415):
list of cleaned polygons
"""
cdef Paths out_polys = _to_clipper_paths(polys)
c_CleanPolygons(out_polys, distance)
with nogil:
c_CleanPolygons(out_polys, distance)
return _from_clipper_paths(out_polys)


Expand All @@ -389,11 +408,10 @@ def MinkowskiSum(pattern, path, bint path_is_closed):
list of polygons (containing one or more polygons)
"""
cdef Paths solution
c_MinkowskiSum(_to_clipper_path(pattern),
_to_clipper_path(path),
solution,
path_is_closed
)
cdef Path c_pat = _to_clipper_path(pattern)
cdef Path c_path = _to_clipper_path(path)
with nogil:
c_MinkowskiSum(c_pat, c_path, solution, path_is_closed)
return _from_clipper_paths(solution)


Expand All @@ -410,12 +428,10 @@ def MinkowskiSum2(pattern, paths, bint path_is_closed):
list of polygons
"""
cdef Paths solution
c_MinkowskiSum(
_to_clipper_path(pattern),
_to_clipper_paths(paths),
solution,
path_is_closed
)
cdef Path c_pat = _to_clipper_path(pattern)
cdef Paths c_paths = _to_clipper_paths(paths)
with nogil:
c_MinkowskiSum(c_pat, c_paths, solution, path_is_closed)
return _from_clipper_paths(solution)


Expand All @@ -431,7 +447,10 @@ def MinkowskiDiff(poly1, poly2):
list of polygons
"""
cdef Paths solution
c_MinkowskiDiff(_to_clipper_path(poly1), _to_clipper_path(poly2), solution)
cdef Path c_path1 = _to_clipper_path(poly1)
cdef Path c_path2 = _to_clipper_path(poly2)
with nogil:
c_MinkowskiDiff(c_path1, c_path2, solution)
return _from_clipper_paths(solution)


Expand Down Expand Up @@ -495,7 +514,8 @@ def ReversePath(path):
reversed path
"""
cdef Path c_path = _to_clipper_path(path)
c_ReversePath(c_path)
with nogil:
c_ReversePath(c_path)
return _from_clipper_path(c_path)


Expand All @@ -513,7 +533,8 @@ def ReversePaths(paths):
list if reversed paths
"""
cdef Paths c_paths = _to_clipper_paths(paths)
c_ReversePaths(c_paths)
with nogil:
c_ReversePaths(c_paths)
return _from_clipper_paths(c_paths)


Expand Down Expand Up @@ -651,7 +672,9 @@ cdef class Pyclipper:
"""
_check_scaling_factor()

cdef IntRect rr = <IntRect> self.thisptr.GetBounds()
cdef IntRect rr
with nogil:
rr = <IntRect> self.thisptr.GetBounds()
return PyIntRect(left=rr.left, top=rr.top,
right=rr.right, bottom=rr.bottom)

Expand All @@ -673,7 +696,9 @@ cdef class Pyclipper:
"""

cdef Paths solution
cdef object success = <bint> self.thisptr.Execute(clip_type, solution, subj_fill_type, clip_fill_type)
cdef bint success
with nogil:
success = <bint> self.thisptr.Execute(clip_type, solution, subj_fill_type, clip_fill_type)
if not success:
raise ClipperException('Execution of clipper did not succeed!')
return _from_clipper_paths(solution)
Expand All @@ -695,7 +720,9 @@ cdef class Pyclipper:
ClipperException -- operation did not succeed
"""
cdef PolyTree solution
cdef object success = <bint> self.thisptr.Execute(clip_type, solution, subj_fill_type, clip_fill_type)
cdef bint success
with nogil:
success = <bint> self.thisptr.Execute(clip_type, solution, subj_fill_type, clip_fill_type)
if not success:
raise ClipperException('Execution of clipper did not succeed!')
return _from_poly_tree(solution)
Expand Down Expand Up @@ -787,7 +814,8 @@ cdef class PyclipperOffset:
list of offset paths
"""
cdef Paths c_solution
self.thisptr.Execute(c_solution, delta)
with nogil:
self.thisptr.Execute(c_solution, delta)
return _from_clipper_paths(c_solution)

def Execute2(self, double delta):
Expand All @@ -802,7 +830,8 @@ cdef class PyclipperOffset:
PyPolyNode
"""
cdef PolyTree solution
self.thisptr.Execute(solution, delta)
with nogil:
self.thisptr.Execute(solution, delta)
return _from_poly_tree(solution)

def Clear(self):
Expand Down

0 comments on commit fc2772a

Please sign in to comment.