Spaces:
Sleeping
Sleeping
File size: 3,730 Bytes
8554568 |
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 |
import numpy as np
import pytest
from pyrender import PerspectiveCamera, OrthographicCamera
def test_perspective_camera():
# Set up constants
znear = 0.05
zfar = 100
yfov = np.pi / 3.0
width = 1000.0
height = 500.0
aspectRatio = 640.0 / 480.0
# Test basics
with pytest.raises(TypeError):
p = PerspectiveCamera()
p = PerspectiveCamera(yfov=yfov)
assert p.yfov == yfov
assert p.znear == 0.05
assert p.zfar is None
assert p.aspectRatio is None
p.name = 'asdf'
p.name = None
with pytest.raises(ValueError):
p.yfov = 0.0
with pytest.raises(ValueError):
p.yfov = -1.0
with pytest.raises(ValueError):
p.znear = -1.0
p.znear = 0.0
p.znear = 0.05
p.zfar = 100.0
assert p.zfar == 100.0
with pytest.raises(ValueError):
p.zfar = 0.03
with pytest.raises(ValueError):
p.zfar = 0.05
p.aspectRatio = 10.0
assert p.aspectRatio == 10.0
with pytest.raises(ValueError):
p.aspectRatio = 0.0
with pytest.raises(ValueError):
p.aspectRatio = -1.0
# Test matrix getting/setting
# NF
p.znear = 0.05
p.zfar = 100
p.aspectRatio = None
with pytest.raises(ValueError):
p.get_projection_matrix()
assert np.allclose(
p.get_projection_matrix(width, height),
np.array([
[1.0 / (width / height * np.tan(yfov / 2.0)), 0.0, 0.0, 0.0],
[0.0, 1.0 / np.tan(yfov / 2.0), 0.0, 0.0],
[0.0, 0.0, (zfar + znear) / (znear - zfar),
(2 * zfar * znear) / (znear - zfar)],
[0.0, 0.0, -1.0, 0.0]
])
)
# NFA
p.aspectRatio = aspectRatio
assert np.allclose(
p.get_projection_matrix(width, height),
np.array([
[1.0 / (aspectRatio * np.tan(yfov / 2.0)), 0.0, 0.0, 0.0],
[0.0, 1.0 / np.tan(yfov / 2.0), 0.0, 0.0],
[0.0, 0.0, (zfar + znear) / (znear - zfar),
(2 * zfar * znear) / (znear - zfar)],
[0.0, 0.0, -1.0, 0.0]
])
)
assert np.allclose(
p.get_projection_matrix(), p.get_projection_matrix(width, height)
)
# N
p.zfar = None
p.aspectRatio = None
assert np.allclose(
p.get_projection_matrix(width, height),
np.array([
[1.0 / (width / height * np.tan(yfov / 2.0)), 0.0, 0.0, 0.0],
[0.0, 1.0 / np.tan(yfov / 2.0), 0.0, 0.0],
[0.0, 0.0, -1.0, -2.0 * znear],
[0.0, 0.0, -1.0, 0.0]
])
)
def test_orthographic_camera():
xm = 1.0
ym = 2.0
n = 0.05
f = 100.0
with pytest.raises(TypeError):
c = OrthographicCamera()
c = OrthographicCamera(xmag=xm, ymag=ym)
assert c.xmag == xm
assert c.ymag == ym
assert c.znear == 0.05
assert c.zfar == 100.0
assert c.name is None
with pytest.raises(TypeError):
c.ymag = None
with pytest.raises(ValueError):
c.ymag = 0.0
with pytest.raises(ValueError):
c.ymag = -1.0
with pytest.raises(TypeError):
c.xmag = None
with pytest.raises(ValueError):
c.xmag = 0.0
with pytest.raises(ValueError):
c.xmag = -1.0
with pytest.raises(TypeError):
c.znear = None
with pytest.raises(ValueError):
c.znear = 0.0
with pytest.raises(ValueError):
c.znear = -1.0
with pytest.raises(ValueError):
c.zfar = 0.01
assert np.allclose(
c.get_projection_matrix(),
np.array([
[1.0 / xm, 0, 0, 0],
[0, 1.0 / ym, 0, 0],
[0, 0, 2.0 / (n - f), (f + n) / (n - f)],
[0, 0, 0, 1.0]
])
)
|