Colliding a Bezier Patch and a linesegment
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

I figured I'd post more of what I've got so far.
Given my control points A-I, I've formed a function which takes (U,V) and gives the point on the surface (in vector format).
I noticed that every combination of U and V raised to powers 0 through 2 are in there (that is, I can build a 3x3 table of terms, where term [1][2] is (u^1)*(v^2), and so on), and that the coefficients (in vector form, call them a-i) for those terms are constants for any given patch.
So, I can form an equation in the form:
startPoint + K * line = a*u*u*v*v + b*u*u*v + c*u*v*v + d*u*u + e*v*v + f*u + g*v + h*u*v + i
and split that into three (one for each XYZ).
So from that equation, I need to isolate each of the terms K, u, and v. K is easy, but u and v are more difficult. How can I isolate each of them in turn?
Superpig
- saving pigs from untimely fates, and when he's not doing that, runs The Binary Refinery.
[edited by - Superpig on April 20, 2003 6:46:32 PM]
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
I can see if my TI-89 can solve it... just to make sure, you are trying to solve Auuvv+Buuv+Cuvv+Duv+Euu+Fvv+Gu+Hv+I=K*t+L for each of three dimensions, right? Actually, I don''t think my calculator will solve that... I know you can get u in terms of v or vice-versa by completing the square, but I don''t know about anything farther than that. I never thought about using bi-quadratic patches in ray-tracing, I didn''t realize that there were like half as many terms in the equation... Now I''m very interested in this, I think I will try to solve for u and v tomorrow in school. Right now, though, I can''t help you there.
I assume you''ve already realised that using that function for a large mesh would be insane. I guess if you''re doing ray-tracing, it doesn''t really matter.
I figured that by getting U in terms of V and K, and V in terms of U and K, I''d just be able to substitute one into the other and get each of U and V in terms of K. That would be a start, at least.

A large mesh? Well, assuming that we stick with a bi-quadratic patch (because that formula only works with bi-quadratic patches), what''s the problem? Given that we''ve got an absolute position for every point on the patch, then surely it would actually be *more* accurate than diving into polygons (and thus potentially losing masses of accuracy) and colliding with them?
I need a decent calculator.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
Once you have this form, substitute f(u,v) = e + d*t to get g(e + d*t) = 0, which is a polynomial in only t. The catch here is generating g. For a bicubic patch you have to invert an 800x800 matrix (ouch) but for biquadratic it might be considerably less (I used to have exact numbers, but no longer). Also, there may be a more reasonable computational way of doing it, though I haven''t found one. The upside is, you can generate g once before rendering and reuse it for every intersection query.
I can post more details later if you''re interested.
"Math is hard" -Barbie
A little recap, mostly for my own benefit...
For ray tracing, solving for the intersection reduces to this problem:
x = x0+j*t = auuvv+buuv+cuvv+duv+euu+fvv+gu+hv+i
where x0 and a-j are all known coefficients. x0 and j have to do with your camera or light, and a-i can be found in terms of your control points (I just did all the algebra for those coefficients... damn! The solutions are so incredibly concise, but it took me like two hours to find them. I could probably just search on google, but I like doing things for myself.)
You need to find u, v, t, and x in terms of x0 and a-j. Of course, some of these are easy, so the real problem is finding:
u(a,b,c,d,e,f,g,h,i,j,x0) and
v(a,b,c,d,e,f,g,h,i,j,x0)
Now, according to you, Pragma, it is possible to find these functions.
Okay. Now I''m ready to start doing the REAL work. I think I have an idea of where to start...

I don''t think it''s possible to get u in terms of just constants - I think we can get u in terms of v and t, and v in terms of u and t; thus, we can substitute the first (using the X-dimension) into the second (using the Y-dimension) and get v in terms of t. Then, we get t in terms of u and v, substitute the first equation in and get t in terms of v (in the Z-dimension), and finally substitute in the second equation to get t in terms of constants.
Once we''ve got t, we can pretty much forget about u and v - t will give us the point of intersection, and the u, v coordinates once we''ve got that are unimportant, although they could easily be found.
There is, of course, a much more elegant solution to the problem, but I don''t know what that elegant solution is, because I''m 16 and officially studing 2D vector addition. *sigh*

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
Of course! How did I forget? I just remembered, you need three independent equations to solve for three independent variables. That means that there should definitely be a closed-form expression. I guess you realized that a while ago...sorry for being so slow. Anyway, I can't wait to try this out tomorrow.
Edit: Don't just forget about the u and v values; they can be used for textures if you need them. I plan to incorporate them into my functions just in case I want them in the future.
Wait... it seems like you know exactly what to do. Are you having trouble with the algebra? Because I'm almost certain that I can solve those equations.
[edited by - vanillacoke on April 21, 2003 11:17:45 PM]
Rather than expressing u and v as functions of the other variables, you change the parametric representation to an implicit representation by eliminating u and v from the equations.
The disadvantage is that u and v have been lost, so you can''t use them as texture co-ordinates. But you can recover the normal, since it is parallel to the gradient of the implicit function.
"Math is hard" -Barbie