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 "controller/SlsAnimationFunction.hxx"
21 : #include "model/SlsPageDescriptor.hxx"
22 : #include "view/SlideSorterView.hxx"
23 :
24 : #include <osl/diagnose.hxx>
25 : #include <rtl/math.hxx>
26 :
27 : namespace sd { namespace slidesorter { namespace controller {
28 :
29 : //===== AnimationBezierFunction ===============================================
30 :
31 1 : AnimationBezierFunction::AnimationBezierFunction (
32 : const double nX1,
33 : const double nY1)
34 : : mnX1(nX1),
35 : mnY1(nY1),
36 1 : mnX2(1-nY1),
37 2 : mnY2(1-nX1)
38 : {
39 1 : }
40 :
41 64 : ::basegfx::B2DPoint AnimationBezierFunction::operator() (const double nT)
42 : {
43 : return ::basegfx::B2DPoint(
44 : EvaluateComponent(nT, mnX1, mnX2),
45 64 : EvaluateComponent(nT, mnY1, mnY2));
46 : }
47 :
48 128 : double AnimationBezierFunction::EvaluateComponent (
49 : const double nT,
50 : const double nV1,
51 : const double nV2)
52 : {
53 128 : const double nS (1-nT);
54 :
55 : // While the control point values 1 and 2 are explicitly given the start
56 : // and end values are implicitly given.
57 128 : const double nV0 (0);
58 128 : const double nV3 (1);
59 :
60 128 : const double nV01 (nS*nV0 + nT*nV1);
61 128 : const double nV12 (nS*nV1 + nT*nV2);
62 128 : const double nV23 (nS*nV2 + nT*nV3);
63 :
64 128 : const double nV012 (nS*nV01 + nT*nV12);
65 128 : const double nV123 (nS*nV12 + nT*nV23);
66 :
67 128 : const double nV0123 (nS*nV012 + nT*nV123);
68 :
69 128 : return nV0123;
70 : }
71 :
72 : //===== AnimationParametricFunction ===========================================
73 :
74 1 : AnimationParametricFunction::AnimationParametricFunction (const ParametricFunction& rFunction)
75 1 : : maY()
76 : {
77 1 : const sal_Int32 nSampleCount (64);
78 :
79 : // Sample the given parametric function.
80 1 : ::std::vector<basegfx::B2DPoint> aPoints;
81 1 : aPoints.reserve(nSampleCount);
82 65 : for (sal_Int32 nIndex=0; nIndex<nSampleCount; ++nIndex)
83 : {
84 64 : const double nT (nIndex/double(nSampleCount-1));
85 64 : aPoints.push_back(basegfx::B2DPoint(rFunction(nT)));
86 : }
87 :
88 : // Interpolate at evenly spaced points.
89 1 : maY.clear();
90 1 : maY.reserve(nSampleCount);
91 1 : double nX0 (aPoints[0].getX());
92 1 : double nY0 (aPoints[0].getY());
93 1 : double nX1 (aPoints[1].getX());
94 1 : double nY1 (aPoints[1].getY());
95 1 : sal_Int32 nIndex (1);
96 65 : for (sal_Int32 nIndex2=0; nIndex2<nSampleCount; ++nIndex2)
97 : {
98 64 : const double nX (nIndex2 / double(nSampleCount-1));
99 191 : while (nX > nX1 && nIndex<nSampleCount)
100 : {
101 63 : nX0 = nX1;
102 63 : nY0 = nY1;
103 63 : nX1 = aPoints[nIndex].getX();
104 63 : nY1 = aPoints[nIndex].getY();
105 63 : ++nIndex;
106 : }
107 64 : const double nU ((nX-nX1) / (nX0 - nX1));
108 64 : const double nY (nY0*nU + nY1*(1-nU));
109 64 : maY.push_back(nY);
110 1 : }
111 1 : }
112 :
113 1 : double AnimationParametricFunction::operator() (const double nX)
114 : {
115 1 : const sal_Int32 nIndex0 (static_cast<sal_Int32>(nX * maY.size()));
116 1 : const double nX0 (nIndex0 / double(maY.size()-1));
117 1 : const sal_uInt32 nIndex1 (nIndex0 + 1);
118 1 : const double nX1 (nIndex1 / double(maY.size()-1));
119 :
120 1 : if (nIndex0<=0)
121 0 : return maY[0];
122 1 : else if (sal_uInt32(nIndex0)>=maY.size() || nIndex1>=maY.size())
123 1 : return maY[maY.size()-1];
124 :
125 0 : const double nU ((nX-nX1) / (nX0 - nX1));
126 0 : return maY[nIndex0]*nU + maY[nIndex1]*(1-nU);
127 : }
128 :
129 66 : } } } // end of namespace ::sd::slidesorter::controller
130 :
131 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|