Use scipy.sparse.coo_matrix instead of numpy.histogram2d to get a decent speedup

release
jan 8 år sedan
förälder 4dcdbf2f37
incheckning 12fdc5401e

@ -8,6 +8,7 @@ See the documentation for raster(...) for details.
import numpy
from numpy import r_, c_, logical_and, diff, floor, ceil, ones, zeros, vstack, hstack,\
full_like, newaxis
from scipy import sparse
__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]
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 = numpy.histogram2d(x_sub, y_sub, bins=num_xy_px, range=hist_range, weights=-area)[0]
cover_grid = numpy.histogram2d(x_sub, y_sub, bins=num_xy_px, range=hist_range, weights=cover)[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
poly_grid = sparse.coo_matrix((-area, (x_sub, y_sub)), shape=num_xy_px).toarray()
cover_grid = sparse.coo_matrix((cover, (x_sub, y_sub)), shape=num_xy_px).toarray()
poly_grid = poly_grid + cover_grid.cumsum(axis=0)
return poly_grid

@ -3,13 +3,14 @@
from setuptools import setup
setup(name='float_raster',
version='0.1',
version='0.2',
description='High-precision anti-aliasing polygon rasterizer',
author='Jan Petykiewicz',
author_email='anewusername@gmail.com',
url='https://mpxd.net/gogs/jan/float_raster',
py_modules=['float_raster'],
install_requires=[
'numpy'
'numpy',
'scipy',
],
)

Laddar…
Avbryt
Spara