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 
30 namespace com{ namespace sun{ namespace star{ namespace uno {
31     template<typename T> class Sequence;
32 }}}}
33 
34 namespace basegfx
35 {
36     namespace tools
37     {
38         /** Lerp in a vector of key stops
39 
40             This class holds a key stop vector and provides the
41             functionality to lerp inside it. Useful e.g. for
42             multi-stop gradients, or the SMIL key time activity.
43 
44             For those, given a global [0,1] lerp alpha, one need to
45             find the suitable bucket index from key stop vector, and
46             then calculate the relative alpha between the two buckets
47             found.
48          */
49         class KeyStopLerp
50         {
51         public:
52             typedef std::pair<std::ptrdiff_t,double> ResultType;
53 
54             /** Create lerper with given vector of stops
55 
56                 @param rKeyStops
57 
58                 Vector of stops, must contain at least two elements
59                 (though preferrably more, otherwise you probably don't
60                 need key stop lerping in the first place). All
61                 elements must be of monotonically increasing value.
62              */
63             explicit KeyStopLerp( const std::vector<double>& rKeyStops );
64 
65             /** Create lerper with given sequence of stops
66 
67                 @param rKeyStops
68 
69                 Sequence of stops, must contain at least two elements
70                 (though preferrably more, otherwise you probably don't
71                 need key stop lerping in the first place). All
72                 elements must be of monotonically increasing value.
73              */
74             explicit KeyStopLerp( const ::com::sun::star::uno::Sequence<double>& rKeyStops );
75 
76             /** Find two nearest bucket index & interpolate
77 
78                 @param fAlpha
79                 Find bucket index i, with keyStops[i] < fAlpha <=
80                 keyStops[i+1]. Return new alpha value in [0,1),
81                 proportional to fAlpha's position between keyStops[i]
82                 and keyStops[i+1]
83              */
84             ResultType lerp(double fAlpha) const;
85 
86         private:
87             std::vector<double>    maKeyStops;
88             mutable std::ptrdiff_t mnLastIndex;
89         };
90     }
91 }
92 
93 #endif
94