Making the Pixel Printer I ran into a problem when trying to create an STL file from all the little cube geometries. The problem was that even after merging the geometries, there where still many interior faces which messed up the interior fill of the 3D printer.

My solution was to run through the faces and find any that share all the same vertices. Since there is no array equality testing in javascript, I put the indices of the face vertices (a, b, c, d) in an array and used a trick from this Stackoverflow question to compare the arrays:

// http://stackoverflow.com/questions/7593590/how-to-detect-array-equality-in-javascript

function isSame(a1, a2){

return !(a1.sort() > a2.sort() || a1.sort() < a2.sort());

}

Using that trick it was pretty easy to go through the faces, marking duplicates as undefined, then filtering the resulting array to remove the undefined elements. I know I could have built the array at each step, saving the double iteration, but I think it is more clear what is going on this way.

function removeDuplicateFaces(geometry){

for(var i=0; i<geometry.faces.length; i++){

var tri = geometry.faces[i];

var inds = [tri.a, tri.b, tri.c, tri.d].sort();

for(var j=0; j<i; j++){

var tri_2 = geometry.faces[j];

if( tri_2 !== undefined ){ // May have already been deleted

var inds_2 = [tri_2.a, tri_2.b, tri_2.c, tri_2.d].sort();

if( isSame( inds, inds_2 ) ){

delete geometry.faces[i]; // Sets these faces to undefined

// If duplicate, it is also interior, so remove both

delete geometry.faces[j];

}

}

}

}

geometry.faces = geometry.faces.filter( function(a){ return a===undefined });

return geometry;

}