109dbbe93SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
309dbbe93SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
409dbbe93SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
509dbbe93SAndrew Rist  * distributed with this work for additional information
609dbbe93SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
709dbbe93SAndrew Rist  * to you under the Apache License, Version 2.0 (the
809dbbe93SAndrew Rist  * "License"); you may not use this file except in compliance
909dbbe93SAndrew Rist  * with the License.  You may obtain a copy of the License at
1009dbbe93SAndrew Rist  *
1109dbbe93SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1209dbbe93SAndrew Rist  *
1309dbbe93SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1409dbbe93SAndrew Rist  * software distributed under the License is distributed on an
1509dbbe93SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1609dbbe93SAndrew Rist  * KIND, either express or implied.  See the License for the
1709dbbe93SAndrew Rist  * specific language governing permissions and limitations
1809dbbe93SAndrew Rist  * under the License.
1909dbbe93SAndrew Rist  *
2009dbbe93SAndrew Rist  *************************************************************/
2109dbbe93SAndrew Rist 
2209dbbe93SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_basegfx.hxx"
26cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygontools.hxx>
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx>
29cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
30cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
31cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
32cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <numeric>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace basegfx
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 	namespace tools
41cdf0e10cSrcweir 	{
42cdf0e10cSrcweir 		B2DPolyPolygon correctOrientations(const B2DPolyPolygon& rCandidate)
43cdf0e10cSrcweir 		{
44cdf0e10cSrcweir 			B2DPolyPolygon aRetval(rCandidate);
45cdf0e10cSrcweir 			const sal_uInt32 nCount(aRetval.count());
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nCount; a++)
48cdf0e10cSrcweir 			{
49cdf0e10cSrcweir 				const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
50cdf0e10cSrcweir 				const B2VectorOrientation aOrientation(tools::getOrientation(aCandidate));
51cdf0e10cSrcweir 				sal_uInt32 nDepth(0L);
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 				for(sal_uInt32 b(0L); b < nCount; b++)
54cdf0e10cSrcweir 				{
55cdf0e10cSrcweir 					if(b != a)
56cdf0e10cSrcweir 					{
57cdf0e10cSrcweir 						const B2DPolygon aCompare(rCandidate.getB2DPolygon(b));
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 						if(tools::isInside(aCompare, aCandidate, true))
60cdf0e10cSrcweir 						{
61cdf0e10cSrcweir 							nDepth++;
62cdf0e10cSrcweir 						}
63cdf0e10cSrcweir 					}
64cdf0e10cSrcweir 				}
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 				const bool bShallBeHole(1L == (nDepth & 0x00000001));
67cdf0e10cSrcweir 				const bool bIsHole(ORIENTATION_NEGATIVE == aOrientation);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 				if(bShallBeHole != bIsHole && ORIENTATION_NEUTRAL != aOrientation)
70cdf0e10cSrcweir 				{
71cdf0e10cSrcweir 					B2DPolygon aFlipped(aCandidate);
72cdf0e10cSrcweir 					aFlipped.flip();
73cdf0e10cSrcweir 					aRetval.setB2DPolygon(a, aFlipped);
74cdf0e10cSrcweir 				}
75cdf0e10cSrcweir 			}
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 			return aRetval;
78cdf0e10cSrcweir 		}
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 		B2DPolyPolygon correctOutmostPolygon(const B2DPolyPolygon& rCandidate)
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			const sal_uInt32 nCount(rCandidate.count());
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 			if(nCount > 1L)
85cdf0e10cSrcweir 			{
86cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < nCount; a++)
87cdf0e10cSrcweir 				{
88cdf0e10cSrcweir 					const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
89cdf0e10cSrcweir 					sal_uInt32 nDepth(0L);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 					for(sal_uInt32 b(0L); b < nCount; b++)
92cdf0e10cSrcweir 					{
93cdf0e10cSrcweir 						if(b != a)
94cdf0e10cSrcweir 						{
95cdf0e10cSrcweir 							const B2DPolygon aCompare(rCandidate.getB2DPolygon(b));
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 							if(tools::isInside(aCompare, aCandidate, true))
98cdf0e10cSrcweir 							{
99cdf0e10cSrcweir 								nDepth++;
100cdf0e10cSrcweir 							}
101cdf0e10cSrcweir 						}
102cdf0e10cSrcweir 					}
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 					if(!nDepth)
105cdf0e10cSrcweir 					{
106cdf0e10cSrcweir 						B2DPolyPolygon aRetval(rCandidate);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 						if(a != 0L)
109cdf0e10cSrcweir 						{
110cdf0e10cSrcweir 							// exchange polygon a and polygon 0L
111cdf0e10cSrcweir 							aRetval.setB2DPolygon(0L, aCandidate);
112cdf0e10cSrcweir 							aRetval.setB2DPolygon(a, rCandidate.getB2DPolygon(0L));
113cdf0e10cSrcweir 						}
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 						// exit
116cdf0e10cSrcweir 						return aRetval;
117cdf0e10cSrcweir 					}
118cdf0e10cSrcweir 				}
119cdf0e10cSrcweir 			}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 			return rCandidate;
122cdf0e10cSrcweir 		}
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 		B2DPolyPolygon adaptiveSubdivideByDistance(const B2DPolyPolygon& rCandidate, double fDistanceBound)
125cdf0e10cSrcweir 		{
126cdf0e10cSrcweir 			if(rCandidate.areControlPointsUsed())
127cdf0e10cSrcweir 			{
128cdf0e10cSrcweir 				const sal_uInt32 nPolygonCount(rCandidate.count());
129cdf0e10cSrcweir 				B2DPolyPolygon aRetval;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < nPolygonCount; a++)
132cdf0e10cSrcweir 				{
133cdf0e10cSrcweir 					const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 					if(aCandidate.areControlPointsUsed())
136cdf0e10cSrcweir 					{
137cdf0e10cSrcweir 						aRetval.append(tools::adaptiveSubdivideByDistance(aCandidate, fDistanceBound));
138cdf0e10cSrcweir 					}
139cdf0e10cSrcweir 					else
140cdf0e10cSrcweir 					{
141cdf0e10cSrcweir 						aRetval.append(aCandidate);
142cdf0e10cSrcweir 					}
143cdf0e10cSrcweir 				}
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 				return aRetval;
146cdf0e10cSrcweir 			}
147cdf0e10cSrcweir 			else
148cdf0e10cSrcweir 			{
149cdf0e10cSrcweir 				return rCandidate;
150cdf0e10cSrcweir 			}
151cdf0e10cSrcweir 		}
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 		B2DPolyPolygon adaptiveSubdivideByAngle(const B2DPolyPolygon& rCandidate, double fAngleBound)
154cdf0e10cSrcweir 		{
155cdf0e10cSrcweir 			if(rCandidate.areControlPointsUsed())
156cdf0e10cSrcweir 			{
157cdf0e10cSrcweir 				const sal_uInt32 nPolygonCount(rCandidate.count());
158cdf0e10cSrcweir 				B2DPolyPolygon aRetval;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < nPolygonCount; a++)
161cdf0e10cSrcweir 				{
162cdf0e10cSrcweir 					const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 					if(aCandidate.areControlPointsUsed())
165cdf0e10cSrcweir 					{
166cdf0e10cSrcweir 						aRetval.append(tools::adaptiveSubdivideByAngle(aCandidate, fAngleBound));
167cdf0e10cSrcweir 					}
168cdf0e10cSrcweir 					else
169cdf0e10cSrcweir 					{
170cdf0e10cSrcweir 						aRetval.append(aCandidate);
171cdf0e10cSrcweir 					}
172cdf0e10cSrcweir 				}
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 				return aRetval;
175cdf0e10cSrcweir 			}
176cdf0e10cSrcweir 			else
177cdf0e10cSrcweir 			{
178cdf0e10cSrcweir 				return rCandidate;
179cdf0e10cSrcweir 			}
180cdf0e10cSrcweir 		}
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 		B2DPolyPolygon adaptiveSubdivideByCount(const B2DPolyPolygon& rCandidate, sal_uInt32 nCount)
183cdf0e10cSrcweir 		{
184cdf0e10cSrcweir 			if(rCandidate.areControlPointsUsed())
185cdf0e10cSrcweir 			{
186cdf0e10cSrcweir 				const sal_uInt32 nPolygonCount(rCandidate.count());
187cdf0e10cSrcweir 				B2DPolyPolygon aRetval;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < nPolygonCount; a++)
190cdf0e10cSrcweir 				{
191cdf0e10cSrcweir 					const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 					if(aCandidate.areControlPointsUsed())
194cdf0e10cSrcweir 					{
195cdf0e10cSrcweir 						aRetval.append(tools::adaptiveSubdivideByCount(aCandidate, nCount));
196cdf0e10cSrcweir 					}
197cdf0e10cSrcweir 					else
198cdf0e10cSrcweir 					{
199cdf0e10cSrcweir 						aRetval.append(aCandidate);
200cdf0e10cSrcweir 					}
201cdf0e10cSrcweir 				}
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 				return aRetval;
204cdf0e10cSrcweir 			}
205cdf0e10cSrcweir 			else
206cdf0e10cSrcweir 			{
207cdf0e10cSrcweir 				return rCandidate;
208cdf0e10cSrcweir 			}
209cdf0e10cSrcweir 		}
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 		bool isInside(const B2DPolyPolygon& rCandidate, const B2DPoint& rPoint, bool bWithBorder)
212cdf0e10cSrcweir 		{
213cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 			if(1L == nPolygonCount)
216cdf0e10cSrcweir 			{
217cdf0e10cSrcweir 				return isInside(rCandidate.getB2DPolygon(0L), rPoint, bWithBorder);
218cdf0e10cSrcweir 			}
219cdf0e10cSrcweir 			else
220cdf0e10cSrcweir 			{
221cdf0e10cSrcweir 				sal_Int32 nInsideCount(0L);
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < nPolygonCount; a++)
224cdf0e10cSrcweir 				{
225cdf0e10cSrcweir 					const B2DPolygon aPolygon(rCandidate.getB2DPolygon(a));
226cdf0e10cSrcweir 					const bool bInside(isInside(aPolygon, rPoint, bWithBorder));
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 					if(bInside)
229cdf0e10cSrcweir 					{
230cdf0e10cSrcweir 						nInsideCount++;
231cdf0e10cSrcweir 					}
232cdf0e10cSrcweir 				}
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 				return (nInsideCount % 2L);
235cdf0e10cSrcweir 			}
236cdf0e10cSrcweir 		}
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 		B2DRange getRangeWithControlPoints(const B2DPolyPolygon& rCandidate)
239cdf0e10cSrcweir 		{
240cdf0e10cSrcweir 			B2DRange aRetval;
241cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
244cdf0e10cSrcweir 			{
245cdf0e10cSrcweir 				B2DPolygon aCandidate = rCandidate.getB2DPolygon(a);
246cdf0e10cSrcweir 				aRetval.expand(tools::getRangeWithControlPoints(aCandidate));
247cdf0e10cSrcweir 			}
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 			return aRetval;
250cdf0e10cSrcweir 		}
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 		B2DRange getRange(const B2DPolyPolygon& rCandidate)
253cdf0e10cSrcweir 		{
254cdf0e10cSrcweir 			B2DRange aRetval;
255cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
258cdf0e10cSrcweir 			{
259cdf0e10cSrcweir 				B2DPolygon aCandidate = rCandidate.getB2DPolygon(a);
260cdf0e10cSrcweir 				aRetval.expand(tools::getRange(aCandidate));
261cdf0e10cSrcweir 			}
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 			return aRetval;
264cdf0e10cSrcweir 		}
265cdf0e10cSrcweir 
266*4665f8d3SArmin Le Grand 		double getSignedArea(const B2DPolyPolygon& rCandidate)
267*4665f8d3SArmin Le Grand 		{
268*4665f8d3SArmin Le Grand 			double fRetval(0.0);
269*4665f8d3SArmin Le Grand 			const sal_uInt32 nPolygonCount(rCandidate.count());
270*4665f8d3SArmin Le Grand 
271*4665f8d3SArmin Le Grand 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
272*4665f8d3SArmin Le Grand 			{
273*4665f8d3SArmin Le Grand 				const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
274*4665f8d3SArmin Le Grand 
275*4665f8d3SArmin Le Grand                 fRetval += tools::getSignedArea(aCandidate);
276*4665f8d3SArmin Le Grand 			}
277*4665f8d3SArmin Le Grand 
278*4665f8d3SArmin Le Grand 			return fRetval;
279*4665f8d3SArmin Le Grand 		}
280*4665f8d3SArmin Le Grand 
281*4665f8d3SArmin Le Grand 		double getArea(const B2DPolyPolygon& rCandidate)
282*4665f8d3SArmin Le Grand 		{
283*4665f8d3SArmin Le Grand 			return fabs(getSignedArea(rCandidate));
284*4665f8d3SArmin Le Grand 		}
285*4665f8d3SArmin Le Grand 
286*4665f8d3SArmin Le Grand         void applyLineDashing(const B2DPolyPolygon& rCandidate, const ::std::vector<double>& rDotDashArray, B2DPolyPolygon* pLineTarget, B2DPolyPolygon* pGapTarget, double fFullDashDotLen)
287cdf0e10cSrcweir 		{
288cdf0e10cSrcweir 			if(0.0 == fFullDashDotLen && rDotDashArray.size())
289cdf0e10cSrcweir 			{
290cdf0e10cSrcweir 				// calculate fFullDashDotLen from rDotDashArray
291cdf0e10cSrcweir 				fFullDashDotLen = ::std::accumulate(rDotDashArray.begin(), rDotDashArray.end(), 0.0);
292cdf0e10cSrcweir 			}
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 			if(rCandidate.count() && fFullDashDotLen > 0.0)
295cdf0e10cSrcweir 			{
296cdf0e10cSrcweir 				B2DPolyPolygon aLineTarget, aGapTarget;
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
299cdf0e10cSrcweir 				{
300cdf0e10cSrcweir 					const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 					applyLineDashing(
303cdf0e10cSrcweir 						aCandidate,
304cdf0e10cSrcweir 						rDotDashArray,
305cdf0e10cSrcweir 						pLineTarget ? &aLineTarget : 0,
306cdf0e10cSrcweir 						pGapTarget ? &aGapTarget : 0,
307cdf0e10cSrcweir 						fFullDashDotLen);
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 					if(pLineTarget)
310cdf0e10cSrcweir 					{
311cdf0e10cSrcweir 						pLineTarget->append(aLineTarget);
312cdf0e10cSrcweir 					}
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 					if(pGapTarget)
315cdf0e10cSrcweir 					{
316cdf0e10cSrcweir 						pGapTarget->append(aGapTarget);
317cdf0e10cSrcweir 					}
318cdf0e10cSrcweir 				}
319cdf0e10cSrcweir 			}
320cdf0e10cSrcweir 		}
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 		bool isInEpsilonRange(const B2DPolyPolygon& rCandidate, const B2DPoint& rTestPosition, double fDistance)
323cdf0e10cSrcweir 		{
324cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
327cdf0e10cSrcweir 			{
328cdf0e10cSrcweir 				B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 				if(isInEpsilonRange(aCandidate, rTestPosition, fDistance))
331cdf0e10cSrcweir 				{
332cdf0e10cSrcweir 					return true;
333cdf0e10cSrcweir 				}
334cdf0e10cSrcweir 			}
335cdf0e10cSrcweir 
336cdf0e10cSrcweir 			return false;
337cdf0e10cSrcweir 		}
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 		B3DPolyPolygon createB3DPolyPolygonFromB2DPolyPolygon(const B2DPolyPolygon& rCandidate, double fZCoordinate)
340cdf0e10cSrcweir 		{
341cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
342cdf0e10cSrcweir 			B3DPolyPolygon aRetval;
343cdf0e10cSrcweir 
344cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
345cdf0e10cSrcweir 			{
346cdf0e10cSrcweir 				B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 				aRetval.append(createB3DPolygonFromB2DPolygon(aCandidate, fZCoordinate));
349cdf0e10cSrcweir 			}
350cdf0e10cSrcweir 
351cdf0e10cSrcweir 			return aRetval;
352cdf0e10cSrcweir 		}
353cdf0e10cSrcweir 
354cdf0e10cSrcweir 		B2DPolyPolygon createB2DPolyPolygonFromB3DPolyPolygon(const B3DPolyPolygon& rCandidate, const B3DHomMatrix& rMat)
355cdf0e10cSrcweir 		{
356cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
357cdf0e10cSrcweir 			B2DPolyPolygon aRetval;
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
360cdf0e10cSrcweir 			{
361cdf0e10cSrcweir 				B3DPolygon aCandidate(rCandidate.getB3DPolygon(a));
362cdf0e10cSrcweir 
363cdf0e10cSrcweir 				aRetval.append(createB2DPolygonFromB3DPolygon(aCandidate, rMat));
364cdf0e10cSrcweir 			}
365cdf0e10cSrcweir 
366cdf0e10cSrcweir 			return aRetval;
367cdf0e10cSrcweir 		}
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 		double getSmallestDistancePointToPolyPolygon(const B2DPolyPolygon& rCandidate, const B2DPoint& rTestPoint, sal_uInt32& rPolygonIndex, sal_uInt32& rEdgeIndex, double& rCut)
370cdf0e10cSrcweir 		{
371cdf0e10cSrcweir 			double fRetval(DBL_MAX);
372cdf0e10cSrcweir 			const double fZero(0.0);
373cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
376cdf0e10cSrcweir 			{
377cdf0e10cSrcweir 				const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
378cdf0e10cSrcweir 				sal_uInt32 nNewEdgeIndex;
379cdf0e10cSrcweir 				double fNewCut;
380cdf0e10cSrcweir 				const double fNewDistance(getSmallestDistancePointToPolygon(aCandidate, rTestPoint, nNewEdgeIndex, fNewCut));
381cdf0e10cSrcweir 
382cdf0e10cSrcweir 				if(DBL_MAX == fRetval || fNewDistance < fRetval)
383cdf0e10cSrcweir 				{
384cdf0e10cSrcweir 					fRetval = fNewDistance;
385cdf0e10cSrcweir 					rPolygonIndex = a;
386cdf0e10cSrcweir 					rEdgeIndex = nNewEdgeIndex;
387cdf0e10cSrcweir 					rCut = fNewCut;
388cdf0e10cSrcweir 
389cdf0e10cSrcweir 					if(fTools::equal(fRetval, fZero))
390cdf0e10cSrcweir 					{
391cdf0e10cSrcweir 						// already found zero distance, cannot get better. Ensure numerical zero value and end loop.
392cdf0e10cSrcweir 						fRetval = 0.0;
393cdf0e10cSrcweir 						break;
394cdf0e10cSrcweir 					}
395cdf0e10cSrcweir 				}
396cdf0e10cSrcweir 			}
397cdf0e10cSrcweir 
398cdf0e10cSrcweir 			return fRetval;
399cdf0e10cSrcweir 		}
400cdf0e10cSrcweir 
401cdf0e10cSrcweir 		B2DPolyPolygon distort(const B2DPolyPolygon& rCandidate, const B2DRange& rOriginal, const B2DPoint& rTopLeft, const B2DPoint& rTopRight, const B2DPoint& rBottomLeft, const B2DPoint& rBottomRight)
402cdf0e10cSrcweir 		{
403cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
404cdf0e10cSrcweir 			B2DPolyPolygon aRetval;
405cdf0e10cSrcweir 
406cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
407cdf0e10cSrcweir 			{
408cdf0e10cSrcweir 				const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 				aRetval.append(distort(aCandidate, rOriginal, rTopLeft, rTopRight, rBottomLeft, rBottomRight));
411cdf0e10cSrcweir 			}
412cdf0e10cSrcweir 
413cdf0e10cSrcweir 			return aRetval;
414cdf0e10cSrcweir 		}
415cdf0e10cSrcweir 
416cdf0e10cSrcweir 		B2DPolyPolygon rotateAroundPoint(const B2DPolyPolygon& rCandidate, const B2DPoint& rCenter, double fAngle)
417cdf0e10cSrcweir 		{
418cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
419cdf0e10cSrcweir 			B2DPolyPolygon aRetval;
420cdf0e10cSrcweir 
421cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
422cdf0e10cSrcweir 			{
423cdf0e10cSrcweir 				const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
424cdf0e10cSrcweir 
425cdf0e10cSrcweir 				aRetval.append(rotateAroundPoint(aCandidate, rCenter, fAngle));
426cdf0e10cSrcweir 			}
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 			return aRetval;
429cdf0e10cSrcweir 		}
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 		B2DPolyPolygon expandToCurve(const B2DPolyPolygon& rCandidate)
432cdf0e10cSrcweir 		{
433cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidate.count());
434cdf0e10cSrcweir 			B2DPolyPolygon aRetval;
435cdf0e10cSrcweir 
436cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < nPolygonCount; a++)
437cdf0e10cSrcweir 			{
438cdf0e10cSrcweir 				const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 				aRetval.append(expandToCurve(aCandidate));
441cdf0e10cSrcweir 			}
442cdf0e10cSrcweir 
443cdf0e10cSrcweir 			return aRetval;
444cdf0e10cSrcweir 		}
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 		B2DPolyPolygon setContinuity(const B2DPolyPolygon& rCandidate, B2VectorContinuity eContinuity)
447cdf0e10cSrcweir 		{
448cdf0e10cSrcweir 			if(rCandidate.areControlPointsUsed())
449cdf0e10cSrcweir 			{
450cdf0e10cSrcweir 				const sal_uInt32 nPolygonCount(rCandidate.count());
451cdf0e10cSrcweir 				B2DPolyPolygon aRetval;
452cdf0e10cSrcweir 
453cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < nPolygonCount; a++)
454cdf0e10cSrcweir 				{
455cdf0e10cSrcweir 					const B2DPolygon aCandidate(rCandidate.getB2DPolygon(a));
456cdf0e10cSrcweir 
457cdf0e10cSrcweir 					aRetval.append(setContinuity(aCandidate, eContinuity));
458cdf0e10cSrcweir 				}
459cdf0e10cSrcweir 
460cdf0e10cSrcweir 				return aRetval;
461cdf0e10cSrcweir 			}
462cdf0e10cSrcweir 			else
463cdf0e10cSrcweir 			{
464cdf0e10cSrcweir 				return rCandidate;
465cdf0e10cSrcweir 			}
466cdf0e10cSrcweir 		}
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 		B2DPolyPolygon growInNormalDirection(const B2DPolyPolygon& rCandidate, double fValue)
469cdf0e10cSrcweir 		{
470cdf0e10cSrcweir 			if(0.0 != fValue)
471cdf0e10cSrcweir 			{
472cdf0e10cSrcweir 				B2DPolyPolygon aRetval;
473cdf0e10cSrcweir 
474cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
475cdf0e10cSrcweir 				{
476cdf0e10cSrcweir 					aRetval.append(growInNormalDirection(rCandidate.getB2DPolygon(a), fValue));
477cdf0e10cSrcweir 				}
478cdf0e10cSrcweir 
479cdf0e10cSrcweir 				return aRetval;
480cdf0e10cSrcweir 			}
481cdf0e10cSrcweir 			else
482cdf0e10cSrcweir 			{
483cdf0e10cSrcweir 				return rCandidate;
484cdf0e10cSrcweir 			}
485cdf0e10cSrcweir 		}
486cdf0e10cSrcweir 
487cdf0e10cSrcweir 		void correctGrowShrinkPolygonPair(B2DPolyPolygon& /*rOriginal*/, B2DPolyPolygon& /*rGrown*/)
488cdf0e10cSrcweir 		{
489cdf0e10cSrcweir 		}
490cdf0e10cSrcweir 
491cdf0e10cSrcweir 		B2DPolyPolygon reSegmentPolyPolygon(const B2DPolyPolygon& rCandidate, sal_uInt32 nSegments)
492cdf0e10cSrcweir 		{
493cdf0e10cSrcweir 			B2DPolyPolygon aRetval;
494cdf0e10cSrcweir 
495cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
496cdf0e10cSrcweir 			{
497cdf0e10cSrcweir 				aRetval.append(reSegmentPolygon(rCandidate.getB2DPolygon(a), nSegments));
498cdf0e10cSrcweir 			}
499cdf0e10cSrcweir 
500cdf0e10cSrcweir 			return aRetval;
501cdf0e10cSrcweir 		}
502cdf0e10cSrcweir 
503cdf0e10cSrcweir 		B2DPolyPolygon interpolate(const B2DPolyPolygon& rOld1, const B2DPolyPolygon& rOld2, double t)
504cdf0e10cSrcweir 		{
505cdf0e10cSrcweir 			OSL_ENSURE(rOld1.count() == rOld2.count(), "B2DPolyPolygon interpolate: Different geometry (!)");
506cdf0e10cSrcweir 			B2DPolyPolygon aRetval;
507cdf0e10cSrcweir 
508cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < rOld1.count(); a++)
509cdf0e10cSrcweir 			{
510cdf0e10cSrcweir 				aRetval.append(interpolate(rOld1.getB2DPolygon(a), rOld2.getB2DPolygon(a), t));
511cdf0e10cSrcweir 			}
512cdf0e10cSrcweir 
513cdf0e10cSrcweir 			return aRetval;
514cdf0e10cSrcweir 		}
515cdf0e10cSrcweir 
516cdf0e10cSrcweir         bool isRectangle( const B2DPolyPolygon& rPoly )
517cdf0e10cSrcweir         {
518cdf0e10cSrcweir             // exclude some cheap cases first
519cdf0e10cSrcweir             if( rPoly.count() != 1 )
520cdf0e10cSrcweir                 return false;
521cdf0e10cSrcweir 
522cdf0e10cSrcweir             return isRectangle( rPoly.getB2DPolygon(0) );
523cdf0e10cSrcweir         }
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 		// #i76891#
526cdf0e10cSrcweir 		B2DPolyPolygon simplifyCurveSegments(const B2DPolyPolygon& rCandidate)
527cdf0e10cSrcweir 		{
528cdf0e10cSrcweir 			if(rCandidate.areControlPointsUsed())
529cdf0e10cSrcweir 			{
530cdf0e10cSrcweir 				B2DPolyPolygon aRetval;
531cdf0e10cSrcweir 
532cdf0e10cSrcweir 				for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
533cdf0e10cSrcweir 				{
534cdf0e10cSrcweir 					aRetval.append(simplifyCurveSegments(rCandidate.getB2DPolygon(a)));
535cdf0e10cSrcweir 				}
536cdf0e10cSrcweir 
537cdf0e10cSrcweir 				return aRetval;
538cdf0e10cSrcweir 			}
539cdf0e10cSrcweir 			else
540cdf0e10cSrcweir 			{
541cdf0e10cSrcweir 				return rCandidate;
542cdf0e10cSrcweir 			}
543cdf0e10cSrcweir 		}
544cdf0e10cSrcweir 
545cdf0e10cSrcweir 		B2DPolyPolygon reSegmentPolyPolygonEdges(const B2DPolyPolygon& rCandidate, sal_uInt32 nSubEdges, bool bHandleCurvedEdges, bool bHandleStraightEdges)
546cdf0e10cSrcweir         {
547cdf0e10cSrcweir 			B2DPolyPolygon aRetval;
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
550cdf0e10cSrcweir 			{
551cdf0e10cSrcweir 				aRetval.append(reSegmentPolygonEdges(rCandidate.getB2DPolygon(a), nSubEdges, bHandleCurvedEdges, bHandleStraightEdges));
552cdf0e10cSrcweir 			}
553cdf0e10cSrcweir 
554cdf0e10cSrcweir 			return aRetval;
555cdf0e10cSrcweir         }
556cdf0e10cSrcweir 
557cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////////
558cdf0e10cSrcweir 		// comparators with tolerance for 2D PolyPolygons
559cdf0e10cSrcweir 
560cdf0e10cSrcweir 		bool equal(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB, const double& rfSmallValue)
561cdf0e10cSrcweir 		{
562cdf0e10cSrcweir 			const sal_uInt32 nPolygonCount(rCandidateA.count());
563cdf0e10cSrcweir 
564cdf0e10cSrcweir 			if(nPolygonCount != rCandidateB.count())
565cdf0e10cSrcweir 				return false;
566cdf0e10cSrcweir 
567cdf0e10cSrcweir 			for(sal_uInt32 a(0); a < nPolygonCount; a++)
568cdf0e10cSrcweir 			{
569cdf0e10cSrcweir 				const B2DPolygon aCandidate(rCandidateA.getB2DPolygon(a));
570cdf0e10cSrcweir 
571cdf0e10cSrcweir 				if(!equal(aCandidate, rCandidateB.getB2DPolygon(a), rfSmallValue))
572cdf0e10cSrcweir 					return false;
573cdf0e10cSrcweir 			}
574cdf0e10cSrcweir 
575cdf0e10cSrcweir 			return true;
576cdf0e10cSrcweir 		}
577cdf0e10cSrcweir 
578cdf0e10cSrcweir 		bool equal(const B2DPolyPolygon& rCandidateA, const B2DPolyPolygon& rCandidateB)
579cdf0e10cSrcweir 		{
580cdf0e10cSrcweir 			const double fSmallValue(fTools::getSmallValue());
581cdf0e10cSrcweir 
582cdf0e10cSrcweir 			return equal(rCandidateA, rCandidateB, fSmallValue);
583cdf0e10cSrcweir 		}
584cdf0e10cSrcweir 
585cdf0e10cSrcweir 		B2DPolyPolygon snapPointsOfHorizontalOrVerticalEdges(const B2DPolyPolygon& rCandidate)
586cdf0e10cSrcweir 		{
587cdf0e10cSrcweir 			B2DPolyPolygon aRetval;
588cdf0e10cSrcweir 
589cdf0e10cSrcweir 			for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
590cdf0e10cSrcweir 			{
591cdf0e10cSrcweir 				aRetval.append(snapPointsOfHorizontalOrVerticalEdges(rCandidate.getB2DPolygon(a)));
592cdf0e10cSrcweir 			}
593cdf0e10cSrcweir 
594cdf0e10cSrcweir 			return aRetval;
595cdf0e10cSrcweir 		}
596cdf0e10cSrcweir 
597cdf0e10cSrcweir 	} // end of namespace tools
598cdf0e10cSrcweir } // end of namespace basegfx
599cdf0e10cSrcweir 
600cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
601cdf0e10cSrcweir // eof
602