Nearest Neighbours using Quaternions
Asked Answered
B

4

8

Given a quaternion value, I would like to find its nearest neighbour in a set of quaternions. To do this, I clearly need a way to compare the "distance" between two quaternions. What distance representation is needed for such a comparison and how is it computed?

Thanks,

Josh

Bassoon answered 7/2, 2011 at 1:10 Comment(0)
U
5

Is your quaternion just a point in 3D space with an orientation?

Then the distance between two quaternions x1,y1,z1,w1 and x2,y2,x2,w2 is given by:

distance = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2), assuming that the w component is used for orientation. I.e. this is the same as the distance between two 3D points.

Is your quaternion a point in 4D space?

Then the distance between them is given by:

distance = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 + (w1-w2)^2).

Which is just the extension to 4D space. This euclidean distance formula works in any number of dimensions.

Unmitigated answered 7/2, 2011 at 1:15 Comment(2)
Thanks for the answer. The quaternions are represented as a point in 4D space. I have used Euclidean distance as suggested, and it provides reasonable results.Bassoon
Found an extra parenthesis in your 2nd formula there, but can't make an edit as it's less than 6 characters. Should be distance = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 + (w1-w2)^2). Thanks for this answer!Penstock
R
10

This is an old question, but it seemed to need a little more answer. If the quaternions are unit-length quaternions being used to represent rotations, then Euclidean distance will give some funny results because quaternions provide 2x redundant representation of rotation space; i.e., a quaternion and its negation represent the same orientation. In this case, the correct distance metric is the angle between the quaternions, constrained to fall within [0,pi/2]:

theta = acos(q1.w*q2.w + q1.x*q2.x + q1.y*q2.y + q1.z*q2.z);
if (theta>pi/2) theta = pi - theta;
Rollway answered 25/4, 2011 at 15:26 Comment(0)
U
5

Is your quaternion just a point in 3D space with an orientation?

Then the distance between two quaternions x1,y1,z1,w1 and x2,y2,x2,w2 is given by:

distance = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2), assuming that the w component is used for orientation. I.e. this is the same as the distance between two 3D points.

Is your quaternion a point in 4D space?

Then the distance between them is given by:

distance = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 + (w1-w2)^2).

Which is just the extension to 4D space. This euclidean distance formula works in any number of dimensions.

Unmitigated answered 7/2, 2011 at 1:15 Comment(2)
Thanks for the answer. The quaternions are represented as a point in 4D space. I have used Euclidean distance as suggested, and it provides reasonable results.Bassoon
Found an extra parenthesis in your 2nd formula there, but can't make an edit as it's less than 6 characters. Should be distance = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 + (w1-w2)^2). Thanks for this answer!Penstock
S
1

This really depends on what you use your quaternions for. A simple distance measure would be the absolute value of their difference.

If x = a + b i + c j + d k y = e + f i + g j + h k

than the Euclidean distance would be

 |x-y| = sqrt( (a-e)² + (b-f)² + (c-g)² + (d-h)² )
Settee answered 7/2, 2011 at 1:14 Comment(0)
R
0

If "distance" you mean the shortest arc rotation between 2 orientations , than simple Euclidean distance is ok (L2 or norm2).

because angle between orientations can be written as

theta = acos(q1.w*q2.w + q1.x*q2.x + q1.y*q2.y + q1.z*q2.z);

Than, the bigger L2, the bigger distance.

NOTE: all quaternions before query should be negated if provide negative dot product. Than you can use usual KNN match to speedup your queries.

Russelrussell answered 26/3, 2014 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.