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();
93 const Vector3Ref p0(workVertex((ear + n - 1) % n));
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){
128 if((a - p).
cross(b - p).
dot(ccs) <= 0){
131 if((b - p).
cross(c - p).
dot(ccs) <= 0){
134 if((c - p).
cross(a - p).
dot(ccs) <= 0){
142 earMask[prev] = earMask[ear] = earMask[next] = 0;
Eigen::Vector3d Vector3Ref
Vector3 cross(const Vector3 &v1, const Vector3 &v2)
double dot(const Vector3 &v1, const Vector3 &v2)