Mapping Bezier Curves Through Points

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
DeadlyDad

Mapping Bezier Curves Through Points

Post by DeadlyDad »

I found the -draw " bezier x0,y0 ... xn,yn" option, and thought that it meant that the curve would be drawn through the points, rather than simply using the points as references.

Unfortunately, that isn't how it works, as the following shows:

Code: Select all

convert -size 230x60 xc:skyblue -fill none -stroke black -draw "bezier  010,010 040,050 070,045 100,015 130,015 160,045 190,050 220,010" -stroke red -draw "polyline 010,010 040,050 070,045 100,015 130,015 160,045 190,050 220,010"  bad_bezier.gif 
Image

To create Bezier curves that include the points, I managed to find Algorithms for Automatically Fitting Digitized Curves at the Graphical Gems Repository. The specific code is FitCurves.c in gems.zip. If someone could fold a version of that code into the main library, I'm sure that a lot of people would find it useful. (For example, converting from raster to vector using the edge points as the source point set, resizing that properly deals with curved lines, etc.)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Mapping Bezier Curves Through Points

Post by fmw42 »

I am no expert on the use of Bezier curves in IM, but see

http://www.imagemagick.org/Usage/draw/#cubic

for details on the use of control points, which you appear not to have used.
DeadlyDad

Re: Mapping Bezier Curves Through Points

Post by DeadlyDad »

fmw42 wrote:I am no expert on the use of Bezier curves in IM, but see
http://www.imagemagick.org/Usage/draw/#cubic
for details on the use of control points, which you appear not to have used.
I guess I didn't make myself clear. What I'd like is a function to draw a set of smooth, logically joined curves through a set of predefined points without requiring user-created control points.

In cases where the arcs between points are very similar and the AB chord isn't on the far side of the center point from C, perhaps a simple three-point-circle solution would work well. You can simply use the determined arc radius & center point with the first two points in the set. If the shape isn't closed, you would just use the last three points for a single arc. I imagine that it would be much faster than calculating Beziers, if not as rigorous.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Mapping Bezier Curves Through Points

Post by fmw42 »

I don't think a 3-point circle would work as the spacing between points is not consistent. Therefore the join between segments will not have continuous slope.

You are basically right that another spline solution is needed. There are two kinds of splines - interpolating and approximating. The former curves pass through the point. The latter does not necessarily - it is just a best fit. You really want the former, but apparently IM and SVG only does the latter. Perhaps there is another better standalone routine. Have you looked at gnuplot? Perhaps it will allow different spline functions.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Mapping Bezier Curves Through Points

Post by anthony »

You are wanting a curve fitting function, rather than a bezier curve.

Unfortunately this is not part of the SVG specification, so will probably not be added to -draw, without breaking its support of SVG standards.

However a new drawing 'primative' could be added to the MVG command set, without breaking the SVG Path Specification.

But unless you like to try and add this yourself and submit a patch (should be reasonably easy C coding for a new MVG primitive), it will probably not get done. IM is only as good as the people who are willing (and have time) to work on it. Yes I am one of those people who add new things to IM (see -layers and -distort), but have a todo list a mile long!

I myself would like to see this sort of function, as it is a lot easier to generate than a bezier curve, though there are a number of methods I have seen. One common one are very similar to cubic bezier, but is based on bi-cubic interpolation of spatial coordinates for line drawing. This is also a type of 'spline' but without control points.

ASIDE: drawing the line will be relativally easy, but what about 'filling' the enclosed area????? That is where you may have difficulty.

One idea I have also seen is to get IM to internally generate, (or have a external script create them) the appropriate control points for a reasonable cubic bezier fit of just the 'pass through' points (one may be on the net already!).

The external script idea has the added advantage of allowing you to do this, but then let you 'tweek' those controls to sharpen or round off the curve for the specific problem. It would be extremely welcome as an addition to the IM example page on the Cubic Bezier primitive, where I have tried to present methods for the display, and adjusting of such a curve, without needing special tools. Also it would for SVG users too, not just IM!

Hey Fred, this sounds right up your alley, with your research knowledge!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Mapping Bezier Curves Through Points

Post by fmw42 »

I have recently created a bash script called spline that will do interpolated splines that pass through the points (kbs, hermite, cubic) as well as approximating splines that only pass near the points (b-spline and bezier). The kbs spline allows tension and bias control. It is a superset for the catmull-rom and cardinal splines.

see http://www.fmwconcepts.com/imagemagick/index.html
Post Reply