Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:sdksamples:radiusofcurvature [2015/09/14] (current)
Line 1: Line 1:
 +====== Computing Radius of Curvature ======
 +====== C++ ======
 +> **Version:​** //Rhino 4//
 +
 +=====Question=====
 +How can I compute the radius of curvature of a curve object at a selected location?
 +
 +=====Answer=====
 +The following example code demonstrates how to compute the radius of curvature of a ON_Curve object. This example uses a a combination of ON_Curve::​TangentAt and ON_Curve::​CurvatureAt. But, we could have also used ON_Curve::​EvCurvature. See opennurbs_curve.h for details on these functions.
 +
 +
 +<code c++>
 +CRhinoCommand::​result CCommandTest::​RunCommand( const CRhinoCommandContext&​ context )
 +{
 +  CRhinoGetObject go;
 +  go.SetCommandPrompt( L"​Select curve for curvature measurement"​ );
 +  go.SetGeometryFilter( CRhinoGetObject::​curve_object );
 +  go.GetObjects( 1, 1 );
 +  if( go.CommandResult() != success )
 +    return go.CommandResult();​
 +
 +  const ON_Curve* crv = go.Object(0).Curve();​
 +  if( 0 == crv )
 +    return failure;
 +
 +  CRhinoGetPoint gp;
 +  gp.SetCommandPrompt( L"​Select point on curve for curvature measurement"​ );
 +  gp.Constrain( *crv );
 +  gp.GetPoint();​
 +  if( gp.CommandResult() != success )
 +    return gp.CommandResult();​
 +
 +  ON_3dPoint pt = gp.Point();
 +
 +  double t = 0.0;
 +  if( !crv->​GetClosestPoint(pt,​ &t) )
 +  {
 +    RhinoApp().Print( L"​Failed to compute radius of curvature.\n"​ );
 +    return failure;
 +  }
 +
 +  ON_3dVector tangent = crv->​TangentAt( t );
 +  if( tangent.IsTiny() )
 +  {
 +    RhinoApp().Print( L"​Failed to compute radius of curvature. Curve may have stacked control points.\n"​ );
 +    return failure;
 +  }
 +
 +  ON_3dVector curvature = crv->​CurvatureAt( t );
 +  const double k = curvature.Length();​
 +  if( k < ON_SQRT_EPSILON )
 +  {
 +    RhinoApp().Print( L"​Radius of curvature: infinite.\n"​ );
 +    return failure;
 +  }
 +
 +  ON_3dVector radius_vector = curvature / (k * k); 
 +  ON_Circle circle;
 +  if ( !circle.Create(pt,​ tangent, pt + 2.0 * radius_vector) )
 +  {
 +    RhinoApp().Print( L"​Failed to compute radius of curvature.\n"​ );
 +    return failure;
 +  }
 +
 +  context.m_doc.AddCurveObject( circle );
 +  context.m_doc.AddPointObject( pt );
 +  context.m_doc.Redraw();​
 +
 +  ON_wString wRadius;
 +  RhinoFormatNumber( circle.Radius(),​ wRadius );
 +  RhinoApp().Print( L"​Radius of curvature: %s.\n",​ wRadius );
 +
 +  return success;
 +}
 +
 +</​code>​
 +
 +
 +{{tag>​Developer cplusplus}}
  
developer/sdksamples/radiusofcurvature.txt ยท Last modified: 2015/09/14 (external edit)