Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / ui / func / fumorph.cxx
blob6fa9494dfb7f5e6eeac64fd36508b4fa8439217a
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 <fumorph.hxx>
21 #include <svx/xfillit.hxx>
22 #include <svx/xlineit.hxx>
23 #include <svx/svdpool.hxx>
24 #include <tools/poly.hxx>
25 #include <svx/svdopath.hxx>
26 #include <svx/svdogrp.hxx>
27 #include <editeng/eeitem.hxx>
29 #include <View.hxx>
30 #include <ViewShell.hxx>
31 #include <Window.hxx>
32 #include <basegfx/polygon/b2dpolygontools.hxx>
33 #include <basegfx/polygon/b2dpolypolygontools.hxx>
34 #include <basegfx/matrix/b2dhommatrix.hxx>
35 #include <basegfx/matrix/b2dhommatrixtools.hxx>
37 #include <strings.hrc>
38 #include <sdresid.hxx>
40 #include <sdabstdlg.hxx>
42 #include <svx/svditer.hxx>
44 #include <basegfx/color/bcolor.hxx>
45 #include <com/sun/star/drawing/LineStyle.hpp>
46 #include <memory>
48 using namespace com::sun::star;
50 namespace sd {
52 FuMorph::FuMorph (
53 ViewShell* pViewSh,
54 ::sd::Window* pWin,
55 ::sd::View* pView,
56 SdDrawDocument* pDoc,
57 SfxRequest& rReq )
58 : FuPoor(pViewSh, pWin, pView, pDoc, rReq)
62 rtl::Reference<FuPoor> FuMorph::Create(
63 ViewShell* pViewSh,
64 ::sd::Window* pWin,
65 ::sd::View* pView,
66 SdDrawDocument* pDoc,
67 SfxRequest& rReq
70 rtl::Reference<FuPoor> xFunc( new FuMorph( pViewSh, pWin, pView, pDoc, rReq ) );
71 xFunc->DoExecute(rReq);
72 return xFunc;
75 void FuMorph::DoExecute( SfxRequest& )
77 const SdrMarkList& rMarkList = mpView->GetMarkedObjectList();
79 if(rMarkList.GetMarkCount() == 2)
81 // create clones
82 SdrObject* pObj1 = rMarkList.GetMark(0)->GetMarkedSdrObj();
83 SdrObject* pObj2 = rMarkList.GetMark(1)->GetMarkedSdrObj();
84 SdrObject* pCloneObj1(pObj1->CloneSdrObject(pObj1->getSdrModelFromSdrObject()));
85 SdrObject* pCloneObj2(pObj2->CloneSdrObject(pObj2->getSdrModelFromSdrObject()));
87 // delete text at clone, otherwise we do net get a correct PathObj
88 pCloneObj1->SetOutlinerParaObject(nullptr);
89 pCloneObj2->SetOutlinerParaObject(nullptr);
91 // create path objects
92 SdrObject* pPolyObj1 = pCloneObj1->ConvertToPolyObj(false, false);
93 SdrObject* pPolyObj2 = pCloneObj2->ConvertToPolyObj(false, false);
94 SdAbstractDialogFactory* pFact = SdAbstractDialogFactory::Create();
95 ScopedVclPtr<AbstractMorphDlg> pDlg(pFact ? pFact->CreateMorphDlg(mpWindow ? mpWindow->GetFrameWeld() : nullptr, pObj1, pObj2) : nullptr);
96 if(pPolyObj1 && pPolyObj2 && pDlg && (pDlg->Execute() == RET_OK))
98 B2DPolyPolygonList_impl aPolyPolyList;
99 ::basegfx::B2DPolyPolygon aPolyPoly1;
100 ::basegfx::B2DPolyPolygon aPolyPoly2;
102 pDlg->SaveSettings();
104 // #i48168# Not always is the pPolyObj1/pPolyObj2 a SdrPathObj, it may also be a group object
105 // containing SdrPathObjs. To get the polygons, I add two iters here
106 SdrObjListIter aIter1(*pPolyObj1);
107 SdrObjListIter aIter2(*pPolyObj2);
109 while(aIter1.IsMore())
111 SdrObject* pObj = aIter1.Next();
112 if(pObj && dynamic_cast< SdrPathObj *>( pObj ) != nullptr)
113 aPolyPoly1.append(static_cast<SdrPathObj*>(pObj)->GetPathPoly());
116 while(aIter2.IsMore())
118 SdrObject* pObj = aIter2.Next();
119 if(pObj && dynamic_cast< SdrPathObj *>( pObj ) != nullptr)
120 aPolyPoly2.append(static_cast<SdrPathObj*>(pObj)->GetPathPoly());
123 // perform morphing
124 if(aPolyPoly1.count() && aPolyPoly2.count())
126 aPolyPoly1 = ::basegfx::utils::correctOrientations(aPolyPoly1);
127 aPolyPoly1.removeDoublePoints();
128 ::basegfx::B2VectorOrientation eIsClockwise1(::basegfx::utils::getOrientation(aPolyPoly1.getB2DPolygon(0)));
130 aPolyPoly2 = ::basegfx::utils::correctOrientations(aPolyPoly2);
131 aPolyPoly2.removeDoublePoints();
132 ::basegfx::B2VectorOrientation eIsClockwise2(::basegfx::utils::getOrientation(aPolyPoly2.getB2DPolygon(0)));
134 // set same orientation
135 if(eIsClockwise1 != eIsClockwise2)
136 aPolyPoly2.flip();
138 // force same poly count
139 if(aPolyPoly1.count() < aPolyPoly2.count())
140 ImpAddPolys(aPolyPoly1, aPolyPoly2);
141 else if(aPolyPoly2.count() < aPolyPoly1.count())
142 ImpAddPolys(aPolyPoly2, aPolyPoly1);
144 // use orientation flag from dialog
145 if(!pDlg->IsOrientationFade())
146 aPolyPoly2.flip();
148 // force same point counts
149 for( sal_uInt32 a(0); a < aPolyPoly1.count(); a++ )
151 ::basegfx::B2DPolygon aSub1(aPolyPoly1.getB2DPolygon(a));
152 ::basegfx::B2DPolygon aSub2(aPolyPoly2.getB2DPolygon(a));
154 if(aSub1.count() < aSub2.count())
155 ImpEqualizePolyPointCount(aSub1, aSub2);
156 else if(aSub2.count() < aSub1.count())
157 ImpEqualizePolyPointCount(aSub2, aSub1);
159 aPolyPoly1.setB2DPolygon(a, aSub1);
160 aPolyPoly2.setB2DPolygon(a, aSub2);
163 if(ImpMorphPolygons(aPolyPoly1, aPolyPoly2, pDlg->GetFadeSteps(), aPolyPolyList))
165 OUString aString(mpView->GetDescriptionOfMarkedObjects());
166 aString += " " + SdResId(STR_UNDO_MORPHING);
168 mpView->BegUndo(aString);
169 ImpInsertPolygons(aPolyPolyList, pDlg->IsAttributeFade(), pObj1, pObj2);
170 mpView->EndUndo();
173 for(basegfx::B2DPolyPolygon * p : aPolyPolyList) {
174 delete p;
178 SdrObject::Free( pCloneObj1 );
179 SdrObject::Free( pCloneObj2 );
181 SdrObject::Free( pPolyObj1 );
182 SdrObject::Free( pPolyObj2 );
186 ::basegfx::B2DPolygon ImpGetExpandedPolygon(
187 const ::basegfx::B2DPolygon& rCandidate,
188 sal_uInt32 nNum
191 if(rCandidate.count() && nNum && rCandidate.count() != nNum)
193 // length of step in dest poly
194 ::basegfx::B2DPolygon aRetval;
195 const double fStep(::basegfx::utils::getLength(rCandidate) / static_cast<double>(rCandidate.isClosed() ? nNum : nNum - 1));
196 double fDestPos(0.0);
197 double fSrcPos(0.0);
198 sal_uInt32 nSrcPos(0);
199 sal_uInt32 nSrcPosNext((nSrcPos + 1 == rCandidate.count()) ? 0L : nSrcPos + 1);
200 double fNextSrcLen(::basegfx::B2DVector(rCandidate.getB2DPoint(nSrcPos) - rCandidate.getB2DPoint(nSrcPosNext)).getLength());
202 for(sal_uInt32 b(0); b < nNum; b++)
204 // calc fDestPos in source
205 while(fSrcPos + fNextSrcLen < fDestPos)
207 fSrcPos += fNextSrcLen;
208 nSrcPos++;
209 nSrcPosNext = (nSrcPos + 1 == rCandidate.count()) ? 0L : nSrcPos + 1;
210 fNextSrcLen = ::basegfx::B2DVector(rCandidate.getB2DPoint(nSrcPos) - rCandidate.getB2DPoint(nSrcPosNext)).getLength();
213 // fDestPos is between fSrcPos and (fSrcPos + fNextSrcLen)
214 const double fLenA((fDestPos - fSrcPos) / fNextSrcLen);
215 const ::basegfx::B2DPoint aOld1(rCandidate.getB2DPoint(nSrcPos));
216 const ::basegfx::B2DPoint aOld2(rCandidate.getB2DPoint(nSrcPosNext));
217 ::basegfx::B2DPoint aNewPoint(basegfx::interpolate(aOld1, aOld2, fLenA));
218 aRetval.append(aNewPoint);
220 // next step
221 fDestPos += fStep;
224 if(aRetval.count() >= 3)
226 aRetval.setClosed(rCandidate.isClosed());
229 return aRetval;
231 else
233 return rCandidate;
238 * make the point count of the polygons equal in adding points
240 void FuMorph::ImpEqualizePolyPointCount(
241 ::basegfx::B2DPolygon& rSmall,
242 const ::basegfx::B2DPolygon& rBig
245 // create poly with equal point count
246 const sal_uInt32 nCnt(rBig.count());
247 ::basegfx::B2DPolygon aPoly1(ImpGetExpandedPolygon(rSmall, nCnt));
249 // create transformation for rBig to do the compare
250 const ::basegfx::B2DRange aSrcSize(::basegfx::utils::getRange(rBig));
251 const ::basegfx::B2DPoint aSrcPos(aSrcSize.getCenter());
252 const ::basegfx::B2DRange aDstSize(::basegfx::utils::getRange(rSmall));
253 const ::basegfx::B2DPoint aDstPos(aDstSize.getCenter());
255 basegfx::B2DHomMatrix aTrans(basegfx::utils::createTranslateB2DHomMatrix(-aSrcPos.getX(), -aSrcPos.getY()));
256 aTrans.scale(aDstSize.getWidth() / aSrcSize.getWidth(), aDstSize.getHeight() / aSrcSize.getHeight());
257 aTrans.translate(aDstPos.getX(), aDstPos.getY());
259 // transpose points to have smooth linear blending
260 ::basegfx::B2DPolygon aPoly2;
261 aPoly2.append(::basegfx::B2DPoint(), nCnt);
262 sal_uInt32 nInd(ImpGetNearestIndex(aPoly1, aTrans * rBig.getB2DPoint(0)));
264 for(sal_uInt32 a(0); a < nCnt; a++)
266 aPoly2.setB2DPoint((a + nCnt - nInd) % nCnt, aPoly1.getB2DPoint(a));
269 aPoly2.setClosed(rBig.isClosed());
270 rSmall = aPoly2;
273 sal_uInt32 FuMorph::ImpGetNearestIndex(
274 const ::basegfx::B2DPolygon& rPoly,
275 const ::basegfx::B2DPoint& rPos
278 double fMinDist = 0.0;
279 sal_uInt32 nActInd = 0;
281 for(sal_uInt32 a(0); a < rPoly.count(); a++)
283 double fNewDist(::basegfx::B2DVector(rPoly.getB2DPoint(a) - rPos).getLength());
285 if(!a || fNewDist < fMinDist)
287 fMinDist = fNewDist;
288 nActInd = a;
292 return nActInd;
296 * add to a point reduced polys until count is same
298 void FuMorph::ImpAddPolys(
299 ::basegfx::B2DPolyPolygon& rSmaller,
300 const ::basegfx::B2DPolyPolygon& rBigger
303 while(rSmaller.count() < rBigger.count())
305 const ::basegfx::B2DPolygon aToBeCopied(rBigger.getB2DPolygon(rSmaller.count()));
306 const ::basegfx::B2DRange aToBeCopiedPolySize(::basegfx::utils::getRange(aToBeCopied));
307 ::basegfx::B2DPoint aNewPoint(aToBeCopiedPolySize.getCenter());
308 ::basegfx::B2DPolygon aNewPoly;
310 const ::basegfx::B2DRange aSrcSize(::basegfx::utils::getRange(rBigger.getB2DPolygon(0)));
311 const ::basegfx::B2DPoint aSrcPos(aSrcSize.getCenter());
312 const ::basegfx::B2DRange aDstSize(::basegfx::utils::getRange(rSmaller.getB2DPolygon(0)));
313 const ::basegfx::B2DPoint aDstPos(aDstSize.getCenter());
314 aNewPoint = aNewPoint - aSrcPos + aDstPos;
316 for(sal_uInt32 a(0); a < aToBeCopied.count(); a++)
318 aNewPoly.append(aNewPoint);
321 rSmaller.append(aNewPoly);
326 * create group object with morphed polygons
328 void FuMorph::ImpInsertPolygons(
329 B2DPolyPolygonList_impl& rPolyPolyList3D,
330 bool bAttributeFade,
331 const SdrObject* pObj1,
332 const SdrObject* pObj2
335 Color aStartFillCol;
336 Color aEndFillCol;
337 Color aStartLineCol;
338 Color aEndLineCol;
339 long nStartLineWidth = 0;
340 long nEndLineWidth = 0;
341 SdrPageView* pPageView = mpView->GetSdrPageView();
342 SfxItemPool & rPool = pObj1->GetObjectItemPool();
343 SfxItemSet aSet1( rPool,svl::Items<SDRATTR_START,SDRATTR_NOTPERSIST_FIRST-1,EE_ITEMS_START,EE_ITEMS_END>{} );
344 SfxItemSet aSet2( aSet1 );
345 bool bLineColor = false;
346 bool bFillColor = false;
347 bool bLineWidth = false;
348 bool bIgnoreLine = false;
349 bool bIgnoreFill = false;
351 aSet1.Put(pObj1->GetMergedItemSet());
352 aSet2.Put(pObj2->GetMergedItemSet());
354 const drawing::LineStyle eLineStyle1 = aSet1.Get(XATTR_LINESTYLE).GetValue();
355 const drawing::LineStyle eLineStyle2 = aSet2.Get(XATTR_LINESTYLE).GetValue();
356 const drawing::FillStyle eFillStyle1 = aSet1.Get(XATTR_FILLSTYLE).GetValue();
357 const drawing::FillStyle eFillStyle2 = aSet2.Get(XATTR_FILLSTYLE).GetValue();
359 if ( bAttributeFade )
361 if ( ( eLineStyle1 != drawing::LineStyle_NONE ) && ( eLineStyle2 != drawing::LineStyle_NONE ) )
363 bLineWidth = bLineColor = true;
365 aStartLineCol = aSet1.Get(XATTR_LINECOLOR).GetColorValue();
366 aEndLineCol = aSet2.Get(XATTR_LINECOLOR).GetColorValue();
368 nStartLineWidth = aSet1.Get(XATTR_LINEWIDTH).GetValue();
369 nEndLineWidth = aSet2.Get(XATTR_LINEWIDTH).GetValue();
371 else if ( ( eLineStyle1 == drawing::LineStyle_NONE ) && ( eLineStyle2 == drawing::LineStyle_NONE ) )
372 bIgnoreLine = true;
374 if ( ( eFillStyle1 == drawing::FillStyle_SOLID ) && ( eFillStyle2 == drawing::FillStyle_SOLID ) )
376 bFillColor = true;
377 aStartFillCol = aSet1.Get(XATTR_FILLCOLOR).GetColorValue();
378 aEndFillCol = aSet2.Get(XATTR_FILLCOLOR).GetColorValue();
380 else if ( ( eFillStyle1 == drawing::FillStyle_NONE ) && ( eFillStyle2 == drawing::FillStyle_NONE ) )
381 bIgnoreFill = true;
384 if ( pPageView )
386 SfxItemSet aSet( aSet1 );
387 SdrObjGroup* pObjGroup = new SdrObjGroup(mpView->getSdrModelFromSdrView());
388 SdrObjList* pObjList = pObjGroup->GetSubList();
389 const size_t nCount = rPolyPolyList3D.size();
390 const double fStep = 1. / ( nCount + 1 );
391 const double fDelta = nEndLineWidth - nStartLineWidth;
392 double fFactor = fStep;
394 aSet.Put( XLineStyleItem( drawing::LineStyle_SOLID ) );
395 aSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
397 for ( size_t i = 0; i < nCount; i++, fFactor += fStep )
399 const ::basegfx::B2DPolyPolygon& rPolyPoly3D = *rPolyPolyList3D[ i ];
400 SdrPathObj* pNewObj = new SdrPathObj(
401 mpView->getSdrModelFromSdrView(),
402 OBJ_POLY,
403 rPolyPoly3D);
405 // line color
406 if ( bLineColor )
408 const basegfx::BColor aLineColor(basegfx::interpolate(aStartLineCol.getBColor(), aEndLineCol.getBColor(), fFactor));
409 aSet.Put( XLineColorItem( "", Color(aLineColor)));
411 else if ( bIgnoreLine )
412 aSet.Put( XLineStyleItem( drawing::LineStyle_NONE ) );
414 // fill color
415 if ( bFillColor )
417 const basegfx::BColor aFillColor(basegfx::interpolate(aStartFillCol.getBColor(), aEndFillCol.getBColor(), fFactor));
418 aSet.Put( XFillColorItem( "", Color(aFillColor)));
420 else if ( bIgnoreFill )
421 aSet.Put( XFillStyleItem( drawing::FillStyle_NONE ) );
423 // line width
424 if ( bLineWidth )
425 aSet.Put( XLineWidthItem( nStartLineWidth + static_cast<long>( fFactor * fDelta + 0.5 ) ) );
427 pNewObj->SetMergedItemSetAndBroadcast(aSet);
429 pObjList->InsertObject( pNewObj );
432 if ( nCount )
434 pObjList->InsertObject(
435 pObj1->CloneSdrObject(pObj1->getSdrModelFromSdrObject()),
436 0 );
437 pObjList->InsertObject(
438 pObj2->CloneSdrObject(pObj2->getSdrModelFromSdrObject()) );
440 mpView->DeleteMarked();
441 mpView->InsertObjectAtView( pObjGroup, *pPageView, SdrInsertFlags:: SETDEFLAYER );
447 * create single morphed PolyPolygon
449 ::basegfx::B2DPolyPolygon* FuMorph::ImpCreateMorphedPolygon(
450 const ::basegfx::B2DPolyPolygon& rPolyPolyStart,
451 const ::basegfx::B2DPolyPolygon& rPolyPolyEnd,
452 double fMorphingFactor
455 ::basegfx::B2DPolyPolygon* pNewPolyPolygon = new ::basegfx::B2DPolyPolygon();
456 const double fFactor = 1.0 - fMorphingFactor;
458 for(sal_uInt32 a(0); a < rPolyPolyStart.count(); a++)
460 const ::basegfx::B2DPolygon aPolyStart(rPolyPolyStart.getB2DPolygon(a));
461 const ::basegfx::B2DPolygon aPolyEnd(rPolyPolyEnd.getB2DPolygon(a));
462 const sal_uInt32 nCount(aPolyStart.count());
463 ::basegfx::B2DPolygon aNewPolygon;
465 for(sal_uInt32 b(0); b < nCount; b++)
467 const ::basegfx::B2DPoint& aPtStart(aPolyStart.getB2DPoint(b));
468 const ::basegfx::B2DPoint& aPtEnd(aPolyEnd.getB2DPoint(b));
469 aNewPolygon.append(aPtEnd + ((aPtStart - aPtEnd) * fFactor));
472 aNewPolygon.setClosed(aPolyStart.isClosed() && aPolyEnd.isClosed());
473 pNewPolyPolygon->append(aNewPolygon);
476 return pNewPolyPolygon;
480 * create morphed PolyPolygons
482 bool FuMorph::ImpMorphPolygons(
483 const ::basegfx::B2DPolyPolygon& rPolyPoly1,
484 const ::basegfx::B2DPolyPolygon& rPolyPoly2,
485 const sal_uInt16 nSteps,
486 B2DPolyPolygonList_impl& rPolyPolyList3D
489 if(nSteps)
491 const ::basegfx::B2DRange aStartPolySize(::basegfx::utils::getRange(rPolyPoly1));
492 const ::basegfx::B2DPoint aStartCenter(aStartPolySize.getCenter());
493 const ::basegfx::B2DRange aEndPolySize(::basegfx::utils::getRange(rPolyPoly2));
494 const ::basegfx::B2DPoint aEndCenter(aEndPolySize.getCenter());
495 const ::basegfx::B2DPoint aDelta(aEndCenter - aStartCenter);
496 const double fFactor(1.0 / (nSteps + 1));
497 double fValue(0.0);
499 for(sal_uInt16 i(0); i < nSteps; i++)
501 fValue += fFactor;
502 ::basegfx::B2DPolyPolygon* pNewPolyPoly2D = ImpCreateMorphedPolygon(rPolyPoly1, rPolyPoly2, fValue);
504 const ::basegfx::B2DRange aNewPolySize(::basegfx::utils::getRange(*pNewPolyPoly2D));
505 const ::basegfx::B2DPoint aNewS(aNewPolySize.getCenter());
506 const ::basegfx::B2DPoint aRealS(aStartCenter + (aDelta * fValue));
507 const ::basegfx::B2DPoint aDiff(aRealS - aNewS);
509 pNewPolyPoly2D->transform(basegfx::utils::createTranslateB2DHomMatrix(aDiff));
510 rPolyPolyList3D.push_back( pNewPolyPoly2D );
513 return true;
516 } // end of namespace sd
518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */