1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <drawinglayer/primitive3d/hatchtextureprimitive3d.hxx>
30 #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
31 #include <basegfx/polygon/b2dpolypolygon.hxx>
32 #include <basegfx/polygon/b3dpolygon.hxx>
33 #include <basegfx/polygon/b2dpolygon.hxx>
34 #include <basegfx/polygon/b2dpolypolygontools.hxx>
35 #include <basegfx/range/b2drange.hxx>
36 #include <drawinglayer/texture/texture.hxx>
37 #include <basegfx/polygon/b2dpolygonclipper.hxx>
38 #include <basegfx/matrix/b3dhommatrix.hxx>
39 #include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
40 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
42 //////////////////////////////////////////////////////////////////////////////
44 using namespace com::sun::star
;
46 //////////////////////////////////////////////////////////////////////////////
48 namespace drawinglayer
52 Primitive3DSequence
HatchTexturePrimitive3D::impCreate3DDecomposition() const
54 Primitive3DSequence aRetval
;
56 if(getChildren().hasElements())
58 const Primitive3DSequence
aSource(getChildren());
59 const sal_uInt32
nSourceCount(aSource
.getLength());
60 std::vector
< Primitive3DReference
> aDestination
;
62 for(sal_uInt32
a(0); a
< nSourceCount
; a
++)
65 const Primitive3DReference
xReference(aSource
[a
]);
69 // try to cast to BasePrimitive2D implementation
70 const BasePrimitive3D
* pBasePrimitive
= dynamic_cast< const BasePrimitive3D
* >(xReference
.get());
74 // it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch
75 // not all content is needed, remove transparencies and ModifiedColorPrimitives
76 switch(pBasePrimitive
->getPrimitive3DID())
78 case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D
:
80 // polyPolygonMaterialPrimitive3D, check texturing and hatching
81 const PolyPolygonMaterialPrimitive3D
& rPrimitive
= static_cast< const PolyPolygonMaterialPrimitive3D
& >(*pBasePrimitive
);
82 const basegfx::B3DPolyPolygon
aFillPolyPolygon(rPrimitive
.getB3DPolyPolygon());
84 if(maHatch
.isFillBackground())
86 // add original primitive for background
87 aDestination
.push_back(xReference
);
90 if(aFillPolyPolygon
.areTextureCoordinatesUsed())
92 const sal_uInt32
nPolyCount(aFillPolyPolygon
.count());
93 basegfx::B2DPolyPolygon aTexPolyPolygon
;
94 basegfx::B2DPoint a2N
;
95 basegfx::B2DVector a2X
, a2Y
;
96 basegfx::B3DPoint a3N
;
97 basegfx::B3DVector a3X
, a3Y
;
98 bool b2N(false), b2X(false), b2Y(false);
100 for(sal_uInt32
b(0); b
< nPolyCount
; b
++)
102 const basegfx::B3DPolygon
aPartPoly(aFillPolyPolygon
.getB3DPolygon(b
));
103 const sal_uInt32
nPointCount(aPartPoly
.count());
104 basegfx::B2DPolygon aTexPolygon
;
106 for(sal_uInt32
c(0); c
< nPointCount
; c
++)
108 const basegfx::B2DPoint
a2Candidate(aPartPoly
.getTextureCoordinate(c
));
113 a3N
= aPartPoly
.getB3DPoint(c
);
116 else if(!b2X
&& !a2N
.equal(a2Candidate
))
118 a2X
= a2Candidate
- a2N
;
119 a3X
= aPartPoly
.getB3DPoint(c
) - a3N
;
122 else if(!b2Y
&& !a2N
.equal(a2Candidate
) && !a2X
.equal(a2Candidate
))
124 a2Y
= a2Candidate
- a2N
;
126 const double fCross(a2X
.cross(a2Y
));
128 if(!basegfx::fTools::equalZero(fCross
))
130 a3Y
= aPartPoly
.getB3DPoint(c
) - a3N
;
135 aTexPolygon
.append(a2Candidate
);
138 aTexPolygon
.setClosed(true);
139 aTexPolyPolygon
.append(aTexPolygon
);
142 if(b2N
&& b2X
&& b2Y
)
144 // found two linearly independent 2D vectors
145 // get 2d range of texture coordinates
146 const basegfx::B2DRange
aOutlineRange(basegfx::tools::getRange(aTexPolyPolygon
));
147 const basegfx::BColor
aHatchColor(getHatch().getColor());
148 const double fAngle(getHatch().getAngle());
149 ::std::vector
< basegfx::B2DHomMatrix
> aMatrices
;
151 // get hatch transformations
152 switch(getHatch().getStyle())
154 case attribute::HATCHSTYLE_TRIPLE
:
156 // rotated 45 degrees
157 texture::GeoTexSvxHatch
aHatch(aOutlineRange
, getHatch().getDistance(), fAngle
- F_PI4
);
158 aHatch
.appendTransformations(aMatrices
);
160 case attribute::HATCHSTYLE_DOUBLE
:
162 // rotated 90 degrees
163 texture::GeoTexSvxHatch
aHatch(aOutlineRange
, getHatch().getDistance(), fAngle
- F_PI2
);
164 aHatch
.appendTransformations(aMatrices
);
166 case attribute::HATCHSTYLE_SINGLE
:
169 texture::GeoTexSvxHatch
aHatch(aOutlineRange
, getHatch().getDistance(), fAngle
);
170 aHatch
.appendTransformations(aMatrices
);
174 // create geometry from unit line
175 basegfx::B2DPolyPolygon a2DHatchLines
;
176 basegfx::B2DPolygon a2DUnitLine
;
177 a2DUnitLine
.append(basegfx::B2DPoint(0.0, 0.0));
178 a2DUnitLine
.append(basegfx::B2DPoint(1.0, 0.0));
180 for(sal_uInt32
c(0); c
< aMatrices
.size(); c
++)
182 const basegfx::B2DHomMatrix
& rMatrix
= aMatrices
[c
];
183 basegfx::B2DPolygon
aNewLine(a2DUnitLine
);
184 aNewLine
.transform(rMatrix
);
185 a2DHatchLines
.append(aNewLine
);
188 if(a2DHatchLines
.count())
190 // clip against texture polygon
191 a2DHatchLines
= basegfx::tools::clipPolyPolygonOnPolyPolygon(a2DHatchLines
, aTexPolyPolygon
, true, true);
194 if(a2DHatchLines
.count())
196 // create 2d matrix with 2d vectors as column vectors and 2d point as offset, this represents
197 // a coordinate system transformation from unit coordinates to the new coordinate system
198 basegfx::B2DHomMatrix a2D
;
199 a2D
.set(0, 0, a2X
.getX());
200 a2D
.set(1, 0, a2X
.getY());
201 a2D
.set(0, 1, a2Y
.getX());
202 a2D
.set(1, 1, a2Y
.getY());
203 a2D
.set(0, 2, a2N
.getX());
204 a2D
.set(1, 2, a2N
.getY());
206 // invert that transformation, so we have a back-transformation from texture coordinates
207 // to unit coordinates
209 a2DHatchLines
.transform(a2D
);
211 // expand back-transformated geometry tpo 3D
212 basegfx::B3DPolyPolygon
a3DHatchLines(basegfx::tools::createB3DPolyPolygonFromB2DPolyPolygon(a2DHatchLines
, 0.0));
214 // create 3d matrix with 3d vectors as column vectors (0,0,1 as Z) and 3d point as offset, this represents
215 // a coordinate system transformation from unit coordinates to the object's 3d coordinate system
216 basegfx::B3DHomMatrix a3D
;
217 a3D
.set(0, 0, a3X
.getX());
218 a3D
.set(1, 0, a3X
.getY());
219 a3D
.set(2, 0, a3X
.getZ());
220 a3D
.set(0, 1, a3Y
.getX());
221 a3D
.set(1, 1, a3Y
.getY());
222 a3D
.set(2, 1, a3Y
.getZ());
223 a3D
.set(0, 3, a3N
.getX());
224 a3D
.set(1, 3, a3N
.getY());
225 a3D
.set(2, 3, a3N
.getZ());
227 // transform hatch lines to 3D object coordinates
228 a3DHatchLines
.transform(a3D
);
230 // build primitives from this geometry
231 const sal_uInt32
nHatchLines(a3DHatchLines
.count());
233 for(sal_uInt32
d(0); d
< nHatchLines
; d
++)
235 const Primitive3DReference
xRef(new PolygonHairlinePrimitive3D(a3DHatchLines
.getB3DPolygon(d
), aHatchColor
));
236 aDestination
.push_back(xRef
);
246 // add reference to result
247 aDestination
.push_back(xReference
);
254 // unknown implementation, add to result
255 aDestination
.push_back(xReference
);
260 // prepare return value
261 const sal_uInt32
nDestSize(aDestination
.size());
262 aRetval
.realloc(nDestSize
);
264 for(sal_uInt32
b(0); b
< nDestSize
; b
++)
266 aRetval
[b
] = aDestination
[b
];
273 HatchTexturePrimitive3D::HatchTexturePrimitive3D(
274 const attribute::FillHatchAttribute
& rHatch
,
275 const Primitive3DSequence
& rChildren
,
276 const basegfx::B2DVector
& rTextureSize
,
279 : TexturePrimitive3D(rChildren
, rTextureSize
, bModulate
, bFilter
),
281 maBuffered3DDecomposition()
285 bool HatchTexturePrimitive3D::operator==(const BasePrimitive3D
& rPrimitive
) const
287 if(TexturePrimitive3D::operator==(rPrimitive
))
289 const HatchTexturePrimitive3D
& rCompare
= (HatchTexturePrimitive3D
&)rPrimitive
;
291 return (getHatch() == rCompare
.getHatch());
297 Primitive3DSequence
HatchTexturePrimitive3D::get3DDecomposition(const geometry::ViewInformation3D
& /*rViewInformation*/) const
299 ::osl::MutexGuard
aGuard( m_aMutex
);
301 if(!getBuffered3DDecomposition().hasElements())
303 const Primitive3DSequence
aNewSequence(impCreate3DDecomposition());
304 const_cast< HatchTexturePrimitive3D
* >(this)->setBuffered3DDecomposition(aNewSequence
);
307 return getBuffered3DDecomposition();
311 ImplPrimitrive3DIDBlock(HatchTexturePrimitive3D
, PRIMITIVE3D_ID_HATCHTEXTUREPRIMITIVE3D
)
313 } // end of namespace primitive3d
314 } // end of namespace drawinglayer
316 //////////////////////////////////////////////////////////////////////////////
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */