Use scipy.sparse.coo_matrix instead of numpy.histogram2d to get a decent speedup
This commit is contained in:
parent
4dcdbf2f37
commit
12fdc5401e
@ -8,6 +8,7 @@ See the documentation for raster(...) for details.
|
|||||||
import numpy
|
import numpy
|
||||||
from numpy import r_, c_, logical_and, diff, floor, ceil, ones, zeros, vstack, hstack,\
|
from numpy import r_, c_, logical_and, diff, floor, ceil, ones, zeros, vstack, hstack,\
|
||||||
full_like, newaxis
|
full_like, newaxis
|
||||||
|
from scipy import sparse
|
||||||
|
|
||||||
__author__ = 'Jan Petykiewicz'
|
__author__ = 'Jan Petykiewicz'
|
||||||
|
|
||||||
@ -157,16 +158,8 @@ def raster(poly_xy: numpy.ndarray,
|
|||||||
cover = diff(poly[:, 1], axis=0)[non_edge] / diff(grid_y)[y_sub]
|
cover = diff(poly[:, 1], axis=0)[non_edge] / diff(grid_y)[y_sub]
|
||||||
area = (endpoint_avg[non_edge, 0] - grid_x[x_sub]) * cover / diff(grid_x)[x_sub]
|
area = (endpoint_avg[non_edge, 0] - grid_x[x_sub]) * cover / diff(grid_x)[x_sub]
|
||||||
|
|
||||||
hist_range = [[0, num_xy_px[0]], [0, num_xy_px[1]]]
|
poly_grid = sparse.coo_matrix((-area, (x_sub, y_sub)), shape=num_xy_px).toarray()
|
||||||
poly_grid = numpy.histogram2d(x_sub, y_sub, bins=num_xy_px, range=hist_range, weights=-area)[0]
|
cover_grid = sparse.coo_matrix((cover, (x_sub, y_sub)), shape=num_xy_px).toarray()
|
||||||
cover_grid = numpy.histogram2d(x_sub, y_sub, bins=num_xy_px, range=hist_range, weights=cover)[0]
|
poly_grid = poly_grid + cover_grid.cumsum(axis=0)
|
||||||
|
|
||||||
poly_grid += cover_grid.cumsum(axis=0)
|
|
||||||
|
|
||||||
# do other stuff for dealing with multiple polygons?
|
|
||||||
|
|
||||||
# # deal with the user inputting the vertices in the wrong order
|
|
||||||
# if poly_grid.sum() < 0:
|
|
||||||
# poly_grid = -poly_grid
|
|
||||||
|
|
||||||
return poly_grid
|
return poly_grid
|
||||||
|
5
setup.py
5
setup.py
@ -3,13 +3,14 @@
|
|||||||
from setuptools import setup
|
from setuptools import setup
|
||||||
|
|
||||||
setup(name='float_raster',
|
setup(name='float_raster',
|
||||||
version='0.1',
|
version='0.2',
|
||||||
description='High-precision anti-aliasing polygon rasterizer',
|
description='High-precision anti-aliasing polygon rasterizer',
|
||||||
author='Jan Petykiewicz',
|
author='Jan Petykiewicz',
|
||||||
author_email='anewusername@gmail.com',
|
author_email='anewusername@gmail.com',
|
||||||
url='https://mpxd.net/gogs/jan/float_raster',
|
url='https://mpxd.net/gogs/jan/float_raster',
|
||||||
py_modules=['float_raster'],
|
py_modules=['float_raster'],
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'numpy'
|
'numpy',
|
||||||
|
'scipy',
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user