I have a set of unit vector orignated from (0,0), distributed evenly with alpha and theta angle from 0 to 360 and 0 to 180. Alpha and Theta are used to index a 2D array of these vectors.
Given an arbitrary direction (another vector originated from (0,0)) how can i find the vector which is the nearest (lets say a floor operation) ?
I can make it using Cartesian to Spherical, then obtain the angles and make a floor on the both angles, then finally get the index of the vector in the original array set. This work perfectly...
But Cartesian to Spherical is using trigonometric function which are heavy on computer (ArcTan)... is there any tricks to get the same result without trigonometric computation ? maybe quaternion could help but i do not know about it..
what i'm doing is in pseudo code for one quadrant
var alpha = Math.atan(direction.z / direction.x);
var I = Math.floor(alpha / deltaAlpha);
var x2 = direction.x * direction.x;
var y2 = direction.y * direction.y;
var z2 = direction.z * direction.z;
var theta= Math.atan( (x2 + z2) / y2);
var J = Math.floor(beta / deltaTheta);
then i get the I,J index of my vector set... but again, how to avoid $$\alpha = atan(z/x)$$ and $$\theta=atan((x^2 + z^2)/ y^2)$$ which are GPU or CPU expensive when running over thousand of location on each frames..
I would treat the vectors as points on the unit sphere and use a k-d tree (or a variant) to store the points. This way, you can identify the vector (point) nearest some new unclassified point in time logarithmic in the number of stored points.