示例#1
0
    def test_ScreenWindowParameters(self):
        cam = Gf.Camera()
        cam.projection = Gf.Camera.Perspective
        cam.horizontalAperture = 184.5
        cam.horizontalApertureOffset = 15.45
        cam.verticalAperture = 20.6
        cam.focalLength = 10.8
        cam.transform = Gf.Matrix4d(0.890425533492, 0.433328071165,
                                    -0.13917310100, 0.0, -0.373912364534,
                                    0.870830610429, 0.31912942765, 0.0,
                                    0.259483935801, -0.232122447617,
                                    0.93743653457, 0.0, 6.533573569142,
                                    9.880622442086, 1.89848943302, 1.0)

        self._IsClose(
            CameraUtil.ScreenWindowParameters(cam).screenWindow,
            (-0.8325203582, 1.167479724, -0.1116531185, 0.1116531185))
        self._IsClose(
            CameraUtil.ScreenWindowParameters(cam).fieldOfView, 166.645202637)
        self._IsClose(
            CameraUtil.ScreenWindowParameters(cam).zFacingViewMatrix,
            Gf.Matrix4d(0.8904255335028, -0.3739123645233, -0.259483935838, 0,
                        0.4333280711640, 0.8708306104262, 0.232122447596, 0,
                        -0.1391731009620, 0.3191294276581, -0.937436534593, 0,
                        -9.8349931341753, -6.7672283767831, 1.181556474823, 1))

        cam.projection = Gf.Camera.Orthographic

        self._IsClose(
            CameraUtil.ScreenWindowParameters(cam).screenWindow,
            (-7.6800003051, 10.770000457, -1.0300000190, 1.0300000190))
示例#2
0
    def test_ConformProjectionMatrix(self):
        for projection in Gf.Camera.Projection.allValues:
            for policy in CameraUtil.ConformWindowPolicy.allValues:
                for targetAspect in [0.5, 1.0, 2.0]:
                    for xMirror in [-1, 1]:
                        for yMirror in [-1, 1]:
                            mirrorMatrix = Gf.Matrix4d(xMirror, 0, 0, 0, 0,
                                                       yMirror, 0, 0, 0, 0, 1,
                                                       0, 0, 0, 0, 1)

                            cam = Gf.Camera(projection=projection,
                                            horizontalAperture=100.0,
                                            verticalAperture=75.0,
                                            horizontalApertureOffset=11.0,
                                            verticalApertureOffset=12.0)

                            originalMatrix = cam.frustum.ComputeProjectionMatrix(
                            )

                            CameraUtil.ConformWindow(cam, policy, targetAspect)

                            self._IsClose(
                                cam.frustum.ComputeProjectionMatrix() *
                                mirrorMatrix,
                                CameraUtil.ConformedWindow(
                                    originalMatrix * mirrorMatrix, policy,
                                    targetAspect))
示例#3
0
    def test_ConformedWindowGfRange2d(self):
        self._IsClose(
            CameraUtil.ConformedWindow(window=Gf.Range2d(
                Gf.Vec2d(-8, -6), Gf.Vec2d(-4, -2)),
                                       targetAspect=3.0,
                                       policy=CameraUtil.Fit),
            Gf.Range2d(Gf.Vec2d(-12, -6), Gf.Vec2d(0, -2)))

        self._IsClose(
            CameraUtil.ConformedWindow(
                Gf.Range2d(Gf.Vec2d(-10, -11), Gf.Vec2d(-1, -1)),
                CameraUtil.MatchHorizontally, 1.5),
            Gf.Range2d(Gf.Vec2d(-10, -9), Gf.Vec2d(-1, -3)))

        self._IsClose(
            CameraUtil.ConformedWindow(
                Gf.Range2d(Gf.Vec2d(-10, -11), Gf.Vec2d(-1, -1)),
                CameraUtil.MatchVertically, 1.5),
            Gf.Range2d(Gf.Vec2d(-13, -11), Gf.Vec2d(2, -1)))

        self._IsClose(
            CameraUtil.ConformedWindow(
                Gf.Range2d(Gf.Vec2d(-10, -11), Gf.Vec2d(-1, -1)),
                CameraUtil.DontConform, 1.5),
            Gf.Range2d(Gf.Vec2d(-10, -11), Gf.Vec2d(-1, -1)))
示例#4
0
    def test_ConformFrustum(self):
        frustum = Gf.Frustum()
        frustum.window = Gf.Range2d(Gf.Vec2d(-1.2, -1.0), Gf.Vec2d(1.0, 1.5))

        CameraUtil.ConformWindow(frustum, CameraUtil.Crop, 1.3333)

        self._IsClose(frustum.window.min, Gf.Vec2d(-1.2, -0.575020625515638))
        self._IsClose(frustum.window.max, Gf.Vec2d(1.0, 1.075020625515638))

        frustum.window = Gf.Range2d(Gf.Vec2d(-1.2, -1.0), Gf.Vec2d(1.0, 1.5))
        CameraUtil.ConformWindow(frustum, CameraUtil.DontConform, 1.3333)
        self._IsClose(frustum.window.min, Gf.Vec2d(-1.2, -1.0))
        self._IsClose(frustum.window.max, Gf.Vec2d(1.0, 1.5))
示例#5
0
    def test_ConformWindow(self):
        cam = Gf.Camera()
        cam.horizontalAperture = 100.0
        cam.verticalAperture = 75.0
        cam.horizontalApertureOffset = 11.0
        cam.verticalApertureOffset = 12.0

        CameraUtil.ConformWindow(camera=cam,
                                 policy=CameraUtil.Fit,
                                 targetAspect=2.0)

        self._IsClose(cam.horizontalAperture, 150.0)
        self._IsClose(cam.verticalAperture, 75.0)
        self._IsClose(cam.horizontalApertureOffset, 11.0)
        self._IsClose(cam.verticalApertureOffset, 12.0)

        CameraUtil.ConformWindow(cam, CameraUtil.Fit, 1.5)

        self._IsClose(cam.horizontalAperture, 150.0)
        self._IsClose(cam.verticalAperture, 100.0)
        self._IsClose(cam.horizontalApertureOffset, 11.0)
        self._IsClose(cam.verticalApertureOffset, 12.0)
示例#6
0
    def test_ConformedWindowGfVec2d(self):
        self._IsClose(
            CameraUtil.ConformedWindow(window=Gf.Vec2d(1.0, 2.0),
                                       targetAspect=3.0,
                                       policy=CameraUtil.Fit),
            Gf.Vec2d(6.0, 2.0))

        self._IsClose(
            CameraUtil.ConformedWindow(window=Gf.Vec2d(9.0, 2.0),
                                       targetAspect=3.0,
                                       policy=CameraUtil.Fit),
            Gf.Vec2d(9.0, 3.0))

        self._IsClose(
            CameraUtil.ConformedWindow(window=Gf.Vec2d(3.3, 4.0),
                                       targetAspect=1.5,
                                       policy=CameraUtil.Crop),
            Gf.Vec2d(3.3, 2.2))

        self._IsClose(
            CameraUtil.ConformedWindow(window=Gf.Vec2d(10.0, 2.0),
                                       targetAspect=4,
                                       policy=CameraUtil.Crop),
            Gf.Vec2d(8.0, 2.0))

        self._IsClose(
            CameraUtil.ConformedWindow(window=Gf.Vec2d(0.1, 2.0),
                                       policy=CameraUtil.Crop,
                                       targetAspect=0.1), Gf.Vec2d(0.1, 1.0))

        self._IsClose(
            CameraUtil.ConformedWindow(Gf.Vec2d(2.0, 1.9),
                                       CameraUtil.MatchVertically, 2.0),
            Gf.Vec2d(3.8, 1.9))

        self._IsClose(
            CameraUtil.ConformedWindow(Gf.Vec2d(2.1, 1.9),
                                       CameraUtil.MatchHorizontally, 1.0),
            Gf.Vec2d(2.1, 2.1))

        self._IsClose(
            CameraUtil.ConformedWindow(Gf.Vec2d(2.1, 1.9),
                                       CameraUtil.DontConform, 1.0),
            Gf.Vec2d(2.1, 1.9))
示例#7
0
    def test_ConformedWindowGfVec4d(self):

        self._IsClose(
            CameraUtil.ConformedWindow(Gf.Vec4d(-10, -1, -11, -1),
                                       CameraUtil.MatchHorizontally, 1.5),
            Gf.Vec4d(-10, -1, -9, -3))
示例#8
0
    def testCameras(self):

        fileName = self.getOutputPath("cameras.usda", cleanUp=False)

        testCameras = []
        for projection in ["orthographic", "perspective"]:
            for horizontalAperture in [0.3, 1, 20, 50, 100]:
                for verticalAperture in [0.3, 1, 20, 50, 100]:
                    for horizontalApertureOffset in [0, -0.4, 2.1]:
                        for verticalApertureOffset in [0, -0.4, 2.1]:
                            for focalLength in [1, 10, 60.5]:
                                index = len(testCameras)
                                c = IECoreScene.Camera()
                                c.setProjection(projection)
                                c.setAperture(
                                    imath.V2f(horizontalAperture,
                                              verticalAperture))
                                c.setApertureOffset(
                                    imath.V2f(horizontalApertureOffset,
                                              verticalApertureOffset))
                                c.setFocalLength(focalLength)
                                testCameras.append((index, imath.M44d(), c))

        for near in [0.01, 0.1, 1.7]:
            for far in [10, 100.9, 10000000]:
                index = len(testCameras)
                c = IECoreScene.Camera()
                c.setClippingPlanes(imath.V2f(near, far))
                testCameras.append((index,
                                    imath.M44d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
                                               0, 1, 2, index, 1), c))

        for scale in [0.01, 0.1, 0.001]:
            index = len(testCameras)
            c = IECoreScene.Camera()
            c.setProjection("perspective")
            c.setAperture(imath.V2f(36, 24))
            c.setFocalLength(35)
            c.setFocalLengthWorldScale(scale)
            testCameras.append((index, imath.M44d(), c))

        sceneWrite = IECoreScene.SceneInterface.create(
            fileName, IECore.IndexedIO.OpenMode.Write)
        root = sceneWrite.createChild("root")

        for i, matrix, camObj in testCameras:
            cam = root.createChild("cam%i" % i)
            cam.writeTransform(IECore.M44dData(matrix), 0.0)
            cam.writeObject(camObj, 0.0)
            del cam

        del root
        del sceneWrite

        usdFile = pxr.Usd.Stage.Open(fileName)

        for i, matrix, cortexCam in testCameras:
            cG = pxr.UsdGeom.Camera.Get(usdFile, "/root/cam%i" % i)
            c = cG.GetCamera()
            usdMatrix = cG.MakeMatrixXform().GetOpTransform(1.0)
            for i in range(16):
                self.assertAlmostEqual(usdMatrix[i // 4][i % 4],
                                       matrix[i // 4][i % 4])

            self.assertEqual(c.projection.name.lower(),
                             cortexCam.getProjection())

            if cortexCam.getProjection() == "orthographic":
                self.assertAlmostEqual(c.horizontalAperture * 0.1,
                                       cortexCam.getAperture()[0],
                                       places=6)
                self.assertAlmostEqual(c.verticalAperture * 0.1,
                                       cortexCam.getAperture()[1],
                                       places=6)
                self.assertAlmostEqual(c.horizontalApertureOffset * 0.1,
                                       cortexCam.getApertureOffset()[0],
                                       places=6)
                self.assertAlmostEqual(c.verticalApertureOffset * 0.1,
                                       cortexCam.getApertureOffset()[1],
                                       places=6)
            else:
                scale = 0.1 / cortexCam.getFocalLengthWorldScale()
                self.assertAlmostEqual(c.horizontalAperture * scale,
                                       cortexCam.getAperture()[0],
                                       places=5)
                self.assertAlmostEqual(c.verticalAperture * scale,
                                       cortexCam.getAperture()[1],
                                       places=5)
                self.assertAlmostEqual(c.horizontalApertureOffset * scale,
                                       cortexCam.getApertureOffset()[0],
                                       places=5)
                self.assertAlmostEqual(c.verticalApertureOffset * scale,
                                       cortexCam.getApertureOffset()[1],
                                       places=5)
                self.assertAlmostEqual(c.focalLength * scale,
                                       cortexCam.getFocalLength(),
                                       places=5)

            self.assertEqual(c.clippingRange.min,
                             cortexCam.getClippingPlanes()[0])
            self.assertEqual(c.clippingRange.max,
                             cortexCam.getClippingPlanes()[1])
            self.assertEqual(c.fStop, cortexCam.getFStop())
            self.assertEqual(c.focusDistance, cortexCam.getFocusDistance())
            self.assertEqual(cG.GetShutterOpenAttr().Get(),
                             cortexCam.getShutter()[0])
            self.assertEqual(cG.GetShutterCloseAttr().Get(),
                             cortexCam.getShutter()[1])

            try:
                from pxr import CameraUtil
            except ImportError:
                # As far as I can tell, CameraUtil is a part of the Imaging
                # module, which we don't currently build in GafferHQ/dependencies.
                continue

            for usdFit, cortexFit in [
                (CameraUtil.MatchHorizontally,
                 IECoreScene.Camera.FilmFit.Horizontal),
                (CameraUtil.MatchVertically,
                 IECoreScene.Camera.FilmFit.Vertical),
                (CameraUtil.Fit, IECoreScene.Camera.FilmFit.Fit),
                (CameraUtil.Crop, IECoreScene.Camera.FilmFit.Fill)
            ]:

                for aspect in [0.3, 1, 2.5]:
                    usdWindow = CameraUtil.ConformedWindow(
                        c.frustum.GetWindow(), usdFit, aspect)
                    cortexWindow = cortexCam.frustum(cortexFit, aspect)
                    for i in range(2):
                        self.assertAlmostEqual(
                            usdWindow.min[i],
                            cortexWindow.min()[i],
                            delta=max(1, math.fabs(cortexWindow.min()[i])) *
                            0.000002)
                        self.assertAlmostEqual(
                            usdWindow.max[i],
                            cortexWindow.max()[i],
                            delta=max(1, math.fabs(cortexWindow.max()[i])) *
                            0.000002)

        del usdFile