thibauts / b-spline Goto Github PK
View Code? Open in Web Editor NEWB-spline interpolation
License: MIT License
B-spline interpolation
License: MIT License
Hello! I've found this repository a couple of days ago when searching for nurbs curve implementations for C#, and I really liked how simple to understand the source code is and how easy is to use.
I've found a strange behaviour when using closed loop curves (repeating the first degree + 1 points at the end of the points array, as in the example), that the end point (t = 1) doesn't match start point (t = 0), actually it overshoots a bit in my tests (random generated 2D points with degree = 3, auto generated knots and weights).
To better illustrate, there's a video of drawing the spline in a canvas:
https://user-images.githubusercontent.com/43936806/125488852-15926514-f731-4173-929e-60493b18691d.mp4
Here's the files used for the test:
BSpline Test.zip
When this part of the codebase runs:
Lines 36 to 40 in fdc3a31
For example with these values, we get an error:
let t = 1;
const high = 83.50120024816177;
const low = 1.545996015332399
t = t * (high - low) + low;
// t is now 83.50120024816178 - higher than high!= by 0.00000000000001
if(t < low || t > high) throw new Error('out of bounds');
subject says it all really.
@thibauts nothing super creative here, just figured it'd be worth commiting before I ever so slightly simplify index.js by removing the rebinding on weights[i]
to v[i][d]
, since we never discard weights[i]
and only call v[s][d]
all the way at the end, where weights[s]
does the exact same thing.
Realize the control (mouse click) point b-spline curve,
Just for anyone who ran into this issue, I was using the docs as a reference for this package (which is nice + simple to use with clear explanation + diagrams, thanks!) and ran into the issue that I was using a clamped knot vector to force the first/last curve points to exactly match the control points, BUT initially the final control point wasn't actually being included when I used the iteration loop as per the docs.
Turns out it's because the doc example was incrementing by 0.01 in the interpolation loop, but because of the numeric representation in JS, adding 0.01 100 times does NOT result in 1 ! (it's like 0.999). Because of this the last point (which would have been when t === 1) was not actually being hit.
For anyone that runs into this issue, just interpolate across an integer range (like 0 < t <= 100) and then /100 for the bspline funcs.
Hope that helps someone :)
It'd be great if there was a built-in option to reparameterize the spline based on arc length, so that it has continuous velocity. See this page for an explanation.
I've implemented this, I'd be happy to submit a pull request, if you think it'd be useful.
Oh and just wanted to say thanks for this; it's by far the easiest and nicest-looking path smoothing solution out there, at least for the sorts of things I do!
Any chance the results could also include the tangent at t
?
I've just found your code and think that it is really good. Currently I'm trying to interpolate Autodesk's dxf file splines and your code comes really close except that Autodesk defines the end tangents of their splines and the length of their knot value fails your test on line 26.
I've been trying to figure out how to implement having the end tangent defined. Is there a way to add extra control points to have this work out correctly or should it be done in a different way?
I've been working on this for a while and still have not been able to figure it out. Please let me know if you know any way to interpolate this kind of spline.
Thank you
Here is an example of an Autodesk b-spline:
Interpolation using your b-spline library
Spline Properties
degree
3
starttangent
[-0.4138029443011841, 0.9103664774626047]
angle 1.9974238199217729 rad
endtangent
[0.8422714006615115, 0.539053696423367]
angle 0.5693131911006616 rad
controlpoints
[[2.399185827365247, 3.556238220882021], [1.79819140306395, 4.878425954344872], [3.793761079285986, 10.00772218210104], [9.597527631961984, 2.634051205137613], [-1.014386849068146, -0.7575600644211504], [-0.0580977089595729, 7.39060578138084], [1.891006407840181, 8.638032416132681]]
number of controlpoints 7
fitpoints
[[2.399185827365247, 3.556238220882021], [4.039879381831888, 7.592634753109687], [7.045397663022992, 3.861145872597061], [1.411865812287976, 1.712272898605353], [1.891006407840181, 8.638032416132681]]
number of fitpoints 5
knots
[0.0, 0.0, 0.0, 0.0, 4.357105955223939, 9.148467912325835, 15.17792362338283, 22.1202374022988, 22.1202374022988, 22.1202374022988, 22.1202374022988]
number of knots 11
weights
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
number of weights 7
normalvector
(0.0, 0.0, 1.0)
is_closed
False
is_periodic
False
is_rational
False
is_planar
True
is_linear
False
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.