bump product version to 4.1.6.2
[LibreOffice.git] / chart2 / source / tools / CommonConverters.cxx
blobca401941da2bf84c94c79ddca41b20f2b8d89fb5
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 "CommonConverters.hxx"
21 #include <com/sun/star/drawing/DoubleSequence.hpp>
22 #include <com/sun/star/text/WritingMode2.hpp>
23 #include <com/sun/star/chart2/data/XNumericalDataSequence.hpp>
24 #include <com/sun/star/chart2/data/XTextualDataSequence.hpp>
25 #include <rtl/math.hxx>
26 #include <basegfx/matrix/b3dhommatrix.hxx>
28 #include <cstdarg>
31 //.............................................................................
32 namespace chart
34 //.............................................................................
36 using namespace ::com::sun::star;
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 // diverse methods for class conversions; e.g. ::basegfx::B3DHomMatrix to HomogenMatrix
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 drawing::HomogenMatrix B3DHomMatrixToHomogenMatrix( const ::basegfx::B3DHomMatrix& rM )
46 drawing::HomogenMatrix aHM;
47 aHM.Line1.Column1 = rM.get(0, 0);
48 aHM.Line1.Column2 = rM.get(0, 1);
49 aHM.Line1.Column3 = rM.get(0, 2);
50 aHM.Line1.Column4 = rM.get(0, 3);
51 aHM.Line2.Column1 = rM.get(1, 0);
52 aHM.Line2.Column2 = rM.get(1, 1);
53 aHM.Line2.Column3 = rM.get(1, 2);
54 aHM.Line2.Column4 = rM.get(1, 3);
55 aHM.Line3.Column1 = rM.get(2, 0);
56 aHM.Line3.Column2 = rM.get(2, 1);
57 aHM.Line3.Column3 = rM.get(2, 2);
58 aHM.Line3.Column4 = rM.get(2, 3);
59 aHM.Line4.Column1 = rM.get(3, 0);
60 aHM.Line4.Column2 = rM.get(3, 1);
61 aHM.Line4.Column3 = rM.get(3, 2);
62 aHM.Line4.Column4 = rM.get(3, 3);
63 return aHM;
66 ::basegfx::B3DHomMatrix HomogenMatrixToB3DHomMatrix( const drawing::HomogenMatrix& rHM )
68 ::basegfx::B3DHomMatrix aM;
69 aM.set(0, 0, rHM.Line1.Column1);
70 aM.set(0, 1, rHM.Line1.Column2);
71 aM.set(0, 2, rHM.Line1.Column3);
72 aM.set(0, 3, rHM.Line1.Column4);
73 aM.set(1, 0, rHM.Line2.Column1);
74 aM.set(1, 1, rHM.Line2.Column2);
75 aM.set(1, 2, rHM.Line2.Column3);
76 aM.set(1, 3, rHM.Line2.Column4);
77 aM.set(2, 0, rHM.Line3.Column1);
78 aM.set(2, 1, rHM.Line3.Column2);
79 aM.set(2, 2, rHM.Line3.Column3);
80 aM.set(2, 3, rHM.Line3.Column4);
81 aM.set(3, 0, rHM.Line4.Column1);
82 aM.set(3, 1, rHM.Line4.Column2);
83 aM.set(3, 2, rHM.Line4.Column3);
84 aM.set(3, 3, rHM.Line4.Column4);
85 return aM;
88 ::basegfx::B2DHomMatrix IgnoreZ( const ::basegfx::B3DHomMatrix& rM )
90 ::basegfx::B2DHomMatrix aM;
91 aM.set(0, 0, rM.get(0, 0));
92 aM.set(0, 1, rM.get(0, 1));
93 aM.set(0, 2, rM.get(0, 3));
94 aM.set(1, 0, rM.get(1, 0));
95 aM.set(1, 1, rM.get(1, 1));
96 aM.set(1, 2, rM.get(1, 3));
97 aM.set(2, 0, rM.get(3, 0));
98 aM.set(2, 1, rM.get(3, 1));
99 aM.set(2, 2, rM.get(3, 3));
100 return aM;
104 drawing::HomogenMatrix3 B2DHomMatrixToHomogenMatrix3( const ::basegfx::B2DHomMatrix& rM )
106 drawing::HomogenMatrix3 aHM;
107 aHM.Line1.Column1 = rM.get(0, 0);
108 aHM.Line1.Column2 = rM.get(0, 1);
109 aHM.Line1.Column3 = rM.get(0, 2);
110 aHM.Line2.Column1 = rM.get(1, 0);
111 aHM.Line2.Column2 = rM.get(1, 1);
112 aHM.Line2.Column3 = rM.get(1, 2);
113 aHM.Line3.Column1 = rM.get(2, 0);
114 aHM.Line3.Column2 = rM.get(2, 1);
115 aHM.Line3.Column3 = rM.get(2, 2);
116 return aHM;
119 ::basegfx::B3DPoint Position3DToB3DPoint( const drawing::Position3D& rPosition )
121 return ::basegfx::B3DPoint(
122 rPosition.PositionX ,
123 rPosition.PositionY ,
124 rPosition.PositionZ );
127 drawing::Direction3D B3DVectorToDirection3D( const ::basegfx::B3DVector& rVector)
129 return drawing::Direction3D(
130 rVector.getX()
131 , rVector.getY()
132 , rVector.getZ()
136 drawing::Position3D B3DPointToPosition3D( const ::basegfx::B3DPoint& rPoint)
138 return drawing::Position3D(
139 rPoint.getX()
140 , rPoint.getY()
141 , rPoint.getZ()
145 ::basegfx::B3DVector Direction3DToB3DVector( const drawing::Direction3D& rDirection)
147 return ::basegfx::B3DVector(
148 rDirection.DirectionX
149 , rDirection.DirectionY
150 , rDirection.DirectionZ
154 void AddPointToPoly( drawing::PolyPolygonShape3D& rPoly, const drawing::Position3D& rPos, sal_Int32 nPolygonIndex )
156 if(nPolygonIndex<0)
158 OSL_FAIL( "The polygon index needs to be > 0");
159 nPolygonIndex=0;
162 //make sure that we have enough polygons
163 if(nPolygonIndex >= rPoly.SequenceX.getLength() )
165 rPoly.SequenceX.realloc(nPolygonIndex+1);
166 rPoly.SequenceY.realloc(nPolygonIndex+1);
167 rPoly.SequenceZ.realloc(nPolygonIndex+1);
170 drawing::DoubleSequence* pOuterSequenceX = &rPoly.SequenceX.getArray()[nPolygonIndex];
171 drawing::DoubleSequence* pOuterSequenceY = &rPoly.SequenceY.getArray()[nPolygonIndex];
172 drawing::DoubleSequence* pOuterSequenceZ = &rPoly.SequenceZ.getArray()[nPolygonIndex];
174 sal_Int32 nOldPointCount = pOuterSequenceX->getLength();
176 pOuterSequenceX->realloc(nOldPointCount+1);
177 pOuterSequenceY->realloc(nOldPointCount+1);
178 pOuterSequenceZ->realloc(nOldPointCount+1);
180 double* pInnerSequenceX = pOuterSequenceX->getArray();
181 double* pInnerSequenceY = pOuterSequenceY->getArray();
182 double* pInnerSequenceZ = pOuterSequenceZ->getArray();
184 pInnerSequenceX[nOldPointCount] = rPos.PositionX;
185 pInnerSequenceY[nOldPointCount] = rPos.PositionY;
186 pInnerSequenceZ[nOldPointCount] = rPos.PositionZ;
189 drawing::Position3D getPointFromPoly( const drawing::PolyPolygonShape3D& rPolygon, sal_Int32 nPointIndex, sal_Int32 nPolyIndex )
191 drawing::Position3D aRet(0.0,0.0,0.0);
193 if( nPolyIndex>=0 && nPolyIndex<rPolygon.SequenceX.getLength())
195 if(nPointIndex<rPolygon.SequenceX[nPolyIndex].getLength())
197 aRet.PositionX = rPolygon.SequenceX[nPolyIndex][nPointIndex];
198 aRet.PositionY = rPolygon.SequenceY[nPolyIndex][nPointIndex];
199 aRet.PositionZ = rPolygon.SequenceZ[nPolyIndex][nPointIndex];
201 else
203 OSL_FAIL("polygon was accessed with a wrong index");
206 else
208 OSL_FAIL("polygon was accessed with a wrong index");
210 return aRet;
213 void addPolygon( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
215 sal_Int32 nAddOuterCount = rAdd.SequenceX.getLength();
216 sal_Int32 nOuterCount = rRet.SequenceX.getLength() + nAddOuterCount;
217 rRet.SequenceX.realloc( nOuterCount );
218 rRet.SequenceY.realloc( nOuterCount );
219 rRet.SequenceZ.realloc( nOuterCount );
221 sal_Int32 nIndex = 0;
222 sal_Int32 nOuter = nOuterCount - nAddOuterCount;
223 for( ; nOuter < nOuterCount; nOuter++ )
225 if( nIndex >= nAddOuterCount )
226 break;
228 rRet.SequenceX[nOuter] = rAdd.SequenceX[nIndex];
229 rRet.SequenceY[nOuter] = rAdd.SequenceY[nIndex];
230 rRet.SequenceZ[nOuter] = rAdd.SequenceZ[nIndex];
232 nIndex++;
236 void appendPoly( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
238 sal_Int32 nOuterCount = std::max( rRet.SequenceX.getLength(), rAdd.SequenceX.getLength() );
239 rRet.SequenceX.realloc(nOuterCount);
240 rRet.SequenceY.realloc(nOuterCount);
241 rRet.SequenceZ.realloc(nOuterCount);
243 for( sal_Int32 nOuter=0;nOuter<nOuterCount;nOuter++ )
245 sal_Int32 nOldPointCount = rRet.SequenceX[nOuter].getLength();
246 sal_Int32 nAddPointCount = 0;
247 if(nOuter<rAdd.SequenceX.getLength())
248 nAddPointCount = rAdd.SequenceX[nOuter].getLength();
249 if(!nAddPointCount)
250 continue;
252 sal_Int32 nNewPointCount = nOldPointCount + nAddPointCount;
254 rRet.SequenceX[nOuter].realloc(nNewPointCount);
255 rRet.SequenceY[nOuter].realloc(nNewPointCount);
256 rRet.SequenceZ[nOuter].realloc(nNewPointCount);
258 sal_Int32 nPointTarget=nOldPointCount;
259 sal_Int32 nPointSource=nAddPointCount;
260 for( ; nPointSource-- ; nPointTarget++ )
262 rRet.SequenceX[nOuter][nPointTarget] = rAdd.SequenceX[nOuter][nPointSource];
263 rRet.SequenceY[nOuter][nPointTarget] = rAdd.SequenceY[nOuter][nPointSource];
264 rRet.SequenceZ[nOuter][nPointTarget] = rAdd.SequenceZ[nOuter][nPointSource];
269 drawing::PolyPolygonShape3D BezierToPoly(
270 const drawing::PolyPolygonBezierCoords& rBezier )
272 const drawing::PointSequenceSequence& rPointSequence = rBezier.Coordinates;
274 drawing::PolyPolygonShape3D aRet;
275 aRet.SequenceX.realloc( rPointSequence.getLength() );
276 aRet.SequenceY.realloc( rPointSequence.getLength() );
277 aRet.SequenceZ.realloc( rPointSequence.getLength() );
279 sal_Int32 nRealOuter = 0;
280 for(sal_Int32 nN = 0; nN < rPointSequence.getLength(); nN++)
282 sal_Int32 nInnerLength = rPointSequence[nN].getLength();
283 aRet.SequenceX[nN].realloc( nInnerLength );
284 aRet.SequenceY[nN].realloc( nInnerLength );
285 aRet.SequenceZ[nN].realloc( nInnerLength );
287 bool bHasOuterFlags = nN < rBezier.Flags.getLength();
289 sal_Int32 nRealInner = 0;
290 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
292 bool bHasInnerFlags = bHasOuterFlags && (nM < rBezier.Flags[nN].getLength());
294 if( !bHasInnerFlags || (rBezier.Flags[nN][nM] == drawing::PolygonFlags_NORMAL) )
296 aRet.SequenceX[nRealOuter][nRealInner] = rPointSequence[nN][nM].X;
297 aRet.SequenceY[nRealOuter][nRealInner] = rPointSequence[nN][nM].Y;
298 aRet.SequenceZ[nRealOuter][nRealInner] = 0.0;
299 nRealInner++;
303 aRet.SequenceX[nRealOuter].realloc( nRealInner );
304 aRet.SequenceY[nRealOuter].realloc( nRealInner );
305 aRet.SequenceZ[nRealOuter].realloc( nRealInner );
307 if( nRealInner>0 )
308 nRealOuter++;
311 aRet.SequenceX.realloc( nRealOuter );
312 aRet.SequenceY.realloc( nRealOuter );
313 aRet.SequenceZ.realloc( nRealOuter );
315 return aRet;
318 drawing::PointSequenceSequence PolyToPointSequence(
319 const drawing::PolyPolygonShape3D& rPolyPolygon )
321 drawing::PointSequenceSequence aRet;
322 aRet.realloc( rPolyPolygon.SequenceX.getLength() );
324 for(sal_Int32 nN = 0; nN < rPolyPolygon.SequenceX.getLength(); nN++)
326 sal_Int32 nInnerLength = rPolyPolygon.SequenceX[nN].getLength();
327 aRet[nN].realloc( nInnerLength );
328 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
330 aRet[nN][nM].X = static_cast<sal_Int32>(rPolyPolygon.SequenceX[nN][nM]);
331 aRet[nN][nM].Y = static_cast<sal_Int32>(rPolyPolygon.SequenceY[nN][nM]);
334 return aRet;
337 void appendPointSequence( drawing::PointSequenceSequence& rTarget
338 , drawing::PointSequenceSequence& rAdd )
340 sal_Int32 nAddCount = rAdd.getLength();
341 if(!nAddCount)
342 return;
343 sal_Int32 nOldCount = rTarget.getLength();
345 rTarget.realloc(nOldCount+nAddCount);
346 for(sal_Int32 nS=0; nS<nAddCount; nS++ )
347 rTarget[nOldCount+nS]=rAdd[nS];
350 drawing::Position3D operator+( const drawing::Position3D& rPos
351 , const drawing::Direction3D& rDirection)
353 return drawing::Position3D(
354 rPos.PositionX + rDirection.DirectionX
355 , rPos.PositionY + rDirection.DirectionY
356 , rPos.PositionZ + rDirection.DirectionZ
360 drawing::Direction3D operator-( const drawing::Position3D& rPos1
361 , const drawing::Position3D& rPos2)
363 return drawing::Direction3D(
364 rPos1.PositionX - rPos2.PositionX
365 , rPos1.PositionY - rPos2.PositionY
366 , rPos1.PositionZ - rPos2.PositionZ
370 bool operator==( const drawing::Position3D& rPos1
371 , const drawing::Position3D& rPos2)
373 return rPos1.PositionX == rPos2.PositionX
374 && rPos1.PositionY == rPos2.PositionY
375 && rPos1.PositionZ == rPos2.PositionZ;
378 awt::Point Position3DToAWTPoint( const drawing::Position3D& rPos )
380 awt::Point aRet;
381 aRet.X = static_cast<sal_Int32>(rPos.PositionX);
382 aRet.Y = static_cast<sal_Int32>(rPos.PositionY);
383 return aRet;
386 awt::Point ToPoint( const awt::Rectangle& rRectangle )
388 return awt::Point( rRectangle.X, rRectangle.Y );
391 awt::Size ToSize( const awt::Rectangle& rRectangle )
393 return awt::Size( rRectangle.Width, rRectangle.Height );
396 awt::Size Direction3DToAWTSize( const drawing::Direction3D& rDirection )
398 awt::Size aRet;
399 aRet.Width = static_cast<sal_Int32>(rDirection.DirectionX);
400 aRet.Height = static_cast<sal_Int32>(rDirection.DirectionY);
401 return aRet;
404 uno::Sequence< double > B3DPointToSequence( const ::basegfx::B3DPoint& rPoint )
406 uno::Sequence< double > aRet(3);
407 aRet[0] = rPoint.getX();
408 aRet[1] = rPoint.getY();
409 aRet[2] = rPoint.getZ();
410 return aRet;
413 drawing::Position3D SequenceToPosition3D( const uno::Sequence< double >& rSeq )
415 OSL_ENSURE(rSeq.getLength()==3,"The sequence needs to have length 3 for conversion into vector");
417 drawing::Position3D aRet;
418 aRet.PositionX = rSeq.getLength()>0?rSeq[0]:0.0;
419 aRet.PositionY = rSeq.getLength()>1?rSeq[1]:0.0;
420 aRet.PositionZ = rSeq.getLength()>2?rSeq[2]:0.0;
421 return aRet;
424 uno::Sequence< double > Position3DToSequence( const drawing::Position3D& rPosition )
426 uno::Sequence< double > aRet(3);
427 aRet[0] = rPosition.PositionX;
428 aRet[1] = rPosition.PositionY;
429 aRet[2] = rPosition.PositionZ;
430 return aRet;
433 using namespace ::com::sun::star::chart2;
435 uno::Sequence< double > DataSequenceToDoubleSequence(
436 const uno::Reference< data::XDataSequence >& xDataSequence )
438 uno::Sequence< double > aResult;
439 OSL_ASSERT( xDataSequence.is());
440 if(!xDataSequence.is())
441 return aResult;
443 uno::Reference< data::XNumericalDataSequence > xNumericalDataSequence( xDataSequence, uno::UNO_QUERY );
444 if( xNumericalDataSequence.is() )
446 aResult = xNumericalDataSequence->getNumericalData();
448 else
450 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
451 aResult.realloc(aValues.getLength());
452 for(sal_Int32 nN=aValues.getLength();nN--;)
454 if( !(aValues[nN] >>= aResult[nN]) )
455 ::rtl::math::setNan( &aResult[nN] );
459 return aResult;
462 uno::Sequence< OUString > DataSequenceToStringSequence(
463 const uno::Reference< data::XDataSequence >& xDataSequence )
465 uno::Sequence< OUString > aResult;
466 if(!xDataSequence.is())
467 return aResult;
469 uno::Reference< data::XTextualDataSequence > xTextualDataSequence( xDataSequence, uno::UNO_QUERY );
470 if( xTextualDataSequence.is() )
472 aResult = xTextualDataSequence->getTextualData();
474 else
476 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
477 aResult.realloc(aValues.getLength());
479 for(sal_Int32 nN=aValues.getLength();nN--;)
480 aValues[nN] >>= aResult[nN];
483 return aResult;
486 sal_Bool hasDoubleValue( const uno::Any& rAny )
488 sal_Bool bRet = sal_False;
489 double fValue = 0.0;
490 if( rAny >>= fValue )
491 bRet = sal_True;
492 return bRet;
495 sal_Bool hasLongOrShortValue( const uno::Any& rAny )
497 sal_Bool bRet = sal_False;
498 sal_Int32 n32 = 0;
499 if( rAny >>= n32 )
500 bRet = sal_True;
501 else
503 sal_Int16 n16 = 0;
504 if( rAny >>= n16 )
505 bRet = sal_True;
507 return bRet;
509 sal_Int16 getShortForLongAlso( const uno::Any& rAny )
511 sal_Int16 nRet = 0;
513 if( !(rAny >>= nRet) )
515 sal_Int32 n32 = 0;
516 if( rAny >>= n32 )
517 nRet = static_cast<sal_Int16>(n32);
519 return nRet;
522 bool replaceParamterInString( OUString & rInOutResourceString,
523 const OUString & rParamToReplace,
524 const OUString & rReplaceWith )
526 sal_Int32 nPos = rInOutResourceString.indexOf( rParamToReplace );
527 if( nPos == -1 )
528 return false;
530 rInOutResourceString = rInOutResourceString.replaceAt( nPos
531 , rParamToReplace.getLength(), rReplaceWith );
532 return true;
535 //.............................................................................
536 } //namespace chart
537 //.............................................................................
539 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */