GetPolygonFaceArea MEL

From scripting
Jump to: navigation, search
global proc float getPolygonFaceArea( string $mesh, int $face ) {
	float $area = 0;
	select -r ($mesh+".f["+$face+"]");
	string $vertIndicesStr[] = `polyInfo -fv`;
	string $vertIndices[];
	tokenize $vertIndicesStr[0] $vertIndices;
	int $vertCount = `size($vertIndices)` - 2;
	// triangle
	if ($vertCount == 3) {
		string $triangle[3] = {$vertIndices[2], $vertIndices[3], $vertIndices[4]};
		$area = getPolyTriangleArea($mesh, $triangle);
	}
	// quad (decompose into two triangles)
	else if ($vertCount == 4) {
		string $triangle1[3] = {$vertIndices[2], $vertIndices[3], $vertIndices[4]};
		string $triangle2[3] = {$vertIndices[2], $vertIndices[4], $vertIndices[5]};
		$area = getPolyTriangleArea($mesh, $triangle1) + getPolyTriangleArea($mesh, $triangle2);
	}
	// n-gon (decompose into n - 2 triangles where n = # of verts)
	else {
		int $i;
		for ($i=0; $i < $vertCount-2; $i++) {
			string $triangle[3] = {$vertIndices[2], $vertIndices[$i+3], $vertIndices[$i+4]};
			$area += getPolyTriangleArea($mesh, $triangle);
		}
	}
	return $area;
}


(Note, this subroutine was greatly influenced by a contributor named YourDaftPunk on CGTalk, located here)


More information on vectors and arrays, read pages 12-16 in YSYT.