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         /// helper to convert from BasePrimitive2DVector to primitive2d::Primitive2DSequence
51         const primitive2d::Primitive2DSequence Shadow3DExtractingProcessor::getPrimitive2DSequenceFromBasePrimitive2DVector(
52             const BasePrimitive2DVector& rVector) const
53         {
54             const sal_uInt32 nCount(rVector.size());
55             primitive2d::Primitive2DSequence aRetval(nCount);
56 
57             for(sal_uInt32 a(0); a < nCount; a++)
58             {
59                 aRetval[a] = rVector[a];
60             }
61 
62             // all entries taken over; no need to delete entries, just reset to
63             // mark as empty
64             const_cast< BasePrimitive2DVector& >(rVector).clear();
65 
66             return aRetval;
67         }
68 
69         // as tooling, the process() implementation takes over API handling and calls this
70 		// virtual render method when the primitive implementation is BasePrimitive3D-based.
71 		void Shadow3DExtractingProcessor::processBasePrimitive3D(const primitive3d::BasePrimitive3D& rCandidate)
72 		{
73 			// it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch
74 			switch(rCandidate.getPrimitive3DID())
75 			{
76 				case PRIMITIVE3D_ID_SHADOWPRIMITIVE3D :
77 				{
78 					// shadow3d object. Call recursive with content and start conversion
79 					const primitive3d::ShadowPrimitive3D& rPrimitive = static_cast< const primitive3d::ShadowPrimitive3D& >(rCandidate);
80 
81 					// set new target
82 					BasePrimitive2DVector aNewSubList;
83 					BasePrimitive2DVector* pLastTargetSequence = mpPrimitive2DSequence;
84 					mpPrimitive2DSequence = &aNewSubList;
85 
86 					// activate convert
87 					const bool bLastConvert(mbConvert);
88 					mbConvert = true;
89 
90 					// set projection flag
91 					const bool bLastUseProjection(mbUseProjection);
92 					mbUseProjection = rPrimitive.getShadow3D();
93 
94 					// process content
95 					process(rPrimitive.getChildren());
96 
97 					// restore values
98 					mbUseProjection = bLastUseProjection;
99 					mbConvert = bLastConvert;
100 					mpPrimitive2DSequence = pLastTargetSequence;
101 
102 					// create 2d shadow primitive with result. This also fetches all entries
103                     // from aNewSubList, so there is no need to delete them
104 					primitive2d::BasePrimitive2D* pNew = new primitive2d::ShadowPrimitive2D(
105                         rPrimitive.getShadowTransform(),
106                         rPrimitive.getShadowColor(),
107                         getPrimitive2DSequenceFromBasePrimitive2DVector(aNewSubList));
108 
109 					if(basegfx::fTools::more(rPrimitive.getShadowTransparence(), 0.0))
110 					{
111 						// create simpleTransparencePrimitive, add created primitives
112     					const primitive2d::Primitive2DReference xRef(pNew);
113 						const primitive2d::Primitive2DSequence aNewTransPrimitiveVector(&xRef, 1);
114 
115                         pNew = new primitive2d::UnifiedTransparencePrimitive2D(
116                             aNewTransPrimitiveVector,
117                             rPrimitive.getShadowTransparence());
118                     }
119 
120                     mpPrimitive2DSequence->push_back(pNew);
121 
122 					break;
123 				}
124 				case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D :
125 				{
126 					// transform group. Remember current transformations
127 					const primitive3d::TransformPrimitive3D& rPrimitive = static_cast< const primitive3d::TransformPrimitive3D& >(rCandidate);
128 					const geometry::ViewInformation3D aLastViewInformation3D(getViewInformation3D());
129 
130 					// create new transformation; add new object transform from right side
131 					const geometry::ViewInformation3D aNewViewInformation3D(
132 						aLastViewInformation3D.getObjectTransformation() * rPrimitive.getTransformation(),
133 						aLastViewInformation3D.getOrientation(),
134 						aLastViewInformation3D.getProjection(),
135 						aLastViewInformation3D.getDeviceToView(),
136 						aLastViewInformation3D.getViewTime(),
137 						aLastViewInformation3D.getExtendedInformationSequence());
138 					updateViewInformation(aNewViewInformation3D);
139 
140 					if(mbShadowProjectionIsValid)
141 					{
142 						// update buffered WorldToEye and EyeToView
143 						maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
144 						maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
145 					}
146 
147 					// let break down
148 					process(rPrimitive.getChildren());
149 
150 					// restore transformations
151 					updateViewInformation(aLastViewInformation3D);
152 
153 					if(mbShadowProjectionIsValid)
154 					{
155 						// update buffered WorldToEye and EyeToView
156 						maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
157 						maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
158 					}
159 					break;
160 				}
161 				case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D :
162 				{
163 					// PolygonHairlinePrimitive3D
164 					if(mbConvert)
165 					{
166 						const primitive3d::PolygonHairlinePrimitive3D& rPrimitive = static_cast< const primitive3d::PolygonHairlinePrimitive3D& >(rCandidate);
167 						basegfx::B2DPolygon a2DHairline;
168 
169 						if(mbUseProjection)
170 						{
171 							if(mbShadowProjectionIsValid)
172 							{
173 								a2DHairline = impDoShadowProjection(rPrimitive.getB3DPolygon());
174 							}
175 						}
176 						else
177 						{
178 							a2DHairline = basegfx::tools::createB2DPolygonFromB3DPolygon(rPrimitive.getB3DPolygon(), getViewInformation3D().getObjectToView());
179 						}
180 
181 						if(a2DHairline.count())
182 						{
183 							a2DHairline.transform(getObjectTransformation());
184                             mpPrimitive2DSequence->push_back(
185                                 new primitive2d::PolygonHairlinePrimitive2D(
186                                     a2DHairline,
187                                     maPrimitiveColor));
188 						}
189 					}
190 					break;
191 				}
192 				case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D :
193 				{
194 					// PolyPolygonMaterialPrimitive3D
195 					if(mbConvert)
196 					{
197 						const primitive3d::PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D& >(rCandidate);
198 						basegfx::B2DPolyPolygon a2DFill;
199 
200 						if(mbUseProjection)
201 						{
202 							if(mbShadowProjectionIsValid)
203 							{
204 								a2DFill = impDoShadowProjection(rPrimitive.getB3DPolyPolygon());
205 							}
206 						}
207 						else
208 						{
209 							a2DFill = basegfx::tools::createB2DPolyPolygonFromB3DPolyPolygon(rPrimitive.getB3DPolyPolygon(), getViewInformation3D().getObjectToView());
210 						}
211 
212 						if(a2DFill.count())
213 						{
214 							a2DFill.transform(getObjectTransformation());
215                             mpPrimitive2DSequence->push_back(
216                                 new primitive2d::PolyPolygonColorPrimitive2D(
217                                     a2DFill,
218                                     maPrimitiveColor));
219 						}
220 					}
221 					break;
222 				}
223 				default :
224 				{
225 					// process recursively
226 					process(rCandidate.get3DDecomposition(getViewInformation3D()));
227 					break;
228 				}
229 			}
230 		}
231 
232 		Shadow3DExtractingProcessor::Shadow3DExtractingProcessor(
233 			const geometry::ViewInformation3D& rViewInformation,
234 			const basegfx::B2DHomMatrix& rObjectTransformation,
235 			const basegfx::B3DVector& rLightNormal,
236 			double fShadowSlant,
237             const basegfx::B3DRange& rContained3DRange)
238 		:	BaseProcessor3D(rViewInformation),
239 			maPrimitive2DSequence(),
240 			mpPrimitive2DSequence(&maPrimitive2DSequence),
241 			maObjectTransformation(rObjectTransformation),
242 			maWorldToEye(),
243 			maEyeToView(),
244 			maLightNormal(rLightNormal),
245 			maShadowPlaneNormal(),
246 			maPlanePoint(),
247 			mfLightPlaneScalar(0.0),
248 			maPrimitiveColor(),
249 			mbShadowProjectionIsValid(false),
250 			mbConvert(false),
251 			mbUseProjection(false)
252 		{
253 			// normalize light normal, get and normalize shadow plane normal and calculate scalar from it
254 			maLightNormal.normalize();
255 			maShadowPlaneNormal = basegfx::B3DVector(0.0, sin(fShadowSlant), cos(fShadowSlant));
256 			maShadowPlaneNormal.normalize();
257 			mfLightPlaneScalar = maLightNormal.scalar(maShadowPlaneNormal);
258 
259 			// use only when scalar is > 0.0, so the light is in front of the object
260 			if(basegfx::fTools::more(mfLightPlaneScalar, 0.0))
261 			{
262 				// prepare buffered WorldToEye and EyeToView
263 				maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
264 				maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
265 
266 				// calculate range to get front edge around which to rotate the shadow's projection
267 				basegfx::B3DRange aContained3DRange(rContained3DRange);
268 				aContained3DRange.transform(getWorldToEye());
269 				maPlanePoint.setX(maShadowPlaneNormal.getX() < 0.0 ? aContained3DRange.getMinX() : aContained3DRange.getMaxX());
270 				maPlanePoint.setY(maShadowPlaneNormal.getY() > 0.0 ? aContained3DRange.getMinY() : aContained3DRange.getMaxY());
271 				maPlanePoint.setZ(aContained3DRange.getMinZ() - (aContained3DRange.getDepth() / 8.0));
272 
273 				// set flag that shadow projection is prepared and allowed
274 				mbShadowProjectionIsValid = true;
275 			}
276 		}
277 
278     	Shadow3DExtractingProcessor::~Shadow3DExtractingProcessor()
279         {
280             OSL_ENSURE(0 == maPrimitive2DSequence.size(),
281                 "OOps, someone used Shadow3DExtractingProcessor, but did not fetch the results (!)");
282             for(sal_uInt32 a(0); a < maPrimitive2DSequence.size(); a++)
283             {
284                 delete maPrimitive2DSequence[a];
285             }
286         }
287 
288 		basegfx::B2DPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolygon& rSource)
289 		{
290 			basegfx::B2DPolygon aRetval;
291 
292 			for(sal_uInt32 a(0L); a < rSource.count(); a++)
293 			{
294 				// get point, transform to eye coordinate system
295 				basegfx::B3DPoint aCandidate(rSource.getB3DPoint(a));
296 				aCandidate *= getWorldToEye();
297 
298 				// we are in eye coordinates
299 				// ray is (aCandidate + fCut * maLightNormal)
300 				// plane is (maPlanePoint, maShadowPlaneNormal)
301 				// maLightNormal.scalar(maShadowPlaneNormal) is already in mfLightPlaneScalar and > 0.0
302 				// get cut point of ray with shadow plane
303 				const double fCut(basegfx::B3DVector(maPlanePoint - aCandidate).scalar(maShadowPlaneNormal) / mfLightPlaneScalar);
304 				aCandidate += maLightNormal * fCut;
305 
306 				// transform to view, use 2d coordinates
307 				aCandidate *= getEyeToView();
308 				aRetval.append(basegfx::B2DPoint(aCandidate.getX(), aCandidate.getY()));
309 			}
310 
311 			// copy closed flag
312 			aRetval.setClosed(rSource.isClosed());
313 
314 			return aRetval;
315 		}
316 
317 		basegfx::B2DPolyPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolyPolygon& rSource)
318 		{
319 			basegfx::B2DPolyPolygon aRetval;
320 
321 			for(sal_uInt32 a(0L); a < rSource.count(); a++)
322 			{
323 				aRetval.append(impDoShadowProjection(rSource.getB3DPolygon(a)));
324 			}
325 
326 			return aRetval;
327 		}
328 
329         const primitive2d::Primitive2DSequence Shadow3DExtractingProcessor::getPrimitive2DSequence() const
330         {
331             return getPrimitive2DSequenceFromBasePrimitive2DVector(maPrimitive2DSequence);
332         }
333 
334     } // end of namespace processor3d
335 } // end of namespace drawinglayer
336 
337 //////////////////////////////////////////////////////////////////////////////
338 // eof
339