LCOV - code coverage report
Current view: top level - drawinglayer/source/primitive3d - hatchtextureprimitive3d.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 116 131 88.5 %
Date: 2014-11-03 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <drawinglayer/primitive3d/hatchtextureprimitive3d.hxx>
      21             : #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
      22             : #include <basegfx/polygon/b2dpolypolygon.hxx>
      23             : #include <basegfx/polygon/b3dpolygon.hxx>
      24             : #include <basegfx/polygon/b2dpolygon.hxx>
      25             : #include <basegfx/polygon/b2dpolypolygontools.hxx>
      26             : #include <basegfx/range/b2drange.hxx>
      27             : #include <drawinglayer/texture/texture.hxx>
      28             : #include <basegfx/polygon/b2dpolygonclipper.hxx>
      29             : #include <basegfx/matrix/b3dhommatrix.hxx>
      30             : #include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
      31             : #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
      32             : 
      33             : 
      34             : 
      35             : using namespace com::sun::star;
      36             : 
      37             : 
      38             : 
      39             : namespace drawinglayer
      40             : {
      41             :     namespace primitive3d
      42             :     {
      43           4 :         Primitive3DSequence HatchTexturePrimitive3D::impCreate3DDecomposition() const
      44             :         {
      45           4 :             Primitive3DSequence aRetval;
      46             : 
      47           4 :             if(getChildren().hasElements())
      48             :             {
      49           4 :                 const Primitive3DSequence aSource(getChildren());
      50           4 :                 const sal_uInt32 nSourceCount(aSource.getLength());
      51           8 :                 std::vector< Primitive3DReference > aDestination;
      52             : 
      53          28 :                 for(sal_uInt32 a(0); a < nSourceCount; a++)
      54             :                 {
      55             :                     // get reference
      56          24 :                     const Primitive3DReference xReference(aSource[a]);
      57             : 
      58          24 :                     if(xReference.is())
      59             :                     {
      60             :                         // try to cast to BasePrimitive2D implementation
      61          24 :                         const BasePrimitive3D* pBasePrimitive = dynamic_cast< const BasePrimitive3D* >(xReference.get());
      62             : 
      63          24 :                         if(pBasePrimitive)
      64             :                         {
      65             :                             // it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch
      66             :                             // not all content is needed, remove transparencies and ModifiedColorPrimitives
      67          24 :                             switch(pBasePrimitive->getPrimitive3DID())
      68             :                             {
      69             :                                 case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D :
      70             :                                 {
      71             :                                     // polyPolygonMaterialPrimitive3D, check texturing and hatching
      72          24 :                                     const PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const PolyPolygonMaterialPrimitive3D& >(*pBasePrimitive);
      73          24 :                                     const basegfx::B3DPolyPolygon aFillPolyPolygon(rPrimitive.getB3DPolyPolygon());
      74             : 
      75          24 :                                     if(maHatch.isFillBackground())
      76             :                                     {
      77             :                                         // add original primitive for background
      78           0 :                                         aDestination.push_back(xReference);
      79             :                                     }
      80             : 
      81          24 :                                     if(aFillPolyPolygon.areTextureCoordinatesUsed())
      82             :                                     {
      83          24 :                                         const sal_uInt32 nPolyCount(aFillPolyPolygon.count());
      84          24 :                                         basegfx::B2DPolyPolygon aTexPolyPolygon;
      85          48 :                                         basegfx::B2DPoint a2N;
      86          48 :                                         basegfx::B2DVector a2X, a2Y;
      87          48 :                                         basegfx::B3DPoint a3N;
      88          48 :                                         basegfx::B3DVector a3X, a3Y;
      89          24 :                                         bool b2N(false), b2X(false), b2Y(false);
      90             : 
      91          48 :                                         for(sal_uInt32 b(0); b < nPolyCount; b++)
      92             :                                         {
      93          24 :                                             const basegfx::B3DPolygon aPartPoly(aFillPolyPolygon.getB3DPolygon(b));
      94          24 :                                             const sal_uInt32 nPointCount(aPartPoly.count());
      95          48 :                                             basegfx::B2DPolygon aTexPolygon;
      96             : 
      97         120 :                                             for(sal_uInt32 c(0); c < nPointCount; c++)
      98             :                                             {
      99          96 :                                                 const basegfx::B2DPoint a2Candidate(aPartPoly.getTextureCoordinate(c));
     100             : 
     101          96 :                                                 if(!b2N)
     102             :                                                 {
     103          24 :                                                     a2N = a2Candidate;
     104          24 :                                                     a3N = aPartPoly.getB3DPoint(c);
     105          24 :                                                     b2N = true;
     106             :                                                 }
     107          72 :                                                 else if(!b2X && !a2N.equal(a2Candidate))
     108             :                                                 {
     109          24 :                                                     a2X = a2Candidate - a2N;
     110          24 :                                                     a3X = aPartPoly.getB3DPoint(c) - a3N;
     111          24 :                                                     b2X = true;
     112             :                                                 }
     113          48 :                                                 else if(!b2Y && !a2N.equal(a2Candidate) && !a2X.equal(a2Candidate))
     114             :                                                 {
     115          20 :                                                     a2Y = a2Candidate - a2N;
     116             : 
     117          20 :                                                     const double fCross(a2X.cross(a2Y));
     118             : 
     119          20 :                                                     if(!basegfx::fTools::equalZero(fCross))
     120             :                                                     {
     121           8 :                                                         a3Y = aPartPoly.getB3DPoint(c) - a3N;
     122           8 :                                                         b2Y = true;
     123             :                                                     }
     124             :                                                 }
     125             : 
     126          96 :                                                 aTexPolygon.append(a2Candidate);
     127          96 :                                             }
     128             : 
     129          24 :                                             aTexPolygon.setClosed(true);
     130          24 :                                             aTexPolyPolygon.append(aTexPolygon);
     131          24 :                                         }
     132             : 
     133          24 :                                         if(b2N && b2X && b2Y)
     134             :                                         {
     135             :                                             // found two linearly independent 2D vectors
     136             :                                             // get 2d range of texture coordinates
     137           8 :                                             const basegfx::B2DRange aOutlineRange(basegfx::tools::getRange(aTexPolyPolygon));
     138           8 :                                             const basegfx::BColor aHatchColor(getHatch().getColor());
     139           8 :                                             const double fAngle(getHatch().getAngle());
     140          16 :                                             ::std::vector< basegfx::B2DHomMatrix > aMatrices;
     141             : 
     142             :                                             // get hatch transformations
     143           8 :                                             switch(getHatch().getStyle())
     144             :                                             {
     145             :                                                 case attribute::HATCHSTYLE_TRIPLE:
     146             :                                                 {
     147             :                                                     // rotated 45 degrees
     148             :                                                     texture::GeoTexSvxHatch aHatch(
     149             :                                                         aOutlineRange,
     150             :                                                         aOutlineRange,
     151           0 :                                                         getHatch().getDistance(),
     152           0 :                                                         fAngle - F_PI4);
     153             : 
     154           0 :                                                     aHatch.appendTransformations(aMatrices);
     155             :                                                 }
     156             :                                                 case attribute::HATCHSTYLE_DOUBLE:
     157             :                                                 {
     158             :                                                     // rotated 90 degrees
     159             :                                                     texture::GeoTexSvxHatch aHatch(
     160             :                                                         aOutlineRange,
     161             :                                                         aOutlineRange,
     162           0 :                                                         getHatch().getDistance(),
     163           0 :                                                         fAngle - F_PI2);
     164             : 
     165           0 :                                                     aHatch.appendTransformations(aMatrices);
     166             :                                                 }
     167             :                                                 case attribute::HATCHSTYLE_SINGLE:
     168             :                                                 {
     169             :                                                     // angle as given
     170             :                                                     texture::GeoTexSvxHatch aHatch(
     171             :                                                         aOutlineRange,
     172             :                                                         aOutlineRange,
     173           8 :                                                         getHatch().getDistance(),
     174           8 :                                                         fAngle);
     175             : 
     176           8 :                                                     aHatch.appendTransformations(aMatrices);
     177             :                                                 }
     178             :                                             }
     179             : 
     180             :                                             // create geometry from unit line
     181           8 :                                             basegfx::B2DPolyPolygon a2DHatchLines;
     182          16 :                                             basegfx::B2DPolygon a2DUnitLine;
     183           8 :                                             a2DUnitLine.append(basegfx::B2DPoint(0.0, 0.0));
     184           8 :                                             a2DUnitLine.append(basegfx::B2DPoint(1.0, 0.0));
     185             : 
     186         376 :                                             for(sal_uInt32 c(0); c < aMatrices.size(); c++)
     187             :                                             {
     188         368 :                                                 const basegfx::B2DHomMatrix& rMatrix = aMatrices[c];
     189         368 :                                                 basegfx::B2DPolygon aNewLine(a2DUnitLine);
     190         368 :                                                 aNewLine.transform(rMatrix);
     191         368 :                                                 a2DHatchLines.append(aNewLine);
     192         368 :                                             }
     193             : 
     194           8 :                                             if(a2DHatchLines.count())
     195             :                                             {
     196             :                                                 // clip against texture polygon
     197           8 :                                                 a2DHatchLines = basegfx::tools::clipPolyPolygonOnPolyPolygon(a2DHatchLines, aTexPolyPolygon, true, true);
     198             :                                             }
     199             : 
     200           8 :                                             if(a2DHatchLines.count())
     201             :                                             {
     202             :                                                 // create 2d matrix with 2d vectors as column vectors and 2d point as offset, this represents
     203             :                                                 // a coordinate system transformation from unit coordinates to the new coordinate system
     204           8 :                                                 basegfx::B2DHomMatrix a2D;
     205           8 :                                                 a2D.set(0, 0, a2X.getX());
     206           8 :                                                 a2D.set(1, 0, a2X.getY());
     207           8 :                                                 a2D.set(0, 1, a2Y.getX());
     208           8 :                                                 a2D.set(1, 1, a2Y.getY());
     209           8 :                                                 a2D.set(0, 2, a2N.getX());
     210           8 :                                                 a2D.set(1, 2, a2N.getY());
     211             : 
     212             :                                                 // invert that transformation, so we have a back-transformation from texture coordinates
     213             :                                                 // to unit coordinates
     214           8 :                                                 a2D.invert();
     215           8 :                                                 a2DHatchLines.transform(a2D);
     216             : 
     217             :                                                 // expand back-transformated geometry tpo 3D
     218          16 :                                                 basegfx::B3DPolyPolygon a3DHatchLines(basegfx::tools::createB3DPolyPolygonFromB2DPolyPolygon(a2DHatchLines, 0.0));
     219             : 
     220             :                                                 // create 3d matrix with 3d vectors as column vectors (0,0,1 as Z) and 3d point as offset, this represents
     221             :                                                 // a coordinate system transformation from unit coordinates to the object's 3d coordinate system
     222          16 :                                                 basegfx::B3DHomMatrix a3D;
     223           8 :                                                 a3D.set(0, 0, a3X.getX());
     224           8 :                                                 a3D.set(1, 0, a3X.getY());
     225           8 :                                                 a3D.set(2, 0, a3X.getZ());
     226           8 :                                                 a3D.set(0, 1, a3Y.getX());
     227           8 :                                                 a3D.set(1, 1, a3Y.getY());
     228           8 :                                                 a3D.set(2, 1, a3Y.getZ());
     229           8 :                                                 a3D.set(0, 3, a3N.getX());
     230           8 :                                                 a3D.set(1, 3, a3N.getY());
     231           8 :                                                 a3D.set(2, 3, a3N.getZ());
     232             : 
     233             :                                                 // transform hatch lines to 3D object coordinates
     234           8 :                                                 a3DHatchLines.transform(a3D);
     235             : 
     236             :                                                 // build primitives from this geometry
     237           8 :                                                 const sal_uInt32 nHatchLines(a3DHatchLines.count());
     238             : 
     239         376 :                                                 for(sal_uInt32 d(0); d < nHatchLines; d++)
     240             :                                                 {
     241         368 :                                                     const Primitive3DReference xRef(new PolygonHairlinePrimitive3D(a3DHatchLines.getB3DPolygon(d), aHatchColor));
     242         368 :                                                     aDestination.push_back(xRef);
     243         376 :                                                 }
     244          16 :                                             }
     245          24 :                                         }
     246             :                                     }
     247             : 
     248          24 :                                     break;
     249             :                                 }
     250             :                                 default :
     251             :                                 {
     252             :                                     // add reference to result
     253           0 :                                     aDestination.push_back(xReference);
     254           0 :                                     break;
     255             :                                 }
     256             :                             }
     257             :                         }
     258             :                         else
     259             :                         {
     260             :                             // unknown implementation, add to result
     261           0 :                             aDestination.push_back(xReference);
     262             :                         }
     263             :                     }
     264          24 :                 }
     265             : 
     266             :                 // prepare return value
     267           4 :                 const sal_uInt32 nDestSize(aDestination.size());
     268           4 :                 aRetval.realloc(nDestSize);
     269             : 
     270         372 :                 for(sal_uInt32 b(0); b < nDestSize; b++)
     271             :                 {
     272         368 :                     aRetval[b] = aDestination[b];
     273           4 :                 }
     274             :             }
     275             : 
     276           4 :             return aRetval;
     277             :         }
     278             : 
     279           4 :         HatchTexturePrimitive3D::HatchTexturePrimitive3D(
     280             :             const attribute::FillHatchAttribute& rHatch,
     281             :             const Primitive3DSequence& rChildren,
     282             :             const basegfx::B2DVector& rTextureSize,
     283             :             bool bModulate,
     284             :             bool bFilter)
     285             :         :   TexturePrimitive3D(rChildren, rTextureSize, bModulate, bFilter),
     286             :             maHatch(rHatch),
     287           4 :             maBuffered3DDecomposition()
     288             :         {
     289           4 :         }
     290             : 
     291           0 :         bool HatchTexturePrimitive3D::operator==(const BasePrimitive3D& rPrimitive) const
     292             :         {
     293           0 :             if(TexturePrimitive3D::operator==(rPrimitive))
     294             :             {
     295           0 :                 const HatchTexturePrimitive3D& rCompare = static_cast<const HatchTexturePrimitive3D&>(rPrimitive);
     296             : 
     297           0 :                 return (getHatch() == rCompare.getHatch());
     298             :             }
     299             : 
     300           0 :             return false;
     301             :         }
     302             : 
     303           4 :         Primitive3DSequence HatchTexturePrimitive3D::get3DDecomposition(const geometry::ViewInformation3D& /*rViewInformation*/) const
     304             :         {
     305           4 :             ::osl::MutexGuard aGuard( m_aMutex );
     306             : 
     307           4 :             if(!getBuffered3DDecomposition().hasElements())
     308             :             {
     309           4 :                 const Primitive3DSequence aNewSequence(impCreate3DDecomposition());
     310           4 :                 const_cast< HatchTexturePrimitive3D* >(this)->setBuffered3DDecomposition(aNewSequence);
     311             :             }
     312             : 
     313           4 :             return getBuffered3DDecomposition();
     314             :         }
     315             : 
     316             :         // provide unique ID
     317           8 :         ImplPrimitive3DIDBlock(HatchTexturePrimitive3D, PRIMITIVE3D_ID_HATCHTEXTUREPRIMITIVE3D)
     318             : 
     319             :     } // end of namespace primitive3d
     320             : } // end of namespace drawinglayer
     321             : 
     322             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10