Skip to main content

Python library for polygon clipping and offsetting based on Clipper2

Project description

GitHub Actions Workflow Status pypi-badge GitHub License pepy-badge python-badge py-version-badge

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 pyclipr

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 recurisve submodule when initialising the repoistory. This ensures that all dependencies (•pybind, • pyclipr, •eigen) are downloaded into the source tree.

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

python -m build

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. The internal scale factor determines the precision of the clipping and offsetting operations but can impact performance. It is recommended to adjust this in accordance to your applications.

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

A variety of other utilities are provided for inspection of polygons and paths.

import numpy as np
import 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)

""" Utility Functions """

# Simplification of Paths
rect = [(0,0), (100,0), (100,100), (0,100), (0,0)]
rect2 = [(20,0), (20,10), (30,10), (30,0), (20,0)]

simplifiedPaths = pyclipr.simplifyPaths([rect, rect2], epsilon=0.001)

# Check the orientation of a path (True if Counter-Clockwise)
isCCW = pyclipr.orientation(rect)

Project details


Download files

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

Source Distribution

pyclipr-0.1.8.tar.gz (4.6 MB view details)

Uploaded Source

Built Distributions

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

pyclipr-0.1.8-cp314-cp314-win_amd64.whl (198.9 kB view details)

Uploaded CPython 3.14Windows x86-64

pyclipr-0.1.8-cp314-cp314-macosx_15_0_arm64.whl (193.3 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

pyclipr-0.1.8-cp314-cp314-macosx_14_0_arm64.whl (198.6 kB view details)

Uploaded CPython 3.14macOS 14.0+ ARM64

pyclipr-0.1.8-cp313-cp313-win_amd64.whl (193.5 kB view details)

Uploaded CPython 3.13Windows x86-64

pyclipr-0.1.8-cp313-cp313-macosx_15_0_arm64.whl (192.9 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

pyclipr-0.1.8-cp313-cp313-macosx_14_0_arm64.whl (198.0 kB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

pyclipr-0.1.8-cp312-cp312-win_amd64.whl (193.3 kB view details)

Uploaded CPython 3.12Windows x86-64

pyclipr-0.1.8-cp312-cp312-macosx_15_0_arm64.whl (192.8 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

pyclipr-0.1.8-cp312-cp312-macosx_14_0_arm64.whl (198.0 kB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

pyclipr-0.1.8-cp311-cp311-win_amd64.whl (192.2 kB view details)

Uploaded CPython 3.11Windows x86-64

pyclipr-0.1.8-cp311-cp311-macosx_15_0_arm64.whl (192.3 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

pyclipr-0.1.8-cp311-cp311-macosx_14_0_arm64.whl (196.4 kB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

pyclipr-0.1.8-cp310-cp310-win_amd64.whl (191.6 kB view details)

Uploaded CPython 3.10Windows x86-64

pyclipr-0.1.8-cp310-cp310-macosx_15_0_arm64.whl (191.1 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

pyclipr-0.1.8-cp310-cp310-macosx_14_0_arm64.whl (195.0 kB view details)

Uploaded CPython 3.10macOS 14.0+ ARM64

pyclipr-0.1.8-cp39-cp39-win_amd64.whl (199.7 kB view details)

Uploaded CPython 3.9Windows x86-64

pyclipr-0.1.8-cp39-cp39-macosx_15_0_arm64.whl (191.2 kB view details)

Uploaded CPython 3.9macOS 15.0+ ARM64

pyclipr-0.1.8-cp39-cp39-macosx_14_0_arm64.whl (195.2 kB view details)

Uploaded CPython 3.9macOS 14.0+ ARM64

File details

Details for the file pyclipr-0.1.8.tar.gz.

File metadata

  • Download URL: pyclipr-0.1.8.tar.gz
  • Upload date:
  • Size: 4.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyclipr-0.1.8.tar.gz
Algorithm Hash digest
SHA256 ff8360330f827dc4801a57cd18d3bc925814b6a9a05f14be1e0f8f72cc27488f
MD5 a097d021593c5dc24e257291089e8c5d
BLAKE2b-256 e396c8049cfb9166c1f50d878535f2d1cdae1303e994dea7783a3f9075aa0679

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: pyclipr-0.1.8-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 198.9 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyclipr-0.1.8-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 daf588b99921ee7e10acf01353ecd434a9931d33cc1eaba9ba27fa810077a999
MD5 8337eaedc8c0e21f80029bd1a371bb06
BLAKE2b-256 b0852091db1fc711afef762769e5c1e4ac5b9442787e55af3c444743bfd4be87

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 b0501fdfedfde2dca68018c987b54965c61f689203877bb46ba3692c231735ab
MD5 7b9f2415e0881387d278b32b007f6436
BLAKE2b-256 692eea808a37078e9379990e059b6a69916b73375e3cb14a89eb604d9e541dc4

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp314-cp314-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp314-cp314-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 a444bf4ff7940fc1417b6e6ca8f65c6c7226c2540033c29dec00e4d1f7cb7989
MD5 6ef00c5be2b8222a44fe53c8f63514ac
BLAKE2b-256 2532f8cd08904dbd2cad91b68594da06afd5dacfd027f973988a61002c13773f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr-0.1.8-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 193.5 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyclipr-0.1.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9ae1c856116fc3b46546f724ca20b1145f085b7bb09d2e743260db59615c2fcb
MD5 1bd15eed5d2d0ba076188b3b8cb71acc
BLAKE2b-256 b128b5b61d8083bfd0c375e1c4e9467f5fb5d40afccc51954b9fdec9997b3601

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 6e377302f1f5c8f9e7b2da3f118c23b70f11da5089b3769bb9ea5b5650fd4560
MD5 292f4aace30bccc0349befbb82afa66d
BLAKE2b-256 6f8a6f7ef580fc12fba2780b63a3f33e5f763ac05cca0ea8262a5f0510dcff6e

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 16efa7fe9c0ddf2123629e3245c531b06d4e8cc1ad3a413c9f86f8d724212ff3
MD5 9f5688615c10e87b11af5c597c1089f4
BLAKE2b-256 a59849df896630649283202bb68b3fef828491796026e59b86be60f759f45cc8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr-0.1.8-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 193.3 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyclipr-0.1.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 502e3404f72186a5571a0d9dddbc2c9b493cb3b56598d4800de3e67e5dc0a5fd
MD5 ad7be47573a2ea8d376934ce0fffa7b5
BLAKE2b-256 a8a7756c10ddbf4f69eb37aeb3d895a29824d5285975db254570758eba57656a

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f961a754b114f69f1638bafd98df60e2fba672ccd921ea5bf2173e271ede43f9
MD5 07a8da9306b9f84a6d7b7cfb750a6a9b
BLAKE2b-256 bbbfe686ab34bf5f45a64548e590a6c794e6dcde56b8c08f1c60bec9ba620ec8

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 3517ce2f86f201bb7be0894bfd368d9e446e2b0c74914b80bdeee551ccc6ee82
MD5 a01f59b86cc6edcd8bcfd963378cfcab
BLAKE2b-256 82d41758ae1390f02f7dccddec8186f04e1e982a59bd978918a381a10e7ccd67

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr-0.1.8-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 192.2 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyclipr-0.1.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c453c85664245beb94e1b2bf49f441faa640da73832a59cfa6a6fbd340e3d890
MD5 8d6772c84c5daf33b9bc73d46b5efb06
BLAKE2b-256 c4e9b38300f5d15f404a6f5aa9050d6e33f6dbca334d649e60364cc89a8df1d7

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 bcb37cee65c82229f34704c305b3ad174c3e91a108f5b9e95467b68036dd06dd
MD5 7a7a61bb4daa3587f19ccb95d12f655e
BLAKE2b-256 f91c6d6e660f015362636bf0b0ae7d2f9eba110b1080d89dec355cdc6c0b8222

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 d20f0e81671ad9e490036d68b475d65a54ad794f890b363ed4525004f4c9bedf
MD5 cdb51e955754cd46bec551efb201e27d
BLAKE2b-256 639f26b36527d36d6589fa2e6148eddda63cfb355e6a157080f7887ad5ade88c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr-0.1.8-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 191.6 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyclipr-0.1.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 51f4203b8b8d6daaa456a19f3313b98dbfeb9bf8f63e7b7ecd1fa46fda8cc8a7
MD5 be6be1a0c691cf51d16976ce1d1b6435
BLAKE2b-256 8fbe6361ce98e03abc11e339b96d2f09d4c9820596f1db0366456800e2dc4eab

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 5030d1183176d183bc53c001e2f97805e9ed004a403cde4fed67045ca62efcd8
MD5 34707822b60ea3d5c3c43875943e9aff
BLAKE2b-256 4f141c3f2bcd7bba84134a28aa51819fe41273e74fe284095f50605c7082e8d6

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp310-cp310-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 a7bcec2ba41b73dbb3d39075ebd8dc9f98025ed6feda521a313046e72dd68522
MD5 ed056a72ef0fdbf2a3f1c308acd14c95
BLAKE2b-256 aadab0b005aa9d1fe05391b8974b425c1fbb2351f9f6967811a023f9ca87821d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyclipr-0.1.8-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 199.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyclipr-0.1.8-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 9ed5e9df0bf887e5ee947578d348e4e45c8fce307f4de2e5f63d43a8203f05c2
MD5 c39b1d3d7da80fa625a573283a58eaef
BLAKE2b-256 8a76557e35a086b9fcbb3a785d71a737711a6786a00d893172bafe974717d050

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp39-cp39-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp39-cp39-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 04920f97c5313f09a5a990078d56c6990143dbc21dbff899c32a0e34310df955
MD5 18e8bb3d9fde8e0984ab92d15b78a445
BLAKE2b-256 22196ee9dee46038e523c070034d9942e6ccd0eb65e6390128b296cda6395bdb

See more details on using hashes here.

File details

Details for the file pyclipr-0.1.8-cp39-cp39-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pyclipr-0.1.8-cp39-cp39-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 1657444d99dce19621a7e314800a3c755376311774e71416fd9a909f2706be06
MD5 dae1d2ac91f9acc27ae327caa142236b
BLAKE2b-256 4700363586cf3c86d1dabe602d30c0ba882fca6e304c0570350047c0fbddaa11

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