Bspline Constraints
Bspline Constraints
FreeCAD Sketcher
Ajinkya Dahale
October 2022
Following lists the logic behind and equations involved in the new constraints
introduced in Sketcher during the August–October 2022 B-splines constraint
project. For understanding where these fit into the planegcs solver architecture,
please refer to Abdullah Tahiri’s document on the same.
1 Preliminaries
We have the general parametric equation of a B-spline, i.e a spline S of degree
n in terms of basis splines Bi,n , 1 ≤ i ≤ k
X
Sn,t = αi Bi,n (u) (1)
i
1
where pi are the positions of the control points. The point vector to be passed
to the solver is {qx , qy , p1x , p1y , ..., pkx , pky }.
We then define the errors as
X
ex = q x − pix Bi,n (U ) (4)
i
X
ey = q y − piy Bi,n (U ) (5)
i
where wi are the weights of the control points. The point vector then becomes
{qx , qy , p1x , p1y , ..., pkx , pky , w1 , w2 , ..., wk }.
We then define the errors as follows, to avoid the complications arising by
the division
X X
ex = wi Bi,n (U )qx − wi pix Bi,n (U ) (9)
i i
X X
ey = wi Bi,n (U )qy − wi piy Bi,n (U ) (10)
i i
2.2 Implementation
Each constraint is then implemented as a ConstraintWeightedLinearCombination,
with the Bi,n (U ) values stored directly as linear combination factors.
2
Since B-splines have a local support, we only need to consider upto n + 1
control points. Further, for a knot with multiplicity m, we only need to consider
n + 1 − m control points (with minimum of 1 for the end-points that have
multiplicity n + 1).
Since a flattened set of knots is needed for running the de Boor algorithm,
they are derived from the un-flattened knot vector and the multiplicity vector
within the BSpline object and stored separately.
3.1 Implementation
This constraint is implemented as ConstraintPointOnBSpline. We cannot now
store Bi,n (U ), since it keeps changing. So we instead compute the sums directly
using de Boor algorithm.
We also use known derivative of B-spline (can be found in a textbook)
∂ X X αi − αi−1
αi Bi,n (u) = n Bi,(n−1) (u), (18)
∂u i i
ti+n − ti
3
4 Tangent at Knot
Once again, we look at controlling the slope of the B-spline at a fixed parameter
u = U.
∂
The direction of the slope can be given by the vector ∂u Sn,t . For conve-
nience, we instead work with a parallel vector
!2
X ∂
r(u) = wi Bi,n (u) Sn,t (u)
i
∂u
! !
X X
′
= wi Bi,n (u) pi wi Bi,n (u)
i i
! !
X X
′
− wi Bi,n (u) pi wi Bi,n (u) (19)
i i
′ ∂
where Bi,n (u) = ∂u Bi,n (u).
We want this to be parallel to some vector, say l. An appropriate error
function then is
e = ẑ · (r(U ) × l) = rx (U )lx − ry lx (20)
Note that in addition to this, the l-vector comes from a line segment, and
we also want to constrain the knot to be on that line segment.
The gradients can be derived from
X
′
∂rx (u)/∂pix = wj Bj,n (u) wi Bi,n (u)
j
X
′
− wj Bj,n (u) wi Bi,n (u), (21)
j
X
′
∂rx (u)/∂wi = Bi,n (u) wj (pjx − pix )Bj,n (u)
j
X
′
−Bi,n (u) wj (pjx − pix )Bj,n (u) , (22)
j
4.1 Implementation
This constraint is implemented as ConstraintSlopeAtBSplineKnot.