Using the new XMVECTOR and XMFLOAT3 classes what is the best way to get the distance between 2 points? I couldn't find a function that does it in XMVector* family of functions so I came up with the following :
float distance(const XMFLOAT3& v1,const XMFLOAT3& v2)
{
XMVECTOR vector1 = XMLoadFloat3(&v1);
XMVECTOR vector2 = XMLoadFloat3(&v2);
XMVECTOR vectorSub = XMVectorSubtract(vector1,vector2);
XMVECTOR length = XMVector3Length(vectorSub);
float distance = 0.0f;
XMStoreFloat(&distance,length);
return distance;
}
Will this be faster than a normal Vector3 class with just 3 floats for x,y,z and then using sqrt because it uses intrinsic optimizations? Namely :
float Distance(const Vector3& point1,const Vector3& point2)
{
float distance = sqrt( (point1.x - point2.x) * (point1.x - point2.x) +
(point1.y - point2.y) * (point1.y - point2.y) +
(point1.z - point2.z) * (point1.z - point2.z) );
return distance;
}
When dealing with horizontal lines, the length of the line is simply the difference between the two points' x-coordinates. In a similar fashion, a vertical line's length can be found by subtracting one of the y-coordinates with the other.
There's only one way to get distance between points. And that's the way you described. vec3 diff = b - a; float distance = sqrtf(dot(diff, diff));
Will this be faster than a normal Vector3 class with just 3 floats for x,y,z and then using sqrt because it uses intrinsic optimizations?
If you're worried about performance, you need to profile your application, instead of trying to guess what will be faster. There's a very good chance that impact of of "less efficient" solution will be completely unnoticeable in your application.
There's also a very good chance that if you start writing routines yourself, you'll make them less efficient or introduce bugs. For example your "hand-written" code might (depending on compiler) perform more floating point calculations than original code that uses "XMVECTOR". In your routine you calculate difference between vectors twice. If compiler doesn't optimize that out, you'll end up with 6 subtractions, 2 additions, 1 sqrtf, and 3 multiplications. In "XMVECTOR" code you'll use 3 subtractions, 2 additions, 3 multiplications, and 1 sqrtf.
For profiling applications you can use AQTime 7 Standard (free at the moment), or gprof (if you're compiling with gcc/g++).
D3DXVec3Length(&(Point1-Point2))
is equivalent to the distance formula.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With