Skip to content

Commit

Permalink
gridshow vs imshow, adds meshgrid and idx array methods
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldugas committed Jul 26, 2021
1 parent 9a3831e commit f15dd0a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
49 changes: 49 additions & 0 deletions CMap2D.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,55 @@ cdef class CMap2D:
min_distances[self.occupancy() > self.thresh_free] *= -1.
return min_distances

def as_idx_array(self, axis=None):
""" Returns an array of shape a containing indices for a
Parameters
----------
a : array_like
Array to be reshaped.
axis : int, None, list of ints, or 'all'
Axis along which to return indices. If None, the flat index.
Returns
-------
result : ndarray
Array of shape (a.shape, len(axis))
if axis is None or a single int, (a.shape,)
"""
a = self.occupancy()
if axis is None:
return np.arange(len(a.flatten())).reshape(a.shape)
idxs = np.array(np.where(np.ones(a.shape))).T.reshape(a.shape + (-1,))
if axis == "all":
return idxs
return idxs[..., axis]

def as_xy_array(self):
""" returns an array containing xy coordinates at every cell """
idxarray = self.as_idx_array(axis='all')
flatidxarray = idxarray.reshape((-1, 2))
flatxyarray = self.ij_to_xy(flatidxarray)
xyarray = flatxyarray.reshape(idxarray.shape)
return xyarray

def as_meshgrid_ij(self):
""" returns a tuple of 2 arrays with same dimension as map, containing respectively
i and j indices of each cell
ii, jj = map2d.as_meshgrid_ij()
"""
idxarray = self.as_idx_array(axis='all')
ii, jj = np.moveaxis(idxarray, -1, 0)
return (ii, jj)

def as_meshgrid_xy(self):
""" returns a tuple of 2 arrays with same dimension as map, containing respectively
x and y indices of each cell
xx, yy = map2d.as_meshgrid_xy()
"""
xx, yy = np.moveaxis(self.as_xy_array(), -1, 0)
return (xx, yy)

cpdef as_coarse_map2d(self, n=1):
# recursion to provide a convenient way to coarsen x times
if n > 1:
Expand Down
Binary file added media/gridshow_vs_imshow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
long_description=long_description,
long_description_content_type='text/markdown',
url="https://github.com/danieldugas/pymap2d",
version='0.1.7',
version='0.1.8',
py_modules=['map2d', 'pose2d', 'circular_index', 'map2d_ros_tools'],
ext_modules=cythonize("CMap2D.pyx", annotate=True),
include_dirs=[numpy.get_include()],
Expand Down
37 changes: 37 additions & 0 deletions test/example_gridshow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import numpy as np
import matplotlib.pyplot as plt
from CMap2D import gridshow, CMap2D

cmap2d = CMap2D().as_coarse_map2d(3)
cmap2d.set_resolution(1.)
cmap2d.origin[0] = -5
cmap2d.origin[1] = -5
cmap2d._occupancy[2:4, 8:10] = 1.
cmap2d._occupancy[8:10, 8:9] = 1.
ii, jj = cmap2d.as_meshgrid_ij()
xx, yy = cmap2d.as_meshgrid_xy()
rr = np.sqrt((xx-1) * (xx-1) + yy * yy)
smile = np.logical_and.reduce((
rr > 3,
rr < 5,
yy < 0,
))
for i, j in np.array(np.where(smile)).T:
cmap2d._occupancy[i, j] = 1.


fig, (ax1, ax2) = plt.subplots(1, 2)
plt.sca(ax1)
gridshow(cmap2d.occupancy(), extent=cmap2d.get_extent_xy())
plt.title("gridshow")
plt.xlabel("x")
plt.ylabel("y")
plt.sca(ax2)
plt.title("plt.imshow")
e = cmap2d.get_extent_xy()
imshow_extent = [e[2], e[3], e[1], e[0]]
plt.imshow(cmap2d.occupancy(), extent=imshow_extent)
plt.xlabel("y")
plt.ylabel("x (descending)")

plt.show()

0 comments on commit f15dd0a

Please sign in to comment.