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