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 "Splines.hxx"
21 : #include <rtl/math.hxx>
22 :
23 : #include <vector>
24 : #include <algorithm>
25 : #include <functional>
26 : #include <boost/scoped_array.hpp>
27 :
28 : #define MAX_BSPLINE_DEGREE 15
29 :
30 : namespace chart
31 : {
32 : using namespace ::com::sun::star;
33 :
34 : namespace
35 : {
36 :
37 : typedef ::std::pair< double, double > tPointType;
38 : typedef ::std::vector< tPointType > tPointVecType;
39 : typedef tPointVecType::size_type lcl_tSizeType;
40 :
41 0 : class lcl_SplineCalculation
42 : {
43 : public:
44 : /** @descr creates an object that calculates cublic splines on construction
45 :
46 : @param rSortedPoints the points for which splines shall be calculated, they need to be sorted in x values
47 : @param fY1FirstDerivation the resulting spline should have the first
48 : derivation equal to this value at the x-value of the first point
49 : of rSortedPoints. If fY1FirstDerivation is set to infinity, a natural
50 : spline is calculated.
51 : @param fYnFirstDerivation the resulting spline should have the first
52 : derivation equal to this value at the x-value of the last point
53 : of rSortedPoints
54 : */
55 : lcl_SplineCalculation( const tPointVecType & rSortedPoints,
56 : double fY1FirstDerivation,
57 : double fYnFirstDerivation );
58 :
59 : /** @descr creates an object that calculates cublic splines on construction
60 : for the special case of periodic cubic spline
61 :
62 : @param rSortedPoints the points for which splines shall be calculated,
63 : they need to be sorted in x values. First and last y value must be equal
64 : */
65 : lcl_SplineCalculation( const tPointVecType & rSortedPoints);
66 :
67 : /** @descr this function corresponds to the function splint in [1].
68 :
69 : [1] Numerical Recipies in C, 2nd edition
70 : William H. Press, et al.,
71 : Section 3.3, page 116
72 : */
73 : double GetInterpolatedValue( double x );
74 :
75 : private:
76 : /// a copy of the points given in the CTOR
77 : tPointVecType m_aPoints;
78 :
79 : /// the result of the Calculate() method
80 : ::std::vector< double > m_aSecDerivY;
81 :
82 : double m_fYp1;
83 : double m_fYpN;
84 :
85 : // these values are cached for performance reasons
86 : lcl_tSizeType m_nKLow;
87 : lcl_tSizeType m_nKHigh;
88 : double m_fLastInterpolatedValue;
89 :
90 : /** @descr this function corresponds to the function spline in [1].
91 :
92 : [1] Numerical Recipies in C, 2nd edition
93 : William H. Press, et al.,
94 : Section 3.3, page 115
95 : */
96 : void Calculate();
97 :
98 : /** @descr this function corresponds to the algorithm 4.76 in [2] and
99 : theorem 5.3.7 in [3]
100 :
101 : [2] Engeln-Müllges, Gisela: Numerik-Algorithmen: Verfahren, Beispiele, Anwendungen
102 : Springer, Berlin; Auflage: 9., überarb. und erw. A. (8. Dezember 2004)
103 : Section 4.10.2, page 175
104 :
105 : [3] Hanrath, Wilhelm: Mathematik III / Numerik, Vorlesungsskript zur
106 : Veranstaltung im WS 2007/2008
107 : Fachhochschule Aachen, 2009-09-19
108 : Numerik_01.pdf, downloaded 2011-04-19 via
109 : http://www.fh-aachen.de/index.php?id=11424&no_cache=1&file=5016&uid=44191
110 : Section 5.3, page 129
111 : */
112 : void CalculatePeriodic();
113 : };
114 :
115 0 : lcl_SplineCalculation::lcl_SplineCalculation(
116 : const tPointVecType & rSortedPoints,
117 : double fY1FirstDerivation,
118 : double fYnFirstDerivation )
119 : : m_aPoints( rSortedPoints ),
120 : m_fYp1( fY1FirstDerivation ),
121 : m_fYpN( fYnFirstDerivation ),
122 : m_nKLow( 0 ),
123 0 : m_nKHigh( rSortedPoints.size() - 1 ),
124 0 : m_fLastInterpolatedValue(0.0)
125 : {
126 0 : ::rtl::math::setInf( &m_fLastInterpolatedValue, false );
127 0 : Calculate();
128 0 : }
129 :
130 0 : lcl_SplineCalculation::lcl_SplineCalculation(
131 : const tPointVecType & rSortedPoints)
132 : : m_aPoints( rSortedPoints ),
133 : m_fYp1( 0.0 ), /*dummy*/
134 : m_fYpN( 0.0 ), /*dummy*/
135 : m_nKLow( 0 ),
136 0 : m_nKHigh( rSortedPoints.size() - 1 ),
137 0 : m_fLastInterpolatedValue(0.0)
138 : {
139 0 : ::rtl::math::setInf( &m_fLastInterpolatedValue, false );
140 0 : CalculatePeriodic();
141 0 : }
142 :
143 0 : void lcl_SplineCalculation::Calculate()
144 : {
145 0 : if( m_aPoints.size() <= 1 )
146 0 : return;
147 :
148 : // n is the last valid index to m_aPoints
149 0 : const lcl_tSizeType n = m_aPoints.size() - 1;
150 0 : ::std::vector< double > u( n );
151 0 : m_aSecDerivY.resize( n + 1, 0.0 );
152 :
153 0 : if( ::rtl::math::isInf( m_fYp1 ) )
154 : {
155 : // natural spline
156 0 : m_aSecDerivY[ 0 ] = 0.0;
157 0 : u[ 0 ] = 0.0;
158 : }
159 : else
160 : {
161 0 : m_aSecDerivY[ 0 ] = -0.5;
162 0 : double xDiff = ( m_aPoints[ 1 ].first - m_aPoints[ 0 ].first );
163 0 : u[ 0 ] = ( 3.0 / xDiff ) *
164 0 : ((( m_aPoints[ 1 ].second - m_aPoints[ 0 ].second ) / xDiff ) - m_fYp1 );
165 : }
166 :
167 0 : for( lcl_tSizeType i = 1; i < n; ++i )
168 : {
169 : tPointType
170 0 : p_i = m_aPoints[ i ],
171 0 : p_im1 = m_aPoints[ i - 1 ],
172 0 : p_ip1 = m_aPoints[ i + 1 ];
173 :
174 0 : double sig = ( p_i.first - p_im1.first ) /
175 0 : ( p_ip1.first - p_im1.first );
176 0 : double p = sig * m_aSecDerivY[ i - 1 ] + 2.0;
177 :
178 0 : m_aSecDerivY[ i ] = ( sig - 1.0 ) / p;
179 0 : u[ i ] =
180 0 : ( ( p_ip1.second - p_i.second ) /
181 0 : ( p_ip1.first - p_i.first ) ) -
182 0 : ( ( p_i.second - p_im1.second ) /
183 0 : ( p_i.first - p_im1.first ) );
184 0 : u[ i ] =
185 0 : ( 6.0 * u[ i ] / ( p_ip1.first - p_im1.first )
186 0 : - sig * u[ i - 1 ] ) / p;
187 : }
188 :
189 : // initialize to values for natural splines (used for m_fYpN equal to
190 : // infinity)
191 0 : double qn = 0.0;
192 0 : double un = 0.0;
193 :
194 0 : if( ! ::rtl::math::isInf( m_fYpN ) )
195 : {
196 0 : qn = 0.5;
197 0 : double xDiff = ( m_aPoints[ n ].first - m_aPoints[ n - 1 ].first );
198 0 : un = ( 3.0 / xDiff ) *
199 0 : ( m_fYpN - ( m_aPoints[ n ].second - m_aPoints[ n - 1 ].second ) / xDiff );
200 : }
201 :
202 0 : m_aSecDerivY[ n ] = ( un - qn * u[ n - 1 ] ) * ( qn * m_aSecDerivY[ n - 1 ] + 1.0 );
203 :
204 : // note: the algorithm in [1] iterates from n-1 to 0, but as size_type
205 : // may be (usuall is) an unsigned type, we can not write k >= 0, as this
206 : // is always true.
207 0 : for( lcl_tSizeType k = n; k > 0; --k )
208 : {
209 0 : ( m_aSecDerivY[ k - 1 ] *= m_aSecDerivY[ k ] ) += u[ k - 1 ];
210 0 : }
211 : }
212 :
213 0 : void lcl_SplineCalculation::CalculatePeriodic()
214 : {
215 0 : if( m_aPoints.size() <= 1 )
216 0 : return;
217 :
218 : // n is the last valid index to m_aPoints
219 0 : const lcl_tSizeType n = m_aPoints.size() - 1;
220 :
221 : // u is used for vector f in A*c=f in [3], vector a in Ax=a in [2],
222 : // vector z in Rtranspose z = a and Dr=z in [2]
223 0 : ::std::vector< double > u( n + 1, 0.0 );
224 :
225 : // used for vector c in A*c=f and vector x in Ax=a in [2]
226 0 : m_aSecDerivY.resize( n + 1, 0.0 );
227 :
228 : // diagonal of matrix A, used index 1 to n
229 0 : ::std::vector< double > Adiag( n + 1, 0.0 );
230 :
231 : // secondary diagonal of matrix A with index 1 to n-1 and upper right element in A[n]
232 0 : ::std::vector< double > Aupper( n + 1, 0.0 );
233 :
234 : // diagonal of matrix D in A=(R transpose)*D*R in [2], used index 1 to n
235 0 : ::std::vector< double > Ddiag( n+1, 0.0 );
236 :
237 : // right column of matrix R, used index 1 to n-2
238 0 : ::std::vector< double > Rright( n-1, 0.0 );
239 :
240 : // secondary diagonal of matrix R, used index 1 to n-1
241 0 : ::std::vector< double > Rupper( n, 0.0 );
242 :
243 0 : if (n<4)
244 : {
245 0 : if (n==3)
246 : { // special handling of three polynomials, that are four points
247 0 : double xDiff0 = m_aPoints[ 1 ].first - m_aPoints[ 0 ].first ;
248 0 : double xDiff1 = m_aPoints[ 2 ].first - m_aPoints[ 1 ].first ;
249 0 : double xDiff2 = m_aPoints[ 3 ].first - m_aPoints[ 2 ].first ;
250 0 : double xDiff2p1 = xDiff2 + xDiff1;
251 0 : double xDiff0p2 = xDiff0 + xDiff2;
252 0 : double xDiff1p0 = xDiff1 + xDiff0;
253 0 : double fFactor = 1.5 / (xDiff0*xDiff1 + xDiff1*xDiff2 + xDiff2*xDiff0);
254 0 : double yDiff0 = (m_aPoints[ 1 ].second - m_aPoints[ 0 ].second) / xDiff0;
255 0 : double yDiff1 = (m_aPoints[ 2 ].second - m_aPoints[ 1 ].second) / xDiff1;
256 0 : double yDiff2 = (m_aPoints[ 0 ].second - m_aPoints[ 2 ].second) / xDiff2;
257 0 : m_aSecDerivY[ 1 ] = fFactor * (yDiff1*xDiff2p1 - yDiff0*xDiff0p2);
258 0 : m_aSecDerivY[ 2 ] = fFactor * (yDiff2*xDiff0p2 - yDiff1*xDiff1p0);
259 0 : m_aSecDerivY[ 3 ] = fFactor * (yDiff0*xDiff1p0 - yDiff2*xDiff2p1);
260 0 : m_aSecDerivY[ 0 ] = m_aSecDerivY[ 3 ];
261 : }
262 0 : else if (n==2)
263 : {
264 : // special handling of two polynomials, that are three points
265 0 : double xDiff0 = m_aPoints[ 1 ].first - m_aPoints[ 0 ].first;
266 0 : double xDiff1 = m_aPoints[ 2 ].first - m_aPoints[ 1 ].first;
267 0 : double fHelp = 3.0 * (m_aPoints[ 0 ].second - m_aPoints[ 1 ].second) / (xDiff0*xDiff1);
268 0 : m_aSecDerivY[ 1 ] = fHelp ;
269 0 : m_aSecDerivY[ 2 ] = -fHelp ;
270 0 : m_aSecDerivY[ 0 ] = m_aSecDerivY[ 2 ] ;
271 : }
272 : else
273 : {
274 : // should be handled with natural spline, periodic not possible.
275 : }
276 : }
277 : else
278 : {
279 0 : double xDiff_i =1.0; // values are dummy;
280 0 : double xDiff_im1 =1.0;
281 0 : double yDiff_i = 1.0;
282 0 : double yDiff_im1 = 1.0;
283 : // fill matrix A and fill right side vector u
284 0 : for( lcl_tSizeType i=1; i<n; ++i )
285 : {
286 0 : xDiff_im1 = m_aPoints[ i ].first - m_aPoints[ i-1 ].first;
287 0 : xDiff_i = m_aPoints[ i+1 ].first - m_aPoints[ i ].first;
288 0 : yDiff_im1 = (m_aPoints[ i ].second - m_aPoints[ i-1 ].second) / xDiff_im1;
289 0 : yDiff_i = (m_aPoints[ i+1 ].second - m_aPoints[ i ].second) / xDiff_i;
290 0 : Adiag[ i ] = 2 * (xDiff_im1 + xDiff_i);
291 0 : Aupper[ i ] = xDiff_i;
292 0 : u [ i ] = 3 * (yDiff_i - yDiff_im1);
293 : }
294 0 : xDiff_im1 = m_aPoints[ n ].first - m_aPoints[ n-1 ].first;
295 0 : xDiff_i = m_aPoints[ 1 ].first - m_aPoints[ 0 ].first;
296 0 : yDiff_im1 = (m_aPoints[ n ].second - m_aPoints[ n-1 ].second) / xDiff_im1;
297 0 : yDiff_i = (m_aPoints[ 1 ].second - m_aPoints[ 0 ].second) / xDiff_i;
298 0 : Adiag[ n ] = 2 * (xDiff_im1 + xDiff_i);
299 0 : Aupper[ n ] = xDiff_i;
300 0 : u [ n ] = 3 * (yDiff_i - yDiff_im1);
301 :
302 : // decomposite A=(R transpose)*D*R
303 0 : Ddiag[1] = Adiag[1];
304 0 : Rupper[1] = Aupper[1] / Ddiag[1];
305 0 : Rright[1] = Aupper[n] / Ddiag[1];
306 0 : for( lcl_tSizeType i=2; i<=n-2; ++i )
307 : {
308 0 : Ddiag[i] = Adiag[i] - Aupper[ i-1 ] * Rupper[ i-1 ];
309 0 : Rupper[ i ] = Aupper[ i ] / Ddiag[ i ];
310 0 : Rright[ i ] = - Rright[ i-1 ] * Aupper[ i-1 ] / Ddiag[ i ];
311 : }
312 0 : Ddiag[ n-1 ] = Adiag[ n-1 ] - Aupper[ n-2 ] * Rupper[ n-2 ];
313 0 : Rupper[ n-1 ] = ( Aupper[ n-1 ] - Aupper[ n-2 ] * Rright[ n-2] ) / Ddiag[ n-1 ];
314 0 : double fSum = 0.0;
315 0 : for ( lcl_tSizeType i=1; i<=n-2; ++i )
316 : {
317 0 : fSum += Ddiag[ i ] * Rright[ i ] * Rright[ i ];
318 : }
319 0 : Ddiag[ n ] = Adiag[ n ] - fSum - Ddiag[ n-1 ] * Rupper[ n-1 ] * Rupper[ n-1 ]; // bug in [2]!
320 :
321 : // solve forward (R transpose)*z=u, overwrite u with z
322 0 : for ( lcl_tSizeType i=2; i<=n-1; ++i )
323 : {
324 0 : u[ i ] -= u[ i-1 ]* Rupper[ i-1 ];
325 : }
326 0 : fSum = 0.0;
327 0 : for ( lcl_tSizeType i=1; i<=n-2; ++i )
328 : {
329 0 : fSum += Rright[ i ] * u[ i ];
330 : }
331 0 : u[ n ] = u[ n ] - fSum - Rupper[ n - 1] * u[ n-1 ];
332 :
333 : // solve forward D*r=z, z is in u, overwrite u with r
334 0 : for ( lcl_tSizeType i=1; i<=n; ++i )
335 : {
336 0 : u[ i ] = u[i] / Ddiag[ i ];
337 : }
338 :
339 : // solve backward R*x= r, r is in u
340 0 : m_aSecDerivY[ n ] = u[ n ];
341 0 : m_aSecDerivY[ n-1 ] = u[ n-1 ] - Rupper[ n-1 ] * m_aSecDerivY[ n ];
342 0 : for ( lcl_tSizeType i=n-2; i>=1; --i)
343 : {
344 0 : m_aSecDerivY[ i ] = u[ i ] - Rupper[ i ] * m_aSecDerivY[ i+1 ] - Rright[ i ] * m_aSecDerivY[ n ];
345 : }
346 : // periodic
347 0 : m_aSecDerivY[ 0 ] = m_aSecDerivY[ n ];
348 : }
349 :
350 : // adapt m_aSecDerivY for usage in GetInterpolatedValue()
351 0 : for( lcl_tSizeType i = 0; i <= n ; ++i )
352 : {
353 0 : m_aSecDerivY[ i ] *= 2.0;
354 0 : }
355 :
356 : }
357 :
358 0 : double lcl_SplineCalculation::GetInterpolatedValue( double x )
359 : {
360 : OSL_PRECOND( ( m_aPoints[ 0 ].first <= x ) &&
361 : ( x <= m_aPoints[ m_aPoints.size() - 1 ].first ),
362 : "Trying to extrapolate" );
363 :
364 0 : const lcl_tSizeType n = m_aPoints.size() - 1;
365 0 : if( x < m_fLastInterpolatedValue )
366 : {
367 0 : m_nKLow = 0;
368 0 : m_nKHigh = n;
369 :
370 : // calculate m_nKLow and m_nKHigh
371 : // first initialization is done in CTOR
372 0 : while( m_nKHigh - m_nKLow > 1 )
373 : {
374 0 : lcl_tSizeType k = ( m_nKHigh + m_nKLow ) / 2;
375 0 : if( m_aPoints[ k ].first > x )
376 0 : m_nKHigh = k;
377 : else
378 0 : m_nKLow = k;
379 : }
380 : }
381 : else
382 : {
383 0 : while( ( m_aPoints[ m_nKHigh ].first < x ) &&
384 0 : ( m_nKHigh <= n ) )
385 : {
386 0 : ++m_nKHigh;
387 0 : ++m_nKLow;
388 : }
389 : OSL_ENSURE( m_nKHigh <= n, "Out of Bounds" );
390 : }
391 0 : m_fLastInterpolatedValue = x;
392 :
393 0 : double h = m_aPoints[ m_nKHigh ].first - m_aPoints[ m_nKLow ].first;
394 : OSL_ENSURE( h != 0, "Bad input to GetInterpolatedValue()" );
395 :
396 0 : double a = ( m_aPoints[ m_nKHigh ].first - x ) / h;
397 0 : double b = ( x - m_aPoints[ m_nKLow ].first ) / h;
398 :
399 0 : return ( a * m_aPoints[ m_nKLow ].second +
400 0 : b * m_aPoints[ m_nKHigh ].second +
401 0 : (( a*a*a - a ) * m_aSecDerivY[ m_nKLow ] +
402 0 : ( b*b*b - b ) * m_aSecDerivY[ m_nKHigh ] ) *
403 0 : ( h*h ) / 6.0 );
404 : }
405 :
406 : // helper methods for B-spline
407 :
408 : // Create parameter t_0 to t_n using the centripetal method with a power of 0.5
409 0 : bool createParameterT(const tPointVecType aUniquePoints, double* t)
410 : { // precondition: no adjacent identical points
411 : // postcondition: 0 = t_0 < t_1 < ... < t_n = 1
412 0 : bool bIsSuccessful = true;
413 0 : const lcl_tSizeType n = aUniquePoints.size() - 1;
414 0 : t[0]=0.0;
415 0 : double dx = 0.0;
416 0 : double dy = 0.0;
417 0 : double fDiffMax = 1.0; //dummy values
418 0 : double fDenominator = 0.0; // initialized for summing up
419 0 : for (lcl_tSizeType i=1; i<=n ; ++i)
420 : { // 4th root(dx^2+dy^2)
421 0 : dx = aUniquePoints[i].first - aUniquePoints[i-1].first;
422 0 : dy = aUniquePoints[i].second - aUniquePoints[i-1].second;
423 : // scaling to avoid underflow or overflow
424 0 : fDiffMax = (fabs(dx)>fabs(dy)) ? fabs(dx) : fabs(dy);
425 0 : if (fDiffMax == 0.0)
426 : {
427 0 : bIsSuccessful = false;
428 0 : break;
429 : }
430 : else
431 : {
432 0 : dx /= fDiffMax;
433 0 : dy /= fDiffMax;
434 0 : fDenominator += sqrt(sqrt(dx * dx + dy * dy)) * sqrt(fDiffMax);
435 : }
436 : }
437 0 : if (fDenominator == 0.0)
438 : {
439 0 : bIsSuccessful = false;
440 : }
441 0 : if (bIsSuccessful)
442 : {
443 0 : for (lcl_tSizeType j=1; j<=n ; ++j)
444 : {
445 0 : double fNumerator = 0.0;
446 0 : for (lcl_tSizeType i=1; i<=j ; ++i)
447 : {
448 0 : dx = aUniquePoints[i].first - aUniquePoints[i-1].first;
449 0 : dy = aUniquePoints[i].second - aUniquePoints[i-1].second;
450 0 : fDiffMax = (fabs(dx)>fabs(dy)) ? fabs(dx) : fabs(dy);
451 : // same as above, so should not be zero
452 0 : dx /= fDiffMax;
453 0 : dy /= fDiffMax;
454 0 : fNumerator += sqrt(sqrt(dx * dx + dy * dy)) * sqrt(fDiffMax);
455 : }
456 0 : t[j] = fNumerator / fDenominator;
457 :
458 : }
459 : // postcondition check
460 0 : t[n] = 1.0;
461 0 : double fPrevious = 0.0;
462 0 : for (lcl_tSizeType i=1; i <= n && bIsSuccessful ; ++i)
463 : {
464 0 : if (fPrevious >= t[i])
465 : {
466 0 : bIsSuccessful = false;
467 : }
468 : else
469 : {
470 0 : fPrevious = t[i];
471 : }
472 : }
473 : }
474 0 : return bIsSuccessful;
475 : }
476 :
477 0 : void createKnotVector(const lcl_tSizeType n, const sal_uInt32 p, double* t, double* u)
478 : { // precondition: 0 = t_0 < t_1 < ... < t_n = 1
479 0 : for (lcl_tSizeType j = 0; j <= p; ++j)
480 : {
481 0 : u[j] = 0.0;
482 : }
483 0 : double fSum = 0.0;
484 0 : for (lcl_tSizeType j = 1; j <= n-p; ++j )
485 : {
486 0 : fSum = 0.0;
487 0 : for (lcl_tSizeType i = j; i <= j+p-1; ++i)
488 : {
489 0 : fSum += t[i];
490 : }
491 : assert(p != 0);
492 0 : u[j+p] = fSum / p ;
493 : }
494 0 : for (lcl_tSizeType j = n+1; j <= n+1+p; ++j)
495 : {
496 0 : u[j] = 1.0;
497 : }
498 0 : }
499 :
500 0 : void applyNtoParameterT(const lcl_tSizeType i,const double tk,const sal_uInt32 p,const double* u, double* rowN)
501 : {
502 : // get N_p(t_k) recursively, only N_(i-p) till N_(i) are relevant, all other N_# are zero
503 0 : double fRightFactor = 0.0;
504 0 : double fLeftFactor = 0.0;
505 :
506 : // initialize with indicator function degree 0
507 0 : rowN[p] = 1.0; // all others are zero
508 :
509 : // calculate up to degree p
510 0 : for (sal_uInt32 s = 1; s <= p; ++s)
511 : {
512 : // first element
513 0 : fRightFactor = ( u[i+1] - tk ) / ( u[i+1]- u[i-s+1] );
514 : // i-s "true index" - (i-p)"shift" = p-s
515 0 : rowN[p-s] = fRightFactor * rowN[p-s+1];
516 :
517 : // middle elements
518 0 : for (sal_uInt32 j = s-1; j>=1 ; --j)
519 : {
520 0 : fLeftFactor = ( tk - u[i-j] ) / ( u[i-j+s] - u[i-j] ) ;
521 0 : fRightFactor = ( u[i-j+s+1] - tk ) / ( u[i-j+s+1] - u[i-j+1] );
522 : // i-j "true index" - (i-p)"shift" = p-j
523 0 : rowN[p-j] = fLeftFactor * rowN[p-j] + fRightFactor * rowN[p-j+1];
524 : }
525 :
526 : // last element
527 0 : fLeftFactor = ( tk - u[i] ) / ( u[i+s] - u[i] );
528 : // i "true index" - (i-p)"shift" = p
529 0 : rowN[p] = fLeftFactor * rowN[p];
530 : }
531 0 : }
532 :
533 : } // anonymous namespace
534 :
535 : // Calculates uniform parametric splines with subinterval length 1,
536 : // according ODF1.2 part 1, chapter 'chart interpolation'.
537 0 : void SplineCalculater::CalculateCubicSplines(
538 : const drawing::PolyPolygonShape3D& rInput
539 : , drawing::PolyPolygonShape3D& rResult
540 : , sal_uInt32 nGranularity )
541 : {
542 : OSL_PRECOND( nGranularity > 0, "Granularity is invalid" );
543 :
544 0 : rResult.SequenceX.realloc(0);
545 0 : rResult.SequenceY.realloc(0);
546 0 : rResult.SequenceZ.realloc(0);
547 :
548 0 : sal_uInt32 nOuterCount = rInput.SequenceX.getLength();
549 0 : if( !nOuterCount )
550 0 : return;
551 :
552 0 : rResult.SequenceX.realloc(nOuterCount);
553 0 : rResult.SequenceY.realloc(nOuterCount);
554 0 : rResult.SequenceZ.realloc(nOuterCount);
555 :
556 0 : for( sal_uInt32 nOuter = 0; nOuter < nOuterCount; ++nOuter )
557 : {
558 0 : if( rInput.SequenceX[nOuter].getLength() <= 1 )
559 0 : continue; //we need at least two points
560 :
561 0 : sal_uInt32 nMaxIndexPoints = rInput.SequenceX[nOuter].getLength()-1; // is >=1
562 0 : const double* pOldX = rInput.SequenceX[nOuter].getConstArray();
563 0 : const double* pOldY = rInput.SequenceY[nOuter].getConstArray();
564 0 : const double* pOldZ = rInput.SequenceZ[nOuter].getConstArray();
565 :
566 0 : ::std::vector < double > aParameter(nMaxIndexPoints+1);
567 0 : aParameter[0]=0.0;
568 0 : for( sal_uInt32 nIndex=1; nIndex<=nMaxIndexPoints; nIndex++ )
569 : {
570 0 : aParameter[nIndex]=aParameter[nIndex-1]+1;
571 : }
572 :
573 : // Split the calculation to X, Y and Z coordinate
574 0 : tPointVecType aInputX;
575 0 : aInputX.resize(nMaxIndexPoints+1);
576 0 : tPointVecType aInputY;
577 0 : aInputY.resize(nMaxIndexPoints+1);
578 0 : tPointVecType aInputZ;
579 0 : aInputZ.resize(nMaxIndexPoints+1);
580 0 : for (sal_uInt32 nN=0;nN<=nMaxIndexPoints; nN++ )
581 : {
582 0 : aInputX[ nN ].first=aParameter[nN];
583 0 : aInputX[ nN ].second=pOldX[ nN ];
584 0 : aInputY[ nN ].first=aParameter[nN];
585 0 : aInputY[ nN ].second=pOldY[ nN ];
586 0 : aInputZ[ nN ].first=aParameter[nN];
587 0 : aInputZ[ nN ].second=pOldZ[ nN ];
588 : }
589 :
590 : // generate a spline for each coordinate. It holds the complete
591 : // information to calculate each point of the curve
592 : double fXDerivation;
593 : double fYDerivation;
594 : lcl_SplineCalculation* aSplineX;
595 : lcl_SplineCalculation* aSplineY;
596 : // lcl_SplineCalculation* aSplineZ; the z-coordinates of all points in
597 : // a data series are equal. No spline calculation needed, but copy
598 : // coordinate to output
599 :
600 0 : if( pOldX[ 0 ] == pOldX[nMaxIndexPoints] &&
601 0 : pOldY[ 0 ] == pOldY[nMaxIndexPoints] &&
602 0 : pOldZ[ 0 ] == pOldZ[nMaxIndexPoints] &&
603 : nMaxIndexPoints >=2 )
604 : { // periodic spline
605 0 : aSplineX = new lcl_SplineCalculation( aInputX) ;
606 0 : aSplineY = new lcl_SplineCalculation( aInputY) ;
607 : // aSplineZ = new lcl_SplineCalculation( aInputZ) ;
608 : }
609 : else // generate the kind "natural spline"
610 : {
611 : double fInfty;
612 0 : ::rtl::math::setInf( &fInfty, false );
613 0 : fXDerivation = fInfty;
614 0 : fYDerivation = fInfty;
615 0 : aSplineX = new lcl_SplineCalculation( aInputX, fXDerivation, fXDerivation );
616 0 : aSplineY = new lcl_SplineCalculation( aInputY, fYDerivation, fYDerivation );
617 : }
618 :
619 : // fill result polygon with calculated values
620 0 : rResult.SequenceX[nOuter].realloc( nMaxIndexPoints*nGranularity + 1);
621 0 : rResult.SequenceY[nOuter].realloc( nMaxIndexPoints*nGranularity + 1);
622 0 : rResult.SequenceZ[nOuter].realloc( nMaxIndexPoints*nGranularity + 1);
623 :
624 0 : double* pNewX = rResult.SequenceX[nOuter].getArray();
625 0 : double* pNewY = rResult.SequenceY[nOuter].getArray();
626 0 : double* pNewZ = rResult.SequenceZ[nOuter].getArray();
627 :
628 0 : sal_uInt32 nNewPointIndex = 0; // Index in result points
629 : // needed for inner loop
630 : double fInc; // step for intermediate points
631 : sal_uInt32 nj; // for loop
632 : double fParam; // a intermediate parameter value
633 :
634 0 : for( sal_uInt32 ni = 0; ni < nMaxIndexPoints; ni++ )
635 : {
636 : // given point is surely a curve point
637 0 : pNewX[nNewPointIndex] = pOldX[ni];
638 0 : pNewY[nNewPointIndex] = pOldY[ni];
639 0 : pNewZ[nNewPointIndex] = pOldZ[ni];
640 0 : nNewPointIndex++;
641 :
642 : // calculate intermediate points
643 0 : fInc = ( aParameter[ ni+1 ] - aParameter[ni] ) / static_cast< double >( nGranularity );
644 0 : for(nj = 1; nj < nGranularity; nj++)
645 : {
646 0 : fParam = aParameter[ni] + ( fInc * static_cast< double >( nj ) );
647 :
648 0 : pNewX[nNewPointIndex]=aSplineX->GetInterpolatedValue( fParam );
649 0 : pNewY[nNewPointIndex]=aSplineY->GetInterpolatedValue( fParam );
650 : // pNewZ[nNewPointIndex]=aSplineZ->GetInterpolatedValue( fParam );
651 0 : pNewZ[nNewPointIndex] = pOldZ[ni];
652 0 : nNewPointIndex++;
653 : }
654 : }
655 : // add last point
656 0 : pNewX[nNewPointIndex] = pOldX[nMaxIndexPoints];
657 0 : pNewY[nNewPointIndex] = pOldY[nMaxIndexPoints];
658 0 : pNewZ[nNewPointIndex] = pOldZ[nMaxIndexPoints];
659 0 : delete aSplineX;
660 0 : delete aSplineY;
661 : // delete aSplineZ;
662 0 : }
663 : }
664 :
665 : // The implementation follows closely ODF1.2 spec, chapter chart:interpolation
666 : // using the same names as in spec as far as possible, without prefix.
667 : // More details can be found on
668 : // Dr. C.-K. Shene: CS3621 Introduction to Computing with Geometry Notes
669 : // Unit 9: Interpolation and Approximation/Curve Global Interpolation
670 : // Department of Computer Science, Michigan Technological University
671 : // http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/
672 : // [last called 2011-05-20]
673 0 : void SplineCalculater::CalculateBSplines(
674 : const ::com::sun::star::drawing::PolyPolygonShape3D& rInput
675 : , ::com::sun::star::drawing::PolyPolygonShape3D& rResult
676 : , sal_uInt32 nResolution
677 : , sal_uInt32 nDegree )
678 : {
679 : // nResolution is ODF1.2 file format attribut chart:spline-resolution and
680 : // ODF1.2 spec variable k. Causion, k is used as index in the spec in addition.
681 : // nDegree is ODF1.2 file format attribut chart:spline-order and
682 : // ODF1.2 spec variable p
683 : OSL_ASSERT( nResolution > 1 );
684 : OSL_ASSERT( nDegree >= 1 );
685 :
686 : // limit the b-spline degree to prevent insanely large sets of points
687 0 : sal_uInt32 p = std::min<sal_uInt32>(nDegree, MAX_BSPLINE_DEGREE);
688 :
689 0 : rResult.SequenceX.realloc(0);
690 0 : rResult.SequenceY.realloc(0);
691 0 : rResult.SequenceZ.realloc(0);
692 :
693 0 : sal_Int32 nOuterCount = rInput.SequenceX.getLength();
694 0 : if( !nOuterCount )
695 0 : return; // no input
696 :
697 0 : rResult.SequenceX.realloc(nOuterCount);
698 0 : rResult.SequenceY.realloc(nOuterCount);
699 0 : rResult.SequenceZ.realloc(nOuterCount);
700 :
701 0 : for( sal_Int32 nOuter = 0; nOuter < nOuterCount; ++nOuter )
702 : {
703 0 : if( rInput.SequenceX[nOuter].getLength() <= 1 )
704 0 : continue; // need at least 2 points, next piece of the series
705 :
706 : // Copy input to vector of points and remove adjacent double points. The
707 : // Z-coordinate is equal for all points in a series and holds the depth
708 : // in 3D mode, simple copying is enough.
709 0 : lcl_tSizeType nMaxIndexPoints = rInput.SequenceX[nOuter].getLength()-1; // is >=1
710 0 : const double* pOldX = rInput.SequenceX[nOuter].getConstArray();
711 0 : const double* pOldY = rInput.SequenceY[nOuter].getConstArray();
712 0 : const double* pOldZ = rInput.SequenceZ[nOuter].getConstArray();
713 0 : double fZCoordinate = pOldZ[0];
714 0 : tPointVecType aPointsIn;
715 0 : aPointsIn.resize(nMaxIndexPoints+1);
716 0 : for (lcl_tSizeType i = 0; i <= nMaxIndexPoints; ++i )
717 : {
718 0 : aPointsIn[ i ].first = pOldX[i];
719 0 : aPointsIn[ i ].second = pOldY[i];
720 : }
721 : aPointsIn.erase( ::std::unique( aPointsIn.begin(), aPointsIn.end()),
722 0 : aPointsIn.end() );
723 :
724 : // n is the last valid index to the reduced aPointsIn
725 : // There are n+1 valid data points.
726 0 : const lcl_tSizeType n = aPointsIn.size() - 1;
727 0 : if (n < 1 || p > n)
728 0 : continue; // need at least 2 points, degree p needs at least n+1 points
729 : // next piece of series
730 :
731 0 : boost::scoped_array<double> t(new double [n+1]);
732 0 : if (!createParameterT(aPointsIn, t.get()))
733 : {
734 0 : continue; // next piece of series
735 : }
736 :
737 0 : lcl_tSizeType m = n + p + 1;
738 0 : boost::scoped_array<double> u(new double [m+1]);
739 0 : createKnotVector(n, p, t.get(), u.get());
740 :
741 : // The matrix N contains the B-spline basis functions applied to parameters.
742 : // In each row only p+1 adjacent elements are non-zero. The starting
743 : // column in a higher row is equal or greater than in the lower row.
744 : // To store this matrix the non-zero elements are shifted to column 0
745 : // and the amount of shifting is remembered in an array.
746 0 : boost::scoped_array<double*> aMatN(new double*[n+1]);
747 0 : for (lcl_tSizeType row = 0; row <=n; ++row)
748 : {
749 0 : aMatN[row] = new double[p+1];
750 0 : for (sal_uInt32 col = 0; col <= p; ++col)
751 0 : aMatN[row][col] = 0.0;
752 : }
753 0 : boost::scoped_array<lcl_tSizeType> aShift(new lcl_tSizeType[n+1]);
754 0 : aMatN[0][0] = 1.0; //all others are zero
755 0 : aShift[0] = 0;
756 0 : aMatN[n][0] = 1.0;
757 0 : aShift[n] = n;
758 0 : for (lcl_tSizeType k = 1; k<=n-1; ++k)
759 : { // all basis functions are applied to t_k,
760 : // results are elements in row k in matrix N
761 :
762 : // find the one interval with u_i <= t_k < u_(i+1)
763 : // remember u_0 = ... = u_p = 0.0 and u_(m-p) = ... u_m = 1.0 and 0<t_k<1
764 0 : lcl_tSizeType i = p;
765 0 : while (!(u[i] <= t[k] && t[k] < u[i+1]))
766 : {
767 0 : ++i;
768 : }
769 :
770 : // index in reduced matrix aMatN = (index in full matrix N) - (i-p)
771 0 : aShift[k] = i - p;
772 :
773 0 : applyNtoParameterT(i, t[k], p, u.get(), aMatN[k]);
774 : } // next row k
775 :
776 : // Get matrix C of control points from the matrix equation aMatN * C = aPointsIn
777 : // aPointsIn is overwritten with C.
778 : // Gaussian elimination is possible without pivoting, see reference
779 0 : lcl_tSizeType r = 0; // true row index
780 0 : lcl_tSizeType c = 0; // true column index
781 0 : double fDivisor = 1.0; // used for diagonal element
782 0 : double fEliminate = 1.0; // used for the element, that will become zero
783 : double fHelp;
784 0 : tPointType aHelp;
785 : lcl_tSizeType nHelp; // used in triangle change
786 0 : bool bIsSuccessful = true;
787 0 : for (c = 0 ; c <= n && bIsSuccessful; ++c)
788 : {
789 : // search for first non-zero downwards
790 0 : r = c;
791 0 : while ( r < n && aMatN[r][c-aShift[r]] == 0 )
792 : {
793 0 : ++r;
794 : }
795 0 : if (aMatN[r][c-aShift[r]] == 0.0)
796 : {
797 : // Matrix N is singular, although this is mathematically impossible
798 0 : bIsSuccessful = false;
799 : }
800 : else
801 : {
802 : // exchange total row r with total row c if necessary
803 0 : if (r != c)
804 : {
805 0 : for ( sal_uInt32 i = 0; i <= p ; ++i)
806 : {
807 0 : fHelp = aMatN[r][i];
808 0 : aMatN[r][i] = aMatN[c][i];
809 0 : aMatN[c][i] = fHelp;
810 : }
811 0 : aHelp = aPointsIn[r];
812 0 : aPointsIn[r] = aPointsIn[c];
813 0 : aPointsIn[c] = aHelp;
814 0 : nHelp = aShift[r];
815 0 : aShift[r] = aShift[c];
816 0 : aShift[c] = nHelp;
817 : }
818 :
819 : // divide row c, so that element(c,c) becomes 1
820 0 : fDivisor = aMatN[c][c-aShift[c]]; // not zero, see above
821 0 : for (sal_uInt32 i = 0; i <= p; ++i)
822 : {
823 0 : aMatN[c][i] /= fDivisor;
824 : }
825 0 : aPointsIn[c].first /= fDivisor;
826 0 : aPointsIn[c].second /= fDivisor;
827 :
828 : // eliminate forward, examine row c+1 to n-1 (worst case)
829 : // stop if first non-zero element in row has an higher column as c
830 : // look at nShift for that, elements in nShift are equal or increasing
831 0 : for ( r = c+1; r < n && aShift[r]<=c ; ++r)
832 : {
833 0 : fEliminate = aMatN[r][0];
834 0 : if (fEliminate != 0.0) // else accidentally zero, nothing to do
835 : {
836 0 : for (sal_uInt32 i = 1; i <= p; ++i)
837 : {
838 0 : aMatN[r][i-1] = aMatN[r][i] - fEliminate * aMatN[c][i];
839 : }
840 0 : aMatN[r][p]=0;
841 0 : aPointsIn[r].first -= fEliminate * aPointsIn[c].first;
842 0 : aPointsIn[r].second -= fEliminate * aPointsIn[c].second;
843 0 : ++aShift[r];
844 : }
845 : }
846 : }
847 : }// upper triangle form is reached
848 0 : if( bIsSuccessful)
849 : {
850 : // eliminate backwards, begin with last column
851 0 : for (lcl_tSizeType cc = n; cc >= 1; --cc )
852 : {
853 : // In row cc the diagonal element(cc,cc) == 1 and all elements left from
854 : // diagonal are zero and do not influence other rows.
855 : // Full matrix N has semibandwidth < p, therefore element(r,c) is
856 : // zero, if abs(r-cc)>=p. abs(r-cc)=cc-r, because r<cc.
857 0 : r = cc - 1;
858 0 : while ( r !=0 && cc-r < p )
859 : {
860 0 : fEliminate = aMatN[r][ cc - aShift[r] ];
861 0 : if ( fEliminate != 0.0) // else element is accidentically zero, no action needed
862 : {
863 : // row r -= fEliminate * row cc only relevant for right side
864 0 : aMatN[r][cc - aShift[r]] = 0.0;
865 0 : aPointsIn[r].first -= fEliminate * aPointsIn[cc].first;
866 0 : aPointsIn[r].second -= fEliminate * aPointsIn[cc].second;
867 : }
868 0 : --r;
869 : }
870 : }
871 : } // aPointsIn contains the control points now.
872 0 : if (bIsSuccessful)
873 : {
874 : // calculate the intermediate points according given resolution
875 : // using deBoor-Cox algorithm
876 0 : lcl_tSizeType nNewSize = nResolution * n + 1;
877 0 : rResult.SequenceX[nOuter].realloc(nNewSize);
878 0 : rResult.SequenceY[nOuter].realloc(nNewSize);
879 0 : rResult.SequenceZ[nOuter].realloc(nNewSize);
880 0 : double* pNewX = rResult.SequenceX[nOuter].getArray();
881 0 : double* pNewY = rResult.SequenceY[nOuter].getArray();
882 0 : double* pNewZ = rResult.SequenceZ[nOuter].getArray();
883 0 : pNewX[0] = aPointsIn[0].first;
884 0 : pNewY[0] = aPointsIn[0].second;
885 0 : pNewZ[0] = fZCoordinate; // Precondition: z-coordinates of all points of a series are equal
886 0 : pNewX[nNewSize -1 ] = aPointsIn[n].first;
887 0 : pNewY[nNewSize -1 ] = aPointsIn[n].second;
888 0 : pNewZ[nNewSize -1 ] = fZCoordinate;
889 0 : boost::scoped_array<double> aP(new double[m+1]);
890 0 : lcl_tSizeType nLow = 0;
891 0 : for ( lcl_tSizeType nTIndex = 0; nTIndex <= n-1; ++nTIndex)
892 : {
893 0 : for (sal_uInt32 nResolutionStep = 1;
894 0 : nResolutionStep <= nResolution && !( nTIndex == n-1 && nResolutionStep == nResolution);
895 : ++nResolutionStep)
896 : {
897 0 : lcl_tSizeType nNewIndex = nTIndex * nResolution + nResolutionStep;
898 0 : double ux = t[nTIndex] + nResolutionStep * ( t[nTIndex+1] - t[nTIndex]) /nResolution;
899 :
900 : // get index nLow, so that u[nLow]<= ux < u[nLow +1]
901 : // continue from previous nLow
902 0 : while ( u[nLow] <= ux)
903 : {
904 0 : ++nLow;
905 : }
906 0 : --nLow;
907 :
908 : // x-coordinate
909 0 : for (lcl_tSizeType i = nLow-p; i <= nLow; ++i)
910 : {
911 0 : aP[i] = aPointsIn[i].first;
912 : }
913 0 : for (sal_uInt32 lcl_Degree = 1; lcl_Degree <= p; ++lcl_Degree)
914 : {
915 0 : double fFactor = 0.0;
916 0 : for (lcl_tSizeType i = nLow; i >= nLow + lcl_Degree - p; --i)
917 : {
918 0 : fFactor = ( ux - u[i] ) / ( u[i+p+1-lcl_Degree] - u[i]);
919 0 : aP[i] = (1 - fFactor)* aP[i-1] + fFactor * aP[i];
920 : }
921 : }
922 0 : pNewX[nNewIndex] = aP[nLow];
923 :
924 : // y-coordinate
925 0 : for (lcl_tSizeType i = nLow - p; i <= nLow; ++i)
926 : {
927 0 : aP[i] = aPointsIn[i].second;
928 : }
929 0 : for (sal_uInt32 lcl_Degree = 1; lcl_Degree <= p; ++lcl_Degree)
930 : {
931 0 : double fFactor = 0.0;
932 0 : for (lcl_tSizeType i = nLow; i >= nLow +lcl_Degree - p; --i)
933 : {
934 0 : fFactor = ( ux - u[i] ) / ( u[i+p+1-lcl_Degree] - u[i]);
935 0 : aP[i] = (1 - fFactor)* aP[i-1] + fFactor * aP[i];
936 : }
937 : }
938 0 : pNewY[nNewIndex] = aP[nLow];
939 0 : pNewZ[nNewIndex] = fZCoordinate;
940 : }
941 0 : }
942 : }
943 0 : for (lcl_tSizeType row = 0; row <=n; ++row)
944 : {
945 0 : delete[] aMatN[row];
946 : }
947 0 : } // next piece of the series
948 : }
949 :
950 : } //namespace chart
951 :
952 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|