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