Skip to content

Commit 0b0f958

Browse files
committed
feat: add support of rendering point cloud coloring with intensity
Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
1 parent 682cbfb commit 0b0f958

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

t4_devkit/viewer/color.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,59 @@
11
from __future__ import annotations
22

3+
from enum import Enum, unique
34
from typing import TYPE_CHECKING
45

56
import matplotlib
7+
import numpy as np
68

79
if TYPE_CHECKING:
8-
from t4_devkit.typing import ArrayLike, NDArrayF64, ScalarLike
10+
from t4_devkit.dataclass import PointCloudLike
11+
from t4_devkit.typing import ArrayLike, NDArrayF64
12+
13+
14+
@unique
15+
class PointCloudColorMode(str, Enum):
16+
"""Color mode of point cloud."""
17+
18+
DISTANCE = "distance"
19+
INTENSITY = "intensity"
20+
21+
22+
def pointcloud_color(
23+
pointcloud: PointCloudLike,
24+
color_mode: PointCloudColorMode = PointCloudColorMode.INTENSITY,
25+
) -> NDArrayF64:
26+
"""Return color map depending on the specified color mode.
27+
28+
Args:
29+
pointcloud (PointCloudLike): Any inheritance of `PointCloud` class.
30+
color_mode (PointCloudColorMode): Color mode of point cloud.
31+
"""
32+
match color_mode:
33+
case PointCloudColorMode.DISTANCE:
34+
values = np.linalg.norm(pointcloud.points[:3].T, axis=1)
35+
case _:
36+
values = pointcloud.points[3]
37+
38+
return distance_color(values)
939

1040

1141
def distance_color(
12-
distances: ScalarLike | ArrayLike,
42+
distances: ArrayLike,
1343
cmap: str | None = None,
14-
v_min: float = 3.0,
15-
v_max: float = 75.0,
1644
) -> tuple[float, float, float] | NDArrayF64:
1745
"""Return color map depending on distance values.
1846
1947
Args:
20-
distances (ScalarLike | ArrayLike): Array of distances in the shape of (N,).
48+
distances (ArrayLike): Array of distances in the shape of (N,).
2149
cmap (str | None, optional): Color map name in matplotlib. If None, `turbo_r` will be used.
22-
v_min (float, optional): Min value to normalize.
23-
v_max (float, optional): Max value to normalize.
2450
2551
Returns:
2652
Color map in the shape of (N,). If input type is any number, returns a color as
2753
`tuple[float, float, float]`. Otherwise, returns colors as `NDArrayF64`.
2854
"""
2955
color_map = matplotlib.colormaps["turbo_r"] if cmap is None else matplotlib.colormaps[cmap]
56+
v_min = np.min(distances)
57+
v_max = np.max(distances)
3058
norm = matplotlib.colors.Normalize(v_min, v_max)
3159
return color_map(norm(distances))

t4_devkit/viewer/viewer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from t4_devkit.lanelet import LaneletParser
1515
from t4_devkit.schema import SensorModality
1616

17-
from .color import distance_color
17+
from .color import pointcloud_color
1818
from .geography import calculate_geodetic_point
1919
from .lanelet import (
2020
render_geographic_borders,
@@ -455,7 +455,7 @@ def render_pointcloud(self, seconds: float, channel: str, pointcloud: PointCloud
455455
# TODO(ktro2828): add support of rendering pointcloud on images
456456
rr.set_time_seconds(self.timeline, seconds)
457457

458-
colors = distance_color(np.linalg.norm(pointcloud.points[:3].T, axis=1))
458+
colors = pointcloud_color(pointcloud)
459459
rr.log(
460460
format_entity(self.ego_entity, channel),
461461
rr.Points3D(

0 commit comments

Comments
 (0)