Skip to content

Commit 8bf26f4

Browse files
committed
Added additional pointcloud functions.
1 parent d1b1b84 commit 8bf26f4

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

pyrep/objects/vision_sensor.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,33 +135,42 @@ def capture_depth(self, in_meters=False) -> np.ndarray:
135135
self._handle, self.resolution, in_meters)
136136

137137
def capture_pointcloud(self) -> np.ndarray:
138-
"""Retrieves pointcloud in word frame.
138+
"""Retrieves point cloud in word frame.
139139
140140
:return: A numpy array of size (width, height, 3)
141141
"""
142+
d = self.capture_depth(in_meters=True)
143+
return self.pointcloud_from_depth(d)
142144

143-
res = np.array(self.get_resolution())
145+
def pointcloud_from_depth(self, depth: np.ndarray) -> np.ndarray:
146+
"""Converts depth (in meters) to point cloud in word frame.
144147
148+
:return: A numpy array of size (width, height, 3)
149+
"""
145150
intrinsics = self.get_intrinsic_matrix()
146-
d = self.capture_depth(in_meters=True)
147-
upc = _create_uniform_pixel_coords_image(d.shape)
148-
pc = upc * np.expand_dims(d, -1)
151+
return VisionSensor.pointcloud_from_depth_and_camera_params(
152+
depth, self.get_matrix(), intrinsics)
149153

150-
# Get extrinsics
151-
C = np.expand_dims(self.get_position(), 0).T
152-
R = self.get_matrix()[:-1, :-1]
154+
@staticmethod
155+
def pointcloud_from_depth_and_camera_params(
156+
depth: np.ndarray, extrinsics: np.ndarray,
157+
intrinsics: np.ndarray) -> np.ndarray:
158+
"""Converts depth (in meters) to point cloud in word frame.
159+
:return: A numpy array of size (width, height, 3)
160+
"""
161+
upc = _create_uniform_pixel_coords_image(depth.shape)
162+
pc = upc * np.expand_dims(depth, -1)
163+
C = np.expand_dims(extrinsics[:3, 3], 0).T
164+
R = extrinsics[:3, :3]
153165
R_inv = R.T # inverse of rot matrix is transpose
154166
R_inv_C = np.matmul(R_inv, C)
155167
extrinsics = np.concatenate((R_inv, -R_inv_C), -1)
156-
157168
cam_proj_mat = np.matmul(intrinsics, extrinsics)
158169
cam_proj_mat_homo = np.concatenate(
159170
[cam_proj_mat, [np.array([0, 0, 0, 1])]])
160171
cam_proj_mat_inv = np.linalg.inv(cam_proj_mat_homo)[0:3]
161-
162172
world_coords_homo = np.expand_dims(_pixel_to_world_coords(
163173
pc, cam_proj_mat_inv), 0)
164-
165174
world_coords = world_coords_homo[..., :-1][0]
166175
return world_coords
167176

pyrep/pyrep.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,6 @@
1515
import warnings
1616

1717

18-
def fileno(file_or_fd):
19-
fd = getattr(file_or_fd, 'fileno', lambda: file_or_fd)()
20-
if not isinstance(fd, int):
21-
raise ValueError("Expected a file (`.fileno()`) or a file descriptor")
22-
return fd
23-
24-
25-
@contextmanager
26-
def stdout_redirected(to=os.devnull, stdout=None):
27-
if stdout is None:
28-
stdout = sys.stdout
29-
stdout_fd = fileno(stdout)
30-
with os.fdopen(os.dup(stdout_fd), 'wb') as copied:
31-
stdout.flush()
32-
try:
33-
os.dup2(fileno(to), stdout_fd)
34-
except ValueError:
35-
with open(to, 'wb') as to_file:
36-
os.dup2(to_file.fileno(), stdout_fd)
37-
try:
38-
yield stdout
39-
finally:
40-
stdout.flush()
41-
os.dup2(copied.fileno(), stdout_fd)
42-
43-
4418
class PyRep(object):
4519
"""Used for interfacing with the CoppeliaSim simulation.
4620

0 commit comments

Comments
 (0)