From 12fdc5401eb2b754a8aa20004d5ce5b100e51c0c Mon Sep 17 00:00:00 2001 From: jan Date: Wed, 13 Jul 2016 16:39:37 -0700 Subject: [PATCH] Use scipy.sparse.coo_matrix instead of numpy.histogram2d to get a decent speedup --- float_raster.py | 15 ++++----------- setup.py | 5 +++-- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/float_raster.py b/float_raster.py index 0c54328..dd22abd 100644 --- a/float_raster.py +++ b/float_raster.py @@ -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 diff --git a/setup.py b/setup.py index d365ae5..9f727d2 100644 --- a/setup.py +++ b/setup.py @@ -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', ], )