Kernel SDK Developer's Guide > Working with the Ge Library > Working with Curves > Working with NURBS Curves
Working with NURBS Curves

Using the Ge library you can create and modify NURBS curves in 2D and 3D space represented by the OdGeNurbCurve2d and OdGeNurbCurve3d classes respectively. This topic describes the most commonly used operations with 3D NURBS curves; the functionality of 2D curves is similar to that of 3D.

In the Ge library, NURBS splines can be defined by control points and fit points.

Creating NURBS curves

To create an OdGeNurbCurve3d object, use the appropriate constructor from the constructors list:

  • OdGeNurbCurve3d() – The default constructor. Creates an empty spline object.
  • OdGeNurbCurve3d(const OdGeNurbCurve3d& source) – Creates an object copied from the source object.
  • OdGeNurbCurve3d::OdGeNurbCurve3d(int degree, const OdGeKnotVector& knots, const OdGePoint3dArray& controlPoints, bool isPeriodic = false) – Creates a 3D non-rational spline with a given knot vector and control points. The isPeriodic flag defines the periodicity of the curve. If it's false, the length of the knot vector must be greater than the length of the control array by degree+1. If isPeriodic is true, a closed spline will be created, the knot vector and control points array must have the same length, and the first and last control points must be identical.
  • OdGeNurbCurve3d(int degree, const OdGeKnotVector& knots, const OdGePoint3dArray& controlPoints, const OdGeDoubleArray& weights, bool isPeriodic = false) – Creates a 3D rational spline with a given knot vector, control points and weights. The control points array and weights array must have the same length. The isPeriodic flag defines the periodicity of the curve. If it's false, the length of the knot vector must be greater than the length of the control array by degree+1. If isPeriodic is true, a closed spline will be created, the knot vector array and the control points array must have the same length, and the first and last control points as well as the corresponding weights must be identical.
  • OdGeNurbCurve3d(int degree, const OdGeKnotVector& knots, const OdGePoint3d* controlPoints, OdUInt32 numControlPoints, const double* weights, OdUInt32 numWeights, bool isPeriodic = false) – Creates a 3D rational spline with given arrays of control points, corresponding weights, and number of control points and weights used for calculating the spline.
  • OdGeNurbCurve3d(int degree, const OdGePolyline3d& fitPolyline, bool isPeriodic = false) – Creates a 3D rational spline with a specified degree and fit points from a polyline.
  • OdGeNurbCurve3d(const OdGePoint3dArray& fitPoints, const OdGeTol& fitTolerance = OdGeContext::gTol) – Creates a 3D spline interpolating a given array of fit points within a given tolerance.
  • OdGeNurbCurve3d(const OdGePoint3dArray& fitPoints, const OdGeVector3dArray& fitTangents, const OdGeTol& fitTolerance = OdGeContext::gTol, bool isPeriodic = false) – Creates a 3D spline interpolating a given array of fit points with array of vectors interpolating the curve's derivatives at fit points. Constructor also uses a tolerance to interpolate fit point and an option to create a periodic or non-periodic curve.
  • OdGeNurbCurve3d(const OdGePoint3dArray& fitPoints, const OdGeVector3d& startTangent, const OdGeVector3d& endTangent, bool startTangentDefined, bool endTangentDefined, const OdGeTol& fitTolerance = OdGeContext::gTol) – Creates a 3D spline interpolating a given array of fit points within a given tolerance. If startTangentDefined and endTangentDefined are true, the specified tangent vectors at the start point and endpoint are used for constructing the spline. Otherwise, tangent values will be calculated by the constructor.
  • OdGeNurbCurve3d(const OdGePoint3dArray& fitPoints, const OdGeVector3d& startTangent, const OdGeVector3d& endTangent, bool startTangentDefined, bool endTangentDefined, OdGeKnotParameterization knotParam, const OdGeTol& fitTolerance = OdGeContext::gTol) – Creates a 3D spline interpolating a given array of fit points within a given tolerance and knot parameterization. If startTangentDefined and endTangentDefined are true, the specified tangent vectors at the start point and endpoint are used for constructing the spline. Otherwise, tangent values will be calculated by the constructor.
  • OdGeNurbCurve3d::OdGeNurbCurve3d(const OdGeEllipArc3d& ellipse, int numSpans = 0) – Creates a spline identical to a specified ellipse.
  • OdGeNurbCurve3d::OdGeNurbCurve3d(const OdGeLineSeg3d& lineSeg) – Creates a spline identical to a specified linear segment.

For example, the following creates a rational NURBS curve by a given degree, knot vector and control points:

OdGeKnotVector knots;
OdGePoint3dArray cntrlPnts;
knots.append(0.0);
knots.append(0.0);
knots.append(0.0);
knots.append(0.0);
knots.append(1.0);
knots.append(1.0);
knots.append(1.0);
knots.append(1.0);
cntrlPnts.append(OdGePoint3d(0,0,0));
cntrlPnts.append(OdGePoint3d(1,1,0));
cntrlPnts.append(OdGePoint3d(2,1,0));
cntrlPnts.append(OdGePoint3d(3,0,0));
OdGeNurbCurve3d nurb(3, knots, cntrlPnts);

Working with the structure of a spline

The OdGeNurbCurve3d class provides a set of methods for working with spline definition data.

Using this functionality you are able to add or delete control points, insert or delete knots, get values of specific spline parameters or set parameters to their new values, etc.

The addControlPointAt() method adds a control point at the specified knot value using the weight parameter to set the weight value for rational splines (if the weight isn't specified, a default value of 1.0 will be used). The deleteControlPointAt() deletes a control point with specified index from the control points array. The addKnot() method adds a new knot in the spline knot vector. To increase the degree of the spline by a specific value, use the elevateDegree() method. If the spline is not already rational, you can make it rational (each control point is assigned the specified weight) using the makeRational() method.

To get all data that defines a NURBS curve, use the getDefinitionData() method. It returns the degree of the spline, two boolean values indicating if the spline is rational and periodic, knots of the spline, and two arrays for control points and for corresponding weights.

OdGeKnotVector knots;
OdGePoint3dArray cntrlPnts;
OdGeDoubleArray weights;
int degree;
bool isRational, isPeriodic;
nurb.getDefinitionData(degree, isRational, isPeriodic, knots, cntrlPnts, weights);

To set definition data of a NURBS curve, use the set() method. It sets the degree of the spline, knots, an array of control points, an array for corresponding weights, and a flag that indicates if the spline is periodic:

nurb.set(degree, knots, cntrlPnts, weights, isPeriodic);

You can also join the spline with another one using the joinWith() method. To calculate the resulting spline, both splines must be open and the start point of the input spline must be equal to the endpoint of the initial spline.

If the spline is open, you can make it closed using the makeClosed() method that creates a smooth spline between the first and end points and joins it with the initial spline.

The hardTrimByParams() method trims NURBS curves to the newStartParam and newEndParam parameter values.

Use one of the convertFrom() methods to convert the specified curve to a NURBS curve. These methods return a raw pointer to the converted curve, so caller is responsible for clearing allocated memory for the converted curve. For example to convert a 3D line segment:


OdGeLineSeg3d line3d;
OdGeNurbCurve3d* convertedCurve = OdGeNurbCurve3d::convertFrom(&line3d);

...

delete convertedCurve;

Working with spline fit data

The spline object can contain fit data that defines its behavior at fit points.

If the spline contains fit data, use one of the getFitData() methods to obtain all the fit data that is used to construct the spline, including the fit points, fit tolerance, start and end tangent values (if they exist), and knot parameterization:

nurb.getFitData(fitPnts, fitTolerance, isTangentsExist, startTangent, endTangent, knotParam);

In the case of a spline constructed by control points, you can calculate fit data using the buildFitData() method. The method returns the true value if the data is successfully calculated.

Besides all fit data, you can get specific fit parameters for the spline using such methods as: getFitPointAt(), getFitKnotParameterization(), getFitTangents() and getFitTolerance(). For example, to get the fit point at an index of 0, fit knot parameterization, start and end tangent vectors, and fit tolerance of the spline:


nurb.getFitPointAt(0, OdGePoint3d());
OdGeKnotParameterization knotParam;
nurb.getFitKnotParameterization(knotParam);
nurb.getFitKnotParameterization(startTangent, endTangent);
nurb.getFitTolerance(fitTolerance);

To set fit data, use the setFitData() method with the following available signatures:

  • OdGeNurbCurve3d& setFitData(const OdGePoint3dArray& fitPoints, const OdGeVector3d& startTangent, const OdGeVector3d& endTangent, const OdGeTol& fitTol = OdGeContext::gTol) – Recalculates the NURBS curve using new values of fit points, start and end tangent vectors, and fit tolerance.
  • OdGeNurbCurve3d& setFitData(int degree, const OdGePoint3dArray& fitPoints, const OdGeTol& fitTol = OdGeContext::gTol) – Recalculates the NURBS curve using new values of fit points, fit tolerance and makes it of a given degree.
  • OdGeNurbCurve3d& setFitData(const OdGeKnotVector& fitKnots, const OdGePoint3dArray& fitPoints, const OdGeVector3d& startTangent, const OdGeVector3d& endTangent, const OdGeTol& fitTol = OdGeContext::gTol, bool isPeriodic = false) – Recalculates the NURBS curve using new values of fit points, fit knots, start and end tangent vectors, fit tolerance and on option to create a periodic or non-periodic curve.
  • OdGeNurbCurve3d& setFitData(const OdGePoint3dArray& fitPoints, const OdGeVector3d& startTangent, const OdGeVector3d& endTangent, OdGeKnotParameterization knotParam, const OdGeTol& fitTol = OdGeContext::gTol) – Recalculates the NURBS curve using new values of fit points, start and end tangent vectors, knot parameterization and fit tolerance.

For example:

nurb.setFitData(fitPnts, OdGeVector3d(0,1,0), OdGeVector3d(1,-1,0), OdGeTol(1.e-6));

To set specific fit data, use such methods as: setFitPointAt(), setFitTangents() and setFitTolerance().

The addFitPointAt() method adds a fit point at the specified specified fit point index. The deleteFitPointAt() deletes a fit point with specified index from the fit points array.

If the spline is open, you can make it closed using the makeClosedFit() method that creates a smooth spline between the first and end interpolated points and joins it with the initial spline.

To purge all fit data that is used to construct the spline, use purgeFitData(). This method cleans the fit points array, start and end tangents without changing knots, control points and their weights, and returns true. If the spline doesn't contain fit data, the method only returns false.

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.