contrib/oxl/mvl/PMatrixDec.cxx
Go to the documentation of this file.
00001 // This is oxl/mvl/PMatrixDec.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 //  \file
00007 
00008 #include "PMatrixDec.h"
00009 
00010 #include <vcl_cmath.h>
00011 #include <vcl_iostream.h>
00012 
00013 //--------------------------------------------------------------
00014 //: Constructor
00015 
00016 PMatrixDec::PMatrixDec(const vnl_matrix<double>& p_matrix)
00017   : PMatrix(p_matrix), j_matrix_(3,3), d_matrix_(4,4)
00018 {
00019   if (!((p_matrix.rows() == 3) && (p_matrix.columns() == 4)))
00020     vcl_cerr << "PMatrixDec WARNING: Incorrect size of matrix\n";
00021   Init();
00022 }
00023 
00024 
00025 //--------------------------------------------------------------
00026 //: Destructor
00027 
00028 PMatrixDec::~PMatrixDec() {}
00029 
00030 
00031 //--------------------------------------------------------------
00032 //: Auxiliary Constructor
00033 
00034 void PMatrixDec::Init()
00035 {
00036     double u0 = p_matrix_(0,0)*p_matrix_(2,0)
00037               + p_matrix_(0,1)*p_matrix_(2,1)
00038               + p_matrix_(0,2)*p_matrix_(2,2);
00039     double v0 = p_matrix_(1,0)*p_matrix_(2,0)
00040               + p_matrix_(1,1)*p_matrix_(2,1)
00041               + p_matrix_(1,2)*p_matrix_(2,2);
00042 
00043     double a_u = p_matrix_(0,0)*p_matrix_(0,0)
00044                + p_matrix_(0,1)*p_matrix_(0,1)
00045                + p_matrix_(0,2)*p_matrix_(0,2);
00046     double a_v = p_matrix_(1,0)*p_matrix_(1,0)
00047                + p_matrix_(1,1)*p_matrix_(1,1)
00048                + p_matrix_(1,2)*p_matrix_(1,2);
00049     a_u = a_u-u0*u0;
00050     a_v = a_v-v0*v0;
00051     if ((a_u <= 0.0) || (a_v <= 0.0))
00052       vcl_cerr << "PMatrixDec WARNING: Incorrect projection matrix\n";
00053     else
00054     {
00055       a_u = vcl_sqrt(a_u);
00056       a_v = vcl_sqrt(a_v);
00057 
00058       // INTRINSIC parameters => J
00059       j_matrix_(0,0) = a_u;
00060       j_matrix_(0,1) = 0.0;
00061       j_matrix_(0,2) = u0;
00062 
00063       j_matrix_(1,0) = 0.0;
00064       j_matrix_(1,1) = a_v;
00065       j_matrix_(1,2) = v0;
00066 
00067       j_matrix_(2,0) = 0.0;
00068       j_matrix_(2,1) = 0.0;
00069       j_matrix_(2,2) = 1.0; // last diagonal element = 1
00070 
00071       // EXTRINSIC parameters => D
00072       d_matrix_(0,0) = (p_matrix_(0,0) - u0*p_matrix_(2,0))/a_u;
00073       d_matrix_(0,1) = (p_matrix_(0,1) - u0*p_matrix_(2,1))/a_u;
00074       d_matrix_(0,2) = (p_matrix_(0,2) - u0*p_matrix_(2,2))/a_u;
00075       d_matrix_(0,3) = (p_matrix_(0,3) - u0*p_matrix_(2,3))/a_u;
00076 
00077       d_matrix_(1,0) = (p_matrix_(1,0) - v0*p_matrix_(2,0))/a_v;
00078       d_matrix_(1,1) = (p_matrix_(1,1) - v0*p_matrix_(2,1))/a_v;
00079       d_matrix_(1,2) = (p_matrix_(1,2) - v0*p_matrix_(2,2))/a_v;
00080       d_matrix_(1,3) = (p_matrix_(1,3) - v0*p_matrix_(2,3))/a_v;
00081 
00082       d_matrix_(2,0) = p_matrix_(2,0);
00083       d_matrix_(2,1) = p_matrix_(2,1);
00084       d_matrix_(2,2) = p_matrix_(2,2);
00085       d_matrix_(2,3) = p_matrix_(2,3);
00086 
00087       d_matrix_(3,0) = 0.0;
00088       d_matrix_(3,1) = 0.0;
00089       d_matrix_(3,2) = 0.0;
00090       d_matrix_(3,3) = 1.0; // last diagonal element = 1
00091     }
00092 }
00093 
00094 
00095 //---------------------------------------------------------------
00096 //: Print to vcl_ostream
00097 vcl_ostream& operator<<(vcl_ostream& s, const PMatrixDec& P)
00098 {
00099   s << "PROJECTION MATRIX = [\n" << P.get_matrix() << "]\n"
00100     << "DECOMPOSITION:\n"
00101     << "Intrinsic Parameters = [\n" << P.j_matrix_ << "]\n"
00102     << "Extrinsic Parameters = [\n" << P.d_matrix_ << "]\n";
00103   return s;
00104 }
00105 
00106 
00107 //---------------------------------------------------------------
00108 //: Test --
00109 
00110 void PMatrixDec::Test()
00111 {
00112   vnl_matrix<double> matrix1(3,4);
00113   matrix1(0,0)= 568.051; matrix1(1,0)= -83.1082;matrix1(2,0)= -0.274578;
00114   matrix1(0,1)= 9.18145; matrix1(1,1)= 689.13;  matrix1(2,1)= -0.0458282;
00115   matrix1(0,2)= 552.506; matrix1(1,2)= 194.806; matrix1(2,2)= 0.960472;
00116   matrix1(0,3)= -596.353;matrix1(1,3)= 92.4159; matrix1(2,3)= 0.228363;
00117 
00118   vcl_cerr << "Correct Matrix:\n" << matrix1 << '\n';
00119   PMatrixDec pmat1(matrix1);
00120   vcl_cerr << pmat1;
00121   vnl_matrix<double> J(3,4);
00122   J.update(pmat1.IntrinsicParameters(), 0, 0); // Copy columns 0,1 and 2
00123   J(0,3) = 0.0; // Last column = 0
00124   J(1,3) = 0.0;
00125   J(2,3) = 0.0;
00126   vcl_cerr << "P = [J O_3]*D = [\n"
00127            << J * pmat1.ExtrinsicParameters() << "]\n"
00128            << "AlphaU=" << pmat1.GetAlphaU() << '\n'
00129            << "AlphaV=" << pmat1.GetAlphaV() << '\n'
00130            << "U0=" << pmat1.GetU0() << '\n'
00131            << "V0=" << pmat1.GetV0() << '\n';
00132 
00133   vnl_matrix<double> matrix2(4, 4);
00134   int i,j;
00135   for (j=0; j<4; j++)
00136     for (i=0; i<4; i++)
00137       matrix2(i,j)= 1.0;
00138 
00139   vcl_cerr << "Another Incorrect Matrix:\n"
00140            << matrix2 << '\n';
00141   PMatrixDec pmat2(matrix2);
00142   vcl_cerr << pmat2;
00143 
00144   vnl_matrix<double> matrix3(2, 2);
00145   for (j=0; j<2; j++)
00146     for (i=0; i<2; i++)
00147       matrix3(i,j)= 2.0;
00148 
00149   vcl_cerr << "Incorrect Matrix:\n"
00150            << matrix3 << '\n';
00151   PMatrixDec pmat3(matrix3);
00152   vcl_cerr << pmat3;
00153 }