I am a physicist, writing some algorithms for 3D movement from a known set of $xyz$ points to a corresponding set of points in a 2D image. I am trying find the best way to calculate a rotation matrix for a specific case.
Let's assume a set of $xyz$ points in space representing a rigid body vertices. These points are denoted as $x_i\mid i=1,\dotsc,k$ , $k>4$.
Each point has a corresponding ray represented by a point $a_i $ and a direction $n_i$. The location of each point after rotation lies on the corresponding ray.
Now I am looking for the rotation matrix $M$ which transforms the set ${x_i}$ closest to the corresponding rays. We denote these points as $x'_i = M\cdot x_i$. I thought of two solutions:
Using the rigid body constrains: representing each new point as a combination: $$x'_i = a_i+t_i \cdot n_i$$ and imposing that the distance between two new points is equal to the distance of the old points (rigid body): $$x’_i – x’_j \equiv (a_i+t_i \cdot n_i) - (a_j+t_j \cdot n_j) =x_i – x_j $$ for any $i,j$.
Minimizing the distances of $x’_i$ its corresponding ray by differentiation of distances sum (calculated using the projection of $(a_i- x'_i)$ on the $n_i$ direction) $$\min\sum_{i=0}^k \Vert (x'_i- a_i)-((x'_i-a_i)\cdot n_i)n_i \Vert $$ or in other words: $$\min\sum_{i=0}^k \Vert (M \cdot x_i - a_i)-((M \cdot x_i - a_i)\cdot n_i)n_i \Vert. $$
Same as (2) but using Pythagoras to calculate the distance: $$\min\sum_{i=0}^k \Vert (M \cdot x_i - a_i)\Vert^2-\Vert((M \cdot x_i - a_i)\cdot n_i)n_i \Vert^2 $$
The first solution is inefficient because I cannot find the matrix straight-forward, but have to find the points first. Moreover, it takes a lot of computation time. So I dumped it.
I wish to find the matrix explicitly using minimization, but I am not sure how to proceed from what I wrote in the (2) and (3)
Of course, if anyone has any other solution, I will appreciate it.