mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-13 11:24:00 +08:00
On Gaussian Splat export, write a 'SplatRenderMode: mip|default' PLY header comment derived from the model's rasterize_mode so viewers can select a matching rasterization mode. 'mip' is the antialiased mode; 'default' is classic. - ExportGaussianSplat.write_ply gains an optional 'antialiased' arg (None keeps the previous no-comment behavior); export main() passes model.config.rasterize_mode == 'antialiased' - splatfacto exposes rasterize_mode as a disabled/frozen viewer dropdown (the viewer renders the live model, so the mode is read from config) - add a test asserting the comment is written for mip/default/none
122 lines
4.6 KiB
Python
122 lines
4.6 KiB
Python
# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from collections import OrderedDict
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import open3d as o3d
|
|
import pytest
|
|
|
|
from nerfstudio.scripts.exporter import ExportGaussianSplat
|
|
|
|
|
|
def test_export_gaussian_splat_write_ply(tmp_path: Path):
|
|
filename = str(tmp_path / "test_export_gaussian_splat_write_ply.ply")
|
|
count = 10
|
|
colors_original = np.random.randint(0, 255, size=(count,), dtype=np.uint8)
|
|
x_original = np.random.rand(count).astype(np.float32)
|
|
y_original = np.random.rand(count).astype(np.float32)
|
|
z_original = np.random.rand(count).astype(np.float64) # should convert to float32
|
|
|
|
map_to_tensors: OrderedDict[str, np.ndarray] = OrderedDict(
|
|
[
|
|
("colors", colors_original),
|
|
("x", x_original),
|
|
("y", y_original),
|
|
("z", z_original),
|
|
]
|
|
)
|
|
|
|
ExportGaussianSplat.write_ply(filename, count, map_to_tensors)
|
|
|
|
# Read the PLY file
|
|
pcd = o3d.t.io.read_point_cloud(filename)
|
|
positions = pcd.point.positions.numpy()
|
|
colors = pcd.point.colors.numpy()
|
|
|
|
# Assertions
|
|
# Check if the file exists and has the correct number of points
|
|
assert len(positions) == count, "Mismatch in the number of points written and read."
|
|
|
|
# Check colors
|
|
np.testing.assert_array_equal(colors_original, colors[:, 0], "Colors do not match.")
|
|
|
|
# Check x, y, and z coordinates with epsilon tollerence
|
|
epsilon = 1e-5 # Adjust based on expected precision
|
|
np.testing.assert_allclose(x_original, positions[:, 0], atol=epsilon, err_msg="X coordinates do not match.")
|
|
np.testing.assert_allclose(y_original, positions[:, 1], atol=epsilon, err_msg="Y coordinates do not match.")
|
|
np.testing.assert_allclose(
|
|
z_original.astype(np.float32), positions[:, 2], atol=epsilon, err_msg="Z coordinates do not match."
|
|
)
|
|
|
|
|
|
def test_export_gaussian_splat_write_ply_mismatched_count(tmp_path: Path):
|
|
filename = str(tmp_path / "test_export_gaussian_splat_write_ply_mismatched_count.ply")
|
|
count = 10
|
|
map_to_tensors: OrderedDict[str, np.ndarray] = OrderedDict(
|
|
[
|
|
("x", np.random.rand(count + 1).astype(np.float32)),
|
|
("y", np.random.rand(count).astype(np.float32)),
|
|
("z", np.random.rand(count - 1).astype(np.float32)),
|
|
]
|
|
)
|
|
with pytest.raises(ValueError):
|
|
ExportGaussianSplat.write_ply(filename, count, map_to_tensors)
|
|
|
|
|
|
def _read_ply_header(filename: str) -> str:
|
|
"""Return the ASCII PLY header (up to and including the end_header line)."""
|
|
with open(filename, "rb") as f:
|
|
data = f.read()
|
|
end = data.index(b"end_header")
|
|
return data[:end].decode("ascii")
|
|
|
|
|
|
def _make_valid_tensors(count: int = 10) -> "OrderedDict[str, np.ndarray]":
|
|
return OrderedDict(
|
|
[
|
|
("x", np.random.rand(count).astype(np.float32)),
|
|
("y", np.random.rand(count).astype(np.float32)),
|
|
("z", np.random.rand(count).astype(np.float32)),
|
|
]
|
|
)
|
|
|
|
|
|
def test_export_gaussian_splat_write_ply_render_mode_comment(tmp_path: Path):
|
|
count = 10
|
|
|
|
# antialiased=True -> mip
|
|
mip_file = str(tmp_path / "mip.ply")
|
|
ExportGaussianSplat.write_ply(mip_file, count, _make_valid_tensors(count), antialiased=True)
|
|
assert "comment SplatRenderMode: mip" in _read_ply_header(mip_file)
|
|
|
|
# antialiased=False -> default
|
|
default_file = str(tmp_path / "default.ply")
|
|
ExportGaussianSplat.write_ply(default_file, count, _make_valid_tensors(count), antialiased=False)
|
|
header = _read_ply_header(default_file)
|
|
assert "comment SplatRenderMode: default" in header
|
|
assert "comment SplatRenderMode: mip" not in header
|
|
|
|
# antialiased=None (default) -> no SplatRenderMode comment (backwards compatible)
|
|
none_file = str(tmp_path / "none.ply")
|
|
ExportGaussianSplat.write_ply(none_file, count, _make_valid_tensors(count))
|
|
assert "SplatRenderMode" not in _read_ply_header(none_file)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Run the test
|
|
test_export_gaussian_splat_write_ply(Path("."))
|
|
test_export_gaussian_splat_write_ply_mismatched_count(Path("."))
|