LibreOffice Module svx (master)  1
viewcontactofe3dscene.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
31 
32 using namespace com::sun::star;
33 
34 namespace {
35 
36 // pActiveVC is only true if ghosted is still activated and maybe needs to be switched off in this path
37 void createSubPrimitive3DVector(
38  const sdr::contact::ViewContact& rCandidate,
41  const SdrLayerIDSet* pVisibleSdrLayerIDSet,
42  const bool bTestSelectedVisibility)
43 {
44  const sdr::contact::ViewContactOfE3dScene* pViewContactOfE3dScene = dynamic_cast< const sdr::contact::ViewContactOfE3dScene* >(&rCandidate);
45 
46  if(pViewContactOfE3dScene)
47  {
48  const sal_uInt32 nChildrenCount(rCandidate.GetObjectCount());
49 
50  if(nChildrenCount)
51  {
52  // provide new collection sequences
55 
56  // add children recursively
57  for(sal_uInt32 a(0); a < nChildrenCount; a++)
58  {
59  createSubPrimitive3DVector(
60  rCandidate.GetViewContact(a),
61  aNewAllTarget,
62  o_pVisibleTarget ? &aNewVisibleTarget : nullptr,
63  pVisibleSdrLayerIDSet,
64  bTestSelectedVisibility);
65  }
66 
67  // create transform primitive for the created content combining content and transformtion
69  pViewContactOfE3dScene->GetE3dScene().GetTransform(),
70  aNewAllTarget));
71 
72  // add created content to all target
73  o_rAllTarget.push_back(xReference);
74 
75  // add created content to visible target if exists
76  if(o_pVisibleTarget)
77  {
78  o_pVisibleTarget->push_back(xReference);
79  }
80  }
81  }
82  else
83  {
84  // access view independent representation of rCandidate
85  const sdr::contact::ViewContactOfE3d* pViewContactOfE3d = dynamic_cast< const sdr::contact::ViewContactOfE3d* >(&rCandidate);
86 
87  if(pViewContactOfE3d)
88  {
90 
91  if(!xPrimitive3DSeq.empty())
92  {
93  // add to all target vector
94  o_rAllTarget.append(xPrimitive3DSeq);
95 
96  if(o_pVisibleTarget)
97  {
98  // test visibility. Primitive is visible when both tests are true (AND)
99  bool bVisible(true);
100 
101  if(pVisibleSdrLayerIDSet)
102  {
103  // test layer visibility
104  const E3dObject& rE3dObject = pViewContactOfE3d->GetE3dObject();
105  const SdrLayerID aLayerID(rE3dObject.GetLayer());
106 
107  bVisible = pVisibleSdrLayerIDSet->IsSet(aLayerID);
108  }
109 
110  if(bVisible && bTestSelectedVisibility)
111  {
112  // test selected visibility (see 3D View's DrawMarkedObj implementation)
113  const E3dObject& rE3dObject = pViewContactOfE3d->GetE3dObject();
114 
115  bVisible = rE3dObject.GetSelected();
116  }
117 
118  if (bVisible)
119  {
120  // add to visible target vector
121  o_pVisibleTarget->append(xPrimitive3DSeq);
122  }
123  }
124  }
125  }
126  }
127 }
128 
129 }
130 
131 namespace sdr::contact {
132 
133 // Create an Object-Specific ViewObjectContact, set ViewContact and
134 // ObjectContact. Always needs to return something.
135 ViewObjectContact& ViewContactOfE3dScene::CreateObjectSpecificViewObjectContact(ObjectContact& rObjectContact)
136 {
137  ViewObjectContact* pRetval = new ViewObjectContactOfE3dScene(rObjectContact, *this);
138  DBG_ASSERT(pRetval, "ViewContactOfE3dScene::CreateObjectSpecificViewObjectContact() failed (!)");
139 
140  return *pRetval;
141 }
142 
143 ViewContactOfE3dScene::ViewContactOfE3dScene(E3dScene& rScene)
144 : ViewContactOfSdrObj(rScene)
145 {
146 }
147 
149 {
150  basegfx::B3DHomMatrix aTransformation;
151  basegfx::B3DHomMatrix aOrientation;
152  basegfx::B3DHomMatrix aProjection;
153  basegfx::B3DHomMatrix aDeviceToView;
154 
155  // create transformation (scene as group's transformation)
156  // For historical reasons, the outmost scene's transformation is handles as part of the
157  // view transformation. This means that the BoundRect of the contained 3D Objects is
158  // without that transformation and makes it necessary to NOT add the first scene to the
159  // Primitive3DContainer of contained objects.
160  {
161  aTransformation = GetE3dScene().GetTransform();
162  }
163 
164  // create orientation (world to camera coordinate system)
165  {
166  // calculate orientation from VRP, VPN and VUV
167  const B3dCamera& rSceneCamera = GetE3dScene().GetCameraSet();
168  const basegfx::B3DPoint& aVRP(rSceneCamera.GetVRP());
169  const basegfx::B3DVector& aVPN(rSceneCamera.GetVPN());
170  const basegfx::B3DVector& aVUV(rSceneCamera.GetVUV());
171 
172  aOrientation.orientation(aVRP, aVPN, aVUV);
173  }
174 
175  // create projection (camera coordinate system to relative 2d where X,Y and Z are [0.0 .. 1.0])
176  {
177  const basegfx::B3DHomMatrix aWorldToCamera(aOrientation * aTransformation);
178  basegfx::B3DRange aCameraRange(rContentRange);
179  aCameraRange.transform(aWorldToCamera);
180 
181  // remember Z-Values, but change orientation
182  const double fMinZ(-aCameraRange.getMaxZ());
183  const double fMaxZ(-aCameraRange.getMinZ());
184 
185  // construct temporary matrix from world to device. Use unit values here to measure expansion
186  basegfx::B3DHomMatrix aWorldToDevice(aWorldToCamera);
188 
189  if(css::drawing::ProjectionMode_PERSPECTIVE == rSdrSceneAttribute.getProjectionMode())
190  {
191  aWorldToDevice.frustum(-1.0, 1.0, -1.0, 1.0, fMinZ, fMaxZ);
192  }
193  else
194  {
195  aWorldToDevice.ortho(-1.0, 1.0, -1.0, 1.0, fMinZ, fMaxZ);
196  }
197 
198  // create B3DRange in device. This will create the real used ranges
199  // in camera space. Do not use the Z-Values, though.
200  basegfx::B3DRange aDeviceRange(rContentRange);
201  aDeviceRange.transform(aWorldToDevice);
202 
203  // set projection
204  if(css::drawing::ProjectionMode_PERSPECTIVE == rSdrSceneAttribute.getProjectionMode())
205  {
206  aProjection.frustum(
207  aDeviceRange.getMinX(), aDeviceRange.getMaxX(),
208  aDeviceRange.getMinY(), aDeviceRange.getMaxY(),
209  fMinZ, fMaxZ);
210  }
211  else
212  {
213  aProjection.ortho(
214  aDeviceRange.getMinX(), aDeviceRange.getMaxX(),
215  aDeviceRange.getMinY(), aDeviceRange.getMaxY(),
216  fMinZ, fMaxZ);
217  }
218  }
219 
220  // create device to view transform
221  {
222  // create standard deviceToView projection for geometry
223  // input is [-1.0 .. 1.0] in X,Y and Z. bring to [0.0 .. 1.0]. Also
224  // necessary to flip Y due to screen orientation
225  // Z is not needed, but will also be brought to [0.0 .. 1.0]
226  aDeviceToView.scale(0.5, -0.5, 0.5);
227  aDeviceToView.translate(0.5, 0.5, 0.5);
228  }
229 
230  const uno::Sequence< beans::PropertyValue > aEmptyProperties;
232  aTransformation, aOrientation, aProjection,
233  aDeviceToView, 0.0, aEmptyProperties);
234 }
235 
237 {
238  // create 2d Object Transformation from relative point in 2d scene to world
239  const tools::Rectangle aRectangle(GetE3dScene().GetSnapRect());
240 
241  maObjectTransformation.set(0, 0, aRectangle.getWidth());
242  maObjectTransformation.set(1, 1, aRectangle.getHeight());
243  maObjectTransformation.set(0, 2, aRectangle.Left());
244  maObjectTransformation.set(1, 2, aRectangle.Top());
245 }
246 
248 {
249  const SfxItemSet& rItemSet = GetE3dScene().GetMergedItemSet();
251 }
252 
254 {
255  const SfxItemSet& rItemSet = GetE3dScene().GetMergedItemSet();
257 }
258 
260  const SdrLayerIDSet* pLayerVisibility) const
261 {
263  const sal_uInt32 nChildrenCount(GetObjectCount());
264 
265  if(nChildrenCount)
266  {
267  // create 3d scene primitive with visible content tested against rLayerVisibility
270  const bool bTestLayerVisibility(nullptr != pLayerVisibility);
271  const bool bTestSelectedVisibility(GetE3dScene().GetDrawOnlySelected());
272  const bool bTestVisibility(bTestLayerVisibility || bTestSelectedVisibility);
273 
274  // add children recursively. Do NOT start with (*this), this would create
275  // a 3D transformPrimitive for the start scene. While this is theoretically not
276  // a bad thing, for historical reasons the transformation of the outmost scene
277  // is seen as part of the ViewTransformation (see text in createViewInformation3D)
278  for(sal_uInt32 a(0); a < nChildrenCount; a++)
279  {
280  createSubPrimitive3DVector(
281  GetViewContact(a),
282  aAllSequence,
283  bTestLayerVisibility ? &aVisibleSequence : nullptr,
284  bTestLayerVisibility ? pLayerVisibility : nullptr,
285  bTestSelectedVisibility);
286  }
287 
288  const size_t nAllSize(!aAllSequence.empty() ? aAllSequence.size() : 0);
289  const size_t nVisibleSize(!aVisibleSequence.empty() ? aVisibleSequence.size() : 0);
290 
291  if((bTestVisibility && nVisibleSize) || nAllSize)
292  {
293  // for getting the 3D range using getB3DRangeFromPrimitive3DContainer a ViewInformation3D
294  // needs to be given for evtl. decompositions. At the same time createViewInformation3D
295  // currently is based on creating the target-ViewInformation3D using a given range. To
296  // get the true range, use a neutral ViewInformation3D here. This leaves all matrices
297  // on identity and the time on 0.0.
298  const uno::Sequence< beans::PropertyValue > aEmptyProperties;
299  const drawinglayer::geometry::ViewInformation3D aNeutralViewInformation3D(aEmptyProperties);
300  const basegfx::B3DRange aContentRange(aAllSequence.getB3DRange(aNeutralViewInformation3D));
301 
302  // create 2d primitive 3dscene with generated sub-list from collector
305  bTestVisibility ? aVisibleSequence : aAllSequence,
309  getViewInformation3D(aContentRange)));
310 
311  xRetval = drawinglayer::primitive2d::Primitive2DContainer{ xReference };
312  }
313  }
314 
315  // always append an invisible outline for the cases where no visible content exists
316  xRetval.push_back(
319 
320  return xRetval;
321 }
322 
324 {
326 
327  if(GetObjectCount())
328  {
329  // create a default ScenePrimitive2D (without visibility test of members)
330  xRetval = createScenePrimitive2DSequence(nullptr);
331  }
332 
333  return xRetval;
334 }
335 
337 {
338  // call parent
340 
341  // mark locally cached values as invalid
346 }
347 
349 {
351  {
352  // this version will create the content range on demand locally and thus is less
353  // performant than the other one. Since the information is buffered the planned
354  // behaviour is that the version with the given range is used initially.
355  basegfx::B3DRange aContentRange(getAllContentRange3D());
356 
357  if(aContentRange.isEmpty())
358  {
359  // empty scene, no 3d action should be necessary. Prepare some
360  // fallback size
361  OSL_FAIL("No need to get ViewInformation3D from an empty scene (!)");
362  aContentRange.expand(basegfx::B3DPoint(-100.0, -100.0, -100.0));
363  aContentRange.expand(basegfx::B3DPoint( 100.0, 100.0, 100.0));
364  }
365 
366  const_cast < ViewContactOfE3dScene* >(this)->createViewInformation3D(aContentRange);
367  }
368 
369  return maViewInformation3D;
370 }
371 
373 {
375  {
376  const_cast < ViewContactOfE3dScene* >(this)->createViewInformation3D(rContentRange);
377  }
378 
379  return maViewInformation3D;
380 }
381 
383 {
385  {
386  const_cast < ViewContactOfE3dScene* >(this)->createObjectTransformation();
387  }
388 
389  return maObjectTransformation;
390 }
391 
393 {
395  {
396  const_cast < ViewContactOfE3dScene* >(this)->createSdrSceneAttribute();
397  }
398 
399  return maSdrSceneAttribute;
400 }
401 
403 {
405  {
406  const_cast < ViewContactOfE3dScene* >(this)->createSdrLightingAttribute();
407  }
408 
409  return maSdrLightingAttribute;
410 }
411 
413 {
414  drawinglayer::primitive3d::Primitive3DContainer aAllPrimitive3DContainer;
415  const sal_uInt32 nChildrenCount(GetObjectCount());
416 
417  // add children recursively. Do NOT start with (*this), this would create
418  // a 3D transformPrimitive for the start scene. While this is theoretically not
419  // a bad thing, for historical reasons the transformation of the outmost scene
420  // is seen as part of the ViewTransformation (see text in createViewInformation3D)
421  for(sal_uInt32 a(0); a < nChildrenCount; a++)
422  {
423  createSubPrimitive3DVector(GetViewContact(a), aAllPrimitive3DContainer, nullptr, nullptr, false);
424  }
425 
426  return aAllPrimitive3DContainer;
427 }
428 
430 {
432  basegfx::B3DRange aAllContentRange3D;
433 
434  if(!xAllSequence.empty())
435  {
436  // for getting the 3D range using getB3DRangeFromPrimitive3DContainer a ViewInformation3D
437  // needs to be given for evtl. decompositions. Use a neutral ViewInformation3D here. This
438  // leaves all matrices on identity and the time on 0.0.
439  const uno::Sequence< beans::PropertyValue > aEmptyProperties;
440  const drawinglayer::geometry::ViewInformation3D aNeutralViewInformation3D(aEmptyProperties);
441 
442  aAllContentRange3D = xAllSequence.getB3DRange(aNeutralViewInformation3D);
443  }
444 
445  return aAllContentRange3D;
446 }
447 
448 }
449 
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const drawinglayer::geometry::ViewInformation3D & getViewInformation3D() const
void append(const Primitive3DContainer &rSource)
bool bVisible
void set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
const basegfx::B3DVector & GetVPN() const
const basegfx::B3DPoint & GetVRP() const
tools::Long getWidth() const
constexpr tools::Long Left() const
virtual drawinglayer::primitive2d::Primitive2DContainer createViewIndependentPrimitive2DSequence() const override
double getMinZ() const
const basegfx::B3DVector & GetVUV() const
bool isEmpty() const
drawinglayer::primitive3d::Primitive3DContainer getViewIndependentPrimitive3DContainer() const
const basegfx::B3DHomMatrix & GetTransform() const
Definition: obj3d.hxx:112
virtual ViewContact & GetViewContact(sal_uInt32 nIndex) const override
drawinglayer::primitive3d::Primitive3DContainer getAllPrimitive3DContainer() const
void scale(double fX, double fY, double fZ)
double getMaxZ() const
tools::Long getHeight() const
const E3dObject & GetE3dObject() const
double getMinX() const
const drawinglayer::attribute::SdrSceneAttribute & getSdrSceneAttribute() const
void frustum(double fLeft=-1.0, double fRight=1.0, double fBottom=-1.0, double fTop=1.0, double fNear=0.001, double fFar=1.0)
basegfx::B3DRange getB3DRange(const geometry::ViewInformation3D &aViewInformation) const
drawinglayer::attribute::SdrLightingAttribute maSdrLightingAttribute
bool GetSelected() const
Definition: obj3d.hxx:135
#define DBG_ASSERT(sCon, aError)
uno_Any a
void expand(const B3DTuple &rTuple)
constexpr tools::Long Top() const
double getMinY() const
bool isIdentity() const
css::uno::Reference< css::graphic::XPrimitive3D > Primitive3DReference
drawinglayer::primitive2d::Primitive2DContainer createScenePrimitive2DSequence(const SdrLayerIDSet *pLayerVisibility) const
drawinglayer::attribute::SdrSceneAttribute maSdrSceneAttribute
BASEGFX_DLLPUBLIC void transform(const B3DHomMatrix &rMatrix)
virtual SdrLayerID GetLayer() const
Definition: svdobj.cxx:678
void ortho(double fLeft=-1.0, double fRight=1.0, double fBottom=-1.0, double fTop=1.0, double fNear=0.0, double fFar=1.0)
void translate(double fX, double fY, double fZ)
drawinglayer::geometry::ViewInformation3D maViewInformation3D
bool IsSet(SdrLayerID a) const
Definition: svdsob.hxx:64
virtual sal_uInt32 GetObjectCount() const
void createViewInformation3D(const ::basegfx::B3DRange &rContentRange)
css::uno::Reference< css::graphic::XPrimitive2D > Primitive2DReference
attribute::SdrSceneAttribute createNewSdrSceneAttribute(const SfxItemSet &rSet)
const basegfx::B2DHomMatrix & getObjectTransformation() const
virtual sal_uInt32 GetObjectCount() const override
const SfxItemSet & GetMergedItemSet() const
Definition: svdobj.cxx:1978
Primitive2DReference createHiddenGeometryPrimitives2D(const basegfx::B2DHomMatrix &rMatrix)
const drawinglayer::attribute::SdrLightingAttribute & getSdrLightingAttribute() const
css::drawing::ProjectionMode getProjectionMode() const
virtual ViewContact & GetViewContact(sal_uInt32 nIndex) const
double getMaxX() const
double getMaxY() const
void orientation(const B3DPoint &rVRP=B3DPoint(0.0, 0.0, 1.0), B3DVector aVPN=B3DVector(0.0, 0.0, 1.0), B3DVector aVUV=B3DVector(0.0, 1.0, 0.0))
basegfx::B3DRange getAllContentRange3D() const
B3dCamera & GetCameraSet()
Definition: scene3d.hxx:147
attribute::SdrLightingAttribute createNewSdrLightingAttribute(const SfxItemSet &rSet)