|
| 1 | +# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import subprocess |
| 20 | +import pytest |
| 21 | +import math |
| 22 | +import random |
| 23 | +import torch |
| 24 | +import shutil |
| 25 | +import numpy as np |
| 26 | +from git import Repo |
| 27 | +from kaolin.render.camera import kaolin_camera_to_gsplats, gsplats_camera_to_kaolin, Camera |
| 28 | + |
| 29 | +# dealing with nvcr |
| 30 | +if torch.version.cuda == '12.5': |
| 31 | + pytest.skip("gsplats is not installable with CUDA 12.5", allow_module_level=True) |
| 32 | + |
| 33 | +ROOT_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'gsplats') |
| 34 | +@pytest.fixture(scope="module") |
| 35 | +def gs_cam_cls(): |
| 36 | + repo = Repo.clone_from( |
| 37 | + url='https://github.com/graphdeco-inria/gaussian-splatting', |
| 38 | + multi_options=['--recursive'], |
| 39 | + to_path=ROOT_DIR |
| 40 | + ) |
| 41 | + sys.path.append(ROOT_DIR) |
| 42 | + subprocess.check_call([ |
| 43 | + sys.executable, "-m", "pip", "install", |
| 44 | + os.path.join(ROOT_DIR, "submodules", "diff-gaussian-rasterization") |
| 45 | + ]) |
| 46 | + subprocess.check_call([ |
| 47 | + sys.executable, "-m", "pip", "install", |
| 48 | + os.path.join(ROOT_DIR, "submodules", "simple-knn") |
| 49 | + ]) |
| 50 | + from .gsplats.scene.cameras import Camera as GSCamera |
| 51 | + |
| 52 | + yield GSCamera |
| 53 | + sys.path.remove(ROOT_DIR) |
| 54 | + shutil.rmtree(ROOT_DIR) |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +class TestGsplats: |
| 59 | + def test_cycle(self, gs_cam_cls): |
| 60 | + kal_cam = Camera.from_args( |
| 61 | + eye=torch.rand((3,)), |
| 62 | + at=torch.rand((3,)), |
| 63 | + up=torch.nn.functional.normalize(torch.rand((3,)), dim=0), |
| 64 | + fov=random.random() * math.pi, |
| 65 | + width=512, height=512, |
| 66 | + ) |
| 67 | + gs_cam = kaolin_camera_to_gsplats(kal_cam, gs_cam_cls) |
| 68 | + out_cam = gsplats_camera_to_kaolin(gs_cam) |
| 69 | + assert torch.allclose(out_cam, kal_cam) |
| 70 | + |
| 71 | + def test_kaolin_to_gsplats_regression(self, gs_cam_cls): |
| 72 | + kal_cam = Camera.from_args( |
| 73 | + eye=torch.tensor([1., 2., 3.]), |
| 74 | + at=torch.tensor([0.3, 0.1, 0.2]), |
| 75 | + up=torch.tensor([0., 1., 0.]), |
| 76 | + fov=math.pi / 4, |
| 77 | + width=512, height=512, |
| 78 | + ) |
| 79 | + gs_cam = kaolin_camera_to_gsplats(kal_cam, gs_cam_cls) |
| 80 | + expected_R = np.array([[ 0.9701425, 0.13336042, -0.20257968], |
| 81 | + [ 0., -0.83525735, -0.5498591 ], |
| 82 | + [-0.24253562, 0.53344166, -0.8103187 ]]) |
| 83 | + expected_T = np.array([-0.24253559, -0.06317067, 3.733254 ]) |
| 84 | + expected_fovx = torch.tensor([0.7854]) |
| 85 | + expected_fovy = torch.tensor([0.7854]) |
| 86 | + assert np.allclose(expected_R, gs_cam.R) |
| 87 | + assert np.allclose(expected_T, gs_cam.T) |
| 88 | + assert torch.allclose(expected_fovx, gs_cam.FoVx) |
| 89 | + assert torch.allclose(expected_fovy, gs_cam.FoVy) |
0 commit comments