knots and direction of B-Spline

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
smktec
Posts: 327
Joined: Thu Mar 05, 2020 5:37 pm

knots and direction of B-Spline

Post by smktec »

I have just started to work with B-Splines and wonder if there is an easy way to walk along a B-Spline, find all knots and their direction (probably their derivative). Your help will be much appreciated
thanks
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: knots and direction of B-Spline

Post by edwilliams16 »

Easy is in the eye of the beholder.

Create a single B-Spline in Sketcher and close. Select it and send to the python console with ctrl-shift-P.

Code: Select all

bsp = sub.Curve
bsp is your BSpline object. Command completion will give you the Properties and methods - like bsp.KnotSequence or bsp.getPolesAndWeights()

Unfortunately help(bsp) is truncated, but

Code: Select all

def fullhelp(obj):
   list = [(getattr(obj, d), d) for d in obj.__dir__() if d[0] != '_' and not d[0].isupper()]
   [help(l[0]) for l in sorted(list, key = lambda s: s[1])]

fullhelp(bsp)
   [code]

gets the internal documentation.
User avatar
jnxd
Posts: 951
Joined: Mon Mar 30, 2015 2:30 pm
Contact:

Re: knots and direction of B-Spline

Post by jnxd »

There are methods in OCCT that do that, named D0, D1 etc. I'm pretty sure they are exposed in Part::Geometry as well.
My latest (or last) project: B-spline Construction Project.
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: knots and direction of B-Spline

Post by edwilliams16 »

Some properties of the <BSplineCurve object>

Code: Select all

Help on built-in function getD0:

getD0(...) method of Part.BSplineCurve instance
    Returns the point of given parameter

Help on built-in function getD1:

getD1(...) method of Part.BSplineCurve instance
    Returns the point and first derivative of given parameter

Help on built-in function getD2:

getD2(...) method of Part.BSplineCurve instance
    Returns the point, first and second derivatives

Help on built-in function getD3:

getD3(...) method of Part.BSplineCurve instance
    Returns the point, first, second and third derivatives

Help on built-in function getDN:

getDN(...) method of Part.BSplineCurve instance
    Returns the n-th derivative
User avatar
mfro
Posts: 663
Joined: Sat Sep 23, 2017 8:15 am

Re: knots and direction of B-Spline

Post by mfro »

edwilliams16 wrote: Tue May 30, 2023 7:46 pm...
Unfortunately help(bsp) is truncated,
...
That is an annoying limitation of the FreeCAD Gui. FreeCADCmd will give you the full help, however:

Code: Select all

bin/FreeCADCmd


FreeCAD 0.21.0, Libs: 0.21.0R33238 (Git)
(c) Juergen Riegel, Werner Mayer, Yorik van Havre and others 2001-2023
FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.
FreeCAD wouldn't be possible without FreeCAD community.
  #####                 ####  ###   ####  
  #                    #      # #   #   # 
  #     ##  #### ####  #     #   #  #   # 
  ####  # # #  # #  #  #     #####  #   # 
  #     #   #### ####  #    #     # #   # 
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##

[FreeCAD Console mode <Use Ctrl-D (i.e. EOF) to exit.>]
>>> import Part
>>> import readline
>>> help(Part.BSplineCurve)
(that helps with other truncated help as well, of course)
Cheers,
Markus
smktec
Posts: 327
Joined: Thu Mar 05, 2020 5:37 pm

Re: knots and direction of B-Spline

Post by smktec »

thanks for your input, that helped me.
However, I could not find anything what kind of parameter is require when calling D0 or D1. I want to call the points (D0) and their derivative D1.
I do get the points directly without any parameters using BSP03.Shape.Curve.NbPoles
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: knots and direction of B-Spline

Post by edwilliams16 »

smktec wrote: Wed May 31, 2023 9:51 am However, I could not find anything what kind of parameter is require when calling D0 or D1. I want to call the points (D0) and their derivative D1.
I do get the points directly without any parameters using BSP03.Shape.Curve.NbPoles
BSP03.Shape.Curve.NbPoles is an integer - the number of Poles of the BSpline.

In FreeCAD every Curve that describes the geometry on an Edge is defined parametrically by equations. For instance, a circle at the origin, of radius R in the XY-Plane is

Code: Select all

x(t) = R * cos(t)
y(t) = R * sin(t)
z(t) = 0 
where the parameter t varies from 0 to 2*pi. A circular arc is a circle with the parameter range restricted to edge.ParameterRange

Straight lines, conics etc. likewise, are defined by an algebraic parameterization. You find a point on the curve with parameter t with

Code: Select all

edge.Curve.value(t)
With

Code: Select all

p, t = edge.Curve.getD1(t)
p is the point, t is the tangent vector i.e. (dx/dt, dy/dt, dz/dt) etc.

Moving on to BSplines. A BSpline curve bsp is parameterized by a sequence of polynomials of degree bsp.Degree. The entire parameterization is divided into intervals t0 <= t1 <= t2 <= ..... <=tn given by bsp.KnotSequence (aka a knot vector). In each interval, there is a different polynomial whose values are influenced by neighboring Poles (aka control points), given by bsp.getPoles(). In the sketcher, it's the poles/control points you move around. They are the handles in Adobe Illustrator.
By manipulating the multiplicities (equality of neighboring t values in the knot vector), you can force a BSpline to go through the corresponding control point, but only at the cost of smoothness at the connection point. This is typically always done at the end-points of the BSpline, and can be done at interior points.

Freya Holmer made an awesome video about Bezier Curves (which are a particular kind of BSpline), showing how they work. https://www.youtube.com/watch?v=aVwxzDHniEw Do some googling, there's lots out there about BSplines. I have a 300 page book about them!

If you have a specific thing you want to do with your BSpline, we can likely point you at the method that does the job.
smktec
Posts: 327
Joined: Thu Mar 05, 2020 5:37 pm

Re: knots and direction of B-Spline

Post by smktec »

thanks for your clear explanation and your references (video). This helped me a lot. I have never been in contact with B-Splines in the past.
Seems to be a great tool
Post Reply