xref: /AOO41X/main/basegfx/source/tools/keystoplerp.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2008 by Sun Microsystems, Inc.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * $RCSfile: canvastools.hxx,v $
10*cdf0e10cSrcweir  * $Revision: 1.10 $
11*cdf0e10cSrcweir  *
12*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
15*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
16*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
17*cdf0e10cSrcweir  *
18*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
19*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
22*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
23*cdf0e10cSrcweir  *
24*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
25*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
26*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
27*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
28*cdf0e10cSrcweir  *
29*cdf0e10cSrcweir  ************************************************************************/
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
32*cdf0e10cSrcweir #include "precompiled_basegfx.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include "basegfx/tools/keystoplerp.hxx"
35*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <algorithm>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir static void validateInput(const std::vector<double>& rKeyStops)
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir     (void)rKeyStops;
42*cdf0e10cSrcweir #ifdef DBG_UTIL
43*cdf0e10cSrcweir     OSL_ENSURE( rKeyStops.size() > 1,
44*cdf0e10cSrcweir                 "KeyStopLerp::KeyStopLerp(): key stop vector must have two entries or more" );
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     // rKeyStops must be sorted in ascending order
47*cdf0e10cSrcweir     for( ::std::size_t i=1, len=rKeyStops.size(); i<len; ++i )
48*cdf0e10cSrcweir     {
49*cdf0e10cSrcweir         if( rKeyStops[i-1] > rKeyStops[i] )
50*cdf0e10cSrcweir             OSL_ENSURE( false,
51*cdf0e10cSrcweir                         "KeyStopLerp::KeyStopLerp(): time vector is not sorted in ascending order!" );
52*cdf0e10cSrcweir     }
53*cdf0e10cSrcweir #endif
54*cdf0e10cSrcweir }
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir namespace basegfx
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     namespace tools
59*cdf0e10cSrcweir     {
60*cdf0e10cSrcweir         KeyStopLerp::KeyStopLerp( const std::vector<double>& rKeyStops ) :
61*cdf0e10cSrcweir             maKeyStops(rKeyStops),
62*cdf0e10cSrcweir             mnLastIndex(0)
63*cdf0e10cSrcweir         {
64*cdf0e10cSrcweir             validateInput(maKeyStops);
65*cdf0e10cSrcweir         }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir         KeyStopLerp::KeyStopLerp( const ::com::sun::star::uno::Sequence<double>& rKeyStops ) :
68*cdf0e10cSrcweir             maKeyStops(rKeyStops.getLength()),
69*cdf0e10cSrcweir             mnLastIndex(0)
70*cdf0e10cSrcweir         {
71*cdf0e10cSrcweir             std::copy( rKeyStops.getConstArray(),
72*cdf0e10cSrcweir                        rKeyStops.getConstArray()+rKeyStops.getLength(),
73*cdf0e10cSrcweir                        maKeyStops.begin() );
74*cdf0e10cSrcweir             validateInput(maKeyStops);
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         KeyStopLerp::ResultType KeyStopLerp::lerp(double fAlpha) const
78*cdf0e10cSrcweir         {
79*cdf0e10cSrcweir             // cached value still okay?
80*cdf0e10cSrcweir             if( maKeyStops.at(mnLastIndex) < fAlpha ||
81*cdf0e10cSrcweir                 maKeyStops.at(mnLastIndex+1) >= fAlpha )
82*cdf0e10cSrcweir             {
83*cdf0e10cSrcweir                 // nope, find new index
84*cdf0e10cSrcweir                 mnLastIndex = std::min<std::ptrdiff_t>(
85*cdf0e10cSrcweir                     maKeyStops.size()-2,
86*cdf0e10cSrcweir                     // range is ensured by max below
87*cdf0e10cSrcweir                     std::max<std::ptrdiff_t>(
88*cdf0e10cSrcweir                         0,
89*cdf0e10cSrcweir                         std::distance( maKeyStops.begin(),
90*cdf0e10cSrcweir                                        std::lower_bound( maKeyStops.begin(),
91*cdf0e10cSrcweir                                                          maKeyStops.end(),
92*cdf0e10cSrcweir                                                          fAlpha )) - 1 ));
93*cdf0e10cSrcweir             }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir             // lerp between stop and stop+1
96*cdf0e10cSrcweir             const double fRawLerp=
97*cdf0e10cSrcweir                 (fAlpha-maKeyStops.at(mnLastIndex)) /
98*cdf0e10cSrcweir                 (maKeyStops.at(mnLastIndex+1) - maKeyStops.at(mnLastIndex));
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir             // clamp to permissible range (input fAlpha might be
101*cdf0e10cSrcweir             // everything)
102*cdf0e10cSrcweir             return ResultType(
103*cdf0e10cSrcweir                 mnLastIndex,
104*cdf0e10cSrcweir                 clamp(fRawLerp,0.0,1.0));
105*cdf0e10cSrcweir         }
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir }
108