title: 3D rotation matrix between two 3D points
body: <p>I have 2 known 3d points OC1 and OC2 which are the origin of 2 axis plot in the space and I need to compute the 3D rotation matrix between them. 
  <img src="https://i.stack.imgur.com/REcE1.png" alt="enter image description here"></p>
  
  <p>I know that using R1&amp;T1 I can get to Oc1 and using R2&amp;T2 I can get to Oc2 but I need to compute the 3D rotation matrix between Oc1 and Oc2. I just thought about this rule:</p>
  
  <pre><code>oMc1=(R1 | T1) and oMc2=(R2 | T2) and what I want is:
  c1Mc2 = (oMc1)^-1 oMc2
  </code></pre>
  
  <p>So I tried to implement it and here is my code:</p>
  
  <pre><code>    vector &lt;Point3f&gt; listOfPointsOnTable;
      cout &lt;&lt; "******** DATA *******" &lt;&lt; endl;
      listOfPointsOnTable.push_back(Point3f(0,0,0));
      listOfPointsOnTable.push_back(Point3f(100,0,0));
      listOfPointsOnTable.push_back(Point3f(100,100,0));
      listOfPointsOnTable.push_back(Point3f(0,100,0));
  
      cout &lt;&lt; endl &lt;&lt; "Scene points :" &lt;&lt; endl;
      for (int i = 0; i &lt; listOfPointsOnTable.size(); i++)
      {
          cout &lt;&lt; listOfPointsOnTable[i] &lt;&lt; endl;
      }
  
      //Define the optical center of each camera
      Point3f centreOfC1 = Point3f(23,0,50);
      Point3f centreOfC2 = Point3f(0,42,20);
      cout &lt;&lt; endl &lt;&lt; "Center Of C1: " &lt;&lt; centreOfC1 &lt;&lt; " , Center of C2 : " &lt;&lt; centreOfC2 &lt;&lt; endl;
  
      //Define the translation and rotation between main axis and the camera 1 axis
      Mat translationOfC1 = (Mat_&lt;double&gt;(3, 1) &lt;&lt; (0-centreOfC1.x), (0-centreOfC1.y), (0-centreOfC1.z));
      float rotxC1 = 0, rotyC1 = 0, rotzC1 = -45;
      int focaleC1 = 2;
      Mat rotationOfC1 = rotation3D(rotxC1, rotyC1,rotzC1);
      cout &lt;&lt; endl &lt;&lt; "Translation from default axis to C1: " &lt;&lt; translationOfC1 &lt;&lt; endl;
      cout &lt;&lt; "Rotation from default axis to C1: " &lt;&lt; rotationOfC1 &lt;&lt; endl;
      Mat transformationToC1 = buildTransformationMatrix(rotationOfC1, translationOfC1);
      cout &lt;&lt; "Transformation from default axis to C1: " &lt;&lt; transformationToC1 &lt;&lt; endl &lt;&lt; endl;
  
      //Define the translation and rotation between main axis and the camera 2 axis
      Mat translationOfC2 = (Mat_&lt;double&gt;(3, 1) &lt;&lt; (0-centreOfC2.x), (0-centreOfC2.y), (0-centreOfC2.z));
      float rotxC2 = 0, rotyC2 = 0, rotzC2 = -90;
      int focaleC2 = 2;
      Mat rotationOfC2 = rotation3D(rotxC2, rotyC2,rotzC2);
      cout &lt;&lt; endl &lt;&lt; "Translation from default axis to C2: " &lt;&lt; translationOfC2 &lt;&lt; endl;
      cout &lt;&lt; "Rotation from default axis to C2: " &lt;&lt; rotationOfC2 &lt;&lt; endl;
      Mat transformationToC2 = buildTransformationMatrix(rotationOfC2, translationOfC2);
      cout &lt;&lt; "Transformation from default axis to C2: " &lt;&lt; transformationToC2 &lt;&lt; endl &lt;&lt; endl;
  
      Mat centreOfC2InMat = (Mat_&lt;double&gt;(3, 1) &lt;&lt; centreOfC2.x, centreOfC2.y, centreOfC2.z);
      Mat centreOfC2InCamera1 = rotationOfC1 * centreOfC2InMat + translationOfC1;
      Mat translationBetweenC1AndC2 = -centreOfC2InCamera1;
      cout &lt;&lt; endl &lt;&lt; "****Translation from C2 to C1" &lt;&lt; endl;
      cout &lt;&lt; translationBetweenC1AndC2 &lt;&lt; endl;
      Mat centreOfC1InMat = (Mat_&lt;double&gt;(3, 1) &lt;&lt; centreOfC1.x, centreOfC1.y, centreOfC1.z);
      Mat centreOfC1InCamera2 = rotationOfC2 * centreOfC1InMat + translationOfC2;
      Mat translationBetweenC2AndC1 = -centreOfC1InCamera2;
      cout &lt;&lt; "****Translation from C1 to C2" &lt;&lt; endl;
      cout &lt;&lt; translationBetweenC2AndC1 &lt;&lt; endl;
  
      cout &lt;&lt; "Tran1-1 * Trans2 = " &lt;&lt; transformationToC1.inv() * transformationToC2 &lt;&lt; endl;
      cout &lt;&lt; "Tran2-1 * Trans1 = " &lt;&lt; transformationToC2.inv() * transformationToC1 &lt;&lt; endl;
  
  Mat rotation3D(int alpha, int beta, int gamma)
  {
      // Rotation matrices around the X, Y, and Z axis
      double alphaInRadian = alpha * M_PI / 180.0;
      double betaInRadian = beta * M_PI / 180.0;
      double gammaInRadian = gamma * M_PI / 180.0;
      Mat RX = (Mat_&lt;double&gt;(3, 3) &lt;&lt;
                    1,          0,           0,
                    0, cosf(alphaInRadian), sinf(alphaInRadian),
                    0, -sinf(alphaInRadian),  cosf(alphaInRadian));
      Mat RY = (Mat_&lt;double&gt;(3, 3) &lt;&lt;
                    cosf(betaInRadian), 0, sinf(betaInRadian),
                    0, 1,          0,
                    -sinf(betaInRadian), 0,  cosf(betaInRadian));
      Mat RZ = (Mat_&lt;double&gt;(3, 3) &lt;&lt;
                    cosf(gammaInRadian), sinf(gammaInRadian), 0,
                    -sinf(gammaInRadian),cosf(gammaInRadian), 0,
                    0,          0,           1);
      // Composed rotation matrix with (RX, RY, RZ)
      Mat R = RX * RY * RZ;
      return R;
  }
  Mat buildTransformationMatrix(Mat rotation, Mat translation)
  {
      Mat transformation = (Mat_&lt;double&gt;(4, 4) &lt;&lt;
                            rotation.at&lt;double&gt;(0,0), rotation.at&lt;double&gt;(0,1), rotation.at&lt;double&gt;(0,2), translation.at&lt;double&gt;(0,0),
                            rotation.at&lt;double&gt;(1,0), rotation.at&lt;double&gt;(1,1), rotation.at&lt;double&gt;(1,2), translation.at&lt;double&gt;(1,0),
                            rotation.at&lt;double&gt;(2,0), rotation.at&lt;double&gt;(2,1), rotation.at&lt;double&gt;(2,2), translation.at&lt;double&gt;(2,0),
                            0, 0, 0, 1);
  
      return transformation;
  }
  </code></pre>
  
  <p>here is the output:</p>
  
  <pre><code>//Origin of 3 axis     
  O(0,0,0), OC1 (23, 0, 50), OC2 (0, 42, 20)
  
  Translation from default axis to OC1: [-23;
   0;
   -50]
  Rotation from default axis to OC1: [0.7071067690849304, -0.7071067690849304, 0;
   0.7071067690849304, 0.7071067690849304, 0;
   0, 0, 1]
  Trans1 = Transformation from default axis to OC1: [0.7071067690849304, -0.7071067690849304, 0, -23;
   0.7071067690849304, 0.7071067690849304, 0, 0;
   0, 0, 1, -50;
   0, 0, 0, 1]
  
  Translation from default axis to OC2: [0;
   -42;
   -20]
  Rotation from default axis to OC2: [-4.371138828673793e-08, -1, 0;
   1, -4.371138828673793e-08, 0;
   0, 0, 1]
  Trans2 = Transformation from default axis to OC2: [-4.371138828673793e-08, -1, 0, 0;
   1, -4.371138828673793e-08, 0, -42;
   0, 0, 1, -20;
   0, 0, 0, 1]
  
  (Trans1)-1 * (Trans2) = [0.7071067623795453, -0.7071068241967844, 0, -13.43502907247513;
   0.7071068241967844, 0.7071067623795453, 0, -45.96194156373071;
   0, 0, 1, 30;
   0, 0, 0, 1]
  (Trans2)-1 * (Trans1) = [0.7071067381763105, 0.7071067999935476, 0, 42.00000100536185;
   -0.7071067999935475, 0.7071067381763104, -0, 22.99999816412165;
   0, 0, 1, -30;
   0, 0, 0, 1]
  
  //Calculation of translation between OC1 and OC2:
  ****Translation from C2 to C1
  [52.69848430156708;
   -29.69848430156708;
   30]
  ****Translation from C1 to C2
  [1.005361930594972e-06;
   19;
   -30]
  </code></pre>
  
  <p>As you can see above, the 4th column of <code>(Trans1)-1 * (Trans2)</code> is not equal neither to the translation from <code>C2-&gt;C1</code> nor to the translation from <code>C2-&gt;C1</code></p>
  
  <p>it lets me think that <code>c1Mc2 = (oMc1)^-1 oMc2</code> does not get what I want but I don't really understand why? Is there any other solution to get what I want?</p>