1#-******************************************************************************
2#
3# Copyright (c) 2012,
4#  Sony Pictures Imageworks Inc. and
5#  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6#
7# All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions are
11# met:
12# *       Redistributions of source code must retain the above copyright
13# notice, this list of conditions and the following disclaimer.
14# *       Redistributions in binary form must reproduce the above
15# copyright notice, this list of conditions and the following disclaimer
16# in the documentation and/or other materials provided with the
17# distribution.
18# *       Neither the name of Sony Pictures Imageworks, nor
19# Industrial Light & Magic, nor the names of their contributors may be used
20# to endorse or promote products derived from this software without specific
21# prior written permission.
22#
23# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35#-******************************************************************************
36import unittest
37from imath import *
38from alembic.AbcCoreAbstract import *
39from alembic.Abc import *
40from alembic.AbcGeom import *
41
42testList = []
43
44kScaleFilmBackOperation = FilmBackXformOperationType.kScaleFilmBackOperation
45kTranslateFilmBackOperation = FilmBackXformOperationType.kTranslateFilmBackOperation
46
47class CameraTest(unittest.TestCase):
48    def testCameraExport(self):
49        """write out camera archive"""
50
51        archive = OArchive("camera1.abc")
52        simpleCamObj = OCamera(archive.getTop(), "simpleCam")
53        samp = CameraSample()
54        simpleCamObj.getSchema().set(samp)
55
56        camObj = OCamera(archive.getTop(), "cam")
57        camSchema = camObj.getSchema()
58        samp.addOp(FilmBackXformOp(kScaleFilmBackOperation, "scale"))
59        samp.addOp(FilmBackXformOp(kTranslateFilmBackOperation, "offset"))
60        self.assertTrue(camObj)
61        self.assertTrue(camObj.valid())
62        camSchema.set(samp)
63
64        samp[0].setScale(V2d(2.0, 3.0))
65        samp[1].setChannelValue(0, 4.0)
66        samp[1].setChannelValue(1, 5.0)
67
68        samp.setLensSqueezeRatio(2.0)
69        samp.setHorizontalAperture(4.8)
70        samp.setVerticalFilmOffset(3.0)
71        self.assertEqual(samp[0].getChannelValue(0), 2.0)
72        camSchema.set(samp)
73
74    def testCameraImport(self):
75        """read in camera archive"""
76
77        archive = IArchive("camera1.abc")
78        identity = M33d()
79        identity.makeIdentity()
80
81        samp = CameraSample()
82
83        simpleCam = ICamera(archive.getTop(), "simpleCam")
84        cam = ICamera(archive.getTop(), "cam")
85
86        self.assertTrue(cam)
87        self.assertTrue(cam.valid())
88
89        # test camera "simpleCam" default values
90        samp = simpleCam.getSchema().getValue()
91        self.assertEqual(simpleCam.getSchema().getNumSamples(), 1)
92        self.assertAlmostEqual(samp.getFocalLength(), 35.0)
93        self.assertAlmostEqual(samp.getHorizontalAperture(), 3.6)
94        self.assertAlmostEqual(samp.getVerticalAperture(), 2.4)
95        self.assertAlmostEqual(samp.getHorizontalFilmOffset(), 0.0)
96        self.assertAlmostEqual(samp.getVerticalFilmOffset(), 0.0)
97        self.assertAlmostEqual(samp.getLensSqueezeRatio(), 1.0)
98        self.assertAlmostEqual(samp.getOverScanLeft(), 0.0)
99        self.assertAlmostEqual(samp.getOverScanRight(), 0.0)
100        self.assertAlmostEqual(samp.getOverScanTop(), 0.0)
101        self.assertAlmostEqual(samp.getOverScanBottom(), 0.0)
102        self.assertAlmostEqual(samp.getFStop(), 5.6)
103        self.assertAlmostEqual(samp.getFocusDistance(), 5.0)
104        self.assertAlmostEqual(samp.getShutterOpen(), 0.0)
105        self.assertAlmostEqual(samp.getShutterClose(), 0.0208333333333)
106        self.assertAlmostEqual(samp.getNearClippingPlane(), 0.1)
107        self.assertAlmostEqual(samp.getFarClippingPlane(), 100000.0)
108        self.assertEqual(samp.getNumOps(), 0)
109        self.assertEqual(samp.getNumOpChannels(), 0)
110        self.assertEqual(samp.getFilmBackMatrix(), identity)
111
112        self.assertEqual(cam.getSchema().getNumSamples(), 2)
113
114        # test camera "cam" values at sample 0
115        samp = cam.getSchema().getValue()
116        self.assertAlmostEqual(samp.getFocalLength(), 35.0)
117        self.assertAlmostEqual(samp.getHorizontalAperture(), 3.6)
118        self.assertAlmostEqual(samp.getVerticalAperture(), 2.4)
119        self.assertAlmostEqual(samp.getHorizontalFilmOffset(), 0.0)
120        self.assertAlmostEqual(samp.getVerticalFilmOffset(), 0.0)
121        self.assertAlmostEqual(samp.getLensSqueezeRatio(), 1.0)
122        self.assertAlmostEqual(samp.getOverScanLeft(), 0.0)
123        self.assertAlmostEqual(samp.getOverScanRight(), 0.0)
124        self.assertAlmostEqual(samp.getOverScanTop(), 0.0)
125        self.assertAlmostEqual(samp.getOverScanBottom(), 0.0)
126        self.assertAlmostEqual(samp.getFStop(), 5.6)
127        self.assertAlmostEqual(samp.getFocusDistance(), 5.0)
128        self.assertAlmostEqual(samp.getShutterOpen(), 0.0)
129        self.assertAlmostEqual(samp.getShutterClose(), 0.0208333333333)
130        self.assertAlmostEqual(samp.getNearClippingPlane(), 0.1)
131        self.assertAlmostEqual(samp.getFarClippingPlane(), 100000.0)
132        self.assertEqual(samp.getNumOps(), 2)
133        self.assertEqual(samp.getNumOpChannels(), 4)
134        self.assertEqual(samp[0].getHint(), "scale")
135        self.assertEqual(samp[0].getType(), kScaleFilmBackOperation)
136        self.assertEqual(samp[0].getNumChannels(), 2)
137        self.assertAlmostEqual(samp[0].getChannelValue(0), 1.0)
138        self.assertAlmostEqual(samp[0].getChannelValue(1), 1.0)
139        self.assertEqual(samp[1].getHint(), "offset")
140        self.assertEqual(samp[1].getType(), kTranslateFilmBackOperation)
141        self.assertEqual(samp[1].getNumChannels(), 2)
142        self.assertAlmostEqual(samp[1].getChannelValue(0), 0.0)
143        self.assertAlmostEqual(samp[1].getChannelValue(1), 0.0)
144        self.assertEqual(samp.getFilmBackMatrix(), identity)
145
146        # test cam values at sample 1
147        samp = cam.getSchema().getValue(ISampleSelector(1))
148        self.assertAlmostEqual(samp.getFocalLength(), 35.0)
149        self.assertAlmostEqual(samp.getHorizontalAperture(), 4.8)
150        self.assertAlmostEqual(samp.getVerticalAperture(), 2.4)
151        self.assertAlmostEqual(samp.getHorizontalFilmOffset(), 0.0)
152        self.assertAlmostEqual(samp.getVerticalFilmOffset(), 3.0)
153        self.assertAlmostEqual(samp.getLensSqueezeRatio(), 2.0)
154        self.assertAlmostEqual(samp.getOverScanLeft(), 0.0)
155        self.assertAlmostEqual(samp.getOverScanRight(), 0.0)
156        self.assertAlmostEqual(samp.getOverScanTop(), 0.0)
157        self.assertAlmostEqual(samp.getOverScanBottom(), 0.0)
158        self.assertAlmostEqual(samp.getFStop(), 5.6)
159        self.assertAlmostEqual(samp.getFocusDistance(), 5.0)
160        self.assertAlmostEqual(samp.getShutterOpen(), 0.0)
161        self.assertAlmostEqual(samp.getShutterClose(), 0.0208333333333)
162        self.assertAlmostEqual(samp.getNearClippingPlane(), 0.1)
163        self.assertAlmostEqual(samp.getFarClippingPlane(), 100000.0)
164        self.assertEqual(samp.getNumOps(), 2)
165        self.assertEqual(samp.getNumOpChannels(), 4)
166        self.assertEqual(samp[0].getHint(), "scale")
167        self.assertEqual(samp[0].getType(), kScaleFilmBackOperation)
168        self.assertEqual(samp[0].getNumChannels(), 2)
169        self.assertAlmostEqual(samp[0].getChannelValue(0), 2.0), samp[0].getChannelValue(0)
170        self.assertAlmostEqual(samp[0].getChannelValue(1), 3.0), samp[0].getChannelValue(1)
171        self.assertEqual(samp[1].getHint(), "offset")
172        self.assertEqual(samp[1].getType(), kTranslateFilmBackOperation)
173        self.assertEqual(samp[1].getNumChannels(), 2)
174        self.assertAlmostEqual(samp[1].getChannelValue(0), 4.0)
175        self.assertAlmostEqual(samp[1].getChannelValue(1), 5.0)
176