Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
21 : #define INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
22 :
23 : #include <basegfx/tools/lerp.hxx>
24 :
25 : namespace basegfx
26 : {
27 : namespace tools
28 : {
29 : // Interpolator specializations
30 : // ============================
31 :
32 : // NOTE: generic lerp is included from lerp.hxx. Following
33 : // are some specializations for various
34 : // not-straight-forward-interpolatable types
35 :
36 : /// Specialization for RGBColor, to employ color-specific interpolator
37 0 : template<> ::slideshow::internal::RGBColor lerp< ::slideshow::internal::RGBColor >(
38 : const ::slideshow::internal::RGBColor& rFrom,
39 : const ::slideshow::internal::RGBColor& rTo,
40 : double t )
41 : {
42 0 : return interpolate( rFrom, rTo, t );
43 : }
44 :
45 : /// Specialization also for sal_Int16, although this code should not be called
46 0 : template<> sal_Int16 lerp< sal_Int16 >( const sal_Int16&,
47 : const sal_Int16& rTo,
48 : double )
49 : {
50 : OSL_FAIL( "lerp<sal_Int16> called" );
51 0 : return rTo;
52 : }
53 :
54 : /// Specialization also for string, although this code should not be called
55 0 : template<> OUString lerp< OUString >( const OUString&,
56 : const OUString& rTo,
57 : double )
58 : {
59 : OSL_FAIL( "lerp<OUString> called" );
60 0 : return rTo;
61 : }
62 :
63 : /// Specialization also for bool, although this code should not be called
64 0 : template<> bool lerp< bool >( const bool&,
65 : const bool& rTo,
66 : double )
67 : {
68 : OSL_FAIL( "lerp<bool> called" );
69 0 : return rTo;
70 : }
71 : }
72 : }
73 :
74 : namespace slideshow
75 : {
76 : namespace internal
77 : {
78 : template< typename ValueType > struct Interpolator
79 : {
80 0 : ValueType operator()( const ValueType& rFrom,
81 : const ValueType& rTo,
82 : double t ) const
83 : {
84 0 : return basegfx::tools::lerp( rFrom, rTo, t );
85 : }
86 : };
87 :
88 : /// Specialization for HSLColor, to employ color-specific interpolator
89 : template<> struct Interpolator< HSLColor >
90 : {
91 0 : Interpolator( bool bCCW ) :
92 0 : mbCCW( bCCW )
93 : {
94 0 : }
95 :
96 0 : HSLColor operator()( const HSLColor& rFrom,
97 : const HSLColor& rTo,
98 : double t ) const
99 : {
100 0 : return interpolate( rFrom, rTo, t, mbCCW );
101 : }
102 :
103 : private:
104 : /// When true: interpolate counter-clockwise
105 : const bool mbCCW;
106 : };
107 :
108 :
109 : /** Generic linear interpolator
110 :
111 : @tpl ValueType
112 : Must have operator+ and operator* defined, and should
113 : have value semantics.
114 :
115 : @param rInterpolator
116 : Interpolator to use for lerp
117 :
118 : @param nFrame
119 : Must be in the [0,nTotalFrames) range
120 :
121 : @param nTotalFrames
122 : Total number of frames. Should be greater than zero.
123 : */
124 0 : template< typename ValueType > ValueType lerp( const Interpolator< ValueType >& rInterpolator,
125 : const ValueType& rFrom,
126 : const ValueType& rTo,
127 : sal_uInt32 nFrame,
128 : ::std::size_t nTotalFrames )
129 : {
130 : // TODO(P1): There's a nice HAKMEM trick for that
131 : // nTotalFrames > 1 condition below
132 :
133 : // for 1 and 0 frame animations, always take end value
134 0 : const double nFraction( nTotalFrames > 1 ? double(nFrame)/(nTotalFrames-1) : 1.0 );
135 :
136 0 : return rInterpolator( rFrom, rTo, nFraction );
137 : }
138 :
139 : /// Specialization for non-interpolatable constants/enums
140 0 : template<> sal_Int16 lerp< sal_Int16 >( const Interpolator< sal_Int16 >& /*rInterpolator*/,
141 : const sal_Int16& rFrom,
142 : const sal_Int16& rTo,
143 : sal_uInt32 nFrame,
144 : ::std::size_t nTotalFrames )
145 : {
146 : // until one half of the total frames are over, take from value.
147 : // after that, take to value.
148 : // For nFrames not divisable by 2, we prefer to over from, which
149 : // also neatly yields to for 1 frame activities
150 0 : return nFrame < nTotalFrames/2 ? rFrom : rTo;
151 : }
152 :
153 : /// Specialization for non-interpolatable strings
154 0 : template<> OUString lerp< OUString >( const Interpolator< OUString >& /*rInterpolator*/,
155 : const OUString& rFrom,
156 : const OUString& rTo,
157 : sal_uInt32 nFrame,
158 : ::std::size_t nTotalFrames )
159 : {
160 : // until one half of the total frames are over, take from value.
161 : // after that, take to value.
162 : // For nFrames not divisable by 2, we prefer to over from, which
163 : // also neatly yields to for 1 frame activities
164 0 : return nFrame < nTotalFrames/2 ? rFrom : rTo;
165 : }
166 :
167 : /// Specialization for non-interpolatable bools
168 0 : template<> bool lerp< bool >( const Interpolator< bool >& /*rInterpolator*/,
169 : const bool& bFrom,
170 : const bool& bTo,
171 : sal_uInt32 nFrame,
172 : ::std::size_t nTotalFrames )
173 : {
174 : // until one half of the total frames are over, take from value.
175 : // after that, take to value.
176 : // For nFrames not divisable by 2, we prefer to over from, which
177 : // also neatly yields to for 1 frame activities
178 0 : return nFrame < nTotalFrames/2 ? bFrom : bTo;
179 : }
180 : }
181 : }
182 :
183 : #endif // INCLUDED_SLIDESHOW_SOURCE_ENGINE_ACTIVITIES_INTERPOLATION_HXX
184 :
185 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|