1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_drawinglayer.hxx"
26 
27 #include <drawinglayer/processor3d/shadow3dextractor.hxx>
28 #include <drawinglayer/primitive3d/shadowprimitive3d.hxx>
29 #include <drawinglayer/primitive2d/shadowprimitive2d.hxx>
30 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
31 #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
32 #include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
33 #include <basegfx/polygon/b2dpolygontools.hxx>
34 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
35 #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
36 #include <basegfx/polygon/b2dpolypolygontools.hxx>
37 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
38 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
39 
40 //////////////////////////////////////////////////////////////////////////////
41 
42 using namespace com::sun::star;
43 
44 //////////////////////////////////////////////////////////////////////////////
45 
46 namespace drawinglayer
47 {
48 	namespace processor3d
49 	{
50         // as tooling, the process() implementation takes over API handling and calls this
51 		// virtual render method when the primitive implementation is BasePrimitive3D-based.
processBasePrimitive3D(const primitive3d::BasePrimitive3D & rCandidate)52 		void Shadow3DExtractingProcessor::processBasePrimitive3D(const primitive3d::BasePrimitive3D& rCandidate)
53 		{
54 			// it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch
55 			switch(rCandidate.getPrimitive3DID())
56 			{
57 				case PRIMITIVE3D_ID_SHADOWPRIMITIVE3D :
58 				{
59 					// shadow3d object. Call recursive with content and start conversion
60 					const primitive3d::ShadowPrimitive3D& rPrimitive = static_cast< const primitive3d::ShadowPrimitive3D& >(rCandidate);
61 
62 					// set new target
63 					primitive2d::Primitive2DVector aNewSubList;
64 					primitive2d::Primitive2DVector* pLastTargetSequence = mpPrimitive2DSequence;
65 					mpPrimitive2DSequence = &aNewSubList;
66 
67 					// activate convert
68 					const bool bLastConvert(mbConvert);
69 					mbConvert = true;
70 
71 					// set projection flag
72 					const bool bLastUseProjection(mbUseProjection);
73 					mbUseProjection = rPrimitive.getShadow3D();
74 
75 					// process content
76 					process(rPrimitive.getChildren());
77 
78 					// restore values
79 					mbUseProjection = bLastUseProjection;
80 					mbConvert = bLastConvert;
81 					mpPrimitive2DSequence = pLastTargetSequence;
82 
83 					// create 2d shadow primitive with result. This also fetches all entries
84                     // from aNewSubList, so there is no need to delete them
85 					primitive2d::BasePrimitive2D* pNew = new primitive2d::ShadowPrimitive2D(
86                         rPrimitive.getShadowTransform(),
87                         rPrimitive.getShadowColor(),
88                         primitive2d::Primitive2DVectorToPrimitive2DSequence(aNewSubList));
89 
90 					if(basegfx::fTools::more(rPrimitive.getShadowTransparence(), 0.0))
91 					{
92 						// create simpleTransparencePrimitive, add created primitives
93     					const primitive2d::Primitive2DReference xRef(pNew);
94 						const primitive2d::Primitive2DSequence aNewTransPrimitiveVector(&xRef, 1);
95 
96                         pNew = new primitive2d::UnifiedTransparencePrimitive2D(
97                             aNewTransPrimitiveVector,
98                             rPrimitive.getShadowTransparence());
99                     }
100 
101                     mpPrimitive2DSequence->push_back(pNew);
102 
103 					break;
104 				}
105 				case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D :
106 				{
107 					// transform group. Remember current transformations
108 					const primitive3d::TransformPrimitive3D& rPrimitive = static_cast< const primitive3d::TransformPrimitive3D& >(rCandidate);
109 					const geometry::ViewInformation3D aLastViewInformation3D(getViewInformation3D());
110 
111 					// create new transformation; add new object transform from right side
112 					const geometry::ViewInformation3D aNewViewInformation3D(
113 						aLastViewInformation3D.getObjectTransformation() * rPrimitive.getTransformation(),
114 						aLastViewInformation3D.getOrientation(),
115 						aLastViewInformation3D.getProjection(),
116 						aLastViewInformation3D.getDeviceToView(),
117 						aLastViewInformation3D.getViewTime(),
118 						aLastViewInformation3D.getExtendedInformationSequence());
119 					updateViewInformation(aNewViewInformation3D);
120 
121 					if(mbShadowProjectionIsValid)
122 					{
123 						// update buffered WorldToEye and EyeToView
124 						maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
125 						maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
126 					}
127 
128 					// let break down
129 					process(rPrimitive.getChildren());
130 
131 					// restore transformations
132 					updateViewInformation(aLastViewInformation3D);
133 
134 					if(mbShadowProjectionIsValid)
135 					{
136 						// update buffered WorldToEye and EyeToView
137 						maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
138 						maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
139 					}
140 					break;
141 				}
142 				case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D :
143 				{
144 					// PolygonHairlinePrimitive3D
145 					if(mbConvert)
146 					{
147 						const primitive3d::PolygonHairlinePrimitive3D& rPrimitive = static_cast< const primitive3d::PolygonHairlinePrimitive3D& >(rCandidate);
148 						basegfx::B2DPolygon a2DHairline;
149 
150 						if(mbUseProjection)
151 						{
152 							if(mbShadowProjectionIsValid)
153 							{
154 								a2DHairline = impDoShadowProjection(rPrimitive.getB3DPolygon());
155 							}
156 						}
157 						else
158 						{
159 							a2DHairline = basegfx::tools::createB2DPolygonFromB3DPolygon(rPrimitive.getB3DPolygon(), getViewInformation3D().getObjectToView());
160 						}
161 
162 						if(a2DHairline.count())
163 						{
164 							a2DHairline.transform(getObjectTransformation());
165                             mpPrimitive2DSequence->push_back(
166                                 new primitive2d::PolygonHairlinePrimitive2D(
167                                     a2DHairline,
168                                     maPrimitiveColor));
169 						}
170 					}
171 					break;
172 				}
173 				case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D :
174 				{
175 					// PolyPolygonMaterialPrimitive3D
176 					if(mbConvert)
177 					{
178 						const primitive3d::PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D& >(rCandidate);
179 						basegfx::B2DPolyPolygon a2DFill;
180 
181 						if(mbUseProjection)
182 						{
183 							if(mbShadowProjectionIsValid)
184 							{
185 								a2DFill = impDoShadowProjection(rPrimitive.getB3DPolyPolygon());
186 							}
187 						}
188 						else
189 						{
190 							a2DFill = basegfx::tools::createB2DPolyPolygonFromB3DPolyPolygon(rPrimitive.getB3DPolyPolygon(), getViewInformation3D().getObjectToView());
191 						}
192 
193 						if(a2DFill.count())
194 						{
195 							a2DFill.transform(getObjectTransformation());
196                             mpPrimitive2DSequence->push_back(
197                                 new primitive2d::PolyPolygonColorPrimitive2D(
198                                     a2DFill,
199                                     maPrimitiveColor));
200 						}
201 					}
202 					break;
203 				}
204 				default :
205 				{
206 					// process recursively
207 					process(rCandidate.get3DDecomposition(getViewInformation3D()));
208 					break;
209 				}
210 			}
211 		}
212 
Shadow3DExtractingProcessor(const geometry::ViewInformation3D & rViewInformation,const basegfx::B2DHomMatrix & rObjectTransformation,const basegfx::B3DVector & rLightNormal,double fShadowSlant,const basegfx::B3DRange & rContained3DRange)213 		Shadow3DExtractingProcessor::Shadow3DExtractingProcessor(
214 			const geometry::ViewInformation3D& rViewInformation,
215 			const basegfx::B2DHomMatrix& rObjectTransformation,
216 			const basegfx::B3DVector& rLightNormal,
217 			double fShadowSlant,
218             const basegfx::B3DRange& rContained3DRange)
219 		:	BaseProcessor3D(rViewInformation),
220 			maPrimitive2DSequence(),
221 			mpPrimitive2DSequence(&maPrimitive2DSequence),
222 			maObjectTransformation(rObjectTransformation),
223 			maWorldToEye(),
224 			maEyeToView(),
225 			maLightNormal(rLightNormal),
226 			maShadowPlaneNormal(),
227 			maPlanePoint(),
228 			mfLightPlaneScalar(0.0),
229 			maPrimitiveColor(),
230 			mbShadowProjectionIsValid(false),
231 			mbConvert(false),
232 			mbUseProjection(false)
233 		{
234 			// normalize light normal, get and normalize shadow plane normal and calculate scalar from it
235 			maLightNormal.normalize();
236 			maShadowPlaneNormal = basegfx::B3DVector(0.0, sin(fShadowSlant), cos(fShadowSlant));
237 			maShadowPlaneNormal.normalize();
238 			mfLightPlaneScalar = maLightNormal.scalar(maShadowPlaneNormal);
239 
240 			// use only when scalar is > 0.0, so the light is in front of the object
241 			if(basegfx::fTools::more(mfLightPlaneScalar, 0.0))
242 			{
243 				// prepare buffered WorldToEye and EyeToView
244 				maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
245 				maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
246 
247 				// calculate range to get front edge around which to rotate the shadow's projection
248 				basegfx::B3DRange aContained3DRange(rContained3DRange);
249 				aContained3DRange.transform(getWorldToEye());
250 				maPlanePoint.setX(maShadowPlaneNormal.getX() < 0.0 ? aContained3DRange.getMinX() : aContained3DRange.getMaxX());
251 				maPlanePoint.setY(maShadowPlaneNormal.getY() > 0.0 ? aContained3DRange.getMinY() : aContained3DRange.getMaxY());
252 				maPlanePoint.setZ(aContained3DRange.getMinZ() - (aContained3DRange.getDepth() / 8.0));
253 
254 				// set flag that shadow projection is prepared and allowed
255 				mbShadowProjectionIsValid = true;
256 			}
257 		}
258 
~Shadow3DExtractingProcessor()259     	Shadow3DExtractingProcessor::~Shadow3DExtractingProcessor()
260         {
261             OSL_ENSURE(0 == maPrimitive2DSequence.size(),
262                 "OOps, someone used Shadow3DExtractingProcessor, but did not fetch the results (!)");
263             for(sal_uInt32 a(0); a < maPrimitive2DSequence.size(); a++)
264             {
265                 delete maPrimitive2DSequence[a];
266             }
267         }
268 
impDoShadowProjection(const basegfx::B3DPolygon & rSource)269 		basegfx::B2DPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolygon& rSource)
270 		{
271 			basegfx::B2DPolygon aRetval;
272 
273 			for(sal_uInt32 a(0L); a < rSource.count(); a++)
274 			{
275 				// get point, transform to eye coordinate system
276 				basegfx::B3DPoint aCandidate(rSource.getB3DPoint(a));
277 				aCandidate *= getWorldToEye();
278 
279 				// we are in eye coordinates
280 				// ray is (aCandidate + fCut * maLightNormal)
281 				// plane is (maPlanePoint, maShadowPlaneNormal)
282 				// maLightNormal.scalar(maShadowPlaneNormal) is already in mfLightPlaneScalar and > 0.0
283 				// get cut point of ray with shadow plane
284 				const double fCut(basegfx::B3DVector(maPlanePoint - aCandidate).scalar(maShadowPlaneNormal) / mfLightPlaneScalar);
285 				aCandidate += maLightNormal * fCut;
286 
287 				// transform to view, use 2d coordinates
288 				aCandidate *= getEyeToView();
289 				aRetval.append(basegfx::B2DPoint(aCandidate.getX(), aCandidate.getY()));
290 			}
291 
292 			// copy closed flag
293 			aRetval.setClosed(rSource.isClosed());
294 
295 			return aRetval;
296 		}
297 
impDoShadowProjection(const basegfx::B3DPolyPolygon & rSource)298 		basegfx::B2DPolyPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolyPolygon& rSource)
299 		{
300 			basegfx::B2DPolyPolygon aRetval;
301 
302 			for(sal_uInt32 a(0L); a < rSource.count(); a++)
303 			{
304 				aRetval.append(impDoShadowProjection(rSource.getB3DPolygon(a)));
305 			}
306 
307 			return aRetval;
308 		}
309 
getPrimitive2DSequence() const310         const primitive2d::Primitive2DSequence Shadow3DExtractingProcessor::getPrimitive2DSequence() const
311         {
312             return Primitive2DVectorToPrimitive2DSequence(maPrimitive2DSequence);
313         }
314 
315     } // end of namespace processor3d
316 } // end of namespace drawinglayer
317 
318 //////////////////////////////////////////////////////////////////////////////
319 // eof
320