in shap_e/rendering/point_cloud.py [0:0]
def from_rgbd(cls, vd: ViewData, num_views: Optional[int] = None) -> "PointCloud":
"""
Construct a point cloud from the given view data.
The data must have a depth channel. All other channels will be stored
in the `channels` attribute of the result.
Pixels in the rendered views are not converted into points in the cloud
if they have infinite depth or less than 1.0 alpha.
"""
channel_names = vd.channel_names
if "D" not in channel_names:
raise ValueError(f"view data must have depth channel")
depth_index = channel_names.index("D")
all_coords = []
all_channels = defaultdict(list)
if num_views is None:
num_views = vd.num_views
for i in range(num_views):
camera, channel_values = vd.load_view(i, channel_names)
flat_values = channel_values.reshape([-1, len(channel_names)])
# Create an array of integer (x, y) image coordinates for Camera methods.
image_coords = camera.image_coords()
# Select subset of pixels that have meaningful depth/color.
image_mask = np.isfinite(flat_values[:, depth_index])
if "A" in channel_names:
image_mask = image_mask & (flat_values[:, channel_names.index("A")] >= 1 - 1e-5)
image_coords = image_coords[image_mask]
flat_values = flat_values[image_mask]
# Use the depth and camera information to compute the coordinates
# corresponding to every visible pixel.
camera_rays = camera.camera_rays(image_coords)
camera_origins = camera_rays[:, 0]
camera_directions = camera_rays[:, 1]
depth_dirs = camera.depth_directions(image_coords)
ray_scales = flat_values[:, depth_index] / np.sum(
camera_directions * depth_dirs, axis=-1
)
coords = camera_origins + camera_directions * ray_scales[:, None]
all_coords.append(coords)
for j, name in enumerate(channel_names):
if name != "D":
all_channels[name].append(flat_values[:, j])
if len(all_coords) == 0:
return cls(coords=np.zeros([0, 3], dtype=np.float32), channels={})
return cls(
coords=np.concatenate(all_coords, axis=0),
channels={k: np.concatenate(v, axis=0) for k, v in all_channels.items()},
)