PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Fast Bezier Curves

I'm searching for source code for fast bezier curve drawing. Any assistance would be appreciated!

Dan East
There's a GBA bezier implementation (with source) here (bezier.zip):
http://www.bits.bris.ac.uk/dooby/gba/

The bezier code itself is in ARM assembler.
There is a site http://membres.lycos.fr/abrobecker/ that describes a method to do different types of interpolation with only addition. And it's a site by an ARM programmer too!

I know it's only interpolation, but faster interpolation would mean faster drawing, right?

His site also has an article on fast circle filling, an algorithm which might be adapted for other implicitly defined figures (i.e. defined by a function such as f(x,y)<0).

I actually wrote a Perlin Noise generator a while back using this technique to calculate a high-quality (cubic interp that is) fixed-res Perlin Noise for the PPC. I can post the source and/or binaries somewhere if anyone wants it. It also has a *very* simple script parser (two actually, a C-like one and one that is very similar to FORTH). If you want it, be forewarned, the coding is *very very very* bad.

And if I understand it right, you could probably use it to make a mix between persp-correct texmapping and affine. Just calculate the deltas for a quadratic (or cubic or higher if you want) approximation of the texture co-ords, and use this technique to interpolate. No divs or muls, and it would probably look better than affine.

[edit]Oops, I took a while to post and was beaten to it
You used the secret words, 'perspective correct texture mapping', so now I have to respond. :)

Doing perspective correct texture mapping by simulating u/z and v/z using a higher order formula may work pretty well, but figuring out that formula will take longer than a couple of divs, making this approach inefficient for all but the largest polygons.

There. I said it.
BTW, I once did a texturemapper that did not just use 16-pixel spans, but 16x16 tiles. Sounds like a good idea, but it looks like crap. OK, back to the bezier topic. :)
Okay, I've got a decent bezier algorithm in place.

Now I'm in need of something else. :) Unfortunately google searches are too muddled for me to find what I need.

What I'm seeking is a general drawing algorithm for stroking a path. The idea is to specify a radius for the pen, then apply it to a path, such as a line or bezier curve, to draw the element with a thickness. I'm aware of a few other variables to such drawing, such as the cap style at the ends of lines, as well as joint styles for 90 degree angles.

I will probably revert to writing my own routine, but I'm certain this problem as been approached intelligently by others with greater mathematical skills than I. Of course performance is important. :)

Alternately, a fast algorithm for drawing a line with some thickness greater than 1 would probably suffice, as I could modify for other uses.

Thanks!

Dan East
I tried searching on Google Groups and found a couple things that might help:
a) Thick lines can be drawn using scan-conversion of a polygon. There is one topic about this at http://groups.google.com/groups?hl=en&lr;=&ie;=UTF-8&threadm;=01bbb7b3%24e55c96e0%243268a5c6%40dpicco.nlnet.nf.ca&rnum;=3&prev;=/groups%3Fq%3Dthick%2Bline%2Bgroup:comp.graphics.algorithms%26hl%3Den%26lr%3D%26ie%3DUTF-8%26selm%3D01bbb7b3%2524e55c96e0%25243268a5c6%2540dpicco.nlnet.nf.ca%26rnum%3D3
b) There is supposedly an article in Graaphic Gems that talks about thick line rendering.
c) There are a whole lot of posts relating to this. You aren't the only one who has no clue how to do it.
Thanks billcow!

Has anyone done a comparison between Bresenham and Xiaolin Wu's 2-step line drawing algorithms?

http://www.cs.unc.edu/~mcmillan/comp136 ... Lines.html

"The two-step algorithm takes the interesting approach of treating line drawing as a automaton, or finite state machine. If one looks at the possible configurations that the next two pixels of a line, it is easy to see that only a finite set of possiblities exist.
The two-step algorithm shown here also exploits the symmetry of line-drawing by simultaneously drawn from both ends towards the midpoint. "

Dan East
I think I'd investigate run-slice line drawing described by Abrash in one of his Zen of ... books. I'd probably alter it so that I'm always drawing in a cache-friendly manner though.
I'll go ahead and post my fixed-integer converted bezier function. You have to supply your own line drawing routine. Also, additional optimization is certainly possible. The fixed point version only costs two extra integer divisions per output vertex compared to the original floating point version.

Code:

struct gfxPOINT {
int nX;
int nY;
};

#define BZS 120 //Bezier Scaler for fixed point math

void DrawBezier(gfxPOINT *VertexPtr, unsigned short Color,
int XOffset, int YOffset, unsigned short *outbuf, int outbuf_w, int outbuf_h) {

//Dan: TODO: We can precalc several mults to constants that occur each iteration
//IE: VertexPtr[0].nX*(BZS*BZS*BZS)
int k=BZS/30;

int x1,x2,y1,y2;
x1 = VertexPtr[0].nX;
y1 = VertexPtr[0].nY;
for(int t=k;t<=BZS+k;t+=k){
//use Berstein polynomials
x2=(VertexPtr[0].nX*(BZS*BZS*BZS) +
t*(VertexPtr[0].nX*(-3*BZS*BZS)+t*((3*BZS)*VertexPtr[0].nX-VertexPtr[0].nX*t)))+

t*(VertexPtr[1].nX*(3*BZS*BZS)+ t*((-6*BZS)*VertexPtr[1].nX+VertexPtr[1].nX*3*t))+

t*t*(VertexPtr[2].nX*(3*BZS)-VertexPtr[2].nX*3*t)+

t*t*t*VertexPtr[3].nX;
x2/=(BZS*BZS*BZS);

y2=(VertexPtr[0].nY*(BZS*BZS*BZS) +
t*(VertexPtr[0].nY*(-3*BZS*BZS)+t*((3*BZS)*VertexPtr[0].nY-VertexPtr[0].nY*t)))+

t*(VertexPtr[1].nY*(3*BZS*BZS)+t*((-6*BZS)*VertexPtr[1].nY+ VertexPtr[1].nY*3*t))+

t*t*(VertexPtr[2].nY*(3*BZS)-VertexPtr[2].nY*3*t)+

t*t*t*VertexPtr[3].nY;
y2/=(BZS*BZS*BZS);

//draw curve segment
DrawLine(x1, y1, x2, y2, Color, XOffset, YOffset, outbuf, outbuf_w, outbuf_h);
x1 = x2;
y1 = y2;
}
}
The fastest line-draw routines always used to be straight branches into totally unrolled code - why have a 2-step algorithm when you can keep track of the deltas and plot 320 pixels at a cost of (IIRC) 4 instructions each ;) (ok, so it tramples the cache badly :)).

Abrash's line-drawing stuff was covered in his black book, which was released in PDF format on the web last year (legally). I can tell you what chapter it was on Monday (or send you the PDFs of the chapters), if you like.

Cheers,

Ref.
Thanks refractor. I've pored over Abrash's books quite a bit, and have most of the chapters of interest (to me) in pdf format. :)

Dan East