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