1*aaef562fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*aaef562fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*aaef562fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*aaef562fSAndrew Rist  * distributed with this work for additional information
6*aaef562fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*aaef562fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*aaef562fSAndrew Rist  * "License"); you may not use this file except in compliance
9*aaef562fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*aaef562fSAndrew Rist  *
11*aaef562fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*aaef562fSAndrew Rist  *
13*aaef562fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*aaef562fSAndrew Rist  * software distributed under the License is distributed on an
15*aaef562fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*aaef562fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*aaef562fSAndrew Rist  * specific language governing permissions and limitations
18*aaef562fSAndrew Rist  * under the License.
19*aaef562fSAndrew Rist  *
20*aaef562fSAndrew Rist  *************************************************************/
21*aaef562fSAndrew Rist 
22*aaef562fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef INCLUDED_SLIDESHOW_INTERPOLATION_HXX
25cdf0e10cSrcweir #define INCLUDED_SLIDESHOW_INTERPOLATION_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <basegfx/tools/lerp.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace basegfx
30cdf0e10cSrcweir {
31cdf0e10cSrcweir     namespace tools
32cdf0e10cSrcweir     {
33cdf0e10cSrcweir         // Interpolator specializations
34cdf0e10cSrcweir         // ============================
35cdf0e10cSrcweir 
36cdf0e10cSrcweir         // NOTE: generic lerp is included from lerp.hxx. Following
37cdf0e10cSrcweir         // are some specializations for various
38cdf0e10cSrcweir         // not-straight-forward-interpolatable types
39cdf0e10cSrcweir 
40cdf0e10cSrcweir         /// Specialization for RGBColor, to employ color-specific interpolator
lerp(const::slideshow::internal::RGBColor & rFrom,const::slideshow::internal::RGBColor & rTo,double t)41cdf0e10cSrcweir 		template<> ::slideshow::internal::RGBColor lerp< ::slideshow::internal::RGBColor >(
42cdf0e10cSrcweir             const ::slideshow::internal::RGBColor& rFrom,
43cdf0e10cSrcweir             const ::slideshow::internal::RGBColor& rTo,
44cdf0e10cSrcweir             double			                       t	 )
45cdf0e10cSrcweir         {
46cdf0e10cSrcweir             return interpolate( rFrom, rTo, t );
47cdf0e10cSrcweir         }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir         /// Specialization also for sal_Int16, although this code should not be called
lerp(const sal_Int16 &,const sal_Int16 & rTo,double)50cdf0e10cSrcweir         template<> sal_Int16 lerp< sal_Int16 >( const sal_Int16&,
51cdf0e10cSrcweir                                                 const sal_Int16& 	rTo,
52cdf0e10cSrcweir                                                 double					   )
53cdf0e10cSrcweir         {
54cdf0e10cSrcweir             OSL_ENSURE( false,
55cdf0e10cSrcweir                         "lerp<sal_Int16> called" );
56cdf0e10cSrcweir             return rTo;
57cdf0e10cSrcweir         }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 		/// Specialization also for string, although this code should not be called
lerp(const::rtl::OUString &,const::rtl::OUString & rTo,double)60cdf0e10cSrcweir         template<> ::rtl::OUString lerp< ::rtl::OUString >( const ::rtl::OUString&,
61cdf0e10cSrcweir                                                             const ::rtl::OUString& 	rTo,
62cdf0e10cSrcweir                                                             double					     )
63cdf0e10cSrcweir         {
64cdf0e10cSrcweir             OSL_ENSURE( false,
65cdf0e10cSrcweir                         "lerp<::rtl::OUString> called" );
66cdf0e10cSrcweir             return rTo;
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 		/// Specialization also for bool, although this code should not be called
lerp(const bool &,const bool & rTo,double)70cdf0e10cSrcweir         template<> bool lerp< bool >( const bool&,
71cdf0e10cSrcweir                                       const bool& 	rTo,
72cdf0e10cSrcweir                                       double		     )
73cdf0e10cSrcweir         {
74cdf0e10cSrcweir             OSL_ENSURE( false,
75cdf0e10cSrcweir                         "lerp<bool> called" );
76cdf0e10cSrcweir             return rTo;
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir namespace slideshow
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     namespace internal
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         template< typename ValueType > struct Interpolator
86cdf0e10cSrcweir         {
operator ()slideshow::internal::Interpolator87cdf0e10cSrcweir             ValueType operator()( const ValueType& 	rFrom,
88cdf0e10cSrcweir                                   const ValueType& 	rTo,
89cdf0e10cSrcweir                                   double			t ) const
90cdf0e10cSrcweir             {
91cdf0e10cSrcweir                 return basegfx::tools::lerp( rFrom, rTo, t );
92cdf0e10cSrcweir             }
93cdf0e10cSrcweir         };
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 		/// Specialization for HSLColor, to employ color-specific interpolator
96cdf0e10cSrcweir         template<> struct Interpolator< HSLColor >
97cdf0e10cSrcweir         {
Interpolatorslideshow::internal::Interpolator98cdf0e10cSrcweir             Interpolator( bool bCCW ) :
99cdf0e10cSrcweir                 mbCCW( bCCW )
100cdf0e10cSrcweir             {
101cdf0e10cSrcweir             }
102cdf0e10cSrcweir 
operator ()slideshow::internal::Interpolator103cdf0e10cSrcweir             HSLColor operator()( const HSLColor&	rFrom,
104cdf0e10cSrcweir                                  const HSLColor&	rTo,
105cdf0e10cSrcweir                                  double				t ) const
106cdf0e10cSrcweir             {
107cdf0e10cSrcweir                 return interpolate( rFrom, rTo, t, mbCCW );
108cdf0e10cSrcweir             }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         private:
111cdf0e10cSrcweir             /// When true: interpolate counter-clockwise
112cdf0e10cSrcweir             const bool mbCCW;
113cdf0e10cSrcweir         };
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		/** Generic linear interpolator
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 			@tpl ValueType
119cdf0e10cSrcweir             Must have operator+ and operator* defined, and should
120cdf0e10cSrcweir             have value semantics.
121cdf0e10cSrcweir 
122cdf0e10cSrcweir             @param rInterpolator
123cdf0e10cSrcweir             Interpolator to use for lerp
124cdf0e10cSrcweir 
125cdf0e10cSrcweir             @param nFrame
126cdf0e10cSrcweir             Must be in the [0,nTotalFrames) range
127cdf0e10cSrcweir 
128cdf0e10cSrcweir             @param nTotalFrames
129cdf0e10cSrcweir             Total number of frames. Should be greater than zero.
130cdf0e10cSrcweir         */
lerp(const Interpolator<ValueType> & rInterpolator,const ValueType & rFrom,const ValueType & rTo,sal_uInt32 nFrame,::std::size_t nTotalFrames)131cdf0e10cSrcweir         template< typename ValueType > ValueType lerp( const Interpolator< ValueType >& rInterpolator,
132cdf0e10cSrcweir                                                        const ValueType& 				rFrom,
133cdf0e10cSrcweir                                                        const ValueType& 				rTo,
134cdf0e10cSrcweir                                                        sal_uInt32						nFrame,
135cdf0e10cSrcweir                                                        ::std::size_t					nTotalFrames )
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             // TODO(P1): There's a nice HAKMEM trick for that
138cdf0e10cSrcweir             // nTotalFrames > 1 condition below
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             // for 1 and 0 frame animations, always take end value
141cdf0e10cSrcweir             const double nFraction( nTotalFrames > 1 ? double(nFrame)/(nTotalFrames-1) : 1.0 );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir             return rInterpolator( rFrom, rTo, nFraction );
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 		/// Specialization for non-interpolatable constants/enums
lerp(const Interpolator<sal_Int16> &,const sal_Int16 & rFrom,const sal_Int16 & rTo,sal_uInt32 nFrame,::std::size_t nTotalFrames)147cdf0e10cSrcweir         template<> sal_Int16 lerp< sal_Int16 >( const Interpolator< sal_Int16 >& 	/*rInterpolator*/,
148cdf0e10cSrcweir                                                 const sal_Int16& 					rFrom,
149cdf0e10cSrcweir                                                 const sal_Int16& 					rTo,
150cdf0e10cSrcweir                                                 sal_uInt32							nFrame,
151cdf0e10cSrcweir                                                 ::std::size_t						nTotalFrames )
152cdf0e10cSrcweir         {
153cdf0e10cSrcweir             // until one half of the total frames are over, take from value.
154cdf0e10cSrcweir             // after that, take to value.
155cdf0e10cSrcweir             // For nFrames not divisable by 2, we prefer to over from, which
156cdf0e10cSrcweir             // also neatly yields to for 1 frame activities
157cdf0e10cSrcweir             return nFrame < nTotalFrames/2 ? rFrom : rTo;
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 		/// Specialization for non-interpolatable strings
lerp(const Interpolator<::rtl::OUString> &,const::rtl::OUString & rFrom,const::rtl::OUString & rTo,sal_uInt32 nFrame,::std::size_t nTotalFrames)161cdf0e10cSrcweir         template<> ::rtl::OUString lerp< ::rtl::OUString >( const Interpolator< ::rtl::OUString >& 	/*rInterpolator*/,
162cdf0e10cSrcweir                                                             const ::rtl::OUString& 					rFrom,
163cdf0e10cSrcweir                                                             const ::rtl::OUString& 					rTo,
164cdf0e10cSrcweir                                                             sal_uInt32								nFrame,
165cdf0e10cSrcweir                                                             ::std::size_t							nTotalFrames )
166cdf0e10cSrcweir         {
167cdf0e10cSrcweir             // until one half of the total frames are over, take from value.
168cdf0e10cSrcweir             // after that, take to value.
169cdf0e10cSrcweir             // For nFrames not divisable by 2, we prefer to over from, which
170cdf0e10cSrcweir             // also neatly yields to for 1 frame activities
171cdf0e10cSrcweir             return nFrame < nTotalFrames/2 ? rFrom : rTo;
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 		/// Specialization for non-interpolatable bools
lerp(const Interpolator<bool> &,const bool & bFrom,const bool & bTo,sal_uInt32 nFrame,::std::size_t nTotalFrames)175cdf0e10cSrcweir         template<> bool lerp< bool >( const Interpolator< bool >& 	/*rInterpolator*/,
176cdf0e10cSrcweir                                       const bool&					bFrom,
177cdf0e10cSrcweir                                       const bool&					bTo,
178cdf0e10cSrcweir                                       sal_uInt32					nFrame,
179cdf0e10cSrcweir                                       ::std::size_t					nTotalFrames )
180cdf0e10cSrcweir         {
181cdf0e10cSrcweir             // until one half of the total frames are over, take from value.
182cdf0e10cSrcweir             // after that, take to value.
183cdf0e10cSrcweir             // For nFrames not divisable by 2, we prefer to over from, which
184cdf0e10cSrcweir             // also neatly yields to for 1 frame activities
185cdf0e10cSrcweir             return nFrame < nTotalFrames/2 ? bFrom : bTo;
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir #endif /* INCLUDED_SLIDESHOW_INTERPOLATION_HXX */
191