Caveman Normalization

Started by
14 comments, last by Charles B 19 years, 6 months ago
Something else I'd like to point out, often times when you normalize a vector and then check its length, it often won't be exactly 1.0f units, rather often it will be 1.00002 with some extra bits on the mantissa.
Why don't alcoholics make good calculus teachers?Because they don't know their limits!Oh come on, Newton wasn't THAT smart...
Advertisement
Quote:Original post by shadow12345
Something else I'd like to point out, often times when you normalize a vector and then check its length, it often won't be exactly 1.0f units, rather often it will be 1.00002 with some extra bits on the mantissa.

and sorry for offtopic, but BTW, it's one of the reasons why
angle_between_vectors=acos(DotProduct(a,b)/(Length(a)*Length(b)))
or
angle_between_vectors=acos(DotProduct(a,b))

is not a good thing for small angles. You can easily get something like acos(1.0000000382313) even if vectors is slightly inparallel. As result, bugs ,indefined results, or at best bad precision at small angles.
And why
angle_between_vectors=atan2(Length(CrossProduct(a,b)),DotProduct(a,b))
(it works for non-normalized as well as for normalized)
is alot better. Also it may even be faster because there's only one square root.
Quote:Original post by Dmytry
And why
angle_between_vectors=atan2(Length(CrossProduct(a,b)),DotProduct(a,b))
(it works for non-normalized as well as for normalized)
is alot better. Also it may even be faster because there's only one square root.


If performance matters so much that you're counting square roots you can eliminate them altogther, as if

tan t = |a ^ b] / (a . b)

then

tan2t = (|a ^ b| / (a . b))2
= |a ^ b|2 / (a . b)2

The numerator and denominator on the right hand side can be calculated without taking the square root, in particular as |a ^ b|2 is just |a ^ b| . |a ^ b|.

t can then be calculated using an inverse function to x2/y2= tan2t. This is not a library function but you can define it yourself and come up with something faster than the atan2 and sqrt it replaces.
John BlackburneProgrammer, The Pitbull Syndicate
Quote:Original post by Jiia
...
The sqrtf method takes about 5 seconds to execute this, and the caveman (likely incorrect) method takes less than 1 second. Using the abs() functions instead of conditional checks takes around 13 seconds [lol].


Yes you lack of knowledge. None considered you are an idiot. Just a math illiterate ;)

Now that you probably know about L1(Manhattan) and L2(Pythagoras, Euclidian) norms. You should also be interested to know about the L0 norm : max(|x|, |y|, |z|).

Here is my touch of speedy math addict.

Your measurement is not significant for a pro game coder. You were possibly in debug mode (cf your remark about abs()) and surely not with the optimizations of the most recent compilers. Today 3DNow/SSE give you rsqrt for free. And even if conditional moves are also very fast, the standard normalization is faster than the caveman hack. So no deal, only cons, no pros. Caveman is deprecated, it was a useful trick ... ten years ago.

EDIT : just realized you were necessarilly in DEBUG mode. Else, with OPTIMIZATIONS ON all your tests would return 0 seconds. Because any compiler would detect redundant computations (constant inputs) and no outputs. The code would be totally culled, considered as dead code. Measuring/profiling code in DEBUG is quasi non sense (apart measuring number of calls).

"Coding math tricks in asm is more fun than Java"
Quote:Original post by Charles B

Now that you probably know about L1(Manhattan) and L2(Pythagoras, Euclidian) norms. You should also be interested to know about the L0 norm : max(|x|, |y|, |z|).



That's actually the L-infinity norm. I don't actually think there is an L0 norm, see definition of the vector norms on

Vector Norm
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Right, memory failure here ;) There is a picture to remember it. See the unit 'circle' of the norm evolve from square to circle, to square again, turned of 45 degrees.

Learned all that 15 years ago.
"Coding math tricks in asm is more fun than Java"

This topic is closed to new replies.

Advertisement