Update ooo320-m1
[ooovba.git] / slideshow / source / engine / activities / interpolation.hxx
blobb97f39555167cf636deaef80592fc18b95327aa7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: interpolation.hxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_SLIDESHOW_INTERPOLATION_HXX
32 #define INCLUDED_SLIDESHOW_INTERPOLATION_HXX
34 #include "lerp.hxx"
36 namespace slideshow
38 namespace internal
40 // Interpolator specializations
41 // ============================
43 // NOTE: generic lerp is included from lerp.hxx. Following
44 // are some specializations for various
45 // not-straight-forward-interpolatable types
47 /// Specialization for RGBColor, to employ color-specific interpolator
48 template<> RGBColor lerp< RGBColor >( const RGBColor& rFrom,
49 const RGBColor& rTo,
50 double t )
52 return interpolate( rFrom, rTo, t );
55 /// Specialization also for sal_Int16, although this code should not be called
56 template<> sal_Int16 lerp< sal_Int16 >( const sal_Int16&,
57 const sal_Int16& rTo,
58 double )
60 OSL_ENSURE( false,
61 "lerp<sal_Int16> called" );
62 return rTo;
65 /// Specialization also for string, although this code should not be called
66 template<> ::rtl::OUString lerp< ::rtl::OUString >( const ::rtl::OUString&,
67 const ::rtl::OUString& rTo,
68 double )
70 OSL_ENSURE( false,
71 "lerp<::rtl::OUString> called" );
72 return rTo;
75 /// Specialization also for bool, although this code should not be called
76 template<> bool lerp< bool >( const bool&,
77 const bool& rTo,
78 double )
80 OSL_ENSURE( false,
81 "lerp<bool> called" );
82 return rTo;
85 template< typename ValueType > struct Interpolator
87 ValueType operator()( const ValueType& rFrom,
88 const ValueType& rTo,
89 double t ) const
91 return lerp( rFrom, rTo, t );
95 /// Specialization for HSLColor, to employ color-specific interpolator
96 template<> struct Interpolator< HSLColor >
98 Interpolator( bool bCCW ) :
99 mbCCW( bCCW )
103 HSLColor operator()( const HSLColor& rFrom,
104 const HSLColor& rTo,
105 double t ) const
107 return interpolate( rFrom, rTo, t, mbCCW );
110 private:
111 /// When true: interpolate counter-clockwise
112 const bool mbCCW;
116 /** Generic linear interpolator
118 @tpl ValueType
119 Must have operator+ and operator* defined, and should
120 have value semantics.
122 @param rInterpolator
123 Interpolator to use for lerp
125 @param nFrame
126 Must be in the [0,nTotalFrames) range
128 @param nTotalFrames
129 Total number of frames. Should be greater than zero.
131 template< typename ValueType > ValueType lerp( const Interpolator< ValueType >& rInterpolator,
132 const ValueType& rFrom,
133 const ValueType& rTo,
134 sal_uInt32 nFrame,
135 ::std::size_t nTotalFrames )
137 // TODO(P1): There's a nice HAKMEM trick for that
138 // nTotalFrames > 1 condition below
140 // for 1 and 0 frame animations, always take end value
141 const double nFraction( nTotalFrames > 1 ? double(nFrame)/(nTotalFrames-1) : 1.0 );
143 return rInterpolator( rFrom, rTo, nFraction );
146 /// Specialization for non-interpolatable constants/enums
147 template<> sal_Int16 lerp< sal_Int16 >( const Interpolator< sal_Int16 >& /*rInterpolator*/,
148 const sal_Int16& rFrom,
149 const sal_Int16& rTo,
150 sal_uInt32 nFrame,
151 ::std::size_t nTotalFrames )
153 // until one half of the total frames are over, take from value.
154 // after that, take to value.
155 // For nFrames not divisable by 2, we prefer to over from, which
156 // also neatly yields to for 1 frame activities
157 return nFrame < nTotalFrames/2 ? rFrom : rTo;
160 /// Specialization for non-interpolatable strings
161 template<> ::rtl::OUString lerp< ::rtl::OUString >( const Interpolator< ::rtl::OUString >& /*rInterpolator*/,
162 const ::rtl::OUString& rFrom,
163 const ::rtl::OUString& rTo,
164 sal_uInt32 nFrame,
165 ::std::size_t nTotalFrames )
167 // until one half of the total frames are over, take from value.
168 // after that, take to value.
169 // For nFrames not divisable by 2, we prefer to over from, which
170 // also neatly yields to for 1 frame activities
171 return nFrame < nTotalFrames/2 ? rFrom : rTo;
174 /// Specialization for non-interpolatable bools
175 template<> bool lerp< bool >( const Interpolator< bool >& /*rInterpolator*/,
176 const bool& bFrom,
177 const bool& bTo,
178 sal_uInt32 nFrame,
179 ::std::size_t nTotalFrames )
181 // until one half of the total frames are over, take from value.
182 // after that, take to value.
183 // For nFrames not divisable by 2, we prefer to over from, which
184 // also neatly yields to for 1 frame activities
185 return nFrame < nTotalFrames/2 ? bFrom : bTo;
190 #endif /* INCLUDED_SLIDESHOW_INTERPOLATION_HXX */