|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from enum import Enum, unique |
3 | 4 | from typing import TYPE_CHECKING |
4 | 5 |
|
5 | 6 | import matplotlib |
| 7 | +import numpy as np |
6 | 8 |
|
7 | 9 | 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) |
9 | 39 |
|
10 | 40 |
|
11 | 41 | def distance_color( |
12 | | - distances: ScalarLike | ArrayLike, |
| 42 | + distances: ArrayLike, |
13 | 43 | cmap: str | None = None, |
14 | | - v_min: float = 3.0, |
15 | | - v_max: float = 75.0, |
16 | 44 | ) -> tuple[float, float, float] | NDArrayF64: |
17 | 45 | """Return color map depending on distance values. |
18 | 46 |
|
19 | 47 | Args: |
20 | | - distances (ScalarLike | ArrayLike): Array of distances in the shape of (N,). |
| 48 | + distances (ArrayLike): Array of distances in the shape of (N,). |
21 | 49 | 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. |
24 | 50 |
|
25 | 51 | Returns: |
26 | 52 | Color map in the shape of (N,). If input type is any number, returns a color as |
27 | 53 | `tuple[float, float, float]`. Otherwise, returns colors as `NDArrayF64`. |
28 | 54 | """ |
29 | 55 | 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) |
30 | 58 | norm = matplotlib.colors.Normalize(v_min, v_max) |
31 | 59 | return color_map(norm(distances)) |
0 commit comments