Site Tools


Comparing Planar Surfaces and Planes

Summary: Demonstrates how to see if a planar surface lies in a specific plane.
Notice: The Rhino.NET SDK is deprecated in Rhino 5. This example adapted for the new RhinoCommon SDK is here.

Question

How can I test if a planar surface lies in a specific plane?

Answer

Unlike curve objects, which implement the ON_Curve::IsInPlane virtual function, there is no function on ON_Surface derive objects that will test for this situation. But, if you obtain the corners of the surface object, you can test to see if they are within a specified tolerance of test plane. The following example code demonstrates how to do this.

C++

static bool IsSurfaceInPlane( const ON_Surface& srf, 
                              const ON_Plane& plane, 
                              double tolerance = ON_ZERO_TOLERANCE )
{
  // Is the surface planar?
  ON_Plane srf_plane;
  if( !srf.IsPlanar(&srf_plane, tolerance) )
    return false;
 
  // Get the surface corner points and project them
  // onto the surface plane
  ON_BoundingBox bbox = srf.BoundingBox();
  ON_3dPoint corners[4];
  corners[0] = srf_plane.ClosestPointTo(bbox.Corner(0,0,0));
  corners[1] = srf_plane.ClosestPointTo(bbox.Corner(1,0,0));
  corners[2] = srf_plane.ClosestPointTo(bbox.Corner(1,1,0));
  corners[3] = srf_plane.ClosestPointTo(bbox.Corner(0,1,0));
 
  // See if the surface corner points are within
  // tolerance of the test plane.
  bool rc = true;
  for( int i=0; rc && i<4; i++ )
  {
    if( fabs(plane.DistanceTo(corners[i])) > tolerance )
      rc = false;
  }
  return rc;
}

VB.NET

Public Function IsSurfaceInPlane( _
        ByVal srf As OnSurface, _
        ByVal plane As OnPlane, _
        ByVal tolerance As Double) _
        As Boolean
 
  ' Is the surface planar?
  Dim srf_plane As New OnPlane
  If (srf.IsPlanar(srf_plane, tolerance) = False) Then
    Return False
  End If
 
  ' Get the surface corner points and project them
  ' onto the surface plane
  Dim bbox As OnBoundingBox = srf.BoundingBox()
  Dim corners(4) As On3dPoint
  corners(0) = srf_plane.ClosestPointTo(bbox.Corner(0, 0, 0))
  corners(1) = srf_plane.ClosestPointTo(bbox.Corner(1, 0, 0))
  corners(2) = srf_plane.ClosestPointTo(bbox.Corner(1, 1, 0))
  corners(3) = srf_plane.ClosestPointTo(bbox.Corner(0, 1, 0))
 
  ' See if the surface corner points are within
  ' tolerance of the test plane.
  Dim rc As Boolean = True
  For i As Integer = 0 To 3
    If (Math.Abs(plane.DistanceTo(corners(i))) > tolerance) Then
      rc = False
    End If
  Next
  Return rc
 
End Function

C#

public bool IsSurfaceInPlane(
        OnSurface srf, 
        OnPlane plane, 
        double tolerance
        )
{
  // Is the surface planar?
  OnPlane srf_plane = new OnPlane();
  if (!srf.IsPlanar(srf_plane, tolerance))
    return false;
 
  // Get the surface corner points and project them
  // onto the surface plane
  OnBoundingBox bbox = srf.BoundingBox();
  On3dPoint[] corners = new On3dPoint[4];
  corners[0] = srf_plane.ClosestPointTo(bbox.Corner(0, 0, 0));
  corners[1] = srf_plane.ClosestPointTo(bbox.Corner(1, 0, 0));
  corners[2] = srf_plane.ClosestPointTo(bbox.Corner(1, 1, 0));
  corners[3] = srf_plane.ClosestPointTo(bbox.Corner(0, 1, 0));
 
  // See if the surface corner points are within
  // tolerance of the test plane.
  bool rc = true;
  for (int i = 0; rc && i < 4; i++)
  {
    if (Math.Abs(plane.DistanceTo(corners[i])) > tolerance)
      rc = false;
  }
  return rc;
}
developer/sdksamples/issurfaceinplane.txt ยท Last modified: 2016/02/11 by sandy