tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sd / source / ui / slidesorter / controller / SlsAnimationFunction.cxx
blob31978baf71894683f97ed848ee468e3b2179fdb3
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 <sal/config.h>
22 #include <o3tl/safeint.hxx>
24 #include <controller/SlsAnimationFunction.hxx>
26 namespace sd::slidesorter::controller {
28 //===== AnimationBezierFunction ===============================================
30 AnimationBezierFunction::AnimationBezierFunction (
31 const double nX1,
32 const double nY1)
33 : mnX1(nX1),
34 mnY1(nY1),
35 mnX2(1-nY1),
36 mnY2(1-nX1)
40 ::basegfx::B2DPoint AnimationBezierFunction::operator() (const double nT)
42 return ::basegfx::B2DPoint(
43 EvaluateComponent(nT, mnX1, mnX2),
44 EvaluateComponent(nT, mnY1, mnY2));
47 double AnimationBezierFunction::EvaluateComponent (
48 const double nT,
49 const double nV1,
50 const double nV2)
52 const double nS (1-nT);
54 // While the control point values 1 and 2 are explicitly given the start
55 // and end values are implicitly given.
56 const double nV0 (0);
57 const double nV3 (1);
59 const double nV01 (nS*nV0 + nT*nV1);
60 const double nV12 (nS*nV1 + nT*nV2);
61 const double nV23 (nS*nV2 + nT*nV3);
63 const double nV012 (nS*nV01 + nT*nV12);
64 const double nV123 (nS*nV12 + nT*nV23);
66 const double nV0123 (nS*nV012 + nT*nV123);
68 return nV0123;
71 //===== AnimationParametricFunction ===========================================
73 AnimationParametricFunction::AnimationParametricFunction (const ParametricFunction& rFunction)
75 const sal_Int32 nSampleCount (64);
77 // Sample the given parametric function.
78 ::std::vector<basegfx::B2DPoint> aPoints;
79 aPoints.reserve(nSampleCount);
80 for (sal_Int32 nIndex=0; nIndex<nSampleCount; ++nIndex)
82 const double nT (nIndex/double(nSampleCount-1));
83 aPoints.emplace_back(rFunction(nT));
86 // Interpolate at evenly spaced points.
87 maY.clear();
88 maY.reserve(nSampleCount);
89 double nX0 (aPoints[0].getX());
90 double nY0 (aPoints[0].getY());
91 double nX1 (aPoints[1].getX());
92 double nY1 (aPoints[1].getY());
93 sal_Int32 nIndex (1);
94 for (sal_Int32 nIndex2=0; nIndex2<nSampleCount; ++nIndex2)
96 const double nX (nIndex2 / double(nSampleCount-1));
97 while (nX > nX1 && nIndex<nSampleCount)
99 nX0 = nX1;
100 nY0 = nY1;
101 nX1 = aPoints[nIndex].getX();
102 nY1 = aPoints[nIndex].getY();
103 ++nIndex;
105 const double nU ((nX-nX1) / (nX0 - nX1));
106 const double nY (nY0*nU + nY1*(1-nU));
107 maY.push_back(nY);
111 double AnimationParametricFunction::operator() (const double nX)
113 const sal_Int32 nIndex0 (static_cast<sal_Int32>(nX * maY.size()));
114 const double nX0 (nIndex0 / double(maY.size()-1));
115 const sal_uInt32 nIndex1 (nIndex0 + 1);
116 const double nX1 (nIndex1 / double(maY.size()-1));
118 if (nIndex0<=0)
119 return maY[0];
120 else if (o3tl::make_unsigned(nIndex0)>=maY.size() || nIndex1>=maY.size())
121 return maY[maY.size()-1];
123 const double nU ((nX-nX1) / (nX0 - nX1));
124 return maY[nIndex0]*nU + maY[nIndex1]*(1-nU);
127 } // end of namespace ::sd::slidesorter::controller
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */