diff --git a/rhinocommon/cs/SampleCsCommands/SampleCsAddNurbsCurve.cs b/rhinocommon/cs/SampleCsCommands/SampleCsAddNurbsCurve.cs index 79d0ef6b..091a8647 100644 --- a/rhinocommon/cs/SampleCsCommands/SampleCsAddNurbsCurve.cs +++ b/rhinocommon/cs/SampleCsCommands/SampleCsAddNurbsCurve.cs @@ -10,11 +10,16 @@ public class SampleCsAddNurbsCurve : Command protected override Result RunCommand(RhinoDoc doc, RunMode mode) { + // The degree must be >= 1 and the number of control points const int degree = 3; + // The order is degree + 1 + const int order = degree + 1; + // The number of control points const int cv_count = 6; + // The number of knots is always (number of control points + degree - 1) const int knot_count = cv_count + degree - 1; - const int order = degree + 1; + // Define the "Euclidean" (world 3-D) locations for the control points. var cvs = new Point3d[cv_count]; cvs[0] = new Point3d(0.0, 0.0, 0.0); cvs[1] = new Point3d(5.0, 10.0, 0.0); @@ -23,21 +28,34 @@ protected override Result RunCommand(RhinoDoc doc, RunMode mode) cvs[4] = new Point3d(20.0, 0.0, 0.0); cvs[5] = new Point3d(25.0, 10.0, 0.0); + // Define the knots. + // Unless you are doing something special, knot vectors should be clamped. + // "Clamped" means the first and last degree many knots are the same. + // In this example the first three knots are 0 and the last three knots are 3. + // The interior knots can have multiplicity from 1 (a "simple" knot) + // to degree (a "full multiplicity") var knots = new double[knot_count]; + // Start with a full multiplicity knot knots[0] = 0.0; knots[1] = 0.0; knots[2] = 0.0; + // Simple interior knot knots[3] = 1.0; + // Simple interior knot knots[4] = 2.0; + // End with a full multiplicity knot knots[5] = 3.0; knots[6] = 3.0; knots[7] = 3.0; + // Create a non-rational NURBS curve var curve = new NurbsCurve(3, false, order, cv_count); + // Set the control points for (int i = 0; i < cv_count; i++) curve.Points.SetPoint(i, cvs[i]); + // Set the knots for (int i = 0; i < knot_count; i++) curve.Knots[i] = knots[i]; diff --git a/rhinocommon/cs/SampleCsCommands/SampleCsNurbsCircle.cs b/rhinocommon/cs/SampleCsCommands/SampleCsNurbsCircle.cs index 6cfb1e08..b8d9561f 100644 --- a/rhinocommon/cs/SampleCsCommands/SampleCsNurbsCircle.cs +++ b/rhinocommon/cs/SampleCsCommands/SampleCsNurbsCircle.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using Rhino; +using Rhino; using Rhino.Commands; using Rhino.Geometry; @@ -12,11 +10,16 @@ public class SampleCsNurbsCircle : Command protected override Result RunCommand(RhinoDoc doc, RunMode mode) { + // The degree must be >= 1 and the number of control points const int degree = 2; + // The order is degree + 1 + const int order = degree + 1; + // The number of control points const int cv_count = 7; + // The number of knots is always (number of control points + degree - 1) const int knot_count = cv_count + degree - 1; - const int order = degree + 1; + // Define the "Euclidean" (world 3-D) locations for the control points. var points = new Point3d[cv_count]; points[0] = new Point3d(2.500, 0.000, 0.000); points[1] = new Point3d(5.000, 0.000, 0.000); @@ -25,7 +28,10 @@ protected override Result RunCommand(RhinoDoc doc, RunMode mode) points[4] = new Point3d(1.250, 2.165, 0.000); points[5] = new Point3d(0.000, 0.000, 0.000); points[6] = new Point3d(2.500, 0.000, 0.000); - + + // Define the weights + // Weights must be > 0. + // In general you should set the first and last weight to 1. var weights = new double[cv_count]; weights[0] = 1.0; weights[1] = 0.5; @@ -35,24 +41,38 @@ protected override Result RunCommand(RhinoDoc doc, RunMode mode) weights[5] = 0.5; weights[6] = 1.0; + // Define the knots. + // Unless you are doing something special, knot vectors should be clamped. + // "Clamped" means the first and last degree many knots are the same. + // In this example the first three knots are 0 and the last three knots are 3. + // The interior knots can have multiplicity from 1 (a "simple" knot) + // to degree (a "full multiplicity") var knots = new double[knot_count]; + // Start with a full multiplicity knot knots[0] = 0.000; knots[1] = 0.000; + // Full multiplicity interior knot knots[2] = 0.333; knots[3] = 0.333; + // Full multiplicity interior knot knots[4] = 0.667; knots[5] = 0.667; + // End with a full multiplicity knot knots[6] = 1.000; knots[7] = 1.000; + // Create a rational NURBS curve var curve = new NurbsCurve(3, true, order, cv_count); + // Set the control points and weights. + // Since our curve is rational, we need homogeneous points (4-D for (var ci = 0; ci < cv_count; ci++) { var cv = new Point4d(points[ci].X * weights[ci], points[ci].Y * weights[ci], points[ci].Z * weights[ci], weights[ci]); curve.Points.SetPoint(ci, cv); } + // Set the knots for (var ki = 0; ki < knot_count; ki++) curve.Knots[ki] = knots[ki];