/src/libreoffice/basegfx/source/tools/keystoplerp.cxx
Line | Count | Source |
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/utils/keystoplerp.hxx> |
21 | | #include <com/sun/star/uno/Sequence.hxx> |
22 | | #include <osl/diagnose.h> |
23 | | |
24 | | #include <algorithm> |
25 | | |
26 | | static void validateInput(const std::vector<double>& rKeyStops) |
27 | 0 | { |
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 | | #else |
39 | 0 | (void)rKeyStops; |
40 | 0 | #endif |
41 | 0 | } |
42 | | |
43 | | namespace basegfx::utils |
44 | | { |
45 | | KeyStopLerp::KeyStopLerp( std::vector<double>&& rKeyStops ) : |
46 | 0 | maKeyStops(std::move(rKeyStops)), |
47 | 0 | mnLastIndex(0) |
48 | 0 | { |
49 | 0 | validateInput(maKeyStops); |
50 | 0 | } |
51 | | |
52 | | KeyStopLerp::KeyStopLerp( const ::css::uno::Sequence<double>& rKeyStops ) : |
53 | 0 | maKeyStops(rKeyStops.begin(), rKeyStops.end()), |
54 | 0 | mnLastIndex(0) |
55 | 0 | { |
56 | 0 | validateInput(maKeyStops); |
57 | 0 | } |
58 | | |
59 | | KeyStopLerp::ResultType KeyStopLerp::lerp(double fAlpha) const |
60 | 0 | { |
61 | | // cached value still okay? |
62 | 0 | if( maKeyStops.at(mnLastIndex) < fAlpha || |
63 | 0 | maKeyStops.at(mnLastIndex+1) >= fAlpha ) |
64 | 0 | { |
65 | | // nope, find new index |
66 | 0 | mnLastIndex = std::min<std::ptrdiff_t>( |
67 | 0 | maKeyStops.size()-2, |
68 | | // range is ensured by max below |
69 | 0 | std::max<std::ptrdiff_t>( |
70 | 0 | 0, |
71 | 0 | std::distance( maKeyStops.begin(), |
72 | 0 | std::lower_bound( maKeyStops.begin(), |
73 | 0 | maKeyStops.end(), |
74 | 0 | fAlpha )) - 1 )); |
75 | 0 | } |
76 | | |
77 | | // lerp between stop and stop+1 |
78 | 0 | const double fRawLerp= |
79 | 0 | (fAlpha-maKeyStops.at(mnLastIndex)) / |
80 | 0 | (maKeyStops.at(mnLastIndex+1) - maKeyStops.at(mnLastIndex)); |
81 | | |
82 | | // clamp to permissible range (input fAlpha might be |
83 | | // everything) |
84 | 0 | return ResultType( |
85 | 0 | mnLastIndex, |
86 | 0 | std::clamp(fRawLerp,0.0,1.0)); |
87 | 0 | } |
88 | | } |
89 | | |
90 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |