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