Curve fitting with Skia

576 views
Skip to first unread message

voidcoder

unread,
Sep 2, 2018, 6:37:43 AM9/2/18
to skia-discuss

Dear Skia community

Does anyone know about a built-in Skia API, primitive, reference code or example on implementing 'curve fitting' with Skia? This is a fairly common vector task. Given an array of SkPoints points or a linear SkPath consisting of line segments turn it into a smoothed rounded SkPath.

Thanks for any pointers!

Hal Canary

unread,
Sep 2, 2018, 9:13:58 AM9/2/18
to skia-discuss
It sounds like you might want to computer a cubic b-spline from your data, then represent it as a sequence of cubic curves. 

I suspect the math to do that is straightforward, but I don't know how off the top of my head. 

Skia does not, as far as I know, have such functionality now. But I can see how useful it might be for nicely displaying the graph of a smooth function at high resolution, but only calculating a finite number of function values ahead of time. I've wanted something like that before. 

voidcoder

unread,
Sep 2, 2018, 9:41:50 AM9/2/18
to skia-discuss

I need this primarily for some freehand drawing experiments. You give it an array of digitised mouse/touch coordinates and the output is a path consisting of smooth cubic bezier segments. My understanding this is knows as Curve Fitting (https://en.wikipedia.org/wiki/Curve_fitting). 

After some googling around it turns most implementations are based on some variation of Philip J. Schneider Fit Curve algorithm described in Graphics Gems, 1990.

Some other vector libs I payed with in the past like e.g. Inkscape lib2geom have this functionality out of the box. I'm not quite sure how to integrate something similar with Skia.

BTW another useful basic API I seems can't locate in Skia is finding the overall path contour length and also finding a point on the path contour at a specified percentage of path length. Had a good look through documentation and headers and seems can't find anything of a sort. Am I missing something obvious or such API doesn't exist?

Thank you Hal!  

Florin Malita

unread,
Sep 2, 2018, 10:02:37 AM9/2/18
to skia-d...@googlegroups.com
On Sun, Sep 2, 2018 at 9:41 AM voidcoder <void...@gmail.com> wrote:

I need this primarily for some freehand drawing experiments. You give it an array of digitised mouse/touch coordinates and the output is a path consisting of smooth cubic bezier segments. My understanding this is knows as Curve Fitting (https://en.wikipedia.org/wiki/Curve_fitting). 

After some googling around it turns most implementations are based on some variation of Philip J. Schneider Fit Curve algorithm described in Graphics Gems, 1990.

Some other vector libs I payed with in the past like e.g. Inkscape lib2geom have this functionality out of the box. I'm not quite sure how to integrate something similar with Skia.

BTW another useful basic API I seems can't locate in Skia is finding the overall path contour length and also finding a point on the path contour at a specified percentage of path length. Had a good look through documentation and headers and seems can't find anything of a sort. Am I missing something obvious or such API doesn't exist?

Sounds like you're looking for SkPathMeasure.  This is a simple example using it for total path length computation.

 

Thank you Hal!  


On Sunday, 2 September 2018 14:13:58 UTC+1, Hal Canary wrote:
It sounds like you might want to computer a cubic b-spline from your data, then represent it as a sequence of cubic curves. 

I suspect the math to do that is straightforward, but I don't know how off the top of my head. 

Skia does not, as far as I know, have such functionality now. But I can see how useful it might be for nicely displaying the graph of a smooth function at high resolution, but only calculating a finite number of function values ahead of time. I've wanted something like that before. 

On Sun, Sep 2, 2018, 06:37 voidcoder <void...@gmail.com> wrote:

Dear Skia community

Does anyone know about a built-in Skia API, primitive, reference code or example on implementing 'curve fitting' with Skia? This is a fairly common vector task. Given an array of SkPoints points or a linear SkPath consisting of line segments turn it into a smoothed rounded SkPath.

Thanks for any pointers!

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To post to this group, send email to skia-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

voidcoder

unread,
Sep 2, 2018, 10:09:12 AM9/2/18
to skia-discuss

Exactly! Thank you Florin!

Hal Canary

unread,
Sep 3, 2018, 2:52:03 PM9/3/18
to skia-discuss
I had to hack something up on my day off:

--

voidcoder

unread,
Sep 3, 2018, 4:18:43 PM9/3/18
to skia-discuss

You are a star! Nice and clean. Thanks a lot Hal!

Perhaps this can be integrated into Skia lib at some point.

Hal Canary

unread,
Sep 4, 2018, 10:41:30 AM9/4/18
to skia-discuss
The previous version had a typo in the code.  Here's the fixed version, also cleaned up:
On Mon, Sep 3, 2018 at 4:18 PM voidcoder <void...@gmail.com> wrote:

You are a star! Nice and clean. Thanks a lot Hal!

Perhaps this can be integrated into Skia lib at some point.


On Monday, 3 September 2018 19:52:03 UTC+1, Hal Canary wrote:
I had to hack something up on my day off:

voidcoder

unread,
Sep 4, 2018, 10:48:19 AM9/4/18
to skia-discuss
Thanks Hal! 

Hal Canary

unread,
Sep 4, 2018, 2:04:25 PM9/4/18
to skia-discuss
It has really bad results.
9dd4508b999005dcc1318071d61ac809_raster.png

--

Jim Van Verth

unread,
Sep 4, 2018, 2:37:11 PM9/4/18
to skia-discuss
Catmull-Rom might also work: https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Catmull%E2%80%93Rom_spline. In the past I've used a quadratic to compute the end tangents.
Reply all
Reply to author
Forward
0 new messages