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 : #include <osl/diagnose.h>
23 :
24 : #include <algorithm>
25 :
26 1 : static void validateInput(const std::vector<double>& rKeyStops)
27 : {
28 : (void)rKeyStops;
29 : #ifdef DBG_UTIL
30 : OSL_ENSURE( rKeyStops.size() > 1,
31 : "KeyStopLerp::KeyStopLerp(): key stop vector must have two entries or more" );
32 :
33 : // rKeyStops must be sorted in ascending order
34 : for( ::std::size_t i=1, len=rKeyStops.size(); i<len; ++i )
35 : {
36 : if( rKeyStops[i-1] > rKeyStops[i] )
37 : OSL_FAIL( "KeyStopLerp::KeyStopLerp(): time vector is not sorted in ascending order!" );
38 : }
39 : #endif
40 1 : }
41 :
42 : namespace basegfx
43 : {
44 : namespace tools
45 : {
46 1 : KeyStopLerp::KeyStopLerp( const std::vector<double>& rKeyStops ) :
47 : maKeyStops(rKeyStops),
48 1 : mnLastIndex(0)
49 : {
50 1 : validateInput(maKeyStops);
51 1 : }
52 :
53 0 : KeyStopLerp::KeyStopLerp( const ::com::sun::star::uno::Sequence<double>& rKeyStops ) :
54 0 : maKeyStops(rKeyStops.getLength()),
55 0 : mnLastIndex(0)
56 : {
57 0 : std::copy( rKeyStops.begin(), rKeyStops.end(), maKeyStops.begin() );
58 0 : validateInput(maKeyStops);
59 0 : }
60 :
61 7 : KeyStopLerp::ResultType KeyStopLerp::lerp(double fAlpha) const
62 : {
63 : // cached value still okay?
64 9 : if( maKeyStops.at(mnLastIndex) < fAlpha ||
65 2 : maKeyStops.at(mnLastIndex+1) >= fAlpha )
66 : {
67 : // nope, find new index
68 : mnLastIndex = std::min<std::ptrdiff_t>(
69 7 : maKeyStops.size()-2,
70 : // range is ensured by max below
71 : std::max<std::ptrdiff_t>(
72 : 0,
73 : std::distance( maKeyStops.begin(),
74 : std::lower_bound( maKeyStops.begin(),
75 : maKeyStops.end(),
76 14 : fAlpha )) - 1 ));
77 : }
78 :
79 : // lerp between stop and stop+1
80 : const double fRawLerp=
81 14 : (fAlpha-maKeyStops.at(mnLastIndex)) /
82 14 : (maKeyStops.at(mnLastIndex+1) - maKeyStops.at(mnLastIndex));
83 :
84 : // clamp to permissible range (input fAlpha might be
85 : // everything)
86 : return ResultType(
87 : mnLastIndex,
88 7 : clamp(fRawLerp,0.0,1.0));
89 : }
90 : }
91 : }
92 :
93 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|