Get distance at locations
returns the distances to the closest boundaries for a subregion of the map layer,
dist = distance(
map. The subregion starts in the corner location
cornerLocation in the coordinate frame
When specifying a corner location and map size,
determines the distances between cell centers within the rectangular query
Get Distance to Nearest Obstacles in Signed Distance Map
exampleMaps MAT file.
Create a signed distance map using the
sdm = signedDistanceMap(simpleMap,InterpolationMethod="none"); show(sdm,BoundaryColor=[0 0 0],Colorbar="on");
setMapData(sdm,[7 1],zeros(5,5)) getMapData(sdm,[9 2])
ans = logical 0
show(sdm,BoundaryColor=[0 0 0],Colorbar="on");
Find the closest boundary to the coordinate,
[16.25 6.25] and calculate the distance to the nearest boundary.
coord = [16.25 6.25]; boundary = closestBoundary(sdm,coord,"world")
boundary = boundary(:,:,1) = 20.5000 boundary(:,:,2) = 6.5000
dist = distance(sdm,coord)
dist = 4
Plot the line between the queried point and the closest obstacle cell center
hold on plot([coord(1) boundary(:,:,1)],[coord(2) boundary(:,:,2)],"-r",Marker=".",MarkerSize=10)
Note that even though distance from the queried point appears greater than
4 when plotted, the distance function calculates the distance from the nearest cell center of the queried point.
map — Signed distance map
Signed distance map, specified as a
location — World or local coordinates, or grid locations
World or local coordinates, or grid locations, specified as an
N-by-2 matrix. N is the number of locations. The
format of the rows depends on the value of the
"world"— [x y] coordinates in the world frame.
"local"— [x y] coordinates in the local frame.
"grid"— [row column] location in the grid frame.
cornerLocation — Location of output matrix
Location of output matrix, specified as a two-element vector of coordinates in the
y]. The location is in world or local coordinates if the
frame argument is set to
"local", respectively. When
cornerLocation is the top-left corner
of the world frame.
mapSize — Subregion map size
two-element vector |
frame — Coordinate frame
"world" (default) |
Coordinate frame, specified as one of these options:
"world"— Specify coordinates in the world frame.
"local"— Specify coordinates in the local frame.
"grid"— Specify grid locations in the grid frame.
dist — Distances to closest obstacles
L-element column vector | M-by-N-by-2 array
Distances to closest obstacles, returned as an L-element column vector for location queries and an M-by-N-by-2 array for subregion queries. L is the number of locations queried. M and N are the dimensions of the map or specified map subregion.
frame is either
distance returns distances in
distance returns distances in grid space.
isValid — Points in boundary
Points in boundary, returned as an L-element vector, where L is the total number of queried locations. Each element is a
true) if the point is within the boundaries, and
false) if the point is outside boundaries.
This output is only returned for point queries.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Introduced in R2023a