Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

New Featurescript Bezier curve. But with a twist. It has Degree control.

Jacob_CorderJacob_Corder Member Posts: 137 PRO
edited May 20 in FeatureScript
I found that in the new bridging curve feature that they use a function called elevateBezier

Here is the feature to create a Bezier curve of up to degree 15 with no change in the shape. I believe @S1mon was asking for this

https://cad.onshape.com/documents/31bc937206006a84d394b252/w/64931344e2a04480f7d0eebc/e/84dff331bec6ad9f5d5e39ae

I cannot take the credit for it as the code just calls onshapes function but was not exposed in this way. 

What is this features purpose? I have no idea :smile:

It also allows you to select an edge as the source the same as fit spline but only one edge



Comments

  • S1monS1mon Member Posts: 2,982 PRO
    Yes. The real trick is to have a way to edit the curve once it's been elevated. Ideally this would be in the context of a 3D sketcher.

    @GregBrown and the team have clearly been working on curve tools.

    I hope to see some of the approximation and tweaking tools find their way into Onshape, or at least get released to the general public.
  • Jacob_CorderJacob_Corder Member Posts: 137 PRO
    @S1mon

    Yeah.  Elevating it direct would be cool, but onshape does not allow the curves data to be modified(direct editing operations)

    Regarding the youtube videos.

    They are likely calling approximateSpline function in those videos for reducing the point count on those curves. That function takes the points and minimizes curvature (basically number of control points) and also allows for creating higher degree curves.

    I use approximateSpline in almost all of my spline creation functions that I create for my clients.  It is the fastest and only feasible way to do a degree 5 fit spline or up to degree 15 fit spline. It really is a powerful function and it is fast. Its true purpose is to generate loft profiles as it takes in points in groups to generate loft profiles of equal control point count to allow the loft feature to execute with higher quality surfaces.

    Fun fact. If you pass in a degree 5 spline to boundarySurface, It will generate a degree 5 surface in that splines direction. Try it with the new bridging curve and the new g3 options and create a boundary surface. The result will be degree 7 b spline surface in one of the directions. if you create the same curves in the other direction you will get a degree 7 x degree 7 b spline surface. 
  • GregBrownGregBrown Member, Onshape Employees, csevp Posts: 197
    Jacob, 

    My features perform the elevation or reduction using standard Bezier math, indeed I implemented my own elevateBezier initially, but just replaced it with the std library one that was recently added. The reduction uses a least squares method to try and maintain shape. In the video there are some other features (the Approximate projected curve) the do indeed use approximateSpline. BTW I agree this is a hugely useful function that more people should use as opposed to opFitSpline. There are some custom features that would really benefit from a refactoring!

    I haven't shared mine publicly yet, as I'm continuing to iterate for a bit....
  • Jed_YeiserJed_Yeiser Member Posts: 37 PRO
    I often work importing .csv files that will create between 10 and 100 curves with each of those curves having between 2 and 200 points. After watching some of @GregBrown's recent videos, I decided to take a shot at using approximateSpline and find the minimum number of control points that must be used to achieve the desired tolerance. I'm HOPING that this helps solve some issues we encounter with 'noisy' surfaces. 

    @GregBrown - I'm very much looking forward to seeing your code once you're ready to release. My code appears to be working, but its very slow (~10seconds runtime) - I'll be curious if/where your approach differs and captures efficiencies. I have zero experience with Bezier math (though I suspect that's about to change....), so I went about it with the tools I have on hand. 

    The first piece of information you need is 'how are you going to measure tolerance'? I'm not sure how approximateSpline calculates tolerance, so I created an enum in my feature that lets me specify if the user-defined tolerance refers to: 1: Maximum deviation from the curve, as measured from the .csv's input points and 2: RMS of the distances between the approximate spline and the .csv's input points. I wrote a quick and dirty function to calculate those values taking a curve body query and an array of points as input. 

    Create an array that will contain sub-arrays. Each row of the array corresponds to one 'guess' for the minimum value allowable in "maxControlPoints" while still staying in the tolerance window. Initialize this array with [0] -> the original number of points in the spline (same as the number of points in the input .csv file) and [1] -> the integer closest to 2/3 of this original value (I'd originally set this as half the number, but given the extreme non-linearity of the 'Tolerance to number of control point' curve, I've found better results with 2/3. 

    Now iterate, saving the number of control points, curve query and calculated delta in a sub-array in each iteration.

    The value that we are trying to get to approach zero is the DIFFERENCE between the specified tolerance and the evaluated tolerance: (e.g) var diff = (UIspecifiedTolerance - evaluatedErrorForThisIteration'sCurve). When this value is negative, the evaluated error is LARGER than the user specified tolerance. If this value is exactly equal to 0, than the error of the approximated spline exactly matches the user specified tolerance. I found newton-rhapson iteration to be a bit brittle. I'm updating guesses by evaluating the following variables and making the following comparisons in each iteration:

    var numControlPoints = number of control points for the approximated spline in each iteration;&nbsp;<br>var evaluatedCurveError = error between the input points from the csv file and the approximated spline;&nbsp;<br>var highMax = number of control points closest to zero with an evaluated error (UIDefinedTolerance - evaluatedCurveError) greater than 0;<br>var lowMax = number of control points closest to zero with an evaluated error (UIDefinedTolerance - evaluatedCurveError) less than 0;<br><br>if (evaluatedCurveError < 0 * millimeter)<br>{<br>&nbsp; &nbsp; &nbsp;nextGuess = ceil((highMax + numControlPoints)/2, 1);<br>}<br>else<br>{<br>&nbsp; &nbsp; &nbsp;nextGuess = ceil((lowMax + numControlPoints)/2, 1)<br>}


    This will converge on one integer value for the 'correct' number of control points. I break out of the loop for each curve when the array containing the number of control points for each guess has a duplicate value (which it ends up landing on because of the ceil function). 

    The results have been astonishing. As you'd expect, the wider the tolerance is set, the higher the reduction in control points. I've attached a very quick and dirty summary below - please note that the X axis is a logarithmic scale. With a 10mm tolerance, I can reduce 94% of the control points. with a .01mm tolerance, I can reduce 21% of the control points. 



    I hope this helps!
     
  • S1monS1mon Member Posts: 2,982 PRO
    I assume you've seen the built-in FeatureScript support for approximating a spline?


  • Jacob_CorderJacob_Corder Member Posts: 137 PRO
    @Jed_Yeiser
    if you specify interpolateIndices as every index range(0,size(points)-1) then the spline will interpolate through those points and compute the b spline curve for this Fit spline up to degree 15
  • Jed_YeiserJed_Yeiser Member Posts: 37 PRO
    Thanks @Jacob_Corder and @S1mon. I've gotten pretty wrapped around the axle on this and realized that my approach was ... let's just say frantic and poor. This all kicked off when I made an approximated spline using the approximateSpline method and saw fairly insignificant reductions in the number of control points. At this point, I convinced myself that the reason I was getting higher degree splines wasn't the fact that my geometry required that number of control points, but that I just  hadn't found the most reduced form of the spline that falls within the specified tolerance - and of I went on an ill-informed adventure. After removing the code I'd added to reduce the number of control points, I'm seeing about a 9% reduction in control points with degree-three splines and a tolerance set to 0.0001mm.  

    Thanks!
    -j


Sign In or Register to comment.