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 20 : Primitive3DSequence HatchTexturePrimitive3D::impCreate3DDecomposition() const
44 : {
45 20 : Primitive3DSequence aRetval;
46 :
47 20 : if(getChildren().hasElements())
48 : {
49 20 : const Primitive3DSequence aSource(getChildren());
50 20 : const sal_uInt32 nSourceCount(aSource.getLength());
51 40 : std::vector< Primitive3DReference > aDestination;
52 :
53 140 : for(sal_uInt32 a(0); a < nSourceCount; a++)
54 : {
55 : // get reference
56 120 : const Primitive3DReference xReference(aSource[a]);
57 :
58 120 : if(xReference.is())
59 : {
60 : // try to cast to BasePrimitive2D implementation
61 120 : const BasePrimitive3D* pBasePrimitive = dynamic_cast< const BasePrimitive3D* >(xReference.get());
62 :
63 120 : 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 120 : switch(pBasePrimitive->getPrimitive3DID())
68 : {
69 : case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D :
70 : {
71 : // polyPolygonMaterialPrimitive3D, check texturing and hatching
72 120 : const PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const PolyPolygonMaterialPrimitive3D& >(*pBasePrimitive);
73 120 : const basegfx::B3DPolyPolygon aFillPolyPolygon(rPrimitive.getB3DPolyPolygon());
74 :
75 120 : if(maHatch.isFillBackground())
76 : {
77 : // add original primitive for background
78 0 : aDestination.push_back(xReference);
79 : }
80 :
81 120 : if(aFillPolyPolygon.areTextureCoordinatesUsed())
82 : {
83 120 : const sal_uInt32 nPolyCount(aFillPolyPolygon.count());
84 120 : basegfx::B2DPolyPolygon aTexPolyPolygon;
85 240 : basegfx::B2DPoint a2N;
86 240 : basegfx::B2DVector a2X, a2Y;
87 240 : basegfx::B3DPoint a3N;
88 240 : basegfx::B3DVector a3X, a3Y;
89 120 : bool b2N(false), b2X(false), b2Y(false);
90 :
91 240 : for(sal_uInt32 b(0); b < nPolyCount; b++)
92 : {
93 120 : const basegfx::B3DPolygon aPartPoly(aFillPolyPolygon.getB3DPolygon(b));
94 120 : const sal_uInt32 nPointCount(aPartPoly.count());
95 240 : basegfx::B2DPolygon aTexPolygon;
96 :
97 600 : for(sal_uInt32 c(0); c < nPointCount; c++)
98 : {
99 480 : const basegfx::B2DPoint a2Candidate(aPartPoly.getTextureCoordinate(c));
100 :
101 480 : if(!b2N)
102 : {
103 120 : a2N = a2Candidate;
104 120 : a3N = aPartPoly.getB3DPoint(c);
105 120 : b2N = true;
106 : }
107 360 : else if(!b2X && !a2N.equal(a2Candidate))
108 : {
109 120 : a2X = a2Candidate - a2N;
110 120 : a3X = aPartPoly.getB3DPoint(c) - a3N;
111 120 : b2X = true;
112 : }
113 240 : else if(!b2Y && !a2N.equal(a2Candidate) && !a2X.equal(a2Candidate))
114 : {
115 100 : a2Y = a2Candidate - a2N;
116 :
117 100 : const double fCross(a2X.cross(a2Y));
118 :
119 100 : if(!basegfx::fTools::equalZero(fCross))
120 : {
121 40 : a3Y = aPartPoly.getB3DPoint(c) - a3N;
122 40 : b2Y = true;
123 : }
124 : }
125 :
126 480 : aTexPolygon.append(a2Candidate);
127 480 : }
128 :
129 120 : aTexPolygon.setClosed(true);
130 120 : aTexPolyPolygon.append(aTexPolygon);
131 120 : }
132 :
133 120 : if(b2N && b2X && b2Y)
134 : {
135 : // found two linearly independent 2D vectors
136 : // get 2d range of texture coordinates
137 40 : const basegfx::B2DRange aOutlineRange(basegfx::tools::getRange(aTexPolyPolygon));
138 40 : const basegfx::BColor aHatchColor(getHatch().getColor());
139 40 : const double fAngle(getHatch().getAngle());
140 80 : ::std::vector< basegfx::B2DHomMatrix > aMatrices;
141 :
142 : // get hatch transformations
143 40 : 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 40 : getHatch().getDistance(),
174 40 : fAngle);
175 :
176 40 : aHatch.appendTransformations(aMatrices);
177 : }
178 : }
179 :
180 : // create geometry from unit line
181 40 : basegfx::B2DPolyPolygon a2DHatchLines;
182 80 : basegfx::B2DPolygon a2DUnitLine;
183 40 : a2DUnitLine.append(basegfx::B2DPoint(0.0, 0.0));
184 40 : a2DUnitLine.append(basegfx::B2DPoint(1.0, 0.0));
185 :
186 1880 : for(size_t c(0); c < aMatrices.size(); c++)
187 : {
188 1840 : const basegfx::B2DHomMatrix& rMatrix = aMatrices[c];
189 1840 : basegfx::B2DPolygon aNewLine(a2DUnitLine);
190 1840 : aNewLine.transform(rMatrix);
191 1840 : a2DHatchLines.append(aNewLine);
192 1840 : }
193 :
194 40 : if(a2DHatchLines.count())
195 : {
196 : // clip against texture polygon
197 40 : a2DHatchLines = basegfx::tools::clipPolyPolygonOnPolyPolygon(a2DHatchLines, aTexPolyPolygon, true, true);
198 : }
199 :
200 40 : 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 40 : basegfx::B2DHomMatrix a2D;
205 40 : a2D.set(0, 0, a2X.getX());
206 40 : a2D.set(1, 0, a2X.getY());
207 40 : a2D.set(0, 1, a2Y.getX());
208 40 : a2D.set(1, 1, a2Y.getY());
209 40 : a2D.set(0, 2, a2N.getX());
210 40 : 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 40 : a2D.invert();
215 40 : a2DHatchLines.transform(a2D);
216 :
217 : // expand back-transformated geometry tpo 3D
218 80 : 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 80 : basegfx::B3DHomMatrix a3D;
223 40 : a3D.set(0, 0, a3X.getX());
224 40 : a3D.set(1, 0, a3X.getY());
225 40 : a3D.set(2, 0, a3X.getZ());
226 40 : a3D.set(0, 1, a3Y.getX());
227 40 : a3D.set(1, 1, a3Y.getY());
228 40 : a3D.set(2, 1, a3Y.getZ());
229 40 : a3D.set(0, 3, a3N.getX());
230 40 : a3D.set(1, 3, a3N.getY());
231 40 : a3D.set(2, 3, a3N.getZ());
232 :
233 : // transform hatch lines to 3D object coordinates
234 40 : a3DHatchLines.transform(a3D);
235 :
236 : // build primitives from this geometry
237 40 : const sal_uInt32 nHatchLines(a3DHatchLines.count());
238 :
239 1880 : for(sal_uInt32 d(0); d < nHatchLines; d++)
240 : {
241 1840 : const Primitive3DReference xRef(new PolygonHairlinePrimitive3D(a3DHatchLines.getB3DPolygon(d), aHatchColor));
242 1840 : aDestination.push_back(xRef);
243 1880 : }
244 80 : }
245 120 : }
246 : }
247 :
248 120 : 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 120 : }
265 :
266 : // prepare return value
267 20 : const sal_uInt32 nDestSize(aDestination.size());
268 20 : aRetval.realloc(nDestSize);
269 :
270 1860 : for(sal_uInt32 b(0); b < nDestSize; b++)
271 : {
272 1840 : aRetval[b] = aDestination[b];
273 20 : }
274 : }
275 :
276 20 : return aRetval;
277 : }
278 :
279 20 : 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 20 : maBuffered3DDecomposition()
288 : {
289 20 : }
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 20 : Primitive3DSequence HatchTexturePrimitive3D::get3DDecomposition(const geometry::ViewInformation3D& /*rViewInformation*/) const
304 : {
305 20 : ::osl::MutexGuard aGuard( m_aMutex );
306 :
307 20 : if(!getBuffered3DDecomposition().hasElements())
308 : {
309 20 : const Primitive3DSequence aNewSequence(impCreate3DDecomposition());
310 20 : const_cast< HatchTexturePrimitive3D* >(this)->setBuffered3DDecomposition(aNewSequence);
311 : }
312 :
313 20 : return getBuffered3DDecomposition();
314 : }
315 :
316 : // provide unique ID
317 40 : 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: */
|