1 //-*****************************************************************************
2 //
3 // Copyright (c) 2009-2011,
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 //-*****************************************************************************
36 
37 #include <Alembic/AbcGeom/CameraSample.h>
38 
39 namespace Alembic {
40 namespace AbcGeom {
41 namespace ALEMBIC_VERSION_NS {
42 
43 //-*****************************************************************************
CameraSample(double iTop,double iBottom,double iLeft,double iRight)44 CameraSample::CameraSample ( double iTop, double iBottom, double iLeft,
45     double iRight )
46 {
47     reset();
48     m_lensSqueezeRatio = (iRight - iLeft) * 0.5;
49     m_horizontalFilmOffset = (iLeft + m_lensSqueezeRatio) *
50         m_horizontalAperture / (2.0 * m_lensSqueezeRatio);
51 
52     m_verticalAperture = (iTop - iBottom) * 0.5 * m_horizontalAperture /
53         m_lensSqueezeRatio;
54 
55     m_verticalFilmOffset = ( (m_lensSqueezeRatio * m_verticalAperture) /
56         m_horizontalAperture + iBottom ) * m_horizontalAperture * 0.5;
57 }
58 
59 //-*****************************************************************************
getCoreValue(std::size_t iIndex) const60 double CameraSample::getCoreValue( std::size_t iIndex ) const
61 {
62     switch ( iIndex )
63     {
64         case 0:
65             return m_focalLength;
66         break;
67 
68         case 1:
69             return m_horizontalAperture;
70         break;
71 
72         case 2:
73             return m_horizontalFilmOffset;
74         break;
75 
76         case 3:
77             return m_verticalAperture;
78         break;
79 
80         case 4:
81             return m_verticalFilmOffset;
82         break;
83 
84         case 5:
85             return m_lensSqueezeRatio;
86         break;
87 
88         case 6:
89             return m_overscanLeft;
90         break;
91 
92         case 7:
93             return m_overscanRight;
94         break;
95 
96         case 8:
97             return m_overscanTop;
98         break;
99 
100         case 9:
101             return m_overscanBottom;
102         break;
103 
104         case 10:
105             return m_fStop;
106         break;
107 
108         case 11:
109             return m_focusDistance;
110         break;
111 
112         case 12:
113             return m_shutterOpen;
114         break;
115 
116         case 13:
117             return m_shutterClose;
118         break;
119 
120         case 14:
121             return m_nearClippingPlane;
122         break;
123 
124         case 15:
125             return m_farClippingPlane;
126         break;
127 
128         default:
129             ABCA_THROW( "Invalid index specified, must be 0-15 not: "
130                 << iIndex );
131         break;
132     }
133     // For compiler warning
134     return 0.0;
135 }
136 
137 //-*****************************************************************************
getFieldOfView() const138 double CameraSample::getFieldOfView () const
139 {
140     // * 10.0 since horizontal film aperture is in cm
141     return  2.0 * RadiansToDegrees( atan( m_horizontalAperture * 10.0 /
142         (2.0 * m_focalLength ) ) );
143 }
144 
145 //-*****************************************************************************
getScreenWindow(double & oTop,double & oBottom,double & oLeft,double & oRight)146 void CameraSample::getScreenWindow( double & oTop, double & oBottom,
147     double & oLeft, double & oRight )
148 {
149     double offsetX = 2.0 * m_horizontalFilmOffset * m_lensSqueezeRatio /
150         m_horizontalAperture;
151 
152     oLeft = -( 1.0 + m_overscanLeft ) * m_lensSqueezeRatio;
153 
154     oRight = ( 1.0 + m_overscanRight ) * m_lensSqueezeRatio;
155 
156     double aperY = m_lensSqueezeRatio * m_verticalAperture /
157         m_horizontalAperture;
158     double offsetY = 2.0 * m_verticalFilmOffset / m_horizontalAperture;
159 
160     oBottom = -( 1.0 + m_overscanBottom ) * aperY;
161     oTop = ( 1.0 + m_overscanTop ) * aperY;
162 
163     Abc::V2d lt ( oLeft, oTop );
164     Abc::V2d rb ( oRight, oBottom );
165     Abc::M33d mat = getFilmBackMatrix();
166 
167     Abc::V2d flt, frb;
168     mat.multVecMatrix( lt, flt );
169     mat.multVecMatrix( rb, frb );
170     oLeft = flt.x + offsetX;
171     oTop = flt.y + offsetY;
172     oRight = frb.x + offsetX;
173     oBottom = frb.y + offsetY;
174 }
175 
176 //-*****************************************************************************
addOp(FilmBackXformOp iOp)177 std::size_t CameraSample::addOp( FilmBackXformOp iOp )
178 {
179     m_ops.push_back( iOp );
180     return m_ops.size() - 1;
181 }
182 
183 //-*****************************************************************************
getOp(std::size_t iIndex) const184 FilmBackXformOp CameraSample::getOp( std::size_t iIndex ) const
185 {
186     ABCA_ASSERT( iIndex < m_ops.size(),
187         "Invalid index in CameraSample: " << iIndex );
188 
189     return m_ops[iIndex];
190 }
191 
192 //-*****************************************************************************
operator [](const std::size_t & iIndex)193 FilmBackXformOp & CameraSample::operator[]( const std::size_t &iIndex )
194 {
195     ABCA_ASSERT( iIndex < m_ops.size(),
196         "Invalid index in CameraSample: " << iIndex );
197 
198     return m_ops[iIndex];
199 }
200 
201 //-*****************************************************************************
operator [](const std::size_t & iIndex) const202 const FilmBackXformOp & CameraSample::operator[]
203     ( const std::size_t &iIndex ) const
204 {
205     ABCA_ASSERT( iIndex < m_ops.size(),
206         "Invalid index in CameraSample: " << iIndex );
207 
208     return m_ops[iIndex];
209 }
210 
211 //-*****************************************************************************
getFilmBackMatrix() const212 Abc::M33d CameraSample::getFilmBackMatrix () const
213 {
214     Abc::M33d ret;
215     ret.makeIdentity();
216 
217     for ( size_t i = 0 ; i < m_ops.size() ; ++i )
218     {
219         Abc::M33d m;
220         m.makeIdentity();
221 
222         FilmBackXformOp op = m_ops[i];
223 
224         if ( op.isMatrixOp() )
225         {
226             m = op.getMatrix();
227         }
228         else if ( op.isScaleOp() )
229         {
230             m.setScale ( op.getScale() );
231         }
232         else if ( op.isTranslateOp() )
233         {
234             m.setTranslation( op.getTranslate() );
235         }
236 
237         ret = m * ret;
238     }
239 
240     return ret;
241 }
242 
243 //-*****************************************************************************
getNumOps() const244 std::size_t CameraSample::getNumOps() const
245 {
246     return m_ops.size();
247 }
248 
249 //-*****************************************************************************
getNumOpChannels() const250 std::size_t CameraSample::getNumOpChannels() const
251 {
252     std::size_t ret = 0;
253     for ( size_t i = 0 ; i < m_ops.size() ; ++i )
254     {
255         ret += m_ops[i].getNumChannels();
256     }
257 
258     return ret;
259 }
260 
261 } // End namespace ALEMBIC_VERSION_NS
262 } // End namespace AbcGeom
263 } // End namespace Alembic
264