# pouët.net

## Gradients of signed distance function

category: general [glöplog]
Does anyone know how to calculate the gradient of any given signed distance function? I have it in the form f(x,y,z)...is there a generalized method to finding the derivative given a point residing on the implicit surface?
added on the 2009-04-21 04:41:31 by ferris
Nevermind, found what I was looking for :) You can get a good estimate with the following equations:

F0 = F(x,y,z)
Fx = F(x + e,y,z)
Fy = F(x,y + e,z)
Fz = F(x,y,z + e)
Fn = normalize(Fx-F0,Fy-F0,Fz-F0)

:)
added on the 2009-04-21 05:52:55 by ferris
...Just make sure e is tiny ;)
added on the 2009-04-21 05:55:39 by ferris
eh, what? You may want to look up basic numerical differentiation.

http://en.wikipedia.org/wiki/Numerical_derivative

added on the 2009-04-21 08:08:38 by Calexico
WHAT DO YOU TEACH IN SCHOOLS NOWADAYS?

Btw, the solution is

Fx=(F(x+h,y,z)-F(x-h,y,z))/2h
etc...

And the gradient is not normalized.

added on the 2009-04-21 08:10:19 by Calexico
What Calexico said!
added on the 2009-04-21 08:19:16 by xTr1m
I usually prefer the 6 point gradient (Calexico's) as it's more accurate (central differences). You can read it in page 46 of

http://iquilezles.org/www/material/nvscene2008/rwwtt.pdf

and also how do add bump (procedural) map to the distance field surface. Note that unsigned distance fields will give inaccurate normals when the sampling points fall inside the objects, so it's almost mandatory to use signed fields for good quality images.

Another trick, if you are having LOD in your distance function (further away objects get a simplified function), then you can also make "e" bigger with the distance, than can reduce aliasing a lot.

Finally, I also I join the "What do you teach in schools nowadays?"
added on the 2009-04-21 09:12:10 by iq
ferris' solution is actually okay if you replace the normalize by "(1/e)*". symmetric differences have a better approximation order for smooth functions, but with the 3 forward differences you only need 4 samples instead of 6, which can mean quite a speed boost for distance functions that are expensive to sample. (if you use a newton-like scheme, you need F(x,y,z) anyway, and it's 4 vs. 7 samples).

also, all higher-order derivative approximations assume higher-order smoothness of the target function. cheaper approximations can actually perform better when that assumption doesn't hold.
added on the 2009-04-21 09:14:02 by ryg
Wow, I lost my faith in the American education system with this question of yours Jake :P.
added on the 2009-04-21 09:14:14 by decipher
Decipher: You had some to begin with?
added on the 2009-04-21 10:32:13 by Preacher
I'm aware it's not normalized :P

And hey lay off, I thought there would be some special case for signed distance functions rather than other types of functions. Besides, gradients of 3D surfaces I thought weren't taught until college anyways, but whatever.
added on the 2009-04-22 06:05:50 by ferris
...seems like you're all forgetting I have alot of school left :P
added on the 2009-04-22 06:06:28 by ferris
..And I forgot to mention thanks :)
added on the 2009-04-22 06:13:36 by ferris
ryg: The problem is that you actually translate your function by h/2 for the "single sided" differential. This is fine if you work on continuous data sets or functions where you can use a vey small value of h, but may become quite an issue if you work with discrete, sampled data. Ok, granted, that is not much of an issues with CG, but I have seen quite a bit of RL applications where this had interesting consequences (oscillations...)

added on the 2009-04-22 08:35:40 by Calexico
The point is, more likely, if you're taught the idea of what differentiation is and the difference quotient, the whole thing should be rather obvious. Then again, when gradients were introduced in my college math classes, a lot of people were baffled..
added on the 2009-04-22 10:54:00 by Preacher
Quote:
WHAT DO YOU TEACH IN SCHOOLS NOWADAYS?

Quote:
Wow, I lost my faith in the American education system with this question of yours Jake :P.

I'm offended. I can't even derive e^x correctly, and still I have an M.Sc. And there's nothing wrong with that!
added on the 2009-04-22 17:36:31 by skrebbel
M.Sc in what? :)
added on the 2009-04-22 17:38:46 by Preacher
computer science, but does it matter?
added on the 2009-04-22 18:58:04 by skrebbel
then again, why would you learn how to derive if there's software for it? :D
added on the 2009-04-22 18:59:59 by Gargaj
Preacher: I'm not in college yet :)
added on the 2009-04-23 03:38:44 by ferris
If you're from the US, college in many European countries actually means High School and not University which you guys call "College". :)

And besides, in Europe, we're taught this stuff in secondary school ! Which I _think_ Americans call "Grade School" :P
added on the 2009-04-23 04:37:06 by button
Secondary school is our high school, and our college is also called post-secondary...so it's not QUITE grade school :P :D
added on the 2009-04-23 06:06:34 by ferris
Quote:
ps 3.0? Probably over 2048^3; it's pretty big

Ferris is further showing us how US mathematics work! Kidding mate :).

But seriously, we learnt differentiation in 10th grade which was two years ago. I am in sheer wonder what the heck were you learning in tenth grade?
added on the 2009-04-23 09:01:15 by decipher
added on the 2009-04-23 12:59:17 by Exin
Afterall, does it even matter what you call it if it solves your problem?
added on the 2009-04-23 15:46:35 by waffle