Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Distances along Bezier Paths

Distances along Bezier Paths
Thread Tools
Ghoser777
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Aug 20, 2002, 10:02 PM
 
I've been working on a way to make objects in an NSView walk along a bezier path, and all has gone well except for one thing: cubic bezier paths.

lineTo, moveTo, and close operations are easy, but the real trick is curveTo operations.

I have already been able to obtain the four control points, P0 (initial point), P1, P2, and P3 (destination point). I also know of this function:

B(u) = P0*(1-u)^3 + P1*3z*(1-u)^2 + P2*3(z^2)*(1-u) + P3*z^3

That gives me the point for each u in [0,1] on the cubic curve. I'm okay so far.

What I need to be able to do is figure out for what x, the distance between B(x) and B(u) is equal to some distance d. If that distance can't be obtained for some u on some path (you go from u to 1), that's okay. I just need to find out if that is the case.

So, this is tricky because it ends up being a nasty arc length calculus problem of this form:

Integral from u to x of (1 + (F'(u,x))^2)^(1/2)dx = d.

F(u,x) in this case is defined as the distance from B(u) to B(x). This ends up being something like this:

F(x) = ((B(u).x - B(x).x)^2 + (B(u).y - B(x).y)^2)^(1/2)

Okay, and now it becomes painfully apparent that things get REALLY messy.

So my question is, does anyone have a hefty bezier path arc length algorithm on them? Or maybe some motivation on how to attack this problem?

If not, I guess I'll end up thowing this at Mathematica, but its symbolic integrator is going to have to deal with an absolute mess.

Thanks,
Matt Fahrenbacher
     
davechen
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status: Offline
Reply With Quote
Aug 21, 2002, 12:23 AM
 
I did a google search on bezier curve arc length and found this link

It sounds like what this guy did was to recursively sub-divide the curve until the segments were small enough, and then add up the segment lengths.
     
Ghoser777  (op)
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Aug 22, 2002, 08:43 PM
 
Yeah, I ended up doing that, but using a different (and I think better) algorithm for calculating points on a bezier path:

http://www.moshplant.com/direct-or/bezier/math.html

Thanks,
Matt Fahrenbacher
     
Detrius
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status: Offline
Reply With Quote
Aug 23, 2002, 12:12 AM
 
This is where Calculus 3&4 come in handy. The subdividing of the path is basically just doing the integral by brute force--physically calculating the distance. However, if you have the equation, the integral can be calculated.

The length is simply the integral of the norm of the derivative. This would be in R2, in this case.

I'm not entirely sure I understand how your equations are set up. In your B(u) equation, what is z? What are your independent variables? I am assuming u and z, but that leaves B(u) to be a y value which makes this a 3D curve, which I doubt is what you want. I pulled out my dusty old Cal 4 book for this. With a little more clarification, I may be able to spit out something more useful.


Hank

(Double major in Math and Computer Science, graduating this December, at UNC Charlotte)
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
davechen
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status: Offline
Reply With Quote
Aug 23, 2002, 12:53 AM
 
Recursive subdivision is the standard way of rendering bezier curves, as opposed to evaluating it at uniform steps in t. With recursion you can have adaptive subdivision. With uniform steps its trickier to pick a step size to guarantee an error bounds.

Also, if I remember correctly, its less computation to do it recursively, although my curved surfaces class was a long time ago.

Note to Detrius, the page at moshplant is simply giving a 2-d bezier curve. x and y are functions of t. For a 3-d curve you'd add another equation for z.

dave

(cs degrees in 88, 91 and 98 )
     
Detrius
Professional Poster
Join Date: Apr 2001
Location: Asheville, NC
Status: Offline
Reply With Quote
Aug 23, 2002, 11:30 AM
 

Note to Detrius, the page at moshplant is simply giving a 2-d bezier curve. x and y are functions of t. For a 3-d curve you'd add another equation for z.
I thought this was rather obvious. This is just parametric functions. The thing that was not obvious is coming in expecting this and trying to figure out where Ghoser got his functions (going strictly on my math background).
ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
davechen
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status: Offline
Reply With Quote
Aug 23, 2002, 01:34 PM
 
Originally posted by Detrius:


I thought this was rather obvious. This is just parametric functions. The thing that was not obvious is coming in expecting this and trying to figure out where Ghoser got his functions (going strictly on my math background).
Oops, I misread your post. Late night surfing.
     
Ghoser777  (op)
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Aug 23, 2002, 05:25 PM
 
Originally posted by Detrius:


I thought this was rather obvious. This is just parametric functions. The thing that was not obvious is coming in expecting this and trying to figure out where Ghoser got his functions (going strictly on my math background).
Some crack pot website. The new functions I found were much easier to understand. u in the original functions I posted was a number between 0 and 1, I believe, so B(u) would yield a point on the bezier path. That definition was funky, and I understand the new equations more.

I'm currently just subdiving the path and adding up the lengths of tangents till I get as far as I want, although its fairly innefficient. Therefore, I'm playing with the integral I've got and I'll post back after I hack it into a better form. This was what I was trying to do originally, but that equation was hideous and unuseful.

Thanks,
Matt Fahrenbacher

(Math major: U of Illinois May 2002)
     
Ghoser777  (op)
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Aug 23, 2002, 06:01 PM
 
Actually, finding the arc length of a paramterized curve is pretty easy (sort of), at least in form.

It's just the integral of the square root of the sum of the squares of the derivatives of the components with respect to t, or:

integral(Sqrt((dx/dt)^2 + (dy/dt)^2))dt

I end up with this:

Integral(Sqrt[a*t^4 + b*t^3 + c*t^2 + d*t + f])dt

as the general form. Doesn't look too bad so far, just the square root of a four degree polynomial. But try this:

http://www.integrals.com/ provides a web interface to mathematics symbolic integrator. I know they're not completely accurate, but look at what results you get if you put in the above (removing the integral command and swapping t's and x's). You may have to drag the image elsewhere, as it won't fit in the box that the webpage provides.

So, anyone got at least the right motivation to integrate this baby? Parts? Substitution? Trig forms? Sacrifice?

Matt Fahrenbacher
     
The Godfather
Addicted to MacNN
Join Date: Dec 1999
Location: Tampa, Florida
Status: Offline
Reply With Quote
Aug 24, 2002, 01:43 PM
 
Da-amn, I want to get my hands on my Mac already and apply all my mad vector skills too
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 11:17 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,