update dev300-m58
[ooovba.git] / basegfx / source / polygon / b2dpolygontriangulator.cxx
blobe4d0fbae989ad1d25203d7406becd295b4c0fc71
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: b2dpolygontriangulator.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basegfx.hxx"
33 #include <basegfx/polygon/b2dpolygontriangulator.hxx>
34 #include <osl/diagnose.h>
35 #include <basegfx/point/b2dpoint.hxx>
36 #include <basegfx/polygon/b2dpolygon.hxx>
37 #include <basegfx/vector/b2dvector.hxx>
38 #include <basegfx/polygon/b2dpolygontools.hxx>
39 #include <basegfx/polygon/b2dpolypolygontools.hxx>
40 #include <basegfx/range/b2drange.hxx>
41 #include <basegfx/numeric/ftools.hxx>
43 #include <algorithm>
45 //////////////////////////////////////////////////////////////////////////////
47 namespace basegfx
49 namespace
51 class EdgeEntry
53 EdgeEntry* mpNext;
54 B2DPoint maStart;
55 B2DPoint maEnd;
56 double mfAtan2;
58 public:
59 EdgeEntry(const B2DPoint& rStart, const B2DPoint& rEnd)
60 : mpNext(0L),
61 maStart(rStart),
62 maEnd(rEnd),
63 mfAtan2(0.0)
65 // make sure edge goes down. If horizontal, let it go to the right (left-handed).
66 bool bSwap(false);
68 if(::basegfx::fTools::equal(maStart.getY(), maEnd.getY()))
70 if(maStart.getX() > maEnd.getX())
72 bSwap = true;
75 else if(maStart.getY() > maEnd.getY())
77 bSwap = true;
80 if(bSwap)
82 maStart = rEnd;
83 maEnd = rStart;
86 mfAtan2 = atan2(maEnd.getY() - maStart.getY(), maEnd.getX() - maStart.getX());
89 ~EdgeEntry()
93 bool operator<(const EdgeEntry& rComp) const
95 if(::basegfx::fTools::equal(maStart.getY(), rComp.maStart.getY()))
97 if(::basegfx::fTools::equal(maStart.getX(), rComp.maStart.getX()))
99 // same in x and y -> same start point. Sort emitting vectors from left to right.
100 return (mfAtan2 > rComp.mfAtan2);
103 return (maStart.getX() < rComp.maStart.getX());
106 return (maStart.getY() < rComp.maStart.getY());
109 bool operator==(const EdgeEntry& rComp) const
111 return (maStart.equal(rComp.maStart) && maEnd.equal(rComp.maEnd));
114 bool operator!=(const EdgeEntry& rComp) const
116 return !(*this == rComp);
119 const B2DPoint& getStart() const { return maStart; }
120 const B2DPoint& getEnd() const { return maEnd; }
122 EdgeEntry* getNext() const { return mpNext; }
123 void setNext(EdgeEntry* pNext) { mpNext = pNext; }
126 //////////////////////////////////////////////////////////////////////////////
128 typedef ::std::vector< EdgeEntry > EdgeEntries;
129 typedef ::std::vector< EdgeEntry* > EdgeEntryPointers;
131 //////////////////////////////////////////////////////////////////////////////
133 class Triangulator
135 EdgeEntry* mpList;
136 EdgeEntries maStartEntries;
137 EdgeEntryPointers maNewEdgeEntries;
138 B2DPolygon maResult;
140 void handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd);
141 bool CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry* pEdgeB, const B2DPoint& rTestPoint);
142 void createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC);
144 public:
145 Triangulator(const B2DPolyPolygon& rCandidate);
146 ~Triangulator();
148 const B2DPolygon getResult() const { return maResult; }
151 void Triangulator::handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd)
153 // create an entry, else the comparison might use the wrong edges
154 EdgeEntry aNew(rStart, rEnd);
155 EdgeEntry* pCurr = mpList;
156 EdgeEntry* pPrev = 0L;
158 while(pCurr
159 && pCurr->getStart().getY() <= aNew.getStart().getY()
160 && *pCurr != aNew)
162 pPrev = pCurr;
163 pCurr = pCurr->getNext();
166 if(pCurr && *pCurr == aNew)
168 // found closing edge, remove
169 if(pPrev)
171 pPrev->setNext(pCurr->getNext());
173 else
175 mpList = pCurr->getNext();
178 else
180 // insert closing edge
181 EdgeEntry* pNew = new EdgeEntry(aNew);
182 maNewEdgeEntries.push_back(pNew);
183 pCurr = mpList;
184 pPrev = 0L;
186 while(pCurr && *pCurr < *pNew)
188 pPrev = pCurr;
189 pCurr = pCurr->getNext();
192 if(pPrev)
194 pNew->setNext(pPrev->getNext());
195 pPrev->setNext(pNew);
197 else
199 pNew->setNext(mpList);
200 mpList = pNew;
205 bool Triangulator::CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry* pEdgeB, const B2DPoint& rTestPoint)
207 // inside triangle or on edge?
208 if(tools::isPointInTriangle(pEdgeA->getStart(), pEdgeA->getEnd(), pEdgeB->getEnd(), rTestPoint, true))
210 // but not on point
211 if(!rTestPoint.equal(pEdgeA->getEnd()) && !rTestPoint.equal(pEdgeB->getEnd()))
213 // found point in triangle -> split triangle inserting two edges
214 EdgeEntry* pStart = new EdgeEntry(pEdgeA->getStart(), rTestPoint);
215 EdgeEntry* pEnd = new EdgeEntry(*pStart);
216 maNewEdgeEntries.push_back(pStart);
217 maNewEdgeEntries.push_back(pEnd);
219 pStart->setNext(pEnd);
220 pEnd->setNext(pEdgeA->getNext());
221 pEdgeA->setNext(pStart);
223 return false;
227 return true;
230 void Triangulator::createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC)
232 maResult.append(rA);
233 maResult.append(rB);
234 maResult.append(rC);
237 // consume as long as there are edges
238 Triangulator::Triangulator(const B2DPolyPolygon& rCandidate)
239 : mpList(0L)
241 // add all available edges to the single linked local list which will be sorted
242 // by Y,X,atan2 when adding nodes
243 if(rCandidate.count())
245 for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
247 const B2DPolygon aPolygonCandidate(rCandidate.getB2DPolygon(a));
248 const sal_uInt32 nCount(aPolygonCandidate.count());
250 if(nCount > 2L)
252 B2DPoint aPrevPnt(aPolygonCandidate.getB2DPoint(nCount - 1L));
254 for(sal_uInt32 b(0L); b < nCount; b++)
256 B2DPoint aNextPnt(aPolygonCandidate.getB2DPoint(b));
258 if( !aPrevPnt.equal(aNextPnt) )
260 maStartEntries.push_back(EdgeEntry(aPrevPnt, aNextPnt));
263 aPrevPnt = aNextPnt;
268 if(maStartEntries.size())
270 // sort initial list
271 ::std::sort(maStartEntries.begin(), maStartEntries.end());
273 // insert to own simply linked list
274 EdgeEntries::iterator aPos(maStartEntries.begin());
275 mpList = &(*aPos++);
276 EdgeEntry* pLast = mpList;
278 while(aPos != maStartEntries.end())
280 EdgeEntry* pEntry = &(*aPos++);
281 pLast->setNext(pEntry);
282 pLast = pEntry;
287 while(mpList)
289 if(mpList->getNext() && mpList->getNext()->getStart().equal(mpList->getStart()))
291 // next candidate. There are two edges and start point is equal.
292 // Length is not zero.
293 EdgeEntry* pEdgeA = mpList;
294 EdgeEntry* pEdgeB = pEdgeA->getNext();
296 if( pEdgeA->getEnd().equal(pEdgeB->getEnd()) )
298 // start and end equal -> neutral triangle, delete both
299 mpList = pEdgeB->getNext();
301 else
303 const B2DVector aLeft(pEdgeA->getEnd() - pEdgeA->getStart());
304 const B2DVector aRight(pEdgeB->getEnd() - pEdgeA->getStart());
306 if(ORIENTATION_NEUTRAL == getOrientation(aLeft, aRight))
308 // edges are parallel and have different length -> neutral triangle,
309 // delete both edges and handle closing edge
310 mpList = pEdgeB->getNext();
311 handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
313 else
315 // not parallel, look for points inside
316 B2DRange aRange(pEdgeA->getStart(), pEdgeA->getEnd());
317 aRange.expand(pEdgeB->getEnd());
318 EdgeEntry* pTestEdge = pEdgeB->getNext();
319 bool bNoPointInTriangle(true);
321 // look for start point in triangle
322 while(bNoPointInTriangle && pTestEdge)
324 if(aRange.getMaxY() < pTestEdge->getStart().getY())
326 // edge is below test range and edges are sorted -> stop looking
327 break;
329 else
331 // do not look for edges with same start point, they are sorted and cannot end inside.
332 if(!pTestEdge->getStart().equal(pEdgeA->getStart()))
334 if(aRange.isInside(pTestEdge->getStart()))
336 bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getStart());
341 // next candidate
342 pTestEdge = pTestEdge->getNext();
345 if(bNoPointInTriangle)
347 // look for end point in triange
348 pTestEdge = pEdgeB->getNext();
350 while(bNoPointInTriangle && pTestEdge)
352 if(aRange.getMaxY() < pTestEdge->getStart().getY())
354 // edge is below test range and edges are sorted -> stop looking
355 break;
357 else
359 // do not look for edges with same end point, they are sorted and cannot end inside.
360 if(!pTestEdge->getEnd().equal(pEdgeA->getStart()))
362 if(aRange.isInside(pTestEdge->getEnd()))
364 bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getEnd());
369 // next candidate
370 pTestEdge = pTestEdge->getNext();
374 if(bNoPointInTriangle)
376 // create triangle, remove edges, handle closing edge
377 mpList = pEdgeB->getNext();
378 createTriangle(pEdgeA->getStart(), pEdgeB->getEnd(), pEdgeA->getEnd());
379 handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
384 else
386 // only one entry at start point, delete it
387 mpList = mpList->getNext();
392 Triangulator::~Triangulator()
394 EdgeEntryPointers::iterator aIter(maNewEdgeEntries.begin());
396 while(aIter != maNewEdgeEntries.end())
398 delete (*aIter++);
402 } // end of anonymous namespace
403 } // end of namespace basegfx
405 //////////////////////////////////////////////////////////////////////////////
407 namespace basegfx
409 namespace triangulator
411 B2DPolygon triangulate(const B2DPolygon& rCandidate)
413 B2DPolygon aRetval;
415 // subdivide locally (triangulate does not work with beziers), remove double and neutral points
416 B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
417 aCandidate.removeDoublePoints();
418 aCandidate = tools::removeNeutralPoints(aCandidate);
420 if(2L == aCandidate.count())
422 // candidate IS a triangle, just append
423 aRetval.append(aCandidate);
425 else if(aCandidate.count() > 2L)
427 if(tools::isConvex(aCandidate))
429 // polygon is convex, just use a triangle fan
430 tools::addTriangleFan(aCandidate, aRetval);
432 else
434 // polygon is concave.
435 const B2DPolyPolygon aCandPolyPoly(aCandidate);
436 Triangulator aTriangulator(aCandPolyPoly);
437 aRetval = aTriangulator.getResult();
441 return aRetval;
444 B2DPolygon triangulate(const B2DPolyPolygon& rCandidate)
446 B2DPolygon aRetval;
448 // subdivide locally (triangulate does not work with beziers)
449 B2DPolyPolygon aCandidate(rCandidate.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
451 if(1L == aCandidate.count())
453 // single polygon -> single polygon triangulation
454 const B2DPolygon aSinglePolygon(aCandidate.getB2DPolygon(0L));
455 aRetval = triangulate(aSinglePolygon);
457 else
459 Triangulator aTriangulator(aCandidate);
460 aRetval = aTriangulator.getResult();
463 return aRetval;
465 } // end of namespace triangulator
466 } // end of namespace basegfx
468 //////////////////////////////////////////////////////////////////////////////
469 // eof