core/vgl/algo/vgl_homg_operators_1d.txx
Go to the documentation of this file.
00001 // This is core/vgl/algo/vgl_homg_operators_1d.txx
00002 #ifndef vgl_homg_operators_1d_txx_
00003 #define vgl_homg_operators_1d_txx_
00004 
00005 #include "vgl_homg_operators_1d.h"
00006 #include <vnl/vnl_vector_fixed.h>
00007 #include <vnl/vnl_matrix_fixed.h>
00008 #include <vgl/vgl_homg.h> // for infinity
00009 #include <vcl_cmath.h>
00010 #include <vcl_iostream.h>
00011 
00012 template <class T>
00013 vnl_vector_fixed<T,2> vgl_homg_operators_1d<T>::get_vector(vgl_homg_point_1d<T> const& p)
00014 {
00015   return vnl_vector_fixed<T,2>(p.x(),p.w());
00016 }
00017 
00018 template <class T>
00019 double vgl_homg_operators_1d<T>::cross_ratio(const vgl_homg_point_1d<T>& a,
00020                                              const vgl_homg_point_1d<T>& b,
00021                                              const vgl_homg_point_1d<T>& c,
00022                                              const vgl_homg_point_1d<T>& d)
00023 {
00024   T x1 = a.x(), w1 = a.w();
00025   T x2 = b.x(), w2 = b.w();
00026   T x3 = c.x(), w3 = c.w();
00027   T x4 = d.x(), w4 = d.w();
00028   T n = (x1*w3-x3*w1)*(x2*w4-x4*w2);
00029   T m = (x1*w4-x4*w1)*(x2*w3-x3*w2);
00030   if (n == 0 && m == 0)
00031     vcl_cerr << "cross_ratio not defined: three of the given points coincide\n";
00032   return n/m;
00033 }
00034 
00035 template <class T>
00036 T vgl_homg_operators_1d<T>::cross(const vgl_homg_point_1d<T>& a, const vgl_homg_point_1d<T>& b)
00037 {
00038   T x1 = a.x(), w1 = a.w();
00039   T x2 = b.x(), w2 = b.w();
00040   return x1*w2-w1*x2;
00041 }
00042 
00043 template <class T>
00044 T vgl_homg_operators_1d<T>::dot(const vgl_homg_point_1d<T>& a, const vgl_homg_point_1d<T>& b)
00045 {
00046   T x1 = a.x(), w1 = a.w();
00047   T x2 = b.x(), w2 = b.w();
00048   return x1*x2 + w1*w2;
00049 }
00050 
00051 template <class T>
00052 void vgl_homg_operators_1d<T>::unitize(vgl_homg_point_1d<T>& a)
00053 {
00054   double norm = vcl_sqrt (a.x()*a.x() + a.w()*a.w());
00055   if (norm == 0.0) {
00056     vcl_cerr << "vgl_homg_operators_1d<T>::unitize() -- Zero length vector\n";
00057     return;
00058   }
00059   norm = 1.0/norm;
00060   a.set(T(a.x()*norm), T(a.w()*norm));
00061 }
00062 
00063 template <class T>
00064 T vgl_homg_operators_1d<T>::distance (const vgl_homg_point_1d<T>& a,
00065                                       const vgl_homg_point_1d<T>& b)
00066 {
00067   T x1 = a.x(), w1 = a.w();
00068   T x2 = b.x(), w2 = b.w();
00069   if (w1 == 0 || w2 == 0) {
00070     vcl_cerr << "vgl_homg_operators_1d<T>::distance() -- point at infinity";
00071     return vgl_homg<T>::infinity;
00072   }
00073   x1 /= w1; x2 /= w2;
00074   return (x1 > x2) ? x1-x2 : x2-x1;
00075 }
00076 
00077 template <class T>
00078 T vgl_homg_operators_1d<T>::distance_squared (const vgl_homg_point_1d<T>& point1,
00079                                               const vgl_homg_point_1d<T>& point2)
00080 {
00081   T d = distance(point1,point2);
00082   return d*d;
00083 }
00084 
00085 template <class T>
00086 vgl_homg_point_1d<T> vgl_homg_operators_1d<T>::midpoint(const vgl_homg_point_1d<T>& a,
00087                                                         const vgl_homg_point_1d<T>& b)
00088 {
00089   T x1 = a.x(), w1 = a.w();
00090   T x2 = b.x(), w2 = b.w();
00091   return vgl_homg_point_1d<T>(x1*w2+x2*w1, 2*w1*w2);
00092 }
00093 
00094 template <class T>
00095 T vgl_homg_operators_1d<T>::conjugate(T x1, T x2, T x3, double cr)
00096 // Default for cr is -1.
00097 {
00098   T a = x1 - x3;  T b = x2 - x3; T c = T(a-cr*b);
00099   if (c == 0) return (x2*a == cr*x1*b) ? 1 : vgl_homg<T>::infinity;
00100   return T((x2*a-cr*x1*b)/c);
00101 }
00102 
00103 template <class T>
00104 vgl_homg_point_1d<T> vgl_homg_operators_1d<T>::conjugate(const vgl_homg_point_1d<T>& a,
00105                                                          const vgl_homg_point_1d<T>& b,
00106                                                          const vgl_homg_point_1d<T>& c,
00107                                                          double cr)
00108 // Default for cr is -1.
00109 {
00110   T x1 = a.x(), w1 = a.w();
00111   T x2 = b.x(), w2 = b.w();
00112   T x3 = c.x(), w3 = c.w();
00113   T k = x1*w3 - x3*w1, m = x2*w3 - x3*w2;
00114   return vgl_homg_point_1d<T>(T(x2*k-cr*x1*m), T(k*w2-cr*m*w1));
00115   // could be (0,0) !!  not checked.
00116 }
00117 
00118 template <class T>
00119 vgl_homg_point_1d<T> operator*(vnl_matrix_fixed<T,2,2> const& m,
00120                                vgl_homg_point_1d<T> const& p)
00121 {
00122   return vgl_homg_point_1d<T>(m(0,0)*p.x()+m(0,1)*p.w(),
00123                               m(1,0)*p.x()+m(1,1)*p.w());
00124 }
00125 
00126 #undef VGL_HOMG_OPERATORS_1D_INSTANTIATE
00127 #define VGL_HOMG_OPERATORS_1D_INSTANTIATE(T) \
00128 template class vgl_homg_operators_1d<T >; \
00129 template vgl_homg_point_1d<T > operator*(vnl_matrix_fixed<T,2,2> const& m, vgl_homg_point_1d<T > const& p)
00130 
00131 #endif // vgl_homg_operators_1d_txx_