# Define end-effector quaternion orientation from unit vector

I want to define ee-poses from a point cloud in a way that the pose for each point is pointed to by a unit vector from origin. Rotation about the vector pointing towards the point can be whatever as the tool is round.

I guess this would be mathematically equal to using the normal of a surface and not care about rotation about the normal?

E.g.

$$p(x, y, z) => q(w,x, y, z)$$

$$p(1, 0, 0) => q(1, 0, 0, 0) => \text{No rotation}$$

$$p(0, 1,0) => q(\frac{1}{\sqrt{2}}, 0, 0, \frac{1}{\sqrt{2}}) => \text{equal to 90deg rotation about Z}$$

$$p(0, 0, 1) => q(\frac{1}{\sqrt{2}}, 0, -\frac{1}{\sqrt{2}}, 0) => \text{equal to -90deg rotation about Y}$$

How can i solve this?

From your examples it seems that you are looking for a rotation that rotates the vector $$(1,0,0)$$ to some given unit vector $$(x,y,z)$$. This indeed doesn't define a unique rotation. However, the rotation with the smallest angle of rotation can in most cases be defined uniquely. If desired one could always apply an additional rotation with as rotation axis the $$(x,y,z)$$ vector. This shortest rotation can be found using the dot and cross products, which gives

\begin{align} \cos \theta =& (1,0,0) \cdot (x,y,z) = x, \\ n \sin\theta =& (1,0,0) \times (x,y,z) = (0,-z,y), \end{align}

with $$\theta$$ the (smallest) angle of rotation and the $$n$$ the unit axis of rotation.

A unit quaternion, used to describe such rotation can be defined with $$\theta$$ and $$n$$ using

$$q = \left(\cos\theta/2,n\sin\theta/2\right).$$

In order to evaluate the sine and cosine of half the angle one could first calculate $$\theta$$ for example using the atan2 function. However, one could also use the following mathematical identities

\begin{align} \cos \theta/2 =& \sqrt{(1+\cos\theta)/2}, \\ \sin\theta/2 =& \frac{\sin\theta}{\sqrt{2\,(1+\cos\theta)}}. \end{align}

Substituting this together with the dot and cross products in the unit quaternion expression yields

$$q = \left(\sqrt{\frac{1+x}2},0,\frac{-z}{\sqrt{2\,(1+x)}},\frac{y}{\sqrt{2\,(1+x)}}\right).$$

The identities used do have some sign discrepancies, but when multiplying a unit quaternion by minus one yields another unit quaternion which represents the same rotation. The only thing you need to watch out for is when $$x$$ is equal (or sufficiently close) to minus one. In that case you could pick any axis perpendicular to the x-axis and rotate around that with 180°.