Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / CommonConverters.cxx
blob01b2c2c758a4b9876162b0ed98aee4f48352a67f
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/awt/Rectangle.hpp>
22 #include <com/sun/star/drawing/DoubleSequence.hpp>
23 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
24 #include <com/sun/star/chart2/data/XDataSequence.hpp>
25 #include <com/sun/star/chart2/data/XNumericalDataSequence.hpp>
26 #include <com/sun/star/chart2/data/XTextualDataSequence.hpp>
27 #include <rtl/math.hxx>
28 #include <osl/diagnose.h>
29 #include <basegfx/matrix/b3dhommatrix.hxx>
31 namespace chart
34 using namespace ::com::sun::star;
36 // diverse methods for class conversions; e.g. ::basegfx::B3DHomMatrix to HomogenMatrix
38 drawing::HomogenMatrix B3DHomMatrixToHomogenMatrix( const ::basegfx::B3DHomMatrix& rM )
40 drawing::HomogenMatrix aHM;
41 aHM.Line1.Column1 = rM.get(0, 0);
42 aHM.Line1.Column2 = rM.get(0, 1);
43 aHM.Line1.Column3 = rM.get(0, 2);
44 aHM.Line1.Column4 = rM.get(0, 3);
45 aHM.Line2.Column1 = rM.get(1, 0);
46 aHM.Line2.Column2 = rM.get(1, 1);
47 aHM.Line2.Column3 = rM.get(1, 2);
48 aHM.Line2.Column4 = rM.get(1, 3);
49 aHM.Line3.Column1 = rM.get(2, 0);
50 aHM.Line3.Column2 = rM.get(2, 1);
51 aHM.Line3.Column3 = rM.get(2, 2);
52 aHM.Line3.Column4 = rM.get(2, 3);
53 aHM.Line4.Column1 = rM.get(3, 0);
54 aHM.Line4.Column2 = rM.get(3, 1);
55 aHM.Line4.Column3 = rM.get(3, 2);
56 aHM.Line4.Column4 = rM.get(3, 3);
57 return aHM;
60 ::basegfx::B3DHomMatrix HomogenMatrixToB3DHomMatrix( const drawing::HomogenMatrix& rHM )
62 ::basegfx::B3DHomMatrix aM;
63 aM.set(0, 0, rHM.Line1.Column1);
64 aM.set(0, 1, rHM.Line1.Column2);
65 aM.set(0, 2, rHM.Line1.Column3);
66 aM.set(0, 3, rHM.Line1.Column4);
67 aM.set(1, 0, rHM.Line2.Column1);
68 aM.set(1, 1, rHM.Line2.Column2);
69 aM.set(1, 2, rHM.Line2.Column3);
70 aM.set(1, 3, rHM.Line2.Column4);
71 aM.set(2, 0, rHM.Line3.Column1);
72 aM.set(2, 1, rHM.Line3.Column2);
73 aM.set(2, 2, rHM.Line3.Column3);
74 aM.set(2, 3, rHM.Line3.Column4);
75 aM.set(3, 0, rHM.Line4.Column1);
76 aM.set(3, 1, rHM.Line4.Column2);
77 aM.set(3, 2, rHM.Line4.Column3);
78 aM.set(3, 3, rHM.Line4.Column4);
79 return aM;
82 ::basegfx::B2DHomMatrix IgnoreZ( const ::basegfx::B3DHomMatrix& rM )
84 ::basegfx::B2DHomMatrix aM;
85 aM.set(0, 0, rM.get(0, 0));
86 aM.set(0, 1, rM.get(0, 1));
87 aM.set(0, 2, rM.get(0, 3));
88 aM.set(1, 0, rM.get(1, 0));
89 aM.set(1, 1, rM.get(1, 1));
90 aM.set(1, 2, rM.get(1, 3));
91 aM.set(2, 0, rM.get(3, 0));
92 aM.set(2, 1, rM.get(3, 1));
93 aM.set(2, 2, rM.get(3, 3));
94 return aM;
97 drawing::HomogenMatrix3 B2DHomMatrixToHomogenMatrix3( const ::basegfx::B2DHomMatrix& rM )
99 drawing::HomogenMatrix3 aHM;
100 aHM.Line1.Column1 = rM.get(0, 0);
101 aHM.Line1.Column2 = rM.get(0, 1);
102 aHM.Line1.Column3 = rM.get(0, 2);
103 aHM.Line2.Column1 = rM.get(1, 0);
104 aHM.Line2.Column2 = rM.get(1, 1);
105 aHM.Line2.Column3 = rM.get(1, 2);
106 aHM.Line3.Column1 = rM.get(2, 0);
107 aHM.Line3.Column2 = rM.get(2, 1);
108 aHM.Line3.Column3 = rM.get(2, 2);
109 return aHM;
112 ::basegfx::B3DPoint Position3DToB3DPoint( const drawing::Position3D& rPosition )
114 return ::basegfx::B3DPoint(
115 rPosition.PositionX ,
116 rPosition.PositionY ,
117 rPosition.PositionZ );
120 drawing::Direction3D B3DVectorToDirection3D( const ::basegfx::B3DVector& rVector)
122 return drawing::Direction3D(
123 rVector.getX()
124 , rVector.getY()
125 , rVector.getZ()
129 drawing::Position3D B3DPointToPosition3D( const ::basegfx::B3DPoint& rPoint)
131 return drawing::Position3D(
132 rPoint.getX()
133 , rPoint.getY()
134 , rPoint.getZ()
138 ::basegfx::B3DVector Direction3DToB3DVector( const drawing::Direction3D& rDirection)
140 return ::basegfx::B3DVector(
141 rDirection.DirectionX
142 , rDirection.DirectionY
143 , rDirection.DirectionZ
147 void AddPointToPoly( drawing::PolyPolygonShape3D& rPoly, const drawing::Position3D& rPos, sal_Int32 nPolygonIndex )
149 if(nPolygonIndex<0)
151 OSL_FAIL( "The polygon index needs to be > 0");
152 nPolygonIndex=0;
155 //make sure that we have enough polygons
156 if(nPolygonIndex >= rPoly.SequenceX.getLength() )
158 rPoly.SequenceX.realloc(nPolygonIndex+1);
159 rPoly.SequenceY.realloc(nPolygonIndex+1);
160 rPoly.SequenceZ.realloc(nPolygonIndex+1);
163 drawing::DoubleSequence* pOuterSequenceX = &rPoly.SequenceX.getArray()[nPolygonIndex];
164 drawing::DoubleSequence* pOuterSequenceY = &rPoly.SequenceY.getArray()[nPolygonIndex];
165 drawing::DoubleSequence* pOuterSequenceZ = &rPoly.SequenceZ.getArray()[nPolygonIndex];
167 sal_Int32 nOldPointCount = pOuterSequenceX->getLength();
169 pOuterSequenceX->realloc(nOldPointCount+1);
170 pOuterSequenceY->realloc(nOldPointCount+1);
171 pOuterSequenceZ->realloc(nOldPointCount+1);
173 double* pInnerSequenceX = pOuterSequenceX->getArray();
174 double* pInnerSequenceY = pOuterSequenceY->getArray();
175 double* pInnerSequenceZ = pOuterSequenceZ->getArray();
177 pInnerSequenceX[nOldPointCount] = rPos.PositionX;
178 pInnerSequenceY[nOldPointCount] = rPos.PositionY;
179 pInnerSequenceZ[nOldPointCount] = rPos.PositionZ;
182 drawing::Position3D getPointFromPoly( const drawing::PolyPolygonShape3D& rPolygon, sal_Int32 nPointIndex, sal_Int32 nPolyIndex )
184 drawing::Position3D aRet(0.0,0.0,0.0);
186 if( nPolyIndex>=0 && nPolyIndex<rPolygon.SequenceX.getLength())
188 if(nPointIndex<rPolygon.SequenceX[nPolyIndex].getLength())
190 aRet.PositionX = rPolygon.SequenceX[nPolyIndex][nPointIndex];
191 aRet.PositionY = rPolygon.SequenceY[nPolyIndex][nPointIndex];
192 aRet.PositionZ = rPolygon.SequenceZ[nPolyIndex][nPointIndex];
194 else
196 OSL_FAIL("polygon was accessed with a wrong index");
199 else
201 OSL_FAIL("polygon was accessed with a wrong index");
203 return aRet;
206 void addPolygon( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
208 sal_Int32 nAddOuterCount = rAdd.SequenceX.getLength();
209 sal_Int32 nOuterCount = rRet.SequenceX.getLength() + nAddOuterCount;
210 rRet.SequenceX.realloc( nOuterCount );
211 rRet.SequenceY.realloc( nOuterCount );
212 rRet.SequenceZ.realloc( nOuterCount );
214 sal_Int32 nIndex = 0;
215 sal_Int32 nOuter = nOuterCount - nAddOuterCount;
216 for( ; nOuter < nOuterCount; nOuter++ )
218 if( nIndex >= nAddOuterCount )
219 break;
221 rRet.SequenceX[nOuter] = rAdd.SequenceX[nIndex];
222 rRet.SequenceY[nOuter] = rAdd.SequenceY[nIndex];
223 rRet.SequenceZ[nOuter] = rAdd.SequenceZ[nIndex];
225 nIndex++;
229 void appendPoly( drawing::PolyPolygonShape3D& rRet, const drawing::PolyPolygonShape3D& rAdd )
231 sal_Int32 nOuterCount = std::max( rRet.SequenceX.getLength(), rAdd.SequenceX.getLength() );
232 rRet.SequenceX.realloc(nOuterCount);
233 rRet.SequenceY.realloc(nOuterCount);
234 rRet.SequenceZ.realloc(nOuterCount);
236 for( sal_Int32 nOuter=0;nOuter<nOuterCount;nOuter++ )
238 sal_Int32 nOldPointCount = rRet.SequenceX[nOuter].getLength();
239 sal_Int32 nAddPointCount = 0;
240 if(nOuter<rAdd.SequenceX.getLength())
241 nAddPointCount = rAdd.SequenceX[nOuter].getLength();
242 if(!nAddPointCount)
243 continue;
245 sal_Int32 nNewPointCount = nOldPointCount + nAddPointCount;
247 rRet.SequenceX[nOuter].realloc(nNewPointCount);
248 rRet.SequenceY[nOuter].realloc(nNewPointCount);
249 rRet.SequenceZ[nOuter].realloc(nNewPointCount);
251 sal_Int32 nPointTarget=nOldPointCount;
252 sal_Int32 nPointSource=nAddPointCount;
253 for( ; nPointSource-- ; nPointTarget++ )
255 rRet.SequenceX[nOuter][nPointTarget] = rAdd.SequenceX[nOuter][nPointSource];
256 rRet.SequenceY[nOuter][nPointTarget] = rAdd.SequenceY[nOuter][nPointSource];
257 rRet.SequenceZ[nOuter][nPointTarget] = rAdd.SequenceZ[nOuter][nPointSource];
262 drawing::PolyPolygonShape3D BezierToPoly(
263 const drawing::PolyPolygonBezierCoords& rBezier )
265 const drawing::PointSequenceSequence& rPointSequence = rBezier.Coordinates;
267 drawing::PolyPolygonShape3D aRet;
268 aRet.SequenceX.realloc( rPointSequence.getLength() );
269 aRet.SequenceY.realloc( rPointSequence.getLength() );
270 aRet.SequenceZ.realloc( rPointSequence.getLength() );
272 sal_Int32 nRealOuter = 0;
273 for(sal_Int32 nN = 0; nN < rPointSequence.getLength(); nN++)
275 sal_Int32 nInnerLength = rPointSequence[nN].getLength();
276 aRet.SequenceX[nN].realloc( nInnerLength );
277 aRet.SequenceY[nN].realloc( nInnerLength );
278 aRet.SequenceZ[nN].realloc( nInnerLength );
280 bool bHasOuterFlags = nN < rBezier.Flags.getLength();
282 sal_Int32 nRealInner = 0;
283 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
285 bool bHasInnerFlags = bHasOuterFlags && (nM < rBezier.Flags[nN].getLength());
287 if( !bHasInnerFlags || (rBezier.Flags[nN][nM] == drawing::PolygonFlags_NORMAL) )
289 aRet.SequenceX[nRealOuter][nRealInner] = rPointSequence[nN][nM].X;
290 aRet.SequenceY[nRealOuter][nRealInner] = rPointSequence[nN][nM].Y;
291 aRet.SequenceZ[nRealOuter][nRealInner] = 0.0;
292 nRealInner++;
296 aRet.SequenceX[nRealOuter].realloc( nRealInner );
297 aRet.SequenceY[nRealOuter].realloc( nRealInner );
298 aRet.SequenceZ[nRealOuter].realloc( nRealInner );
300 if( nRealInner>0 )
301 nRealOuter++;
304 aRet.SequenceX.realloc( nRealOuter );
305 aRet.SequenceY.realloc( nRealOuter );
306 aRet.SequenceZ.realloc( nRealOuter );
308 return aRet;
311 drawing::PointSequenceSequence PolyToPointSequence(
312 const drawing::PolyPolygonShape3D& rPolyPolygon )
314 drawing::PointSequenceSequence aRet;
315 aRet.realloc( rPolyPolygon.SequenceX.getLength() );
317 for(sal_Int32 nN = 0; nN < rPolyPolygon.SequenceX.getLength(); nN++)
319 sal_Int32 nInnerLength = rPolyPolygon.SequenceX[nN].getLength();
320 aRet[nN].realloc( nInnerLength );
321 for( sal_Int32 nM = 0; nM < nInnerLength; nM++)
323 aRet[nN][nM].X = static_cast<sal_Int32>(rPolyPolygon.SequenceX[nN][nM]);
324 aRet[nN][nM].Y = static_cast<sal_Int32>(rPolyPolygon.SequenceY[nN][nM]);
327 return aRet;
330 void appendPointSequence( drawing::PointSequenceSequence& rTarget
331 , drawing::PointSequenceSequence& rAdd )
333 sal_Int32 nAddCount = rAdd.getLength();
334 if(!nAddCount)
335 return;
336 sal_Int32 nOldCount = rTarget.getLength();
338 rTarget.realloc(nOldCount+nAddCount);
339 for(sal_Int32 nS=0; nS<nAddCount; nS++ )
340 rTarget[nOldCount+nS]=rAdd[nS];
343 drawing::Position3D operator+( const drawing::Position3D& rPos
344 , const drawing::Direction3D& rDirection)
346 return drawing::Position3D(
347 rPos.PositionX + rDirection.DirectionX
348 , rPos.PositionY + rDirection.DirectionY
349 , rPos.PositionZ + rDirection.DirectionZ
353 drawing::Direction3D operator-( const drawing::Position3D& rPos1
354 , const drawing::Position3D& rPos2)
356 return drawing::Direction3D(
357 rPos1.PositionX - rPos2.PositionX
358 , rPos1.PositionY - rPos2.PositionY
359 , rPos1.PositionZ - rPos2.PositionZ
363 awt::Point Position3DToAWTPoint( const drawing::Position3D& rPos )
365 awt::Point aRet;
366 aRet.X = static_cast<sal_Int32>(rPos.PositionX);
367 aRet.Y = static_cast<sal_Int32>(rPos.PositionY);
368 return aRet;
371 awt::Point ToPoint( const awt::Rectangle& rRectangle )
373 return awt::Point( rRectangle.X, rRectangle.Y );
376 awt::Size ToSize( const awt::Rectangle& rRectangle )
378 return awt::Size( rRectangle.Width, rRectangle.Height );
381 awt::Size Direction3DToAWTSize( const drawing::Direction3D& rDirection )
383 awt::Size aRet;
384 aRet.Width = static_cast<sal_Int32>(rDirection.DirectionX);
385 aRet.Height = static_cast<sal_Int32>(rDirection.DirectionY);
386 return aRet;
389 uno::Sequence< double > B3DPointToSequence( const ::basegfx::B3DPoint& rPoint )
391 uno::Sequence< double > aRet(3);
392 aRet[0] = rPoint.getX();
393 aRet[1] = rPoint.getY();
394 aRet[2] = rPoint.getZ();
395 return aRet;
398 drawing::Position3D SequenceToPosition3D( const uno::Sequence< double >& rSeq )
400 OSL_ENSURE(rSeq.getLength()==3,"The sequence needs to have length 3 for conversion into vector");
402 drawing::Position3D aRet;
403 aRet.PositionX = rSeq.getLength()>0?rSeq[0]:0.0;
404 aRet.PositionY = rSeq.getLength()>1?rSeq[1]:0.0;
405 aRet.PositionZ = rSeq.getLength()>2?rSeq[2]:0.0;
406 return aRet;
409 uno::Sequence< double > Position3DToSequence( const drawing::Position3D& rPosition )
411 uno::Sequence< double > aRet(3);
412 aRet[0] = rPosition.PositionX;
413 aRet[1] = rPosition.PositionY;
414 aRet[2] = rPosition.PositionZ;
415 return aRet;
418 using namespace ::com::sun::star::chart2;
420 uno::Sequence< double > DataSequenceToDoubleSequence(
421 const uno::Reference< data::XDataSequence >& xDataSequence )
423 uno::Sequence< double > aResult;
424 OSL_ASSERT( xDataSequence.is());
425 if(!xDataSequence.is())
426 return aResult;
428 uno::Reference< data::XNumericalDataSequence > xNumericalDataSequence( xDataSequence, uno::UNO_QUERY );
429 if( xNumericalDataSequence.is() )
431 aResult = xNumericalDataSequence->getNumericalData();
433 else
435 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
436 aResult.realloc(aValues.getLength());
437 for(sal_Int32 nN=aValues.getLength();nN--;)
439 if( !(aValues[nN] >>= aResult[nN]) )
440 ::rtl::math::setNan( &aResult[nN] );
444 return aResult;
447 uno::Sequence< OUString > DataSequenceToStringSequence(
448 const uno::Reference< data::XDataSequence >& xDataSequence )
450 uno::Sequence< OUString > aResult;
451 if(!xDataSequence.is())
452 return aResult;
454 uno::Reference< data::XTextualDataSequence > xTextualDataSequence( xDataSequence, uno::UNO_QUERY );
455 if( xTextualDataSequence.is() )
457 aResult = xTextualDataSequence->getTextualData();
459 else
461 uno::Sequence< uno::Any > aValues = xDataSequence->getData();
462 aResult.realloc(aValues.getLength());
464 for(sal_Int32 nN=aValues.getLength();nN--;)
465 aValues[nN] >>= aResult[nN];
468 return aResult;
471 bool hasDoubleValue( const uno::Any& rAny )
473 bool bRet = false;
474 double fValue = 0.0;
475 if( rAny >>= fValue )
476 bRet = true;
477 return bRet;
480 bool hasLongOrShortValue( const uno::Any& rAny )
482 bool bRet = false;
483 sal_Int32 n32 = 0;
484 if( rAny >>= n32 )
485 bRet = true;
486 else
488 sal_Int16 n16 = 0;
489 if( rAny >>= n16 )
490 bRet = true;
492 return bRet;
494 sal_Int16 getShortForLongAlso( const uno::Any& rAny )
496 sal_Int16 nRet = 0;
498 if( !(rAny >>= nRet) )
500 sal_Int32 n32 = 0;
501 if( rAny >>= n32 )
502 nRet = static_cast<sal_Int16>(n32);
504 return nRet;
507 bool replaceParamterInString( OUString & rInOutResourceString,
508 const OUString & rParamToReplace,
509 const OUString & rReplaceWith )
511 sal_Int32 nPos = rInOutResourceString.indexOf( rParamToReplace );
512 if( nPos == -1 )
513 return false;
515 rInOutResourceString = rInOutResourceString.replaceAt( nPos
516 , rParamToReplace.getLength(), rReplaceWith );
517 return true;
520 } //namespace chart
522 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */