1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: canvastools.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basegfx.hxx"
34 #include "basegfx/tools/keystoplerp.hxx"
35 #include <com/sun/star/uno/Sequence.hxx>
39 static void validateInput(const std::vector
<double>& rKeyStops
)
43 OSL_ENSURE( rKeyStops
.size() > 1,
44 "KeyStopLerp::KeyStopLerp(): key stop vector must have two entries or more" );
46 // rKeyStops must be sorted in ascending order
47 for( ::std::size_t i
=1, len
=rKeyStops
.size(); i
<len
; ++i
)
49 if( rKeyStops
[i
-1] > rKeyStops
[i
] )
51 "KeyStopLerp::KeyStopLerp(): time vector is not sorted in ascending order!" );
60 KeyStopLerp::KeyStopLerp( const std::vector
<double>& rKeyStops
) :
61 maKeyStops(rKeyStops
),
64 validateInput(maKeyStops
);
67 KeyStopLerp::KeyStopLerp( const ::com::sun::star::uno::Sequence
<double>& rKeyStops
) :
68 maKeyStops(rKeyStops
.getLength()),
71 std::copy( rKeyStops
.getConstArray(),
72 rKeyStops
.getConstArray()+rKeyStops
.getLength(),
74 validateInput(maKeyStops
);
77 KeyStopLerp::ResultType
KeyStopLerp::lerp(double fAlpha
) const
79 // cached value still okay?
80 if( maKeyStops
.at(mnLastIndex
) < fAlpha
||
81 maKeyStops
.at(mnLastIndex
+1) >= fAlpha
)
83 // nope, find new index
84 mnLastIndex
= std::min
<std::ptrdiff_t>(
86 // range is ensured by max below
87 std::max
<std::ptrdiff_t>(
89 std::distance( maKeyStops
.begin(),
90 std::lower_bound( maKeyStops
.begin(),
95 // lerp between stop and stop+1
96 const double fRawLerp
=
97 (fAlpha
-maKeyStops
.at(mnLastIndex
)) /
98 (maKeyStops
.at(mnLastIndex
+1) - maKeyStops
.at(mnLastIndex
));
100 // clamp to permissible range (input fAlpha might be
104 clamp(fRawLerp
,0.0,1.0));