polynomial solution needed (3rd & 4th order)

Started by
26 comments, last by avianRR 22 years, 5 months ago
I''m going to render the volume in 3d. I have an input ray to a bounded volume (an octree node) and I know the formula for the cubic function inside the volume of the node, and I want to find where the line intersects the surface that you get wherever the function equals the cutoff point. So, I figure, for each subtree of the node, if there are any points inside where the value of the function exceeds the cutoff, trace the ray inside that node. Otherwise, skip it, and do the same for the next subnode that the ray hits.

FallingFrog
"The reasonable man adapts himself to the world, butthe unreasonable man tries to adapt the world to him-therefore, all progress depends on the unreasonable man." -Samuel Butler
Advertisement
Can''t you guys post these super-fast root-finders?

y^3 + p*y^2 + q*y + r = 0
Set y = (x - p/3) and we have

x^3 + a*x + b = 0 were
a = (3*q - P^2)/3
b = (2*p^3 - 9*p*q + 27*r)/27

Let
A = cubicroot(sqrt(b^2/4 + ^3/27) - b/2)
B = - cubicroot(sqrt(b^2/4 + a^3/27) + b/2)

And the roots are

A+B
sqrt(-3)*(A - B)/2 - (A + B)/2
sqrt(-3)*(B - A)/2 - (A + B)/2

If p,q,r are real then

If (b^2/4 + a^3/27).GT.0 there one real root and a pair
of conjugate complex ones

If (b^2/4 + a^3/27).EQ.0 there three real roots and
at least two equal

If (b^2/4 + a^3/27).LT.0 there three real roots and all
unequal

All from CRC Math Table... (any errors is for sure mine)

But you got some to do to make use of it ...

micca
Newton''s Method is pretty cool (and it works for pretty much anything). It uses an iterative approach, so you can be as accurate or inaccurate as you want.

NewX = OldX - (f(OldX) / f''(oldX))

Provided that f''(OldX) isn''t 0.

What it actually does, is starting from a point, the tangent to the curve at that point is found, then at the point where that tangent hits the x axis, is the new x coordinate. This will eventually reach (or converge on) a solution most of the time. Problems come when you can''t get the solution you are looking for, in which case, your best bet is to find the turning points and other useful points which are often found near roots (points of inflexion) and use them for initial values of x.

The other possible problem can come when f''(oldX) is a really small number for a few values of x which totally puts off the search because the tangent is almost horizontal and intercepts the x-axis a long way away.

And, if you don''t know calculus, then this will be a bit harder. But here is the crash course:

Polynomial f(x) = ax^n + bx^m + cx^o + dx...

Derivative f''(x) = anx^(n - 1) + bmx^(m - 1) + cox^(o - 1) + d

(so you take the power, multiply it by the coefficient and then subtract 1 from the power)

Trying is the first step towards failure.
Trying is the first step towards failure.
I''m a bit busy at the moment, but I''ll try and post my source some time tonight.

FallingFrog - my cutoff code will now run 50M times in 10.6 seconds (1.06s for 5M times). I know this is a bit slower than what you wanted, but I can''t see any further way to speed it up (at least not without knowing what kind of values it will be given and trying to approximate based on these).
Enigma- that would be great
Incidentally, I thought of another way of solving my problem-
if(f(x + q) > v || f''(x + q) > v || f''''(x + q) > v)
then it is possible that there is a value above v somewhere between x and x + q. But this method will give me a lot of false positives. Because the cubic will usually be non-flat, if you know what I mean. Yours will be faster, I think. Say, you wouldn''t want to help me implement reflection and shadows, would you?

FallingFrog

PS. I just invented the number of times it has to run in a second. I multiplied 320 * 200 * 20fps * 5 recursions, and rounded it down a little.

PPS. This is just an idea, in case you haven''t done it yet: Since I only need to know if at least one max exists, you can return as soon as you find one- you don''t have to find them all.
"The reasonable man adapts himself to the world, butthe unreasonable man tries to adapt the world to him-therefore, all progress depends on the unreasonable man." -Samuel Butler
OK, so posting the code tonight didn't quite happen then! I'll try to post it tomorrow instead (sorry to keep anyone waiting but I felt I really ought to do some coursework today)!

FallingFrog - As I say, I'll post tomorrow (need to properly comment the code first) - I squeezed a couple more milliseconds out of it for you. I'm quite happy to help with anything else (time allowing - lots of coursework coming up!). What exactly is it you're doing? (sorry - couldn't follow your last explanation, what volume are you rendering?, what does the cubic represent? - apologies if I'm being really thick here)

avianRR - If you're still reading this thread, I just found that I managed to break my polynomial solver code at some point. It's fixed again now but if the code I sent you doesn't work then that's probably the reason. I'll try and make a few more optimisations before I post (I think I made an error in my timings, it's not quite as fast as I had worked out).

EDIT: mis-spellings

Edited by - Enigma on November 14, 2001 8:02:43 PM
OK, to save gamedev some server space and bandwidth I've stuck my code on my uni webspace.

apologies all round for speed of code - my compiler was being smart and optimising my timer function a bit, so my code is actually a little slower than I had posted before.

avianRR - final speed approx. 15k quartics/second, probably about twice that for cubics. Still a few issues, but I'm not going to get round to solving those for a while I'm afraid.

FallingFrog - final speed approx. 3.9M solutions/second providing you only change the start and end bounds, about 1M solutions/second if you change everything.

Here's the link: www.ecs.soton.ac.uk/~psk200

EDIT: it treated my link as a relative address
EDIT: spelling mistake

Edited by - Enigma on November 15, 2001 6:17:55 PM

Edited by - Enigma on November 15, 2001 6:19:19 PM

This topic is closed to new replies.

Advertisement