# Dealing with B-splines in OpenCASCADE

# Introduction

In this simple article, we’ll talk about two types of splines and their math representations theoretically. We’ll focus on b-spline in OpenCASCADE (OCC) technology with a simple scenario and the necessary classes we used.

# Types of Curves

- Splines
- B-splines

# Spline Curve

A spline curve is a mathematical representation for which it is easy to build an interface that will allow a user to design and control the shape of complex curves and surfaces. The general approach is that the user enters a sequence of points, and a curve is constructed whose shape closely follows this sequence. The points are called control points. A curve that actually passes through each control point is called an interpolating curve; a curve that passes near to the control points but not necessarily through them is called an approximating curve. Once we establish this interface, then to change the shape of the curve we just move the control points.

# Polynomial curves

Polynomials have the general form:

The degree of a polynomial corresponds with the highest coefficient that is non-Zero. For example, if c is non-zero but coefficients d and higher are all zero, the polynomial is of degree 2.

Example: degree 3: Cubic, d is highest non-zero coefficient.

A cubic curve (which can have an inflection, at x = 0 in this example), uniquely defined by four points.

The degree three polynomial — known as a cubic polynomial is the one that is most typically chosen for constructing smooth curves in computer graphics. It is used because:

- It is the lowest degree polynomial that can support an inflection, so we can make interesting curves.
- It is very well behaved numerically, that means that the curves will usually be smooth and not jumpy

# Piecewise polynomial curves

In the previous section, we saw how four control points can defined a cubic polynomial curve, allowing the solution of four linear equations for the four coefficients of the curve. Here we will see how more complex curves can be made using two new ideas:

- Construction of piecewise polynomial curves
- Parameterization of the curve.

Suppose we wanted to make the curve shown to the bottom. We know that a single cubic curve can only have one infection point, but this curve has three, marked with O’s. We could make this curve by entering extra control points and using a 5th degree polynomial, with six coefficients, but polynomials with degree higher than three tend to be very sensitive to the positions of the control points and thus do not always make smooth shapes.

The usual solution to this problem in computer graphics and computer aided design is to construct a complex curve, with a high number of inflection points, by piecing together several cubic curves:

Here is one way that this can be done. Let each pair of control points represent one segment of the curve. Each curve segment is a cubic polynomial with its own coefficients:

In this example, the ten control points have ascending values for the x coordinate, and are numbered with indices 0 through 9. Between each control point pair is a function, which is numbered identically to the index of its leftmost point.

# Curve parameterization

So far we have learned how a sequence of control points can define a piecewise polynomial curve, using cubic functions to define curve segments between control points and enforcing various levels of continuity where segments join. In particular, we employed:

- C0 continuity, meaning that the two segments match values at the join.
- C1 continuity, meaning that they match slopes at the join.
- C2 continuity, meaning that they match curvatures at the join.

We were able to determine coefficients for the curve segments via a set of linear equations

where a is the vector of all coefficients, y is the vector of constants on the right-hand side of the linear equations, and M is a matrix encoding the C0, C1 and C2 conditions.

This approach can be modified to specify each curve segment in parametric form, as indicated in the figure below.

In the example, both curves are identical, however, the equations describing them will be different. In the parametric form on the right, we have defined parameters t0, t1 and t2 that vary between 0 and 1 as we step along the x axis between control points. We could write equations:

Relating the t’s to the original x coordinate. The derivatives indicate how quickly each t varies as we move in the x direction.

Now we specify each curve segment by a parametric cubic curve

# B-splines (from Open-Cascade’s perspective)

A b-spline curve is defined by:

- Its degree: the degree for a Geom_BSplineCurve is limited to a value (25) which is defined and controlled by the system. This value is returned by the function MaxDegree.
- A table of poles (also called control points) : The poles of the curve are “control points” used to deform the curve .
- A table of knots with their multiplicities. For a Geom_BSplineCurve, the table of knots is an increasing sequence of reals without repetition; the multiplicities define the repetition of the knots. A BSpline curve is a piecewise polynomial or rational curve. The knots are the parameters of junction points between two pieces.

Illustrate the difference between knots & control points of a b-spline:

Our use cases:

- Step 1

Selecting edge (group of edges) on a 3D model (.STEP file)

- Step 2

Combining these 3 edges into on curve using GeomConvert_CompCurveToBSplineCurve class.

- Step 3

Getting the B-spline of the object from class above using BSplineCurve().

`Handle(Geom_BSplineCurve) bs = comined_crv.BSplineCurve();`

int deg = bs->Degree();

int num_knots = bs->NbKnots();

int num_poles = bs->NbPoles();

- Step 4

- Displaying some important attributes of the b-spline like (degree, number of knots, number of control points “poles in OCC”).

- Visualizing 3D vertex (represent according knot) with its tangent and second derivative vectors.

`gp_Pnt point;//3D point represents the knot(i)`

Standard_Real U = bs->Knot(i);//Actual knot

//D2 function computes the corresponding point, tangent vector at the point

//and the second derevitive vector at the same point also.

bs->D2(U, point, tangent_vector, d2_vector);

Green cross: represents 3D point of the knot, red arrow: represents tangent vector at the point, yellow arrow: represents 2nd derivative vector at the point

We conclude that OCC deals with many b-splines as third-degree splines and the continuity on the connecting point between two adjacent sub-segments is C1 continuity.

# References

https://people.cs.clemson.edu/~dhouse/courses/405/notes/splines.pdf

https://old.opencascade.com/doc/occt-6.9.0/refman/html/class_geom___b_spline_curve.html