Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / basegfx / source / polygon / b2dpolygontriangulator.cxx
blobab97419144d4f63dd9fa44dc349f69311bf652a2
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 <basegfx/point/b2dpoint.hxx>
22 #include <basegfx/polygon/b2dpolygon.hxx>
23 #include <basegfx/vector/b2dvector.hxx>
24 #include <basegfx/polygon/b2dpolygontools.hxx>
25 #include <basegfx/polygon/b2dpolypolygontools.hxx>
26 #include <basegfx/range/b2drange.hxx>
27 #include <basegfx/numeric/ftools.hxx>
29 #include <algorithm>
31 namespace basegfx
33 namespace
35 class EdgeEntry
37 EdgeEntry* mpNext;
38 B2DPoint maStart;
39 B2DPoint maEnd;
40 double mfAtan2;
42 public:
43 EdgeEntry(const B2DPoint& rStart, const B2DPoint& rEnd)
44 : mpNext(nullptr),
45 maStart(rStart),
46 maEnd(rEnd),
47 mfAtan2(0.0)
49 // make sure edge goes down. If horizontal, let it go to the right (left-handed).
50 bool bSwap(false);
52 if(::basegfx::fTools::equal(maStart.getY(), maEnd.getY()))
54 if(maStart.getX() > maEnd.getX())
56 bSwap = true;
59 else if(maStart.getY() > maEnd.getY())
61 bSwap = true;
64 if(bSwap)
66 maStart = rEnd;
67 maEnd = rStart;
70 mfAtan2 = atan2(maEnd.getY() - maStart.getY(), maEnd.getX() - maStart.getX());
73 bool operator<(const EdgeEntry& rComp) const
75 if(::basegfx::fTools::equal(maStart.getY(), rComp.maStart.getY()))
77 if(::basegfx::fTools::equal(maStart.getX(), rComp.maStart.getX()))
79 // same in x and y -> same start point. Sort emitting vectors from left to right.
80 return (mfAtan2 > rComp.mfAtan2);
83 return (maStart.getX() < rComp.maStart.getX());
86 return (maStart.getY() < rComp.maStart.getY());
89 bool operator==(const EdgeEntry& rComp) const
91 return (maStart.equal(rComp.maStart) && maEnd.equal(rComp.maEnd));
94 bool operator!=(const EdgeEntry& rComp) const
96 return !(*this == rComp);
99 const B2DPoint& getStart() const { return maStart; }
100 const B2DPoint& getEnd() const { return maEnd; }
102 EdgeEntry* getNext() const { return mpNext; }
103 void setNext(EdgeEntry* pNext) { mpNext = pNext; }
106 typedef std::vector< EdgeEntry > EdgeEntries;
108 class Triangulator
110 EdgeEntry* mpList;
111 EdgeEntries maStartEntries;
112 std::vector< std::unique_ptr<EdgeEntry> > maNewEdgeEntries;
113 B2DPolygon maResult;
115 void handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd);
116 bool CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry const * pEdgeB, const B2DPoint& rTestPoint);
117 void createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC);
119 public:
120 explicit Triangulator(const B2DPolyPolygon& rCandidate);
122 const B2DPolygon& getResult() const { return maResult; }
125 void Triangulator::handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd)
127 // create an entry, else the comparison might use the wrong edges
128 EdgeEntry aNew(rStart, rEnd);
129 EdgeEntry* pCurr = mpList;
130 EdgeEntry* pPrev = nullptr;
132 while(pCurr
133 && pCurr->getStart().getY() <= aNew.getStart().getY()
134 && *pCurr != aNew)
136 pPrev = pCurr;
137 pCurr = pCurr->getNext();
140 if(pCurr && *pCurr == aNew)
142 // found closing edge, remove
143 if(pPrev)
145 pPrev->setNext(pCurr->getNext());
147 else
149 mpList = pCurr->getNext();
152 else
154 // insert closing edge
155 EdgeEntry* pNew = new EdgeEntry(aNew);
156 maNewEdgeEntries.emplace_back(pNew);
157 pCurr = mpList;
158 pPrev = nullptr;
160 while(pCurr && *pCurr < *pNew)
162 pPrev = pCurr;
163 pCurr = pCurr->getNext();
166 if(pPrev)
168 pNew->setNext(pPrev->getNext());
169 pPrev->setNext(pNew);
171 else
173 pNew->setNext(mpList);
174 mpList = pNew;
179 bool Triangulator::CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry const * pEdgeB, const B2DPoint& rTestPoint)
181 // inside triangle or on edge?
182 if(utils::isPointInTriangle(pEdgeA->getStart(), pEdgeA->getEnd(), pEdgeB->getEnd(), rTestPoint, true))
184 // but not on point
185 if(!rTestPoint.equal(pEdgeA->getEnd()) && !rTestPoint.equal(pEdgeB->getEnd()))
187 // found point in triangle -> split triangle inserting two edges
188 EdgeEntry* pStart = new EdgeEntry(pEdgeA->getStart(), rTestPoint);
189 EdgeEntry* pEnd = new EdgeEntry(*pStart);
190 maNewEdgeEntries.emplace_back(pStart);
191 maNewEdgeEntries.emplace_back(pEnd);
193 pStart->setNext(pEnd);
194 pEnd->setNext(pEdgeA->getNext());
195 pEdgeA->setNext(pStart);
197 return false;
201 return true;
204 void Triangulator::createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC)
206 maResult.append(rA);
207 maResult.append(rB);
208 maResult.append(rC);
211 // consume as long as there are edges
212 Triangulator::Triangulator(const B2DPolyPolygon& rCandidate)
213 : mpList(nullptr)
215 // add all available edges to the single linked local list which will be sorted
216 // by Y,X,atan2 when adding nodes
217 if(rCandidate.count())
219 for(sal_uInt32 a(0); a < rCandidate.count(); a++)
221 const B2DPolygon aPolygonCandidate(rCandidate.getB2DPolygon(a));
222 const sal_uInt32 nCount(aPolygonCandidate.count());
224 if(nCount > 2)
226 B2DPoint aPrevPnt(aPolygonCandidate.getB2DPoint(nCount - 1));
228 for(sal_uInt32 b(0); b < nCount; b++)
230 B2DPoint aNextPnt(aPolygonCandidate.getB2DPoint(b));
232 if( !aPrevPnt.equal(aNextPnt) )
234 maStartEntries.emplace_back(aPrevPnt, aNextPnt);
237 aPrevPnt = aNextPnt;
242 if(!maStartEntries.empty())
244 // sort initial list
245 std::sort(maStartEntries.begin(), maStartEntries.end());
247 // insert to own simply linked list
248 EdgeEntries::iterator aPos(maStartEntries.begin());
249 mpList = &(*aPos++);
250 EdgeEntry* pLast = mpList;
252 while(aPos != maStartEntries.end())
254 EdgeEntry* pEntry = &(*aPos++);
255 pLast->setNext(pEntry);
256 pLast = pEntry;
261 while(mpList)
263 if(mpList->getNext() && mpList->getNext()->getStart().equal(mpList->getStart()))
265 // next candidate. There are two edges and start point is equal.
266 // Length is not zero.
267 EdgeEntry* pEdgeA = mpList;
268 EdgeEntry* pEdgeB = pEdgeA->getNext();
270 if( pEdgeA->getEnd().equal(pEdgeB->getEnd()) )
272 // start and end equal -> neutral triangle, delete both
273 mpList = pEdgeB->getNext();
275 else
277 const B2DVector aLeft(pEdgeA->getEnd() - pEdgeA->getStart());
278 const B2DVector aRight(pEdgeB->getEnd() - pEdgeA->getStart());
280 if(getOrientation(aLeft, aRight) == B2VectorOrientation::Neutral)
282 // edges are parallel and have different length -> neutral triangle,
283 // delete both edges and handle closing edge
284 mpList = pEdgeB->getNext();
285 handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
287 else
289 // not parallel, look for points inside
290 B2DRange aRange(pEdgeA->getStart(), pEdgeA->getEnd());
291 aRange.expand(pEdgeB->getEnd());
292 EdgeEntry* pTestEdge = pEdgeB->getNext();
293 bool bNoPointInTriangle(true);
295 // look for start point in triangle
296 while(bNoPointInTriangle && pTestEdge)
298 if(aRange.getMaxY() < pTestEdge->getStart().getY())
300 // edge is below test range and edges are sorted -> stop looking
301 break;
303 else
305 // do not look for edges with same start point, they are sorted and cannot end inside.
306 if(!pTestEdge->getStart().equal(pEdgeA->getStart()))
308 if(aRange.isInside(pTestEdge->getStart()))
310 bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getStart());
315 // next candidate
316 pTestEdge = pTestEdge->getNext();
319 if(bNoPointInTriangle)
321 // look for end point in triangle
322 pTestEdge = pEdgeB->getNext();
324 while(bNoPointInTriangle && pTestEdge)
326 if(aRange.getMaxY() < pTestEdge->getStart().getY())
328 // edge is below test range and edges are sorted -> stop looking
329 break;
331 else
333 // do not look for edges with same end point, they are sorted and cannot end inside.
334 if(!pTestEdge->getEnd().equal(pEdgeA->getStart()))
336 if(aRange.isInside(pTestEdge->getEnd()))
338 bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getEnd());
343 // next candidate
344 pTestEdge = pTestEdge->getNext();
348 if(bNoPointInTriangle)
350 // create triangle, remove edges, handle closing edge
351 mpList = pEdgeB->getNext();
352 createTriangle(pEdgeA->getStart(), pEdgeB->getEnd(), pEdgeA->getEnd());
353 handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
358 else
360 // only one entry at start point, delete it
361 mpList = mpList->getNext();
366 } // end of anonymous namespace
367 } // end of namespace basegfx
369 namespace basegfx
371 namespace triangulator
373 B2DPolygon triangulate(const B2DPolygon& rCandidate)
375 B2DPolygon aRetval;
377 // subdivide locally (triangulate does not work with beziers), remove double and neutral points
378 B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? utils::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
379 aCandidate.removeDoublePoints();
380 aCandidate = utils::removeNeutralPoints(aCandidate);
382 if(aCandidate.count() == 2)
384 // candidate IS a triangle, just append
385 aRetval.append(aCandidate);
387 else if(aCandidate.count() > 2)
389 if(utils::isConvex(aCandidate))
391 // polygon is convex, just use a triangle fan
392 utils::addTriangleFan(aCandidate, aRetval);
394 else
396 // polygon is concave.
397 const B2DPolyPolygon aCandPolyPoly(aCandidate);
398 Triangulator aTriangulator(aCandPolyPoly);
399 aRetval = aTriangulator.getResult();
403 return aRetval;
406 B2DPolygon triangulate(const B2DPolyPolygon& rCandidate)
408 B2DPolygon aRetval;
410 // subdivide locally (triangulate does not work with beziers)
411 B2DPolyPolygon aCandidate(rCandidate.areControlPointsUsed() ? utils::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
413 if(aCandidate.count() == 1)
415 // single polygon -> single polygon triangulation
416 const B2DPolygon aSinglePolygon(aCandidate.getB2DPolygon(0));
417 aRetval = triangulate(aSinglePolygon);
419 else
421 Triangulator aTriangulator(aCandidate);
422 aRetval = aTriangulator.getResult();
425 return aRetval;
427 } // end of namespace triangulator
428 } // end of namespace basegfx
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */