Skip to content

Commit

Permalink
adding padding option
Browse files Browse the repository at this point in the history
  • Loading branch information
brimoor committed May 24, 2024
1 parent dc7f082 commit 8abd806
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions fiftyone/utils/utils3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ def compute_orthographic_projection_images(
subsampling_rate=None,
projection_normal=None,
bounds=None,
padding=None,
skip_failures=False,
progress=None,
):
Expand Down Expand Up @@ -600,6 +601,11 @@ def compute_orthographic_projection_images(
to generate each map. Either element of the tuple or any/all of its
values can be None, in which case a tight crop of the point cloud
along the missing dimension(s) are used
padding (None): a relative padding(s) in ``[0, 1]]`` to apply to the
field of view bounds prior to projection. Can either be a single
value to apply in all directions or a ``[padx, pady, padz]``. For
example, pass ``padding=0.05`` with no ``bounds`` to project onto
a tight crop of each point cloud with 5% padding around it
skip_failures (False): whether to gracefully continue without raising
an error if a projection fails
progress (None): whether to render a progress bar (True/False), use the
Expand Down Expand Up @@ -647,6 +653,7 @@ def compute_orthographic_projection_images(
subsampling_rate=subsampling_rate,
projection_normal=projection_normal,
bounds=bounds,
padding=padding,
)
except Exception as e:
if not skip_failures:
Expand Down Expand Up @@ -680,6 +687,7 @@ def compute_orthographic_projection_image(
subsampling_rate=None,
projection_normal=None,
bounds=None,
padding=None,
):
"""Generates an orthographic projection image for the given PCD file onto
the specified plane (default xy plane).
Expand Down Expand Up @@ -714,6 +722,11 @@ def compute_orthographic_projection_image(
the projected plane. Either element of the tuple or any/all of its
values can be None, in which case a tight crop of the point cloud
along the missing dimension(s) are used
padding (None): a relative padding(s) in ``[0, 1]]`` to apply to the
field of view bounds prior to projection. Can either be a single
value to apply in all directions or a ``[padx, pady, padz]``. For
example, pass ``padding=0.05`` with no ``bounds`` to project onto
a tight crop of the point cloud with 5% padding around it
Returns:
a tuple of
Expand All @@ -731,6 +744,7 @@ def compute_orthographic_projection_image(
filepath,
size=size,
bounds=bounds,
padding=padding,
projection_normal=projection_normal,
subsampling_rate=subsampling_rate,
)
Expand Down Expand Up @@ -828,6 +842,7 @@ def _parse_point_cloud(
filepath,
size=None,
bounds=None,
padding=None,
projection_normal=None,
subsampling_rate=None,
):
Expand Down Expand Up @@ -876,19 +891,24 @@ def _parse_point_cloud(
_max_bound = pc.get_max_bound()
max_bound = _fill_none(max_bound, _max_bound)

min_bound = np.asarray(min_bound, dtype=float)
max_bound = np.asarray(max_bound, dtype=float)

if padding is not None:
pad = 0.5 * np.asarray(padding) * (max_bound - min_bound)
min_bound -= pad
max_bound += pad

# Ensure bbox will not have 0 volume by adding a small value if max_bound
# and min_bound are close to each other
delta = np.isclose(
np.asarray(max_bound) - np.asarray(min_bound), 0
) * np.repeat(0.000001, 3)
max_bound += delta
# and min_bound are close to each other
max_bound += 1e-6 * np.isclose(max_bound - min_bound, 0)

bbox = o3d.geometry.AxisAlignedBoundingBox(
min_bound=min_bound, max_bound=max_bound
)

# crop bounds and translate so that min bound is at the origin
pc = pc.crop(bbox).translate((-min_bound[0], -min_bound[1], -min_bound[2]))
# Crop bounds and translate so that min bound is at the origin
pc = pc.crop(bbox).translate(-min_bound)

if subsampling_rate is not None and subsampling_rate > 0:
pc = pc.uniform_down_sample(subsampling_rate)
Expand All @@ -902,9 +922,9 @@ def _parse_point_cloud(
width, height = None, None

metadata = OrthographicProjectionMetadata(
min_bound=min_bound,
max_bound=max_bound,
normal=projection_normal,
min_bound=list(min_bound),
max_bound=list(max_bound),
normal=list(projection_normal),
width=width,
height=height,
)
Expand Down

0 comments on commit 8abd806

Please sign in to comment.