Skip to content

Commit 5bb52b6

Browse files
committed
pep8 mypy
1 parent 5508588 commit 5bb52b6

File tree

10 files changed

+170
-154
lines changed

10 files changed

+170
-154
lines changed

.appveyor.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
image:
22
- Visual Studio 2017
3-
- Ubuntu
43

54
stack: python 3
65

.coveragerc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[run]
2+
omit =
3+
/home/travis/virtualenv/*
4+
5+
[report]
6+
exclude_lines =
7+
pragma: no cover
8+
def __repr__
9+
raise NotImplementedError
10+
raise ImportError
11+
raise ValueError
12+
raise TypeError

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: python
22
fast_finish: true
33

44
python:
5-
- 2.7
5+
- 3.5
66
- 3.6
77
- "3.7-dev"
88

@@ -43,7 +43,9 @@ install:
4343
- cd ..
4444

4545
script:
46-
- pytest -v
46+
- pytest -sv
47+
- flake8
48+
- mypy . --ignore-missing-imports
4749

4850
after_success:
4951
- if [[ $TRAVIS_PYTHON_VERSION == 3.6* ]]; then

DemoLineclip.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
make box with corners LL/UR (1,3) (4,5)
77
and line segment with ends (0,0) (4,6)
88
"""
9-
#%% LOWER to UPPER test
9+
# %% LOWER to UPPER test
1010
x1, y1, x2, y2 = cohensutherland(1, 5, 4, 3,
1111
0, 0, 4, 6)
1212

13-
np.testing.assert_array_almost_equal([x1,y1,x2,y2],[2, 3, 3.3333333333333,5])
14-
#%% no intersection test
15-
x1,y1,x2,y2 = cohensutherland(1,5, 4,3,
16-
0,0.1,0,0.1)
17-
assert x1==y1==x2==y2==None
18-
#%% left to right test
19-
x1,y1,x2,y2 = cohensutherland(1,5,4,3,
20-
0,4,5,4)
21-
np.testing.assert_array_almost_equal([x1,y1,x2,y2],[1, 4, 4, 4])
13+
np.testing.assert_array_almost_equal([x1, y1, x2, y2], [2, 3, 3.3333333333333, 5])
14+
# %% no intersection test
15+
x1, y1, x2, y2 = cohensutherland(1, 5, 4, 3,
16+
0, 0.1, 0, 0.1)
17+
18+
assert x1 is None and y1 is None and x2 is None and y2 is None
19+
# %% left to right test
20+
x1, y1, x2, y2 = cohensutherland(1, 5, 4, 3,
21+
0, 4, 5, 4)
22+
np.testing.assert_array_almost_equal([x1, y1, x2, y2], [1, 4, 4, 4])

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[![Travis CI](https://travis-ci.org/scivision/lineclipping-python-fortran.svg?branch=master)](https://travis-ci.org/scivision/lineclipping-python-fortran)
2+
[![AppVeyor](https://ci.appveyor.com/api/projects/status/cr0omkhjvgwcyxiy?svg=true)](https://ci.appveyor.com/project/scivision/lineclipping-python-fortran)
3+
4+
# Line clipping
5+
6+
- lineClipping.jl Cohen-Sutherland line clipping algorithm for Julia.
7+
Input scalars, output intersection length, or `None` if no intersection.
8+
- lineclipping.f90 Cohen-Sutherland line clipping algorithm for
9+
massively parallel coarray modern Fortran. Input scalars or arrays,
10+
output intersections or `NaN` if no intersection.
11+
- lineClipping.py Cohen-Sutherland line clipping algorithm for Python.
12+
Input scalars, output intersection length, or `None` if no intersection.
13+
14+
## Install
15+
16+
### Python
17+
18+
python -m pip install -e .
19+
20+
### Fortran
21+
22+
If you want to use the Fortran Cohen-Sutherland line clipping modules
23+
directly (optional):
24+
25+
cd bin
26+
cmake ..
27+
make
28+
29+
## Usage
30+
31+
The main difference with textbook implementations is that I return a
32+
sentinel value (NaN, None, nothing) if there's no intersection of line
33+
with box.
34+
35+
### Python
36+
37+
```python
38+
import pylineclip.lineclipping as lc
39+
40+
x3,y3,x4,y4 = lc.cohensutherland((xmin, ymax, xmax, ymin, x1, y1, x2, y2)
41+
```
42+
43+
If no intersection, `(None, None, None, None)` is returned.
44+
45+
### Fortran
46+
47+
lineclipping.f90 has two subroutines.
48+
Pick Ccohensutherland if you're calling from C/C++/Python, which cannot tolerate assummed-shape arrays.
49+
It's a slim wrapper to cohensutherland which is elemental (can handle scalar or any rank array).
50+
51+
Fortran programs will simply use
52+
53+
```fortran
54+
use lineclipping
55+
call cohensutherland(xmin,ymax,xmax,ymin,x1,y1,x2,y2)
56+
```
57+
58+
The arguments are:
59+
60+
INPUTS
61+
------
62+
xmin,ymax,xmax,ymin: upper left and lower right corners of box (pixel coordinates)
63+
64+
INOUT
65+
-----
66+
x1,y1,x2,y2:
67+
in - endpoints of line
68+
out - intersection points with box. If no intersection, all NaN
69+
70+
### Julia
71+
72+
Simliar to Python, except `nothing` is returned if no intersection
73+
found.
74+
75+
```julia
76+
cohensutherland(xmin, ymax, xmax, ymin, x1, y1, x2, y2)
77+
```

README.rst

Lines changed: 0 additions & 85 deletions
This file was deleted.

pylineclip/__init__.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
#!/usr/bin/env python
2-
from __future__ import division
3-
#from numba import jit
2+
from typing import Union, Tuple
3+
# from numba import jit
44
'''
55
The MIT License (MIT)
66
Copyright (c) 2014 Michael Hirsch
77
reference: http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
8-
* The best way to Numba JIT this would probably be in the function calling this,
8+
* The best way to Numba JIT this would probably be in the function calling this,
99
to include the loop itself inside the jit decoration.
1010
'''
11-
#@jit
12-
def cohensutherland(xmin, ymax, xmax, ymin, x1, y1, x2, y2):
11+
12+
# @jit
13+
14+
15+
def cohensutherland(xmin: float, ymax: float, xmax: float, ymin: float,
16+
x1: float, y1: float, x2: float, y2: float) -> Tuple[
17+
Union[None, float], Union[None, float], Union[None, float], Union[None, float]]:
1318
"""Clips a line to a rectangular area.
1419
1520
This implements the Cohen-Sutherland line clipping algorithm. xmin,
@@ -21,11 +26,11 @@ def cohensutherland(xmin, ymax, xmax, ymin, x1, y1, x2, y2):
2126
four None values will be returned as tuple. Otherwise a tuple of the
2227
clipped line points will be returned in the form (cx1, cy1, cx2, cy2).
2328
"""
24-
INSIDE,LEFT, RIGHT, LOWER, UPPER = 0,1, 2, 4, 8
29+
INSIDE, LEFT, RIGHT, LOWER, UPPER = 0, 1, 2, 4, 8
2530

2631
def _getclip(xa, ya):
27-
#if dbglvl>1: print('point: '),; print(xa,ya)
28-
p = INSIDE #default is inside
32+
# if dbglvl>1: print('point: '),; print(xa,ya)
33+
p = INSIDE # default is inside
2934

3035
# consider x
3136
if xa < xmin:
@@ -35,29 +40,29 @@ def _getclip(xa, ya):
3540

3641
# consider y
3742
if ya < ymin:
38-
p |= LOWER # bitwise OR
43+
p |= LOWER # bitwise OR
3944
elif ya > ymax:
40-
p |= UPPER #bitwise OR
45+
p |= UPPER # bitwise OR
4146
return p
4247

4348
# check for trivially outside lines
4449
k1 = _getclip(x1, y1)
4550
k2 = _getclip(x2, y2)
4651

47-
#%% examine non-trivially outside points
48-
#bitwise OR |
49-
while (k1 | k2) != 0: # if both points are inside box (0000) , ACCEPT trivial whole line in box
52+
# %% examine non-trivially outside points
53+
# bitwise OR |
54+
while (k1 | k2) != 0: # if both points are inside box (0000) , ACCEPT trivial whole line in box
5055

5156
# if line trivially outside window, REJECT
52-
if (k1 & k2) != 0: #bitwise AND &
53-
#if dbglvl>1: print(' REJECT trivially outside box')
54-
#return nan, nan, nan, nan
57+
if (k1 & k2) != 0: # bitwise AND &
58+
# if dbglvl>1: print(' REJECT trivially outside box')
59+
# return nan, nan, nan, nan
5560
return None, None, None, None
5661

57-
#non-trivial case, at least one point outside window
62+
# non-trivial case, at least one point outside window
5863
# this is not a bitwise or, it's the word "or"
59-
opt = k1 or k2 # take first non-zero point, short circuit logic
60-
if opt & UPPER: #these are bitwise ANDS
64+
opt = k1 or k2 # take first non-zero point, short circuit logic
65+
if opt & UPPER: # these are bitwise ANDS
6166
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1)
6267
y = ymax
6368
elif opt & LOWER:
@@ -75,11 +80,10 @@ def _getclip(xa, ya):
7580
if opt == k1:
7681
x1, y1 = x, y
7782
k1 = _getclip(x1, y1)
78-
#if dbglvl>1: print('checking k1: ' + str(x) + ',' + str(y) + ' ' + str(k1))
83+
# if dbglvl>1: print('checking k1: ' + str(x) + ',' + str(y) + ' ' + str(k1))
7984
elif opt == k2:
80-
#if dbglvl>1: print('checking k2: ' + str(x) + ',' + str(y) + ' ' + str(k2))
85+
# if dbglvl>1: print('checking k2: ' + str(x) + ',' + str(y) + ' ' + str(k2))
8186
x2, y2 = x, y
8287
k2 = _getclip(x2, y2)
83-
84-
return x1, y1, x2, y2
8588

89+
return x1, y1, x2, y2

setup.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[metadata]
2+
description-file = README.md
3+
4+
[flake8]
5+
max-line-length = 132
6+
exclude = .git,__pycache__,docs/,build/,dist/,archive/

setup.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
#!/usr/bin/env python
2+
from setuptools import setup, find_packages
3+
24
install_requires = ['numpy']
3-
tests_require=['pytest','nose','coveralls']
4-
# %%
5-
from setuptools import setup,find_packages
5+
tests_require = ['pytest', 'coveralls', 'flake8', 'mypy']
6+
67

78
setup(name='pylineclip',
89
packages=find_packages(),
910
description='Line clipping: Cohen-Sutherland',
10-
long_description=open('README.rst').read(),
11+
long_description=open('README.md').read(),
12+
long_description_content_type="text/markdown",
1113
version='0.9.0',
1214
author='Michael Hirsch, Ph.D.',
1315
url='https://github.com/scivision/lineclipping-python-fortran',
1416
classifiers=[
15-
'Development Status :: 4 - Beta',
16-
'Environment :: Console',
17-
'Intended Audience :: Science/Research',
18-
'License :: OSI Approved :: MIT License',
19-
'Operating System :: OS Independent',
20-
'Programming Language :: Python :: 2.7',
21-
'Programming Language :: Python :: 3.4',
22-
'Programming Language :: Python :: 3.5',
23-
'Programming Language :: Python :: 3.6',
24-
'Programming Language :: Python :: 3.7',
25-
'Topic :: Scientific/Engineering :: Visualization',
17+
'Development Status :: 4 - Beta',
18+
'Environment :: Console',
19+
'Intended Audience :: Science/Research',
20+
'License :: OSI Approved :: MIT License',
21+
'Operating System :: OS Independent',
22+
'Programming Language :: Python :: 3.5',
23+
'Programming Language :: Python :: 3.6',
24+
'Programming Language :: Python :: 3.7',
25+
'Topic :: Scientific/Engineering :: Visualization',
2626
],
2727
install_requires=install_requires,
2828
tests_require=tests_require,
29-
extras_require={'tests':tests_require},
30-
python_requires='>=2.7',
29+
extras_require={'tests': tests_require},
30+
python_requires='>=3.5',
3131
scripts=['DemoLineclip.py'],
3232
include_package_data=True,
33-
)
34-
33+
)

0 commit comments

Comments
 (0)