[comp.lang.postscript] Spline data points to Bezier curve

cse0080@desire.wright.edu (06/03/91)

Help!

I need to know how to generate spline curves in PostScript when
all the data I have on hand is an array of data points that lie on 
the curve itself.  (IE: no control points for the curveto operator)

Anyone know a good way to do this?

Thanks for your assistance.

John Hansen
cse0080@wsu.bitnet
cse0080@desire.wright.edu

dgc@cs.purdue.EDU (Doug Crabill) (06/04/91)

In article <1991Jun3.095901.3793@desire.wright.edu> cse0080@desire.wright.edu writes:

>   Help!
>
>   I need to know how to generate spline curves in PostScript when
>   all the data I have on hand is an array of data points that lie on 
>   the curve itself.  (IE: no control points for the curveto operator)
>
>   Anyone know a good way to do this?
>
>   Thanks for your assistance.
>
>   John Hansen
>   cse0080@wsu.bitnet
>   cse0080@desire.wright.edu

I had a need for this some time ago and hacked my way to a solution.
I knew that InterViews and the idraw editor in particular somehow
performed the transformation from spline data to a form PostScript
would like.  So I hacked the idraw PostScript prolog till I had a
minimum copy of their BSpl function.  I made no attempt to optimize or
modify what they have done.  All credit goes to the authors of
InterViews for this code -- I have only cut and pasted this solution.
I've added an example of how to call BSpl at the end.

Doug Crabill
Department of Computer Science
Purdue University
dgc@cs.purdue.edu


%!
/BSpl {
0 begin
storexyn
newpath
n 1 gt {
0 0 0 0 0 0 1 1 true subspline
n 2 gt {
0 0 0 0 1 1 2 2 false subspline
1 1 n 3 sub {
/i exch def
i 1 sub dup i dup i 1 add dup i 2 add dup false subspline
} for
n 3 sub dup n 2 sub dup n 1 sub dup 2 copy false subspline
} if
n 2 sub dup n 1 sub dup 2 copy 2 copy false subspline
stroke
} if
end
} dup 0 4 dict put def

/midpoint {
0 begin
/y1 exch def
/x1 exch def
/y0 exch def
/x0 exch def
x0 x1 add 2 div
y0 y1 add 2 div
end
} dup 0 4 dict put def

/thirdpoint {
0 begin
/y1 exch def
/x1 exch def
/y0 exch def
/x0 exch def
x0 2 mul x1 add 3 div
y0 2 mul y1 add 3 div
end
} dup 0 4 dict put def

/subspline {
0 begin
/movetoNeeded exch def
y exch get /y3 exch def
x exch get /x3 exch def
y exch get /y2 exch def
x exch get /x2 exch def
y exch get /y1 exch def
x exch get /x1 exch def
y exch get /y0 exch def
x exch get /x0 exch def
x1 y1 x2 y2 thirdpoint
/p1y exch def
/p1x exch def
x2 y2 x1 y1 thirdpoint
/p2y exch def
/p2x exch def
x1 y1 x0 y0 thirdpoint
p1x p1y midpoint
/p0y exch def
/p0x exch def
x2 y2 x3 y3 thirdpoint
p2x p2y midpoint
/p3y exch def
/p3x exch def
movetoNeeded { p0x p0y moveto } if
p1x p1y p2x p2y p3x p3y curveto
end
} dup 0 17 dict put def

/storexyn {
/n exch def
/y n array def
/x n array def
n 1 sub -1 0 {
/i exch def
y i 3 2 roll put
x i 3 2 roll put
} for
} def


% Now make a spline with four data points

100 100
150 150
200 125
250 300
4 BSpl
showpage

dgc@cs.purdue.EDU (Doug Crabill) (06/05/91)

>    In article <1991Jun3.095901.3793@desire.wright.edu> cse0080@desire.wright.edu writes:
> 
>    >   Help!
>    >
>    >   I need to know how to generate spline curves in PostScript when
>    >   all the data I have on hand is an array of data points that lie on 
>    >   the curve itself.  (IE: no control points for the curveto operator)
[deleted]
> 
>    I had a need for this some time ago and hacked my way to a solution.
>    I knew that InterViews and the idraw editor in particular somehow
>    performed the transformation from spline data to a form PostScript
>    would like.  So I hacked the idraw PostScript prolog till I had a
>    minimum copy of their BSpl function.
[deleted]

Two people have (correctly) pointed out that the BSpl solution I
posted is not a solution to the problem as stated.  The curve
generated by BSpl is guaranteed to contain only the first and last
data points and generally does not contain the intermediate data
points, but passes near them.  I do not have a solution for the
problem as stated.

The behavior matches that of idraw's "splines".  Try creating a spline
in idraw and you will see that only the first and last data points
actually lie on the curve.

Sorry for the confusion.

Doug

larry@csccat.cs.com (Larry Spence) (06/06/91)

In article <1991Jun3.095901.3793@desire.wright.edu> cse0080@desire.wright.edu writes:
>Help!
>
>I need to know how to generate spline curves in PostScript when
>all the data I have on hand is an array of data points that lie on 
>the curve itself.  (IE: no control points for the curveto operator)
>
>Anyone know a good way to do this?

The method will depend on what flavor of spline was used, uniform or
non-uniform (I'm assuming that it's cubic and non-rational).  If it's
non-uniform, you'll need to know what form of parametrization was used, 
plus the end conditions (there are some weird ones out there!).  You might 
take a look at Farin's book on curves and surfaces as a starting point.

That may sound nitpicky, but I had to convert a bunch of Kanji character
outlines that were in interpolating-spline form to PostScript, and found
that all sorts of bizarre assumptions were made about the end curvatures,
etc.  I found out _after_ I had written a straightforward conversion
algorithm, of course.  Duhhhhh.... %(

-- 
Larry Spence
larry@csccat.cs.com
...{uunet,texsun,cs.utexas.edu,decwrl}!csccat!larry

davis@3d.enet.dec.com (Peter Davis) (06/07/91)

In article <1991Jun3.095901.3793@desire.wright.edu> cse0080@desire.wright.edu writes:
>Help!
>
>I need to know how to generate spline curves in PostScript when
>all the data I have on hand is an array of data points that lie on 
>the curve itself.  (IE: no control points for the curveto operator)
>
>Anyone know a good way to do this?
>
There are a number of ways to approach this.  Probably the simplest thing to do
is get the book _Graphics Gems_, editted by Andrew Glassner, Academic Press.
Look at the gem "An Algorithm For Automatically Fitting Digitized Curves," by
Philip J. Schneider, pp. 612-626.  You may not want to wade through all the math
and the pseudo-code in the article, but the C source code is available on the
net, and is listed in the back of the book.  This code can, with very little
effort, be made to generate PostScript.

cet1@cl.cam.ac.uk (C.E. Thompson) (06/07/91)

In article <1991Jun3.095901.3793@desire.wright.edu> cse0080@desire.wright.edu writes:
>I need to know how to generate spline curves in PostScript when
>all the data I have on hand is an array of data points that lie on 
>the curve itself.  (IE: no control points for the curveto operator)
>
You could do worse than look at the algorithms used by METAFONT to 
choose control points for partially specified paths. They are due to
John Hobby, and are justified in a Stanford U technical report, but
if you just want the formulae you can find them on pp. 130-131 of the
METAFONTbook.

Chris Thompson
JANET:    cet1@uk.ac.cam.phx
Internet: cet1%phx.cam.ac.uk@nsfnet-relay.ac.uk