1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_basegfx.hxx"
30 #include <basegfx/polygon/b2dpolygontriangulator.hxx>
31 #include <osl/diagnose.h>
32 #include <basegfx/point/b2dpoint.hxx>
33 #include <basegfx/polygon/b2dpolygon.hxx>
34 #include <basegfx/vector/b2dvector.hxx>
35 #include <basegfx/polygon/b2dpolygontools.hxx>
36 #include <basegfx/polygon/b2dpolypolygontools.hxx>
37 #include <basegfx/range/b2drange.hxx>
38 #include <basegfx/numeric/ftools.hxx>
42 //////////////////////////////////////////////////////////////////////////////
56 EdgeEntry(const B2DPoint
& rStart
, const B2DPoint
& rEnd
)
62 // make sure edge goes down. If horizontal, let it go to the right (left-handed).
65 if(::basegfx::fTools::equal(maStart
.getY(), maEnd
.getY()))
67 if(maStart
.getX() > maEnd
.getX())
72 else if(maStart
.getY() > maEnd
.getY())
83 mfAtan2
= atan2(maEnd
.getY() - maStart
.getY(), maEnd
.getX() - maStart
.getX());
90 bool operator<(const EdgeEntry
& rComp
) const
92 if(::basegfx::fTools::equal(maStart
.getY(), rComp
.maStart
.getY()))
94 if(::basegfx::fTools::equal(maStart
.getX(), rComp
.maStart
.getX()))
96 // same in x and y -> same start point. Sort emitting vectors from left to right.
97 return (mfAtan2
> rComp
.mfAtan2
);
100 return (maStart
.getX() < rComp
.maStart
.getX());
103 return (maStart
.getY() < rComp
.maStart
.getY());
106 bool operator==(const EdgeEntry
& rComp
) const
108 return (maStart
.equal(rComp
.maStart
) && maEnd
.equal(rComp
.maEnd
));
111 bool operator!=(const EdgeEntry
& rComp
) const
113 return !(*this == rComp
);
116 const B2DPoint
& getStart() const { return maStart
; }
117 const B2DPoint
& getEnd() const { return maEnd
; }
119 EdgeEntry
* getNext() const { return mpNext
; }
120 void setNext(EdgeEntry
* pNext
) { mpNext
= pNext
; }
123 //////////////////////////////////////////////////////////////////////////////
125 typedef ::std::vector
< EdgeEntry
> EdgeEntries
;
126 typedef ::std::vector
< EdgeEntry
* > EdgeEntryPointers
;
128 //////////////////////////////////////////////////////////////////////////////
133 EdgeEntries maStartEntries
;
134 EdgeEntryPointers maNewEdgeEntries
;
137 void handleClosingEdge(const B2DPoint
& rStart
, const B2DPoint
& rEnd
);
138 bool CheckPointInTriangle(EdgeEntry
* pEdgeA
, EdgeEntry
* pEdgeB
, const B2DPoint
& rTestPoint
);
139 void createTriangle(const B2DPoint
& rA
, const B2DPoint
& rB
, const B2DPoint
& rC
);
142 Triangulator(const B2DPolyPolygon
& rCandidate
);
145 const B2DPolygon
getResult() const { return maResult
; }
148 void Triangulator::handleClosingEdge(const B2DPoint
& rStart
, const B2DPoint
& rEnd
)
150 // create an entry, else the comparison might use the wrong edges
151 EdgeEntry
aNew(rStart
, rEnd
);
152 EdgeEntry
* pCurr
= mpList
;
153 EdgeEntry
* pPrev
= 0L;
156 && pCurr
->getStart().getY() <= aNew
.getStart().getY()
160 pCurr
= pCurr
->getNext();
163 if(pCurr
&& *pCurr
== aNew
)
165 // found closing edge, remove
168 pPrev
->setNext(pCurr
->getNext());
172 mpList
= pCurr
->getNext();
177 // insert closing edge
178 EdgeEntry
* pNew
= new EdgeEntry(aNew
);
179 maNewEdgeEntries
.push_back(pNew
);
183 while(pCurr
&& *pCurr
< *pNew
)
186 pCurr
= pCurr
->getNext();
191 pNew
->setNext(pPrev
->getNext());
192 pPrev
->setNext(pNew
);
196 pNew
->setNext(mpList
);
202 bool Triangulator::CheckPointInTriangle(EdgeEntry
* pEdgeA
, EdgeEntry
* pEdgeB
, const B2DPoint
& rTestPoint
)
204 // inside triangle or on edge?
205 if(tools::isPointInTriangle(pEdgeA
->getStart(), pEdgeA
->getEnd(), pEdgeB
->getEnd(), rTestPoint
, true))
208 if(!rTestPoint
.equal(pEdgeA
->getEnd()) && !rTestPoint
.equal(pEdgeB
->getEnd()))
210 // found point in triangle -> split triangle inserting two edges
211 EdgeEntry
* pStart
= new EdgeEntry(pEdgeA
->getStart(), rTestPoint
);
212 EdgeEntry
* pEnd
= new EdgeEntry(*pStart
);
213 maNewEdgeEntries
.push_back(pStart
);
214 maNewEdgeEntries
.push_back(pEnd
);
216 pStart
->setNext(pEnd
);
217 pEnd
->setNext(pEdgeA
->getNext());
218 pEdgeA
->setNext(pStart
);
227 void Triangulator::createTriangle(const B2DPoint
& rA
, const B2DPoint
& rB
, const B2DPoint
& rC
)
234 // consume as long as there are edges
235 Triangulator::Triangulator(const B2DPolyPolygon
& rCandidate
)
238 // add all available edges to the single linked local list which will be sorted
239 // by Y,X,atan2 when adding nodes
240 if(rCandidate
.count())
242 for(sal_uInt32
a(0L); a
< rCandidate
.count(); a
++)
244 const B2DPolygon
aPolygonCandidate(rCandidate
.getB2DPolygon(a
));
245 const sal_uInt32
nCount(aPolygonCandidate
.count());
249 B2DPoint
aPrevPnt(aPolygonCandidate
.getB2DPoint(nCount
- 1L));
251 for(sal_uInt32
b(0L); b
< nCount
; b
++)
253 B2DPoint
aNextPnt(aPolygonCandidate
.getB2DPoint(b
));
255 if( !aPrevPnt
.equal(aNextPnt
) )
257 maStartEntries
.push_back(EdgeEntry(aPrevPnt
, aNextPnt
));
265 if(maStartEntries
.size())
268 ::std::sort(maStartEntries
.begin(), maStartEntries
.end());
270 // insert to own simply linked list
271 EdgeEntries::iterator
aPos(maStartEntries
.begin());
273 EdgeEntry
* pLast
= mpList
;
275 while(aPos
!= maStartEntries
.end())
277 EdgeEntry
* pEntry
= &(*aPos
++);
278 pLast
->setNext(pEntry
);
286 if(mpList
->getNext() && mpList
->getNext()->getStart().equal(mpList
->getStart()))
288 // next candidate. There are two edges and start point is equal.
289 // Length is not zero.
290 EdgeEntry
* pEdgeA
= mpList
;
291 EdgeEntry
* pEdgeB
= pEdgeA
->getNext();
293 if( pEdgeA
->getEnd().equal(pEdgeB
->getEnd()) )
295 // start and end equal -> neutral triangle, delete both
296 mpList
= pEdgeB
->getNext();
300 const B2DVector
aLeft(pEdgeA
->getEnd() - pEdgeA
->getStart());
301 const B2DVector
aRight(pEdgeB
->getEnd() - pEdgeA
->getStart());
303 if(ORIENTATION_NEUTRAL
== getOrientation(aLeft
, aRight
))
305 // edges are parallel and have different length -> neutral triangle,
306 // delete both edges and handle closing edge
307 mpList
= pEdgeB
->getNext();
308 handleClosingEdge(pEdgeA
->getEnd(), pEdgeB
->getEnd());
312 // not parallel, look for points inside
313 B2DRange
aRange(pEdgeA
->getStart(), pEdgeA
->getEnd());
314 aRange
.expand(pEdgeB
->getEnd());
315 EdgeEntry
* pTestEdge
= pEdgeB
->getNext();
316 bool bNoPointInTriangle(true);
318 // look for start point in triangle
319 while(bNoPointInTriangle
&& pTestEdge
)
321 if(aRange
.getMaxY() < pTestEdge
->getStart().getY())
323 // edge is below test range and edges are sorted -> stop looking
328 // do not look for edges with same start point, they are sorted and cannot end inside.
329 if(!pTestEdge
->getStart().equal(pEdgeA
->getStart()))
331 if(aRange
.isInside(pTestEdge
->getStart()))
333 bNoPointInTriangle
= CheckPointInTriangle(pEdgeA
, pEdgeB
, pTestEdge
->getStart());
339 pTestEdge
= pTestEdge
->getNext();
342 if(bNoPointInTriangle
)
344 // look for end point in triange
345 pTestEdge
= pEdgeB
->getNext();
347 while(bNoPointInTriangle
&& pTestEdge
)
349 if(aRange
.getMaxY() < pTestEdge
->getStart().getY())
351 // edge is below test range and edges are sorted -> stop looking
356 // do not look for edges with same end point, they are sorted and cannot end inside.
357 if(!pTestEdge
->getEnd().equal(pEdgeA
->getStart()))
359 if(aRange
.isInside(pTestEdge
->getEnd()))
361 bNoPointInTriangle
= CheckPointInTriangle(pEdgeA
, pEdgeB
, pTestEdge
->getEnd());
367 pTestEdge
= pTestEdge
->getNext();
371 if(bNoPointInTriangle
)
373 // create triangle, remove edges, handle closing edge
374 mpList
= pEdgeB
->getNext();
375 createTriangle(pEdgeA
->getStart(), pEdgeB
->getEnd(), pEdgeA
->getEnd());
376 handleClosingEdge(pEdgeA
->getEnd(), pEdgeB
->getEnd());
383 // only one entry at start point, delete it
384 mpList
= mpList
->getNext();
389 Triangulator::~Triangulator()
391 EdgeEntryPointers::iterator
aIter(maNewEdgeEntries
.begin());
393 while(aIter
!= maNewEdgeEntries
.end())
399 } // end of anonymous namespace
400 } // end of namespace basegfx
402 //////////////////////////////////////////////////////////////////////////////
406 namespace triangulator
408 B2DPolygon
triangulate(const B2DPolygon
& rCandidate
)
412 // subdivide locally (triangulate does not work with beziers), remove double and neutral points
413 B2DPolygon
aCandidate(rCandidate
.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate
) : rCandidate
);
414 aCandidate
.removeDoublePoints();
415 aCandidate
= tools::removeNeutralPoints(aCandidate
);
417 if(2L == aCandidate
.count())
419 // candidate IS a triangle, just append
420 aRetval
.append(aCandidate
);
422 else if(aCandidate
.count() > 2L)
424 if(tools::isConvex(aCandidate
))
426 // polygon is convex, just use a triangle fan
427 tools::addTriangleFan(aCandidate
, aRetval
);
431 // polygon is concave.
432 const B2DPolyPolygon
aCandPolyPoly(aCandidate
);
433 Triangulator
aTriangulator(aCandPolyPoly
);
434 aRetval
= aTriangulator
.getResult();
441 B2DPolygon
triangulate(const B2DPolyPolygon
& rCandidate
)
445 // subdivide locally (triangulate does not work with beziers)
446 B2DPolyPolygon
aCandidate(rCandidate
.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate
) : rCandidate
);
448 if(1L == aCandidate
.count())
450 // single polygon -> single polygon triangulation
451 const B2DPolygon
aSinglePolygon(aCandidate
.getB2DPolygon(0L));
452 aRetval
= triangulate(aSinglePolygon
);
456 Triangulator
aTriangulator(aCandidate
);
457 aRetval
= aTriangulator
.getResult();
462 } // end of namespace triangulator
463 } // end of namespace basegfx
465 //////////////////////////////////////////////////////////////////////////////