Skip to content

Commit

Permalink
fix solvespace#259: 'Cannot allocate memory' when exporting mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
Evil-Spirit committed May 22, 2017
1 parent 33b6e51 commit 46e95ff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/mesh.cpp
Expand Up @@ -601,6 +601,10 @@ void SKdNode::SnapToVertex(Vector v, SMesh *extras) {
if(tr->b.Equals(v)) { tr->b = v; continue; }
if(tr->c.Equals(v)) { tr->c = v; continue; }

if(tr->IsDegenerate()) {
continue;
}

if(v.OnLineSegment(tr->a, tr->b)) {
STriangle nt = STriangle::From(tr->meta, tr->a, v, tr->c);
extras->AddTriangle(&nt);
Expand Down Expand Up @@ -632,6 +636,9 @@ void SKdNode::SnapToMesh(SMesh *m) {
int i, j, k;
for(i = 0; i < m->l.n; i++) {
STriangle *tr = &(m->l.elem[i]);
if(tr->IsDegenerate()) {
continue;
}
for(j = 0; j < 3; j++) {
Vector v = tr->vertices[j];

Expand Down Expand Up @@ -1117,10 +1124,7 @@ void SMesh::PrecomputeTransparency() {

void SMesh::RemoveDegenerateTriangles() {
for(auto &tr : l) {
bool isDegenerate = tr.a.OnLineSegment(tr.b, tr.c) ||
tr.b.OnLineSegment(tr.a, tr.c) ||
tr.c.OnLineSegment(tr.a, tr.b);
tr.tag = (int)isDegenerate;
tr.tag = (int)tr.IsDegenerate();
}
l.RemoveTagged();
}
6 changes: 6 additions & 0 deletions src/polygon.cpp
Expand Up @@ -87,6 +87,12 @@ double STriangle::SignedVolume() const {
return a.Dot(b.Cross(c)) / 6.0;
}

bool STriangle::IsDegenerate() const {
return a.OnLineSegment(b, c) ||
b.OnLineSegment(a, c) ||
c.OnLineSegment(a, b);
}

void STriangle::FlipNormal() {
swap(a, b);
swap(an, bn);
Expand Down
1 change: 1 addition & 0 deletions src/polygon.h
Expand Up @@ -191,6 +191,7 @@ class STriangle {
bool Raytrace(const Vector &rayPoint, const Vector &rayDir,
double *t, Vector *inters) const;
double SignedVolume() const;
bool IsDegenerate() const;
};

class SBsp2 {
Expand Down

0 comments on commit 46e95ff

Please sign in to comment.