-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_structures.py
More file actions
176 lines (157 loc) · 6.5 KB
/
Copy pathtest_structures.py
File metadata and controls
176 lines (157 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Tests for SplatForge core structures."""
import sys
import os
import numpy as np
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from splatforge.core.structures import SplatData
class TestSplatData:
"""Test SplatData core functionality."""
def test_empty_creation(self):
"""Test creating an empty SplatData."""
data = SplatData()
assert data.num_splats == 0
assert data.is_empty is True
def test_basic_creation(self):
"""Test creating SplatData with values."""
n = 100
data = SplatData(
positions=np.random.randn(n, 3).astype(np.float32),
scales=np.random.rand(n, 3).astype(np.float32) * 0.1,
rotations=np.tile([1, 0, 0, 0], (n, 1)).astype(np.float32),
colors=np.random.rand(n, 3).astype(np.float32),
alphas=np.random.rand(n, 1).astype(np.float32),
)
assert data.num_splats == 100
assert data.is_empty is False
assert data.positions.shape == (100, 3)
def test_bounding_box(self):
"""Test bounding box calculation."""
positions = np.array([
[0, 0, 0],
[10, 5, 3],
[5, 10, 7],
], dtype=np.float32)
data = SplatData(
positions=positions,
scales=np.ones((3, 3), dtype=np.float32) * 0.01,
rotations=np.tile([1, 0, 0, 0], (3, 1)).astype(np.float32),
colors=np.ones((3, 3), dtype=np.float32),
alphas=np.ones((3, 1), dtype=np.float32),
)
bb_min, bb_max = data.bounding_box
assert np.allclose(bb_min, [0, 0, 0])
assert np.allclose(bb_max, [10, 10, 7])
def test_center(self):
"""Test center calculation."""
positions = np.array([[0, 0, 0], [10, 0, 0]], dtype=np.float32)
data = SplatData(
positions=positions,
scales=np.ones((2, 3), dtype=np.float32) * 0.01,
rotations=np.tile([1, 0, 0, 0], (2, 1)).astype(np.float32),
colors=np.ones((2, 3), dtype=np.float32),
alphas=np.ones((2, 1), dtype=np.float32),
)
center = data.center
assert np.allclose(center, [5, 0, 0])
def test_statistics(self):
"""Test statistics computation."""
n = 1000
data = SplatData(
positions=np.random.randn(n, 3).astype(np.float32),
scales=np.random.rand(n, 3).astype(np.float32) * 0.1,
rotations=np.tile([1, 0, 0, 0], (n, 1)).astype(np.float32),
colors=np.random.rand(n, 3).astype(np.float32),
alphas=np.random.rand(n, 1).astype(np.float32),
)
stats = data.get_statistics()
assert stats['num_splats'] == 1000
assert 'bounding_box_min' in stats
assert 'bounding_box_max' in stats
assert 'position_mean' in stats
assert 'scale_mean' in stats
assert 'alpha_mean' in stats
assert 'color_mean' in stats
def test_validate_clean_data(self):
"""Test validation of clean data."""
n = 100
data = SplatData(
positions=np.random.randn(n, 3).astype(np.float32),
scales=np.random.rand(n, 3).astype(np.float32) * 0.1 + 0.01,
rotations=np.tile([1, 0, 0, 0], (n, 1)).astype(np.float32),
colors=np.random.rand(n, 3).astype(np.float32),
alphas=np.random.rand(n, 1).astype(np.float32) * 0.5 + 0.5,
)
is_valid, issues = data.validate()
assert is_valid is True
assert len(issues) == 0
def test_validate_nan_data(self):
"""Test detection of NaN values."""
positions = np.array([[1, 2, 3], [4, float('nan'), 6]], dtype=np.float32)
data = SplatData(
positions=positions,
scales=np.ones((2, 3), dtype=np.float32) * 0.01,
rotations=np.tile([1, 0, 0, 0], (2, 1)).astype(np.float32),
colors=np.ones((2, 3), dtype=np.float32),
alphas=np.ones((2, 1), dtype=np.float32),
)
is_valid, issues = data.validate()
assert is_valid is False
assert any('NaN' in i for i in issues)
def test_validate_out_of_range_alpha(self):
"""Test detection of out-of-range alpha."""
data = SplatData(
positions=np.array([[1, 2, 3]], dtype=np.float32),
scales=np.ones((1, 3), dtype=np.float32) * 0.01,
rotations=np.array([[1, 0, 0, 0]], dtype=np.float32),
colors=np.ones((1, 3), dtype=np.float32),
alphas=np.array([[1.5]], dtype=np.float32),
)
is_valid, issues = data.validate()
assert is_valid is False
def test_normalize_quaternions(self):
"""Test quaternion normalization."""
rotations = np.array([[2, 0, 0, 0], [0, 3, 0, 0]], dtype=np.float32)
data = SplatData(
positions=np.zeros((2, 3), dtype=np.float32),
scales=np.ones((2, 3), dtype=np.float32) * 0.01,
rotations=rotations,
colors=np.ones((2, 3), dtype=np.float32),
alphas=np.ones((2, 1), dtype=np.float32),
)
data.normalize_quaternions()
norms = np.linalg.norm(data.rotations, axis=1)
assert np.allclose(norms, 1.0)
def test_clamp_values(self):
"""Test value clamping."""
data = SplatData(
positions=np.zeros((1, 3), dtype=np.float32),
scales=np.ones((1, 3), dtype=np.float32) * 0.01,
rotations=np.array([[1, 0, 0, 0]], dtype=np.float32),
colors=np.array([[1.5, -0.5, 0.5]], dtype=np.float32),
alphas=np.array([[2.0]], dtype=np.float32),
)
data.clamp_values()
assert data.alphas[0, 0] == 1.0
assert data.colors[0, 0] == 1.0
assert data.colors[0, 1] == 0.0
def test_copy(self):
"""Test deep copy."""
positions = np.array([[1, 2, 3]], dtype=np.float32)
data = SplatData(
positions=positions,
scales=np.ones((1, 3), dtype=np.float32) * 0.01,
rotations=np.array([[1, 0, 0, 0]], dtype=np.float32),
colors=np.ones((1, 3), dtype=np.float32),
alphas=np.ones((1, 1), dtype=np.float32),
)
copied = data.copy()
copied.positions[0, 0] = 999
assert data.positions[0, 0] == 1.0 # Original unchanged
def test_repr(self):
"""Test string representation."""
data = SplatData()
assert 'SplatData' in repr(data)
assert 'num_splats=0' in repr(data)
if __name__ == '__main__':
pytest.main([__file__, '-v'])