Am I doing this right?

Started by
7 comments, last by Jederas 21 years, 10 months ago
Hi, just want to make sure that I have these functions right.. just dot product, cross product, and normalization of a vector. VECTOR NormalizeVector(VECTOR Vector) { Vector.x = Vector.x / Vector.x; Vector.y = Vector.y / Vector.y; Vector.z = Vector.z / Vector.z; return Vector; } VECTOR CrossProduct(VECTOR V1, VECTOR V2) { VECTOR Vector; Vector.x = (V1.y * V2.z) - (V1.z * V2.y); Vector.y = (V1.z * V2.x) - (V1.x * V2.z); Vector.z = (V1.x * V2.y) - (V1.y * V2.x); return Vector; } float DotProduct(VECTOR V1, VECTOR V2) { float mag1, mag2, result, angle1, angle2; angle1 = float(atan(V1.y / V1.x)); angle2 = float(atan(V2.y / V2.x)); mag1 = float(sqrt((V1.x*V1.x)+(V1.y*V1.y)+(V1.z*V1.z))); mag2 = float(sqrt((V2.x*V2.x)+(V2.y*V2.y)+(V2.z*V2.z))); result = mag1*mag2 * float(cos(angle1-angle2)); return result; }
Advertisement
Your NormalizeVector function should look something like this ...

VECTOR NormalizeVector(VECTOR Vector)
{
float length = sqrt(Vector.x * Vector.x + Vector.y * Vector.y + Vector.z * Vector.z);
Vector.x = Vector.x / length;
Vector.y = Vector.y / length;
Vector.z = Vector.z / length;
return Vector;
}

and your DotProduct would probably be a bit faster like this ...
float DotProduct(VECTOR V1, VECTOR V2)
{
return (V1.x * V2.x + V1.y * V2.y + V1.z * V2.z);
}
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Wow, thanks.
It might be useful to put an assert in to check for length == 0 in your normalise function.
small optimization...

VECTOR NormalizeVector(VECTOR Vector)
{
float length = 1 / sqrt(Vector.x * Vector.x + Vector.y * Vector.y + Vector.z * Vector.z);
Vector.x = Vector.x * length;
Vector.y = Vector.y * length;
Vector.z = Vector.z * length;
return Vector;
}
-----------------------------------------------------------"People who usualy use the word pedantic usualy are pedantic!"-me
Regarding that optimisation... I see a lot of sample code that does stuff like that. OneOverMass, InverseInertiaTensor, etc. For clarity I tend not to do that sort of thing, but does anyone have a good idea of the performance benefit? Also, will modern compilers ever attempt a similar optimisation?
If you''re talking about the dot product function, no compiler I''ve ever heard of could see that what Martee wrote is equivalent to what Jederas wrote. Simply because compilers aren''t in the habit of taking apart the functions which are called and testing all possible values to see that those function calls coincide with a simple multiplication...

Does a compiler actually know what the arctangent of a number is?

Jederas, if you''re going to worry about optimization, you should inline dotproduct and crossproduct because when you get into advanced linear algebra tricks you''ll be calling them an awful lot and it''s somewhat a waste of time to get bogged down with function calls when you''ll only be out of context for ten processor cycles or so (in the dot-product this is just about reasonable).

George D. Filiotis
Are you in support of the ban of Dihydrogen Monoxide? You should be!
Geordi
George D. Filiotis
I meant Ecko''s optimisation, precomputing the inverse of a value to do multiplication instead of division.
It tends to do a world of difference actually, but i''d say
the treshold for increasing performance usually comes when
you''re using the value in atleast 3 places, like here.
I''ve also been told, although i''m not sure it''s true,
that processors and compilers of today are somewhat optimized
for taking the inverse, and does that a lot faster than
a standard division.

T

--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.

This topic is closed to new replies.

Advertisement