Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / svx / source / sdr / contact / viewcontactofe3d.cxx
blobc824e784616c31ec766dcb6cc747f100877f6e42
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 .
20 #include <sdr/contact/viewcontactofe3d.hxx>
21 #include <sdr/contact/viewobjectcontactofe3d.hxx>
22 #include <svx/obj3d.hxx>
23 #include <drawinglayer/primitive2d/embedded3dprimitive2d.hxx>
24 #include <svx/sdr/contact/viewcontactofe3dscene.hxx>
25 #include <svx/scene3d.hxx>
26 #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
27 #include <drawinglayer/attribute/sdrsceneattribute3d.hxx>
28 #include <drawinglayer/attribute/sdrlightingattribute3d.hxx>
29 #include <drawinglayer/attribute/sdrlightattribute3d.hxx>
31 namespace {
33 const sdr::contact::ViewContactOfE3dScene* tryToFindVCOfE3DScene(
34 const sdr::contact::ViewContact& rCandidate,
35 basegfx::B3DHomMatrix& o_rInBetweenObjectTransform)
37 const sdr::contact::ViewContactOfE3dScene* pSceneParent =
38 dynamic_cast< const sdr::contact::ViewContactOfE3dScene* >(rCandidate.GetParentContact());
40 if(pSceneParent)
42 // each 3d object (including in-between scenes) should have a scene as parent
43 const sdr::contact::ViewContactOfE3dScene* pSceneParentParent =
44 dynamic_cast< const sdr::contact::ViewContactOfE3dScene* >(pSceneParent->GetParentContact());
46 if(pSceneParentParent)
48 // the parent scene of rCandidate is an in-between scene, call recursively and collect
49 // the in-between scene's object transformation part in o_rInBetweenObjectTransform
50 const basegfx::B3DHomMatrix& rSceneParentTransform = pSceneParent->GetE3dScene().GetTransform();
51 o_rInBetweenObjectTransform = rSceneParentTransform * o_rInBetweenObjectTransform;
52 return tryToFindVCOfE3DScene(*pSceneParent, o_rInBetweenObjectTransform);
54 else
56 // the parent scene is the outmost scene
57 return pSceneParent;
61 // object hierarchy structure is incorrect; no result
62 return nullptr;
65 } // end of anonymous namespace
67 namespace sdr::contact {
69 drawinglayer::primitive2d::Primitive2DContainer ViewContactOfE3d::impCreateWithGivenPrimitive3DContainer(
70 const drawinglayer::primitive3d::Primitive3DContainer& rxContent3D) const
72 drawinglayer::primitive2d::Primitive2DContainer xRetval;
74 if(!rxContent3D.empty())
76 // try to get the outmost ViewObjectContactOfE3dScene for this single 3d object,
77 // the ones on the way there are grouping scenes. Collect the in-between scene's
78 // transformations to build a correct object transformation for the embedded
79 // object
80 basegfx::B3DHomMatrix aInBetweenObjectTransform;
81 const ViewContactOfE3dScene* pVCOfE3DScene = tryToFindVCOfE3DScene(*this, aInBetweenObjectTransform);
83 if(pVCOfE3DScene)
85 basegfx::B3DVector aLightNormal;
86 const double fShadowSlant(pVCOfE3DScene->getSdrSceneAttribute().getShadowSlant());
87 const basegfx::B3DRange aAllContentRange = pVCOfE3DScene->getAllContentRange3D();
88 drawinglayer::geometry::ViewInformation3D aViewInformation3D(pVCOfE3DScene->getViewInformation3D());
90 if(!pVCOfE3DScene->getSdrLightingAttribute().getLightVector().empty())
92 // get light normal from first light and normalize
93 aLightNormal = pVCOfE3DScene->getSdrLightingAttribute().getLightVector()[0].getDirection();
94 aLightNormal.normalize();
97 if(!aInBetweenObjectTransform.isIdentity())
99 // if aInBetweenObjectTransform is used, create combined ViewInformation3D which
100 // contains the correct object transformation for the embedded 3d object
101 aViewInformation3D = drawinglayer::geometry::ViewInformation3D(
102 aViewInformation3D.getObjectTransformation() * aInBetweenObjectTransform,
103 aViewInformation3D.getOrientation(),
104 aViewInformation3D.getProjection(),
105 aViewInformation3D.getDeviceToView(),
106 aViewInformation3D.getViewTime(),
107 aViewInformation3D.getExtendedInformationSequence());
110 // create embedded 2d primitive and add. LightNormal and ShadowSlant are needed for evtl.
111 // 3D shadow extraction for correct B2DRange calculation (shadow is part of the object)
112 const drawinglayer::primitive2d::Primitive2DReference xReference(
113 new drawinglayer::primitive2d::Embedded3DPrimitive2D(
114 rxContent3D,
115 pVCOfE3DScene->getObjectTransformation(),
116 std::move(aViewInformation3D),
117 aLightNormal,
118 fShadowSlant,
119 aAllContentRange));
121 xRetval = drawinglayer::primitive2d::Primitive2DContainer { xReference };
125 return xRetval;
128 ViewContactOfE3d::ViewContactOfE3d(E3dObject& rSdrObject)
129 : ViewContactOfSdrObj(rSdrObject)
133 ViewContactOfE3d::~ViewContactOfE3d()
137 drawinglayer::primitive3d::Primitive3DContainer const & ViewContactOfE3d::getVIP3DSWithoutObjectTransform() const
139 // local up-to-date checks. Create new list and compare.
140 drawinglayer::primitive3d::Primitive3DContainer xNew(createViewIndependentPrimitive3DContainer());
142 if(mxViewIndependentPrimitive3DContainer != xNew)
144 // has changed, copy content
145 const_cast< ViewContactOfE3d* >(this)->mxViewIndependentPrimitive3DContainer = std::move(xNew);
148 // return current Primitive2DContainer
149 return mxViewIndependentPrimitive3DContainer;
152 drawinglayer::primitive3d::Primitive3DContainer ViewContactOfE3d::getViewIndependentPrimitive3DContainer() const
154 // get sequence without object transform
155 drawinglayer::primitive3d::Primitive3DContainer xRetval(getVIP3DSWithoutObjectTransform());
157 if(!xRetval.empty())
159 // add object transform if it's used
160 const basegfx::B3DHomMatrix& rObjectTransform(GetE3dObject().GetTransform());
162 if(!rObjectTransform.isIdentity())
164 const drawinglayer::primitive3d::Primitive3DReference xReference(
165 new drawinglayer::primitive3d::TransformPrimitive3D(
166 rObjectTransform,
167 xRetval));
169 xRetval = { xReference };
173 // return current Primitive2DContainer
174 return xRetval;
177 void ViewContactOfE3d::createViewIndependentPrimitive2DSequence(drawinglayer::primitive2d::Primitive2DDecompositionVisitor& rVisitor) const
179 // also need to create a 2D embedding when the view-independent part is requested,
180 // see view-dependent part in ViewObjectContactOfE3d::createPrimitive2DSequence
181 // get 3d primitive vector, isPrimitiveVisible() is done in 3d creator
182 return rVisitor.visit(impCreateWithGivenPrimitive3DContainer(getViewIndependentPrimitive3DContainer()));
185 ViewObjectContact& ViewContactOfE3d::CreateObjectSpecificViewObjectContact(ObjectContact& rObjectContact)
187 ViewObjectContact* pRetval = new ViewObjectContactOfE3d(rObjectContact, *this);
188 DBG_ASSERT(pRetval, "ViewContactOfE3d::CreateObjectSpecificViewObjectContact() failed (!)");
190 return *pRetval;
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */