Python library for polygon clipping and offsetting based on Clipper2.
Project description
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, 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 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 Distribution
Built Distributions
Hashes for pyclipr-0.1.7-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7f6a2eff2bf6ac6fa72c21cdcb8441be5a5de37fd46764644f754d9826f3957 |
|
MD5 | 72aa0778aa2377cb8625cd9f88fd1af7 |
|
BLAKE2b-256 | e2a6d1b89a1a288f491d44762a0682f9bd5f9505a90d1d7959b4290dcfd5600d |
Hashes for pyclipr-0.1.7-cp312-cp312-macosx_12_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46783b321fcbb62c433b17a9c6551f8d50a62979dd95002f9743c2f1eadd9b69 |
|
MD5 | f3301dc18e567fe80dae464d6a2b114c |
|
BLAKE2b-256 | 140e4241947596318a658592dbf978852654138dd8f0dcfefb9e0efe085600ec |
Hashes for pyclipr-0.1.7-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89a7b22e968834a49c62f690850b97bbc2fd0d5dfc50073229d0c95c710294b5 |
|
MD5 | 6dfa3e67655a2cac594d9cffc8d3015d |
|
BLAKE2b-256 | 249f7e5d3c1a10d722de2640f564b82c070e79946c973cce9c1cb259b900a831 |
Hashes for pyclipr-0.1.7-cp311-cp311-macosx_12_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 421a2b8c913def9c006cf626c4814f54e00b0d93794e00123235c1aafc671866 |
|
MD5 | 0b613a98cc0aed5161ac1652e1eb0f2e |
|
BLAKE2b-256 | ff3708b3ff69025b504d26b220ebec87c6b545d4dc37f77bb7a4b77b4779b757 |
Hashes for pyclipr-0.1.7-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d68b88c0995b54eb934d51e74dc1a33d32d7ba93bc569dae7b1b6137872d9a21 |
|
MD5 | 016e1848bc7e4d23e54ed36db0bda5d5 |
|
BLAKE2b-256 | b266585a9b5a033187ca3f380137740d9166359420168b2f0daf1e13d158f57a |
Hashes for pyclipr-0.1.7-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8384107881204a5de2034cbe8fef6a61a9f04e5d42f7a0921b62b86324848709 |
|
MD5 | 36a9d14773fa142a29b22a09779435e6 |
|
BLAKE2b-256 | 4ea597a7cf7b5e7d8e830db6de2c54f179a618b817b4fc4cdb9b76a5c49b00b6 |
Hashes for pyclipr-0.1.7-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6629bd321d1d81bfa8af00167d9a613b6e370a1741b5d3e5b6e871b08c34cc4 |
|
MD5 | cf875c149311f1b4097b81fb93418c0f |
|
BLAKE2b-256 | af7e62da6662f183355dccce25f8993530d0ae4587b7ddca8bd53cd9f3c0b5bd |
Hashes for pyclipr-0.1.7-cp39-cp39-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2533135d68eba973c4ce18244a88e539fdeecf6f436b63ae829d44f8dbdf6272 |
|
MD5 | 0b2e028eaedaf6e74397b15e4199e570 |
|
BLAKE2b-256 | 98a2a3da1b868403d263c5a823d646e41987ec3e1ff548b6134c1cc156df38ee |
Hashes for pyclipr-0.1.7-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61d36938d41b046ddaf4ec7f0e7112fc5f2bcfba23b7bf46679556512af02ed2 |
|
MD5 | 216b54e2098a920fe1cb2c40f8ec6151 |
|
BLAKE2b-256 | f59e36423c9142207fbc922185036321db6da5ef99c55bedf34c1f456a4e05f6 |
Hashes for pyclipr-0.1.7-cp38-cp38-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 849c47c6f2c691690a82f923b10bff7676a7f85731eaede3c142c0f59eb4f0a4 |
|
MD5 | 5586fd77eaed45094353bddae8ed9c59 |
|
BLAKE2b-256 | 1175073a123405f23930d695e28ff0e1c9f5de34c8c1a090315bbe52c2645513 |
Hashes for pyclipr-0.1.7-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04b5454344130c69876fa5d19af5a4b1cc4d6f2a5b88c6c85ac6827a3e61bb82 |
|
MD5 | bd2bfb844212c36037e8b6a2021c60fb |
|
BLAKE2b-256 | c318dca42229af51dd6eabd939bb74d82bddddf4533517acda2ac9fabccc5005 |
Hashes for pyclipr-0.1.7-cp37-cp37m-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abde4a7c9506aac31f2b25c7048a10ac6a3824abf248c232fe15c1ae2fe14854 |
|
MD5 | 41cbde24a79c8ce8d2dee603b3092969 |
|
BLAKE2b-256 | a317f7a4b0c19f142e856bcc07c8fa5a8d4bda2e52cb6b2fb8b83ca0fe867459 |