developer:sdksamples:meshmeshintersection


Intersecting Meshes

C++

Summary: Demonstrates how to intersect two meshes using the Rhino SDK.

Question

I would like to calculate the polylines that would be created if you intersected two mesh objects. I see the the SDK has a ON_MeshMeshIntersect function, which does calculate the intersection of two meshes. But, I am unable to interpret the output. Any example that shows how to do this would be helpful.

Answer

The following function demonstrates how you use ON_MeshMeshIntersect to find the polylines that result from intersecting two meshes.

 /*
Description:
  Intersects two meshes.
Parameters:
  meshA     - [in]  The first mesh.
  meshB     - [in]  The second mesh.
  OutCurves - [out] The resulting polyline curves.
                    NOTE: THE CALLER IS RESPONSIBLE FOR 
                    DESTROYING THESE CURVES.
  tolerance - [in]  The intersection tolerance.
Returns:
  True if successful, false otherwise.
*/
static bool MeshMeshIntersection( 
        const ON_Mesh* meshA, 
        const ON_Mesh* meshB, 
        ON_SimpleArray<ON_Curve*>& OutCurves, 
        double tolerance = ON_ZERO_TOLERANCE 
        )
{
  if( 0 == meshA | 0 == meshB )
    return false;
 
  ON_ClassArray<ON_MMX_Polyline> transverse; 
  ON_ClassArray<ON_MMX_Polyline> overlap;
  bool rc = ON_MeshMeshIntersect( meshA, meshB, transverse, overlap, tolerance, tolerance );
  if( !rc )
    return rc;
 
  ON_ClassArray<ON_MMX_Polyline>& arr = transverse;
  int i, j, k;
  for( i = 0; i < 2; i++ )
  {
    if( i == 1 )
      arr = overlap;
 
    int ct = arr.Count();
    for( j = 0; j < ct; j++ )
    {
      ON_MMX_Polyline& mmxpline = arr[j];
      if( 0 == mmxpline.Count() )
        continue;
 
      ON_Polyline polyline;
      for( k = 0; k < mmxpline.Count(); k++ )
      {
        double x = mmxpline[k].m_A.m_P.x;
        double y = mmxpline[k].m_A.m_P.y;
        double z = mmxpline[k].m_A.m_P.z;
        polyline.Append( ON_3dPoint(x,y,z) );
      }
 
      if( false == polyline.IsValid() )
        continue;
 
      ON_PolylineCurve* polyline_crv = new ON_PolylineCurve( polyline );
      if( 0 == polyline_crv )
        continue;
 
      OutCurves.Append( polyline_crv );
    }
  }
 
  return ( OutCurves.Count() ) ? true : false;
}

You can use the above function as follows:

CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select meshes to intersect" );
  go.SetGeometryFilter( CRhinoGetObject::mesh_object );
  go.GetObjects( 2, 2 );
  if( go.CommandResult() != success )
    return go.CommandResult();
 
  const ON_Mesh* meshA = go.Object(0).Mesh();
  const ON_Mesh* meshB = go.Object(1).Mesh();
  if( 0 == meshA | 0 == meshA )
    return failure;
 
  ON_SimpleArray<ON_Curve*> OutCurves;
  bool rc = MeshMeshIntersection( meshA, meshB, OutCurves );
  if( rc )
  {
    int i;
    for( i = 0; i < OutCurves.Count(); i++ )
    {
      CRhinoCurveObject* curve_obj = new CRhinoCurveObject();
      curve_obj->SetCurve( OutCurves[i] );
      context.m_doc.AddObject( curve_obj );
    }
    context.m_doc.Redraw();
  }
 
  return success;
}
developer/sdksamples/meshmeshintersection.txt · Last modified: 2010/01/26 16:37 (external edit) Driven by DokuWiki Recent changes RSS feed

 © 1997-2010 

McNeel North America Europe Latin AmericaAsia