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