1464702f4SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3464702f4SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4464702f4SAndrew Rist * or more contributor license agreements. See the NOTICE file 5464702f4SAndrew Rist * distributed with this work for additional information 6464702f4SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7464702f4SAndrew Rist * to you under the Apache License, Version 2.0 (the 8464702f4SAndrew Rist * "License"); you may not use this file except in compliance 9464702f4SAndrew Rist * with the License. You may obtain a copy of the License at 10464702f4SAndrew Rist * 11464702f4SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12464702f4SAndrew Rist * 13464702f4SAndrew Rist * Unless required by applicable law or agreed to in writing, 14464702f4SAndrew Rist * software distributed under the License is distributed on an 15464702f4SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16464702f4SAndrew Rist * KIND, either express or implied. See the License for the 17464702f4SAndrew Rist * specific language governing permissions and limitations 18464702f4SAndrew Rist * under the License. 19464702f4SAndrew Rist * 20464702f4SAndrew Rist *************************************************************/ 21464702f4SAndrew Rist 22464702f4SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <drawinglayer/primitive3d/hatchtextureprimitive3d.hxx> 28cdf0e10cSrcweir #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx> 29cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx> 30cdf0e10cSrcweir #include <basegfx/polygon/b3dpolygon.hxx> 31cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx> 32cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygontools.hxx> 33cdf0e10cSrcweir #include <basegfx/range/b2drange.hxx> 34cdf0e10cSrcweir #include <drawinglayer/texture/texture.hxx> 35cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygonclipper.hxx> 36cdf0e10cSrcweir #include <basegfx/matrix/b3dhommatrix.hxx> 37cdf0e10cSrcweir #include <drawinglayer/primitive3d/polygonprimitive3d.hxx> 38cdf0e10cSrcweir #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx> 39cdf0e10cSrcweir 40cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////// 41cdf0e10cSrcweir 42cdf0e10cSrcweir using namespace com::sun::star; 43cdf0e10cSrcweir 44cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////// 45cdf0e10cSrcweir 46cdf0e10cSrcweir namespace drawinglayer 47cdf0e10cSrcweir { 48*64b14621SArmin Le Grand namespace primitive3d 49*64b14621SArmin Le Grand { impCreate3DDecomposition() const50*64b14621SArmin Le Grand Primitive3DSequence HatchTexturePrimitive3D::impCreate3DDecomposition() const 51*64b14621SArmin Le Grand { 52*64b14621SArmin Le Grand Primitive3DSequence aRetval; 53*64b14621SArmin Le Grand 54*64b14621SArmin Le Grand if(getChildren().hasElements()) 55*64b14621SArmin Le Grand { 56*64b14621SArmin Le Grand const Primitive3DSequence aSource(getChildren()); 57*64b14621SArmin Le Grand const sal_uInt32 nSourceCount(aSource.getLength()); 58*64b14621SArmin Le Grand std::vector< Primitive3DReference > aDestination; 59*64b14621SArmin Le Grand 60*64b14621SArmin Le Grand for(sal_uInt32 a(0); a < nSourceCount; a++) 61*64b14621SArmin Le Grand { 62*64b14621SArmin Le Grand // get reference 63*64b14621SArmin Le Grand const Primitive3DReference xReference(aSource[a]); 64*64b14621SArmin Le Grand 65*64b14621SArmin Le Grand if(xReference.is()) 66*64b14621SArmin Le Grand { 67*64b14621SArmin Le Grand // try to cast to BasePrimitive2D implementation 68*64b14621SArmin Le Grand const BasePrimitive3D* pBasePrimitive = dynamic_cast< const BasePrimitive3D* >(xReference.get()); 69*64b14621SArmin Le Grand 70*64b14621SArmin Le Grand if(pBasePrimitive) 71*64b14621SArmin Le Grand { 72*64b14621SArmin Le Grand // it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch 73*64b14621SArmin Le Grand // not all content is needed, remove transparencies and ModifiedColorPrimitives 74*64b14621SArmin Le Grand switch(pBasePrimitive->getPrimitive3DID()) 75*64b14621SArmin Le Grand { 76*64b14621SArmin Le Grand case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D : 77*64b14621SArmin Le Grand { 78*64b14621SArmin Le Grand // polyPolygonMaterialPrimitive3D, check texturing and hatching 79*64b14621SArmin Le Grand const PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const PolyPolygonMaterialPrimitive3D& >(*pBasePrimitive); 80*64b14621SArmin Le Grand const basegfx::B3DPolyPolygon aFillPolyPolygon(rPrimitive.getB3DPolyPolygon()); 81*64b14621SArmin Le Grand 82*64b14621SArmin Le Grand if(maHatch.isFillBackground()) 83*64b14621SArmin Le Grand { 84*64b14621SArmin Le Grand // add original primitive for background 85*64b14621SArmin Le Grand aDestination.push_back(xReference); 86*64b14621SArmin Le Grand } 87*64b14621SArmin Le Grand 88*64b14621SArmin Le Grand if(aFillPolyPolygon.areTextureCoordinatesUsed()) 89*64b14621SArmin Le Grand { 90*64b14621SArmin Le Grand const sal_uInt32 nPolyCount(aFillPolyPolygon.count()); 91*64b14621SArmin Le Grand basegfx::B2DPolyPolygon aTexPolyPolygon; 92*64b14621SArmin Le Grand basegfx::B2DPoint a2N; 93*64b14621SArmin Le Grand basegfx::B2DVector a2X, a2Y; 94*64b14621SArmin Le Grand basegfx::B3DPoint a3N; 95*64b14621SArmin Le Grand basegfx::B3DVector a3X, a3Y; 96*64b14621SArmin Le Grand bool b2N(false), b2X(false), b2Y(false); 97*64b14621SArmin Le Grand 98*64b14621SArmin Le Grand for(sal_uInt32 b(0); b < nPolyCount; b++) 99*64b14621SArmin Le Grand { 100*64b14621SArmin Le Grand const basegfx::B3DPolygon aPartPoly(aFillPolyPolygon.getB3DPolygon(b)); 101*64b14621SArmin Le Grand const sal_uInt32 nPointCount(aPartPoly.count()); 102*64b14621SArmin Le Grand basegfx::B2DPolygon aTexPolygon; 103*64b14621SArmin Le Grand 104*64b14621SArmin Le Grand for(sal_uInt32 c(0); c < nPointCount; c++) 105*64b14621SArmin Le Grand { 106*64b14621SArmin Le Grand const basegfx::B2DPoint a2Candidate(aPartPoly.getTextureCoordinate(c)); 107*64b14621SArmin Le Grand 108*64b14621SArmin Le Grand if(!b2N) 109*64b14621SArmin Le Grand { 110*64b14621SArmin Le Grand a2N = a2Candidate; 111*64b14621SArmin Le Grand a3N = aPartPoly.getB3DPoint(c); 112*64b14621SArmin Le Grand b2N = true; 113*64b14621SArmin Le Grand } 114*64b14621SArmin Le Grand else if(!b2X && !a2N.equal(a2Candidate)) 115*64b14621SArmin Le Grand { 116*64b14621SArmin Le Grand a2X = a2Candidate - a2N; 117*64b14621SArmin Le Grand a3X = aPartPoly.getB3DPoint(c) - a3N; 118*64b14621SArmin Le Grand b2X = true; 119*64b14621SArmin Le Grand } 120*64b14621SArmin Le Grand else if(!b2Y && !a2N.equal(a2Candidate) && !a2X.equal(a2Candidate)) 121*64b14621SArmin Le Grand { 122*64b14621SArmin Le Grand a2Y = a2Candidate - a2N; 123*64b14621SArmin Le Grand 124*64b14621SArmin Le Grand const double fCross(a2X.cross(a2Y)); 125*64b14621SArmin Le Grand 126*64b14621SArmin Le Grand if(!basegfx::fTools::equalZero(fCross)) 127*64b14621SArmin Le Grand { 128*64b14621SArmin Le Grand a3Y = aPartPoly.getB3DPoint(c) - a3N; 129*64b14621SArmin Le Grand b2Y = true; 130*64b14621SArmin Le Grand } 131*64b14621SArmin Le Grand } 132*64b14621SArmin Le Grand 133*64b14621SArmin Le Grand aTexPolygon.append(a2Candidate); 134*64b14621SArmin Le Grand } 135*64b14621SArmin Le Grand 136*64b14621SArmin Le Grand aTexPolygon.setClosed(true); 137*64b14621SArmin Le Grand aTexPolyPolygon.append(aTexPolygon); 138*64b14621SArmin Le Grand } 139*64b14621SArmin Le Grand 140*64b14621SArmin Le Grand if(b2N && b2X && b2Y) 141*64b14621SArmin Le Grand { 142*64b14621SArmin Le Grand // found two linearly independent 2D vectors 143*64b14621SArmin Le Grand // get 2d range of texture coordinates 144*64b14621SArmin Le Grand const basegfx::B2DRange aOutlineRange(basegfx::tools::getRange(aTexPolyPolygon)); 145*64b14621SArmin Le Grand const basegfx::BColor aHatchColor(getHatch().getColor()); 146*64b14621SArmin Le Grand const double fAngle(getHatch().getAngle()); 147*64b14621SArmin Le Grand ::std::vector< basegfx::B2DHomMatrix > aMatrices; 148*64b14621SArmin Le Grand 149*64b14621SArmin Le Grand // get hatch transformations 150*64b14621SArmin Le Grand switch(getHatch().getStyle()) 151*64b14621SArmin Le Grand { 152*64b14621SArmin Le Grand case attribute::HATCHSTYLE_TRIPLE: 153*64b14621SArmin Le Grand { 154*64b14621SArmin Le Grand // rotated 45 degrees 155*64b14621SArmin Le Grand texture::GeoTexSvxHatch aHatch( 156*64b14621SArmin Le Grand aOutlineRange, 157*64b14621SArmin Le Grand aOutlineRange, 158*64b14621SArmin Le Grand getHatch().getDistance(), 159*64b14621SArmin Le Grand fAngle - F_PI4); 160*64b14621SArmin Le Grand 161*64b14621SArmin Le Grand aHatch.appendTransformations(aMatrices); 162*64b14621SArmin Le Grand } 163*64b14621SArmin Le Grand case attribute::HATCHSTYLE_DOUBLE: 164*64b14621SArmin Le Grand { 165*64b14621SArmin Le Grand // rotated 90 degrees 166*64b14621SArmin Le Grand texture::GeoTexSvxHatch aHatch( 167*64b14621SArmin Le Grand aOutlineRange, 168*64b14621SArmin Le Grand aOutlineRange, 169*64b14621SArmin Le Grand getHatch().getDistance(), 170*64b14621SArmin Le Grand fAngle - F_PI2); 171*64b14621SArmin Le Grand 172*64b14621SArmin Le Grand aHatch.appendTransformations(aMatrices); 173*64b14621SArmin Le Grand } 174*64b14621SArmin Le Grand case attribute::HATCHSTYLE_SINGLE: 175*64b14621SArmin Le Grand { 176*64b14621SArmin Le Grand // angle as given 177*64b14621SArmin Le Grand texture::GeoTexSvxHatch aHatch( 178*64b14621SArmin Le Grand aOutlineRange, 179*64b14621SArmin Le Grand aOutlineRange, 180*64b14621SArmin Le Grand getHatch().getDistance(), 181*64b14621SArmin Le Grand fAngle); 182*64b14621SArmin Le Grand 183*64b14621SArmin Le Grand aHatch.appendTransformations(aMatrices); 184*64b14621SArmin Le Grand } 185*64b14621SArmin Le Grand } 186*64b14621SArmin Le Grand 187*64b14621SArmin Le Grand // create geometry from unit line 188*64b14621SArmin Le Grand basegfx::B2DPolyPolygon a2DHatchLines; 189*64b14621SArmin Le Grand basegfx::B2DPolygon a2DUnitLine; 190*64b14621SArmin Le Grand a2DUnitLine.append(basegfx::B2DPoint(0.0, 0.0)); 191*64b14621SArmin Le Grand a2DUnitLine.append(basegfx::B2DPoint(1.0, 0.0)); 192*64b14621SArmin Le Grand 193*64b14621SArmin Le Grand for(sal_uInt32 c(0); c < aMatrices.size(); c++) 194*64b14621SArmin Le Grand { 195*64b14621SArmin Le Grand const basegfx::B2DHomMatrix& rMatrix = aMatrices[c]; 196*64b14621SArmin Le Grand basegfx::B2DPolygon aNewLine(a2DUnitLine); 197*64b14621SArmin Le Grand aNewLine.transform(rMatrix); 198*64b14621SArmin Le Grand a2DHatchLines.append(aNewLine); 199*64b14621SArmin Le Grand } 200*64b14621SArmin Le Grand 201*64b14621SArmin Le Grand if(a2DHatchLines.count()) 202*64b14621SArmin Le Grand { 203*64b14621SArmin Le Grand // clip against texture polygon 204*64b14621SArmin Le Grand a2DHatchLines = basegfx::tools::clipPolyPolygonOnPolyPolygon(a2DHatchLines, aTexPolyPolygon, true, true); 205*64b14621SArmin Le Grand } 206*64b14621SArmin Le Grand 207*64b14621SArmin Le Grand if(a2DHatchLines.count()) 208*64b14621SArmin Le Grand { 209*64b14621SArmin Le Grand // create 2d matrix with 2d vectors as column vectors and 2d point as offset, this represents 210*64b14621SArmin Le Grand // a coordinate system transformation from unit coordinates to the new coordinate system 211*64b14621SArmin Le Grand basegfx::B2DHomMatrix a2D; 212*64b14621SArmin Le Grand a2D.set(0, 0, a2X.getX()); 213*64b14621SArmin Le Grand a2D.set(1, 0, a2X.getY()); 214*64b14621SArmin Le Grand a2D.set(0, 1, a2Y.getX()); 215*64b14621SArmin Le Grand a2D.set(1, 1, a2Y.getY()); 216*64b14621SArmin Le Grand a2D.set(0, 2, a2N.getX()); 217*64b14621SArmin Le Grand a2D.set(1, 2, a2N.getY()); 218*64b14621SArmin Le Grand 219*64b14621SArmin Le Grand // invert that transformation, so we have a back-transformation from texture coordinates 220*64b14621SArmin Le Grand // to unit coordinates 221*64b14621SArmin Le Grand a2D.invert(); 222*64b14621SArmin Le Grand a2DHatchLines.transform(a2D); 223*64b14621SArmin Le Grand 224*64b14621SArmin Le Grand // expand back-transformated geometry tpo 3D 225*64b14621SArmin Le Grand basegfx::B3DPolyPolygon a3DHatchLines(basegfx::tools::createB3DPolyPolygonFromB2DPolyPolygon(a2DHatchLines, 0.0)); 226*64b14621SArmin Le Grand 227*64b14621SArmin Le Grand // create 3d matrix with 3d vectors as column vectors (0,0,1 as Z) and 3d point as offset, this represents 228*64b14621SArmin Le Grand // a coordinate system transformation from unit coordinates to the object's 3d coordinate system 229*64b14621SArmin Le Grand basegfx::B3DHomMatrix a3D; 230*64b14621SArmin Le Grand a3D.set(0, 0, a3X.getX()); 231*64b14621SArmin Le Grand a3D.set(1, 0, a3X.getY()); 232*64b14621SArmin Le Grand a3D.set(2, 0, a3X.getZ()); 233*64b14621SArmin Le Grand a3D.set(0, 1, a3Y.getX()); 234*64b14621SArmin Le Grand a3D.set(1, 1, a3Y.getY()); 235*64b14621SArmin Le Grand a3D.set(2, 1, a3Y.getZ()); 236*64b14621SArmin Le Grand a3D.set(0, 3, a3N.getX()); 237*64b14621SArmin Le Grand a3D.set(1, 3, a3N.getY()); 238*64b14621SArmin Le Grand a3D.set(2, 3, a3N.getZ()); 239*64b14621SArmin Le Grand 240*64b14621SArmin Le Grand // transform hatch lines to 3D object coordinates 241*64b14621SArmin Le Grand a3DHatchLines.transform(a3D); 242*64b14621SArmin Le Grand 243*64b14621SArmin Le Grand // build primitives from this geometry 244*64b14621SArmin Le Grand const sal_uInt32 nHatchLines(a3DHatchLines.count()); 245*64b14621SArmin Le Grand 246*64b14621SArmin Le Grand for(sal_uInt32 d(0); d < nHatchLines; d++) 247*64b14621SArmin Le Grand { 248*64b14621SArmin Le Grand const Primitive3DReference xRef(new PolygonHairlinePrimitive3D(a3DHatchLines.getB3DPolygon(d), aHatchColor)); 249*64b14621SArmin Le Grand aDestination.push_back(xRef); 250*64b14621SArmin Le Grand } 251*64b14621SArmin Le Grand } 252*64b14621SArmin Le Grand } 253*64b14621SArmin Le Grand } 254*64b14621SArmin Le Grand 255*64b14621SArmin Le Grand break; 256*64b14621SArmin Le Grand } 257*64b14621SArmin Le Grand default : 258*64b14621SArmin Le Grand { 259*64b14621SArmin Le Grand // add reference to result 260*64b14621SArmin Le Grand aDestination.push_back(xReference); 261*64b14621SArmin Le Grand break; 262*64b14621SArmin Le Grand } 263*64b14621SArmin Le Grand } 264*64b14621SArmin Le Grand } 265*64b14621SArmin Le Grand else 266*64b14621SArmin Le Grand { 267*64b14621SArmin Le Grand // unknown implementation, add to result 268*64b14621SArmin Le Grand aDestination.push_back(xReference); 269*64b14621SArmin Le Grand } 270*64b14621SArmin Le Grand } 271*64b14621SArmin Le Grand } 272*64b14621SArmin Le Grand 273*64b14621SArmin Le Grand // prepare return value 274*64b14621SArmin Le Grand const sal_uInt32 nDestSize(aDestination.size()); 275*64b14621SArmin Le Grand aRetval.realloc(nDestSize); 276*64b14621SArmin Le Grand 277*64b14621SArmin Le Grand for(sal_uInt32 b(0); b < nDestSize; b++) 278*64b14621SArmin Le Grand { 279*64b14621SArmin Le Grand aRetval[b] = aDestination[b]; 280*64b14621SArmin Le Grand } 281*64b14621SArmin Le Grand } 282*64b14621SArmin Le Grand 283*64b14621SArmin Le Grand return aRetval; 284*64b14621SArmin Le Grand } 285cdf0e10cSrcweir HatchTexturePrimitive3D(const attribute::FillHatchAttribute & rHatch,const Primitive3DSequence & rChildren,const basegfx::B2DVector & rTextureSize,bool bModulate,bool bFilter)286*64b14621SArmin Le Grand HatchTexturePrimitive3D::HatchTexturePrimitive3D( 287*64b14621SArmin Le Grand const attribute::FillHatchAttribute& rHatch, 288*64b14621SArmin Le Grand const Primitive3DSequence& rChildren, 289*64b14621SArmin Le Grand const basegfx::B2DVector& rTextureSize, 290*64b14621SArmin Le Grand bool bModulate, 291*64b14621SArmin Le Grand bool bFilter) 292*64b14621SArmin Le Grand : TexturePrimitive3D(rChildren, rTextureSize, bModulate, bFilter), 293*64b14621SArmin Le Grand maHatch(rHatch), 294*64b14621SArmin Le Grand maBuffered3DDecomposition() 295*64b14621SArmin Le Grand { 296*64b14621SArmin Le Grand } 297cdf0e10cSrcweir operator ==(const BasePrimitive3D & rPrimitive) const298*64b14621SArmin Le Grand bool HatchTexturePrimitive3D::operator==(const BasePrimitive3D& rPrimitive) const 299*64b14621SArmin Le Grand { 300*64b14621SArmin Le Grand if(TexturePrimitive3D::operator==(rPrimitive)) 301*64b14621SArmin Le Grand { 302*64b14621SArmin Le Grand const HatchTexturePrimitive3D& rCompare = (HatchTexturePrimitive3D&)rPrimitive; 303*64b14621SArmin Le Grand 304*64b14621SArmin Le Grand return (getHatch() == rCompare.getHatch()); 305*64b14621SArmin Le Grand } 306*64b14621SArmin Le Grand 307*64b14621SArmin Le Grand return false; 308*64b14621SArmin Le Grand } 309cdf0e10cSrcweir get3DDecomposition(const geometry::ViewInformation3D &) const310cdf0e10cSrcweir Primitive3DSequence HatchTexturePrimitive3D::get3DDecomposition(const geometry::ViewInformation3D& /*rViewInformation*/) const 311cdf0e10cSrcweir { 312*64b14621SArmin Le Grand ::osl::MutexGuard aGuard( m_aMutex ); 313cdf0e10cSrcweir 314*64b14621SArmin Le Grand if(!getBuffered3DDecomposition().hasElements()) 315*64b14621SArmin Le Grand { 316*64b14621SArmin Le Grand const Primitive3DSequence aNewSequence(impCreate3DDecomposition()); 317*64b14621SArmin Le Grand const_cast< HatchTexturePrimitive3D* >(this)->setBuffered3DDecomposition(aNewSequence); 318*64b14621SArmin Le Grand } 319cdf0e10cSrcweir 320*64b14621SArmin Le Grand return getBuffered3DDecomposition(); 321cdf0e10cSrcweir } 322cdf0e10cSrcweir 323*64b14621SArmin Le Grand // provide unique ID 324*64b14621SArmin Le Grand ImplPrimitrive3DIDBlock(HatchTexturePrimitive3D, PRIMITIVE3D_ID_HATCHTEXTUREPRIMITIVE3D) 325cdf0e10cSrcweir 326*64b14621SArmin Le Grand } // end of namespace primitive3d 327cdf0e10cSrcweir } // end of namespace drawinglayer 328cdf0e10cSrcweir 329cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////// 330cdf0e10cSrcweir // eof 331