Skip to content

Commit b4f9060

Browse files
authored
refactor(viewer): refactoring internal logics (#264)
* refactor: update lidarseg seraching logic Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> * refactor: update rendering log logic Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> * refactor: replace assert into TypeError Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> * refactor: use direct truthiness Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> --------- Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
1 parent b7c56e4 commit b4f9060

2 files changed

Lines changed: 26 additions & 33 deletions

File tree

t4_devkit/helper/rendering.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,17 @@ def __init__(self, t4: Tier4) -> None:
5959
self._label2id: dict[str, int] = {
6060
category.name: category.index for category in self._t4.category
6161
}
62-
self._sample_data_to_lidarseg_filename: dict[str, str] | None = (
63-
{lidarseg.sample_data_token: lidarseg.filename for lidarseg in self._t4.lidarseg}
64-
if self._t4.lidarseg
65-
else None
66-
)
62+
self._sample_data_to_lidarseg_filename: dict[str, str] = {
63+
lidarseg.sample_data_token: lidarseg.filename for lidarseg in self._t4.lidarseg
64+
}
6765

6866
self._executor = concurrent.futures.ThreadPoolExecutor()
6967

7068
def _has_lidarseg(self) -> bool:
71-
return self._sample_data_to_lidarseg_filename is not None
69+
return bool(self._sample_data_to_lidarseg_filename)
70+
71+
def _find_lidarseg_file(self, sample_data_token: str) -> str | None:
72+
return self._sample_data_to_lidarseg_filename.get(sample_data_token)
7273

7374
def _init_viewer(
7475
self,
@@ -432,13 +433,10 @@ def _render_single_lidar(first_lidar_token: str) -> None:
432433

433434
# render segmentation pointcloud if available, otherwise render raw pointcloud
434435
if color_mode == PointCloudColorMode.SEGMENTATION:
435-
if not (
436-
self._has_lidarseg()
437-
and sample_data.token in self._sample_data_to_lidarseg_filename
438-
):
436+
label_filename = self._find_lidarseg_file(sample_data.token)
437+
if label_filename is None:
439438
continue
440439

441-
label_filename = self._sample_data_to_lidarseg_filename[sample_data.token]
442440
pointcloud = SegmentationPointCloud.from_file(
443441
point_filepath=osp.join(self._t4.data_root, sample_data.filename),
444442
label_filepath=osp.join(self._t4.data_root, label_filename),

t4_devkit/viewer/viewer.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -427,18 +427,19 @@ def render_pointcloud(
427427
# TODO(ktro2828): add support of rendering pointcloud on images
428428
rr.set_time_seconds(self.config.timeline, seconds)
429429

430+
entity_path = format_entity(self.config.ego_entity, channel)
430431
if color_mode == PointCloudColorMode.SEGMENTATION:
431-
assert isinstance(pointcloud, SegmentationPointCloud)
432-
rr.log(
433-
format_entity(self.config.ego_entity, channel),
434-
rr.Points3D(pointcloud.points[:3].T, class_ids=pointcloud.labels),
435-
)
432+
if not isinstance(pointcloud, SegmentationPointCloud):
433+
raise TypeError(
434+
f"Expected SegmentationPointCloud instance, but got {type(pointcloud)}"
435+
)
436+
437+
entity = rr.Points3D(pointcloud.points[:3].T, class_ids=pointcloud.labels)
436438
else:
437439
colors = pointcloud_color(pointcloud, color_mode=color_mode)
438-
rr.log(
439-
format_entity(self.config.ego_entity, channel),
440-
rr.Points3D(pointcloud.points[:3].T, colors=colors),
441-
)
440+
entity = rr.Points3D(pointcloud.points[:3].T, colors=colors)
441+
442+
rr.log(entity_path, entity)
442443

443444
@_check_spatial2d
444445
def render_image(self, seconds: float, camera: str, image: str | NDArrayU8) -> None:
@@ -451,10 +452,10 @@ def render_image(self, seconds: float, camera: str, image: str | NDArrayU8) -> N
451452
"""
452453
rr.set_time_seconds(self.config.timeline, seconds)
453454

454-
if isinstance(image, str):
455-
rr.log(format_entity(self.config.ego_entity, camera), rr.ImageEncoded(path=image))
456-
else:
457-
rr.log(format_entity(self.config.ego_entity, camera), rr.Image(image))
455+
entity_path = format_entity(self.config.ego_entity, camera)
456+
entity = rr.ImageEncoded(path=image) if isinstance(image, str) else rr.Image(image)
457+
458+
rr.log(entity_path, entity)
458459

459460
@overload
460461
def render_ego(self, ego_pose: EgoPose) -> None:
@@ -519,18 +520,12 @@ def _render_ego_without_schema(
519520
),
520521
)
521522

523+
entity_path = self.config.geocoordinate_entity
522524
if geocoordinate is not None:
523-
latitude, longitude, _ = geocoordinate
524-
rr.log(
525-
self.config.geocoordinate_entity,
526-
rr.GeoPoints(lat_lon=(latitude, longitude)),
527-
)
525+
rr.log(entity_path, rr.GeoPoints(lat_lon=geocoordinate[:2]))
528526
elif self.latlon is not None:
529527
latitude, longitude = calculate_geodetic_point(translation, self.latlon)
530-
rr.log(
531-
self.config.geocoordinate_entity,
532-
rr.GeoPoints(lat_lon=(latitude, longitude)),
533-
)
528+
rr.log(entity_path, rr.GeoPoints(lat_lon=(latitude, longitude)))
534529

535530
@overload
536531
def render_calibration(

0 commit comments

Comments
 (0)