I already posted a similar question. but since the problem is still unanswered and the details are a bit changed, I'm posting it again.
I'm trying to solve the following mathematical problem:
Given 2 sets of 3 lines each in 3D space (where each line is given by a $3D$ point and a $3D$ vector): $$ set1=\{ [(px1,py1,pz1), (vx1,vy1,vz1)], [(px2,py2,pz2),(vx2,vy2,vz2)], [(px3,py3,pz3), (vx3,vy3,vz3)] \} $$ $$ set2=\{ [(qx1,qy1,qz1), (ux1,uy1,uz1)], [(qx2,qy2,qz2),(ux2,uy2,uz2)], [(qx3,qy3,qz3), (ux3,uy3,uz3)] \} $$
find the angles of rotation $x,y,z$ (in radians) such that:
$$R=rotate\_x*rotate\_y*rotate\_z$$ $$ {rotate\_x} = \left( {\matrix{ {1} & {0} & {0} \cr {0} & {cos(x)} & {-sin(x)} \cr {0} & {sin(x)} & {cos(x)} \cr } } \right) $$ $$ {rotate\_y} = \left( {\matrix{ {cos(y)} & {0} & {sin(y)} \cr {0} & {1} & {0} \cr {-sin(y)} & {0} & {cos(y)} \cr } } \right) $$ $$ {rotate\_z} = \left( {\matrix{ {cos(z)} & {-sin(z)} & {0} \cr {sin(z)} & {cos(z)} & {0} \cr {0} & {0} & {1} \cr } } \right) $$
and if we rotate $set1$ by $R$ then all the lines in $set1$ will intersect their corresponding lines in $set2$.
Meaning, let's define $set1\_r$ as $set1$ after the above rotation.
Then line $i$ $(i=1,2,3)$ in $set1\_r$ will intersect line $i$ in $set2$.
I already built the 3 equations with 3 variables that needed to be solved. but they seem to be unsolvable.
Here are the equations (only the angles $x,y,z$ are unknown and the rest are known):
eq1 = qx1*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)) + qy1*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)) + ((ux1*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)) + uy1*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)) + uz1*cos(x)*cos(y))*(px1 - qz1*sin(y) - qx1*cos(y)*cos(z) + qy1*cos(y)*sin(z) + (vx1*(qx1*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) - py1 + qy1*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - qz1*cos(y)*sin(x) + ((ux1*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy1*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz1*cos(y)*sin(x))*(px1 - qz1*sin(y) - qx1*cos(y)*cos(z) + qy1*cos(y)*sin(z)))/(uz1*sin(y) + ux1*cos(y)*cos(z) - uy1*cos(y)*sin(z))))/(vy1 - (vx1*(ux1*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy1*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz1*cos(y)*sin(x)))/(uz1*sin(y) + ux1*cos(y)*cos(z) - uy1*cos(y)*sin(z)))))/(uz1*sin(y) + ux1*cos(y)*cos(z) - uy1*cos(y)*sin(z)) + qz1*cos(x)*cos(y) == pz1 + (vz1*(qx1*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) - py1 + qy1*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - qz1*cos(y)*sin(x) + ((ux1*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy1*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz1*cos(y)*sin(x))*(px1 - qz1*sin(y) - qx1*cos(y)*cos(z) + qy1*cos(y)*sin(z)))/(uz1*sin(y) + ux1*cos(y)*cos(z) - uy1*cos(y)*sin(z))))/(vy1 - (vx1*(ux1*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy1*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz1*cos(y)*sin(x)))/(uz1*sin(y) + ux1*cos(y)*cos(z) - uy1*cos(y)*sin(z)))
eq2 = qx2*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)) + qy2*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)) + ((ux2*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)) + uy2*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)) + uz2*cos(x)*cos(y))*(px2 - qz2*sin(y) - qx2*cos(y)*cos(z) + qy2*cos(y)*sin(z) + (vx2*(qx2*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) - py2 + qy2*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - qz2*cos(y)*sin(x) + ((ux2*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy2*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz2*cos(y)*sin(x))*(px2 - qz2*sin(y) - qx2*cos(y)*cos(z) + qy2*cos(y)*sin(z)))/(uz2*sin(y) + ux2*cos(y)*cos(z) - uy2*cos(y)*sin(z))))/(vy2 - (vx2*(ux2*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy2*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz2*cos(y)*sin(x)))/(uz2*sin(y) + ux2*cos(y)*cos(z) - uy2*cos(y)*sin(z)))))/(uz2*sin(y) + ux2*cos(y)*cos(z) - uy2*cos(y)*sin(z)) + qz2*cos(x)*cos(y) == pz2 + (vz2*(qx2*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) - py2 + qy2*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - qz2*cos(y)*sin(x) + ((ux2*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy2*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz2*cos(y)*sin(x))*(px2 - qz2*sin(y) - qx2*cos(y)*cos(z) + qy2*cos(y)*sin(z)))/(uz2*sin(y) + ux2*cos(y)*cos(z) - uy2*cos(y)*sin(z))))/(vy2 - (vx2*(ux2*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy2*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz2*cos(y)*sin(x)))/(uz2*sin(y) + ux2*cos(y)*cos(z) - uy2*cos(y)*sin(z)))
eq3 = qx3*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)) + qy3*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)) + ((ux3*(sin(x)*sin(z) - cos(x)*cos(z)*sin(y)) + uy3*(cos(z)*sin(x) + cos(x)*sin(y)*sin(z)) + uz3*cos(x)*cos(y))*(px3 - qz3*sin(y) - qx3*cos(y)*cos(z) + qy3*cos(y)*sin(z) + (vx3*(qx3*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) - py3 + qy3*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - qz3*cos(y)*sin(x) + ((ux3*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy3*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz3*cos(y)*sin(x))*(px3 - qz3*sin(y) - qx3*cos(y)*cos(z) + qy3*cos(y)*sin(z)))/(uz3*sin(y) + ux3*cos(y)*cos(z) - uy3*cos(y)*sin(z))))/(vy3 - (vx3*(ux3*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy3*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz3*cos(y)*sin(x)))/(uz3*sin(y) + ux3*cos(y)*cos(z) - uy3*cos(y)*sin(z)))))/(uz3*sin(y) + ux3*cos(y)*cos(z) - uy3*cos(y)*sin(z)) + qz3*cos(x)*cos(y) == pz3 + (vz3*(qx3*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) - py3 + qy3*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - qz3*cos(y)*sin(x) + ((ux3*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy3*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz3*cos(y)*sin(x))*(px3 - qz3*sin(y) - qx3*cos(y)*cos(z) + qy3*cos(y)*sin(z)))/(uz3*sin(y) + ux3*cos(y)*cos(z) - uy3*cos(y)*sin(z))))/(vy3 - (vx3*(ux3*(cos(x)*sin(z) + cos(z)*sin(x)*sin(y)) + uy3*(cos(x)*cos(z) - sin(x)*sin(y)*sin(z)) - uz3*cos(y)*sin(x)))/(uz3*sin(y) + ux3*cos(y)*cos(z) - uy3*cos(y)*sin(z)))
It can be seen that it's almost impossible (hopefully not impossible) to extract $x,y,z$ from the equations since there are many multiplications between $sin$ and $cos$ of the different angles. I couldn't solve it. and I also tried it in matlab using the 'solve' function but it didn't work.
Can anyone suggest a solution for finding the angles $x,y,z$? or any other way of finding the matrix $R$?
If it helps, here's a specific example of the 2 sets of lines in which I know the desired angles $x,y,z$ (there are probably many more triplets of angles that also solve the problem).
%set1
px1=0;
py1=0;
pz1=-30;
px2=0;
py2=0;
pz2=-30;
px3=0;
py3=0;
pz3=-30;
vx1 = -0.083717247687439;
vy1 = -0.107930827800543;
vz1 = 0.990627255252918;
vx2 = 0.076364294519742;
vy2 = 0.060269029165473;
vz2 = 0.995256820446840;
vx3 = -0.081460429387834;
vy3 = 0.105021268850622;
vz3 = 0.991128009660183;
%set2
qx1=0;
qy1=0;
qz1=-30;
qx2=0;
qy2=0;
qz2=-30;
qx3=0;
qy3=0;
qz3=-30;
ux1 = -0.079382581863774;
uy1 = -0.095259098236529;
uz1 = 0.992282273297173;
ux2 = 0.079382581863774;
uy2 = 0.095259098236529;
uz2 = 0.992282273297173;
ux3 = -0.086165283952334;
uy3 = 0.103398340742801;
uz3 = 0.990900765451843;
For the above example, for $x=0.5236$ ($x$ is 30 degrees), $y=0, z=0$, meaning, $$ {R} = \left( {\matrix{ {1} & {0} & {0} \cr {0} & {0.866} & {-0.5} \cr {0} & {0.5} & {0.866} \cr } } \right) $$
$line$ $i$ ($i=1,2,3$) in $set1$ will intersect $line$ $i$ in $set2$.
Also, $x=0, y=0, z=0$ ($R$ is the identity matrix) is also a solution since all the $6$ lines already intersect at the point $(0,0,-30)$.
And I'm sure there are many more solutions.
But, of course I need the general solution.
Hope anyone can solve it since I've been trying to solve it for weeks :(.
Thanks
Writing the paramtric equations of the lines, with respective parameters $r_i$ and $s_j$, you have
$$R\,\,(\mathbb p_i+r_i\mathbb v_i)=\mathbb q_j+s_j\mathbb u_j$$ where $i,j$ are some (possibly distinct) permutations of $\{0,1,2\}$ (so that you will have to solve the problem six times).
Rewriting
$$R\,\mathbb p_i+r_iR\,\mathbb v_i=q_j+s_j\mathbb u_j$$ and taking the cross product with $(R\,\mathbb v_i)\times\mathbb u_j$, you can get rid of the parameters with
$$R\,\mathbb p_i\times((R\,\mathbb v_i)\times\mathbb u_j)=q_j\times((R\,\mathbb v_i)\times\mathbb u_j).$$
Then using the expression of $R$,
$$R=R_x\cdot R_y\cdot R_z,$$ you get a nasty system of nine nonlinear (trigonometric) equations in three unknows.
The equations can also be cast in terms of the matrix elements, which you constrain by expressing the orthonormality of the matrix,
$$R^TR=I.$$
Hence you get a monstrous system of fifteen quadratic equations in nine unknowns, which can have up to $512$ distinct solutions, if I am right, and will be subject to compatibility conditions.