Remove vertices that are too close to each other

Let us know if you have a nice R.U.B.E script to share!
Post Reply
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Remove vertices that are too close to each other

Post by iforce2d »

Code: Select all

float minDist = 0.1;

bool selectOnly = false;

fixture[] fs = sf();
for (uint i = 0; i < fs.length; i++) {
	fixture f = fs[i];
	vertex[] vs = f.getVertices();
	uint[] indices;
	for (uint k = 0; k < vs.length; k++) {
		vertex v0 = vs[k];
		vertex v1 = vs[ (k+1) % vs.length ];
		float d = v0.pos.distanceTo( v1.pos );
		if ( d < minDist ) {
			indices.insertLast( k );
			if ( selectOnly )
				v1.select();
		}
	}
	
	if ( selectOnly )
		continue;

	// iterate from highest to lowest to preserve index validity
	uint numVerticesBefore = f.getNumVertices();
	for ( int m = indices.length - 1; m >= 0; m-- )
		f.deleteVertex( indices[m] );
	uint removedCount = numVerticesBefore - f.getNumVertices();
	if ( removedCount > 0 ) 
		print( "Removed "+removedCount+" vertices from fixture "+f.getName() );	
}
Post Reply