1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <vcl/msgbox.hxx>
24 #include <svx/svdpool.hxx>
25 #include <tools/poly.hxx>
26 #include <svx/svdopath.hxx>
27 #include <svx/svdogrp.hxx>
28 #include <editeng/eeitem.hxx>
31 #include "ViewShell.hxx"
33 #include <basegfx/polygon/b2dpolygontools.hxx>
34 #include <basegfx/polygon/b2dpolypolygontools.hxx>
35 #include <basegfx/matrix/b2dhommatrix.hxx>
36 #include <basegfx/matrix/b2dhommatrixtools.hxx>
38 #include "strings.hrc"
39 #include "sdresid.hxx"
41 #include "sdabstdlg.hxx"
43 #include <svx/svditer.hxx>
45 #include <basegfx/color/bcolor.hxx>
46 #include <boost/scoped_ptr.hpp>
47 #include <com/sun/star/drawing/LineStyle.hpp>
49 using namespace com::sun::star
;
53 #define ITEMVALUE( ItemSet, Id, Cast ) ( static_cast<const Cast&>( (ItemSet).Get( (Id) ) ).GetValue() )
54 TYPEINIT1( FuMorph
, FuPoor
);
62 : FuPoor(pViewSh
, pWin
, pView
, pDoc
, rReq
)
66 rtl::Reference
<FuPoor
> FuMorph::Create(
74 rtl::Reference
<FuPoor
> xFunc( new FuMorph( pViewSh
, pWin
, pView
, pDoc
, rReq
) );
75 xFunc
->DoExecute(rReq
);
79 void FuMorph::DoExecute( SfxRequest
& )
81 const SdrMarkList
& rMarkList
= mpView
->GetMarkedObjectList();
83 if(rMarkList
.GetMarkCount() == 2)
86 SdrObject
* pObj1
= rMarkList
.GetMark(0)->GetMarkedSdrObj();
87 SdrObject
* pObj2
= rMarkList
.GetMark(1)->GetMarkedSdrObj();
88 SdrObject
* pCloneObj1
= pObj1
->Clone();
89 SdrObject
* pCloneObj2
= pObj2
->Clone();
91 // delete text at clone, otherwise we do net get a correct PathObj
92 pCloneObj1
->SetOutlinerParaObject(NULL
);
93 pCloneObj2
->SetOutlinerParaObject(NULL
);
95 // create path objects
96 SdrObject
* pPolyObj1
= pCloneObj1
->ConvertToPolyObj(false, false);
97 SdrObject
* pPolyObj2
= pCloneObj2
->ConvertToPolyObj(false, false);
98 SdAbstractDialogFactory
* pFact
= SdAbstractDialogFactory::Create();
99 boost::scoped_ptr
<AbstractMorphDlg
> pDlg(pFact
? pFact
->CreateMorphDlg( static_cast< vcl::Window
*>(mpWindow
), pObj1
, pObj2
) : 0);
100 if(pPolyObj1
&& pPolyObj2
&& pDlg
&& (pDlg
->Execute() == RET_OK
))
102 B2DPolyPolygonList_impl aPolyPolyList
;
103 ::basegfx::B2DPolyPolygon aPolyPoly1
;
104 ::basegfx::B2DPolyPolygon aPolyPoly2
;
106 pDlg
->SaveSettings();
108 // #i48168# Not always is the pPolyObj1/pPolyObj2 a SdrPathObj, it may also be a group object
109 // containing SdrPathObjs. To get the polygons, i add two iters here
110 SdrObjListIter
aIter1(*pPolyObj1
);
111 SdrObjListIter
aIter2(*pPolyObj2
);
113 while(aIter1
.IsMore())
115 SdrObject
* pObj
= aIter1
.Next();
116 if(pObj
&& pObj
->ISA(SdrPathObj
))
117 aPolyPoly1
.append(static_cast<SdrPathObj
*>(pObj
)->GetPathPoly());
120 while(aIter2
.IsMore())
122 SdrObject
* pObj
= aIter2
.Next();
123 if(pObj
&& pObj
->ISA(SdrPathObj
))
124 aPolyPoly2
.append(static_cast<SdrPathObj
*>(pObj
)->GetPathPoly());
128 if(aPolyPoly1
.count() && aPolyPoly2
.count())
130 aPolyPoly1
= ::basegfx::tools::correctOrientations(aPolyPoly1
);
131 aPolyPoly1
.removeDoublePoints();
132 ::basegfx::B2VectorOrientation
eIsClockwise1(::basegfx::tools::getOrientation(aPolyPoly1
.getB2DPolygon(0L)));
134 aPolyPoly2
= ::basegfx::tools::correctOrientations(aPolyPoly2
);
135 aPolyPoly2
.removeDoublePoints();
136 ::basegfx::B2VectorOrientation
eIsClockwise2(::basegfx::tools::getOrientation(aPolyPoly2
.getB2DPolygon(0L)));
138 // set same orientation
139 if(eIsClockwise1
!= eIsClockwise2
)
142 // force same poly count
143 if(aPolyPoly1
.count() < aPolyPoly2
.count())
144 ImpAddPolys(aPolyPoly1
, aPolyPoly2
);
145 else if(aPolyPoly2
.count() < aPolyPoly1
.count())
146 ImpAddPolys(aPolyPoly2
, aPolyPoly1
);
148 // use orientation flag from dialog
149 if(!pDlg
->IsOrientationFade())
152 // force same point counts
153 for( sal_uInt32
a(0L); a
< aPolyPoly1
.count(); a
++ )
155 ::basegfx::B2DPolygon
aSub1(aPolyPoly1
.getB2DPolygon(a
));
156 ::basegfx::B2DPolygon
aSub2(aPolyPoly2
.getB2DPolygon(a
));
158 if(aSub1
.count() < aSub2
.count())
159 ImpEqualizePolyPointCount(aSub1
, aSub2
);
160 else if(aSub2
.count() < aSub1
.count())
161 ImpEqualizePolyPointCount(aSub2
, aSub1
);
163 aPolyPoly1
.setB2DPolygon(a
, aSub1
);
164 aPolyPoly2
.setB2DPolygon(a
, aSub2
);
167 if(ImpMorphPolygons(aPolyPoly1
, aPolyPoly2
, pDlg
->GetFadeSteps(), aPolyPolyList
))
169 OUString
aString(mpView
->GetDescriptionOfMarkedObjects());
170 aString
+= " " + SD_RESSTR(STR_UNDO_MORPHING
);
172 mpView
->BegUndo(aString
);
173 ImpInsertPolygons(aPolyPolyList
, pDlg
->IsAttributeFade(), pObj1
, pObj2
);
177 for( size_t i
= 0, n
= aPolyPolyList
.size(); i
< n
; ++i
) {
178 delete aPolyPolyList
[ i
];
182 SdrObject::Free( pCloneObj1
);
183 SdrObject::Free( pCloneObj2
);
185 SdrObject::Free( pPolyObj1
);
186 SdrObject::Free( pPolyObj2
);
190 ::basegfx::B2DPolygon
ImpGetExpandedPolygon(
191 const ::basegfx::B2DPolygon
& rCandidate
,
195 if(rCandidate
.count() && nNum
&& rCandidate
.count() != nNum
)
197 // length of step in dest poly
198 ::basegfx::B2DPolygon aRetval
;
199 const double fStep(::basegfx::tools::getLength(rCandidate
) / (double)(rCandidate
.isClosed() ? nNum
: nNum
- 1L));
200 double fDestPos(0.0);
202 sal_uInt32
nSrcPos(0L);
203 sal_uInt32
nSrcPosNext((nSrcPos
+ 1L == rCandidate
.count()) ? 0L : nSrcPos
+ 1L);
204 double fNextSrcLen(::basegfx::B2DVector(rCandidate
.getB2DPoint(nSrcPos
) - rCandidate
.getB2DPoint(nSrcPosNext
)).getLength());
206 for(sal_uInt32
b(0L); b
< nNum
; b
++)
208 // calc fDestPos in source
209 while(fSrcPos
+ fNextSrcLen
< fDestPos
)
211 fSrcPos
+= fNextSrcLen
;
213 nSrcPosNext
= (nSrcPos
+ 1L == rCandidate
.count()) ? 0L : nSrcPos
+ 1L;
214 fNextSrcLen
= ::basegfx::B2DVector(rCandidate
.getB2DPoint(nSrcPos
) - rCandidate
.getB2DPoint(nSrcPosNext
)).getLength();
217 // fDestPos is between fSrcPos and (fSrcPos + fNextSrcLen)
218 const double fLenA((fDestPos
- fSrcPos
) / fNextSrcLen
);
219 const ::basegfx::B2DPoint
aOld1(rCandidate
.getB2DPoint(nSrcPos
));
220 const ::basegfx::B2DPoint
aOld2(rCandidate
.getB2DPoint(nSrcPosNext
));
221 ::basegfx::B2DPoint
aNewPoint(basegfx::interpolate(aOld1
, aOld2
, fLenA
));
222 aRetval
.append(aNewPoint
);
228 if(aRetval
.count() >= 3L)
230 aRetval
.setClosed(rCandidate
.isClosed());
242 * make the point count of the polygons equal in adding points
244 void FuMorph::ImpEqualizePolyPointCount(
245 ::basegfx::B2DPolygon
& rSmall
,
246 const ::basegfx::B2DPolygon
& rBig
249 // create poly with equal point count
250 const sal_uInt32
nCnt(rBig
.count());
251 ::basegfx::B2DPolygon
aPoly1(ImpGetExpandedPolygon(rSmall
, nCnt
));
253 // create transformation for rBig to do the compare
254 const ::basegfx::B2DRange
aSrcSize(::basegfx::tools::getRange(rBig
));
255 const ::basegfx::B2DPoint
aSrcPos(aSrcSize
.getCenter());
256 const ::basegfx::B2DRange
aDstSize(::basegfx::tools::getRange(rSmall
));
257 const ::basegfx::B2DPoint
aDstPos(aDstSize
.getCenter());
259 basegfx::B2DHomMatrix
aTrans(basegfx::tools::createTranslateB2DHomMatrix(-aSrcPos
.getX(), -aSrcPos
.getY()));
260 aTrans
.scale(aDstSize
.getWidth() / aSrcSize
.getWidth(), aDstSize
.getHeight() / aSrcSize
.getHeight());
261 aTrans
.translate(aDstPos
.getX(), aDstPos
.getY());
263 // transpose points to have smooth linear blending
264 ::basegfx::B2DPolygon aPoly2
;
265 aPoly2
.append(::basegfx::B2DPoint(), nCnt
);
266 sal_uInt32
nInd(ImpGetNearestIndex(aPoly1
, aTrans
* rBig
.getB2DPoint(0L)));
268 for(sal_uInt32
a(0L); a
< nCnt
; a
++)
270 aPoly2
.setB2DPoint((a
+ nCnt
- nInd
) % nCnt
, aPoly1
.getB2DPoint(a
));
273 aPoly2
.setClosed(rBig
.isClosed());
277 sal_uInt32
FuMorph::ImpGetNearestIndex(
278 const ::basegfx::B2DPolygon
& rPoly
,
279 const ::basegfx::B2DPoint
& rPos
282 double fMinDist
= 0.0;
283 sal_uInt32 nActInd
= 0;
285 for(sal_uInt32
a(0L); a
< rPoly
.count(); a
++)
287 double fNewDist(::basegfx::B2DVector(rPoly
.getB2DPoint(a
) - rPos
).getLength());
289 if(!a
|| fNewDist
< fMinDist
)
300 * add to a point reduced polys until count is same
302 void FuMorph::ImpAddPolys(
303 ::basegfx::B2DPolyPolygon
& rSmaller
,
304 const ::basegfx::B2DPolyPolygon
& rBigger
307 while(rSmaller
.count() < rBigger
.count())
309 const ::basegfx::B2DPolygon
aToBeCopied(rBigger
.getB2DPolygon(rSmaller
.count()));
310 const ::basegfx::B2DRange
aToBeCopiedPolySize(::basegfx::tools::getRange(aToBeCopied
));
311 ::basegfx::B2DPoint
aNewPoint(aToBeCopiedPolySize
.getCenter());
312 ::basegfx::B2DPolygon aNewPoly
;
314 const ::basegfx::B2DRange
aSrcSize(::basegfx::tools::getRange(rBigger
.getB2DPolygon(0L)));
315 const ::basegfx::B2DPoint
aSrcPos(aSrcSize
.getCenter());
316 const ::basegfx::B2DRange
aDstSize(::basegfx::tools::getRange(rSmaller
.getB2DPolygon(0L)));
317 const ::basegfx::B2DPoint
aDstPos(aDstSize
.getCenter());
318 aNewPoint
= aNewPoint
- aSrcPos
+ aDstPos
;
320 for(sal_uInt32
a(0L); a
< aToBeCopied
.count(); a
++)
322 aNewPoly
.append(aNewPoint
);
325 rSmaller
.append(aNewPoly
);
330 * create group object with morphed polygons
332 void FuMorph::ImpInsertPolygons(
333 B2DPolyPolygonList_impl
& rPolyPolyList3D
,
335 const SdrObject
* pObj1
,
336 const SdrObject
* pObj2
343 long nStartLineWidth
= 0;
344 long nEndLineWidth
= 0;
345 SdrPageView
* pPageView
= mpView
->GetSdrPageView();
346 SfxItemPool
& pPool
= pObj1
->GetObjectItemPool();
347 SfxItemSet
aSet1( pPool
,SDRATTR_START
,SDRATTR_NOTPERSIST_FIRST
-1,EE_ITEMS_START
,EE_ITEMS_END
,0 );
348 SfxItemSet
aSet2( aSet1
);
349 bool bLineColor
= false;
350 bool bFillColor
= false;
351 bool bLineWidth
= false;
352 bool bIgnoreLine
= false;
353 bool bIgnoreFill
= false;
355 aSet1
.Put(pObj1
->GetMergedItemSet());
356 aSet2
.Put(pObj2
->GetMergedItemSet());
358 const drawing::LineStyle eLineStyle1
= ITEMVALUE( aSet1
, XATTR_LINESTYLE
, XLineStyleItem
);
359 const drawing::LineStyle eLineStyle2
= ITEMVALUE( aSet2
, XATTR_LINESTYLE
, XLineStyleItem
);
360 const drawing::FillStyle eFillStyle1
= ITEMVALUE( aSet1
, XATTR_FILLSTYLE
, XFillStyleItem
);
361 const drawing::FillStyle eFillStyle2
= ITEMVALUE( aSet2
, XATTR_FILLSTYLE
, XFillStyleItem
);
363 if ( bAttributeFade
)
365 if ( ( eLineStyle1
!= drawing::LineStyle_NONE
) && ( eLineStyle2
!= drawing::LineStyle_NONE
) )
367 bLineWidth
= bLineColor
= true;
369 aStartLineCol
= static_cast< XLineColorItem
const & >(
370 aSet1
.Get(XATTR_LINECOLOR
)).GetColorValue();
371 aEndLineCol
= static_cast< XLineColorItem
const & >(
372 aSet2
.Get(XATTR_LINECOLOR
)).GetColorValue();
374 nStartLineWidth
= ITEMVALUE( aSet1
, XATTR_LINEWIDTH
, XLineWidthItem
);
375 nEndLineWidth
= ITEMVALUE( aSet2
, XATTR_LINEWIDTH
, XLineWidthItem
);
377 else if ( ( eLineStyle1
== drawing::LineStyle_NONE
) && ( eLineStyle2
== drawing::LineStyle_NONE
) )
380 if ( ( eFillStyle1
== drawing::FillStyle_SOLID
) && ( eFillStyle2
== drawing::FillStyle_SOLID
) )
383 aStartFillCol
= static_cast< XFillColorItem
const & >(
384 aSet1
.Get(XATTR_FILLCOLOR
)).GetColorValue();
385 aEndFillCol
= static_cast< XFillColorItem
const & >(
386 aSet2
.Get(XATTR_FILLCOLOR
)).GetColorValue();
388 else if ( ( eFillStyle1
== drawing::FillStyle_NONE
) && ( eFillStyle2
== drawing::FillStyle_NONE
) )
394 SfxItemSet
aSet( aSet1
);
395 SdrObjGroup
* pObjGroup
= new SdrObjGroup
;
396 SdrObjList
* pObjList
= pObjGroup
->GetSubList();
397 const size_t nCount
= rPolyPolyList3D
.size();
398 const double fStep
= 1. / ( nCount
+ 1 );
399 const double fDelta
= nEndLineWidth
- nStartLineWidth
;
400 double fFactor
= fStep
;
402 aSet
.Put( XLineStyleItem( drawing::LineStyle_SOLID
) );
403 aSet
.Put( XFillStyleItem( drawing::FillStyle_SOLID
) );
405 for ( size_t i
= 0; i
< nCount
; i
++, fFactor
+= fStep
)
407 const ::basegfx::B2DPolyPolygon
& rPolyPoly3D
= *rPolyPolyList3D
[ i
];
408 SdrPathObj
* pNewObj
= new SdrPathObj(OBJ_POLY
, rPolyPoly3D
);
413 const basegfx::BColor
aLineColor(basegfx::interpolate(aStartLineCol
.getBColor(), aEndLineCol
.getBColor(), fFactor
));
414 aSet
.Put( XLineColorItem( aEmptyStr
, Color(aLineColor
)));
416 else if ( bIgnoreLine
)
417 aSet
.Put( XLineStyleItem( drawing::LineStyle_NONE
) );
422 const basegfx::BColor
aFillColor(basegfx::interpolate(aStartFillCol
.getBColor(), aEndFillCol
.getBColor(), fFactor
));
423 aSet
.Put( XFillColorItem( aEmptyStr
, Color(aFillColor
)));
425 else if ( bIgnoreFill
)
426 aSet
.Put( XFillStyleItem( drawing::FillStyle_NONE
) );
430 aSet
.Put( XLineWidthItem( nStartLineWidth
+ (long) ( fFactor
* fDelta
+ 0.5 ) ) );
432 pNewObj
->SetMergedItemSetAndBroadcast(aSet
);
434 pObjList
->InsertObject( pNewObj
);
439 pObjList
->InsertObject( pObj1
->Clone(), 0 );
440 pObjList
->InsertObject( pObj2
->Clone() );
441 mpView
->DeleteMarked();
442 mpView
->InsertObjectAtView( pObjGroup
, *pPageView
, SdrInsertFlags:: SETDEFLAYER
);
448 * create single morphed PolyPolygon
450 ::basegfx::B2DPolyPolygon
* FuMorph::ImpCreateMorphedPolygon(
451 const ::basegfx::B2DPolyPolygon
& rPolyPolyStart
,
452 const ::basegfx::B2DPolyPolygon
& rPolyPolyEnd
,
453 double fMorphingFactor
456 ::basegfx::B2DPolyPolygon
* pNewPolyPolygon
= new ::basegfx::B2DPolyPolygon();
457 const double fFactor
= 1.0 - fMorphingFactor
;
459 for(sal_uInt32
a(0L); a
< rPolyPolyStart
.count(); a
++)
461 const ::basegfx::B2DPolygon
aPolyStart(rPolyPolyStart
.getB2DPolygon(a
));
462 const ::basegfx::B2DPolygon
aPolyEnd(rPolyPolyEnd
.getB2DPolygon(a
));
463 const sal_uInt32
nCount(aPolyStart
.count());
464 ::basegfx::B2DPolygon aNewPolygon
;
466 for(sal_uInt32
b(0L); b
< nCount
; b
++)
468 const ::basegfx::B2DPoint
& aPtStart(aPolyStart
.getB2DPoint(b
));
469 const ::basegfx::B2DPoint
& aPtEnd(aPolyEnd
.getB2DPoint(b
));
470 aNewPolygon
.append(aPtEnd
+ ((aPtStart
- aPtEnd
) * fFactor
));
473 aNewPolygon
.setClosed(aPolyStart
.isClosed() && aPolyEnd
.isClosed());
474 pNewPolyPolygon
->append(aNewPolygon
);
477 return pNewPolyPolygon
;
481 * create morphed PolyPolygons
483 bool FuMorph::ImpMorphPolygons(
484 const ::basegfx::B2DPolyPolygon
& rPolyPoly1
,
485 const ::basegfx::B2DPolyPolygon
& rPolyPoly2
,
486 const sal_uInt16 nSteps
,
487 B2DPolyPolygonList_impl
& rPolyPolyList3D
492 const ::basegfx::B2DRange
aStartPolySize(::basegfx::tools::getRange(rPolyPoly1
));
493 const ::basegfx::B2DPoint
aStartCenter(aStartPolySize
.getCenter());
494 const ::basegfx::B2DRange
aEndPolySize(::basegfx::tools::getRange(rPolyPoly2
));
495 const ::basegfx::B2DPoint
aEndCenter(aEndPolySize
.getCenter());
496 const ::basegfx::B2DPoint
aDelta(aEndCenter
- aStartCenter
);
497 const double fFactor(1.0 / (nSteps
+ 1));
500 for(sal_uInt16
i(0); i
< nSteps
; i
++)
503 ::basegfx::B2DPolyPolygon
* pNewPolyPoly2D
= ImpCreateMorphedPolygon(rPolyPoly1
, rPolyPoly2
, fValue
);
505 const ::basegfx::B2DRange
aNewPolySize(::basegfx::tools::getRange(*pNewPolyPoly2D
));
506 const ::basegfx::B2DPoint
aNewS(aNewPolySize
.getCenter());
507 const ::basegfx::B2DPoint
aRealS(aStartCenter
+ (aDelta
* fValue
));
508 const ::basegfx::B2DPoint
aDiff(aRealS
- aNewS
);
510 pNewPolyPoly2D
->transform(basegfx::tools::createTranslateB2DHomMatrix(aDiff
));
511 rPolyPolyList3D
.push_back( pNewPolyPoly2D
);
517 } // end of namespace sd
519 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */