Enumerating Polygon Islands

 
 
 

This example demonstrates how to enumerate polygon islands by implementing a function that returns an array of all the polygon islands, each one as a PolygonFaceCollection.

JScript Example: enumerating polygon islands

function GetIslands(in_obj) 
{
	var arrIslands = new Array();
	var plyPolygons = in_obj.ActivePrimitive.Geometry.Polygons;
	var arrSeen = new Array(plyPolygons.Count);					// for optimization

	for ( var i=0; i<plyPolygons.Count; i++ ) {
		// Skip any we've already seen
		if ( arrSeen[i] ) { continue; }
		
		// Stuff the current polygon into an XSICollection (custom 
		// filters only accept and return XSICollections)
		var xsiCurrPolygons = new ActiveXObject("XSI.Collection");
		xsiCurrPolygons.Add(plyPolygons(i));

		// Activate and apply the filter 
		var fltPolyIslandFilter = Application.Filters("Polygon_Island");
		var xsiCurrIslands = fltPolyIslandFilter.Subset(xsiCurrPolygons);
		
		// To return an actual PolygonFaceCollection instead of an
		// XSICollection, we can convert it via the SubComponent 
		var plyCurrIslands = Dictionary.GetObject(xsiCurrIslands).SubComponent.ComponentCollection;
		
		// Indicate that we've see all of these components (optimization) 
		for ( var j=0; j<plyCurrIslands.Count; j++ ) {
			arrSeen[plyCurrIslands(j).Index] = 1;
		}
		
		// Add the PolygonFaceCollection of polygon islands to the return array
		arrIslands.push(plyCurrIslands);
	}

	return arrIslands;
}
 
 
// Open the club bot sample scene and select the feet polygon mesh
var strClubBot = XSIUtils.BuildPath(
	Application.InstallationPath(siFactoryPath),
	"Data", "XSI_SAMPLES", "Scenes", "club_bot.scn"
);
OpenScene(strClubBot, false);
SelectObj("club_bot.BotFeet");

// Using this in a tool, you might use the selection:
var e = new Enumerator(Selection);
for ( ; !e.atEnd(); e.moveNext() ) {
	var obj = e.item();
	var arrPolyIslands = GetIslands(obj);
	Application.LogMessage( "# of polygon islands: " + arrPolyIslands.length );
}

// Expected results:
// INFO : # of arrPolyIslands: 2