1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <basegfx/polygon/b2dpolygontriangulator.hxx>
#include <osl/diagnose.h>
#include <basegfx/point/b2dpoint.hxx>
#include <basegfx/polygon/b2dpolygon.hxx>
#include <basegfx/vector/b2dvector.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/polygon/b2dpolypolygontools.hxx>
#include <basegfx/range/b2drange.hxx>
#include <basegfx/numeric/ftools.hxx>
#include <algorithm>
namespace basegfx
{
namespace
{
class EdgeEntry
{
EdgeEntry* mpNext;
B2DPoint maStart;
B2DPoint maEnd;
double mfAtan2;
public:
EdgeEntry(const B2DPoint& rStart, const B2DPoint& rEnd)
: mpNext(0L),
maStart(rStart),
maEnd(rEnd),
mfAtan2(0.0)
{
// make sure edge goes down. If horizontal, let it go to the right (left-handed).
bool bSwap(false);
if(::basegfx::fTools::equal(maStart.getY(), maEnd.getY()))
{
if(maStart.getX() > maEnd.getX())
{
bSwap = true;
}
}
else if(maStart.getY() > maEnd.getY())
{
bSwap = true;
}
if(bSwap)
{
maStart = rEnd;
maEnd = rStart;
}
mfAtan2 = atan2(maEnd.getY() - maStart.getY(), maEnd.getX() - maStart.getX());
}
~EdgeEntry()
{
}
bool operator<(const EdgeEntry& rComp) const
{
if(::basegfx::fTools::equal(maStart.getY(), rComp.maStart.getY()))
{
if(::basegfx::fTools::equal(maStart.getX(), rComp.maStart.getX()))
{
// same in x and y -> same start point. Sort emitting vectors from left to right.
return (mfAtan2 > rComp.mfAtan2);
}
return (maStart.getX() < rComp.maStart.getX());
}
return (maStart.getY() < rComp.maStart.getY());
}
bool operator==(const EdgeEntry& rComp) const
{
return (maStart.equal(rComp.maStart) && maEnd.equal(rComp.maEnd));
}
bool operator!=(const EdgeEntry& rComp) const
{
return !(*this == rComp);
}
const B2DPoint& getStart() const { return maStart; }
const B2DPoint& getEnd() const { return maEnd; }
EdgeEntry* getNext() const { return mpNext; }
void setNext(EdgeEntry* pNext) { mpNext = pNext; }
};
typedef ::std::vector< EdgeEntry > EdgeEntries;
typedef ::std::vector< EdgeEntry* > EdgeEntryPointers;
class Triangulator
{
EdgeEntry* mpList;
EdgeEntries maStartEntries;
EdgeEntryPointers maNewEdgeEntries;
B2DPolygon maResult;
void handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd);
bool CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry* pEdgeB, const B2DPoint& rTestPoint);
void createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC);
public:
explicit Triangulator(const B2DPolyPolygon& rCandidate);
~Triangulator();
const B2DPolygon getResult() const { return maResult; }
};
void Triangulator::handleClosingEdge(const B2DPoint& rStart, const B2DPoint& rEnd)
{
// create an entry, else the comparison might use the wrong edges
EdgeEntry aNew(rStart, rEnd);
EdgeEntry* pCurr = mpList;
EdgeEntry* pPrev = 0L;
while(pCurr
&& pCurr->getStart().getY() <= aNew.getStart().getY()
&& *pCurr != aNew)
{
pPrev = pCurr;
pCurr = pCurr->getNext();
}
if(pCurr && *pCurr == aNew)
{
// found closing edge, remove
if(pPrev)
{
pPrev->setNext(pCurr->getNext());
}
else
{
mpList = pCurr->getNext();
}
}
else
{
// insert closing edge
EdgeEntry* pNew = new EdgeEntry(aNew);
maNewEdgeEntries.push_back(pNew);
pCurr = mpList;
pPrev = 0L;
while(pCurr && *pCurr < *pNew)
{
pPrev = pCurr;
pCurr = pCurr->getNext();
}
if(pPrev)
{
pNew->setNext(pPrev->getNext());
pPrev->setNext(pNew);
}
else
{
pNew->setNext(mpList);
mpList = pNew;
}
}
}
bool Triangulator::CheckPointInTriangle(EdgeEntry* pEdgeA, EdgeEntry* pEdgeB, const B2DPoint& rTestPoint)
{
// inside triangle or on edge?
if(tools::isPointInTriangle(pEdgeA->getStart(), pEdgeA->getEnd(), pEdgeB->getEnd(), rTestPoint, true))
{
// but not on point
if(!rTestPoint.equal(pEdgeA->getEnd()) && !rTestPoint.equal(pEdgeB->getEnd()))
{
// found point in triangle -> split triangle inserting two edges
EdgeEntry* pStart = new EdgeEntry(pEdgeA->getStart(), rTestPoint);
EdgeEntry* pEnd = new EdgeEntry(*pStart);
maNewEdgeEntries.push_back(pStart);
maNewEdgeEntries.push_back(pEnd);
pStart->setNext(pEnd);
pEnd->setNext(pEdgeA->getNext());
pEdgeA->setNext(pStart);
return false;
}
}
return true;
}
void Triangulator::createTriangle(const B2DPoint& rA, const B2DPoint& rB, const B2DPoint& rC)
{
maResult.append(rA);
maResult.append(rB);
maResult.append(rC);
}
// consume as long as there are edges
Triangulator::Triangulator(const B2DPolyPolygon& rCandidate)
: mpList(0L)
{
// add all available edges to the single linked local list which will be sorted
// by Y,X,atan2 when adding nodes
if(rCandidate.count())
{
for(sal_uInt32 a(0L); a < rCandidate.count(); a++)
{
const B2DPolygon aPolygonCandidate(rCandidate.getB2DPolygon(a));
const sal_uInt32 nCount(aPolygonCandidate.count());
if(nCount > 2L)
{
B2DPoint aPrevPnt(aPolygonCandidate.getB2DPoint(nCount - 1L));
for(sal_uInt32 b(0L); b < nCount; b++)
{
B2DPoint aNextPnt(aPolygonCandidate.getB2DPoint(b));
if( !aPrevPnt.equal(aNextPnt) )
{
maStartEntries.push_back(EdgeEntry(aPrevPnt, aNextPnt));
}
aPrevPnt = aNextPnt;
}
}
}
if(!maStartEntries.empty())
{
// sort initial list
::std::sort(maStartEntries.begin(), maStartEntries.end());
// insert to own simply linked list
EdgeEntries::iterator aPos(maStartEntries.begin());
mpList = &(*aPos++);
EdgeEntry* pLast = mpList;
while(aPos != maStartEntries.end())
{
EdgeEntry* pEntry = &(*aPos++);
pLast->setNext(pEntry);
pLast = pEntry;
}
}
}
while(mpList)
{
if(mpList->getNext() && mpList->getNext()->getStart().equal(mpList->getStart()))
{
// next candidate. There are two edges and start point is equal.
// Length is not zero.
EdgeEntry* pEdgeA = mpList;
EdgeEntry* pEdgeB = pEdgeA->getNext();
if( pEdgeA->getEnd().equal(pEdgeB->getEnd()) )
{
// start and end equal -> neutral triangle, delete both
mpList = pEdgeB->getNext();
}
else
{
const B2DVector aLeft(pEdgeA->getEnd() - pEdgeA->getStart());
const B2DVector aRight(pEdgeB->getEnd() - pEdgeA->getStart());
if(B2VectorOrientation::Neutral == getOrientation(aLeft, aRight))
{
// edges are parallel and have different length -> neutral triangle,
// delete both edges and handle closing edge
mpList = pEdgeB->getNext();
handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
}
else
{
// not parallel, look for points inside
B2DRange aRange(pEdgeA->getStart(), pEdgeA->getEnd());
aRange.expand(pEdgeB->getEnd());
EdgeEntry* pTestEdge = pEdgeB->getNext();
bool bNoPointInTriangle(true);
// look for start point in triangle
while(bNoPointInTriangle && pTestEdge)
{
if(aRange.getMaxY() < pTestEdge->getStart().getY())
{
// edge is below test range and edges are sorted -> stop looking
break;
}
else
{
// do not look for edges with same start point, they are sorted and cannot end inside.
if(!pTestEdge->getStart().equal(pEdgeA->getStart()))
{
if(aRange.isInside(pTestEdge->getStart()))
{
bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getStart());
}
}
}
// next candidate
pTestEdge = pTestEdge->getNext();
}
if(bNoPointInTriangle)
{
// look for end point in triangle
pTestEdge = pEdgeB->getNext();
while(bNoPointInTriangle && pTestEdge)
{
if(aRange.getMaxY() < pTestEdge->getStart().getY())
{
// edge is below test range and edges are sorted -> stop looking
break;
}
else
{
// do not look for edges with same end point, they are sorted and cannot end inside.
if(!pTestEdge->getEnd().equal(pEdgeA->getStart()))
{
if(aRange.isInside(pTestEdge->getEnd()))
{
bNoPointInTriangle = CheckPointInTriangle(pEdgeA, pEdgeB, pTestEdge->getEnd());
}
}
}
// next candidate
pTestEdge = pTestEdge->getNext();
}
}
if(bNoPointInTriangle)
{
// create triangle, remove edges, handle closing edge
mpList = pEdgeB->getNext();
createTriangle(pEdgeA->getStart(), pEdgeB->getEnd(), pEdgeA->getEnd());
handleClosingEdge(pEdgeA->getEnd(), pEdgeB->getEnd());
}
}
}
}
else
{
// only one entry at start point, delete it
mpList = mpList->getNext();
}
}
}
Triangulator::~Triangulator()
{
EdgeEntryPointers::iterator aIter(maNewEdgeEntries.begin());
while(aIter != maNewEdgeEntries.end())
{
delete (*aIter++);
}
}
} // end of anonymous namespace
} // end of namespace basegfx
namespace basegfx
{
namespace triangulator
{
B2DPolygon triangulate(const B2DPolygon& rCandidate)
{
B2DPolygon aRetval;
// subdivide locally (triangulate does not work with beziers), remove double and neutral points
B2DPolygon aCandidate(rCandidate.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
aCandidate.removeDoublePoints();
aCandidate = tools::removeNeutralPoints(aCandidate);
if(2L == aCandidate.count())
{
// candidate IS a triangle, just append
aRetval.append(aCandidate);
}
else if(aCandidate.count() > 2L)
{
if(tools::isConvex(aCandidate))
{
// polygon is convex, just use a triangle fan
tools::addTriangleFan(aCandidate, aRetval);
}
else
{
// polygon is concave.
const B2DPolyPolygon aCandPolyPoly(aCandidate);
Triangulator aTriangulator(aCandPolyPoly);
aRetval = aTriangulator.getResult();
}
}
return aRetval;
}
B2DPolygon triangulate(const B2DPolyPolygon& rCandidate)
{
B2DPolygon aRetval;
// subdivide locally (triangulate does not work with beziers)
B2DPolyPolygon aCandidate(rCandidate.areControlPointsUsed() ? tools::adaptiveSubdivideByAngle(rCandidate) : rCandidate);
if(1L == aCandidate.count())
{
// single polygon -> single polygon triangulation
const B2DPolygon aSinglePolygon(aCandidate.getB2DPolygon(0L));
aRetval = triangulate(aSinglePolygon);
}
else
{
Triangulator aTriangulator(aCandidate);
aRetval = aTriangulator.getResult();
}
return aRetval;
}
} // end of namespace triangulator
} // end of namespace basegfx
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|