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