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 #include "SampleUtil.h"
37 
38 #include <algorithm>
39 #include <ImathMatrix.h>
40 #include <ImathMatrixAlgo.h>
41 #include <ImathQuat.h>
42 #include <ImathEuler.h>
43 
44 //-*****************************************************************************
GetRelevantSampleTimes(ProcArgs & args,TimeSamplingPtr timeSampling,size_t numSamples,SampleTimeSet & output,MatrixSampleMap * inheritedSamples)45 void GetRelevantSampleTimes( ProcArgs &args, TimeSamplingPtr timeSampling,
46                             size_t numSamples, SampleTimeSet &output,
47                             MatrixSampleMap * inheritedSamples)
48 {
49     if ( numSamples < 2 )
50     {
51         output.insert( 0.0 );
52         return;
53     }
54 
55     chrono_t frameTime = args.frame / args.fps;
56 
57     chrono_t shutterOpenTime = ( args.frame + args.shutterOpen ) / args.fps;
58 
59     chrono_t shutterCloseTime = ( args.frame + args.shutterClose ) / args.fps;
60 
61 
62     // For interpolating and concatenating samples, we need to consider
63     // possible inherited sample times outside of our natural shutter range
64     if (inheritedSamples && inheritedSamples->size() > 1)
65     {
66         shutterOpenTime = std::min(shutterOpenTime,
67                 inheritedSamples->begin()->first);
68         shutterCloseTime = std::max(shutterCloseTime,
69                 inheritedSamples->rbegin()->first);
70     }
71 
72 
73 
74     std::pair<index_t, chrono_t> shutterOpenFloor =
75         timeSampling->getFloorIndex( shutterOpenTime, numSamples );
76 
77     std::pair<index_t, chrono_t> shutterCloseCeil =
78         timeSampling->getCeilIndex( shutterCloseTime, numSamples );
79 
80     //TODO, what's a reasonable episilon?
81     static const chrono_t epsilon = 1.0 / 10000.0;
82 
83     //check to see if our second sample is really the
84     //floor that we want due to floating point slop
85     //first make sure that we have at least two samples to work with
86     if ( shutterOpenFloor.first < shutterCloseCeil.first )
87     {
88         //if our open sample is less than open time,
89         //look at the next index time
90         if ( shutterOpenFloor.second < shutterOpenTime )
91         {
92             chrono_t nextSampleTime =
93                      timeSampling->getSampleTime( shutterOpenFloor.first + 1 );
94 
95             if ( fabs( nextSampleTime - shutterOpenTime ) < epsilon )
96             {
97                 shutterOpenFloor.first += 1;
98                 shutterOpenFloor.second = nextSampleTime;
99             }
100         }
101     }
102 
103 
104     for ( index_t i = shutterOpenFloor.first; i < shutterCloseCeil.first; ++i )
105     {
106         output.insert( timeSampling->getSampleTime( i ) );
107     }
108 
109     //no samples above? put frame time in there and get out
110     if ( output.size() == 0 )
111     {
112         output.insert( frameTime );
113         return;
114     }
115 
116     chrono_t lastSample = *(output.rbegin() );
117 
118     //determine whether we need the extra sample at the end
119     if ( ( fabs( lastSample - shutterCloseTime ) > epsilon )
120          && lastSample < shutterCloseTime )
121     {
122         output.insert( shutterCloseCeil.second );
123     }
124 }
125 
126 //-*****************************************************************************
127 
128 namespace
129 {
130 
DecomposeXForm(const Imath::M44d & mat,Imath::V3d & scale,Imath::V3d & shear,Imath::Quatd & rotation,Imath::V3d & translation)131     void DecomposeXForm(
132             const Imath::M44d &mat,
133             Imath::V3d &scale,
134             Imath::V3d &shear,
135             Imath::Quatd &rotation,
136             Imath::V3d &translation
137     )
138     {
139         Imath::M44d mat_remainder(mat);
140 
141         // Extract Scale, Shear
142         Imath::extractAndRemoveScalingAndShear(mat_remainder, scale, shear);
143 
144         // Extract translation
145         translation.x = mat_remainder[3][0];
146         translation.y = mat_remainder[3][1];
147         translation.z = mat_remainder[3][2];
148 
149         // Extract rotation
150         rotation = extractQuat(mat_remainder);
151     }
152 
RecomposeXForm(const Imath::V3d & scale,const Imath::V3d & shear,const Imath::Quatd & rotation,const Imath::V3d & translation)153     M44d RecomposeXForm(
154             const Imath::V3d &scale,
155             const Imath::V3d &shear,
156             const Imath::Quatd &rotation,
157             const Imath::V3d &translation
158     )
159     {
160         Imath::M44d scale_mtx, shear_mtx, rotation_mtx, translation_mtx;
161 
162         scale_mtx.setScale(scale);
163         shear_mtx.setShear(shear);
164         rotation_mtx = rotation.toMatrix44();
165         translation_mtx.setTranslation(translation);
166 
167         return scale_mtx * shear_mtx * rotation_mtx * translation_mtx;
168     }
169 
170 
171     // when amt is 0, a is returned
lerp(double a,double b,double amt)172     inline double lerp(double a, double b, double amt)
173     {
174         return (a + (b-a)*amt);
175     }
176 
177 
lerp(const Imath::V3d & a,const Imath::V3d & b,double amt)178     Imath::V3d lerp(const Imath::V3d &a, const Imath::V3d &b, double amt)
179     {
180         return Imath::V3d(lerp(a[0], b[0], amt),
181                           lerp(a[1], b[1], amt),
182                           lerp(a[2], b[2], amt));
183     }
184 
185 
GetNaturalOrInterpolatedSampleForTime(const MatrixSampleMap & samples,Abc::chrono_t sampleTime)186     M44d GetNaturalOrInterpolatedSampleForTime(const MatrixSampleMap & samples,
187             Abc::chrono_t sampleTime)
188     {
189         MatrixSampleMap::const_iterator I = samples.find(sampleTime);
190         if (I != samples.end())
191         {
192             return (*I).second;
193         }
194 
195         if (samples.empty())
196         {
197             return M44d();
198         }
199 
200         if (samples.size() == 1)
201         {
202             return samples.begin()->second;
203         }
204 
205         if (sampleTime <= samples.begin()->first)
206         {
207             return samples.begin()->second;
208         }
209 
210         if (sampleTime >= samples.rbegin()->first)
211         {
212             return samples.rbegin()->second;
213         }
214 
215         //find the floor and ceiling samples and interpolate
216         Abc::chrono_t lTime = samples.begin()->first;
217         Abc::chrono_t rTime = samples.rbegin()->first;
218 
219 
220 
221         for (MatrixSampleMap::const_iterator I = samples.begin();
222                 I != samples.end(); ++I)
223         {
224             Abc::chrono_t testSampleTime= (*I).first;
225 
226             if (testSampleTime > lTime && testSampleTime <= sampleTime)
227             {
228                 lTime = testSampleTime;
229             }
230             if (testSampleTime > rTime && testSampleTime >= sampleTime)
231             {
232                 rTime = testSampleTime;
233             }
234         }
235 
236 
237         M44d mtx_l;
238         M44d mtx_r;
239 
240         {
241             MatrixSampleMap::const_iterator I;
242 
243             I = samples.find(lTime);
244             if (I != samples.end())
245             {
246                 mtx_l = (*I).second;
247             }
248 
249             I = samples.find(rTime);
250             if (I != samples.end())
251             {
252                 mtx_r = (*I).second;
253             }
254 
255 
256 
257 
258         }
259 
260         Imath::V3d s_l,s_r,h_l,h_r,t_l,t_r;
261         Imath::Quatd quat_l,quat_r;
262 
263         DecomposeXForm(mtx_l, s_l, h_l, quat_l, t_l);
264         DecomposeXForm(mtx_r, s_r, h_r, quat_r, t_r);
265 
266         Abc::chrono_t amt = (sampleTime-lTime) / (rTime-lTime);
267 
268         if ((quat_l ^ quat_r) < 0)
269         {
270             quat_r = -quat_r;
271         }
272 
273         return RecomposeXForm(lerp(s_l, s_r, amt),
274                                  lerp(h_l, h_r, amt),
275                                  Imath::slerp(quat_l, quat_r, amt),
276                                  lerp(t_l, t_r, amt));
277 
278 
279 
280     }
281 
282 
283 
284 }
285 
286 //-*****************************************************************************
287 
ConcatenateXformSamples(ProcArgs & args,const MatrixSampleMap & parentSamples,const MatrixSampleMap & localSamples,MatrixSampleMap & outputSamples)288 void ConcatenateXformSamples( ProcArgs &args,
289         const MatrixSampleMap & parentSamples,
290         const MatrixSampleMap & localSamples,
291         MatrixSampleMap & outputSamples)
292 {
293     SampleTimeSet unionOfSampleTimes;
294 
295     for (MatrixSampleMap::const_iterator I = parentSamples.begin();
296             I != parentSamples.end(); ++I)
297     {
298         unionOfSampleTimes.insert((*I).first);
299     }
300 
301     for (MatrixSampleMap::const_iterator I = localSamples.begin();
302             I != localSamples.end(); ++I)
303     {
304         unionOfSampleTimes.insert((*I).first);
305     }
306 
307     for (SampleTimeSet::iterator I = unionOfSampleTimes.begin();
308             I != unionOfSampleTimes.end(); ++I)
309     {
310         M44d parentMtx = GetNaturalOrInterpolatedSampleForTime(parentSamples,
311                 (*I));
312         M44d localMtx = GetNaturalOrInterpolatedSampleForTime(localSamples,
313                 (*I));
314 
315         outputSamples[(*I)] = localMtx * parentMtx;
316     }
317 }
318 
319 //-*****************************************************************************
320 
GetRelativeSampleTime(ProcArgs & args,Abc::chrono_t sampleTime)321 Abc::chrono_t GetRelativeSampleTime( ProcArgs &args, Abc::chrono_t sampleTime)
322 {
323     const chrono_t epsilon = 1.0 / 10000.0;
324 
325 
326     chrono_t frameTime = args.frame / args.fps;
327 
328     Abc::chrono_t result = ( sampleTime - frameTime ) * args.fps;
329 
330     if ( fabs( result ) < epsilon )
331     {
332         result = 0.0;
333     }
334 
335     return result;
336 }
337 
338 
339