Skip to content

Commit

Permalink
triangulate fans locally.
Browse files Browse the repository at this point in the history
  • Loading branch information
phkahler committed Jul 18, 2020
1 parent 188b2e2 commit 316b8c3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/polygon.h
Expand Up @@ -131,7 +131,7 @@ class SContour {
void FindPointWithMinX();
Vector AnyEdgeMidpoint() const;

bool IsEar(int bp, double scaledEps) const;
bool IsEar(int ap, int bp, int cp, double scaledEps) const;
bool BridgeToContour(SContour *sc, SEdgeList *el, List<Vector> *vl);
void ClipEarInto(SMesh *m, int bp, double scaledEps);
void UvTriangulateInto(SMesh *m, SSurface *srf);
Expand Down
73 changes: 68 additions & 5 deletions src/srf/triangulate.cpp
Expand Up @@ -213,9 +213,7 @@ bool SContour::BridgeToContour(SContour *sc,
return true;
}

bool SContour::IsEar(int bp, double scaledEps) const {
int ap = WRAP(bp-1, l.n),
cp = WRAP(bp+1, l.n);
bool SContour::IsEar(int ap, int bp, int cp, double scaledEps) const {

STriangle tr = {};
tr.a = l[ap].p;
Expand Down Expand Up @@ -305,17 +303,82 @@ void SContour::UvTriangulateInto(SMesh *m, SSurface *srf) {
}
l.RemoveTagged();

// Handle simple triangle fans all at once.
if(srf->degm == 1 && srf->degn == 1) {
l.ClearTags();
int j=0;
int pstart = 0;
double elen = -1.0;
double oldspan = 0.0;
for(i = 1; i < l.n; i++) {
Vector ab = l[i].p.Minus(l[i-1].p);
// first time just measure the segment
if (elen < 0.0) {
elen = ab.Dot(ab);
oldspan = elen;
j = 1;
continue;
}
// check for consecutive segments of similar size which are also
// ears and where the group forms a convex ear
bool end = false;
double ratio = ab.Dot(ab) / elen;
if ((ratio < 0.25) || (ratio > 4.0)) end = true;

double slen = l[pstart].p.Minus(l[i].p).MagSquared();
if (slen < oldspan) end = true;

if (!IsEar(i-2, i-1, i, scaledEps) ) end = true;
if ((j>0) && !IsEar(pstart, i-1, i, scaledEps)) end = true;
if (i == l.n-1) end = true;

if (!end) {
j++;
oldspan = slen;
} else {
if (j > 3) {
Vector center = l[pstart+1].p.Plus(l[pstart+j-1].p).ScaledBy(0.5);
for (int x=0; x<j; x++) {
STriangle tr = {};
tr.a = center;
tr.b = l[pstart+x].p;
tr.c = l[pstart+x+1].p;
m->AddTriangle(&tr);
}
for (int x=1; x<j; x++) {
l[pstart+x].tag = 1;
}
STriangle tr = {};
tr.a = center;
tr.b = l[pstart+j].p;
tr.c = l[pstart].p;
m->AddTriangle(&tr);
}
// pstart += j;
pstart = i-1;
elen = ab.Dot(ab);
oldspan = elen;
j = 1;
}
}
l.RemoveTagged();
}

// Now calculate the ear-ness of each vertex
for(i = 0; i < l.n; i++) {
(l[i]).ear = IsEar(i, scaledEps) ? EarType::EAR : EarType::NOT_EAR;
int ap = WRAP(i-1, l.n),
cp = WRAP(i+1, l.n);
(l[i]).ear = IsEar(ap, i, cp, scaledEps) ? EarType::EAR : EarType::NOT_EAR;
}

bool toggle = false;
while(l.n > 3) {
// Some points may have changed ear-ness, so recalculate
for(i = 0; i < l.n; i++) {
int ap = WRAP(i-1, l.n),
cp = WRAP(i+1, l.n);
if(l[i].ear == EarType::UNKNOWN) {
(l[i]).ear = IsEar(i, scaledEps) ?
(l[i]).ear = IsEar(ap, i, cp, scaledEps) ?
EarType::EAR : EarType::NOT_EAR;
}
}
Expand Down

0 comments on commit 316b8c3

Please sign in to comment.