forked from mikedh/trimesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_camera.py
131 lines (109 loc) · 4.29 KB
/
test_camera.py
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
try:
from . import generic as g
except BaseException:
import generic as g
import numpy as np
class CameraTests(g.unittest.TestCase):
def test_K(self):
resolution = (320, 240)
fov = (60, 40)
camera = g.trimesh.scene.Camera(
resolution=resolution,
fov=fov)
# ground truth matrix
K_expected = np.array([[277.128, 0, 160],
[0, 329.697, 120],
[0, 0, 1]],
dtype=np.float64)
assert np.allclose(camera.K, K_expected, rtol=1e-3)
# check to make sure assignment from matrix works
K_set = K_expected.copy()
K_set[:2, 2] = 300
camera.K = K_set
assert np.allclose(camera.resolution, 600)
def test_consistency(self):
resolution = (320, 240)
focal = None
fov = (60, 40)
camera = g.trimesh.scene.Camera(
resolution=resolution,
focal=focal,
fov=fov)
assert np.allclose(camera.fov, fov)
camera = g.trimesh.scene.Camera(
resolution=resolution,
focal=camera.focal,
fov=None)
assert np.allclose(camera.fov, fov)
def test_lookat(self):
"""
Test the "look at points" function
"""
# original points
ori = np.array([[-1, -1],
[1, -1],
[1, 1],
[-1, 1]])
for i in range(10):
# set the extents to be random but positive
extents = g.random() * 10
points = g.trimesh.util.stack_3D(ori.copy() * extents)
fov = g.np.array([20, 50])
# offset the points by a random amount
offset = (g.random(3) - 0.5) * 100
T = g.trimesh.scene.cameras.look_at(points + offset, fov)
# check using trig
check = (points.ptp(axis=0)[:2] / 2.0) / \
g.np.tan(np.radians(fov / 2))
check += points[:, 2].mean()
# Z should be the same as maximum trig option
assert np.linalg.inv(T)[2, 3] >= check.max()
# just run to test other arguments
# TODO(unknown): find the way to test it correctly
g.trimesh.scene.cameras.look_at(points, fov, center=points[0])
g.trimesh.scene.cameras.look_at(points, fov, distance=1)
def test_scene(self):
"""
Check camera transforms in different cases
- with no scene, should return identity and be settable
- with scene, should use node name and set scene.graph
- if scene is added later should set correctly set transform
"""
fov = (60, 40)
resolution = (320, 240)
# create camera with no scene
camera = g.trimesh.scene.Camera(
resolution=resolution,
fov=fov)
matrix = g.trimesh.transformations.random_rotation_matrix()
# no transform set, should be identity
assert g.np.allclose(camera.transform, np.eye(4))
# set transform with no scene
camera.transform = matrix
assert g.np.allclose(camera.transform, matrix)
# add a scene reference later
assert camera.scene is None
scene = g.trimesh.Scene()
camera.scene = scene
# should have transferred the local transform to both
# the scene graph as well as the returned value
assert g.np.allclose(camera.transform, matrix)
assert g.np.allclose(scene.graph[camera.name][0], matrix)
# should be setting it correctly still
matrix = g.trimesh.transformations.random_rotation_matrix()
camera.transform = matrix
assert g.np.allclose(camera.transform, matrix)
assert g.np.allclose(scene.graph[camera.name][0], matrix)
# create camera with scene and transform set on creation
scene = g.trimesh.Scene()
matrix = g.trimesh.transformations.random_rotation_matrix()
camera = g.trimesh.scene.Camera(
resolution=resolution,
fov=fov,
scene=scene,
transform=matrix)
assert g.np.allclose(camera.transform, matrix)
assert g.np.allclose(scene.graph[camera.name][0], matrix)
if __name__ == '__main__':
g.trimesh.util.attach_to_log()
g.unittest.main()