Adding a NURBS Curve from Control Points

This guide demonstrates two ways to create a clamped NURBS curve from a set of control points using C/C++.

Overview

Imagine you would like to create a NURBS curve from a set of control points, such that it looks like this:

NURBS Curve Control Points

There are two methods to achieve this…

Method 1

CRhinoCommand::result CCommandTest::RunCommand(
        const CRhinoCommandContext& context )
{
  ON_3dPointArray points;
  points.Append( ON_3dPoint(0, 0, 0) );
  points.Append( ON_3dPoint(0, 2, 0) );
  points.Append( ON_3dPoint(2, 4, 0) );
  points.Append( ON_3dPoint(4, 2, 0) );
  points.Append( ON_3dPoint(4, 0, 0) );

  ON_NurbsCurve* nc = ON_NurbsCurve::New();
  nc->CreateClampedUniformNurbs( 3, 4, points.Count(), points );

  if( nc->IsValid() )
  {
    context.m_doc.AddCurveObject( *nc );
    context.m_doc.Redraw();
  }

  RhinoApp().ActiveDoc()->Redraw();
  return CRhinoCommand::success;
}

Method 2

CRhinoCommand::result CCommandTest::RunCommand(
        const CRhinoCommandContext& context )
{
  ON_3dPointArray points;
  points.Append( ON_3dPoint(0, 0, 0) );
  points.Append( ON_3dPoint(0, 2, 0) );
  points.Append( ON_3dPoint(2, 4, 0) );
  points.Append( ON_3dPoint(4, 2, 0) );
  points.Append( ON_3dPoint(4, 0, 0) );

  int dimension = 3;
  bool bIsRat = false;
  int order = 4;
  int cv_count = points.Count();

  ON_NurbsCurve* nc = ON_NurbsCurve::New(dimension, bIsRat, order, cv_count);
  if( !nc )
        return CRhinoCommand::failure;

  //Set CV points
  nc->ReserveCVCapacity( cv_count );
  for( int i = 0; i < points.Count(); i++ )
  {
        nc->SetCV(i, points[i] );
  }

  //Set Knots
  nc->ReserveKnotCapacity( order+cv_count-2 );
  ON_MakeClampedUniformKnotVector( order, cv_count, nc->m_knot );

  if( nc->IsValid() )
  {
    context.m_doc.AddCurveObject( *nc );
    context.m_doc.Redraw();
  }

  RhinoApp().ActiveDoc()->Redraw();
  return CRhinoCommand::success;
}