Least square rotation from a set of normals to a set of normals I know how to calculate the rotation matrix given $n$ and $m$ (both norm equal to 1) and I would like to find rotation matrix $R$ such that $m = Rn$.
now assume I have $\{n_i,m_i\}_{i=1}^K$, how do I calculate R?
I've tried the following loss function:
$ \hat{R} = \arg_R min \sum_k || m_i R n_i - 1 ||^2 $
but deriving it created a mess (I had a term over R).
how can this be solved?
 A: $$arg \min_R \sum_i{ \| m^T_i R n_i - 1 \|^2}$$
$$s.t. \ \ \ R^T R = I$$
This is a least squares optimization with non-linear constraint. The traditional approach would be to solve it using lagrange multipliers. However this particular problem has been studied a lot in the last decades and the usual approach is as follows:
Taking the products:
$$\sum_i \| m^T_i R n_i - 1 \|^2 = \sum_i (m^T_i R n_i - 1)(m^T_i R n_i - 1)$$
$$\sum_i \| m^T_i R n_i - 1 \|^2 = \sum_i (m^T_i R n_i m^T_i R n_i) - 2 m^T_i R n_i +1$$
Since we can consider any sacalar as a 1×1 matrix, we can take the trace of the products:
$$\sum_i \| m^T_i R n_i - 1 \|^2 = \sum_i Tr(m^T_i R n_i m^T_i R n_i) - 2 Tr(m^T_i R n_i) +1$$
Applying the properties of the trace we get:
$$\sum_i \| m^T_i R n_i - 1 \|^2 = \sum_i Tr(n_i m^T_i R n_i m^T_i R) - 2 Tr(n_i m^T_i R) +1$$
We denote the matrix $M_i = n_i m^T_i$ so:
$$\sum_i \| m^T_i R n_i - 1 \|^2 = \sum_i Tr(M_i R M_i R) - 2 Tr(M_i R) +1$$
Taking the derivative with respect to $R$ we get:
$$\sum_i  \frac{\partial}{\partial R} (Tr(M_i R M_i R) - 2 Tr(M_i R) +1)$$
$$ =  \sum_i  {2 M_i R M_i - 2 M_i}$$
Equating the gradient to zero we get:
$$\sum_i  {2 M_i R M_i - 2 M_i} = 0$$
Multiplying both sides by $M_i^{-1}$ on the right and dividing both sides by $2$ we get:
$$\sum_i  {M_i R} - \sum_i{I} = 0$$
$$\sum_i {  \frac{1}{K} M_i } R = I$$
We denote the matrix $C = \sum_i{ \frac{1}{K} M_i}$ so the system is:
$$C R = I$$
$$s.t. \ \ \ R^T R = I$$
The Singular Value Decomposition (SVD) of $C$ is given by $C = U D V^T$ where $U$ and $V$ are orthogonal matrices and $D$ is a diagonal matrix containing the singular values of $C$.
Geometrically, the matrices $U$ and $V$ represent rotations and reflections while the matrix $D$ represent non-uniform scaling such as shearing or stretching.
Replacing $C$ with its svd we get:
$$U D V^T R = I$$
Multiplying both sides by $U^T$ on the left:
$$D V^T R = U^T$$
Since $D$ is representing scaling, the closest rotation must not include any scaling, we must consider $D = I$. Taking that we get:
$$V^T R = U^T$$
Multiplying both sides by $V$ on the left:
$$R = V U^T$$
Since $U$ and $V$ are orthogonal matrices the matrix $R$ is also orthogonal.
$$R^T R = U V^T V U^T = I$$
However, the determinant of $R$ can be negative one. In that case $R$ is a reflection instead of a rotation. A rotation can be enforced by flipping the sign of the last column of $U$.
