1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: b2dpolygontriangulator.cxx,v $
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>
45 //////////////////////////////////////////////////////////////////////////////
59 EdgeEntry(const B2DPoint
& rStart
, const B2DPoint
& rEnd
)
65 // make sure edge goes down. If horizontal, let it go to the right (left-handed).
68 if(::basegfx::fTools::equal(maStart
.getY(), maEnd
.getY()))
70 if(maStart
.getX() > maEnd
.getX())
75 else if(maStart
.getY() > maEnd
.getY())
86 mfAtan2
= atan2(maEnd
.getY() - maStart
.getY(), maEnd
.getX() - maStart
.getX());
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 //////////////////////////////////////////////////////////////////////////////
136 EdgeEntries maStartEntries
;
137 EdgeEntryPointers maNewEdgeEntries
;
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
);
145 Triangulator(const B2DPolyPolygon
& rCandidate
);
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;
159 && pCurr
->getStart().getY() <= aNew
.getStart().getY()
163 pCurr
= pCurr
->getNext();
166 if(pCurr
&& *pCurr
== aNew
)
168 // found closing edge, remove
171 pPrev
->setNext(pCurr
->getNext());
175 mpList
= pCurr
->getNext();
180 // insert closing edge
181 EdgeEntry
* pNew
= new EdgeEntry(aNew
);
182 maNewEdgeEntries
.push_back(pNew
);
186 while(pCurr
&& *pCurr
< *pNew
)
189 pCurr
= pCurr
->getNext();
194 pNew
->setNext(pPrev
->getNext());
195 pPrev
->setNext(pNew
);
199 pNew
->setNext(mpList
);
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))
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
);
230 void Triangulator::createTriangle(const B2DPoint
& rA
, const B2DPoint
& rB
, const B2DPoint
& rC
)
237 // consume as long as there are edges
238 Triangulator::Triangulator(const B2DPolyPolygon
& rCandidate
)
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());
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
));
268 if(maStartEntries
.size())
271 ::std::sort(maStartEntries
.begin(), maStartEntries
.end());
273 // insert to own simply linked list
274 EdgeEntries::iterator
aPos(maStartEntries
.begin());
276 EdgeEntry
* pLast
= mpList
;
278 while(aPos
!= maStartEntries
.end())
280 EdgeEntry
* pEntry
= &(*aPos
++);
281 pLast
->setNext(pEntry
);
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();
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());
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
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());
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
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());
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());
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())
402 } // end of anonymous namespace
403 } // end of namespace basegfx
405 //////////////////////////////////////////////////////////////////////////////
409 namespace triangulator
411 B2DPolygon
triangulate(const B2DPolygon
& rCandidate
)
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
);
434 // polygon is concave.
435 const B2DPolyPolygon
aCandPolyPoly(aCandidate
);
436 Triangulator
aTriangulator(aCandPolyPoly
);
437 aRetval
= aTriangulator
.getResult();
444 B2DPolygon
triangulate(const B2DPolyPolygon
& rCandidate
)
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
);
459 Triangulator
aTriangulator(aCandidate
);
460 aRetval
= aTriangulator
.getResult();
465 } // end of namespace triangulator
466 } // end of namespace basegfx
468 //////////////////////////////////////////////////////////////////////////////