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.8-cp313-cp313-win_amd64.whl (170.2 kB view details)

Uploaded CPython 3.13Windows x86-64

pyclipr2-0.1.8-cp313-cp313-macosx_15_0_universal2.whl (168.4 kB view details)

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

pyclipr2-0.1.8-cp312-cp312-win_amd64.whl (170.2 kB view details)

Uploaded CPython 3.12Windows x86-64

pyclipr2-0.1.8-cp312-cp312-macosx_15_0_universal2.whl (168.4 kB view details)

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

pyclipr2-0.1.8-cp311-cp311-win_amd64.whl (169.4 kB view details)

Uploaded CPython 3.11Windows x86-64

pyclipr2-0.1.8-cp311-cp311-macosx_15_0_universal2.whl (167.5 kB view details)

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

pyclipr2-0.1.8-cp310-cp310-win_amd64.whl (168.6 kB view details)

Uploaded CPython 3.10Windows x86-64

pyclipr2-0.1.8-cp310-cp310-macosx_15_0_universal2.whl (166.5 kB view details)

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

pyclipr2-0.1.8-cp39-cp39-win_amd64.whl (177.5 kB view details)

Uploaded CPython 3.9Windows x86-64

pyclipr2-0.1.8-cp39-cp39-macosx_15_0_universal2.whl (166.6 kB view details)

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

pyclipr2-0.1.8-cp38-cp38-win_amd64.whl (168.6 kB view details)

Uploaded CPython 3.8Windows x86-64

pyclipr2-0.1.8-cp38-cp38-macosx_15_0_universal2.whl (166.4 kB view details)

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

File details

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

File metadata

  • Download URL: pyclipr2-0.1.8-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 170.2 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.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6a82fe8566cacecca001269d1ed82dc401bea5556165903e3fcab8c0ab719353
MD5 a66aab0b6b9901293f8d8ece2fbcba49
BLAKE2b-256 e2dc9d1399eac7e8fc6bbb6ad2e81465b3d4151ae793dca9728ac968b14c621e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyclipr2-0.1.8-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 20e62b8270364b193d387feeb532774222e9da11c7b8c53fc9fee4a83c0d5a34
MD5 ca2d690c9588b020f07db1eb0124669e
BLAKE2b-256 bda790ccfd57a0650dab58a2db07c75ccc80983256fa74e2456d42af53dcb54a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr2-0.1.8-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 170.2 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.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d7a9e39b06cc2f1b3dc330499b53d1c7f6a7655d8d4ce392a82ad3b387157f66
MD5 216e2d46cca58f1b394f1f6244b093f4
BLAKE2b-256 870ec0643dc9777c60c2a31f3f8e1a23b3934becbe802b5c855aab95e453d3f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyclipr2-0.1.8-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8586fa7b95b23e2ef7d34d158232301a6dfb2035a4e90b9105a021dbf1f1f082
MD5 9c1bfd747c9dc070757190fd07448583
BLAKE2b-256 ca80e679c6bf86076e1abe44b0288aa181318274b4462751ea4963f6a6f6abbf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr2-0.1.8-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 169.4 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.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 29b1f286beffee98f0b8f9f03a32f7b8dad7db3e4f764b389daae922319ab15d
MD5 67efc94e4b19ab1a01885255b3011df2
BLAKE2b-256 32a29262a5136b4d1076fd68a4f7a5b3e4b6a6bd3159afc8e835c9979038de8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyclipr2-0.1.8-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 53b4aca2a9097130ee7120144421d53b17349cdfcf59921525dc4e390f5d90a6
MD5 37edba3b89e518144404b17feed1957c
BLAKE2b-256 228f83c9c5f002f90a18a78a848dc0fae5bc30d61809ceb4f4f684b5588d8445

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr2-0.1.8-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 168.6 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.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6922888e5f5a50e608f9166bb57e833e00a1a50f47b7297fb147ea24a780b8a5
MD5 1ab345aa90fd66e1a39a79789c24cfeb
BLAKE2b-256 d07c1785a4b9135a2667c108d22454071c47df329d2d4088941b58b9ee213fe0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyclipr2-0.1.8-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 df0790cd4dc55c41b2c1d906206671e6641655d3ad2f1d54289f44e49b93d992
MD5 b344815a3098d4095335188acd19ce27
BLAKE2b-256 985c74c8b97ab6874cef86612120ff49409b9985bf24daa7bd7d8f2d5cba11a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr2-0.1.8-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 177.5 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.8-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 39f6d22069612d2614a931f69ce59d87add49532e37032359c0330b04e928239
MD5 a7f1fc239bfc42d65526e10f488a1460
BLAKE2b-256 ced4eca5a2a663629ce92ed1ec19ab4c9303607a63921c039c415cbc2d389d36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyclipr2-0.1.8-cp39-cp39-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 0be9ce46ad271573bd61583321452e4c69212e38a0f8a54b558eaeb9d5208de9
MD5 1efdbfa95b65a466853f7f7f316f8cb8
BLAKE2b-256 ff4d203cb608120a05db35c4da63ae40339d638da02ae07616c6bb4c7cfd3893

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr2-0.1.8-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 168.6 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.8-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 62c900722315bfc52054c16f7c41ca23079b9c47d76d67907a9e5d08f65d775a
MD5 5ec9a4d231afc7f6643a714735f12ae6
BLAKE2b-256 63392eb924a634b0ed25af9518ef9f1c4534d6e257ce4da803a370378f03d061

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyclipr2-0.1.8-cp38-cp38-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 db695bb2f374031961ce72cc5343ab9873ca6bc108f2d94a6484afca2d60b7e7
MD5 4aea4edc4b1baaa8d19a1498fb664c7c
BLAKE2b-256 d8f9a681fc31edf1a4786c2511b31c810ab3f2b2b57f6ad5b2717a92a9027284

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