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