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 <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>
32 //////////////////////////////////////////////////////////////////////////////
46 EdgeEntry(const B2DPoint
& rStart
, const B2DPoint
& rEnd
)
52 // make sure edge goes down. If horizontal, let it go to the right (left-handed).
55 if(::basegfx::fTools::equal(maStart
.getY(), maEnd
.getY()))
57 if(maStart
.getX() > maEnd
.getX())
62 else if(maStart
.getY() > maEnd
.getY())
73 mfAtan2
= atan2(maEnd
.getY() - maStart
.getY(), maEnd
.getX() - maStart
.getX());
80 bool operator<(const EdgeEntry
& rComp
) const
82 if(::basegfx::fTools::equal(maStart
.getY(), rComp
.maStart
.getY()))
84 if(::basegfx::fTools::equal(maStart
.getX(), rComp
.maStart
.getX()))
86 // same in x and y -> same start point. Sort emitting vectors from left to right.
87 return (mfAtan2
> rComp
.mfAtan2
);
90 return (maStart
.getX() < rComp
.maStart
.getX());
93 return (maStart
.getY() < rComp
.maStart
.getY());
96 bool operator==(const EdgeEntry
& rComp
) const
98 return (maStart
.equal(rComp
.maStart
) && maEnd
.equal(rComp
.maEnd
));
101 bool operator!=(const EdgeEntry
& rComp
) const
103 return !(*this == rComp
);
106 const B2DPoint
& getStart() const { return maStart
; }
107 const B2DPoint
& getEnd() const { return maEnd
; }
109 EdgeEntry
* getNext() const { return mpNext
; }
110 void setNext(EdgeEntry
* pNext
) { mpNext
= pNext
; }
113 //////////////////////////////////////////////////////////////////////////////
115 typedef ::std::vector
< EdgeEntry
> EdgeEntries
;
116 typedef ::std::vector
< EdgeEntry
* > EdgeEntryPointers
;
118 //////////////////////////////////////////////////////////////////////////////
123 EdgeEntries maStartEntries
;
124 EdgeEntryPointers maNewEdgeEntries
;
127 void handleClosingEdge(const B2DPoint
& rStart
, const B2DPoint
& rEnd
);
128 bool CheckPointInTriangle(EdgeEntry
* pEdgeA
, EdgeEntry
* pEdgeB
, const B2DPoint
& rTestPoint
);
129 void createTriangle(const B2DPoint
& rA
, const B2DPoint
& rB
, const B2DPoint
& rC
);
132 explicit Triangulator(const B2DPolyPolygon
& rCandidate
);
135 const B2DPolygon
getResult() const { return maResult
; }
138 void Triangulator::handleClosingEdge(const B2DPoint
& rStart
, const B2DPoint
& rEnd
)
140 // create an entry, else the comparison might use the wrong edges
141 EdgeEntry
aNew(rStart
, rEnd
);
142 EdgeEntry
* pCurr
= mpList
;
143 EdgeEntry
* pPrev
= 0L;
146 && pCurr
->getStart().getY() <= aNew
.getStart().getY()
150 pCurr
= pCurr
->getNext();
153 if(pCurr
&& *pCurr
== aNew
)
155 // found closing edge, remove
158 pPrev
->setNext(pCurr
->getNext());
162 mpList
= pCurr
->getNext();
167 // insert closing edge
168 EdgeEntry
* pNew
= new EdgeEntry(aNew
);
169 maNewEdgeEntries
.push_back(pNew
);
173 while(pCurr
&& *pCurr
< *pNew
)
176 pCurr
= pCurr
->getNext();
181 pNew
->setNext(pPrev
->getNext());
182 pPrev
->setNext(pNew
);
186 pNew
->setNext(mpList
);
192 bool Triangulator::CheckPointInTriangle(EdgeEntry
* pEdgeA
, EdgeEntry
* pEdgeB
, const B2DPoint
& rTestPoint
)
194 // inside triangle or on edge?
195 if(tools::isPointInTriangle(pEdgeA
->getStart(), pEdgeA
->getEnd(), pEdgeB
->getEnd(), rTestPoint
, true))
198 if(!rTestPoint
.equal(pEdgeA
->getEnd()) && !rTestPoint
.equal(pEdgeB
->getEnd()))
200 // found point in triangle -> split triangle inserting two edges
201 EdgeEntry
* pStart
= new EdgeEntry(pEdgeA
->getStart(), rTestPoint
);
202 EdgeEntry
* pEnd
= new EdgeEntry(*pStart
);
203 maNewEdgeEntries
.push_back(pStart
);
204 maNewEdgeEntries
.push_back(pEnd
);
206 pStart
->setNext(pEnd
);
207 pEnd
->setNext(pEdgeA
->getNext());
208 pEdgeA
->setNext(pStart
);
217 void Triangulator::createTriangle(const B2DPoint
& rA
, const B2DPoint
& rB
, const B2DPoint
& rC
)
224 // consume as long as there are edges
225 Triangulator::Triangulator(const B2DPolyPolygon
& rCandidate
)
228 // add all available edges to the single linked local list which will be sorted
229 // by Y,X,atan2 when adding nodes
230 if(rCandidate
.count())
232 for(sal_uInt32
a(0L); a
< rCandidate
.count(); a
++)
234 const B2DPolygon
aPolygonCandidate(rCandidate
.getB2DPolygon(a
));
235 const sal_uInt32
nCount(aPolygonCandidate
.count());
239 B2DPoint
aPrevPnt(aPolygonCandidate
.getB2DPoint(nCount
- 1L));
241 for(sal_uInt32
b(0L); b
< nCount
; b
++)
243 B2DPoint
aNextPnt(aPolygonCandidate
.getB2DPoint(b
));
245 if( !aPrevPnt
.equal(aNextPnt
) )
247 maStartEntries
.push_back(EdgeEntry(aPrevPnt
, aNextPnt
));
255 if(!maStartEntries
.empty())
258 ::std::sort(maStartEntries
.begin(), maStartEntries
.end());
260 // insert to own simply linked list
261 EdgeEntries::iterator
aPos(maStartEntries
.begin());
263 EdgeEntry
* pLast
= mpList
;
265 while(aPos
!= maStartEntries
.end())
267 EdgeEntry
* pEntry
= &(*aPos
++);
268 pLast
->setNext(pEntry
);
276 if(mpList
->getNext() && mpList
->getNext()->getStart().equal(mpList
->getStart()))
278 // next candidate. There are two edges and start point is equal.
279 // Length is not zero.
280 EdgeEntry
* pEdgeA
= mpList
;
281 EdgeEntry
* pEdgeB
= pEdgeA
->getNext();
283 if( pEdgeA
->getEnd().equal(pEdgeB
->getEnd()) )
285 // start and end equal -> neutral triangle, delete both
286 mpList
= pEdgeB
->getNext();
290 const B2DVector
aLeft(pEdgeA
->getEnd() - pEdgeA
->getStart());
291 const B2DVector
aRight(pEdgeB
->getEnd() - pEdgeA
->getStart());
293 if(ORIENTATION_NEUTRAL
== getOrientation(aLeft
, aRight
))
295 // edges are parallel and have different length -> neutral triangle,
296 // delete both edges and handle closing edge
297 mpList
= pEdgeB
->getNext();
298 handleClosingEdge(pEdgeA
->getEnd(), pEdgeB
->getEnd());
302 // not parallel, look for points inside
303 B2DRange
aRange(pEdgeA
->getStart(), pEdgeA
->getEnd());
304 aRange
.expand(pEdgeB
->getEnd());
305 EdgeEntry
* pTestEdge
= pEdgeB
->getNext();
306 bool bNoPointInTriangle(true);
308 // look for start point in triangle
309 while(bNoPointInTriangle
&& pTestEdge
)
311 if(aRange
.getMaxY() < pTestEdge
->getStart().getY())
313 // edge is below test range and edges are sorted -> stop looking
318 // do not look for edges with same start point, they are sorted and cannot end inside.
319 if(!pTestEdge
->getStart().equal(pEdgeA
->getStart()))
321 if(aRange
.isInside(pTestEdge
->getStart()))
323 bNoPointInTriangle
= CheckPointInTriangle(pEdgeA
, pEdgeB
, pTestEdge
->getStart());
329 pTestEdge
= pTestEdge
->getNext();
332 if(bNoPointInTriangle
)
334 // look for end point in triange
335 pTestEdge
= pEdgeB
->getNext();
337 while(bNoPointInTriangle
&& pTestEdge
)
339 if(aRange
.getMaxY() < pTestEdge
->getStart().getY())
341 // edge is below test range and edges are sorted -> stop looking
346 // do not look for edges with same end point, they are sorted and cannot end inside.
347 if(!pTestEdge
->getEnd().equal(pEdgeA
->getStart()))
349 if(aRange
.isInside(pTestEdge
->getEnd()))
351 bNoPointInTriangle
= CheckPointInTriangle(pEdgeA
, pEdgeB
, pTestEdge
->getEnd());
357 pTestEdge
= pTestEdge
->getNext();
361 if(bNoPointInTriangle
)
363 // create triangle, remove edges, handle closing edge
364 mpList
= pEdgeB
->getNext();
365 createTriangle(pEdgeA
->getStart(), pEdgeB
->getEnd(), pEdgeA
->getEnd());
366 handleClosingEdge(pEdgeA
->getEnd(), pEdgeB
->getEnd());
373 // only one entry at start point, delete it
374 mpList
= mpList
->getNext();
379 Triangulator::~Triangulator()
381 EdgeEntryPointers::iterator
aIter(maNewEdgeEntries
.begin());
383 while(aIter
!= maNewEdgeEntries
.end())
389 } // end of anonymous namespace
390 } // end of namespace basegfx
392 //////////////////////////////////////////////////////////////////////////////
396 namespace triangulator
398 B2DPolygon
triangulate(const B2DPolygon
& rCandidate
)
402 // subdivide locally (triangulate does not work with beziers), remove double and neutral points
403 B2DPolygon
aCandidate(rCandidate
.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate
) : rCandidate
);
404 aCandidate
.removeDoublePoints();
405 aCandidate
= tools::removeNeutralPoints(aCandidate
);
407 if(2L == aCandidate
.count())
409 // candidate IS a triangle, just append
410 aRetval
.append(aCandidate
);
412 else if(aCandidate
.count() > 2L)
414 if(tools::isConvex(aCandidate
))
416 // polygon is convex, just use a triangle fan
417 tools::addTriangleFan(aCandidate
, aRetval
);
421 // polygon is concave.
422 const B2DPolyPolygon
aCandPolyPoly(aCandidate
);
423 Triangulator
aTriangulator(aCandPolyPoly
);
424 aRetval
= aTriangulator
.getResult();
431 B2DPolygon
triangulate(const B2DPolyPolygon
& rCandidate
)
435 // subdivide locally (triangulate does not work with beziers)
436 B2DPolyPolygon
aCandidate(rCandidate
.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate
) : rCandidate
);
438 if(1L == aCandidate
.count())
440 // single polygon -> single polygon triangulation
441 const B2DPolygon
aSinglePolygon(aCandidate
.getB2DPolygon(0L));
442 aRetval
= triangulate(aSinglePolygon
);
446 Triangulator
aTriangulator(aCandidate
);
447 aRetval
= aTriangulator
.getResult();
452 } // end of namespace triangulator
453 } // end of namespace basegfx
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */