Skip to main content

Python library for polygon clipping and offsetting based on Clipper2.

Project description

https://github.com/2ico/pyclipr/actions/workflows/pythonpublish.yml/badge.svg https://badge.fury.io/py/pyclipr.svg https://static.pepy.tech/personalized-badge/pyclipr?period=total&units=international_system&left_color=black&right_color=orange&left_text=Downloads

Pyclipr is a Python library offering the functionality of the Clipper2 polygon clipping and offsetting library and are built upon pybind . The underlying Clipper2 library performs intersection, union, difference and XOR boolean operations on both simple and complex polygons and also performs offsetting of polygons and inflation of paths.

Unlike pyclipper, this library is not built using cython. Instead the full use of capability pybind is exploited. This library aims to provide convenient access to the Clipper2 library for Python users, especially with its usage in 3D Printing and computer graphics applications.

For further information, see the latest release notes.

Installation

Installation using pre-built packages are currently supported on Windows, Mac but excludes Linux because pre-built packages are unsupported via PyPi. Otherwise, no special requirements or prerequisites are necessary.

conda install -c numpy
pip install numpy

Installation of pyclipr can then be performed using the pre-built python packages using the PyPi repository.

pip install pyclipr2

Alternatively, pyclipr may be compiled directly from source within the python environment. Currently the prerequisites are the a compliant c++ build environment include CMake build system (>v3.15) and the availability of a compiler with c++17 compatibility. Currently the package has been tested built using Windows 10, using VS2019 and Mac OSX Sonoma.

Firstly, clone the PyClipr repository whilst ensuring that you perform the recursive submodule when initialising the repository. This ensures that all dependencies (pybind, pyclipr, eigen, fmt) are downloaded into the source tree.

git clone https://github.com/drlukeparry/pyclipr.git && cd ./pyclipr
git submodule update --init --recursive

python setup.py install

Usage

The pyclipr library follows similar structure to that documented in Clipper2 library. Although for consistency most methods are implemented using camelcase naming convention and more generic functions are provided for the addition of paths.

The library assumes that coordinates are provided and scaled by a scaleFactor (default = 1e3), set within the Clipper and ClipperOffset classes to ensure correct numerical robustness outlined in the underlying Clipper library. The coordinates for the paths may be provided as a list of tuples or a numpy array.

Both Path64 and PolyTree64 structures are supported from the clipping and offseting operations, which are enacted by using either execute or execute2 methods, respectively.

import numpy as np
import pyclipr2 as pyclipr

# Tuple definition of a path
path = [(0.0, 0.), (0, 105.1234), (100, 105.1234), (100, 0), (0, 0)]
path2 = [(1.0, 1.0), (1.0, 50), (100, 50), (100, 1.0), (1.0,1.0)]

# Create an offsetting object
po = pyclipr.ClipperOffset()

# Set the scale factor to convert to internal integer representation
po.scaleFactor = int(1000)

# add the path - ensuring to use Polygon for the endType argument
# addPaths is required when working with polygon - this is a list of correctly orientated paths for exterior
# and interior holes
po.addPaths([np.array(path)], pyclipr.JoinType.Miter, pyclipr.EndType.Polygon)

# Apply the offsetting operation using a delta.
offsetSquare = po.execute(10.0)

# Create a clipping object
pc = pyclipr.Clipper()
pc.scaleFactor = int(1000)

# Add the paths to the clipping object. Ensure the subject and clip arguments are set to differentiate
# the paths during the Boolean operation. The final argument specifies if the path is
# open.
pc.addPaths(offsetSquare, pyclipr.Subject)
pc.addPath(np.array(path2), pyclipr.Clip)

""" Test Polygon Clipping """
# Below returns paths
out  = pc.execute(pyclipr.Intersection, pyclipr.FillRule.EvenOdd)
out2 = pc.execute(pyclipr.Union, pyclipr.FillRule.EvenOdd)
out3 = pc.execute(pyclipr.Difference, pyclipr.FillRule.EvenOdd)
out4 = pc.execute(pyclipr.Xor, pyclipr.FillRule.EvenOdd)

# Using execute2 returns a PolyTree structure that provides hierarchical information inflormation
# if the paths are interior or exterior
outB = pc.execute2(pyclipr.Intersection, pyclipr.FillRule.EvenOdd)

# An alternative equivalent name is executeTree
outB = pc.executeTree(pyclipr.Intersection, pyclipr.FillRule.EvenOdd)


""" Test Open Path Clipping """
# Pyclipr can be used for clipping open paths.  This remains simple to complete using the Clipper2 library

pc2 = pyclipr.Clipper()
pc2.scaleFactor = int(1e5)

# The open path is added as a subject (note the final argument is set to True)
pc2.addPath( ((40,-10),(50,130)), pyclipr.Subject, True)

# The clipping object is usually set to the Polygon
pc2.addPaths(offsetSquare, pyclipr.Clip, False)

""" Test the return types for open path clipping with option enabled"""
# The returnOpenPaths argument is set to True to return the open paths. Note this function only works
# well using the Boolean intersection option
outC = pc2.execute(pyclipr.Intersection, pyclipr.FillRule.NonZero)
outC2, openPathsC = pc2.execute(pyclipr.Intersection, pyclipr.FillRule.NonZero, returnOpenPaths=True)

outD = pc2.execute2(pyclipr.Intersection,  pyclipr.FillRule.NonZero)
outD2, openPathsD = pc2.execute2(pyclipr.Intersection,  pyclipr.FillRule.NonZero, returnOpenPaths=True)

# Plot the results
pathPoly = np.array(path)

import matplotlib.pyplot as plt
plt.figure()
plt.axis('equal')

# Plot the original polygon
plt.fill(pathPoly[:,0], pathPoly[:,1], 'b', alpha=0.1, linewidth=1.0, linestyle='dashed', edgecolor='#000')

# Plot the offset square
plt.fill(offsetSquare[0][:, 0], offsetSquare[0][:, 1], linewidth=1.0, linestyle='dashed', edgecolor='#333', facecolor='none')

# Plot the intersection
plt.fill(out[0][:, 0], out[0][:, 1],  facecolor='#75507b')

# Plot the open path intersection
plt.plot(openPathsC[0][:,0], openPathsC[0][:,1],color='#222', linewidth=1.0, linestyle='dashed', marker='.',markersize=20.0)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyclipr2-0.1.7-cp313-cp313-win_amd64.whl (167.4 kB view details)

Uploaded CPython 3.13Windows x86-64

pyclipr2-0.1.7-cp313-cp313-macosx_15_0_universal2.whl (164.8 kB view details)

Uploaded CPython 3.13macOS 15.0+ universal2 (ARM64, x86-64)

pyclipr2-0.1.7-cp312-cp312-win_amd64.whl (167.4 kB view details)

Uploaded CPython 3.12Windows x86-64

pyclipr2-0.1.7-cp312-cp312-macosx_15_0_universal2.whl (164.8 kB view details)

Uploaded CPython 3.12macOS 15.0+ universal2 (ARM64, x86-64)

pyclipr2-0.1.7-cp311-cp311-win_amd64.whl (166.8 kB view details)

Uploaded CPython 3.11Windows x86-64

pyclipr2-0.1.7-cp311-cp311-macosx_15_0_universal2.whl (164.1 kB view details)

Uploaded CPython 3.11macOS 15.0+ universal2 (ARM64, x86-64)

pyclipr2-0.1.7-cp310-cp310-win_amd64.whl (166.0 kB view details)

Uploaded CPython 3.10Windows x86-64

pyclipr2-0.1.7-cp310-cp310-macosx_15_0_universal2.whl (162.9 kB view details)

Uploaded CPython 3.10macOS 15.0+ universal2 (ARM64, x86-64)

pyclipr2-0.1.7-cp39-cp39-win_amd64.whl (175.3 kB view details)

Uploaded CPython 3.9Windows x86-64

pyclipr2-0.1.7-cp39-cp39-macosx_15_0_universal2.whl (163.0 kB view details)

Uploaded CPython 3.9macOS 15.0+ universal2 (ARM64, x86-64)

pyclipr2-0.1.7-cp38-cp38-win_amd64.whl (166.0 kB view details)

Uploaded CPython 3.8Windows x86-64

pyclipr2-0.1.7-cp38-cp38-macosx_15_0_universal2.whl (162.8 kB view details)

Uploaded CPython 3.8macOS 15.0+ universal2 (ARM64, x86-64)

File details

Details for the file pyclipr2-0.1.7-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: pyclipr2-0.1.7-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 167.4 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for pyclipr2-0.1.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fda9769523076172e5ebc3acbadcb740ea49853d493dd5ac9a6fdd6db6743a6c
MD5 46a8f36e5dbf334e1565598c94cb6de1
BLAKE2b-256 7559a2018b927bcec6c34c62044d8e5c2569be5dfd33d4f8a64ae52f034ccd6c

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp313-cp313-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for pyclipr2-0.1.7-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 26b872b359f9cd6d4ee1d1caf697c87ae1ebf31fb30563badbf973da4a00ca4c
MD5 be07b85ebb15280a5c3abc7fb108c6ae
BLAKE2b-256 40befeb8a23b35f688fb8eeb7a942045babc5be67719b7febada6f4d7812a6f5

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pyclipr2-0.1.7-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 167.4 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for pyclipr2-0.1.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f9d9f76e1d450aa575e9d3fe7f07e2b7f73a51159fd1f2a016a6edc4f3f9b47f
MD5 1e3af23e985b9590b9bf0f819bdff499
BLAKE2b-256 f5be6013e603cc23c15dd0d63d6e522e0d4e4b32ae74159b109b74bf9394fe91

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp312-cp312-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for pyclipr2-0.1.7-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 69a45a25b4b203209fc2cdf24437e20438785754e06acad312fc6ee6abe15ccb
MD5 4b4896b68688629c5b4108a270f6d3b6
BLAKE2b-256 2650d80487cf3b7ec987a4977aa5af28d53d2a1fa324e3686cd9b12135912a17

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pyclipr2-0.1.7-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 166.8 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for pyclipr2-0.1.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2661d57cec64e1156bc45af220b9abc7cc102ea8df9efe6ce1ff6990261c0399
MD5 8c876ade54621390177f4cdf35327ae8
BLAKE2b-256 4f64c3863233b28ebada79781e3c00da271c7b4bf8486327f0cecdb32b23e7fe

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp311-cp311-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for pyclipr2-0.1.7-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 790ca1e33c750ea860f4400c7f7160fbca269817789b59628ffc37b8e6080120
MD5 06d0bbdde32f5df2acb3d1df09a4df94
BLAKE2b-256 a21250c0a77e0fabe3cfef7e69d8badd9be2d2ef49460d790339e221f5bc577a

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pyclipr2-0.1.7-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 166.0 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for pyclipr2-0.1.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 25e59e74e4860c9d6660ca7a6450ab4fb967f72b3dc02ff223ef99c342808ca0
MD5 6db87261d47ff06b5905958f3ee25d00
BLAKE2b-256 54cf803728b9c04fbbbd8c6bf3f018146e695e1f91024d42b015fbfbd25366fa

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp310-cp310-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for pyclipr2-0.1.7-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 820657238ee3c897e49a252afa4adcd6f7be6a2496b4818347067e1f20549930
MD5 69b2c2528b3ba747f00793df70567452
BLAKE2b-256 22d054599ba98717d2d317a85365a9cbfa33b3658a52668e7d53dce2447cb4d2

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pyclipr2-0.1.7-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 175.3 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for pyclipr2-0.1.7-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 49c4a50e05e5a6107d708feb95353338e5b9887edaed48404ccd2e0037f48327
MD5 f296ab558820cc9c05a42bb30045086a
BLAKE2b-256 f425b4ab0112c1c55f3cbd0341f4dcabae759dd10c5e923cd5b1aaedc8116300

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp39-cp39-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for pyclipr2-0.1.7-cp39-cp39-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ced5f15e8aa71c4694eac61ad1b6291ebd9593ccfab485cb50284f662bb27686
MD5 ca63042a47c120a7b554fe53cd8dc18e
BLAKE2b-256 723dee61f1a8ce26c0f7e8ff69de47aea8825b74e71f2d8cc705068f0bc2f39c

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pyclipr2-0.1.7-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 166.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for pyclipr2-0.1.7-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 305b5fc347dda1ab6b62ebdd5bcd1667a02bcd9f0f5231a1a5c45069d4a2a6f4
MD5 78bb91aeb4ae39dfd069b3097a301e3a
BLAKE2b-256 3b7431083460d904884173bd6c2cf1c323b1b11d25d1ab31fb12098cec84afa8

See more details on using hashes here.

File details

Details for the file pyclipr2-0.1.7-cp38-cp38-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for pyclipr2-0.1.7-cp38-cp38-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5f17a5915a0ec60caaa70039dc4a3c5313676e0db2e941b674033609b3dc911b
MD5 b6e587196aba93ba4c617c9566c8946f
BLAKE2b-256 1b15c3797d3f04ee3685cd3567da2735fe4e08fb8c6e26b50f02afe001992483

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page