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