Bump for 3.6-28
[LibreOffice.git] / chart2 / source / tools / CommonConverters.cxx
blob36aa57cf3966d22b314c24376de1ec41cb2378fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "CommonConverters.hxx"
30 #include <com/sun/star/drawing/DoubleSequence.hpp>
31 #include <com/sun/star/text/WritingMode2.hpp>
32 #include <com/sun/star/chart2/data/XNumericalDataSequence.hpp>
33 #include <com/sun/star/chart2/data/XTextualDataSequence.hpp>
34 #include <rtl/math.hxx>
35 #include <basegfx/matrix/b3dhommatrix.hxx>
37 #include <cstdarg>
40 //.............................................................................
41 namespace chart
43 //.............................................................................
45 using namespace ::com::sun::star;
47 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 // diverse methods for class conversions; e.g. ::basegfx::B3DHomMatrix to HomogenMatrix
50 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 drawing::HomogenMatrix B3DHomMatrixToHomogenMatrix( const ::basegfx::B3DHomMatrix& rM )
55 drawing::HomogenMatrix aHM;
56 aHM.Line1.Column1 = rM.get(0, 0);
57 aHM.Line1.Column2 = rM.get(0, 1);
58 aHM.Line1.Column3 = rM.get(0, 2);
59 aHM.Line1.Column4 = rM.get(0, 3);
60 aHM.Line2.Column1 = rM.get(1, 0);
61 aHM.Line2.Column2 = rM.get(1, 1);
62 aHM.Line2.Column3 = rM.get(1, 2);
63 aHM.Line2.Column4 = rM.get(1, 3);
64 aHM.Line3.Column1 = rM.get(2, 0);
65 aHM.Line3.Column2 = rM.get(2, 1);
66 aHM.Line3.Column3 = rM.get(2, 2);
67 aHM.Line3.Column4 = rM.get(2, 3);
68 aHM.Line4.Column1 = rM.get(3, 0);
69 aHM.Line4.Column2 = rM.get(3, 1);
70 aHM.Line4.Column3 = rM.get(3, 2);
71 aHM.Line4.Column4 = rM.get(3, 3);
72 return aHM;
75 ::basegfx::B3DHomMatrix HomogenMatrixToB3DHomMatrix( const drawing::HomogenMatrix& rHM )
77 ::basegfx::B3DHomMatrix aM;
78 aM.set(0, 0, rHM.Line1.Column1);
79 aM.set(0, 1, rHM.Line1.Column2);
80 aM.set(0, 2, rHM.Line1.Column3);
81 aM.set(0, 3, rHM.Line1.Column4);
82 aM.set(1, 0, rHM.Line2.Column1);
83 aM.set(1, 1, rHM.Line2.Column2);
84 aM.set(1, 2, rHM.Line2.Column3);
85 aM.set(1, 3, rHM.Line2.Column4);
86 aM.set(2, 0, rHM.Line3.Column1);
87 aM.set(2, 1, rHM.Line3.Column2);
88 aM.set(2, 2, rHM.Line3.Column3);
89 aM.set(2, 3, rHM.Line3.Column4);
90 aM.set(3, 0, rHM.Line4.Column1);
91 aM.set(3, 1, rHM.Line4.Column2);
92 aM.set(3, 2, rHM.Line4.Column3);
93 aM.set(3, 3, rHM.Line4.Column4);
94 return aM;
97 ::basegfx::B2DHomMatrix IgnoreZ( const ::basegfx::B3DHomMatrix& rM )
99 ::basegfx::B2DHomMatrix aM;
100 aM.set(0, 0, rM.get(0, 0));
101 aM.set(0, 1, rM.get(0, 1));
102 aM.set(0, 2, rM.get(0, 3));
103 aM.set(1, 0, rM.get(1, 0));
104 aM.set(1, 1, rM.get(1, 1));
105 aM.set(1, 2, rM.get(1, 3));
106 aM.set(2, 0, rM.get(3, 0));
107 aM.set(2, 1, rM.get(3, 1));
108 aM.set(2, 2, rM.get(3, 3));
109 return aM;
113 drawing::HomogenMatrix3 B2DHomMatrixToHomogenMatrix3( const ::basegfx::B2DHomMatrix& rM )
115 drawing::HomogenMatrix3 aHM;
116 aHM.Line1.Column1 = rM.get(0, 0);
117 aHM.Line1.Column2 = rM.get(0, 1);
118 aHM.Line1.Column3 = rM.get(0, 2);
119 aHM.Line2.Column1 = rM.get(1, 0);
120 aHM.Line2.Column2 = rM.get(1, 1);
121 aHM.Line2.Column3 = rM.get(1, 2);
122 aHM.Line3.Column1 = rM.get(2, 0);
123 aHM.Line3.Column2 = rM.get(2, 1);
124 aHM.Line3.Column3 = rM.get(2, 2);
125 return aHM;
128 ::basegfx::B3DPoint Position3DToB3DPoint( const drawing::Position3D& rPosition )
130 return ::basegfx::B3DPoint(
131 rPosition.PositionX ,
132 rPosition.PositionY ,
133 rPosition.PositionZ );
136 drawing::Direction3D B3DVectorToDirection3D( const ::basegfx::B3DVector& rVector)
138 return drawing::Direction3D(
139 rVector.getX()
140 , rVector.getY()
141 , rVector.getZ()
145 drawing::Position3D B3DPointToPosition3D( const ::basegfx::B3DPoint& rPoint)
147 return drawing::Position3D(
148 rPoint.getX()
149 , rPoint.getY()
150 , rPoint.getZ()
154 ::basegfx::B3DVector Direction3DToB3DVector( const drawing::Direction3D& rDirection)
156 return ::basegfx::B3DVector(
157 rDirection.DirectionX
158 , rDirection.DirectionY
159 , rDirection.DirectionZ
163 void AddPointToPoly( drawing::PolyPolygonShape3D& rPoly, const drawing::Position3D& rPos, sal_Int32 nPolygonIndex )
165 if(nPolygonIndex<0)
167 OSL_FAIL( "The polygon index needs to be > 0");
168 nPolygonIndex=0;
171 //make sure that we have enough polygons
172 if(nPolygonIndex >= rPoly.SequenceX.getLength() )
174 rPoly.SequenceX.realloc(nPolygonIndex+1);
175 rPoly.SequenceY.realloc(nPolygonIndex+1);
176 rPoly.SequenceZ.realloc(nPolygonIndex+1);
179 drawing::DoubleSequence* pOuterSequenceX = &rPoly.SequenceX.getArray()[nPolygonIndex];
180 drawing::DoubleSequence* pOuterSequenceY = &rPoly.SequenceY.getArray()[nPolygonIndex];
181 drawing::DoubleSequence* pOuterSequenceZ = &rPoly.SequenceZ.getArray()[nPolygonIndex];
183 sal_Int32 nOldPointCount = pOuterSequenceX->getLength();
185 pOuterSequenceX->realloc(nOldPointCount+1);
186 pOuterSequenceY->realloc(nOldPointCount+1);
187 pOuterSequenceZ->realloc(nOldPointCount+1);
189 double* pInnerSequenceX = pOuterSequenceX->getArray();
190 double* pInnerSequenceY = pOuterSequenceY->getArray();
191 double* pInnerSequenceZ = pOuterSequenceZ->getArray();
193 pInnerSequenceX[nOldPointCount] = rPos.PositionX;
194 pInnerSequenceY[nOldPointCount] = rPos.PositionY;
195 pInnerSequenceZ[nOldPointCount] = rPos.PositionZ;
198 drawing::Position3D getPointFromPoly( const drawing::PolyPolygonShape3D& rPolygon, sal_Int32 nPointIndex, sal_Int32 nPolyIndex )
200 drawing::Position3D aRet(0.0,0.0,0.0);
202 if( nPolyIndex>=0 && nPolyIndex<rPolygon.SequenceX.getLength())
204 if(nPointIndex<rPolygon.SequenceX[nPolyIndex].getLength())
206 aRet.PositionX = rPolygon.SequenceX[nPolyIndex][nPointIndex];
207 aRet.PositionY = rPolygon.SequenceY[nPolyIndex][nPointIndex];
208 aRet.PositionZ = rPolygon.SequenceZ[nPolyIndex][nPointIndex];
210 else
212 OSL_FAIL("polygon was accessed with a wrong index");
215 else
217 OSL_FAIL("polygon was accessed with a wrong index");
219 return aRet;
222 void addPolygon( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
224 sal_Int32 nAddOuterCount = rAdd.SequenceX.getLength();
225 sal_Int32 nOuterCount = rRet.SequenceX.getLength() + nAddOuterCount;
226 rRet.SequenceX.realloc( nOuterCount );
227 rRet.SequenceY.realloc( nOuterCount );
228 rRet.SequenceZ.realloc( nOuterCount );
230 sal_Int32 nIndex = 0;
231 sal_Int32 nOuter = nOuterCount - nAddOuterCount;
232 for( ; nOuter < nOuterCount; nOuter++ )
234 if( nIndex >= nAddOuterCount )
235 break;
237 rRet.SequenceX[nOuter] = rAdd.SequenceX[nIndex];
238 rRet.SequenceY[nOuter] = rAdd.SequenceY[nIndex];
239 rRet.SequenceZ[nOuter] = rAdd.SequenceZ[nIndex];
241 nIndex++;
245 void appendPoly( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
247 sal_Int32 nOuterCount = Max( rRet.SequenceX.getLength(), rAdd.SequenceX.getLength() );
248 rRet.SequenceX.realloc(nOuterCount);
249 rRet.SequenceY.realloc(nOuterCount);
250 rRet.SequenceZ.realloc(nOuterCount);
252 for( sal_Int32 nOuter=0;nOuter<nOuterCount;nOuter++ )
254 sal_Int32 nOldPointCount = rRet.SequenceX[nOuter].getLength();
255 sal_Int32 nAddPointCount = 0;
256 if(nOuter<rAdd.SequenceX.getLength())
257 nAddPointCount = rAdd.SequenceX[nOuter].getLength();
258 if(!nAddPointCount)
259 continue;
261 sal_Int32 nNewPointCount = nOldPointCount + nAddPointCount;
263 rRet.SequenceX[nOuter].realloc(nNewPointCount);
264 rRet.SequenceY[nOuter].realloc(nNewPointCount);
265 rRet.SequenceZ[nOuter].realloc(nNewPointCount);
267 sal_Int32 nPointTarget=nOldPointCount;
268 sal_Int32 nPointSource=nAddPointCount;
269 for( ; nPointSource-- ; nPointTarget++ )
271 rRet.SequenceX[nOuter][nPointTarget] = rAdd.SequenceX[nOuter][nPointSource];
272 rRet.SequenceY[nOuter][nPointTarget] = rAdd.SequenceY[nOuter][nPointSource];
273 rRet.SequenceZ[nOuter][nPointTarget] = rAdd.SequenceZ[nOuter][nPointSource];
278 drawing::PolyPolygonShape3D BezierToPoly(
279 const drawing::PolyPolygonBezierCoords& rBezier )
281 const drawing::PointSequenceSequence& rPointSequence = rBezier.Coordinates;
283 drawing::PolyPolygonShape3D aRet;
284 aRet.SequenceX.realloc( rPointSequence.getLength() );
285 aRet.SequenceY.realloc( rPointSequence.getLength() );
286 aRet.SequenceZ.realloc( rPointSequence.getLength() );
288 sal_Int32 nRealOuter = 0;
289 for(sal_Int32 nN = 0; nN < rPointSequence.getLength(); nN++)
291 sal_Int32 nInnerLength = rPointSequence[nN].getLength();
292 aRet.SequenceX[nN].realloc( nInnerLength );
293 aRet.SequenceY[nN].realloc( nInnerLength );
294 aRet.SequenceZ[nN].realloc( nInnerLength );
296 bool bHasOuterFlags = nN < rBezier.Flags.getLength();
298 sal_Int32 nRealInner = 0;
299 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
301 bool bHasInnerFlags = bHasOuterFlags && (nM < rBezier.Flags[nN].getLength());
303 if( !bHasInnerFlags || (rBezier.Flags[nN][nM] == drawing::PolygonFlags_NORMAL) )
305 aRet.SequenceX[nRealOuter][nRealInner] = rPointSequence[nN][nM].X;
306 aRet.SequenceY[nRealOuter][nRealInner] = rPointSequence[nN][nM].Y;
307 aRet.SequenceZ[nRealOuter][nRealInner] = 0.0;
308 nRealInner++;
312 aRet.SequenceX[nRealOuter].realloc( nRealInner );
313 aRet.SequenceY[nRealOuter].realloc( nRealInner );
314 aRet.SequenceZ[nRealOuter].realloc( nRealInner );
316 if( nRealInner>0 )
317 nRealOuter++;
320 aRet.SequenceX.realloc( nRealOuter );
321 aRet.SequenceY.realloc( nRealOuter );
322 aRet.SequenceZ.realloc( nRealOuter );
324 return aRet;
327 drawing::PointSequenceSequence PolyToPointSequence(
328 const drawing::PolyPolygonShape3D& rPolyPolygon )
330 drawing::PointSequenceSequence aRet;
331 aRet.realloc( rPolyPolygon.SequenceX.getLength() );
333 for(sal_Int32 nN = 0; nN < rPolyPolygon.SequenceX.getLength(); nN++)
335 sal_Int32 nInnerLength = rPolyPolygon.SequenceX[nN].getLength();
336 aRet[nN].realloc( nInnerLength );
337 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
339 aRet[nN][nM].X = static_cast<sal_Int32>(rPolyPolygon.SequenceX[nN][nM]);
340 aRet[nN][nM].Y = static_cast<sal_Int32>(rPolyPolygon.SequenceY[nN][nM]);
343 return aRet;
346 void appendPointSequence( drawing::PointSequenceSequence& rTarget
347 , drawing::PointSequenceSequence& rAdd )
349 sal_Int32 nAddCount = rAdd.getLength();
350 if(!nAddCount)
351 return;
352 sal_Int32 nOldCount = rTarget.getLength();
354 rTarget.realloc(nOldCount+nAddCount);
355 for(sal_Int32 nS=0; nS<nAddCount; nS++ )
356 rTarget[nOldCount+nS]=rAdd[nS];
359 drawing::Position3D operator+( const drawing::Position3D& rPos
360 , const drawing::Direction3D& rDirection)
362 return drawing::Position3D(
363 rPos.PositionX + rDirection.DirectionX
364 , rPos.PositionY + rDirection.DirectionY
365 , rPos.PositionZ + rDirection.DirectionZ
369 drawing::Direction3D operator-( const drawing::Position3D& rPos1
370 , const drawing::Position3D& rPos2)
372 return drawing::Direction3D(
373 rPos1.PositionX - rPos2.PositionX
374 , rPos1.PositionY - rPos2.PositionY
375 , rPos1.PositionZ - rPos2.PositionZ
379 bool operator==( const drawing::Position3D& rPos1
380 , const drawing::Position3D& rPos2)
382 return rPos1.PositionX == rPos2.PositionX
383 && rPos1.PositionY == rPos2.PositionY
384 && rPos1.PositionZ == rPos2.PositionZ;
387 awt::Point Position3DToAWTPoint( const drawing::Position3D& rPos )
389 awt::Point aRet;
390 aRet.X = static_cast<sal_Int32>(rPos.PositionX);
391 aRet.Y = static_cast<sal_Int32>(rPos.PositionY);
392 return aRet;
395 awt::Point ToPoint( const awt::Rectangle& rRectangle )
397 return awt::Point( rRectangle.X, rRectangle.Y );
400 awt::Size ToSize( const awt::Rectangle& rRectangle )
402 return awt::Size( rRectangle.Width, rRectangle.Height );
405 awt::Size Direction3DToAWTSize( const drawing::Direction3D& rDirection )
407 awt::Size aRet;
408 aRet.Width = static_cast<sal_Int32>(rDirection.DirectionX);
409 aRet.Height = static_cast<sal_Int32>(rDirection.DirectionY);
410 return aRet;
413 uno::Sequence< double > B3DPointToSequence( const ::basegfx::B3DPoint& rPoint )
415 uno::Sequence< double > aRet(3);
416 aRet[0] = rPoint.getX();
417 aRet[1] = rPoint.getY();
418 aRet[2] = rPoint.getZ();
419 return aRet;
422 drawing::Position3D SequenceToPosition3D( const uno::Sequence< double >& rSeq )
424 OSL_ENSURE(rSeq.getLength()==3,"The sequence needs to have length 3 for conversion into vector");
426 drawing::Position3D aRet;
427 aRet.PositionX = rSeq.getLength()>0?rSeq[0]:0.0;
428 aRet.PositionY = rSeq.getLength()>1?rSeq[1]:0.0;
429 aRet.PositionZ = rSeq.getLength()>2?rSeq[2]:0.0;
430 return aRet;
433 uno::Sequence< double > Position3DToSequence( const drawing::Position3D& rPosition )
435 uno::Sequence< double > aRet(3);
436 aRet[0] = rPosition.PositionX;
437 aRet[1] = rPosition.PositionY;
438 aRet[2] = rPosition.PositionZ;
439 return aRet;
442 using namespace ::com::sun::star::chart2;
444 uno::Sequence< double > DataSequenceToDoubleSequence(
445 const uno::Reference< data::XDataSequence >& xDataSequence )
447 uno::Sequence< double > aResult;
448 OSL_ASSERT( xDataSequence.is());
449 if(!xDataSequence.is())
450 return aResult;
452 uno::Reference< data::XNumericalDataSequence > xNumericalDataSequence( xDataSequence, uno::UNO_QUERY );
453 if( xNumericalDataSequence.is() )
455 aResult = xNumericalDataSequence->getNumericalData();
457 else
459 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
460 aResult.realloc(aValues.getLength());
461 for(sal_Int32 nN=aValues.getLength();nN--;)
463 if( !(aValues[nN] >>= aResult[nN]) )
464 ::rtl::math::setNan( &aResult[nN] );
468 return aResult;
471 uno::Sequence< rtl::OUString > DataSequenceToStringSequence(
472 const uno::Reference< data::XDataSequence >& xDataSequence )
474 uno::Sequence< rtl::OUString > aResult;
475 if(!xDataSequence.is())
476 return aResult;
478 uno::Reference< data::XTextualDataSequence > xTextualDataSequence( xDataSequence, uno::UNO_QUERY );
479 if( xTextualDataSequence.is() )
481 aResult = xTextualDataSequence->getTextualData();
483 else
485 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
486 aResult.realloc(aValues.getLength());
488 for(sal_Int32 nN=aValues.getLength();nN--;)
489 aValues[nN] >>= aResult[nN];
492 return aResult;
495 sal_Bool hasDoubleValue( const uno::Any& rAny )
497 sal_Bool bRet = sal_False;
498 double fValue = 0.0;
499 if( rAny >>= fValue )
500 bRet = sal_True;
501 return bRet;
504 sal_Bool hasLongOrShortValue( const uno::Any& rAny )
506 sal_Bool bRet = sal_False;
507 sal_Int32 n32 = 0;
508 if( rAny >>= n32 )
509 bRet = sal_True;
510 else
512 sal_Int16 n16 = 0;
513 if( rAny >>= n16 )
514 bRet = sal_True;
516 return bRet;
518 sal_Int16 getShortForLongAlso( const uno::Any& rAny )
520 sal_Int16 nRet = 0;
522 if( !(rAny >>= nRet) )
524 sal_Int32 n32 = 0;
525 if( rAny >>= n32 )
526 nRet = static_cast<sal_Int16>(n32);
528 return nRet;
531 bool replaceParamterInString( rtl::OUString & rInOutResourceString,
532 const rtl::OUString & rParamToReplace,
533 const rtl::OUString & rReplaceWith )
535 sal_Int32 nPos = rInOutResourceString.indexOf( rParamToReplace );
536 if( nPos == -1 )
537 return false;
539 rInOutResourceString = rInOutResourceString.replaceAt( nPos
540 , rParamToReplace.getLength(), rReplaceWith );
541 return true;
544 //.............................................................................
545 } //namespace chart
546 //.............................................................................
548 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */