core/vgl/vgl_homg_point_2d.txx
Go to the documentation of this file.
00001 // This is core/vgl/vgl_homg_point_2d.txx
00002 #ifndef vgl_homg_point_2d_txx_
00003 #define vgl_homg_point_2d_txx_
00004 
00005 #include "vgl_homg_point_2d.h"
00006 #include <vgl/vgl_homg_line_2d.h>
00007 #include <vcl_iostream.h>
00008 
00009 // Note that the given lines must be distinct!
00010 template <class Type>
00011 vgl_homg_point_2d<Type>::vgl_homg_point_2d (vgl_homg_line_2d<Type> const& l1,
00012                                             vgl_homg_line_2d<Type> const& l2)
00013 {
00014   set(l1.b()*l2.c()-l1.c()*l2.b(),
00015       l1.c()*l2.a()-l1.a()*l2.c(),
00016       l1.a()*l2.b()-l1.b()*l2.a());
00017 }
00018 
00019 template <class T>
00020 double cross_ratio(vgl_homg_point_2d<T>const& p1, vgl_homg_point_2d<T>const& p2,
00021                    vgl_homg_point_2d<T>const& p3, vgl_homg_point_2d<T>const& p4)
00022 {
00023   // least squares solution: (Num_x-CR*Den_x)^2 + (Num_y-CR*Den_y)^2 minimal.
00024   double Num_x = (p1.x()*p3.w()-p3.x()*p1.w())*(p2.x()*p4.w()-p4.x()*p2.w());
00025   double Num_y = (p1.y()*p3.w()-p3.y()*p1.w())*(p2.y()*p4.w()-p4.y()*p2.w());
00026   double Den_x = (p1.x()*p4.w()-p4.x()*p1.w())*(p2.x()*p3.w()-p3.x()*p2.w());
00027   double Den_y = (p1.y()*p4.w()-p4.y()*p1.w())*(p2.y()*p3.w()-p3.y()*p2.w());
00028   if (Den_x == Den_y) return 0.5*(Num_x+Num_y)/Den_x;
00029   else return (Den_x*Num_x+Den_y*Num_y)/(Den_x*Den_x+Den_y*Den_y);
00030 }
00031 
00032 template <class Type>
00033 vcl_ostream& operator<<(vcl_ostream& s, vgl_homg_point_2d<Type> const& p)
00034 {
00035   return s << " <vgl_homg_point_2d ("
00036            << p.x() << ',' << p.y() << ',' << p.w() << ") >";
00037 }
00038 
00039 template <class Type>
00040 vcl_istream& operator>>(vcl_istream& s, vgl_homg_point_2d<Type>& p)
00041 {
00042   Type x, y, w;
00043   s >> x >> y >> w;
00044   p.set(x,y,w);
00045   return s;
00046 }
00047 
00048 #undef VGL_HOMG_POINT_2D_INSTANTIATE
00049 #define VGL_HOMG_POINT_2D_INSTANTIATE(T) \
00050 template class vgl_homg_point_2d<T >; \
00051 template double cross_ratio(vgl_homg_point_2d<T >const&, vgl_homg_point_2d<T >const&, \
00052                             vgl_homg_point_2d<T >const&, vgl_homg_point_2d<T >const&); \
00053 template vcl_ostream& operator<<(vcl_ostream&, vgl_homg_point_2d<T >const&); \
00054 template vcl_istream& operator>>(vcl_istream&, vgl_homg_point_2d<T >&)
00055 
00056 #endif // vgl_homg_point_2d_txx_