(
xyz,
rgb=None,
path='output.ply',
)
| 159 | |
| 160 | |
| 161 | def write_ply( |
| 162 | xyz, |
| 163 | rgb=None, |
| 164 | path='output.ply', |
| 165 | ) -> None: |
| 166 | if torch.is_tensor(xyz): |
| 167 | xyz = xyz.detach().cpu().numpy() |
| 168 | |
| 169 | if torch.is_tensor(rgb): |
| 170 | rgb = rgb.detach().cpu().numpy() |
| 171 | |
| 172 | if rgb is not None and rgb.max() > 1: |
| 173 | rgb = rgb / 255. |
| 174 | |
| 175 | xyz = rotate_target_dim_to_last_axis(xyz, 3) |
| 176 | xyz = xyz.reshape(-1, 3) |
| 177 | |
| 178 | if rgb is not None: |
| 179 | rgb = rotate_target_dim_to_last_axis(rgb, 3) |
| 180 | rgb = rgb.reshape(-1, 3) |
| 181 | |
| 182 | if rgb is None: |
| 183 | min_coord = np.min(xyz, axis=0) |
| 184 | max_coord = np.max(xyz, axis=0) |
| 185 | normalized_coord = (xyz - min_coord) / (max_coord - min_coord + 1e-8) |
| 186 | |
| 187 | hue = 0.7 * normalized_coord[:,0] + 0.2 * normalized_coord[:,1] + 0.1 * normalized_coord[:,2] |
| 188 | hsv = np.stack([hue, 0.9*np.ones_like(hue), 0.8*np.ones_like(hue)], axis=1) |
| 189 | |
| 190 | c = hsv[:,2:] * hsv[:,1:2] |
| 191 | x = c * (1 - np.abs( (hsv[:,0:1]*6) % 2 - 1 )) |
| 192 | m = hsv[:,2:] - c |
| 193 | |
| 194 | rgb = np.zeros_like(hsv) |
| 195 | cond = (0 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 1) |
| 196 | rgb[cond] = np.hstack([c[cond], x[cond], np.zeros_like(x[cond])]) |
| 197 | cond = (1 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 2) |
| 198 | rgb[cond] = np.hstack([x[cond], c[cond], np.zeros_like(x[cond])]) |
| 199 | cond = (2 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 3) |
| 200 | rgb[cond] = np.hstack([np.zeros_like(x[cond]), c[cond], x[cond]]) |
| 201 | cond = (3 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 4) |
| 202 | rgb[cond] = np.hstack([np.zeros_like(x[cond]), x[cond], c[cond]]) |
| 203 | cond = (4 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 5) |
| 204 | rgb[cond] = np.hstack([x[cond], np.zeros_like(x[cond]), c[cond]]) |
| 205 | cond = (5 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 6) |
| 206 | rgb[cond] = np.hstack([c[cond], np.zeros_like(x[cond]), x[cond]]) |
| 207 | rgb = (rgb + m) |
| 208 | |
| 209 | dtype = [ |
| 210 | ("x", "f4"), |
| 211 | ("y", "f4"), |
| 212 | ("z", "f4"), |
| 213 | ("nx", "f4"), |
| 214 | ("ny", "f4"), |
| 215 | ("nz", "f4"), |
| 216 | ("red", "u1"), |
| 217 | ("green", "u1"), |
| 218 | ("blue", "u1"), |
nothing calls this directly
no test coverage detected