1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
43 EdgeEntry(const B2DPoint
& rStart
, const B2DPoint
& rEnd
)
49 // make sure edge goes down. If horizontal, let it go to the right (left-handed).
52 if(::basegfx::fTools::equal(maStart
.getY(), maEnd
.getY()))
54 if(maStart
.getX() > maEnd
.getX())
59 else if(maStart
.getY() > maEnd
.getY())
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
;
111 EdgeEntries maStartEntries
;
112 std::vector
< std::unique_ptr
<EdgeEntry
> > maNewEdgeEntries
;
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
);
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;
133 && pCurr
->getStart().getY() <= aNew
.getStart().getY()
137 pCurr
= pCurr
->getNext();
140 if(pCurr
&& *pCurr
== aNew
)
142 // found closing edge, remove
145 pPrev
->setNext(pCurr
->getNext());
149 mpList
= pCurr
->getNext();
154 // insert closing edge
155 EdgeEntry
* pNew
= new EdgeEntry(aNew
);
156 maNewEdgeEntries
.emplace_back(pNew
);
160 while(pCurr
&& *pCurr
< *pNew
)
163 pCurr
= pCurr
->getNext();
168 pNew
->setNext(pPrev
->getNext());
169 pPrev
->setNext(pNew
);
173 pNew
->setNext(mpList
);
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))
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
);
204 void Triangulator::createTriangle(const B2DPoint
& rA
, const B2DPoint
& rB
, const B2DPoint
& rC
)
211 // consume as long as there are edges
212 Triangulator::Triangulator(const B2DPolyPolygon
& rCandidate
)
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());
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
);
242 if(!maStartEntries
.empty())
245 std::sort(maStartEntries
.begin(), maStartEntries
.end());
247 // insert to own simply linked list
248 EdgeEntries::iterator
aPos(maStartEntries
.begin());
250 EdgeEntry
* pLast
= mpList
;
252 while(aPos
!= maStartEntries
.end())
254 EdgeEntry
* pEntry
= &(*aPos
++);
255 pLast
->setNext(pEntry
);
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();
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());
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
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());
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
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());
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());
360 // only one entry at start point, delete it
361 mpList
= mpList
->getNext();
366 } // end of anonymous namespace
367 } // end of namespace basegfx
371 namespace triangulator
373 B2DPolygon
triangulate(const B2DPolygon
& rCandidate
)
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
);
396 // polygon is concave.
397 const B2DPolyPolygon
aCandPolyPoly(aCandidate
);
398 Triangulator
aTriangulator(aCandPolyPoly
);
399 aRetval
= aTriangulator
.getResult();
406 B2DPolygon
triangulate(const B2DPolyPolygon
& rCandidate
)
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
);
421 Triangulator
aTriangulator(aCandidate
);
422 aRetval
= aTriangulator
.getResult();
427 } // end of namespace triangulator
428 } // end of namespace basegfx
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */