fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / svx / source / engine3d / scene3d.cxx
blob2876b4d0b6b9a9f86bfd14d904398182da990bee
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 .
21 #include "svx/svdstr.hrc"
22 #include "svdglob.hxx"
23 #include "svx/svditer.hxx"
25 #include <stdlib.h>
26 #include <svx/globl3d.hxx>
27 #include <svx/svdpage.hxx>
28 #include <svl/style.hxx>
29 #include <svx/scene3d.hxx>
30 #include <svx/e3dundo.hxx>
31 #include <svx/svdtrans.hxx>
32 #include <svx/svxids.hrc>
33 #include <editeng/colritem.hxx>
34 #include <svx/e3ditem.hxx>
35 #include <svx/xlntrit.hxx>
36 #include <svx/xfltrit.hxx>
37 #include <svx/svx3ditems.hxx>
38 #include <svl/whiter.hxx>
39 #include <svx/xflftrit.hxx>
40 #include <sdr/properties/e3dsceneproperties.hxx>
41 #include <svx/sdr/contact/viewcontactofe3dscene.hxx>
42 #include <svx/svddrag.hxx>
43 #include <helperminimaldepth3d.hxx>
44 #include <algorithm>
45 #include <drawinglayer/geometry/viewinformation3d.hxx>
46 #include <basegfx/polygon/b2dpolypolygontools.hxx>
47 #include <svx/e3dsceneupdater.hxx>
48 #include <svx/svdmodel.hxx>
52 class ImpRemap3DDepth
54 sal_uInt32 mnOrdNum;
55 double mfMinimalDepth;
57 // bit field
58 bool mbIsScene : 1;
60 public:
61 ImpRemap3DDepth(sal_uInt32 nOrdNum, double fMinimalDepth);
62 ImpRemap3DDepth(sal_uInt32 nOrdNum);
63 ~ImpRemap3DDepth();
65 // for ::std::sort
66 bool operator<(const ImpRemap3DDepth& rComp) const;
68 sal_uInt32 GetOrdNum() const { return mnOrdNum; }
69 bool IsScene() const { return mbIsScene; }
72 ImpRemap3DDepth::ImpRemap3DDepth(sal_uInt32 nOrdNum, double fMinimalDepth)
73 : mnOrdNum(nOrdNum),
74 mfMinimalDepth(fMinimalDepth),
75 mbIsScene(false)
79 ImpRemap3DDepth::ImpRemap3DDepth(sal_uInt32 nOrdNum)
80 : mnOrdNum(nOrdNum),
81 mfMinimalDepth(0.0),
82 mbIsScene(true)
86 ImpRemap3DDepth::~ImpRemap3DDepth()
90 bool ImpRemap3DDepth::operator<(const ImpRemap3DDepth& rComp) const
92 if(IsScene())
94 return false;
96 else
98 if(rComp.IsScene())
100 return true;
102 else
104 return mfMinimalDepth < rComp.mfMinimalDepth;
109 // typedefs for a vector of ImpRemap3DDepths
110 typedef ::std::vector< ImpRemap3DDepth > ImpRemap3DDepthVector;
114 class Imp3DDepthRemapper
116 ImpRemap3DDepthVector maVector;
118 public:
119 Imp3DDepthRemapper(E3dScene& rScene);
120 ~Imp3DDepthRemapper();
122 sal_uInt32 RemapOrdNum(sal_uInt32 nOrdNum) const;
125 Imp3DDepthRemapper::Imp3DDepthRemapper(E3dScene& rScene)
127 // only called when rScene.GetSubList() and nObjCount > 1L
128 SdrObjList* pList = rScene.GetSubList();
129 const size_t nObjCount(pList->GetObjCount());
131 for(size_t a = 0; a < nObjCount; ++a)
133 SdrObject* pCandidate = pList->GetObj(a);
135 if(pCandidate)
137 if(pCandidate->ISA(E3dCompoundObject))
139 // single 3d object, calc depth
140 const double fMinimalDepth(getMinimalDepthInViewCoordinates(static_cast< const E3dCompoundObject& >(*pCandidate)));
141 ImpRemap3DDepth aEntry(a, fMinimalDepth);
142 maVector.push_back(aEntry);
144 else
146 // scene, use standard entry for scene
147 ImpRemap3DDepth aEntry(a);
148 maVector.push_back(aEntry);
153 // now, we need to sort the maVector by it's members minimal depth. The
154 // smaller, the nearer to the viewer.
155 ::std::sort(maVector.begin(), maVector.end());
158 Imp3DDepthRemapper::~Imp3DDepthRemapper()
162 sal_uInt32 Imp3DDepthRemapper::RemapOrdNum(sal_uInt32 nOrdNum) const
164 if(nOrdNum < maVector.size())
166 nOrdNum = maVector[(maVector.size() - 1) - nOrdNum].GetOrdNum();
169 return nOrdNum;
173 // BaseProperties section
175 sdr::properties::BaseProperties* E3dScene::CreateObjectSpecificProperties()
177 return new sdr::properties::E3dSceneProperties(*this);
181 // DrawContact section
183 sdr::contact::ViewContact* E3dScene::CreateObjectSpecificViewContact()
185 return new sdr::contact::ViewContactOfE3dScene(*this);
190 TYPEINIT1(E3dScene, E3dObject);
192 E3dScene::E3dScene()
193 : E3dObject(),
194 aCamera(basegfx::B3DPoint(0.0, 0.0, 4.0), basegfx::B3DPoint()),
195 mp3DDepthRemapper(0L),
196 bDrawOnlySelected(false)
198 // Set defaults
199 E3dDefaultAttributes aDefault;
200 SetDefaultAttributes(aDefault);
203 E3dScene::E3dScene(E3dDefaultAttributes& rDefault)
204 : E3dObject(),
205 aCamera(basegfx::B3DPoint(0.0, 0.0, 4.0), basegfx::B3DPoint()),
206 mp3DDepthRemapper(0L),
207 bDrawOnlySelected(false)
209 // Set defaults
210 SetDefaultAttributes(rDefault);
213 void E3dScene::SetDefaultAttributes(E3dDefaultAttributes& /*rDefault*/)
215 // For WIN95/NT turn off the FP-Exceptions
216 #if defined(WNT)
217 _control87( _MCW_EM, _MCW_EM );
218 #endif
220 // Set defaults
221 aCamera.SetViewWindow(-2, -2, 4, 4);
222 aCameraSet.SetDeviceRectangle(-2, 2, -2, 2);
223 aCamera.SetDeviceWindow(Rectangle(0, 0, 10, 10));
224 Rectangle aRect(0, 0, 10, 10);
225 aCameraSet.SetViewportRectangle(aRect);
227 // set defaults for Camera from ItemPool
228 aCamera.SetProjection(GetPerspective());
229 basegfx::B3DPoint aActualPosition(aCamera.GetPosition());
230 double fNew = GetDistance();
232 if(fabs(fNew - aActualPosition.getZ()) > 1.0)
234 aCamera.SetPosition( basegfx::B3DPoint( aActualPosition.getX(), aActualPosition.getY(), fNew) );
237 fNew = GetFocalLength() / 100.0;
238 aCamera.SetFocalLength(fNew);
241 E3dScene::~E3dScene()
243 ImpCleanup3DDepthMapper();
246 basegfx::B2DPolyPolygon E3dScene::TakeXorPoly() const
248 const sdr::contact::ViewContactOfE3dScene& rVCScene = static_cast< sdr::contact::ViewContactOfE3dScene& >(GetViewContact());
249 const drawinglayer::geometry::ViewInformation3D aViewInfo3D(rVCScene.getViewInformation3D());
250 const basegfx::B3DPolyPolygon aCubePolyPolygon(CreateWireframe());
252 basegfx::B2DPolyPolygon aRetval(basegfx::tools::createB2DPolyPolygonFromB3DPolyPolygon(aCubePolyPolygon,
253 aViewInfo3D.getObjectToView()));
254 aRetval.transform(rVCScene.getObjectTransformation());
256 return aRetval;
259 void E3dScene::ImpCleanup3DDepthMapper()
261 if(mp3DDepthRemapper)
263 delete mp3DDepthRemapper;
264 mp3DDepthRemapper = 0L;
268 sal_uInt32 E3dScene::RemapOrdNum(sal_uInt32 nNewOrdNum) const
270 if(!mp3DDepthRemapper)
272 const size_t nObjCount(GetSubList() ? GetSubList()->GetObjCount() : 0);
274 if(nObjCount > 1)
276 const_cast<E3dScene*>(this)->mp3DDepthRemapper = new Imp3DDepthRemapper((E3dScene&)(*this));
280 if(mp3DDepthRemapper)
282 return mp3DDepthRemapper->RemapOrdNum(nNewOrdNum);
285 return nNewOrdNum;
288 sal_uInt16 E3dScene::GetObjIdentifier() const
290 return E3D_SCENE_ID;
293 void E3dScene::SetBoundRectDirty()
295 E3dScene* pScene = GetScene();
297 if(pScene == this)
299 // avoid resetting aOutRect which in case of a 3D scene used as 2d object
300 // is model data,not re-creatable view data
302 else
304 // if not the outmost scene it is used as group in 3d, call parent
305 E3dObject::SetBoundRectDirty();
309 void E3dScene::NbcSetSnapRect(const Rectangle& rRect)
311 SetRectsDirty();
312 E3dObject::NbcSetSnapRect(rRect);
313 aCamera.SetDeviceWindow(rRect);
314 aCameraSet.SetViewportRectangle((Rectangle&)rRect);
316 ImpCleanup3DDepthMapper();
319 void E3dScene::NbcMove(const Size& rSize)
321 Rectangle aNewSnapRect = GetSnapRect();
322 MoveRect(aNewSnapRect, rSize);
323 NbcSetSnapRect(aNewSnapRect);
326 void E3dScene::NbcResize(const Point& rRef, const Fraction& rXFact,
327 const Fraction& rYFact)
329 Rectangle aNewSnapRect = GetSnapRect();
330 ResizeRect(aNewSnapRect, rRef, rXFact, rYFact);
331 NbcSetSnapRect(aNewSnapRect);
334 // Set new camera, and thus mark the scene and if possible the bound volume
335 // as changed
337 void E3dScene::SetCamera(const Camera3D& rNewCamera)
339 // Set old camera
340 aCamera = rNewCamera;
341 static_cast<sdr::properties::E3dSceneProperties&>(GetProperties()).SetSceneItemsFromCamera();
343 SetRectsDirty();
345 // Fill new camera from old
346 Camera3D& rCam = (Camera3D&)GetCamera();
348 // Turn off ratio
349 if(rCam.GetAspectMapping() == AS_NO_MAPPING)
350 GetCameraSet().SetRatio(0.0);
352 // Set Imaging geometry
353 basegfx::B3DPoint aVRP(rCam.GetViewPoint());
354 basegfx::B3DVector aVPN(aVRP - rCam.GetVRP());
355 basegfx::B3DVector aVUV(rCam.GetVUV());
357 // use SetViewportValues() to set VRP, VPN and VUV as vectors, too.
358 // Else these values would not be exported/imported correctly.
359 GetCameraSet().SetViewportValues(aVRP, aVPN, aVUV);
361 // Set perspective
362 GetCameraSet().SetPerspective(rCam.GetProjection() == PR_PERSPECTIVE);
363 GetCameraSet().SetViewportRectangle((Rectangle&)rCam.GetDeviceWindow());
365 ImpCleanup3DDepthMapper();
368 void E3dScene::NewObjectInserted(const E3dObject* p3DObj)
370 E3dObject::NewObjectInserted(p3DObj);
372 if ( p3DObj == this )
373 return;
375 ImpCleanup3DDepthMapper();
378 // Inform parent of changes of a child
380 void E3dScene::StructureChanged()
382 E3dObject::StructureChanged();
383 SetRectsDirty();
385 ImpCleanup3DDepthMapper();
388 // Determine the overall scene object
390 E3dScene* E3dScene::GetScene() const
392 if(GetParentObj())
393 return GetParentObj()->GetScene();
394 else
395 return const_cast<E3dScene*>(this);
398 void E3dScene::removeAllNonSelectedObjects()
400 E3DModifySceneSnapRectUpdater aUpdater(this);
402 for(size_t a = 0; a < maSubList.GetObjCount(); ++a)
404 SdrObject* pObj = maSubList.GetObj(a);
406 if(pObj)
408 bool bRemoveObject(false);
410 if(pObj->ISA(E3dScene))
412 E3dScene* pScene = static_cast<E3dScene*>(pObj);
414 // iterate over this sub-scene
415 pScene->removeAllNonSelectedObjects();
417 // check object count. Empty scenes can be deleted
418 const size_t nObjCount(pScene->GetSubList() ? pScene->GetSubList()->GetObjCount() : 0);
420 if(!nObjCount)
422 // all objects removed, scene can be removed, too
423 bRemoveObject = true;
426 else if(pObj->ISA(E3dCompoundObject))
428 E3dCompoundObject* pCompound = static_cast<E3dCompoundObject*>(pObj);
430 if(!pCompound->GetSelected())
432 bRemoveObject = true;
436 if(bRemoveObject)
438 maSubList.NbcRemoveObject(pObj->GetOrdNum());
439 a--;
440 SdrObject::Free(pObj);
446 E3dScene* E3dScene::Clone() const
448 return CloneHelper< E3dScene >();
451 E3dScene& E3dScene::operator=(const E3dScene& rObj)
453 if( this == &rObj )
454 return *this;
455 E3dObject::operator=(rObj);
457 const E3dScene& r3DObj = (const E3dScene&) rObj;
458 aCamera = r3DObj.aCamera;
460 aCameraSet = r3DObj.aCameraSet;
461 static_cast<sdr::properties::E3dSceneProperties&>(GetProperties()).SetSceneItemsFromCamera();
463 InvalidateBoundVolume();
464 RebuildLists();
465 SetRectsDirty();
467 ImpCleanup3DDepthMapper();
469 // #i101941#
470 // After a Scene as model object is cloned, the used
471 // ViewContactOfE3dScene is created and partially used
472 // to calculate Bound/SnapRects, but - since quite some
473 // values are buffered at the VC - not really well
474 // initialized. It would be possible to always watch for
475 // preconditions of buffered data, but this would be expensive
476 // and would create a lot of short living data structures.
477 // It is currently better to flush that data, e.g. by using
478 // ActionChanged at the VC which will for this class
479 // flush that cached data and initalize it's valid reconstruction
480 GetViewContact().ActionChanged();
481 return *this;
484 // Rebuild Light- and label- object lists rebuild (after loading, allocation)
486 void E3dScene::RebuildLists()
488 // first delete
489 SdrLayerID nCurrLayerID = GetLayer();
491 SdrObjListIter a3DIterator(maSubList, IM_FLAT);
493 // then examine all the objects in the scene
494 while ( a3DIterator.IsMore() )
496 E3dObject* p3DObj = static_cast<E3dObject*>(a3DIterator.Next());
497 p3DObj->NbcSetLayer(nCurrLayerID);
498 NewObjectInserted(p3DObj);
502 SdrObjGeoData *E3dScene::NewGeoData() const
504 return new E3DSceneGeoData;
507 void E3dScene::SaveGeoData(SdrObjGeoData& rGeo) const
509 E3dObject::SaveGeoData (rGeo);
511 static_cast<E3DSceneGeoData &>(rGeo).aCamera = aCamera;
514 void E3dScene::RestGeoData(const SdrObjGeoData& rGeo)
516 // #i94832# removed E3DModifySceneSnapRectUpdater here.
517 // It should not be needed, is already part of E3dObject::RestGeoData
518 E3dObject::RestGeoData (rGeo);
519 SetCamera (static_cast<const E3DSceneGeoData &>(rGeo).aCamera);
522 // Something was changed in the style sheet, so change scene
524 void E3dScene::Notify(SfxBroadcaster &rBC, const SfxHint &rHint)
526 SetRectsDirty();
527 E3dObject::Notify(rBC, rHint);
530 void E3dScene::RotateScene (const Point& rRef, long /*nAngle*/, double sn, double cs)
532 Point UpperLeft, LowerRight, Center, NewCenter;
534 UpperLeft = aOutRect.TopLeft();
535 LowerRight = aOutRect.BottomRight();
537 long dxOutRectHalf = labs(UpperLeft.X() - LowerRight.X());
538 dxOutRectHalf /= 2;
539 long dyOutRectHalf = labs(UpperLeft.Y() - LowerRight.Y());
540 dyOutRectHalf /= 2;
542 // Only the center is moved. The corners are moved by NbcMove. For the
543 // rotation a cartesian coordinate system is used in which the pivot
544 // point is the origin, and the y-axis increases upward, the X-axis to
545 // the right. This must be especially noted for the Y-values.
546 // (When considering a flat piece of paper the Y-axis pointing downwards
547 Center.X() = (UpperLeft.X() + dxOutRectHalf) - rRef.X();
548 Center.Y() = -((UpperLeft.Y() + dyOutRectHalf) - rRef.Y());
549 // A few special cases has to be dealt with first (n * 90 degrees n integer)
550 if (sn==1.0 && cs==0.0) { // 90deg
551 NewCenter.X() = -Center.Y();
552 NewCenter.Y() = -Center.X();
553 } else if (sn==0.0 && cs==-1.0) { // 180deg
554 NewCenter.X() = -Center.X();
555 NewCenter.Y() = -Center.Y();
556 } else if (sn==-1.0 && cs==0.0) { // 270deg
557 NewCenter.X() = Center.Y();
558 NewCenter.Y() = -Center.X();
560 else // Here it is rotated to any angle in the mathematically
561 // positive direction!
562 { // xnew = x * cos(alpha) - y * sin(alpha)
563 // ynew = x * sin(alpha) + y * cos(alpha)
564 // Bottom Right is not rotated: the pages of aOutRect must
565 // remain parallel to the coordinate axes.
566 NewCenter.X() = (long) (Center.X() * cs - Center.Y() * sn);
567 NewCenter.Y() = (long) (Center.X() * sn + Center.Y() * cs);
570 Size Differenz;
571 Point DiffPoint = (NewCenter - Center);
572 Differenz.Width() = DiffPoint.X();
573 Differenz.Height() = -DiffPoint.Y(); // Note that the Y-axis is counted ad positive downward.
574 NbcMove (Differenz); // Actually executes the coordinate transformation.
577 OUString E3dScene::TakeObjNameSingul() const
579 OUStringBuffer sName(ImpGetResStr(STR_ObjNameSingulScene3d));
581 OUString aName(GetName());
582 if (!aName.isEmpty())
584 sName.append(' ');
585 sName.append('\'');
586 sName.append(aName);
587 sName.append('\'');
589 return sName.makeStringAndClear();
592 OUString E3dScene::TakeObjNamePlural() const
594 return ImpGetResStr(STR_ObjNamePluralScene3d);
597 // The NbcRotate routine overrides the one of the SdrObject. The idea is
598 // to be able to rotate the scene relative to the position of the scene
599 // and then the objects in the scene
601 void E3dScene::NbcSetTransform(const basegfx::B3DHomMatrix& rMatrix)
603 if(maTransformation != rMatrix)
605 // call parent
606 E3dObject::NbcSetTransform(rMatrix);
610 void E3dScene::SetTransform(const basegfx::B3DHomMatrix& rMatrix)
612 if(rMatrix != maTransformation)
614 // call parent
615 E3dObject::SetTransform(rMatrix);
619 void E3dScene::NbcRotate(const Point& rRef, long nAngle, double sn, double cs)
621 // So currently the glue points are defined relative to the scene aOutRect.
622 // Before turning the glue points are defined relative to the page. They
623 // take no part in the rotation of the scene. To ensure this, there is the
624 // SetGlueReallyAbsolute(sal_True);
626 // So that was the scene, now the objects used in the scene
627 // 3D objects, if there is only one it can still have multiple surfaces but
628 // the surfaces do not hve to be connected. This allows you to access child
629 // objects. So going through the entire list and rotate around the Z axis
630 // through the enter of aOutRect's (Steiner's theorem), so RotateZ
632 RotateScene (rRef, nAngle, sn, cs); // Rotates the scene
633 double fWinkelInRad = nAngle/100.0 * F_PI180;
635 basegfx::B3DHomMatrix aRotation;
636 aRotation.rotate(0.0, 0.0, fWinkelInRad);
637 NbcSetTransform(aRotation * GetTransform());
639 SetRectsDirty(); // This forces a recalculation of all BoundRects
640 NbcRotateGluePoints(rRef,nAngle,sn,cs); // Rotate the glue points (who still
641 // have coordinates relative to the
642 // original page)
643 SetGlueReallyAbsolute(false); // from now they are again relative to BoundRect (that is defined as aOutRect)
644 SetRectsDirty();
647 void E3dScene::RecalcSnapRect()
649 E3dScene* pScene = GetScene();
651 if(pScene == this)
653 // The Scene is used as a 2D-Objekt, take the SnapRect from the
654 // 2D Display settings
655 Camera3D& rCam = (Camera3D&)pScene->GetCamera();
656 maSnapRect = rCam.GetDeviceWindow();
658 else
660 // The Scene itself is a member of another scene, get the SnapRect
661 // as a composite object
662 E3dObject::RecalcSnapRect();
666 bool E3dScene::IsBreakObjPossible()
668 // Break scene, if all members are able to break
669 SdrObjListIter a3DIterator(maSubList, IM_DEEPWITHGROUPS);
671 while ( a3DIterator.IsMore() )
673 E3dObject* pObj = static_cast<E3dObject*>(a3DIterator.Next());
674 DBG_ASSERT(pObj->ISA(E3dObject), "only 3D objects are allowed in scenes!");
675 if(!pObj->IsBreakObjPossible())
676 return false;
679 return true;
682 basegfx::B2DPolyPolygon E3dScene::TakeCreatePoly(const SdrDragStat& /*rDrag*/) const
684 return TakeXorPoly();
687 bool E3dScene::BegCreate(SdrDragStat& rStat)
689 rStat.SetOrtho4Possible();
690 Rectangle aRect1(rStat.GetStart(), rStat.GetNow());
691 aRect1.Justify();
692 rStat.SetActionRect(aRect1);
693 NbcSetSnapRect(aRect1);
694 return true;
697 bool E3dScene::MovCreate(SdrDragStat& rStat)
699 Rectangle aRect1;
700 rStat.TakeCreateRect(aRect1);
701 aRect1.Justify();
702 rStat.SetActionRect(aRect1);
703 NbcSetSnapRect(aRect1);
704 SetBoundRectDirty();
705 bSnapRectDirty=true;
706 return true;
709 bool E3dScene::EndCreate(SdrDragStat& rStat, SdrCreateCmd eCmd)
711 Rectangle aRect1;
712 rStat.TakeCreateRect(aRect1);
713 aRect1.Justify();
714 NbcSetSnapRect(aRect1);
715 SetRectsDirty();
716 return (eCmd==SDRCREATE_FORCEEND || rStat.GetPointAnz()>=2);
719 bool E3dScene::BckCreate(SdrDragStat& /*rStat*/)
721 return false;
724 void E3dScene::BrkCreate(SdrDragStat& /*rStat*/)
728 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */