Line data Source code
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 :
20 : #include <svx/sdr/contact/viewcontactofe3dscene.hxx>
21 : #include <svx/polysc3d.hxx>
22 : #include <svx/sdr/contact/displayinfo.hxx>
23 : #include <svx/sdr/contact/viewobjectcontact.hxx>
24 : #include <basegfx/polygon/b2dpolygontools.hxx>
25 : #include <basegfx/color/bcolor.hxx>
26 : #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
27 : #include <svx/sdr/primitive2d/sdrattributecreator.hxx>
28 : #include <svx/sdr/contact/viewobjectcontactofe3dscene.hxx>
29 : #include <basegfx/matrix/b2dhommatrix.hxx>
30 : #include <basegfx/range/b3drange.hxx>
31 : #include <drawinglayer/primitive3d/baseprimitive3d.hxx>
32 : #include <svx/sdr/contact/viewcontactofe3d.hxx>
33 : #include <drawinglayer/primitive2d/sceneprimitive2d.hxx>
34 : #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
35 : #include <drawinglayer/primitive2d/sdrdecompositiontools2d.hxx>
36 :
37 :
38 :
39 : using namespace com::sun::star;
40 :
41 :
42 :
43 : namespace
44 : {
45 : // pActiveVC is only true if ghosted is still activated and maybe needs to be switched off in this path
46 3223 : void createSubPrimitive3DVector(
47 : const sdr::contact::ViewContact& rCandidate,
48 : drawinglayer::primitive3d::Primitive3DSequence& o_rAllTarget,
49 : drawinglayer::primitive3d::Primitive3DSequence* o_pVisibleTarget,
50 : const SetOfByte* pVisibleLayerSet,
51 : const bool bTestSelectedVisibility)
52 : {
53 3223 : const sdr::contact::ViewContactOfE3dScene* pViewContactOfE3dScene = dynamic_cast< const sdr::contact::ViewContactOfE3dScene* >(&rCandidate);
54 :
55 3223 : if(pViewContactOfE3dScene)
56 : {
57 1900 : const sal_uInt32 nChildrenCount(rCandidate.GetObjectCount());
58 :
59 1900 : if(nChildrenCount)
60 : {
61 : // provide new collection sequences
62 1084 : drawinglayer::primitive3d::Primitive3DSequence aNewAllTarget;
63 2168 : drawinglayer::primitive3d::Primitive3DSequence aNewVisibleTarget;
64 :
65 : // add children recursively
66 4063 : for(sal_uInt32 a(0L); a < nChildrenCount; a++)
67 : {
68 : createSubPrimitive3DVector(
69 2979 : rCandidate.GetViewContact(a),
70 : aNewAllTarget,
71 : o_pVisibleTarget ? &aNewVisibleTarget : 0,
72 : pVisibleLayerSet,
73 5958 : bTestSelectedVisibility);
74 : }
75 :
76 : // create transform primitive for the created content combining content and transformtion
77 : const drawinglayer::primitive3d::Primitive3DReference xReference(new drawinglayer::primitive3d::TransformPrimitive3D(
78 1084 : pViewContactOfE3dScene->GetE3dScene().GetTransform(),
79 2168 : aNewAllTarget));
80 :
81 : // add created content to all target
82 1084 : drawinglayer::primitive3d::appendPrimitive3DReferenceToPrimitive3DSequence(o_rAllTarget, xReference);
83 :
84 : // add created content to visibiel target if exists
85 1084 : if(o_pVisibleTarget)
86 : {
87 9 : drawinglayer::primitive3d::appendPrimitive3DReferenceToPrimitive3DSequence(*o_pVisibleTarget, xReference);
88 1084 : }
89 : }
90 : }
91 : else
92 : {
93 : // access view independent representation of rCandidate
94 1323 : const sdr::contact::ViewContactOfE3d* pViewContactOfE3d = dynamic_cast< const sdr::contact::ViewContactOfE3d* >(&rCandidate);
95 :
96 1323 : if(pViewContactOfE3d)
97 : {
98 1323 : drawinglayer::primitive3d::Primitive3DSequence xPrimitive3DSeq(pViewContactOfE3d->getViewIndependentPrimitive3DSequence());
99 :
100 1323 : if(xPrimitive3DSeq.hasElements())
101 : {
102 : // add to all target vector
103 1323 : drawinglayer::primitive3d::appendPrimitive3DSequenceToPrimitive3DSequence(o_rAllTarget, xPrimitive3DSeq);
104 :
105 1323 : if(o_pVisibleTarget)
106 : {
107 : // test visibility. Primitive is visible when both tests are true (AND)
108 15 : bool bVisible(true);
109 :
110 15 : if(pVisibleLayerSet)
111 : {
112 : // test layer visibility
113 15 : const E3dObject& rE3dObject = pViewContactOfE3d->GetE3dObject();
114 15 : const SdrLayerID aLayerID(rE3dObject.GetLayer());
115 :
116 15 : bVisible = pVisibleLayerSet->IsSet(aLayerID);
117 : }
118 :
119 15 : if(bVisible && bTestSelectedVisibility)
120 : {
121 : // test selected visibility (see 3D View's DrawMarkedObj implementation)
122 0 : const E3dObject& rE3dObject = pViewContactOfE3d->GetE3dObject();
123 :
124 0 : bVisible = rE3dObject.GetSelected();
125 : }
126 :
127 15 : if(bVisible && o_pVisibleTarget)
128 : {
129 : // add to visible target vector
130 15 : drawinglayer::primitive3d::appendPrimitive3DSequenceToPrimitive3DSequence(*o_pVisibleTarget, xPrimitive3DSeq);
131 : }
132 : }
133 1323 : }
134 : }
135 : }
136 3223 : }
137 : } // end of anonymous namespace
138 :
139 :
140 :
141 : namespace sdr
142 : {
143 : namespace contact
144 : {
145 : // Create a Object-Specific ViewObjectContact, set ViewContact and
146 : // ObjectContact. Always needs to return something.
147 18 : ViewObjectContact& ViewContactOfE3dScene::CreateObjectSpecificViewObjectContact(ObjectContact& rObjectContact)
148 : {
149 18 : ViewObjectContact* pRetval = new ViewObjectContactOfE3dScene(rObjectContact, *this);
150 : DBG_ASSERT(pRetval, "ViewContactOfE3dScene::CreateObjectSpecificViewObjectContact() failed (!)");
151 :
152 18 : return *pRetval;
153 : }
154 :
155 344 : ViewContactOfE3dScene::ViewContactOfE3dScene(E3dScene& rScene)
156 : : ViewContactOfSdrObj(rScene),
157 : maViewInformation3D(),
158 : maObjectTransformation(),
159 : maSdrSceneAttribute(),
160 344 : maSdrLightingAttribute()
161 : {
162 344 : }
163 :
164 139 : void ViewContactOfE3dScene::createViewInformation3D(const basegfx::B3DRange& rContentRange)
165 : {
166 139 : basegfx::B3DHomMatrix aTransformation;
167 278 : basegfx::B3DHomMatrix aOrientation;
168 278 : basegfx::B3DHomMatrix aProjection;
169 278 : basegfx::B3DHomMatrix aDeviceToView;
170 :
171 : // create transformation (scene as group's transformation)
172 : // For historical reasons, the outmost scene's transformation is handles as part of the
173 : // view transformation. This means that the BoundRect of the contained 3D Objects is
174 : // without that transformation and makes it necessary to NOT add the first scene to the
175 : // Primitive3DSequence of contained objects.
176 : {
177 139 : aTransformation = GetE3dScene().GetTransform();
178 : }
179 :
180 : // create orientation (world to camera coordinate system)
181 : {
182 : // calculate orientation from VRP, VPN and VUV
183 139 : const B3dCamera& rSceneCamera = GetE3dScene().GetCameraSet();
184 139 : const basegfx::B3DPoint aVRP(rSceneCamera.GetVRP());
185 278 : const basegfx::B3DVector aVPN(rSceneCamera.GetVRP());
186 278 : const basegfx::B3DVector aVUV(rSceneCamera.GetVUV());
187 :
188 278 : aOrientation.orientation(aVRP, aVPN, aVUV);
189 : }
190 :
191 : // create projection (camera coordinate system to relative 2d where X,Y and Z are [0.0 .. 1.0])
192 : {
193 139 : const basegfx::B3DHomMatrix aWorldToCamera(aOrientation * aTransformation);
194 139 : basegfx::B3DRange aCameraRange(rContentRange);
195 139 : aCameraRange.transform(aWorldToCamera);
196 :
197 : // remember Z-Values, but change orientation
198 139 : const double fMinZ(-aCameraRange.getMaxZ());
199 139 : const double fMaxZ(-aCameraRange.getMinZ());
200 :
201 : // construct temporary matrix from world to device. Use unit values here to measure expansion
202 278 : basegfx::B3DHomMatrix aWorldToDevice(aWorldToCamera);
203 139 : const drawinglayer::attribute::SdrSceneAttribute& rSdrSceneAttribute = getSdrSceneAttribute();
204 :
205 139 : if(::com::sun::star::drawing::ProjectionMode_PERSPECTIVE == rSdrSceneAttribute.getProjectionMode())
206 : {
207 34 : aWorldToDevice.frustum(-1.0, 1.0, -1.0, 1.0, fMinZ, fMaxZ);
208 : }
209 : else
210 : {
211 105 : aWorldToDevice.ortho(-1.0, 1.0, -1.0, 1.0, fMinZ, fMaxZ);
212 : }
213 :
214 : // create B3DRange in device. This will create the real used ranges
215 : // in camera space. Do not use the Z-Values, though.
216 139 : basegfx::B3DRange aDeviceRange(rContentRange);
217 139 : aDeviceRange.transform(aWorldToDevice);
218 :
219 : // set projection
220 139 : if(::com::sun::star::drawing::ProjectionMode_PERSPECTIVE == rSdrSceneAttribute.getProjectionMode())
221 : {
222 : aProjection.frustum(
223 : aDeviceRange.getMinX(), aDeviceRange.getMaxX(),
224 : aDeviceRange.getMinY(), aDeviceRange.getMaxY(),
225 34 : fMinZ, fMaxZ);
226 : }
227 : else
228 : {
229 : aProjection.ortho(
230 : aDeviceRange.getMinX(), aDeviceRange.getMaxX(),
231 : aDeviceRange.getMinY(), aDeviceRange.getMaxY(),
232 105 : fMinZ, fMaxZ);
233 139 : }
234 : }
235 :
236 : // create device to view transform
237 : {
238 : // create standard deviceToView projection for geometry
239 : // input is [-1.0 .. 1.0] in X,Y and Z. bring to [0.0 .. 1.0]. Also
240 : // necessary to flip Y due to screen orientation
241 : // Z is not needed, but will also be brought to [0.0 .. 1.0]
242 139 : aDeviceToView.scale(0.5, -0.5, 0.5);
243 139 : aDeviceToView.translate(0.5, 0.5, 0.5);
244 : }
245 :
246 278 : const uno::Sequence< beans::PropertyValue > aEmptyProperties;
247 278 : maViewInformation3D = drawinglayer::geometry::ViewInformation3D(
248 : aTransformation, aOrientation, aProjection,
249 278 : aDeviceToView, 0.0, aEmptyProperties);
250 139 : }
251 :
252 139 : void ViewContactOfE3dScene::createObjectTransformation()
253 : {
254 : // create 2d Object Transformation from relative point in 2d scene to world
255 139 : Rectangle aRectangle = GetE3dScene().GetSnapRect();
256 : // Hack for calc, transform position of object according
257 : // to current zoom so as objects relative position to grid
258 : // appears stable
259 139 : aRectangle += GetE3dScene().GetGridOffset();
260 139 : maObjectTransformation.set(0, 0, aRectangle.getWidth());
261 139 : maObjectTransformation.set(1, 1, aRectangle.getHeight());
262 139 : maObjectTransformation.set(0, 2, aRectangle.Left());
263 139 : maObjectTransformation.set(1, 2, aRectangle.Top());
264 139 : }
265 :
266 139 : void ViewContactOfE3dScene::createSdrSceneAttribute()
267 : {
268 139 : const SfxItemSet& rItemSet = GetE3dScene().GetMergedItemSet();
269 139 : maSdrSceneAttribute = drawinglayer::primitive2d::createNewSdrSceneAttribute(rItemSet);
270 139 : }
271 :
272 11 : void ViewContactOfE3dScene::createSdrLightingAttribute()
273 : {
274 11 : const SfxItemSet& rItemSet = GetE3dScene().GetMergedItemSet();
275 11 : maSdrLightingAttribute = drawinglayer::primitive2d::createNewSdrLightingAttribute(rItemSet);
276 11 : }
277 :
278 13 : drawinglayer::primitive2d::Primitive2DSequence ViewContactOfE3dScene::createScenePrimitive2DSequence(
279 : const SetOfByte* pLayerVisibility) const
280 : {
281 13 : drawinglayer::primitive2d::Primitive2DSequence xRetval;
282 13 : const sal_uInt32 nChildrenCount(GetObjectCount());
283 :
284 13 : if(nChildrenCount)
285 : {
286 : // create 3d scene primitive with visible content tested against rLayerVisibility
287 13 : drawinglayer::primitive3d::Primitive3DSequence aAllSequence;
288 26 : drawinglayer::primitive3d::Primitive3DSequence aVisibleSequence;
289 13 : const bool bTestLayerVisibility(0 != pLayerVisibility);
290 13 : const bool bTestSelectedVisibility(GetE3dScene().GetDrawOnlySelected());
291 13 : const bool bTestVisibility(bTestLayerVisibility || bTestSelectedVisibility);
292 :
293 : // add children recursively. Do NOT start with (*this), this would create
294 : // a 3D transformPrimitive for the start scene. While this is theoretically not
295 : // a bad thing, for historical reasons the transformation of the outmost scene
296 : // is seen as part of the ViewTransformation (see text in createViewInformation3D)
297 26 : for(sal_uInt32 a(0L); a < nChildrenCount; a++)
298 : {
299 : createSubPrimitive3DVector(
300 13 : GetViewContact(a),
301 : aAllSequence,
302 : bTestLayerVisibility ? &aVisibleSequence : 0,
303 : bTestLayerVisibility ? pLayerVisibility : 0,
304 26 : bTestSelectedVisibility);
305 : }
306 :
307 13 : const sal_uInt32 nAllSize(aAllSequence.hasElements() ? aAllSequence.getLength() : 0);
308 13 : const sal_uInt32 nVisibleSize(aVisibleSequence.hasElements() ? aVisibleSequence.getLength() : 0);
309 :
310 13 : if((bTestVisibility && nVisibleSize) || nAllSize)
311 : {
312 : // for getting the 3D range using getB3DRangeFromPrimitive3DSequence a ViewInformation3D
313 : // needs to be given for evtl. decompositions. At the same time createViewInformation3D
314 : // currently is based on creating the target-ViewInformation3D using a given range. To
315 : // get the true range, use a neutral ViewInformation3D here. This leaves all matrices
316 : // on identity and the time on 0.0.
317 13 : const uno::Sequence< beans::PropertyValue > aEmptyProperties;
318 26 : const drawinglayer::geometry::ViewInformation3D aNeutralViewInformation3D(aEmptyProperties);
319 : const basegfx::B3DRange aContentRange(
320 13 : drawinglayer::primitive3d::getB3DRangeFromPrimitive3DSequence(aAllSequence, aNeutralViewInformation3D));
321 :
322 : // create 2d primitive 3dscene with generated sub-list from collector
323 : const drawinglayer::primitive2d::Primitive2DReference xReference(
324 : new drawinglayer::primitive2d::ScenePrimitive2D(
325 : bTestVisibility ? aVisibleSequence : aAllSequence,
326 : getSdrSceneAttribute(),
327 : getSdrLightingAttribute(),
328 : getObjectTransformation(),
329 26 : getViewInformation3D(aContentRange)));
330 :
331 26 : xRetval = drawinglayer::primitive2d::Primitive2DSequence(&xReference, 1);
332 13 : }
333 : }
334 :
335 : // always append an invisible outline for the cases where no visible content exists
336 : drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(xRetval,
337 : drawinglayer::primitive2d::createHiddenGeometryPrimitives2D(
338 13 : false, getObjectTransformation()));
339 :
340 13 : return xRetval;
341 : }
342 :
343 12 : drawinglayer::primitive2d::Primitive2DSequence ViewContactOfE3dScene::createViewIndependentPrimitive2DSequence() const
344 : {
345 12 : drawinglayer::primitive2d::Primitive2DSequence xRetval;
346 :
347 12 : if(GetObjectCount())
348 : {
349 : // create a default ScenePrimitive2D (without visibility test of members)
350 12 : xRetval = createScenePrimitive2DSequence(0);
351 : }
352 :
353 12 : return xRetval;
354 : }
355 :
356 1711 : void ViewContactOfE3dScene::ActionChanged()
357 : {
358 : // call parent
359 1711 : ViewContactOfSdrObj::ActionChanged();
360 :
361 : // mark locally cached values as invalid
362 1711 : maViewInformation3D = drawinglayer::geometry::ViewInformation3D();
363 1711 : maObjectTransformation.identity();
364 1711 : maSdrSceneAttribute = drawinglayer::attribute::SdrSceneAttribute();
365 1711 : maSdrLightingAttribute = drawinglayer::attribute::SdrLightingAttribute();
366 1711 : }
367 :
368 946 : const drawinglayer::geometry::ViewInformation3D& ViewContactOfE3dScene::getViewInformation3D() const
369 : {
370 946 : if(maViewInformation3D.isDefault())
371 : {
372 : // this version will create the content range on demand locally and thus is less
373 : // performant than the other one. Since the information is buffered the planned
374 : // behaviour is that the version with the given range is used initially.
375 91 : basegfx::B3DRange aContentRange(getAllContentRange3D());
376 :
377 91 : if(aContentRange.isEmpty())
378 : {
379 : // empty scene, no 3d action should be necessary. Prepare some
380 : // fallback size
381 : OSL_FAIL("No need to get ViewInformation3D from an empty scene (!)");
382 12 : aContentRange.expand(basegfx::B3DPoint(-100.0, -100.0, -100.0));
383 12 : aContentRange.expand(basegfx::B3DPoint( 100.0, 100.0, 100.0));
384 : }
385 :
386 91 : const_cast < ViewContactOfE3dScene* >(this)->createViewInformation3D(aContentRange);
387 : }
388 :
389 946 : return maViewInformation3D;
390 : }
391 :
392 83 : const drawinglayer::geometry::ViewInformation3D& ViewContactOfE3dScene::getViewInformation3D(const basegfx::B3DRange& rContentRange) const
393 : {
394 83 : if(maViewInformation3D.isDefault())
395 : {
396 48 : const_cast < ViewContactOfE3dScene* >(this)->createViewInformation3D(rContentRange);
397 : }
398 :
399 83 : return maViewInformation3D;
400 : }
401 :
402 1042 : const basegfx::B2DHomMatrix& ViewContactOfE3dScene::getObjectTransformation() const
403 : {
404 1042 : if(maObjectTransformation.isIdentity())
405 : {
406 139 : const_cast < ViewContactOfE3dScene* >(this)->createObjectTransformation();
407 : }
408 :
409 1042 : return maObjectTransformation;
410 : }
411 :
412 152 : const drawinglayer::attribute::SdrSceneAttribute& ViewContactOfE3dScene::getSdrSceneAttribute() const
413 : {
414 152 : if(maSdrSceneAttribute.isDefault())
415 : {
416 139 : const_cast < ViewContactOfE3dScene* >(this)->createSdrSceneAttribute();
417 : }
418 :
419 152 : return maSdrSceneAttribute;
420 : }
421 :
422 13 : const drawinglayer::attribute::SdrLightingAttribute& ViewContactOfE3dScene::getSdrLightingAttribute() const
423 : {
424 13 : if(maSdrLightingAttribute.isDefault())
425 : {
426 11 : const_cast < ViewContactOfE3dScene* >(this)->createSdrLightingAttribute();
427 : }
428 :
429 13 : return maSdrLightingAttribute;
430 : }
431 :
432 231 : drawinglayer::primitive3d::Primitive3DSequence ViewContactOfE3dScene::getAllPrimitive3DSequence() const
433 : {
434 231 : drawinglayer::primitive3d::Primitive3DSequence aAllPrimitive3DSequence;
435 231 : const sal_uInt32 nChildrenCount(GetObjectCount());
436 :
437 : // add children recursively. Do NOT start with (*this), this would create
438 : // a 3D transformPrimitive for the start scene. While this is theoretically not
439 : // a bad thing, for historical reasons the transformation of the outmost scene
440 : // is seen as part of the ViewTransformation (see text in createViewInformation3D)
441 462 : for(sal_uInt32 a(0L); a < nChildrenCount; a++)
442 : {
443 231 : createSubPrimitive3DVector(GetViewContact(a), aAllPrimitive3DSequence, 0, 0, false);
444 : }
445 :
446 231 : return aAllPrimitive3DSequence;
447 : }
448 :
449 231 : basegfx::B3DRange ViewContactOfE3dScene::getAllContentRange3D() const
450 : {
451 231 : const drawinglayer::primitive3d::Primitive3DSequence xAllSequence(getAllPrimitive3DSequence());
452 231 : basegfx::B3DRange aAllContentRange3D;
453 :
454 231 : if(xAllSequence.hasElements())
455 : {
456 : // for getting the 3D range using getB3DRangeFromPrimitive3DSequence a ViewInformation3D
457 : // needs to be given for evtl. decompositions. Use a neutral ViewInformation3D here. This
458 : // leaves all matrices on identity and the time on 0.0.
459 231 : const uno::Sequence< beans::PropertyValue > aEmptyProperties;
460 462 : const drawinglayer::geometry::ViewInformation3D aNeutralViewInformation3D(aEmptyProperties);
461 :
462 462 : aAllContentRange3D = drawinglayer::primitive3d::getB3DRangeFromPrimitive3DSequence(xAllSequence, aNeutralViewInformation3D);
463 : }
464 :
465 231 : return aAllContentRange3D;
466 : }
467 : } // end of namespace contact
468 : } // end of namespace sdr
469 :
470 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|