OpenMotionLab/MotionGPT
118
1import numpy as np2import pytest3 4from pyrender import PerspectiveCamera, OrthographicCamera5 6 7def test_perspective_camera():8 9 # Set up constants10 znear = 0.0511 zfar = 10012 yfov = np.pi / 3.013 width = 1000.014 height = 500.015 aspectRatio = 640.0 / 480.016 17 # Test basics18 with pytest.raises(TypeError):19 p = PerspectiveCamera()20 21 p = PerspectiveCamera(yfov=yfov)22 assert p.yfov == yfov23 assert p.znear == 0.0524 assert p.zfar is None25 assert p.aspectRatio is None26 p.name = 'asdf'27 p.name = None28 29 with pytest.raises(ValueError):30 p.yfov = 0.031 32 with pytest.raises(ValueError):33 p.yfov = -1.034 35 with pytest.raises(ValueError):36 p.znear = -1.037 38 p.znear = 0.039 p.znear = 0.0540 p.zfar = 100.041 assert p.zfar == 100.042 43 with pytest.raises(ValueError):44 p.zfar = 0.0345 46 with pytest.raises(ValueError):47 p.zfar = 0.0548 49 p.aspectRatio = 10.050 assert p.aspectRatio == 10.051 52 with pytest.raises(ValueError):53 p.aspectRatio = 0.054 55 with pytest.raises(ValueError):56 p.aspectRatio = -1.057 58 # Test matrix getting/setting59 60 # NF61 p.znear = 0.0562 p.zfar = 10063 p.aspectRatio = None64 65 with pytest.raises(ValueError):66 p.get_projection_matrix()67 68 assert np.allclose(69 p.get_projection_matrix(width, height),70 np.array([71 [1.0 / (width / height * np.tan(yfov / 2.0)), 0.0, 0.0, 0.0],72 [0.0, 1.0 / np.tan(yfov / 2.0), 0.0, 0.0],73 [0.0, 0.0, (zfar + znear) / (znear - zfar),74 (2 * zfar * znear) / (znear - zfar)],75 [0.0, 0.0, -1.0, 0.0]76 ])77 )78 79 # NFA80 p.aspectRatio = aspectRatio81 assert np.allclose(82 p.get_projection_matrix(width, height),83 np.array([84 [1.0 / (aspectRatio * np.tan(yfov / 2.0)), 0.0, 0.0, 0.0],85 [0.0, 1.0 / np.tan(yfov / 2.0), 0.0, 0.0],86 [0.0, 0.0, (zfar + znear) / (znear - zfar),87 (2 * zfar * znear) / (znear - zfar)],88 [0.0, 0.0, -1.0, 0.0]89 ])90 )91 assert np.allclose(92 p.get_projection_matrix(), p.get_projection_matrix(width, height)93 )94 95 # N96 p.zfar = None97 p.aspectRatio = None98 assert np.allclose(99 p.get_projection_matrix(width, height),100 np.array([101 [1.0 / (width / height * np.tan(yfov / 2.0)), 0.0, 0.0, 0.0],102 [0.0, 1.0 / np.tan(yfov / 2.0), 0.0, 0.0],103 [0.0, 0.0, -1.0, -2.0 * znear],104 [0.0, 0.0, -1.0, 0.0]105 ])106 )107 108 109def test_orthographic_camera():110 xm = 1.0111 ym = 2.0112 n = 0.05113 f = 100.0114 115 with pytest.raises(TypeError):116 c = OrthographicCamera()117 118 c = OrthographicCamera(xmag=xm, ymag=ym)119 120 assert c.xmag == xm121 assert c.ymag == ym122 assert c.znear == 0.05123 assert c.zfar == 100.0124 assert c.name is None125 126 with pytest.raises(TypeError):127 c.ymag = None128 129 with pytest.raises(ValueError):130 c.ymag = 0.0131 132 with pytest.raises(ValueError):133 c.ymag = -1.0134 135 with pytest.raises(TypeError):136 c.xmag = None137 138 with pytest.raises(ValueError):139 c.xmag = 0.0140 141 with pytest.raises(ValueError):142 c.xmag = -1.0143 144 with pytest.raises(TypeError):145 c.znear = None146 147 with pytest.raises(ValueError):148 c.znear = 0.0149 150 with pytest.raises(ValueError):151 c.znear = -1.0152 153 with pytest.raises(ValueError):154 c.zfar = 0.01155 156 assert np.allclose(157 c.get_projection_matrix(),158 np.array([159 [1.0 / xm, 0, 0, 0],160 [0, 1.0 / ym, 0, 0],161 [0, 0, 2.0 / (n - f), (f + n) / (n - f)],162 [0, 0, 0, 1.0]163 ])164 )165 