[Solved] BSPlineCurve insert Knots question.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

[Solved] BSPlineCurve insert Knots question.

Post by onekk »

Trying to solve this problem, using scripting:

https://forum.freecadweb.org/viewtopic. ... 92#p618192


I've put together the code linked below.

I've inserted some knots, as it seems that the insertion is possible using already available methods insertKnot

Code: Select all

bs0c.insertKnot(0.10)
bs0c.insertKnot(0.333333333333333)
bs0c.insertKnot(0.666666666666666)
bs0c.insertKnot(0.75)
I know it is rough but I wonder if there is a method to equally spacing points to avoid the bend of the curve.

What happens to poles?

Or better how to add control points to equally spaced along the curve?

Probably I'm not guessing something correct as I can't get to work the method removeKnot
20220816-bspline_addp.py
(3.92 KiB) Downloaded 32 times
Regards

Carlo D.

Edited to try to make the question and the title more clear
Last edited by onekk on Sat Aug 20, 2022 8:43 am, edited 1 time in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSPlineCurve insert Knots question.

Post by onekk »

Hoping to be more precise:

I have modified the code, to be more articulated, to show some data:
bspline_knots.png
bspline_knots.png (4.15 KiB) Viewed 930 times
The above is a try to visualize the position of the knots on the curve.

But I've a doubt:

These are the knots of the "upper bspline" after having added some knots using insertKnots

Code: Select all

