merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / CommonConverters.cxx
blobe9d90248ce84f667df435505309926d7b7bb4e41
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "CommonConverters.hxx"
31 #include <com/sun/star/drawing/DoubleSequence.hpp>
32 #include <com/sun/star/text/WritingMode2.hpp>
33 #include <com/sun/star/chart2/data/XNumericalDataSequence.hpp>
34 #include <com/sun/star/chart2/data/XTextualDataSequence.hpp>
35 #include <rtl/math.hxx>
36 #include <basegfx/matrix/b3dhommatrix.hxx>
38 #include <cstdarg>
41 //.............................................................................
42 namespace chart
44 //.............................................................................
46 using namespace ::com::sun::star;
48 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 // diverse methods for class conversions; e.g. ::basegfx::B3DHomMatrix to HomogenMatrix
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 drawing::HomogenMatrix B3DHomMatrixToHomogenMatrix( const ::basegfx::B3DHomMatrix& rM )
56 drawing::HomogenMatrix aHM;
57 aHM.Line1.Column1 = rM.get(0, 0);
58 aHM.Line1.Column2 = rM.get(0, 1);
59 aHM.Line1.Column3 = rM.get(0, 2);
60 aHM.Line1.Column4 = rM.get(0, 3);
61 aHM.Line2.Column1 = rM.get(1, 0);
62 aHM.Line2.Column2 = rM.get(1, 1);
63 aHM.Line2.Column3 = rM.get(1, 2);
64 aHM.Line2.Column4 = rM.get(1, 3);
65 aHM.Line3.Column1 = rM.get(2, 0);
66 aHM.Line3.Column2 = rM.get(2, 1);
67 aHM.Line3.Column3 = rM.get(2, 2);
68 aHM.Line3.Column4 = rM.get(2, 3);
69 aHM.Line4.Column1 = rM.get(3, 0);
70 aHM.Line4.Column2 = rM.get(3, 1);
71 aHM.Line4.Column3 = rM.get(3, 2);
72 aHM.Line4.Column4 = rM.get(3, 3);
73 return aHM;
76 ::basegfx::B3DHomMatrix HomogenMatrixToB3DHomMatrix( const drawing::HomogenMatrix& rHM )
78 ::basegfx::B3DHomMatrix aM;
79 aM.set(0, 0, rHM.Line1.Column1);
80 aM.set(0, 1, rHM.Line1.Column2);
81 aM.set(0, 2, rHM.Line1.Column3);
82 aM.set(0, 3, rHM.Line1.Column4);
83 aM.set(1, 0, rHM.Line2.Column1);
84 aM.set(1, 1, rHM.Line2.Column2);
85 aM.set(1, 2, rHM.Line2.Column3);
86 aM.set(1, 3, rHM.Line2.Column4);
87 aM.set(2, 0, rHM.Line3.Column1);
88 aM.set(2, 1, rHM.Line3.Column2);
89 aM.set(2, 2, rHM.Line3.Column3);
90 aM.set(2, 3, rHM.Line3.Column4);
91 aM.set(3, 0, rHM.Line4.Column1);
92 aM.set(3, 1, rHM.Line4.Column2);
93 aM.set(3, 2, rHM.Line4.Column3);
94 aM.set(3, 3, rHM.Line4.Column4);
95 return aM;
98 ::basegfx::B2DHomMatrix IgnoreZ( const ::basegfx::B3DHomMatrix& rM )
100 ::basegfx::B2DHomMatrix aM;
101 aM.set(0, 0, rM.get(0, 0));
102 aM.set(0, 1, rM.get(0, 1));
103 aM.set(0, 2, rM.get(0, 3));
104 aM.set(1, 0, rM.get(1, 0));
105 aM.set(1, 1, rM.get(1, 1));
106 aM.set(1, 2, rM.get(1, 3));
107 aM.set(2, 0, rM.get(3, 0));
108 aM.set(2, 1, rM.get(3, 1));
109 aM.set(2, 2, rM.get(3, 3));
110 return aM;
114 drawing::HomogenMatrix3 B2DHomMatrixToHomogenMatrix3( const ::basegfx::B2DHomMatrix& rM )
116 drawing::HomogenMatrix3 aHM;
117 aHM.Line1.Column1 = rM.get(0, 0);
118 aHM.Line1.Column2 = rM.get(0, 1);
119 aHM.Line1.Column3 = rM.get(0, 2);
120 aHM.Line2.Column1 = rM.get(1, 0);
121 aHM.Line2.Column2 = rM.get(1, 1);
122 aHM.Line2.Column3 = rM.get(1, 2);
123 aHM.Line3.Column1 = rM.get(2, 0);
124 aHM.Line3.Column2 = rM.get(2, 1);
125 aHM.Line3.Column3 = rM.get(2, 2);
126 return aHM;
129 ::basegfx::B3DPoint Position3DToB3DPoint( const drawing::Position3D& rPosition )
131 return ::basegfx::B3DPoint(
132 rPosition.PositionX ,
133 rPosition.PositionY ,
134 rPosition.PositionZ );
137 drawing::Direction3D B3DVectorToDirection3D( const ::basegfx::B3DVector& rVector)
139 return drawing::Direction3D(
140 rVector.getX()
141 , rVector.getY()
142 , rVector.getZ()
146 drawing::Position3D B3DPointToPosition3D( const ::basegfx::B3DPoint& rPoint)
148 return drawing::Position3D(
149 rPoint.getX()
150 , rPoint.getY()
151 , rPoint.getZ()
155 ::basegfx::B3DVector Direction3DToB3DVector( const drawing::Direction3D& rDirection)
157 return ::basegfx::B3DVector(
158 rDirection.DirectionX
159 , rDirection.DirectionY
160 , rDirection.DirectionZ
164 void AddPointToPoly( drawing::PolyPolygonShape3D& rPoly, const drawing::Position3D& rPos, sal_Int32 nPolygonIndex )
166 if(nPolygonIndex<0)
168 OSL_ENSURE( false, "The polygon index needs to be > 0");
169 nPolygonIndex=0;
172 //make sure that we have enough polygons
173 if(nPolygonIndex >= rPoly.SequenceX.getLength() )
175 rPoly.SequenceX.realloc(nPolygonIndex+1);
176 rPoly.SequenceY.realloc(nPolygonIndex+1);
177 rPoly.SequenceZ.realloc(nPolygonIndex+1);
180 drawing::DoubleSequence* pOuterSequenceX = &rPoly.SequenceX.getArray()[nPolygonIndex];
181 drawing::DoubleSequence* pOuterSequenceY = &rPoly.SequenceY.getArray()[nPolygonIndex];
182 drawing::DoubleSequence* pOuterSequenceZ = &rPoly.SequenceZ.getArray()[nPolygonIndex];
184 sal_Int32 nOldPointCount = pOuterSequenceX->getLength();
186 pOuterSequenceX->realloc(nOldPointCount+1);
187 pOuterSequenceY->realloc(nOldPointCount+1);
188 pOuterSequenceZ->realloc(nOldPointCount+1);
190 double* pInnerSequenceX = pOuterSequenceX->getArray();
191 double* pInnerSequenceY = pOuterSequenceY->getArray();
192 double* pInnerSequenceZ = pOuterSequenceZ->getArray();
194 pInnerSequenceX[nOldPointCount] = rPos.PositionX;
195 pInnerSequenceY[nOldPointCount] = rPos.PositionY;
196 pInnerSequenceZ[nOldPointCount] = rPos.PositionZ;
199 drawing::Position3D getPointFromPoly( const drawing::PolyPolygonShape3D& rPolygon, sal_Int32 nPointIndex, sal_Int32 nPolyIndex )
201 drawing::Position3D aRet(0.0,0.0,0.0);
203 if( nPolyIndex>=0 && nPolyIndex<rPolygon.SequenceX.getLength())
205 if(nPointIndex<rPolygon.SequenceX[nPolyIndex].getLength())
207 aRet.PositionX = rPolygon.SequenceX[nPolyIndex][nPointIndex];
208 aRet.PositionY = rPolygon.SequenceY[nPolyIndex][nPointIndex];
209 aRet.PositionZ = rPolygon.SequenceZ[nPolyIndex][nPointIndex];
211 else
213 ;DBG_ERROR("polygon was accessed with a wrong index");
216 else
218 ;DBG_ERROR("polygon was accessed with a wrong index");
220 return aRet;
223 void addPolygon( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
225 sal_Int32 nAddOuterCount = rAdd.SequenceX.getLength();
226 sal_Int32 nOuterCount = rRet.SequenceX.getLength() + nAddOuterCount;
227 rRet.SequenceX.realloc( nOuterCount );
228 rRet.SequenceY.realloc( nOuterCount );
229 rRet.SequenceZ.realloc( nOuterCount );
231 sal_Int32 nIndex = 0;
232 sal_Int32 nOuter = nOuterCount - nAddOuterCount;
233 for( ; nOuter < nOuterCount; nOuter++ )
235 if( nIndex >= nAddOuterCount )
236 break;
238 rRet.SequenceX[nOuter] = rAdd.SequenceX[nIndex];
239 rRet.SequenceY[nOuter] = rAdd.SequenceY[nIndex];
240 rRet.SequenceZ[nOuter] = rAdd.SequenceZ[nIndex];
242 nIndex++;
246 void appendPoly( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
248 sal_Int32 nOuterCount = Max( rRet.SequenceX.getLength(), rAdd.SequenceX.getLength() );
249 rRet.SequenceX.realloc(nOuterCount);
250 rRet.SequenceY.realloc(nOuterCount);
251 rRet.SequenceZ.realloc(nOuterCount);
253 for( sal_Int32 nOuter=0;nOuter<nOuterCount;nOuter++ )
255 sal_Int32 nOldPointCount = rRet.SequenceX[nOuter].getLength();
256 sal_Int32 nAddPointCount = 0;
257 if(nOuter<rAdd.SequenceX.getLength())
258 nAddPointCount = rAdd.SequenceX[nOuter].getLength();
259 if(!nAddPointCount)
260 continue;
262 sal_Int32 nNewPointCount = nOldPointCount + nAddPointCount;
264 rRet.SequenceX[nOuter].realloc(nNewPointCount);
265 rRet.SequenceY[nOuter].realloc(nNewPointCount);
266 rRet.SequenceZ[nOuter].realloc(nNewPointCount);
268 sal_Int32 nPointTarget=nOldPointCount;
269 sal_Int32 nPointSource=nAddPointCount;
270 for( ; nPointSource-- ; nPointTarget++ )
272 rRet.SequenceX[nOuter][nPointTarget] = rAdd.SequenceX[nOuter][nPointSource];
273 rRet.SequenceY[nOuter][nPointTarget] = rAdd.SequenceY[nOuter][nPointSource];
274 rRet.SequenceZ[nOuter][nPointTarget] = rAdd.SequenceZ[nOuter][nPointSource];
279 drawing::PolyPolygonShape3D BezierToPoly(
280 const drawing::PolyPolygonBezierCoords& rBezier )
282 const drawing::PointSequenceSequence& rPointSequence = rBezier.Coordinates;
283 // const drawing::FlagSequenceSequence& rFlags = rBezier.Flags;
285 drawing::PolyPolygonShape3D aRet;
286 aRet.SequenceX.realloc( rPointSequence.getLength() );
287 aRet.SequenceY.realloc( rPointSequence.getLength() );
288 aRet.SequenceZ.realloc( rPointSequence.getLength() );
290 sal_Int32 nRealOuter = 0;
291 for(sal_Int32 nN = 0; nN < rPointSequence.getLength(); nN++)
293 sal_Int32 nInnerLength = rPointSequence[nN].getLength();
294 aRet.SequenceX[nN].realloc( nInnerLength );
295 aRet.SequenceY[nN].realloc( nInnerLength );
296 aRet.SequenceZ[nN].realloc( nInnerLength );
298 bool bHasOuterFlags = nN < rBezier.Flags.getLength();
300 sal_Int32 nRealInner = 0;
301 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
303 bool bHasInnerFlags = bHasOuterFlags && (nM < rBezier.Flags[nN].getLength());
305 if( !bHasInnerFlags || (rBezier.Flags[nN][nM] == drawing::PolygonFlags_NORMAL) )
307 aRet.SequenceX[nRealOuter][nRealInner] = rPointSequence[nN][nM].X;
308 aRet.SequenceY[nRealOuter][nRealInner] = rPointSequence[nN][nM].Y;
309 aRet.SequenceZ[nRealOuter][nRealInner] = 0.0;
310 nRealInner++;
314 aRet.SequenceX[nRealOuter].realloc( nRealInner );
315 aRet.SequenceY[nRealOuter].realloc( nRealInner );
316 aRet.SequenceZ[nRealOuter].realloc( nRealInner );
318 if( nRealInner>0 )
319 nRealOuter++;
322 aRet.SequenceX.realloc( nRealOuter );
323 aRet.SequenceY.realloc( nRealOuter );
324 aRet.SequenceZ.realloc( nRealOuter );
326 return aRet;
329 drawing::PointSequenceSequence PolyToPointSequence(
330 const drawing::PolyPolygonShape3D& rPolyPolygon )
332 drawing::PointSequenceSequence aRet;
333 aRet.realloc( rPolyPolygon.SequenceX.getLength() );
335 for(sal_Int32 nN = 0; nN < rPolyPolygon.SequenceX.getLength(); nN++)
337 sal_Int32 nInnerLength = rPolyPolygon.SequenceX[nN].getLength();
338 aRet[nN].realloc( nInnerLength );
339 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
341 aRet[nN][nM].X = static_cast<sal_Int32>(rPolyPolygon.SequenceX[nN][nM]);
342 aRet[nN][nM].Y = static_cast<sal_Int32>(rPolyPolygon.SequenceY[nN][nM]);
345 return aRet;
348 void appendPointSequence( drawing::PointSequenceSequence& rTarget
349 , drawing::PointSequenceSequence& rAdd )
351 sal_Int32 nAddCount = rAdd.getLength();
352 if(!nAddCount)
353 return;
354 sal_Int32 nOldCount = rTarget.getLength();
356 rTarget.realloc(nOldCount+nAddCount);
357 for(sal_Int32 nS=0; nS<nAddCount; nS++ )
358 rTarget[nOldCount+nS]=rAdd[nS];
361 drawing::Position3D operator+( const drawing::Position3D& rPos
362 , const drawing::Direction3D& rDirection)
364 return drawing::Position3D(
365 rPos.PositionX + rDirection.DirectionX
366 , rPos.PositionY + rDirection.DirectionY
367 , rPos.PositionZ + rDirection.DirectionZ
371 drawing::Direction3D operator-( const drawing::Position3D& rPos1
372 , const drawing::Position3D& rPos2)
374 return drawing::Direction3D(
375 rPos1.PositionX - rPos2.PositionX
376 , rPos1.PositionY - rPos2.PositionY
377 , rPos1.PositionZ - rPos2.PositionZ
381 bool operator==( const drawing::Position3D& rPos1
382 , const drawing::Position3D& rPos2)
384 return rPos1.PositionX == rPos2.PositionX
385 && rPos1.PositionY == rPos2.PositionY
386 && rPos1.PositionZ == rPos2.PositionZ;
389 awt::Point Position3DToAWTPoint( const drawing::Position3D& rPos )
391 awt::Point aRet;
392 aRet.X = static_cast<sal_Int32>(rPos.PositionX);
393 aRet.Y = static_cast<sal_Int32>(rPos.PositionY);
394 return aRet;
397 awt::Point ToPoint( const awt::Rectangle& rRectangle )
399 return awt::Point( rRectangle.X, rRectangle.Y );
402 awt::Size ToSize( const awt::Rectangle& rRectangle )
404 return awt::Size( rRectangle.Width, rRectangle.Height );
407 awt::Size Direction3DToAWTSize( const drawing::Direction3D& rDirection )
409 awt::Size aRet;
410 aRet.Width = static_cast<sal_Int32>(rDirection.DirectionX);
411 aRet.Height = static_cast<sal_Int32>(rDirection.DirectionY);
412 return aRet;
415 uno::Sequence< double > B3DPointToSequence( const ::basegfx::B3DPoint& rPoint )
417 uno::Sequence< double > aRet(3);
418 aRet[0] = rPoint.getX();
419 aRet[1] = rPoint.getY();
420 aRet[2] = rPoint.getZ();
421 return aRet;
424 drawing::Position3D SequenceToPosition3D( const uno::Sequence< double >& rSeq )
426 OSL_ENSURE(rSeq.getLength()==3,"The sequence needs to have length 3 for conversion into vector");
428 drawing::Position3D aRet;
429 aRet.PositionX = rSeq.getLength()>0?rSeq[0]:0.0;
430 aRet.PositionY = rSeq.getLength()>1?rSeq[1]:0.0;
431 aRet.PositionZ = rSeq.getLength()>2?rSeq[2]:0.0;
432 return aRet;
435 uno::Sequence< double > Position3DToSequence( const drawing::Position3D& rPosition )
437 uno::Sequence< double > aRet(3);
438 aRet[0] = rPosition.PositionX;
439 aRet[1] = rPosition.PositionY;
440 aRet[2] = rPosition.PositionZ;
441 return aRet;
444 using namespace ::com::sun::star::chart2;
446 uno::Sequence< double > DataSequenceToDoubleSequence(
447 const uno::Reference< data::XDataSequence >& xDataSequence )
449 uno::Sequence< double > aResult;
450 OSL_ASSERT( xDataSequence.is());
451 if(!xDataSequence.is())
452 return aResult;
454 uno::Reference< data::XNumericalDataSequence > xNumericalDataSequence( xDataSequence, uno::UNO_QUERY );
455 if( xNumericalDataSequence.is() )
457 aResult = xNumericalDataSequence->getNumericalData();
459 else
461 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
462 aResult.realloc(aValues.getLength());
463 for(sal_Int32 nN=aValues.getLength();nN--;)
465 if( !(aValues[nN] >>= aResult[nN]) )
466 ::rtl::math::setNan( &aResult[nN] );
470 return aResult;
473 uno::Sequence< rtl::OUString > DataSequenceToStringSequence(
474 const uno::Reference< data::XDataSequence >& xDataSequence )
476 uno::Sequence< rtl::OUString > aResult;
477 OSL_ASSERT( xDataSequence.is());
478 if(!xDataSequence.is())
479 return aResult;
481 uno::Reference< data::XTextualDataSequence > xTextualDataSequence( xDataSequence, uno::UNO_QUERY );
482 if( xTextualDataSequence.is() )
484 aResult = xTextualDataSequence->getTextualData();
486 else
488 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
489 aResult.realloc(aValues.getLength());
491 for(sal_Int32 nN=aValues.getLength();nN--;)
492 aValues[nN] >>= aResult[nN];
495 return aResult;
498 sal_Bool hasDoubleValue( const uno::Any& rAny )
500 sal_Bool bRet = sal_False;
501 double fValue = 0.0;
502 if( rAny >>= fValue )
503 bRet = sal_True;
504 return bRet;
507 sal_Bool hasLongOrShortValue( const uno::Any& rAny )
509 sal_Bool bRet = sal_False;
510 sal_Int32 n32 = 0;
511 if( rAny >>= n32 )
512 bRet = sal_True;
513 else
515 sal_Int16 n16 = 0;
516 if( rAny >>= n16 )
517 bRet = sal_True;
519 return bRet;
521 sal_Int16 getShortForLongAlso( const uno::Any& rAny )
523 sal_Int16 nRet = 0;
525 if( !(rAny >>= nRet) )
527 sal_Int32 n32 = 0;
528 if( rAny >>= n32 )
529 nRet = static_cast<sal_Int16>(n32);
531 return nRet;
534 bool replaceParamterInString( rtl::OUString & rInOutResourceString,
535 const rtl::OUString & rParamToReplace,
536 const rtl::OUString & rReplaceWith )
538 sal_Int32 nPos = rInOutResourceString.indexOf( rParamToReplace );
539 if( nPos == -1 )
540 return false;
542 rInOutResourceString = rInOutResourceString.replaceAt( nPos
543 , rParamToReplace.getLength(), rReplaceWith );
544 return true;
547 //.............................................................................
548 } //namespace chart
549 //.............................................................................