contrib/oxl/mvl/mvl_six_point_design_matrix_row.cxx
Go to the documentation of this file.
00001 // This is oxl/mvl/mvl_six_point_design_matrix_row.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "mvl_six_point_design_matrix_row.h"
00010 
00011 #include <vcl_cmath.h>
00012 
00013 void mvl_six_point_design_matrix_row(vnl_matrix<double> const &A,
00014                                      vnl_matrix<double> const &B,
00015                                      double u, double v,
00016                                      double out_row[5])
00017 {
00018   // Translate the last point (u, v) to the origin (0, 0):
00019   double CA[2][4], CB[2][4];
00020   for (int j=0; j<4; ++j) {
00021     CA[0][j] = A[0][j] - u * A[2][j];
00022     CA[1][j] = A[1][j] - v * A[2][j];
00023     //CA[2][j] = A[2][j];
00024 
00025     CB[0][j] = B[0][j] - u * B[2][j];
00026     CB[1][j] = B[1][j] - v * B[2][j];
00027     //CB[2][j] = B[2][j];
00028   }
00029 
00030   // Normalize CA, CB using magic.
00031   {
00032     double nn = 0;
00033     for (int i=0; i<2; ++i)
00034       for (int j=0; j<4; ++j)
00035         nn += CA[i][j] * CA[i][j] + CB[i][j] * CB[i][j];
00036     nn = vcl_sqrt(nn);
00037 
00038     for (int i=0; i<2; ++i) {
00039       for (int j=0; j<4; ++j) {
00040         CA[i][j] /= nn;
00041         CB[i][j] /= nn;
00042       }
00043     }
00044   }
00045 
00046   // The constraint on X (last point) is that [A X, B X, x] = 0. This means
00047   // that the last entry of the cross product of CA*X and CB*X is zero, and
00048   // can be written as Q(X) = Q(X, X) = 0 where the (asymmetric) matrix of
00049   // the quadric Q is computed as follows:
00050   double Q[4][4];
00051   for (int r=0; r<4; ++r)
00052     for (int s=0; s<4; ++s)
00053       Q[r][s] = CA[0][r]*CB[1][s] - CA[1][r]*CB[0][s];
00054 
00055   // The quadric Q vanishes on the five canonical base points, so
00056   // the quadratic expression 2 Q(X) is equivalent to a linear one
00057   // in the five variables
00058   //    (a, b, c, d, e) = \psi(X)
00059   // Compute the coefficients.
00060   out_row[0] = Q[0][1] + Q[1][0]; // a
00061   out_row[1] = Q[0][2] + Q[2][0]; // b
00062   out_row[2] = Q[1][2] + Q[2][1]; // c
00063   out_row[3] = Q[1][3] + Q[3][1]; // d
00064   out_row[4] = Q[2][3] + Q[3][2]; // e
00065 }