Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / basegfx / source / tools / keystoplerp.cxx
blob0b0f230253f1bc08fe6770a0fd37abd4828728ca
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <basegfx/utils/keystoplerp.hxx>
21 #include <com/sun/star/uno/Sequence.hxx>
22 #include <osl/diagnose.h>
24 #include <algorithm>
26 static void validateInput(const std::vector<double>& rKeyStops)
28 #ifdef DBG_UTIL
29 OSL_ENSURE( rKeyStops.size() > 1,
30 "KeyStopLerp::KeyStopLerp(): key stop vector must have two entries or more" );
32 // rKeyStops must be sorted in ascending order
33 for( std::size_t i=1, len=rKeyStops.size(); i<len; ++i )
35 if( rKeyStops[i-1] > rKeyStops[i] )
36 OSL_FAIL( "KeyStopLerp::KeyStopLerp(): time vector is not sorted in ascending order!" );
38 #else
39 (void)rKeyStops;
40 #endif
43 namespace basegfx
45 namespace utils
47 KeyStopLerp::KeyStopLerp( const std::vector<double>& rKeyStops ) :
48 maKeyStops(rKeyStops),
49 mnLastIndex(0)
51 validateInput(maKeyStops);
54 KeyStopLerp::KeyStopLerp( const ::css::uno::Sequence<double>& rKeyStops ) :
55 maKeyStops(rKeyStops.getLength()),
56 mnLastIndex(0)
58 std::copy( rKeyStops.begin(), rKeyStops.end(), maKeyStops.begin() );
59 validateInput(maKeyStops);
62 KeyStopLerp::ResultType KeyStopLerp::lerp(double fAlpha) const
64 // cached value still okay?
65 if( maKeyStops.at(mnLastIndex) < fAlpha ||
66 maKeyStops.at(mnLastIndex+1) >= fAlpha )
68 // nope, find new index
69 mnLastIndex = std::min<std::ptrdiff_t>(
70 maKeyStops.size()-2,
71 // range is ensured by max below
72 std::max<std::ptrdiff_t>(
74 std::distance( maKeyStops.begin(),
75 std::lower_bound( maKeyStops.begin(),
76 maKeyStops.end(),
77 fAlpha )) - 1 ));
80 // lerp between stop and stop+1
81 const double fRawLerp=
82 (fAlpha-maKeyStops.at(mnLastIndex)) /
83 (maKeyStops.at(mnLastIndex+1) - maKeyStops.at(mnLastIndex));
85 // clamp to permissible range (input fAlpha might be
86 // everything)
87 return ResultType(
88 mnLastIndex,
89 clamp(fRawLerp,0.0,1.0));
94 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */