Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <basegfx/tools/keystoplerp.hxx>
21 : #include <com/sun/star/uno/Sequence.hxx>
22 :
23 : #include <algorithm>
24 :
25 0 : static void validateInput(const std::vector<double>& rKeyStops)
26 : {
27 : (void)rKeyStops;
28 : #ifdef DBG_UTIL
29 : OSL_ENSURE( rKeyStops.size() > 1,
30 : "KeyStopLerp::KeyStopLerp(): key stop vector must have two entries or more" );
31 :
32 : // rKeyStops must be sorted in ascending order
33 : for( ::std::size_t i=1, len=rKeyStops.size(); i<len; ++i )
34 : {
35 : if( rKeyStops[i-1] > rKeyStops[i] )
36 : OSL_FAIL( "KeyStopLerp::KeyStopLerp(): time vector is not sorted in ascending order!" );
37 : }
38 : #endif
39 0 : }
40 :
41 : namespace basegfx
42 : {
43 : namespace tools
44 : {
45 0 : KeyStopLerp::KeyStopLerp( const std::vector<double>& rKeyStops ) :
46 : maKeyStops(rKeyStops),
47 0 : mnLastIndex(0)
48 : {
49 0 : validateInput(maKeyStops);
50 0 : }
51 :
52 0 : KeyStopLerp::KeyStopLerp( const ::com::sun::star::uno::Sequence<double>& rKeyStops ) :
53 0 : maKeyStops(rKeyStops.getLength()),
54 0 : mnLastIndex(0)
55 : {
56 : std::copy( rKeyStops.getConstArray(),
57 0 : rKeyStops.getConstArray()+rKeyStops.getLength(),
58 0 : maKeyStops.begin() );
59 0 : validateInput(maKeyStops);
60 0 : }
61 :
62 0 : KeyStopLerp::ResultType KeyStopLerp::lerp(double fAlpha) const
63 : {
64 : // cached value still okay?
65 0 : if( maKeyStops.at(mnLastIndex) < fAlpha ||
66 0 : maKeyStops.at(mnLastIndex+1) >= fAlpha )
67 : {
68 : // nope, find new index
69 : mnLastIndex = std::min<std::ptrdiff_t>(
70 0 : maKeyStops.size()-2,
71 : // range is ensured by max below
72 : std::max<std::ptrdiff_t>(
73 : 0,
74 : std::distance( maKeyStops.begin(),
75 : std::lower_bound( maKeyStops.begin(),
76 : maKeyStops.end(),
77 0 : fAlpha )) - 1 ));
78 : }
79 :
80 : // lerp between stop and stop+1
81 : const double fRawLerp=
82 0 : (fAlpha-maKeyStops.at(mnLastIndex)) /
83 0 : (maKeyStops.at(mnLastIndex+1) - maKeyStops.at(mnLastIndex));
84 :
85 : // clamp to permissible range (input fAlpha might be
86 : // everything)
87 : return ResultType(
88 : mnLastIndex,
89 0 : clamp(fRawLerp,0.0,1.0));
90 : }
91 : }
92 : }
93 :
94 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|