merged tag ooo/OOO330_m14
[LibreOffice.git] / basegfx / source / polygon / b2dpolygontriangulator.cxx
blob83fcc036c996fb1d67e803dc9bcd09dbd69eb6c4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basegfx.hxx"
30 #include <basegfx/polygon/b2dpolygontriangulator.hxx>
31 #include <osl/diagnose.h>
32 #include <basegfx/point/b2dpoint.hxx>
33 #include <basegfx/polygon/b2dpolygon.hxx>
34 #include <basegfx/vector/b2dvector.hxx>
35 #include <basegfx/polygon/b2dpolygontools.hxx>
36 #include <basegfx/polygon/b2dpolypolygontools.hxx>
37 #include <basegfx/range/b2drange.hxx>
38 #include <basegfx/numeric/ftools.hxx>
40 #include <algorithm>
42 //////////////////////////////////////////////////////////////////////////////
44 namespace basegfx
46 namespace
48 class EdgeEntry
50 EdgeEntry* mpNext;
51 B2DPoint maStart;
52 B2DPoint maEnd;
53 double mfAtan2;
55 public:
56 EdgeEntry(const B2DPoint& rStart, const B2DPoint& rEnd)
57 : mpNext(0L),
58 maStart(rStart),
59 maEnd(rEnd),
60 mfAtan2(0.0)
62 // make sure edge goes down. If horizontal, let it go to the right (left-handed).
63 bool bSwap(false);
65 if(::basegfx::fTools::equal(maStart.getY(), maEnd.getY()))
67 if(maStart.getX() > maEnd.getX())
69 bSwap = true;
72 else if(maStart.getY() > maEnd.getY())
74 bSwap = true;
77 if(bSwap)
79 maStart = rEnd;
80 maEnd = rStart;
83 mfAtan2 = atan2(maEnd.getY() - maStart.getY(), maEnd.getX() - maStart.getX());
86 ~EdgeEntry()
90 bool operator<(const EdgeEntry& rComp) const
92 if(::basegfx::fTools::equal(maStart.getY(), rComp.maStart.getY()))
94 if(::basegfx::fTools::equal(maStart.getX(), rComp.maStart.getX()))
96 // same in x and y -> same start point. Sort emitting vectors from left to right.
97 return (mfAtan2 > rComp.mfAtan2);
100 return (maStart.getX() < rComp.maStart.getX());
103 return (maStart.getY() < rComp.maStart.getY());
106 bool operator==(const EdgeEntry& rComp) const
108 return (maStart.equal(rComp.maStart) && maEnd.equal(rComp.maEnd));
111 bool operator!=(const EdgeEntry& rComp) const
113 return !(*this == rComp);
116 const B2DPoint& getStart() const { return maStart; }
117 const B2DPoint& getEnd() const { return maEnd; }
119 EdgeEntry* getNext() const { return mpNext; }
120 void setNext(EdgeEntry* pNext) { mpNext = pNext; }
123 //////////////////////////////////////////////////////////////////////////////
125 typedef ::std::vector< EdgeEntry > EdgeEntries;
126 typedef ::std::vector< EdgeEntry* > EdgeEntryPointers;
128 //////////////////////////////////////////////////////////////////////////////
130 class Triangulator
132 EdgeEntry* mpList;
133 EdgeEntries maStartEntries;
134 EdgeEntryPointers maNewEdgeEntries;
135 B2DPolygon maResult;
137 void handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd);
138 bool CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry* pEdgeB, const B2DPoint& rTestPoint);
139 void createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC);
141 public:
142 Triangulator(const B2DPolyPolygon& rCandidate);
143 ~Triangulator();
145 const B2DPolygon getResult() const { return maResult; }
148 void Triangulator::handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd)
150 // create an entry, else the comparison might use the wrong edges
151 EdgeEntry aNew(rStart, rEnd);
152 EdgeEntry* pCurr = mpList;
153 EdgeEntry* pPrev = 0L;
155 while(pCurr
156 && pCurr->getStart().getY() <= aNew.getStart().getY()
157 && *pCurr != aNew)
159 pPrev = pCurr;
160 pCurr = pCurr->getNext();
163 if(pCurr && *pCurr == aNew)
165 // found closing edge, remove
166 if(pPrev)
168 pPrev->setNext(pCurr->getNext());
170 else
172 mpList = pCurr->getNext();
175 else
177 // insert closing edge
178 EdgeEntry* pNew = new EdgeEntry(aNew);
179 maNewEdgeEntries.push_back(pNew);
180 pCurr = mpList;
181 pPrev = 0L;
183 while(pCurr && *pCurr < *pNew)
185 pPrev = pCurr;
186 pCurr = pCurr->getNext();
189 if(pPrev)
191 pNew->setNext(pPrev->getNext());
192 pPrev->setNext(pNew);
194 else
196 pNew->setNext(mpList);
197 mpList = pNew;
202 bool Triangulator::CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry* pEdgeB, const B2DPoint& rTestPoint)
204 // inside triangle or on edge?
205 if(tools::isPointInTriangle(pEdgeA->getStart(), pEdgeA->getEnd(), pEdgeB->getEnd(), rTestPoint, true))
207 // but not on point
208 if(!rTestPoint.equal(pEdgeA->getEnd()) && !rTestPoint.equal(pEdgeB->getEnd()))
210 // found point in triangle -> split triangle inserting two edges
211 EdgeEntry* pStart = new EdgeEntry(pEdgeA->getStart(), rTestPoint);
212 EdgeEntry* pEnd = new EdgeEntry(*pStart);
213 maNewEdgeEntries.push_back(pStart);
214 maNewEdgeEntries.push_back(pEnd);
216 pStart->setNext(pEnd);
217 pEnd->setNext(pEdgeA->getNext());
218 pEdgeA->setNext(pStart);
220 return false;
224 return true;
227 void Triangulator::createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC)
229 maResult.append(rA);
230 maResult.append(rB);
231 maResult.append(rC);
234 // consume as long as there are edges
235 Triangulator::Triangulator(const B2DPolyPolygon& rCandidate)
236 : mpList(0L)
238 // add all available edges to the single linked local list which will be sorted
239 // by Y,X,atan2 when adding nodes
240 if(rCandidate.count())
242 for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
244 const B2DPolygon aPolygonCandidate(rCandidate.getB2DPolygon(a));
245 const sal_uInt32 nCount(aPolygonCandidate.count());
247 if(nCount > 2L)
249 B2DPoint aPrevPnt(aPolygonCandidate.getB2DPoint(nCount - 1L));
251 for(sal_uInt32 b(0L); b < nCount; b++)
253 B2DPoint aNextPnt(aPolygonCandidate.getB2DPoint(b));
255 if( !aPrevPnt.equal(aNextPnt) )
257 maStartEntries.push_back(EdgeEntry(aPrevPnt, aNextPnt));
260 aPrevPnt = aNextPnt;
265 if(maStartEntries.size())
267 // sort initial list
268 ::std::sort(maStartEntries.begin(), maStartEntries.end());
270 // insert to own simply linked list
271 EdgeEntries::iterator aPos(maStartEntries.begin());
272 mpList = &(*aPos++);
273 EdgeEntry* pLast = mpList;
275 while(aPos != maStartEntries.end())
277 EdgeEntry* pEntry = &(*aPos++);
278 pLast->setNext(pEntry);
279 pLast = pEntry;
284 while(mpList)
286 if(mpList->getNext() && mpList->getNext()->getStart().equal(mpList->getStart()))
288 // next candidate. There are two edges and start point is equal.
289 // Length is not zero.
290 EdgeEntry* pEdgeA = mpList;
291 EdgeEntry* pEdgeB = pEdgeA->getNext();
293 if( pEdgeA->getEnd().equal(pEdgeB->getEnd()) )
295 // start and end equal -> neutral triangle, delete both
296 mpList = pEdgeB->getNext();
298 else
300 const B2DVector aLeft(pEdgeA->getEnd() - pEdgeA->getStart());
301 const B2DVector aRight(pEdgeB->getEnd() - pEdgeA->getStart());
303 if(ORIENTATION_NEUTRAL == getOrientation(aLeft, aRight))
305 // edges are parallel and have different length -> neutral triangle,
306 // delete both edges and handle closing edge
307 mpList = pEdgeB->getNext();
308 handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
310 else
312 // not parallel, look for points inside
313 B2DRange aRange(pEdgeA->getStart(), pEdgeA->getEnd());
314 aRange.expand(pEdgeB->getEnd());
315 EdgeEntry* pTestEdge = pEdgeB->getNext();
316 bool bNoPointInTriangle(true);
318 // look for start point in triangle
319 while(bNoPointInTriangle && pTestEdge)
321 if(aRange.getMaxY() < pTestEdge->getStart().getY())
323 // edge is below test range and edges are sorted -> stop looking
324 break;
326 else
328 // do not look for edges with same start point, they are sorted and cannot end inside.
329 if(!pTestEdge->getStart().equal(pEdgeA->getStart()))
331 if(aRange.isInside(pTestEdge->getStart()))
333 bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getStart());
338 // next candidate
339 pTestEdge = pTestEdge->getNext();
342 if(bNoPointInTriangle)
344 // look for end point in triange
345 pTestEdge = pEdgeB->getNext();
347 while(bNoPointInTriangle && pTestEdge)
349 if(aRange.getMaxY() < pTestEdge->getStart().getY())
351 // edge is below test range and edges are sorted -> stop looking
352 break;
354 else
356 // do not look for edges with same end point, they are sorted and cannot end inside.
357 if(!pTestEdge->getEnd().equal(pEdgeA->getStart()))
359 if(aRange.isInside(pTestEdge->getEnd()))
361 bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getEnd());
366 // next candidate
367 pTestEdge = pTestEdge->getNext();
371 if(bNoPointInTriangle)
373 // create triangle, remove edges, handle closing edge
374 mpList = pEdgeB->getNext();
375 createTriangle(pEdgeA->getStart(), pEdgeB->getEnd(), pEdgeA->getEnd());
376 handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
381 else
383 // only one entry at start point, delete it
384 mpList = mpList->getNext();
389 Triangulator::~Triangulator()
391 EdgeEntryPointers::iterator aIter(maNewEdgeEntries.begin());
393 while(aIter != maNewEdgeEntries.end())
395 delete (*aIter++);
399 } // end of anonymous namespace
400 } // end of namespace basegfx
402 //////////////////////////////////////////////////////////////////////////////
404 namespace basegfx
406 namespace triangulator
408 B2DPolygon triangulate(const B2DPolygon& rCandidate)
410 B2DPolygon aRetval;
412 // subdivide locally (triangulate does not work with beziers), remove double and neutral points
413 B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
414 aCandidate.removeDoublePoints();
415 aCandidate = tools::removeNeutralPoints(aCandidate);
417 if(2L == aCandidate.count())
419 // candidate IS a triangle, just append
420 aRetval.append(aCandidate);
422 else if(aCandidate.count() > 2L)
424 if(tools::isConvex(aCandidate))
426 // polygon is convex, just use a triangle fan
427 tools::addTriangleFan(aCandidate, aRetval);
429 else
431 // polygon is concave.
432 const B2DPolyPolygon aCandPolyPoly(aCandidate);
433 Triangulator aTriangulator(aCandPolyPoly);
434 aRetval = aTriangulator.getResult();
438 return aRetval;
441 B2DPolygon triangulate(const B2DPolyPolygon& rCandidate)
443 B2DPolygon aRetval;
445 // subdivide locally (triangulate does not work with beziers)
446 B2DPolyPolygon aCandidate(rCandidate.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
448 if(1L == aCandidate.count())
450 // single polygon -> single polygon triangulation
451 const B2DPolygon aSinglePolygon(aCandidate.getB2DPolygon(0L));
452 aRetval = triangulate(aSinglePolygon);
454 else
456 Triangulator aTriangulator(aCandidate);
457 aRetval = aTriangulator.getResult();
460 return aRetval;
462 } // end of namespace triangulator
463 } // end of namespace basegfx
465 //////////////////////////////////////////////////////////////////////////////
466 // eof