Site Tools


Create a NURBS Surface

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

C++

static bool CreateSurfacesExample( CRhinoDoc& doc )
{
  const int bIsRational = false;
  const int dim = 3;
  const int u_degree = 2;
  const int v_degree = 3;
  const int u_cv_count = 3;
  const int v_cv_count = 5;
 
  // The knot vectors do NOT have the 2 superfluous knots
  // at the start and end of the knot vector.  If you are
  // coming from a system that has the 2 superfluous knots,
  // just ignore them when creating NURBS surfaces.
  double u_knot[ u_cv_count + u_degree - 1 ];
  double v_knot[ v_cv_count + v_degree - 1 ];
 
  // make up a quadratic knot vector with no interior knots
  u_knot[0] = u_knot[1] = 0.0;
  u_knot[2] = u_knot[3] = 1.0;
 
  // make up a cubic knot vector with one simple interior knot
  v_knot[0] = v_knot[1] = v_knot[2] = 0.0;
  v_knot[3] = 1.5;
  v_knot[4] = v_knot[5] = v_knot[6] = 2.0;
 
  // Rational control points can be in either homogeneous
  // or euclidean form. Non-rational control points do not
  // need to specify a weight.  
  ON_3dPoint CV[u_cv_count][v_cv_count];
 
  int i, j;
  for ( i = 0; i < u_cv_count; i++ ) {
    for ( j = 0; j < v_cv_count; j++ ) {
      CV[i][j].x = i;
      CV[i][j].y = j;
      CV[i][j].z = i-j;
    }
  }
 
  ON_NurbsSurface nurbs_surface( dim, bIsRational, 
                        u_degree+1, v_degree+1,
                        u_cv_count, v_cv_count );
 
  for ( i = 0; i < nurbs_surface.KnotCount(0); i++ )
    nurbs_surface.SetKnot( 0, i, u_knot[i] );
 
  for ( j = 0; j < nurbs_surface.KnotCount(1); j++ )
    nurbs_surface.SetKnot( 1, j, v_knot[j] );
 
  for ( i = 0; i < nurbs_surface.CVCount(0); i++ ) {
    for ( j = 0; j < nurbs_surface.CVCount(1); j++ ) {
      nurbs_surface.SetCV( i, j, CV[i][j] );
    }
  }
 
  bool ok = false;
  if ( nurbs_surface.IsValid() ) 
  {
    doc.AddSurfaceObject( nurbs_surface );
    doc.Redraw();
    ok = true;
  }
 
  return ok;
}

C#

static bool CreateSurfacesExample(MRhinoDoc doc)
{
  const bool bIsRational = false;
  const int dim = 3;
  const int u_degree = 2;
  const int v_degree = 3;
  const int u_cv_count = 3;
  const int v_cv_count = 5;
 
  // The knot vectors do NOT have the 2 superfluous knots
  // at the start and end of the knot vector.  If you are
  // coming from a system that has the 2 superfluous knots,
  // just ignore them when creating NURBS surfaces.
  double[] u_knot = new double[u_cv_count + u_degree - 1];
  double[] v_knot = new double[v_cv_count + v_degree - 1];
 
  // make up a quadratic knot vector with no interior knots
  u_knot[0] = u_knot[1] = 0.0;
  u_knot[2] = u_knot[3] = 1.0;
 
  // make up a cubic knot vector with one simple interior knot
  v_knot[0] = v_knot[1] = v_knot[2] = 0.0;
  v_knot[3] = 1.5;
  v_knot[4] = v_knot[5] = v_knot[6] = 2.0;
 
  // Rational control points can be in either homogeneous
  // or euclidean form. Non-rational control points do not
  // need to specify a weight.  
  On3dPoint[,] CV = new On3dPoint[u_cv_count, v_cv_count];
 
  for (int i = 0; i < u_cv_count; i++)
  {
    for (int j = 0; j < v_cv_count; j++)
    {
      CV[i, j] = new On3dPoint();
      CV[i, j].x = i;
      CV[i, j].y = j;
      CV[i, j].z = i - j;
    }
  }
 
  OnNurbsSurface nurbs_surface = new OnNurbsSurface(
    dim, 
    bIsRational,
    u_degree + 1,
    v_degree + 1,
    u_cv_count,
    v_cv_count
    );
 
  for (int i = 0; i < nurbs_surface.KnotCount(0); i++)
    nurbs_surface.SetKnot(0, i, u_knot[i]);
 
  for (int j = 0; j < nurbs_surface.KnotCount(1); j++)
    nurbs_surface.SetKnot(1, j, v_knot[j]);
 
  for (int i = 0; i < nurbs_surface.CVCount(0); i++)
  {
    for (int j = 0; j < nurbs_surface.CVCount(1); j++)
      nurbs_surface.SetCV(i, j, CV[i, j]);
  }
 
  bool ok = false;
  if (nurbs_surface.IsValid())
  {
    doc.AddSurfaceObject(nurbs_surface);
    doc.Redraw();
    ok = true;
  }
 
  return ok;
}

VB.NET

Function CreateSurfacesExample(ByRef doc As MRhinoDoc) As Boolean
  Const bIsRational As Boolean = False
  Const dimension As Integer = 3
  Const u_degree As Integer = 2
  Const v_degree As Integer = 3
  Const u_cv_count As Integer = 3
  Const v_cv_count As Integer = 5
 
  ' The knot vectors do NOT have the 2 superfluous knots
  ' at the start and end of the knot vector.  If you are
  ' coming from a system that has the 2 superfluous knots,
  ' just ignore them when creating NURBS surfaces.
  Dim u_knot(u_cv_count + u_degree - 2) As Double
  Dim v_knot(v_cv_count + v_degree - 2) As Double
 
  ' make up a quadratic knot vector with no interior knots
  u_knot(0) = 0.0
  u_knot(1) = 0.0
  u_knot(2) = 1.0
  u_knot(3) = 1.0
 
  ' make up a cubic knot vector with one simple interior knot
  v_knot(0) = 0.0
  v_knot(1) = 0.0
  v_knot(2) = 0.0
  v_knot(3) = 1.5
  v_knot(4) = 2.0
  v_knot(5) = 2.0
  v_knot(6) = 2.0
 
  ' Rational control points can be in either homogeneous
  ' or euclidean form. Non-rational control points do not
  ' need to specify a weight.  
  Dim CV(,) As On3dPoint = New On3dPoint(u_cv_count, v_cv_count) {}
 
  For i As Integer = 0 To u_cv_count - 1
    For j As Integer = 0 To v_cv_count - 1
      CV(i, j) = New On3dPoint()
      CV(i, j).x = i
      CV(i, j).y = j
      CV(i, j).z = i - j
    Next
  Next
 
  Dim nurbs_surface As New OnNurbsSurface(dimension, bIsRational, u_degree + 1, v_degree + 1, u_cv_count, v_cv_count)
 
  For i As Integer = 0 To nurbs_surface.KnotCount(0) - 1
    nurbs_surface.SetKnot(0, i, u_knot(i))
  Next
 
  For j As Integer = 0 To nurbs_surface.KnotCount(1) - 1
    nurbs_surface.SetKnot(1, j, v_knot(j))
  Next
 
  For i As Integer = 0 To nurbs_surface.CVCount(0) - 1
    For j As Integer = 0 To nurbs_surface.CVCount(1) - 1
      nurbs_surface.SetCV(i, j, CV(i, j))
    Next
  Next
 
  Dim ok As Boolean = False
  If (nurbs_surface.IsValid()) Then
    doc.AddSurfaceObject(nurbs_surface)
    doc.Redraw()
    ok = True
  End If
  Return ok
 
End Function


developer/sdksamples/createsurfaceexample.txt ยท Last modified: 2010/08/24 by dale