bs0c Knots:  [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
I visualize them using:

Code: Select all

vtx = Part.Vertex(spline.value(knt))
Where knt is the knot obtained from the above "Knot vector"

is correct to assume that the value returned from the "Knot Vector" is the u parameter of Part.BSplineCurve.value()?

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSPlineCurve insert Knots question.

Post by onekk »

Some more iterations and some simplified code:

Added a sort of polygon visualization, as seen on BSpline explanation.
bspline_ana1t.png
bspline_ana1t.png (6 KiB) Viewed 883 times
Shortened the code to include only relevant bspline (limiting to one BSpline for clarity)
20220818-bspline_analysis.py
(4.02 KiB) Downloaded 29 times
Hoping this is correct.


In the meantime, I'm studying some CG text found on Accademia.edu, for which a subscriptions is needed

COMPUTER AIDED GEOMETRIC DESIGN - Thomas W. Sederberg - October 9, 2014

But is rather advanced for my math skills, see what happens and if my brain will not smoke. :-D

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: BSPlineCurve insert Knots question.

Post by Chris_G »

onekk wrote: Wed Aug 17, 2022 5:26 pm Trying to solve this problem, using scripting:
https://forum.freecadweb.org/viewtopic. ... 92#p618192
I've put together the code linked below.
I've inserted some knots, as it seems that the insertion is possible using already available methods insertKnot
I don't understand what you're trying the achieve by inserting knots ?
onekk wrote: Wed Aug 17, 2022 5:26 pm I know it is rough but I wonder if there is a method to equally spacing points to avoid the bend of the curve.
What points ? The points on curve corresponding to the knots ?
No. A knot is just a parameter in the parametric space of the curve.
A parameter has a corresponding 3D point image on the curve (Curve.value(u))
And a 3D point on the curve has a corresponding parameter in the parameter range (Curve.parameter(point))
You can get equally spaced points on curve with :

Code: Select all

pts = curve.discretize(number_of_points)
And get back the corresponding parameters with:

Code: Select all

params = [curve.parameter(p) for p in pts]
onekk wrote: Wed Aug 17, 2022 5:26 pm What happens to poles?
When you insert a knot ? A new set of poles is computed.
onekk wrote: Wed Aug 17, 2022 5:26 pm Probably I'm not guessing something correct as I can't get to work the method removeKnot
Inserting a knot is always possible.
Removing a knot usually need to approximate the curve.
But I have experienced that removeKnot often fails, even with very high tolerance values. I don't know why.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSPlineCurve insert Knots question.

Post by onekk »

Chris_G wrote: Thu Aug 18, 2022 1:55 pm I don't understand what you're trying the achieve by inserting knots ?
I was trying to find a solution to this question:

https://forum.freecadweb.org/viewtopic. ... 92#p618192

Probably in a wrong approximation I could think that the curve is bent due to the irregular spacing of bspline poles.

More generally I'm trying to guess the implications of making surface between two bsplines, using "ruled surfaces" could be a solution, but I could not find a way to "segment" the bspline in a manner that permit to create ruled surfaces that not present the bend shown in the images.


With other curves I could create "curve segments" in both curves and use them to try to avoid delicate segments that cause "bends" or twistings.

I have thought that this could be possible adding poles as result of adding knots to the bspline curve.

Chris_G wrote: Thu Aug 18, 2022 1:55 pm What points ? The points on curve corresponding to the knots ?
No. A knot is just a parameter in the parametric space of the curve.
A parameter has a corresponding 3D point image on the curve (Curve.value(u))
And a 3D point on the curve has a corresponding parameter in the parameter range (Curve.parameter(point))
You can get equally spaced points on curve with :

Code: Select all

pts = curve.discretize(number_of_points)
And get back the corresponding parameters with:

Code: Select all

params = [curve.parameter(p) for p in pts]
...
When you insert a knot ? A new set of poles is computed.
Thanks, this is an advancement in my knowledge, (Sorry but I'm "math impaired" :D )
Chris_G wrote: Thu Aug 18, 2022 1:55 pm Inserting a knot is always possible.
Removing a knot usually need to approximate the curve.
But I have experienced that removeKnot often fails, even with very high tolerance values. I don't know why.
Thanks again another information that is difficult to have, from the explanation in both OCCT documentation and FC source code (That not surprisingly is very similar to the OCCT documentation) it seems possible in some cases.

Regards

Carlo D.
Last edited by onekk on Thu Aug 18, 2022 3:00 pm, edited 1 time in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSPlineCurve insert Knots question.

Post by onekk »

Some new code:
20220816-bspline_addp.py
(5.19 KiB) Downloaded 36 times
Thanks to @Chris_G invaluable help.

This lead to this:
bsplines_seg.png
bsplines_seg.png (58.39 KiB) Viewed 813 times
Hope this could be thought as a solution.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSPlineCurve insert Knots question.

Post by onekk »

Above solution lead to another question?

is it possible to confront the two curves, the original bspline and the "segmented one".

Visually they are different, but I suspect that this could be due even to the different approximation made during the "visualization phase"?

EDIT
even surfaces seems different, so probably is not the best solution.
END EDIT

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: BSPlineCurve insert Knots question.

Post by Chris_G »

onekk wrote: Thu Aug 18, 2022 2:59 pm Some new code:
...
This lead to this:
Great Job !
onekk wrote: Thu Aug 18, 2022 3:13 pm is it possible to confront the two curves, the original bspline and the "segmented one".
Visually they are different, but I suspect that this could be due even to the different approximation made during the "visualization phase"?
I think a segmented curve perfectly matches its parent curve.
onekk wrote: Thu Aug 18, 2022 3:13 pm EDIT
even surfaces seems different, so probably is not the best solution.
END EDIT
Well, that was exactly the goal, no ?
Build a different surface that looks more natural and avoids the twist.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSPlineCurve insert Knots question.

Post by onekk »

Chris_G wrote: Thu Aug 18, 2022 4:15 pm Great Job !
Thanks but your contribution is relevant, many of my knowledge about BSplines are from you, @chrisb and few others guys.

Did you know a viable way to confront the two curves, the original bspline and the "segmented one" to see if they overlap without checking all points.


Chris_G wrote: Thu Aug 18, 2022 4:15 pm I think a segmented curve perfectly matches its parent curve.
Do you know a way to check if a curve (the segments) is laying on another curve (the original one)?
Chris_G wrote: Thu Aug 18, 2022 4:15 pm Well, that was exactly the goal, no ?
Build a different surface that looks more natural and avoids the twist.
In fact visually it is good, when needed it could be found a minimal number of segments that will do the job.

Thanks again and Regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSPlineCurve insert Knots question.

Post by onekk »

edwilliams16 wrote: Wed Aug 17, 2022 8:11 pm
Do you have some idea about the question above:

how to check if "BSPline segments" that form the new "segmented wire" are overlapping the original BSPline.

Sorry for bothering and for the not so clear question.

Regards

Carlo D.
Last edited by onekk on Thu Aug 18, 2022 7:12 pm, edited 1 time in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply