20 int Triangulator::apply(
const vector<int>& polygon)
24 unsigned int numOrgVertices = polygon.size();
26 if(numOrgVertices > earMask.size()){
27 earMask.resize(numOrgVertices);
30 if(numOrgVertices < 3){
32 }
else if(numOrgVertices == 3){
33 triangles_.push_back(0);
34 triangles_.push_back(1);
35 triangles_.push_back(2);
39 orgPolygon = &polygon;
41 workPolygon.resize(numOrgVertices);
42 for(
unsigned int i=0;
i < numOrgVertices; ++
i){
48 for(
unsigned int i=1;
i < numOrgVertices - 1; ++
i){
49 ccs += (vertex(
i) - o).
cross(vertex((
i+1) % numOrgVertices) - o);
55 int n = workPolygon.size();
60 for(
int i=0;
i <
n; ++
i){
62 if(convexity == FLAT){
65 }
else if(convexity == CONVEX){
66 if(!checkIfEarContainsOtherVertices(
i)){
67 triangles_.push_back(workPolygon[(
i +
n - 1) %
n]);
68 triangles_.push_back(workPolygon[
i]);
69 triangles_.push_back(workPolygon[(
i + 1) %
n]);
79 for(
int i = target + 1;
i <
n; ++
i){
80 workPolygon[target++] = workPolygon[
i];
82 workPolygon.pop_back();
91 int n = workPolygon.size();
95 Vector3 b(workVertex((ear + 1) %
n) - p0);
100 if((ccs.norm() /
a.norm() +
b.norm()) < 1.0e-4){
103 convexity = (this->ccs.dot(ccs) > 0.0) ? CONVEX : CONCAVE;
110 bool Triangulator::checkIfEarContainsOtherVertices(
int ear)
112 bool contains =
false;
114 const int n = workPolygon.size();
117 const int prev = (ear +
n -1) %
n;
118 const int next = (ear+1) %
n;
123 earMask[prev] = earMask[ear] = earMask[next] = 1;
125 for(
unsigned int i=0;
i < workPolygon.size(); ++
i){
142 earMask[prev] = earMask[ear] = earMask[next] = 0;