1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <svx/sdr/contact/viewcontactofe3d.hxx>
28cdf0e10cSrcweir #include <svx/sdr/contact/viewobjectcontactofe3d.hxx>
29cdf0e10cSrcweir #include <svx/obj3d.hxx>
30cdf0e10cSrcweir #include <drawinglayer/primitive2d/embedded3dprimitive2d.hxx>
31cdf0e10cSrcweir #include <svx/sdr/contact/viewcontactofe3dscene.hxx>
32cdf0e10cSrcweir #include <svx/scene3d.hxx>
33cdf0e10cSrcweir #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
34cdf0e10cSrcweir #include <drawinglayer/attribute/sdrsceneattribute3d.hxx>
35cdf0e10cSrcweir #include <drawinglayer/attribute/sdrlightingattribute3d.hxx>
36cdf0e10cSrcweir #include <drawinglayer/attribute/sdrlightattribute3d.hxx>
37cdf0e10cSrcweir #include <drawinglayer/attribute/sdrlineattribute.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace
42cdf0e10cSrcweir {
tryToFindVCOfE3DScene(const sdr::contact::ViewContact & rCandidate,basegfx::B3DHomMatrix & o_rInBetweenObjectTransform)43cdf0e10cSrcweir     const sdr::contact::ViewContactOfE3dScene* tryToFindVCOfE3DScene(
44cdf0e10cSrcweir         const sdr::contact::ViewContact& rCandidate,
45cdf0e10cSrcweir         basegfx::B3DHomMatrix& o_rInBetweenObjectTransform)
46cdf0e10cSrcweir     {
47cdf0e10cSrcweir 	    const sdr::contact::ViewContactOfE3dScene* pSceneParent =
48cdf0e10cSrcweir             dynamic_cast< const sdr::contact::ViewContactOfE3dScene* >(rCandidate.GetParentContact());
49cdf0e10cSrcweir 
50cdf0e10cSrcweir         if(pSceneParent)
51cdf0e10cSrcweir         {
52cdf0e10cSrcweir             // each 3d object (including in-between scenes) should have a scene as parent
53cdf0e10cSrcweir 		    const sdr::contact::ViewContactOfE3dScene* pSceneParentParent =
54cdf0e10cSrcweir                 dynamic_cast< const sdr::contact::ViewContactOfE3dScene* >(pSceneParent->GetParentContact());
55cdf0e10cSrcweir 
56cdf0e10cSrcweir             if(pSceneParentParent)
57cdf0e10cSrcweir             {
58cdf0e10cSrcweir                 // the parent scene of rCandidate is a in-between scene, call recursively and collect
59cdf0e10cSrcweir                 // the in-between scene's object transformation part in o_rInBetweenObjectTransform
60cdf0e10cSrcweir                 const basegfx::B3DHomMatrix& rSceneParentTransform = pSceneParent->GetE3dScene().GetTransform();
61cdf0e10cSrcweir                 o_rInBetweenObjectTransform = rSceneParentTransform * o_rInBetweenObjectTransform;
62cdf0e10cSrcweir 			    return tryToFindVCOfE3DScene(*pSceneParent, o_rInBetweenObjectTransform);
63cdf0e10cSrcweir             }
64cdf0e10cSrcweir             else
65cdf0e10cSrcweir             {
66cdf0e10cSrcweir                 // the parent scene is the outmost scene
67cdf0e10cSrcweir                 return pSceneParent;
68cdf0e10cSrcweir             }
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         // object hierarchy structure is incorrect; no result
72cdf0e10cSrcweir 	    return 0;
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir } // end of anonymous namespace
75cdf0e10cSrcweir 
76cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
77cdf0e10cSrcweir 
78cdf0e10cSrcweir namespace sdr
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	namespace contact
81cdf0e10cSrcweir 	{
impCreateWithGivenPrimitive3DSequence(const drawinglayer::primitive3d::Primitive3DSequence & rxContent3D) const82cdf0e10cSrcweir 		drawinglayer::primitive2d::Primitive2DSequence ViewContactOfE3d::impCreateWithGivenPrimitive3DSequence(
83cdf0e10cSrcweir             const drawinglayer::primitive3d::Primitive3DSequence& rxContent3D) const
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir 			drawinglayer::primitive2d::Primitive2DSequence xRetval;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             if(rxContent3D.hasElements())
88cdf0e10cSrcweir 			{
89cdf0e10cSrcweir 				// try to get the outmost ViewObjectContactOfE3dScene for this single 3d object,
90cdf0e10cSrcweir                 // the ones on the way there are grouping scenes. Collect the in-between scene's
91cdf0e10cSrcweir                 // transformations to build a correct object transformation for the embedded
92cdf0e10cSrcweir                 // object
93cdf0e10cSrcweir                 basegfx::B3DHomMatrix aInBetweenObjectTransform;
94cdf0e10cSrcweir 				const ViewContactOfE3dScene* pVCOfE3DScene = tryToFindVCOfE3DScene(*this, aInBetweenObjectTransform);
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 				if(pVCOfE3DScene)
97cdf0e10cSrcweir 				{
98cdf0e10cSrcweir 					basegfx::B3DVector aLightNormal;
99cdf0e10cSrcweir                     const double fShadowSlant(pVCOfE3DScene->getSdrSceneAttribute().getShadowSlant());
100cdf0e10cSrcweir                     const basegfx::B3DRange& rAllContentRange = pVCOfE3DScene->getAllContentRange3D();
101cdf0e10cSrcweir             		drawinglayer::geometry::ViewInformation3D aViewInformation3D(pVCOfE3DScene->getViewInformation3D());
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 					if(pVCOfE3DScene->getSdrLightingAttribute().getLightVector().size())
104cdf0e10cSrcweir 					{
105cdf0e10cSrcweir 						// get light normal from first light and normalize
106cdf0e10cSrcweir 						aLightNormal = pVCOfE3DScene->getSdrLightingAttribute().getLightVector()[0].getDirection();
107cdf0e10cSrcweir 						aLightNormal.normalize();
108cdf0e10cSrcweir 					}
109cdf0e10cSrcweir 
110cdf0e10cSrcweir                     if(!aInBetweenObjectTransform.isIdentity())
111cdf0e10cSrcweir                     {
112cdf0e10cSrcweir                         // if aInBetweenObjectTransform is used, create combined ViewInformation3D which
113cdf0e10cSrcweir                         // contains the correct object transformation for the embedded 3d object
114cdf0e10cSrcweir                         aViewInformation3D = drawinglayer::geometry::ViewInformation3D(
115cdf0e10cSrcweir                             aViewInformation3D.getObjectTransformation() * aInBetweenObjectTransform,
116cdf0e10cSrcweir                             aViewInformation3D.getOrientation(),
117cdf0e10cSrcweir                             aViewInformation3D.getProjection(),
118cdf0e10cSrcweir                             aViewInformation3D.getDeviceToView(),
119cdf0e10cSrcweir                             aViewInformation3D.getViewTime(),
120cdf0e10cSrcweir                             aViewInformation3D.getExtendedInformationSequence());
121cdf0e10cSrcweir                     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 					// create embedded 2d primitive and add. LightNormal and ShadowSlant are needed for evtl.
124cdf0e10cSrcweir 					// 3D shadow extraction for correct B2DRange calculation (shadow is part of the object)
125cdf0e10cSrcweir                     const drawinglayer::primitive2d::Primitive2DReference xReference(
126cdf0e10cSrcweir 						new drawinglayer::primitive2d::Embedded3DPrimitive2D(
127cdf0e10cSrcweir 							rxContent3D,
128cdf0e10cSrcweir 							pVCOfE3DScene->getObjectTransformation(),
129cdf0e10cSrcweir 							aViewInformation3D,
130cdf0e10cSrcweir 							aLightNormal,
131cdf0e10cSrcweir 							fShadowSlant,
132cdf0e10cSrcweir 							rAllContentRange));
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 					xRetval = drawinglayer::primitive2d::Primitive2DSequence(&xReference, 1);
135cdf0e10cSrcweir 				}
136cdf0e10cSrcweir 			}
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 			return xRetval;
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir 
ViewContactOfE3d(E3dObject & rSdrObject)141cdf0e10cSrcweir 		ViewContactOfE3d::ViewContactOfE3d(E3dObject& rSdrObject)
142cdf0e10cSrcweir 		:	ViewContactOfSdrObj(rSdrObject)
143cdf0e10cSrcweir 		{
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir 
~ViewContactOfE3d()146cdf0e10cSrcweir 		ViewContactOfE3d::~ViewContactOfE3d()
147cdf0e10cSrcweir 		{
148cdf0e10cSrcweir 		}
149cdf0e10cSrcweir 
getVIP3DSWithoutObjectTransform() const150cdf0e10cSrcweir 		drawinglayer::primitive3d::Primitive3DSequence ViewContactOfE3d::getVIP3DSWithoutObjectTransform() const
151cdf0e10cSrcweir 		{
152cdf0e10cSrcweir 			// local up-to-date checks. Create new list and compare.
153cdf0e10cSrcweir 			drawinglayer::primitive3d::Primitive3DSequence xNew(createViewIndependentPrimitive3DSequence());
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 			if(!drawinglayer::primitive3d::arePrimitive3DSequencesEqual(mxViewIndependentPrimitive3DSequence, xNew))
156cdf0e10cSrcweir 			{
157cdf0e10cSrcweir 				// has changed, copy content
158cdf0e10cSrcweir 				const_cast< ViewContactOfE3d* >(this)->mxViewIndependentPrimitive3DSequence = xNew;
159cdf0e10cSrcweir 			}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 			// return current Primitive2DSequence
162cdf0e10cSrcweir 			return mxViewIndependentPrimitive3DSequence;
163cdf0e10cSrcweir 		}
164cdf0e10cSrcweir 
getViewIndependentPrimitive3DSequence() const165cdf0e10cSrcweir 		drawinglayer::primitive3d::Primitive3DSequence ViewContactOfE3d::getViewIndependentPrimitive3DSequence() const
166cdf0e10cSrcweir 		{
167cdf0e10cSrcweir 			// get sequence without object transform
168cdf0e10cSrcweir 			drawinglayer::primitive3d::Primitive3DSequence xRetval(getVIP3DSWithoutObjectTransform());
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 			if(xRetval.hasElements())
171cdf0e10cSrcweir 			{
172cdf0e10cSrcweir 				// add object transform if it's used
173cdf0e10cSrcweir 				const basegfx::B3DHomMatrix& rObjectTransform(GetE3dObject().GetTransform());
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 				if(!rObjectTransform.isIdentity())
176cdf0e10cSrcweir 				{
177cdf0e10cSrcweir 					const drawinglayer::primitive3d::Primitive3DReference xReference(
178cdf0e10cSrcweir 						new drawinglayer::primitive3d::TransformPrimitive3D(
179cdf0e10cSrcweir 							rObjectTransform,
180cdf0e10cSrcweir 							xRetval));
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 					xRetval = drawinglayer::primitive3d::Primitive3DSequence(&xReference, 1);
183cdf0e10cSrcweir 				}
184cdf0e10cSrcweir 			}
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 			// return current Primitive2DSequence
187cdf0e10cSrcweir 			return xRetval;
188cdf0e10cSrcweir 		}
189cdf0e10cSrcweir 
createViewIndependentPrimitive2DSequence() const190cdf0e10cSrcweir 		drawinglayer::primitive2d::Primitive2DSequence ViewContactOfE3d::createViewIndependentPrimitive2DSequence() const
191cdf0e10cSrcweir 		{
192cdf0e10cSrcweir             // also need to create a 2D embedding when the view-independent part is requested,
193cdf0e10cSrcweir 			// see view-dependent part in ViewObjectContactOfE3d::createPrimitive2DSequence
194cdf0e10cSrcweir 			// get 3d primitive vector, isPrimitiveVisible() is done in 3d creator
195cdf0e10cSrcweir     		return impCreateWithGivenPrimitive3DSequence(getViewIndependentPrimitive3DSequence());
196cdf0e10cSrcweir 		}
197cdf0e10cSrcweir 
CreateObjectSpecificViewObjectContact(ObjectContact & rObjectContact)198cdf0e10cSrcweir 		ViewObjectContact& ViewContactOfE3d::CreateObjectSpecificViewObjectContact(ObjectContact& rObjectContact)
199cdf0e10cSrcweir 		{
200cdf0e10cSrcweir 			ViewObjectContact* pRetval = new ViewObjectContactOfE3d(rObjectContact, *this);
201cdf0e10cSrcweir 			DBG_ASSERT(pRetval, "ViewContactOfE3d::CreateObjectSpecificViewObjectContact() failed (!)");
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 			return *pRetval;
204cdf0e10cSrcweir 		}
205cdf0e10cSrcweir 
impCreateFallbackLineAttribute(const basegfx::BColor & rBColor) const206cdf0e10cSrcweir 		drawinglayer::attribute::SdrLineAttribute* ViewContactOfE3d::impCreateFallbackLineAttribute(const basegfx::BColor& rBColor) const
207cdf0e10cSrcweir 		{
208cdf0e10cSrcweir 			static bool bFallbackToCreateAsLineForTest(false);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 			if(bFallbackToCreateAsLineForTest)
211cdf0e10cSrcweir 			{
212cdf0e10cSrcweir 				return new drawinglayer::attribute::SdrLineAttribute(rBColor);
213cdf0e10cSrcweir 			}
214cdf0e10cSrcweir 			else
215cdf0e10cSrcweir 			{
216cdf0e10cSrcweir 				return 0;
217cdf0e10cSrcweir 			}
218cdf0e10cSrcweir 		}
219cdf0e10cSrcweir 	} // end of namespace contact
220cdf0e10cSrcweir } // end of namespace sdr
221cdf0e10cSrcweir 
222cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
223cdf0e10cSrcweir // eof
224