POV-Ray : Newsgroups : povray.general : Proposed VAngle alternative AngleBetweenVectors. (Attn: Tor Olav) : Proposed VAngle alternative AngleBetweenVectors. (Attn: Tor Olav) Server Time 4 Mar 2024 23:45:23 EST (-0500)
 Proposed VAngle alternative AngleBetweenVectors. (Attn: Tor Olav)
 From: William F Pokorny Date: 11 May 2021 04:50:49 Message: <609a4569\$1@news.povray.org>
OK. Been running both approaches within the c++ 'vectors.h' framework of
POV-Ray. Initial idea is both forms could be in one inbuilt function and
therefore "munction" too.

// Original macro approach:

#macro VAngle(V1, V2)
acos(max(-1,min(1,vdot(vnormalize(V1),vnormalize(V2)))))
#end

// Tor Olav's proposal for better accuracy:
#macro AngleBetweenVectors(v1, v2)

#local v1n = vnormalize(v1);
#local v2n = vnormalize(v2);

(2*atan2(vlength(v1n - v2n), vlength(v1n + v2n)))

#end // macro AngleBetweenVectors

-----------------------
Over millions of randomly generated vectors with component values in the
+-25.0 range.

Max dot value seen: +0.99999999991558441
Min dot value seen: -0.99999999134553685

Largest difference between approaches was 0.00000000000995055 (1e-11).
The vectors for this difference were:

<6.67080701406617393,
9.26765558974242509,
20.94262630955438453>

and

<7.10376197570178647,
9.86894333496174880,
22.30091129769613900>

Angles near zero and pi (near same-direction and nearly opposing)
showing the larger differences. The 0 angle case running larger for
differences than those near pi.

The performance difference in the context of all programming framework
VAngle to AngleBetweenVectors.

127.15 -> 164.98 ---> +29.75%  slower

Taking a specific measure of the overhead out of both times:

100.5  -> 138.33 ---> +37.64%  slower

I have still a few other tests in mind(1), but busy for a couple days -
starting now.

(1) - Different optimizations, etc.

Bill P.