update credits
[LibreOffice.git] / drawinglayer / source / processor3d / shadow3dextractor.cxx
blob69eea72721089d87f5140515f2b254e1a21b1994
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 <drawinglayer/processor3d/shadow3dextractor.hxx>
21 #include <drawinglayer/primitive3d/shadowprimitive3d.hxx>
22 #include <drawinglayer/primitive2d/shadowprimitive2d.hxx>
23 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
24 #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
25 #include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
26 #include <basegfx/polygon/b2dpolygontools.hxx>
27 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
28 #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
29 #include <basegfx/polygon/b2dpolypolygontools.hxx>
30 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
31 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
33 //////////////////////////////////////////////////////////////////////////////
35 using namespace com::sun::star;
37 //////////////////////////////////////////////////////////////////////////////
39 namespace drawinglayer
41 namespace processor3d
43 // as tooling, the process() implementation takes over API handling and calls this
44 // virtual render method when the primitive implementation is BasePrimitive3D-based.
45 void Shadow3DExtractingProcessor::processBasePrimitive3D(const primitive3d::BasePrimitive3D& rCandidate)
47 // it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch
48 switch(rCandidate.getPrimitive3DID())
50 case PRIMITIVE3D_ID_SHADOWPRIMITIVE3D :
52 // shadow3d object. Call recursive with content and start conversion
53 const primitive3d::ShadowPrimitive3D& rPrimitive = static_cast< const primitive3d::ShadowPrimitive3D& >(rCandidate);
55 // set new target
56 primitive2d::Primitive2DVector aNewSubList;
57 primitive2d::Primitive2DVector* pLastTargetSequence = mpPrimitive2DSequence;
58 mpPrimitive2DSequence = &aNewSubList;
60 // activate convert
61 const bool bLastConvert(mbConvert);
62 mbConvert = true;
64 // set projection flag
65 const bool bLastUseProjection(mbUseProjection);
66 mbUseProjection = rPrimitive.getShadow3D();
68 // process content
69 process(rPrimitive.getChildren());
71 // restore values
72 mbUseProjection = bLastUseProjection;
73 mbConvert = bLastConvert;
74 mpPrimitive2DSequence = pLastTargetSequence;
76 // create 2d shadow primitive with result. This also fetches all entries
77 // from aNewSubList, so there is no need to delete them
78 primitive2d::BasePrimitive2D* pNew = new primitive2d::ShadowPrimitive2D(
79 rPrimitive.getShadowTransform(),
80 rPrimitive.getShadowColor(),
81 primitive2d::Primitive2DVectorToPrimitive2DSequence(aNewSubList));
83 if(basegfx::fTools::more(rPrimitive.getShadowTransparence(), 0.0))
85 // create simpleTransparencePrimitive, add created primitives
86 const primitive2d::Primitive2DReference xRef(pNew);
87 const primitive2d::Primitive2DSequence aNewTransPrimitiveVector(&xRef, 1);
89 pNew = new primitive2d::UnifiedTransparencePrimitive2D(
90 aNewTransPrimitiveVector,
91 rPrimitive.getShadowTransparence());
94 mpPrimitive2DSequence->push_back(pNew);
96 break;
98 case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D :
100 // transform group. Remember current transformations
101 const primitive3d::TransformPrimitive3D& rPrimitive = static_cast< const primitive3d::TransformPrimitive3D& >(rCandidate);
102 const geometry::ViewInformation3D aLastViewInformation3D(getViewInformation3D());
104 // create new transformation; add new object transform from right side
105 const geometry::ViewInformation3D aNewViewInformation3D(
106 aLastViewInformation3D.getObjectTransformation() * rPrimitive.getTransformation(),
107 aLastViewInformation3D.getOrientation(),
108 aLastViewInformation3D.getProjection(),
109 aLastViewInformation3D.getDeviceToView(),
110 aLastViewInformation3D.getViewTime(),
111 aLastViewInformation3D.getExtendedInformationSequence());
112 updateViewInformation(aNewViewInformation3D);
114 if(mbShadowProjectionIsValid)
116 // update buffered WorldToEye and EyeToView
117 maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
118 maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
121 // let break down
122 process(rPrimitive.getChildren());
124 // restore transformations
125 updateViewInformation(aLastViewInformation3D);
127 if(mbShadowProjectionIsValid)
129 // update buffered WorldToEye and EyeToView
130 maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
131 maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
133 break;
135 case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D :
137 // PolygonHairlinePrimitive3D
138 if(mbConvert)
140 const primitive3d::PolygonHairlinePrimitive3D& rPrimitive = static_cast< const primitive3d::PolygonHairlinePrimitive3D& >(rCandidate);
141 basegfx::B2DPolygon a2DHairline;
143 if(mbUseProjection)
145 if(mbShadowProjectionIsValid)
147 a2DHairline = impDoShadowProjection(rPrimitive.getB3DPolygon());
150 else
152 a2DHairline = basegfx::tools::createB2DPolygonFromB3DPolygon(rPrimitive.getB3DPolygon(), getViewInformation3D().getObjectToView());
155 if(a2DHairline.count())
157 a2DHairline.transform(getObjectTransformation());
158 mpPrimitive2DSequence->push_back(
159 new primitive2d::PolygonHairlinePrimitive2D(
160 a2DHairline,
161 maPrimitiveColor));
164 break;
166 case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D :
168 // PolyPolygonMaterialPrimitive3D
169 if(mbConvert)
171 const primitive3d::PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D& >(rCandidate);
172 basegfx::B2DPolyPolygon a2DFill;
174 if(mbUseProjection)
176 if(mbShadowProjectionIsValid)
178 a2DFill = impDoShadowProjection(rPrimitive.getB3DPolyPolygon());
181 else
183 a2DFill = basegfx::tools::createB2DPolyPolygonFromB3DPolyPolygon(rPrimitive.getB3DPolyPolygon(), getViewInformation3D().getObjectToView());
186 if(a2DFill.count())
188 a2DFill.transform(getObjectTransformation());
189 mpPrimitive2DSequence->push_back(
190 new primitive2d::PolyPolygonColorPrimitive2D(
191 a2DFill,
192 maPrimitiveColor));
195 break;
197 default :
199 // process recursively
200 process(rCandidate.get3DDecomposition(getViewInformation3D()));
201 break;
206 Shadow3DExtractingProcessor::Shadow3DExtractingProcessor(
207 const geometry::ViewInformation3D& rViewInformation,
208 const basegfx::B2DHomMatrix& rObjectTransformation,
209 const basegfx::B3DVector& rLightNormal,
210 double fShadowSlant,
211 const basegfx::B3DRange& rContained3DRange)
212 : BaseProcessor3D(rViewInformation),
213 maPrimitive2DSequence(),
214 mpPrimitive2DSequence(&maPrimitive2DSequence),
215 maObjectTransformation(rObjectTransformation),
216 maWorldToEye(),
217 maEyeToView(),
218 maLightNormal(rLightNormal),
219 maShadowPlaneNormal(),
220 maPlanePoint(),
221 mfLightPlaneScalar(0.0),
222 maPrimitiveColor(),
223 mbShadowProjectionIsValid(false),
224 mbConvert(false),
225 mbUseProjection(false)
227 // normalize light normal, get and normalize shadow plane normal and calculate scalar from it
228 maLightNormal.normalize();
229 maShadowPlaneNormal = basegfx::B3DVector(0.0, sin(fShadowSlant), cos(fShadowSlant));
230 maShadowPlaneNormal.normalize();
231 mfLightPlaneScalar = maLightNormal.scalar(maShadowPlaneNormal);
233 // use only when scalar is > 0.0, so the light is in front of the object
234 if(basegfx::fTools::more(mfLightPlaneScalar, 0.0))
236 // prepare buffered WorldToEye and EyeToView
237 maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
238 maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
240 // calculate range to get front edge around which to rotate the shadow's projection
241 basegfx::B3DRange aContained3DRange(rContained3DRange);
242 aContained3DRange.transform(getWorldToEye());
243 maPlanePoint.setX(maShadowPlaneNormal.getX() < 0.0 ? aContained3DRange.getMinX() : aContained3DRange.getMaxX());
244 maPlanePoint.setY(maShadowPlaneNormal.getY() > 0.0 ? aContained3DRange.getMinY() : aContained3DRange.getMaxY());
245 maPlanePoint.setZ(aContained3DRange.getMinZ() - (aContained3DRange.getDepth() / 8.0));
247 // set flag that shadow projection is prepared and allowed
248 mbShadowProjectionIsValid = true;
252 Shadow3DExtractingProcessor::~Shadow3DExtractingProcessor()
254 OSL_ENSURE(0 == maPrimitive2DSequence.size(),
255 "OOps, someone used Shadow3DExtractingProcessor, but did not fetch the results (!)");
256 for(sal_uInt32 a(0); a < maPrimitive2DSequence.size(); a++)
258 delete maPrimitive2DSequence[a];
262 basegfx::B2DPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolygon& rSource)
264 basegfx::B2DPolygon aRetval;
266 for(sal_uInt32 a(0L); a < rSource.count(); a++)
268 // get point, transform to eye coordinate system
269 basegfx::B3DPoint aCandidate(rSource.getB3DPoint(a));
270 aCandidate *= getWorldToEye();
272 // we are in eye coordinates
273 // ray is (aCandidate + fCut * maLightNormal)
274 // plane is (maPlanePoint, maShadowPlaneNormal)
275 // maLightNormal.scalar(maShadowPlaneNormal) is already in mfLightPlaneScalar and > 0.0
276 // get cut point of ray with shadow plane
277 const double fCut(basegfx::B3DVector(maPlanePoint - aCandidate).scalar(maShadowPlaneNormal) / mfLightPlaneScalar);
278 aCandidate += maLightNormal * fCut;
280 // transform to view, use 2d coordinates
281 aCandidate *= getEyeToView();
282 aRetval.append(basegfx::B2DPoint(aCandidate.getX(), aCandidate.getY()));
285 // copy closed flag
286 aRetval.setClosed(rSource.isClosed());
288 return aRetval;
291 basegfx::B2DPolyPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolyPolygon& rSource)
293 basegfx::B2DPolyPolygon aRetval;
295 for(sal_uInt32 a(0L); a < rSource.count(); a++)
297 aRetval.append(impDoShadowProjection(rSource.getB3DPolygon(a)));
300 return aRetval;
303 const primitive2d::Primitive2DSequence Shadow3DExtractingProcessor::getPrimitive2DSequence() const
305 return Primitive2DVectorToPrimitive2DSequence(maPrimitive2DSequence);
308 } // end of namespace processor3d
309 } // end of namespace drawinglayer
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */