MEL How-To #90

Back · Previous · Next Maya

How can I determine if a polygon face component is planar?

The following MEL procedure can be used to determine if a polymesh face is planar.

Some error-checking is avoided in the following example code. The downloadable script (at left) contains robust error-checking.

global proc int isPlanar( string $mesh, int $face )
{
  // Get vertices for this face
  string $vertices[] = `polyListComponentConversion -toVertex ( $mesh + ".f[" + $face + "]" )`;

  // Because the -toVertex result is non-expanded, select and get expanded version

  select -r $vertices;
  $vertices = `filterExpand -sm 31 -ex true`;

  // If this face is triangular, it is a given that it is planar.
  if ( size( $vertices ) == 3 )
    return true;

  // Derive normal from first three vertices in face
  float $f0[3] = `xform -ws -q -t $vertices[0]`;
  float $f1[3] = `xform -ws -q -t $vertices[1]`;
  float $f2[3] = `xform -ws -q -t $vertices[2]`;

  vector $v1 = << $f1[0], $f1[1], $f1[2] >> - << $f0[0], $f0[1], $f0[2] >>;
  vector $v2 = << $f2[0], $f2[1], $f2[2] >> - << $f0[0], $f0[1], $f0[2] >>;

  vector $normal = unit( $v1 ^ $v2 );   // cross product

  // For each additional vertex, generate vector from vertex to plane of triangle
  for ( $v = 3; $v < size( $vertices ); $v++ )
  {
    float $f[3] = `xform -ws -q -t $vertices[$v]`;
    vector $vp  = << $f[0] - $f0[0], $f[1] - $f0[1], $f[2] - $f0[2] >>;

    // Take dot-product of vector to normal of plane
    float $dot = $vp * $normal;

    // If distance determined by dot-product is not equal to 0, this face is not planar
    if ( $dot != 0 )
      return false;
  }

  // The face is planar
  return true;
}

Related How-To's

Tuesday, December 05, 2000