Site Tools


Creating NURBS Surfaces

Developer: C++, .NET
Summary: Demonstrates how to create a NURBS surface using the Rhino SDK

Question

Given a set of control points, how can I create an open NURBS surface?

Example

C++

// The maximum NURBS degree for curves or sufaces.
#define RhMaxNurbsDegree 11
 
ON_NurbsSurface* CreateOpenNurbsSurface(ON_ClassArray<ON_3dPointArray> cvpoints, int degreeU, int degreeV )
{
  //declare the NURBS surface
  ON_NurbsSurface* ns = 0;
 
  int cv_countU = cvpoints.Count();
  if (cv_countU < 2)
      return ns;
 
  int cv_countV = cvpoints[0].Count();
  if( cv_countV < 2 )
      return ns;
 
  //Make sure degree is within reasonable range
  if (degreeU < 1 || degreeU > RhMaxNurbsDegree )
      degreeU = 3;
  if (degreeV < 1 || degreeV > RhMaxNurbsDegree )
      degreeV = 3;
 
  //Get order (order = degree+1)
  //Make sure that the order is not greated than CV count
  int orderU = degreeU + 1;
  if( orderU > cv_countU )
      orderU = cv_countU;
  int orderV = degreeV + 1;
  if( orderV > cv_countV )
      orderV = cv_countU;
 
  //Make the surface
  ns = ON_NurbsSurface::New(3, false, orderU, orderV, cv_countU, cv_countV);
 
  //Add cv points
  int i;
  int j;
  ON_3dPoint pt = ON_UNSET_POINT;
  for( i = 0; i< cv_countU; i++ )
  {
      for( j = 0; j < cv_countV; j++ )
      {
          pt = cvpoints[i][j];
          //RhinoApp().ActiveDoc()->AddPointObject(pt);
          ns->SetCV(i, j, pt);
      }
  }
 
  //Set knots for open/closed surface
  ns->MakeClampedUniformKnotVector(0);
  ns->MakeClampedUniformKnotVector(1);
 
  return ns;
}

VB.NET

  Function CreateOpenNurbsSurface(ByVal cvpoints As List(Of On3dPointArray), ByVal degreeU As Integer, ByVal degreeV As Integer) As OnNurbsSurface
 
      Const RhMaxNurbsDegree As Integer = 11
 
      Dim cv_countU As Integer
      cv_countU = cvpoints.Count()
      If (cv_countU < 2) Then
          Return Nothing
      End If
 
      Dim cv_countV As Integer
      cv_countV = cvpoints(0).Count()
      If (cv_countV < 2) Then
          Return Nothing
      End If
 
      'Make sure degree is within reasonable range
      If (degreeU < 1 Or degreeU > RhMaxNurbsDegree) Then
          degreeU = 3
      End If
      If (degreeV < 1 Or degreeV > RhMaxNurbsDegree) Then
          degreeV = 3
      End If
 
      'Make the surface
      Dim ns As New OnNurbsSurface
      Dim orderU As Integer = degreeU + 1
      If (orderU > cv_countU) Then
          orderU = cv_countU
      End If
      Dim orderV As Integer = degreeV + 1
      If (orderV > cv_countV) Then
          orderV = cv_countV
      End If
      ns.Create(3, False, orderU, orderV, cv_countU, cv_countV)
 
      'Add cv points
      Dim i As Integer
      Dim j As Integer
      Dim pt As On3dPoint
      For i = 0 To cv_countU - 1
          For j = 0 To cv_countV - 1
              pt = cvpoints(i)(j)
              'RhinoApp().ActiveDoc().AddPointObject(pt)
              ns.SetCV(i, j, pt)
          Next
      Next
 
      'Set knots for open surface
      ns.MakeClampedUniformKnotVector(0)
      ns.MakeClampedUniformKnotVector(1)
 
      CreateNurbsSurface_WithoutKnotsOrWeights = ns
 
  End Function


developer/sdksamples/createnurbssurface.txt ยท Last modified: 2015/11/25 by sandy