1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_TOOLS_KEYSTOPLERP_HXX
25 #define _BGFX_TOOLS_KEYSTOPLERP_HXX
26 
27 #include <basegfx/numeric/ftools.hxx>
28 #include <vector>
29 #include <basegfx/basegfxdllapi.h>
30 
31 namespace com{ namespace sun{ namespace star{ namespace uno {
32     template<typename T> class Sequence;
33 }}}}
34 
35 namespace basegfx
36 {
37     namespace tools
38     {
39         /** Lerp in a vector of key stops
40 
41             This class holds a key stop vector and provides the
42             functionality to lerp inside it. Useful e.g. for
43             multi-stop gradients, or the SMIL key time activity.
44 
45             For those, given a global [0,1] lerp alpha, one need to
46             find the suitable bucket index from key stop vector, and
47             then calculate the relative alpha between the two buckets
48             found.
49          */
50         class BASEGFX_DLLPUBLIC KeyStopLerp
51         {
52         public:
53             typedef std::pair<std::ptrdiff_t,double> ResultType;
54 
55             /** Create lerper with given vector of stops
56 
57                 @param rKeyStops
58 
59                 Vector of stops, must contain at least two elements
60                 (though preferably more, otherwise you probably don't
61                 need key stop lerping in the first place). All
62                 elements must be of monotonically increasing value.
63              */
64             explicit KeyStopLerp( const std::vector<double>& rKeyStops );
65 
66             /** Create lerper with given sequence of stops
67 
68                 @param rKeyStops
69 
70                 Sequence of stops, must contain at least two elements
71                 (though preferably more, otherwise you probably don't
72                 need key stop lerping in the first place). All
73                 elements must be of monotonically increasing value.
74              */
75             explicit KeyStopLerp( const ::com::sun::star::uno::Sequence<double>& rKeyStops );
76 
77             /** Find two nearest bucket index & interpolate
78 
79                 @param fAlpha
80                 Find bucket index i, with keyStops[i] < fAlpha <=
81                 keyStops[i+1]. Return new alpha value in [0,1),
82                 proportional to fAlpha's position between keyStops[i]
83                 and keyStops[i+1]
84              */
85             ResultType lerp(double fAlpha) const;
86 
87         private:
88             std::vector<double>    maKeyStops;
89             mutable std::ptrdiff_t mnLastIndex;
90         };
91     }
92 }
93 
94 #endif
95