1*464702f4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*464702f4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*464702f4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*464702f4SAndrew Rist  * distributed with this work for additional information
6*464702f4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*464702f4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*464702f4SAndrew Rist  * "License"); you may not use this file except in compliance
9*464702f4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*464702f4SAndrew Rist  *
11*464702f4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*464702f4SAndrew Rist  *
13*464702f4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*464702f4SAndrew Rist  * software distributed under the License is distributed on an
15*464702f4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*464702f4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*464702f4SAndrew Rist  * specific language governing permissions and limitations
18*464702f4SAndrew Rist  * under the License.
19*464702f4SAndrew Rist  *
20*464702f4SAndrew Rist  *************************************************************/
21*464702f4SAndrew Rist 
22*464702f4SAndrew 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
155cdf0e10cSrcweir 													texture::GeoTexSvxHatch aHatch(aOutlineRange, getHatch().getDistance(), fAngle - F_PI4);
156cdf0e10cSrcweir 													aHatch.appendTransformations(aMatrices);
157cdf0e10cSrcweir 												}
158cdf0e10cSrcweir 												case attribute::HATCHSTYLE_DOUBLE:
159cdf0e10cSrcweir 												{
160cdf0e10cSrcweir 													// rotated 90 degrees
161cdf0e10cSrcweir 													texture::GeoTexSvxHatch aHatch(aOutlineRange, getHatch().getDistance(), fAngle - F_PI2);
162cdf0e10cSrcweir 													aHatch.appendTransformations(aMatrices);
163cdf0e10cSrcweir 												}
164cdf0e10cSrcweir 												case attribute::HATCHSTYLE_SINGLE:
165cdf0e10cSrcweir 												{
166cdf0e10cSrcweir 													// angle as given
167cdf0e10cSrcweir 													texture::GeoTexSvxHatch aHatch(aOutlineRange, getHatch().getDistance(), fAngle);
168cdf0e10cSrcweir 													aHatch.appendTransformations(aMatrices);
169cdf0e10cSrcweir 												}
170cdf0e10cSrcweir 											}
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 											// create geometry from unit line
173cdf0e10cSrcweir 											basegfx::B2DPolyPolygon a2DHatchLines;
174cdf0e10cSrcweir 											basegfx::B2DPolygon a2DUnitLine;
175cdf0e10cSrcweir 											a2DUnitLine.append(basegfx::B2DPoint(0.0, 0.0));
176cdf0e10cSrcweir 											a2DUnitLine.append(basegfx::B2DPoint(1.0, 0.0));
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 											for(sal_uInt32 c(0); c < aMatrices.size(); c++)
179cdf0e10cSrcweir 											{
180cdf0e10cSrcweir 												const basegfx::B2DHomMatrix& rMatrix = aMatrices[c];
181cdf0e10cSrcweir 												basegfx::B2DPolygon aNewLine(a2DUnitLine);
182cdf0e10cSrcweir 												aNewLine.transform(rMatrix);
183cdf0e10cSrcweir 												a2DHatchLines.append(aNewLine);
184cdf0e10cSrcweir 											}
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 											if(a2DHatchLines.count())
187cdf0e10cSrcweir 											{
188cdf0e10cSrcweir 												// clip against texture polygon
189cdf0e10cSrcweir 												a2DHatchLines = basegfx::tools::clipPolyPolygonOnPolyPolygon(a2DHatchLines, aTexPolyPolygon, true, true);
190cdf0e10cSrcweir 											}
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 											if(a2DHatchLines.count())
193cdf0e10cSrcweir 											{
194cdf0e10cSrcweir 												// create 2d matrix with 2d vectors as column vectors and 2d point as offset, this represents
195cdf0e10cSrcweir 												// a coordinate system transformation from unit coordinates to the new coordinate system
196cdf0e10cSrcweir 												basegfx::B2DHomMatrix a2D;
197cdf0e10cSrcweir 												a2D.set(0, 0, a2X.getX());
198cdf0e10cSrcweir 												a2D.set(1, 0, a2X.getY());
199cdf0e10cSrcweir 												a2D.set(0, 1, a2Y.getX());
200cdf0e10cSrcweir 												a2D.set(1, 1, a2Y.getY());
201cdf0e10cSrcweir 												a2D.set(0, 2, a2N.getX());
202cdf0e10cSrcweir 												a2D.set(1, 2, a2N.getY());
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 												// invert that transformation, so we have a back-transformation from texture coordinates
205cdf0e10cSrcweir 												// to unit coordinates
206cdf0e10cSrcweir 												a2D.invert();
207cdf0e10cSrcweir 												a2DHatchLines.transform(a2D);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 												// expand back-transformated geometry tpo 3D
210cdf0e10cSrcweir 												basegfx::B3DPolyPolygon a3DHatchLines(basegfx::tools::createB3DPolyPolygonFromB2DPolyPolygon(a2DHatchLines, 0.0));
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 												// create 3d matrix with 3d vectors as column vectors (0,0,1 as Z) and 3d point as offset, this represents
213cdf0e10cSrcweir 												// a coordinate system transformation from unit coordinates to the object's 3d coordinate system
214cdf0e10cSrcweir 												basegfx::B3DHomMatrix a3D;
215cdf0e10cSrcweir 												a3D.set(0, 0, a3X.getX());
216cdf0e10cSrcweir 												a3D.set(1, 0, a3X.getY());
217cdf0e10cSrcweir 												a3D.set(2, 0, a3X.getZ());
218cdf0e10cSrcweir 												a3D.set(0, 1, a3Y.getX());
219cdf0e10cSrcweir 												a3D.set(1, 1, a3Y.getY());
220cdf0e10cSrcweir 												a3D.set(2, 1, a3Y.getZ());
221cdf0e10cSrcweir 												a3D.set(0, 3, a3N.getX());
222cdf0e10cSrcweir 												a3D.set(1, 3, a3N.getY());
223cdf0e10cSrcweir 												a3D.set(2, 3, a3N.getZ());
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 												// transform hatch lines to 3D object coordinates
226cdf0e10cSrcweir 												a3DHatchLines.transform(a3D);
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 												// build primitives from this geometry
229cdf0e10cSrcweir 												const sal_uInt32 nHatchLines(a3DHatchLines.count());
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 												for(sal_uInt32 d(0); d < nHatchLines; d++)
232cdf0e10cSrcweir 												{
233cdf0e10cSrcweir 													const Primitive3DReference xRef(new PolygonHairlinePrimitive3D(a3DHatchLines.getB3DPolygon(d), aHatchColor));
234cdf0e10cSrcweir 													aDestination.push_back(xRef);
235cdf0e10cSrcweir 												}
236cdf0e10cSrcweir 											}
237cdf0e10cSrcweir 										}
238cdf0e10cSrcweir 									}
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 									break;
241cdf0e10cSrcweir 								}
242cdf0e10cSrcweir 								default :
243cdf0e10cSrcweir 								{
244cdf0e10cSrcweir 									// add reference to result
245cdf0e10cSrcweir 									aDestination.push_back(xReference);
246cdf0e10cSrcweir 									break;
247cdf0e10cSrcweir 								}
248cdf0e10cSrcweir 							}
249cdf0e10cSrcweir 						}
250cdf0e10cSrcweir 						else
251cdf0e10cSrcweir 						{
252cdf0e10cSrcweir 							// unknown implementation, add to result
253cdf0e10cSrcweir 							aDestination.push_back(xReference);
254cdf0e10cSrcweir 						}
255cdf0e10cSrcweir 					}
256cdf0e10cSrcweir 				}
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 				// prepare return value
259cdf0e10cSrcweir 				const sal_uInt32 nDestSize(aDestination.size());
260cdf0e10cSrcweir 				aRetval.realloc(nDestSize);
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 				for(sal_uInt32 b(0); b < nDestSize; b++)
263cdf0e10cSrcweir 				{
264cdf0e10cSrcweir 					aRetval[b] = aDestination[b];
265cdf0e10cSrcweir 				}
266cdf0e10cSrcweir 			}
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 			return aRetval;
269cdf0e10cSrcweir 		}
270cdf0e10cSrcweir 
HatchTexturePrimitive3D(const attribute::FillHatchAttribute & rHatch,const Primitive3DSequence & rChildren,const basegfx::B2DVector & rTextureSize,bool bModulate,bool bFilter)271cdf0e10cSrcweir 		HatchTexturePrimitive3D::HatchTexturePrimitive3D(
272cdf0e10cSrcweir 			const attribute::FillHatchAttribute& rHatch,
273cdf0e10cSrcweir 			const Primitive3DSequence& rChildren,
274cdf0e10cSrcweir 			const basegfx::B2DVector& rTextureSize,
275cdf0e10cSrcweir 			bool bModulate,
276cdf0e10cSrcweir 			bool bFilter)
277cdf0e10cSrcweir 		:	TexturePrimitive3D(rChildren, rTextureSize, bModulate, bFilter),
278cdf0e10cSrcweir 			maHatch(rHatch),
279cdf0e10cSrcweir             maBuffered3DDecomposition()
280cdf0e10cSrcweir 		{
281cdf0e10cSrcweir 		}
282cdf0e10cSrcweir 
operator ==(const BasePrimitive3D & rPrimitive) const283cdf0e10cSrcweir 		bool HatchTexturePrimitive3D::operator==(const BasePrimitive3D& rPrimitive) const
284cdf0e10cSrcweir 		{
285cdf0e10cSrcweir 			if(TexturePrimitive3D::operator==(rPrimitive))
286cdf0e10cSrcweir 			{
287cdf0e10cSrcweir 				const HatchTexturePrimitive3D& rCompare = (HatchTexturePrimitive3D&)rPrimitive;
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 				return (getHatch() == rCompare.getHatch());
290cdf0e10cSrcweir 			}
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 			return false;
293cdf0e10cSrcweir 		}
294cdf0e10cSrcweir 
get3DDecomposition(const geometry::ViewInformation3D &) const295cdf0e10cSrcweir         Primitive3DSequence HatchTexturePrimitive3D::get3DDecomposition(const geometry::ViewInformation3D& /*rViewInformation*/) const
296cdf0e10cSrcweir         {
297cdf0e10cSrcweir 			::osl::MutexGuard aGuard( m_aMutex );
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 			if(!getBuffered3DDecomposition().hasElements())
300cdf0e10cSrcweir 			{
301cdf0e10cSrcweir 				const Primitive3DSequence aNewSequence(impCreate3DDecomposition());
302cdf0e10cSrcweir 				const_cast< HatchTexturePrimitive3D* >(this)->setBuffered3DDecomposition(aNewSequence);
303cdf0e10cSrcweir 			}
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 			return getBuffered3DDecomposition();
306cdf0e10cSrcweir         }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 		// provide unique ID
309cdf0e10cSrcweir 		ImplPrimitrive3DIDBlock(HatchTexturePrimitive3D, PRIMITIVE3D_ID_HATCHTEXTUREPRIMITIVE3D)
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 	} // end of namespace primitive3d
312cdf0e10cSrcweir } // end of namespace drawinglayer
313cdf0e10cSrcweir 
314cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
315cdf0e10cSrcweir // eof
316