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
|
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Current versions can be found at www.libavg.de
//
//
// Based on Poly2Tri algorithm.
// Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
// http://code.google.com/p/poly2tri/
//
#include "Triangulate.h"
#include "Sweep.h"
#include "SweepContext.h"
#include "Shapes.h"
using namespace std;
namespace avg {
void triangulatePolygon(std::vector<unsigned int>& dest, const Vec2Vector& points,
const std::vector<unsigned int>& holeIndexes)
{
std::vector<Point*> polyline;
std::vector<Point*> holeLine;
unsigned int contourEnd;
if (holeIndexes.size() > 0) {
contourEnd = holeIndexes[0];
} else {
contourEnd = points.size();
}
for (unsigned int i = 0; i < contourEnd; i++) {
polyline.push_back(new Point(points[i].x, points[i].y, i));
}
SweepContext* sweepContext = new SweepContext(polyline);
Sweep* sweep = new Sweep;
if (holeIndexes.size() > 0) {
for (unsigned int i = 0; i < holeIndexes.size(); i++) {
if ( i < holeIndexes.size()-1) {
for (unsigned int j = holeIndexes[i]; j < points.size() && j <
holeIndexes[i+1]; j++)
{
holeLine.push_back(new Point(points[j].x, points[j].y, j));
}
} else {
for (unsigned int j = holeIndexes[i]; j < points.size(); j++) {
holeLine.push_back(new Point(points[j].x, points[j].y, j));
}
}
sweepContext->addHole(holeLine);
holeLine.clear();
}
}
sweep->Triangulate(*sweepContext);
std::vector<avg::TriangulationTriangle*>& triangles = sweepContext->getTriangles();
for (unsigned int i = 0; i < triangles.size(); ++i) {
dest.push_back(triangles[i]->getPoint(0)->m_Index);
dest.push_back(triangles[i]->getPoint(1)->m_Index);
dest.push_back(triangles[i]->getPoint(2)->m_Index);
}
delete sweep;
delete sweepContext;
for (unsigned int i = 0; i < polyline.size(); i++) {
delete polyline[i];
}
}
}
|