Update git submodules
[LibreOffice.git] / slideshow / source / engine / activities / interpolation.hxx
blob155456ed576dca655af55986b5f4b110b7a3f281
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
21 #define INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
23 #include <basegfx/utils/lerp.hxx>
24 #include <osl/diagnose.h>
25 #include <rtl/ustring.hxx>
27 #include <rgbcolor.hxx>
28 #include <hslcolor.hxx>
30 namespace basegfx
32 namespace utils
34 // Interpolator specializations
35 // ============================
37 // NOTE: generic lerp is included from lerp.hxx. Following
38 // are some specializations for various
39 // not-straight-forward-interpolatable types
41 /// Specialization for RGBColor, to employ color-specific interpolator
42 template<> ::slideshow::internal::RGBColor lerp< ::slideshow::internal::RGBColor >(
43 const ::slideshow::internal::RGBColor& rFrom,
44 const ::slideshow::internal::RGBColor& rTo,
45 double t )
47 return interpolate( rFrom, rTo, t );
50 /// Specialization also for sal_Int16, although this code should not be called
51 template<> sal_Int16 lerp< sal_Int16 >( const sal_Int16&,
52 const sal_Int16& rTo,
53 double )
55 OSL_FAIL( "lerp<sal_Int16> called" );
56 return rTo;
59 /// Specialization also for string, although this code should not be called
60 template<> OUString lerp< OUString >( const OUString&,
61 const OUString& rTo,
62 double )
64 OSL_FAIL( "lerp<OUString> called" );
65 return rTo;
68 /// Specialization also for bool, although this code should not be called
69 template<> bool lerp< bool >( const bool&,
70 const bool& rTo,
71 double )
73 OSL_FAIL( "lerp<bool> called" );
74 return rTo;
79 namespace slideshow
81 namespace internal
83 template< typename ValueType > struct Interpolator
85 ValueType operator()( const ValueType& rFrom,
86 const ValueType& rTo,
87 double t ) const
89 return basegfx::utils::lerp( rFrom, rTo, t );
93 /// Specialization for HSLColor, to employ color-specific interpolator
94 template<> struct Interpolator< HSLColor >
96 explicit Interpolator( bool bCCW ) :
97 mbCCW( bCCW )
101 HSLColor operator()( const HSLColor& rFrom,
102 const HSLColor& rTo,
103 double t ) const
105 return interpolate( rFrom, rTo, t, mbCCW );
108 private:
109 /// When true: interpolate counter-clockwise
110 const bool mbCCW;
114 /** Generic linear interpolator
116 @tpl ValueType
117 Must have operator+ and operator* defined, and should
118 have value semantics.
120 @param rInterpolator
121 Interpolator to use for lerp
123 @param nFrame
124 Must be in the [0,nTotalFrames) range
126 @param nTotalFrames
127 Total number of frames. Should be greater than zero.
129 template< typename ValueType > ValueType lerp( const Interpolator< ValueType >& rInterpolator,
130 const ValueType& rFrom,
131 const ValueType& rTo,
132 sal_uInt32 nFrame,
133 ::std::size_t nTotalFrames )
135 // TODO(P1): There's a nice HAKMEM trick for that
136 // nTotalFrames > 1 condition below
138 // for 1 and 0 frame animations, always take end value
139 const double nFraction( nTotalFrames > 1 ? double(nFrame)/(nTotalFrames-1) : 1.0 );
141 return rInterpolator( rFrom, rTo, nFraction );
144 /// Specialization for non-interpolatable constants/enums
145 template<> sal_Int16 lerp< sal_Int16 >( const Interpolator< sal_Int16 >& /*rInterpolator*/,
146 const sal_Int16& rFrom,
147 const sal_Int16& rTo,
148 sal_uInt32 nFrame,
149 ::std::size_t nTotalFrames )
151 // until one half of the total frames are over, take from value.
152 // after that, take to value.
153 // For nFrames not divisible by 2, we prefer to over from, which
154 // also neatly yields to for 1 frame activities
155 return nFrame < nTotalFrames/2 ? rFrom : rTo;
158 /// Specialization for non-interpolatable strings
159 template<> OUString lerp< OUString >( const Interpolator< OUString >& /*rInterpolator*/,
160 const OUString& rFrom,
161 const OUString& rTo,
162 sal_uInt32 nFrame,
163 ::std::size_t nTotalFrames )
165 // until one half of the total frames are over, take from value.
166 // after that, take to value.
167 // For nFrames not divisible by 2, we prefer to over from, which
168 // also neatly yields to for 1 frame activities
169 return nFrame < nTotalFrames/2 ? rFrom : rTo;
172 /// Specialization for non-interpolatable bools
173 template<> bool lerp< bool >( const Interpolator< bool >& /*rInterpolator*/,
174 const bool& bFrom,
175 const bool& bTo,
176 sal_uInt32 nFrame,
177 ::std::size_t nTotalFrames )
179 // until one half of the total frames are over, take from value.
180 // after that, take to value.
181 // For nFrames not divisible by 2, we prefer to over from, which
182 // also neatly yields to for 1 frame activities
183 return nFrame < nTotalFrames/2 ? bFrom : bTo;
188 #endif // INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */