[Bf-committers] Mesh-Tools in 2.36?

Robert Wenzlaff rwenzlaff at soylent-green.com
Wed Dec 1 04:08:32 CET 2004


On Tuesday 30 November 2004 15:18, Zach Morgan wrote:

> How do you find the angle between two vectors?  

As said before, the dot product of two vectors is the cosine of the angle 
between the the vectors (times the product of the lengths of the vectors, but 
most vectors we deal with are normalized).  

If cos(angle) is sufficient, use this.  

If your equation can be re-writen in terms of cos(angle), do it.   

Avoid making the compiler generate an facos instruction (or even an fdiv), 
since, even though it's only one instruction, it can take 50 or more clock 
cycle to execute (even with the dedicated math co-processor hardware).  Esp. 
if it ends up in the rendering loop.  

>Is there a reason that there isn't a function called 
>getAngleBetween2Vectors in the Blender maths library ( as well as 
>several other utility functions )?

Because anyone who is serious about 3D programing knows the above, and there 
is a dot-product function (thought I cant recall its name off hand).

>Doing a function to do all that at once would be easy.
>I'll wip one up when CVS thaws.

It's OK for a one-off call, but please keep in mind the speed penalty of the 
facos and fdiv instructions.    For something that gets called on a per pixel 
basis it's an avoid at all cost call.  For something that gets called during 
a screen update, it's a use with extream caution call.

Having such a call "handy" for people who are unwilling to learn a little math 
sets a dangerous precident.   What you end up with is people writing stuff 
like:

L=length(v1) * sin(get_angle(v1,v2));

which could probably be greatly simplified in terms of the dot product since 
it's already a related trig form and usually the difference between choosing 
sin and cos is just a matter picking a reference point.  It's certainly 
asking an awful lot of the optimizer to try to sort it out.  

In reality, the actual angle is rarely useful.  If you just want to compare 
magnitudes of two angles, the greater cos is the smaller angle.

At least include speed warnings in the function.  There's no sense in making 
the coding go quickly, if it makes the app so slow noboby wants to use it.

*********************************************
Robert Wenzlaff   rwenzlaff at soylent-green.com



More information about the Bf-committers mailing list