bump product version to 5.0.4.1
[LibreOffice.git] / basegfx / source / polygon / b2dpolygontriangulator.cxx
blob0f02913ffeb551ff265b6d7d43cd24fc02a82518
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <basegfx/polygon/b2dpolygontriangulator.hxx>
21 #include <osl/diagnose.h>
22 #include <basegfx/point/b2dpoint.hxx>
23 #include <basegfx/polygon/b2dpolygon.hxx>
24 #include <basegfx/vector/b2dvector.hxx>
25 #include <basegfx/polygon/b2dpolygontools.hxx>
26 #include <basegfx/polygon/b2dpolypolygontools.hxx>
27 #include <basegfx/range/b2drange.hxx>
28 #include <basegfx/numeric/ftools.hxx>
30 #include <algorithm>
32 namespace basegfx
34 namespace
36 class EdgeEntry
38 EdgeEntry* mpNext;
39 B2DPoint maStart;
40 B2DPoint maEnd;
41 double mfAtan2;
43 public:
44 EdgeEntry(const B2DPoint& rStart, const B2DPoint& rEnd)
45 : mpNext(0L),
46 maStart(rStart),
47 maEnd(rEnd),
48 mfAtan2(0.0)
50 // make sure edge goes down. If horizontal, let it go to the right (left-handed).
51 bool bSwap(false);
53 if(::basegfx::fTools::equal(maStart.getY(), maEnd.getY()))
55 if(maStart.getX() > maEnd.getX())
57 bSwap = true;
60 else if(maStart.getY() > maEnd.getY())
62 bSwap = true;
65 if(bSwap)
67 maStart = rEnd;
68 maEnd = rStart;
71 mfAtan2 = atan2(maEnd.getY() - maStart.getY(), maEnd.getX() - maStart.getX());
74 ~EdgeEntry()
78 bool operator<(const EdgeEntry& rComp) const
80 if(::basegfx::fTools::equal(maStart.getY(), rComp.maStart.getY()))
82 if(::basegfx::fTools::equal(maStart.getX(), rComp.maStart.getX()))
84 // same in x and y -> same start point. Sort emitting vectors from left to right.
85 return (mfAtan2 > rComp.mfAtan2);
88 return (maStart.getX() < rComp.maStart.getX());
91 return (maStart.getY() < rComp.maStart.getY());
94 bool operator==(const EdgeEntry& rComp) const
96 return (maStart.equal(rComp.maStart) && maEnd.equal(rComp.maEnd));
99 bool operator!=(const EdgeEntry& rComp) const
101 return !(*this == rComp);
104 const B2DPoint& getStart() const { return maStart; }
105 const B2DPoint& getEnd() const { return maEnd; }
107 EdgeEntry* getNext() const { return mpNext; }
108 void setNext(EdgeEntry* pNext) { mpNext = pNext; }
111 typedef ::std::vector< EdgeEntry > EdgeEntries;
112 typedef ::std::vector< EdgeEntry* > EdgeEntryPointers;
114 class Triangulator
116 EdgeEntry* mpList;
117 EdgeEntries maStartEntries;
118 EdgeEntryPointers maNewEdgeEntries;
119 B2DPolygon maResult;
121 void handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd);
122 bool CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry* pEdgeB, const B2DPoint& rTestPoint);
123 void createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC);
125 public:
126 explicit Triangulator(const B2DPolyPolygon& rCandidate);
127 ~Triangulator();
129 const B2DPolygon getResult() const { return maResult; }
132 void Triangulator::handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd)
134 // create an entry, else the comparison might use the wrong edges
135 EdgeEntry aNew(rStart, rEnd);
136 EdgeEntry* pCurr = mpList;
137 EdgeEntry* pPrev = 0L;
139 while(pCurr
140 && pCurr->getStart().getY() <= aNew.getStart().getY()
141 && *pCurr != aNew)
143 pPrev = pCurr;
144 pCurr = pCurr->getNext();
147 if(pCurr && *pCurr == aNew)
149 // found closing edge, remove
150 if(pPrev)
152 pPrev->setNext(pCurr->getNext());
154 else
156 mpList = pCurr->getNext();
159 else
161 // insert closing edge
162 EdgeEntry* pNew = new EdgeEntry(aNew);
163 maNewEdgeEntries.push_back(pNew);
164 pCurr = mpList;
165 pPrev = 0L;
167 while(pCurr && *pCurr < *pNew)
169 pPrev = pCurr;
170 pCurr = pCurr->getNext();
173 if(pPrev)
175 pNew->setNext(pPrev->getNext());
176 pPrev->setNext(pNew);
178 else
180 pNew->setNext(mpList);
181 mpList = pNew;
186 bool Triangulator::CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry* pEdgeB, const B2DPoint& rTestPoint)
188 // inside triangle or on edge?
189 if(tools::isPointInTriangle(pEdgeA->getStart(), pEdgeA->getEnd(), pEdgeB->getEnd(), rTestPoint, true))
191 // but not on point
192 if(!rTestPoint.equal(pEdgeA->getEnd()) && !rTestPoint.equal(pEdgeB->getEnd()))
194 // found point in triangle -> split triangle inserting two edges
195 EdgeEntry* pStart = new EdgeEntry(pEdgeA->getStart(), rTestPoint);
196 EdgeEntry* pEnd = new EdgeEntry(*pStart);
197 maNewEdgeEntries.push_back(pStart);
198 maNewEdgeEntries.push_back(pEnd);
200 pStart->setNext(pEnd);
201 pEnd->setNext(pEdgeA->getNext());
202 pEdgeA->setNext(pStart);
204 return false;
208 return true;
211 void Triangulator::createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC)
213 maResult.append(rA);
214 maResult.append(rB);
215 maResult.append(rC);
218 // consume as long as there are edges
219 Triangulator::Triangulator(const B2DPolyPolygon& rCandidate)
220 : mpList(0L)
222 // add all available edges to the single linked local list which will be sorted
223 // by Y,X,atan2 when adding nodes
224 if(rCandidate.count())
226 for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
228 const B2DPolygon aPolygonCandidate(rCandidate.getB2DPolygon(a));
229 const sal_uInt32 nCount(aPolygonCandidate.count());
231 if(nCount > 2L)
233 B2DPoint aPrevPnt(aPolygonCandidate.getB2DPoint(nCount - 1L));
235 for(sal_uInt32 b(0L); b < nCount; b++)
237 B2DPoint aNextPnt(aPolygonCandidate.getB2DPoint(b));
239 if( !aPrevPnt.equal(aNextPnt) )
241 maStartEntries.push_back(EdgeEntry(aPrevPnt, aNextPnt));
244 aPrevPnt = aNextPnt;
249 if(!maStartEntries.empty())
251 // sort initial list
252 ::std::sort(maStartEntries.begin(), maStartEntries.end());
254 // insert to own simply linked list
255 EdgeEntries::iterator aPos(maStartEntries.begin());
256 mpList = &(*aPos++);
257 EdgeEntry* pLast = mpList;
259 while(aPos != maStartEntries.end())
261 EdgeEntry* pEntry = &(*aPos++);
262 pLast->setNext(pEntry);
263 pLast = pEntry;
268 while(mpList)
270 if(mpList->getNext() && mpList->getNext()->getStart().equal(mpList->getStart()))
272 // next candidate. There are two edges and start point is equal.
273 // Length is not zero.
274 EdgeEntry* pEdgeA = mpList;
275 EdgeEntry* pEdgeB = pEdgeA->getNext();
277 if( pEdgeA->getEnd().equal(pEdgeB->getEnd()) )
279 // start and end equal -> neutral triangle, delete both
280 mpList = pEdgeB->getNext();
282 else
284 const B2DVector aLeft(pEdgeA->getEnd() - pEdgeA->getStart());
285 const B2DVector aRight(pEdgeB->getEnd() - pEdgeA->getStart());
287 if(ORIENTATION_NEUTRAL == getOrientation(aLeft, aRight))
289 // edges are parallel and have different length -> neutral triangle,
290 // delete both edges and handle closing edge
291 mpList = pEdgeB->getNext();
292 handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
294 else
296 // not parallel, look for points inside
297 B2DRange aRange(pEdgeA->getStart(), pEdgeA->getEnd());
298 aRange.expand(pEdgeB->getEnd());
299 EdgeEntry* pTestEdge = pEdgeB->getNext();
300 bool bNoPointInTriangle(true);
302 // look for start point in triangle
303 while(bNoPointInTriangle && pTestEdge)
305 if(aRange.getMaxY() < pTestEdge->getStart().getY())
307 // edge is below test range and edges are sorted -> stop looking
308 break;
310 else
312 // do not look for edges with same start point, they are sorted and cannot end inside.
313 if(!pTestEdge->getStart().equal(pEdgeA->getStart()))
315 if(aRange.isInside(pTestEdge->getStart()))
317 bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getStart());
322 // next candidate
323 pTestEdge = pTestEdge->getNext();
326 if(bNoPointInTriangle)
328 // look for end point in triange
329 pTestEdge = pEdgeB->getNext();
331 while(bNoPointInTriangle && pTestEdge)
333 if(aRange.getMaxY() < pTestEdge->getStart().getY())
335 // edge is below test range and edges are sorted -> stop looking
336 break;
338 else
340 // do not look for edges with same end point, they are sorted and cannot end inside.
341 if(!pTestEdge->getEnd().equal(pEdgeA->getStart()))
343 if(aRange.isInside(pTestEdge->getEnd()))
345 bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getEnd());
350 // next candidate
351 pTestEdge = pTestEdge->getNext();
355 if(bNoPointInTriangle)
357 // create triangle, remove edges, handle closing edge
358 mpList = pEdgeB->getNext();
359 createTriangle(pEdgeA->getStart(), pEdgeB->getEnd(), pEdgeA->getEnd());
360 handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
365 else
367 // only one entry at start point, delete it
368 mpList = mpList->getNext();
373 Triangulator::~Triangulator()
375 EdgeEntryPointers::iterator aIter(maNewEdgeEntries.begin());
377 while(aIter != maNewEdgeEntries.end())
379 delete (*aIter++);
383 } // end of anonymous namespace
384 } // end of namespace basegfx
386 namespace basegfx
388 namespace triangulator
390 B2DPolygon triangulate(const B2DPolygon& rCandidate)
392 B2DPolygon aRetval;
394 // subdivide locally (triangulate does not work with beziers), remove double and neutral points
395 B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
396 aCandidate.removeDoublePoints();
397 aCandidate = tools::removeNeutralPoints(aCandidate);
399 if(2L == aCandidate.count())
401 // candidate IS a triangle, just append
402 aRetval.append(aCandidate);
404 else if(aCandidate.count() > 2L)
406 if(tools::isConvex(aCandidate))
408 // polygon is convex, just use a triangle fan
409 tools::addTriangleFan(aCandidate, aRetval);
411 else
413 // polygon is concave.
414 const B2DPolyPolygon aCandPolyPoly(aCandidate);
415 Triangulator aTriangulator(aCandPolyPoly);
416 aRetval = aTriangulator.getResult();
420 return aRetval;
423 B2DPolygon triangulate(const B2DPolyPolygon& rCandidate)
425 B2DPolygon aRetval;
427 // subdivide locally (triangulate does not work with beziers)
428 B2DPolyPolygon aCandidate(rCandidate.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
430 if(1L == aCandidate.count())
432 // single polygon -> single polygon triangulation
433 const B2DPolygon aSinglePolygon(aCandidate.getB2DPolygon(0L));
434 aRetval = triangulate(aSinglePolygon);
436 else
438 Triangulator aTriangulator(aCandidate);
439 aRetval = aTriangulator.getResult();
442 return aRetval;
444 } // end of namespace triangulator
445 } // end of namespace basegfx
447 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */