Update ooo320-m1
[ooovba.git] / svx / source / sdr / contact / viewcontactofe3d.cxx
blob948bab1b2fca173aa7d270c7c8c3d5e6ba680072
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: viewcontactofe3d.cxx,v $
11 * $Revision: 1.2.18.1 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_svx.hxx"
35 #include <svx/sdr/contact/viewcontactofe3d.hxx>
36 #include <svx/sdr/contact/viewobjectcontactofe3d.hxx>
37 #include <svx/obj3d.hxx>
38 #include <drawinglayer/primitive2d/embedded3dprimitive2d.hxx>
39 #include <svx/sdr/contact/viewcontactofe3dscene.hxx>
40 #include <drawinglayer/attribute/sdrattribute.hxx>
41 #include <drawinglayer/attribute/sdrattribute3d.hxx>
42 #include <svx/scene3d.hxx>
43 #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
45 //////////////////////////////////////////////////////////////////////////////
47 namespace
49 const sdr::contact::ViewContactOfE3dScene* tryToFindVCOfE3DScene(
50 const sdr::contact::ViewContact& rCandidate,
51 basegfx::B3DHomMatrix& o_rInBetweenObjectTransform)
53 const sdr::contact::ViewContactOfE3dScene* pSceneParent =
54 dynamic_cast< const sdr::contact::ViewContactOfE3dScene* >(rCandidate.GetParentContact());
56 if(pSceneParent)
58 // each 3d object (including in-between scenes) should have a scene as parent
59 const sdr::contact::ViewContactOfE3dScene* pSceneParentParent =
60 dynamic_cast< const sdr::contact::ViewContactOfE3dScene* >(pSceneParent->GetParentContact());
62 if(pSceneParentParent)
64 // the parent scene of rCandidate is a in-between scene, call recursively and collect
65 // the in-between scene's object transformation part in o_rInBetweenObjectTransform
66 const basegfx::B3DHomMatrix& rSceneParentTransform = pSceneParent->GetE3dScene().GetTransform();
67 o_rInBetweenObjectTransform = rSceneParentTransform * o_rInBetweenObjectTransform;
68 return tryToFindVCOfE3DScene(*pSceneParent, o_rInBetweenObjectTransform);
70 else
72 // the parent scene is the outmost scene
73 return pSceneParent;
77 // object hierarchy structure is incorrect; no result
78 return 0;
80 } // end of anonymous namespace
82 //////////////////////////////////////////////////////////////////////////////
84 namespace sdr
86 namespace contact
88 drawinglayer::primitive2d::Primitive2DSequence ViewContactOfE3d::impCreateWithGivenPrimitive3DSequence(
89 const drawinglayer::primitive3d::Primitive3DSequence& rxContent3D) const
91 drawinglayer::primitive2d::Primitive2DSequence xRetval;
93 if(rxContent3D.hasElements())
95 // try to get the outmost ViewObjectContactOfE3dScene for this single 3d object,
96 // the ones on the way there are grouping scenes. Collect the in-between scene's
97 // transformations to build a correct object transformation for the embedded
98 // object
99 basegfx::B3DHomMatrix aInBetweenObjectTransform;
100 const ViewContactOfE3dScene* pVCOfE3DScene = tryToFindVCOfE3DScene(*this, aInBetweenObjectTransform);
102 if(pVCOfE3DScene)
104 basegfx::B3DVector aLightNormal;
105 const double fShadowSlant(pVCOfE3DScene->getSdrSceneAttribute().getShadowSlant());
106 const basegfx::B3DRange& rAllContentRange = pVCOfE3DScene->getAllContentRange3D();
107 drawinglayer::geometry::ViewInformation3D aViewInformation3D(pVCOfE3DScene->getViewInformation3D());
109 if(pVCOfE3DScene->getSdrLightingAttribute().getLightVector().size())
111 // get light normal from first light and normalize
112 aLightNormal = pVCOfE3DScene->getSdrLightingAttribute().getLightVector()[0].getDirection();
113 aLightNormal.normalize();
116 if(!aInBetweenObjectTransform.isIdentity())
118 // if aInBetweenObjectTransform is used, create combined ViewInformation3D which
119 // contains the correct object transformation for the embedded 3d object
120 aViewInformation3D = drawinglayer::geometry::ViewInformation3D(
121 aViewInformation3D.getObjectTransformation() * aInBetweenObjectTransform,
122 aViewInformation3D.getOrientation(),
123 aViewInformation3D.getProjection(),
124 aViewInformation3D.getDeviceToView(),
125 aViewInformation3D.getViewTime(),
126 aViewInformation3D.getExtendedInformationSequence());
129 // create embedded 2d primitive and add. LightNormal and ShadowSlant are needed for evtl.
130 // 3D shadow extraction for correct B2DRange calculation (shadow is part of the object)
131 const drawinglayer::primitive2d::Primitive2DReference xReference(new drawinglayer::primitive2d::Embedded3DPrimitive2D(
132 rxContent3D,
133 pVCOfE3DScene->getObjectTransformation(),
134 aViewInformation3D,
135 aLightNormal,
136 fShadowSlant,
137 rAllContentRange));
139 xRetval = drawinglayer::primitive2d::Primitive2DSequence(&xReference, 1);
143 return xRetval;
146 ViewContactOfE3d::ViewContactOfE3d(E3dObject& rSdrObject)
147 : ViewContactOfSdrObj(rSdrObject)
151 ViewContactOfE3d::~ViewContactOfE3d()
155 drawinglayer::primitive3d::Primitive3DSequence ViewContactOfE3d::getVIP3DSWithoutObjectTransform() const
157 // local up-to-date checks. Create new list and compare.
158 drawinglayer::primitive3d::Primitive3DSequence xNew(createViewIndependentPrimitive3DSequence());
160 if(!drawinglayer::primitive3d::arePrimitive3DSequencesEqual(mxViewIndependentPrimitive3DSequence, xNew))
162 // has changed, copy content
163 const_cast< ViewContactOfE3d* >(this)->mxViewIndependentPrimitive3DSequence = xNew;
166 // return current Primitive2DSequence
167 return mxViewIndependentPrimitive3DSequence;
170 drawinglayer::primitive3d::Primitive3DSequence ViewContactOfE3d::getViewIndependentPrimitive3DSequence() const
172 // get sequence without object transform
173 drawinglayer::primitive3d::Primitive3DSequence xRetval(getVIP3DSWithoutObjectTransform());
175 if(xRetval.hasElements())
177 // add object transform if it's used
178 const basegfx::B3DHomMatrix& rObjectTransform(GetE3dObject().GetTransform());
180 if(!rObjectTransform.isIdentity())
182 const drawinglayer::primitive3d::Primitive3DReference xReference(new drawinglayer::primitive3d::TransformPrimitive3D(
183 rObjectTransform, xRetval));
184 return drawinglayer::primitive3d::Primitive3DSequence(&xReference, 1);
188 // return current Primitive2DSequence
189 return xRetval;
192 drawinglayer::primitive2d::Primitive2DSequence ViewContactOfE3d::createViewIndependentPrimitive2DSequence() const
194 // also need to create a 2D embedding when the view-independent part is requested,
195 // see view-dependent part in ViewObjectContactOfE3d::createPrimitive2DSequence
196 // get 3d primitive vector, isPrimitiveVisible() is done in 3d creator
197 return impCreateWithGivenPrimitive3DSequence(getViewIndependentPrimitive3DSequence());
200 ViewObjectContact& ViewContactOfE3d::CreateObjectSpecificViewObjectContact(ObjectContact& rObjectContact)
202 ViewObjectContact* pRetval = new ViewObjectContactOfE3d(rObjectContact, *this);
203 DBG_ASSERT(pRetval, "ViewContactOfE3d::CreateObjectSpecificViewObjectContact() failed (!)");
205 return *pRetval;
208 drawinglayer::attribute::SdrLineAttribute* ViewContactOfE3d::impCreateFallbackLineAttribute(const basegfx::BColor& rBColor) const
210 static bool bFallbackToCreateAsLineForTest(false);
212 if(bFallbackToCreateAsLineForTest)
214 return new drawinglayer::attribute::SdrLineAttribute(rBColor);
216 else
218 return 0;
221 } // end of namespace contact
222 } // end of namespace sdr
224 //////////////////////////////////////////////////////////////////////////////
225 // eof