Skip to main content

Polygons clipping based on algorithm by F. Martinez et al.

Project description

clipping

In what follows python is an alias for python3.5 or pypy3.5 or any later version (python3.6, pypy3.6 and so on).

Installation

Install the latest pip & setuptools packages versions

python -m pip install --upgrade pip setuptools

User

Download and install the latest stable version from PyPI repository

python -m pip install --upgrade clipping

Developer

Download the latest version from GitHub repository

git clone https://github.com/lycantropos/clipping.git
cd clipping

Install dependencies

python -m pip install -r requirements.txt

Install

python setup.py install

Usage

>>> from ground.base import get_context
>>> context = get_context()
>>> Multipoint, Multisegment, Point, Segment = (context.multipoint_cls,
...                                             context.multisegment_cls,
...                                             context.point_cls,
...                                             context.segment_cls)
>>> left_edge = Segment(Point(0, 0), Point(0, 1))
>>> right_edge = Segment(Point(1, 0), Point(1, 1))
>>> bottom_edge = Segment(Point(0, 0), Point(1, 0))
>>> top_edge = Segment(Point(0, 1), Point(1, 1))
>>> main_diagonal = Segment(Point(0, 0), Point(1, 1))
>>> trident = Multisegment([left_edge, main_diagonal, bottom_edge])
>>> square_edges = Multisegment([bottom_edge, right_edge, top_edge, left_edge])
>>> from clipping.planar import intersect_multisegments
>>> (intersect_multisegments(trident, square_edges)
...  == intersect_multisegments(square_edges, trident)
...  == Multisegment([left_edge, bottom_edge]))
True
>>> from clipping.planar import complete_intersect_multisegments
>>> (complete_intersect_multisegments(trident, square_edges)
...  == complete_intersect_multisegments(square_edges, trident)
...  == (Multipoint([Point(1, 1)]),
...      intersect_multisegments(trident, square_edges)))
True
>>> from clipping.planar import unite_multisegments
>>> (unite_multisegments(trident, square_edges)
...  == unite_multisegments(square_edges, trident)
...  == Multisegment([left_edge, bottom_edge, main_diagonal, top_edge,
...                   right_edge]))
True
>>> from clipping.planar import subtract_multisegments
>>> (subtract_multisegments(trident, square_edges)
...  == Multisegment([main_diagonal]))
True
>>> (subtract_multisegments(square_edges, trident)
...  == Multisegment([top_edge, right_edge]))
True
>>> from clipping.planar import symmetric_subtract_multisegments
>>> (symmetric_subtract_multisegments(trident, square_edges)
...  == symmetric_subtract_multisegments(square_edges, trident)
...  == Multisegment([main_diagonal, top_edge, right_edge]))
True
>>> Contour, Multipolygon, Polygon = (context.contour_cls,
...                                   context.multipolygon_cls,
...                                   context.polygon_cls)
>>> left_triangle = Multipolygon([Polygon(Contour([Point(0, 0), Point(1, 0),
...                                                Point(0, 1)]), [])])
>>> right_triangle = Multipolygon([Polygon(Contour([Point(0, 1), Point(1, 0),
...                                                 Point(1, 1)]), [])])
>>> square = Multipolygon([Polygon(Contour([Point(0, 0), Point(1, 0),
...                                         Point(1, 1), Point(0, 1)]), [])])
>>> from clipping.planar import intersect_multipolygons
>>> all(intersect_multipolygons(square, triangle)
...     == intersect_multipolygons(triangle, square) == triangle
...     for triangle in (left_triangle, right_triangle))
True
>>> intersect_multipolygons(left_triangle, right_triangle) == Multipolygon([])
True
>>> from clipping.planar import complete_intersect_multipolygons
>>> all(complete_intersect_multipolygons(square, triangle)
...     == (Multipoint([]), Multisegment([]),
...         intersect_multipolygons(square, triangle))
...     for triangle in (left_triangle, right_triangle))
True
>>> (complete_intersect_multipolygons(left_triangle, right_triangle)
...  == (Multipoint([]), Multisegment([Segment(Point(0, 1), Point(1, 0))]),
...      Multipolygon([])))
True
>>> from clipping.planar import unite_multipolygons
>>> all(unite_multipolygons(square, triangle)
...     == unite_multipolygons(triangle, square)
...     == square
...     for triangle in (left_triangle, right_triangle))
True
>>> (unite_multipolygons(left_triangle, right_triangle)
...  == unite_multipolygons(right_triangle, left_triangle)
...  == square)
True
>>> from clipping.planar import subtract_multipolygons
>>> all(subtract_multipolygons(triangle, square) == Multipolygon([])
...     for triangle in (left_triangle, right_triangle))
True
>>> subtract_multipolygons(square, left_triangle) == right_triangle
True
>>> subtract_multipolygons(square, right_triangle) == left_triangle
True
>>> from clipping.planar import symmetric_subtract_multipolygons
>>> symmetric_subtract_multipolygons(left_triangle, right_triangle) == square
True
>>> symmetric_subtract_multipolygons(square, left_triangle) == right_triangle
True
>>> symmetric_subtract_multipolygons(square, right_triangle) == left_triangle
True

Development

Bumping version

Preparation

Install bump2version.

Pre-release

Choose which version number category to bump following semver specification.

Test bumping version

bump2version --dry-run --verbose $CATEGORY

where $CATEGORY is the target version number category name, possible values are patch/minor/major.

Bump version

bump2version --verbose $CATEGORY

This will set version to major.minor.patch-alpha.

Release

Test bumping version

bump2version --dry-run --verbose release

Bump version

bump2version --verbose release

This will set version to major.minor.patch.

Running tests

Install dependencies

python -m pip install -r requirements-tests.txt

Plain

pytest

Inside Docker container:

  • with CPython
    docker-compose --file docker-compose.cpython.yml up
    
  • with PyPy
    docker-compose --file docker-compose.pypy.yml up
    

Bash script (e.g. can be used in Git hooks):

  • with CPython

    ./run-tests.sh
    

    or

    ./run-tests.sh cpython
    
  • with PyPy

    ./run-tests.sh pypy
    

PowerShell script (e.g. can be used in Git hooks):

  • with CPython
    .\run-tests.ps1
    
    or
    .\run-tests.ps1 cpython
    
  • with PyPy
    .\run-tests.ps1 pypy
    

Project details


Download files

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

Files for clipping, version 1.4.0
Filename, size File type Python version Upload date Hashes
Filename, size clipping-1.4.0-py3-none-any.whl (27.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size clipping-1.4.0.tar.gz (22.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page