in safety_gym/envs/engine.py [0:0]
def draw_placement(self, placements, keepout):
'''
Sample an (x,y) location, based on potential placement areas.
Summary of behavior:
'placements' is a list of (xmin, xmax, ymin, ymax) tuples that specify
rectangles in the XY-plane where an object could be placed.
'keepout' describes how much space an object is required to have
around it, where that keepout space overlaps with the placement rectangle.
To sample an (x,y) pair, first randomly select which placement rectangle
to sample from, where the probability of a rectangle is weighted by its
area. If the rectangles are disjoint, there's an equal chance the (x,y)
location will wind up anywhere in the placement space. If they overlap, then
overlap areas are double-counted and will have higher density. This allows
the user some flexibility in building placement distributions. Finally,
randomly draw a uniform point within the selected rectangle.
'''
if placements is None:
choice = self.constrain_placement(self.placements_extents, keepout)
else:
# Draw from placements according to placeable area
constrained = []
for placement in placements:
xmin, ymin, xmax, ymax = self.constrain_placement(placement, keepout)
if xmin > xmax or ymin > ymax:
continue
constrained.append((xmin, ymin, xmax, ymax))
assert len(constrained), 'Failed to find any placements with satisfy keepout'
if len(constrained) == 1:
choice = constrained[0]
else:
areas = [(x2 - x1)*(y2 - y1) for x1, y1, x2, y2 in constrained]
probs = np.array(areas) / np.sum(areas)
choice = constrained[self.rs.choice(len(constrained), p=probs)]
xmin, ymin, xmax, ymax = choice
return np.array([self.rs.uniform(xmin, xmax), self.rs.uniform(ymin, ymax)])