Class BSpline
- Namespace
- CADability.GeoObject
- Assembly
- CADability.dll
A BSpline is a smooth curve defined by a set of control points. It is implemented as a NURBS - non uniform rational b-spline.
public class BSpline : IGeoObjectImpl, IFeedBack, ICloneable, IColorDef, ILineWidth, ILinePattern, ISerializable, ICurve, IGeoObject, ILayer, IStyle, IOctTreeInsertable, IComparable, IExplicitPCurve3D, IExportStep
- Inheritance
-
BSpline
- Implements
- Inherited Members
Constructors
BSpline()
Creates a spline without data and raises Constructed. Protected: use Construct() to create a spline.
protected BSpline()
BSpline(SerializationInfo, StreamingContext)
Constructor required by deserialization
protected BSpline(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoSerializationInfo
contextStreamingContextStreamingContext
Fields
Constructor
Optional hook for polymorphic construction. When set, Construct() calls this delegate instead of creating a plain BSpline, so that an application can have its own derived class instantiated wherever CADability creates a spline.
public static BSpline.ConstructionDelegate Constructor
Field Value
OnPaintTo3D
Static hook called at the beginning of PaintTo3D(IPaintTo3D) for every spline. Set it to replace or extend the way splines are displayed; when the handler returns true, the default painting is skipped.
public static BSpline.PaintTo3DDelegate OnPaintTo3D
Field Value
Properties
ColorDef
The color of this spline, null if it has none of its own. Setting it is an undoable attribute change that raises the change events; a "color from parent" value is resolved against the Block that owns this object.
public ColorDef ColorDef { get; set; }
Property Value
Degree
The polynomial degree of the spline: 1 for a spline that is a polyline through its poles, 3 for the usual cubic spline.
public int Degree { get; }
Property Value
HasWeights
true if this spline is rational, i.e. at least one weight differs from 1.0. Non-rational splines can be exported in simpler forms (e.g. without the rational flag in DXF, as a plain B_SPLINE_CURVE_WITH_KNOTS in STEP).
public bool HasWeights { get; }
Property Value
IsClosed
true for a periodic (closed) spline. Setting it closes or opens the spline: when through points exist, the spline is recomputed through them with the new closedness; otherwise through points are derived from the curve first. If that fails, the knot multiplicities are adapted directly. The change is undoable and raises the change events.
public bool IsClosed { get; set; }
Property Value
IsSingular
true if all poles coincide, i.e. the spline degenerates to a single point.
public bool IsSingular { get; }
Property Value
KnotPoints
The points of the curve at the knot values (PointAtParam(double) of each entry of Knots), i.e. the junctions of the polynomial segments of the spline.
public GeoPoint[] KnotPoints { get; }
Property Value
- GeoPoint[]
Knots
The distinct knot values in ascending order; Multiplicities says how often each of them is repeated in the flat knot vector. The curve parameter (as used by PointAtParam(double) and TrimParam(double, double)) runs from the first to the last knot. The returned array is the internal one and must not be modified.
public double[] Knots { get; }
Property Value
- double[]
LinePattern
The line pattern (dashes) used to display this spline, null for a solid line. Setting it is an undoable attribute change that raises the change events.
public LinePattern LinePattern { get; set; }
Property Value
LineWidth
The line width used to display this spline, null for the default. Setting it is an undoable attribute change that raises the change events.
public LineWidth LineWidth { get; set; }
Property Value
Multiplicities
The multiplicity of each entry of Knots. For a non-periodic spline the sum of all multiplicities is PoleCount + Degree + 1. The returned array is the internal one and must not be modified.
public int[] Multiplicities { get; }
Property Value
- int[]
PoleCount
The number of control points (poles).
public int PoleCount { get; }
Property Value
Poles
The control points (poles) of this spline. The returned array is the internal one and must not be modified; use SetPole(int, GeoPoint) or SetData(int, GeoPoint[], double[], double[], int[], bool) to change the poles.
public GeoPoint[] Poles { get; }
Property Value
- GeoPoint[]
PreferredStyle
Returns the preferred style for this objects, see Style.EDefaultFor.
public override Style.EDefaultFor PreferredStyle { get; }
Property Value
ThroughPoint
The points this spline was defined to pass through (see ThroughPoints(GeoPoint[], int, bool, double[])), or null when it was defined otherwise or the through points were invalidated (see ThroughPoints3dExist). The returned array is the internal one and must not be modified; use SetThroughPoint(int, GeoPoint) to move a point.
public GeoPoint[] ThroughPoint { get; }
Property Value
- GeoPoint[]
ThroughPointCount
The number of through points, 0 when the spline has none (see ThroughPoints3dExist).
public int ThroughPointCount { get; }
Property Value
ThroughPoints3dExist
true if this spline was defined by points it passes through (ThroughPoints(GeoPoint[], int, bool, double[]), Approximate(GeoPoint[], double)) and those points are still valid. They become invalid when the poles are changed directly (SetPole(int, GeoPoint), SetData(int, GeoPoint[], double[], double[], int[], bool)). Only when this is true, ThroughPoint, GetThroughPoint(int) and SetThroughPoint(int, GeoPoint) may be used.
public bool ThroughPoints3dExist { get; }
Property Value
Weights
The weight of each pole, same length as Poles; all 1.0 for a non-rational spline (see HasWeights). The returned array is the internal one and must not be modified.
public double[] Weights { get; }
Property Value
- double[]
Methods
Approximate(GeoPoint[], double)
Makes this spline a cubic spline through a subset of the given points, chosen so that none of the given points is
farther than precision from the spline: starting with the first and the last point (and the middle
one for a closed polygon), points are added where the deviation is too large, until the tolerance is met. If the first
and the last point coincide (within a tenth of the precision), the spline is closed. Used to simplify polylines and
splines with many poles, see ApproximateByBSpline(double) and ReducePoles(double). Raises the
change events.
public bool Approximate(GeoPoint[] vertex, double precision)
Parameters
vertexGeoPoint[]The points to approximate, in order
precisiondoubleMaximum allowed distance of the points from the spline
Returns
- bool
true on success, false when there are too few points (less than 2, or less than 3 for a closed polygon)
Approximate(Func<double, GeoPoint>, double, double, double, int)
Approximates the provided curve by a cubic BSpline which interpolates the curve at
adaptively chosen parameters. The deviation is checked at the quarter points of each interval between two
interpolation parameters, and an interval that deviates more than precision is split
into as many parts as the error order of cubic interpolation (h^4) predicts to be necessary. This is repeated
until the deviation is less than precision everywhere or maxCount
points are used, where the intervals with the largest deviation are refined first. Every curve point is
calculated only once.
The parameter of the resulting BSpline is the parameter of the provided function, i.e. it runs from
minPar to maxPar. precision bounds the geometric
distance between the curve and the spline, the distance between the points with the same parameter may be
larger where it is mostly tangential.
public static BSpline Approximate(Func<double, GeoPoint> curve, double precision = 0, double minPar = 0, double maxPar = 1, int maxCount = 1000)
Parameters
curveFunc<double, GeoPoint>the curve to approximate, in terms of a parameter to point function
precisiondoublethe maximum deviation, 0.0: a small fraction of the extent of the curve
minPardoublethe parameter where the curve starts
maxPardoublethe parameter where the curve ends, must be greater than
minParmaxCountintthe maximum number of points to use
Returns
- BSpline
the approximating BSpline, null if the parameter range is invalid or the interpolation fails
Clone()
Clones this GeoObject.
public override IGeoObject Clone()
Returns
- IGeoObject
the cloned GeoObject
Construct()
Creates a new spline. This is the only way to create one, the constructor is protected. The returned spline has no data yet; define it with one of the ThroughPoints(GeoPoint[], int, bool, double[]) methods, with SetData(int, GeoPoint[], double[], double[], int[], bool) or with Approximate(GeoPoint[], double).
public static BSpline Construct()
Returns
- BSpline
A new BSpline, or an object of the derived class provided by Constructor
CopyAttributes(IGeoObject)
Implements CopyAttributes(IGeoObject). The default implementation handles all CADability attributes and leaves the handling of non CADability attributes to your code.
public override void CopyAttributes(IGeoObject ToCopyFrom)
Parameters
ToCopyFromIGeoObjectGeoObject to copy attribute data from
CopyGeometry(IGeoObject)
Copies the geometrical aspects of the given GeoObject to this GeoObject. Both objects must be of the same type. The attributes are not copied.
public override void CopyGeometry(IGeoObject ToCopyFrom)
Parameters
ToCopyFromIGeoObjectGeoObject to copy from
FindSnapPoint(SnapPointFinder)
Asks the object to add its snap points (end points, centres, intersections...) of the kinds that
spf asks for. The default implementation adds none; objects with geometry override it.
public override void FindSnapPoint(SnapPointFinder spf)
Parameters
spfSnapPointFinderdefinition and collection of the snap points
GetBoundingCube()
Returns a bounding cube of the object. The object must fir into this cube. There may be a smaller cube that contains the object if it is to expensive to calculate the exact cube.
public override BoundingCube GetBoundingCube()
Returns
- BoundingCube
the bounding cube
GetData(out GeoPoint[], out double[], out double[], out int)
Returns copies of the defining data of this spline with a flat knot vector: each knot is repeated according to its multiplicity, as e.g. OpenGL evaluators and NurbsSurface expect it. See GetData(out int, out GeoPoint[], out double[], out double[], out int[]) for the form with separate multiplicities.
public void GetData(out GeoPoint[] poles, out double[] weights, out double[] knots, out int degree)
Parameters
polesGeoPoint[]The control points
weightsdouble[]The weight of each control point, all 1.0 for a non-rational spline
knotsdouble[]The flat knot vector; its length is the number of poles +
degree+ 1degreeintThe degree of the spline
GetData(out int, out GeoPoint[], out double[], out double[], out int[])
Returns copies of the defining NURBS data of this spline: degree, poles, weights, distinct knots and their multiplicities. The same data can be passed to SetData(int, GeoPoint[], double[], double[], int[], bool) to define another spline.
public void GetData(out int degree, out GeoPoint[] poles, out double[] weights, out double[] knots, out int[] multiplicities)
Parameters
degreeintThe degree of the spline
polesGeoPoint[]The control points
weightsdouble[]The weight of each control point, all 1.0 for a non-rational spline
knotsdouble[]The distinct knot values in ascending order
multiplicitiesint[]The multiplicity of each knot
GetExtent(Projection, ExtentPrecision)
Returns the 2d extent of the object as it appears in the given projection, in the coordinates of the projection's plane (not in pixels).
public override BoundingRect GetExtent(Projection projection, ExtentPrecision extentPrecision)
Parameters
projectionProjectionthe projection
extentPrecisionExtentPrecisionExact for the exact extent, Raw for a quick estimate that may be larger
Returns
- BoundingRect
the extent in the projection's 2d coordinates
GetExtent(double)
Gets the 3-dimensional extent of an object
public override BoundingCube GetExtent(double precision)
Parameters
precisiondoublePrecision of the test
Returns
- BoundingCube
The minmax cube
GetObjectData(SerializationInfo, StreamingContext)
Writes the data of this object for the legacy binary serialization (ISerializable). Files are written in the JSON format (IJsonSerialize) now; the binary format is still read.
public override void GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInforeceives the data
contextStreamingContextthe destination of the serialization
GetPole(int)
Returns the control point (pole) with the given index.
public GeoPoint GetPole(int Index)
Parameters
Returns
- GeoPoint
The pole
GetQuadTreeItem(Projection, ExtentPrecision)
Returns an item for a 2d quad tree of the projected objects, used for hit tests in a projection: it knows the 2d extent of the object, whether a 2d rectangle hits it, and the depth (z position) of the object at a 2d point. Composite objects (blocks, solids, paths) return a collection of the items of their parts.
public override IQuadTreeInsertableZ GetQuadTreeItem(Projection projection, ExtentPrecision extentPrecision)
Parameters
projectionProjectionthe projection
extentPrecisionExtentPrecisionprecision of the extent, see GetExtent(Projection, ExtentPrecision)
Returns
- IQuadTreeInsertableZ
the item for the quad tree
GetShowProperties(IFrame)
Should be overridden and return a IPropertyEntry derived object that handles the display and modification of the properties of the IGeoObject derived object. Default implementation return null.
public override IPropertyEntry GetShowProperties(IFrame Frame)
Parameters
FrameIFrame
Returns
GetThroughPoint(int)
Returns the through point with the given index. Only valid when ThroughPoints3dExist is true.
public GeoPoint GetThroughPoint(int Index)
Parameters
IndexintIndex of the through point, 0 to ThroughPointCount-1
Returns
- GeoPoint
The through point
HitTest(ref BoundingCube, double)
Tests whether an object is touched by the provided cube
public override bool HitTest(ref BoundingCube cube, double precision)
Parameters
cubeBoundingCubeTo test with
precisiondoubleRequired precision
Returns
- bool
true if the object strikes the cube
HitTest(Projection, BoundingRect, bool)
Tests whether an object is inside or touched by the rectangle when projected with the provided projection
public override bool HitTest(Projection projection, BoundingRect rect, bool onlyInside)
Parameters
projectionProjectionThe projection for the test
rectBoundingRectThe rectangle to test the position
onlyInsidebooltrue: the object must be totaly inside the rectangle, false: the object must be totaly or partially inside the rectangle
Returns
- bool
true when the test succeedes, false otherwise
HitTest(PickArea, bool)
Tests whether an object is inside or touched by the provided area
public override bool HitTest(Projection.PickArea area, bool onlyInside)
Parameters
areaProjection.PickAreaThe area to check for
onlyInsidebooltrue: the object must be totaly inside the area, false: the object must be totaly or partially inside the area
Returns
- bool
true when the test succeedes, false otherwise
Modify(ModOp)
Modifies this GeoObject with the given modification operation (includes moving, rotating, reflecting, scaling etc.).
public override void Modify(ModOp m)
Parameters
mModOpOperation to be applied
PaintTo3D(IPaintTo3D)
Paints the object with the methods of paintTo3D: lines, triangles, text... The views implement
IPaintTo3D (with OpenGL or GDI) and collect what the object paints in a display list, so this is
called rarely, after PrepareDisplayList(double) has done the expensive work (triangulation, approximation).
public override void PaintTo3D(IPaintTo3D paintTo3D)
Parameters
paintTo3DIPaintTo3Dwhere to paint
PointAndDerivativesAt(double, int)
Returns all derivatives up to grad. The first vector is actually the point at this position.
public IReadOnlyList<GeoVector> PointAndDerivativesAt(double position, int grad)
Parameters
positiondoubleWhere to calculate the derivatives
gradintNumber of derivatives. grad==2 returns the point, the first and the second derivative
Returns
- IReadOnlyList<GeoVector>
Sequence of derivatives: element 0 = point-vector (P), 1 = first derivative (P'), 2 = second (P''), … . Length ≥ grad+1.
PointAtParam(double)
Returns the point at the provided parameter. Unlike the ICurve method PointAt(double) this Method takes a parameter in the natural space of the BSpline.
public GeoPoint PointAtParam(double param)
Parameters
paramdoubleParameter to get the point for
Returns
- GeoPoint
The resulting point
Position(GeoPoint, GeoVector, double)
Returns the smallest parameter value (l) where the provided line hits the object so that the point
fromHere + l * direction is on or close to the object. The result may be negative.
Return double.MaxValue if there is no such point.
public override double Position(GeoPoint fromHere, GeoVector direction, double precision)
Parameters
fromHereGeoPointStartpoint of the line
directionGeoVectorDirection of the line
precisiondoublePrecision for the test
Returns
- double
Position on the line
PositionOfThroughPoint(int)
Returns the position on the curve (0 at the start point, 1 at the end point, as used by PointAt(double)) of the through point with the given index. The positions are only known for splines created by ThroughPoints(GeoPoint[], int, bool, double[]), which records the parameters of its through points; for all other splines 0.0 is returned.
public double PositionOfThroughPoint(int ind)
Parameters
indintIndex of the through point
Returns
- double
The position of that point on the curve, or 0.0 when unknown
PrepareDisplayList(double)
Called before PaintTo3D(IPaintTo3D) is called, should implement the time consuming work for the display (like calculationg the triangulation of Faces or Polylines of Curves). Not normally used by user code.
public override void PrepareDisplayList(double precision)
Parameters
precisiondoubleRequired precision
ReducePoles(double)
Replaces this spline by an approximation with (usually) fewer poles: a cubic spline through a subset of its through
points, or of its knot points (KnotPoints) when it has no through points, that deviates less than
precision from those points (see Approximate(GeoPoint[], double)). Useful for splines with many poles,
e.g. from imported data.
public void ReducePoles(double precision)
Parameters
precisiondoubleMaximum allowed distance of the original through or knot points from the new spline
SetData(int, GeoPoint[], double[], double[], int[], bool)
Defines this spline directly by its NURBS data, using the whole range from the first to the last knot. See SetData(int, GeoPoint[], double[], double[], int[], bool, double, double) for the details.
public bool SetData(int degree, GeoPoint[] poles, double[] weights, double[] knots, int[] multiplicities, bool periodic)
Parameters
degreeintThe degree of the spline
polesGeoPoint[]The control points
weightsdouble[]The weight of each control point, or null for a non-rational spline
knotsdouble[]The distinct knot values in ascending order, or the flat knot vector when
multiplicitiesis nullmultiplicitiesint[]The multiplicity of each knot, or null when
knotsis a flat knot vectorperiodicbooltrue for a closed (periodic) spline
Returns
- bool
true if the data is consistent, false otherwise
SetData(int, GeoPoint[], double[], double[], int[], bool, double, double)
Defines this spline directly by its NURBS data, e.g. when importing from a file. The data is checked and completed
where possible: null or wrongly sized weights are replaced by weights of 1.0; when
multiplicities is null or does not match knots, knots is
taken as a flat knot vector and the multiplicities are counted from it; for a non-periodic spline the first and last
multiplicities are raised or the end poles are duplicated so that the number of poles equals the sum of the
multiplicities minus degree minus 1. If startParam and
endParam lie inside the knot range, the spline is trimmed to that range afterwards. Through points
of a previous definition are discarded. This is a low level setter: it does not raise the change events and is not
recorded for undo.
public bool SetData(int degree, GeoPoint[] poles, double[] weights, double[] knots, int[] multiplicities, bool periodic, double startParam, double endParam)
Parameters
degreeintThe degree of the spline
polesGeoPoint[]The control points
weightsdouble[]The weight of each control point, or null for a non-rational spline
knotsdouble[]The distinct knot values in ascending order, or the flat knot vector when
multiplicitiesis nullmultiplicitiesint[]The multiplicity of each knot, or null when
knotsis a flat knot vectorperiodicbooltrue for a closed (periodic) spline
startParamdoubleParameter where the curve starts, at least the first knot
endParamdoubleParameter where the curve ends, at most the last knot
Returns
- bool
true if the data is consistent (number of poles = sum of multiplicities - degree - 1 after the completion), false otherwise; the data is set in either case
Exceptions
- ApplicationException
when the knots are not in ascending order
SetPole(int, GeoPoint)
Modifies the value of a pole. The Index must be between 0 and PoleCount.
public void SetPole(int Index, GeoPoint ThePoint)
Parameters
SetThroughPoint(int, GeoPoint)
Moves one of the through points and recomputes the spline through the modified points with the same degree and closedness (see ThroughPoints(GeoPoint[], int, bool, double[])). Only valid when ThroughPoints3dExist is true. The change is undoable and raises the change events.
public void SetThroughPoint(int Index, GeoPoint NewValue)
Parameters
IndexintIndex of the through point, 0 to ThroughPointCount-1
NewValueGeoPointNew position of that point
ShowPoints(bool, bool)
Controls whether the through points and the poles (the control polygon) are displayed together with the curve, e.g. while the spline is being constructed interactively. This is a display setting only: it does not change the geometry and is not saved with the object.
public void ShowPoints(bool showThroughPoints, bool showPoles)
Parameters
ThroughPoints(GeoPoint[], GeoVector[], int, bool)
Makes this spline interpolate the given points with prescribed tangents: the spline passes through
points[i] with the tangent direction directions[i] (the directions should be
unit vectors). Previous data is discarded; the points are remembered as through points (see
ThroughPoint) and all weights are 1. The degree is maxDegree, limited to 2·n-1 for n
points; degrees 2 and 3 give the best results, high degrees behave badly near the ends. The change is undoable and
raises the change events.
public bool ThroughPoints(GeoPoint[] points, GeoVector[] directions, int maxDegree, bool closed)
Parameters
pointsGeoPoint[]The points to interpolate, at least two
directionsGeoVector[]The tangent direction at each point, same length as
pointsmaxDegreeintThe degree of the spline
closedbooltrue for a closed (periodic) spline
Returns
- bool
true on success, false when there are less than two points or the interpolation fails; the spline is then unchanged
ThroughPoints(GeoPoint[], int, bool, double[])
Makes this BSpline go through the given points. Previous data of this BSpline (if any) is discarded. The BSpline remembers both this points and the calculated poles, multiplicities, knots and weights values. If all points lie in a single plane it is better to use the appropriate ThroughPoints method.
public bool ThroughPoints(GeoPoint[] points, int maxDegree, bool closed, double[] throughPointKnots = null)
Parameters
pointsGeoPoint[]List of points to pass through
maxDegreeintmaximum degree for the BSpline. Must be between 3 an 25
closedbooltrue if the resulting BSpline should be closed
throughPointKnotsdouble[]Optional parameter values for the through points. If null, they are calculated from the distances of the points
Returns
- bool
success
ThroughPoints(Plane, GeoPoint2D[], GeoVector2D[], int, bool)
Not implemented: always returns false and leaves the spline unchanged. Convert the points and directions with ToGlobal(GeoPoint2D) and ToGlobal(GeoVector2D) and use ThroughPoints(GeoPoint[], GeoVector[], int, bool) instead.
public bool ThroughPoints(Plane p, GeoPoint2D[] points, GeoVector2D[] directions, int maxDegree, bool closed)
Parameters
pPlaneThe plane of the points
pointsGeoPoint2D[]The points to interpolate, in
pdirectionsGeoVector2D[]The tangent direction at each point, in
pmaxDegreeintThe degree of the spline
closedbooltrue for a closed spline
Returns
- bool
Always false
TrimParam(double, double)
Returns a new spline that is the part of this spline between the two parameters. The parameters are given in the
parameter space of the knots (see Knots, PointAtParam(double)), not as positions in [0, 1] like
Trim(double, double) expects them. If spar is greater than epar, the result
is reversed. This spline is not changed; only the geometry is copied, not the attributes.
public BSpline TrimParam(double spar, double epar)
Parameters
Returns
- BSpline
The trimmed spline
Events
Constructed
Raised from the constructor whenever a new spline is created, i.e. for every Construct() call, every clone and every spline read from a file. The handler gets the new object; when it comes from Construct(), it has no data yet.
public static event BSpline.ConstructedDelegate Constructed