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  maViewInformation3D(),
146  maObjectTransformation(),
147  maSdrSceneAttribute(),
148  maSdrLightingAttribute()
149 {
150 }
151 
153 {
154  basegfx::B3DHomMatrix aTransformation;
155  basegfx::B3DHomMatrix aOrientation;
156  basegfx::B3DHomMatrix aProjection;
157  basegfx::B3DHomMatrix aDeviceToView;
158 
159  // create transformation (scene as group's transformation)
160  // For historical reasons, the outmost scene's transformation is handles as part of the
161  // view transformation. This means that the BoundRect of the contained 3D Objects is
162  // without that transformation and makes it necessary to NOT add the first scene to the
163  // Primitive3DContainer of contained objects.
164  {
165  aTransformation = GetE3dScene().GetTransform();
166  }
167 
168  // create orientation (world to camera coordinate system)
169  {
170  // calculate orientation from VRP, VPN and VUV
171  const B3dCamera& rSceneCamera = GetE3dScene().GetCameraSet();
172  const basegfx::B3DPoint& aVRP(rSceneCamera.GetVRP());
173  const basegfx::B3DVector& aVPN(rSceneCamera.GetVPN());
174  const basegfx::B3DVector& aVUV(rSceneCamera.GetVUV());
175 
176  aOrientation.orientation(aVRP, aVPN, aVUV);
177  }
178 
179  // create projection (camera coordinate system to relative 2d where X,Y and Z are [0.0 .. 1.0])
180  {
181  const basegfx::B3DHomMatrix aWorldToCamera(aOrientation * aTransformation);
182  basegfx::B3DRange aCameraRange(rContentRange);
183  aCameraRange.transform(aWorldToCamera);
184 
185  // remember Z-Values, but change orientation
186  const double fMinZ(-aCameraRange.getMaxZ());
187  const double fMaxZ(-aCameraRange.getMinZ());
188 
189  // construct temporary matrix from world to device. Use unit values here to measure expansion
190  basegfx::B3DHomMatrix aWorldToDevice(aWorldToCamera);
192 
193  if(css::drawing::ProjectionMode_PERSPECTIVE == rSdrSceneAttribute.getProjectionMode())
194  {
195  aWorldToDevice.frustum(-1.0, 1.0, -1.0, 1.0, fMinZ, fMaxZ);
196  }
197  else
198  {
199  aWorldToDevice.ortho(-1.0, 1.0, -1.0, 1.0, fMinZ, fMaxZ);
200  }
201 
202  // create B3DRange in device. This will create the real used ranges
203  // in camera space. Do not use the Z-Values, though.
204  basegfx::B3DRange aDeviceRange(rContentRange);
205  aDeviceRange.transform(aWorldToDevice);
206 
207  // set projection
208  if(css::drawing::ProjectionMode_PERSPECTIVE == rSdrSceneAttribute.getProjectionMode())
209  {
210  aProjection.frustum(
211  aDeviceRange.getMinX(), aDeviceRange.getMaxX(),
212  aDeviceRange.getMinY(), aDeviceRange.getMaxY(),
213  fMinZ, fMaxZ);
214  }
215  else
216  {
217  aProjection.ortho(
218  aDeviceRange.getMinX(), aDeviceRange.getMaxX(),
219  aDeviceRange.getMinY(), aDeviceRange.getMaxY(),
220  fMinZ, fMaxZ);
221  }
222  }
223 
224  // create device to view transform
225  {
226  // create standard deviceToView projection for geometry
227  // input is [-1.0 .. 1.0] in X,Y and Z. bring to [0.0 .. 1.0]. Also
228  // necessary to flip Y due to screen orientation
229  // Z is not needed, but will also be brought to [0.0 .. 1.0]
230  aDeviceToView.scale(0.5, -0.5, 0.5);
231  aDeviceToView.translate(0.5, 0.5, 0.5);
232  }
233 
234  const uno::Sequence< beans::PropertyValue > aEmptyProperties;
236  aTransformation, aOrientation, aProjection,
237  aDeviceToView, 0.0, aEmptyProperties);
238 }
239 
241 {
242  // create 2d Object Transformation from relative point in 2d scene to world
243  const tools::Rectangle aRectangle(GetE3dScene().GetSnapRect());
244 
245  maObjectTransformation.set(0, 0, aRectangle.getWidth());
246  maObjectTransformation.set(1, 1, aRectangle.getHeight());
247  maObjectTransformation.set(0, 2, aRectangle.Left());
248  maObjectTransformation.set(1, 2, aRectangle.Top());
249 }
250 
252 {
253  const SfxItemSet& rItemSet = GetE3dScene().GetMergedItemSet();
255 }
256 
258 {
259  const SfxItemSet& rItemSet = GetE3dScene().GetMergedItemSet();
261 }
262 
264  const SdrLayerIDSet* pLayerVisibility) const
265 {
267  const sal_uInt32 nChildrenCount(GetObjectCount());
268 
269  if(nChildrenCount)
270  {
271  // create 3d scene primitive with visible content tested against rLayerVisibility
274  const bool bTestLayerVisibility(nullptr != pLayerVisibility);
275  const bool bTestSelectedVisibility(GetE3dScene().GetDrawOnlySelected());
276  const bool bTestVisibility(bTestLayerVisibility || bTestSelectedVisibility);
277 
278  // add children recursively. Do NOT start with (*this), this would create
279  // a 3D transformPrimitive for the start scene. While this is theoretically not
280  // a bad thing, for historical reasons the transformation of the outmost scene
281  // is seen as part of the ViewTransformation (see text in createViewInformation3D)
282  for(sal_uInt32 a(0); a < nChildrenCount; a++)
283  {
284  createSubPrimitive3DVector(
285  GetViewContact(a),
286  aAllSequence,
287  bTestLayerVisibility ? &aVisibleSequence : nullptr,
288  bTestLayerVisibility ? pLayerVisibility : nullptr,
289  bTestSelectedVisibility);
290  }
291 
292  const size_t nAllSize(!aAllSequence.empty() ? aAllSequence.size() : 0);
293  const size_t nVisibleSize(!aVisibleSequence.empty() ? aVisibleSequence.size() : 0);
294 
295  if((bTestVisibility && nVisibleSize) || nAllSize)
296  {
297  // for getting the 3D range using getB3DRangeFromPrimitive3DContainer a ViewInformation3D
298  // needs to be given for evtl. decompositions. At the same time createViewInformation3D
299  // currently is based on creating the target-ViewInformation3D using a given range. To
300  // get the true range, use a neutral ViewInformation3D here. This leaves all matrices
301  // on identity and the time on 0.0.
302  const uno::Sequence< beans::PropertyValue > aEmptyProperties;
303  const drawinglayer::geometry::ViewInformation3D aNeutralViewInformation3D(aEmptyProperties);
304  const basegfx::B3DRange aContentRange(aAllSequence.getB3DRange(aNeutralViewInformation3D));
305 
306  // create 2d primitive 3dscene with generated sub-list from collector
309  bTestVisibility ? aVisibleSequence : aAllSequence,
313  getViewInformation3D(aContentRange)));
314 
315  xRetval = drawinglayer::primitive2d::Primitive2DContainer{ xReference };
316  }
317  }
318 
319  // always append an invisible outline for the cases where no visible content exists
320  xRetval.push_back(
323 
324  return xRetval;
325 }
326 
328 {
330 
331  if(GetObjectCount())
332  {
333  // create a default ScenePrimitive2D (without visibility test of members)
334  xRetval = createScenePrimitive2DSequence(nullptr);
335  }
336 
337  return xRetval;
338 }
339 
341 {
342  // call parent
344 
345  // mark locally cached values as invalid
350 }
351 
353 {
355  {
356  // this version will create the content range on demand locally and thus is less
357  // performant than the other one. Since the information is buffered the planned
358  // behaviour is that the version with the given range is used initially.
359  basegfx::B3DRange aContentRange(getAllContentRange3D());
360 
361  if(aContentRange.isEmpty())
362  {
363  // empty scene, no 3d action should be necessary. Prepare some
364  // fallback size
365  OSL_FAIL("No need to get ViewInformation3D from an empty scene (!)");
366  aContentRange.expand(basegfx::B3DPoint(-100.0, -100.0, -100.0));
367  aContentRange.expand(basegfx::B3DPoint( 100.0, 100.0, 100.0));
368  }
369 
370  const_cast < ViewContactOfE3dScene* >(this)->createViewInformation3D(aContentRange);
371  }
372 
373  return maViewInformation3D;
374 }
375 
377 {
379  {
380  const_cast < ViewContactOfE3dScene* >(this)->createViewInformation3D(rContentRange);
381  }
382 
383  return maViewInformation3D;
384 }
385 
387 {
389  {
390  const_cast < ViewContactOfE3dScene* >(this)->createObjectTransformation();
391  }
392 
393  return maObjectTransformation;
394 }
395 
397 {
399  {
400  const_cast < ViewContactOfE3dScene* >(this)->createSdrSceneAttribute();
401  }
402 
403  return maSdrSceneAttribute;
404 }
405 
407 {
409  {
410  const_cast < ViewContactOfE3dScene* >(this)->createSdrLightingAttribute();
411  }
412 
413  return maSdrLightingAttribute;
414 }
415 
417 {
418  drawinglayer::primitive3d::Primitive3DContainer aAllPrimitive3DContainer;
419  const sal_uInt32 nChildrenCount(GetObjectCount());
420 
421  // add children recursively. Do NOT start with (*this), this would create
422  // a 3D transformPrimitive for the start scene. While this is theoretically not
423  // a bad thing, for historical reasons the transformation of the outmost scene
424  // is seen as part of the ViewTransformation (see text in createViewInformation3D)
425  for(sal_uInt32 a(0); a < nChildrenCount; a++)
426  {
427  createSubPrimitive3DVector(GetViewContact(a), aAllPrimitive3DContainer, nullptr, nullptr, false);
428  }
429 
430  return aAllPrimitive3DContainer;
431 }
432 
434 {
436  basegfx::B3DRange aAllContentRange3D;
437 
438  if(!xAllSequence.empty())
439  {
440  // for getting the 3D range using getB3DRangeFromPrimitive3DContainer a ViewInformation3D
441  // needs to be given for evtl. decompositions. Use a neutral ViewInformation3D here. This
442  // leaves all matrices on identity and the time on 0.0.
443  const uno::Sequence< beans::PropertyValue > aEmptyProperties;
444  const drawinglayer::geometry::ViewInformation3D aNeutralViewInformation3D(aEmptyProperties);
445 
446  aAllContentRange3D = xAllSequence.getB3DRange(aNeutralViewInformation3D);
447  }
448 
449  return aAllContentRange3D;
450 }
451 
452 }
453 
454 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long getHeight() const
const drawinglayer::geometry::ViewInformation3D & getViewInformation3D() const
void append(const Primitive3DContainer &rSource)
bool bVisible
void set(sal_uInt16 nRow, sal_uInt16 nColumn, double fValue)
long getWidth() const
virtual drawinglayer::primitive2d::Primitive2DContainer createViewIndependentPrimitive2DSequence() const override
double getMinZ() const
bool isEmpty() const
drawinglayer::primitive3d::Primitive3DContainer getViewIndependentPrimitive3DContainer() const
const basegfx::B3DHomMatrix & GetTransform() const
Definition: obj3d.hxx:111
virtual ViewContact & GetViewContact(sal_uInt32 nIndex) const override
drawinglayer::primitive3d::Primitive3DContainer getAllPrimitive3DContainer() const
void scale(double fX, double fY, double fZ)
double getMaxZ() const
const E3dObject & GetE3dObject() const
double getMinX() const
const drawinglayer::attribute::SdrSceneAttribute & getSdrSceneAttribute() const
long Top() 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)
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:576
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:66
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:1883
Primitive2DReference createHiddenGeometryPrimitives2D(const basegfx::B2DHomMatrix &rMatrix)
const drawinglayer::attribute::SdrLightingAttribute & getSdrLightingAttribute() const
css::drawing::ProjectionMode getProjectionMode() const
long Left() 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:148
attribute::SdrLightingAttribute createNewSdrLightingAttribute(const SfxItemSet &rSet)