vgl_homg_point_2d.hxx
Go to the documentation of this file.
1 // This is core/vgl/vgl_homg_point_2d.hxx
2 #ifndef vgl_homg_point_2d_hxx_
3 #define vgl_homg_point_2d_hxx_
4 
5 #include <iostream>
6 #include "vgl_homg_point_2d.h"
7 #include <vgl/vgl_homg_line_2d.h>
8 #ifdef _MSC_VER
9 # include <vcl_msvc_warnings.h>
10 #endif
11 
12 // Note that the given lines must be distinct!
13 template <class Type>
15  vgl_homg_line_2d<Type> const& l2)
16 {
17  set(l1.b()*l2.c()-l1.c()*l2.b(),
18  l1.c()*l2.a()-l1.a()*l2.c(),
19  l1.a()*l2.b()-l1.b()*l2.a());
20 }
21 
22 template <class T>
24  vgl_homg_point_2d<T>const& p3, vgl_homg_point_2d<T>const& p4)
25 {
26  // least squares solution: (Num_x-CR*Den_x)^2 + (Num_y-CR*Den_y)^2 minimal.
27  double Num_x = (p1.x()*p3.w()-p3.x()*p1.w())*(p2.x()*p4.w()-p4.x()*p2.w());
28  double Num_y = (p1.y()*p3.w()-p3.y()*p1.w())*(p2.y()*p4.w()-p4.y()*p2.w());
29  double Den_x = (p1.x()*p4.w()-p4.x()*p1.w())*(p2.x()*p3.w()-p3.x()*p2.w());
30  double Den_y = (p1.y()*p4.w()-p4.y()*p1.w())*(p2.y()*p3.w()-p3.y()*p2.w());
31  if (Den_x == Den_y) return 0.5*(Num_x+Num_y)/Den_x;
32  else return (Den_x*Num_x+Den_y*Num_y)/(Den_x*Den_x+Den_y*Den_y);
33 }
34 
35 template <class Type>
36 std::ostream& operator<<(std::ostream& s, vgl_homg_point_2d<Type> const& p)
37 {
38  return s << " <vgl_homg_point_2d ("
39  << p.x() << ',' << p.y() << ',' << p.w() << ") >";
40 }
41 
42 template <class Type>
43 std::istream& operator>>(std::istream& s, vgl_homg_point_2d<Type>& p)
44 {
45  Type x, y, w;
46  s >> x >> y >> w;
47  p.set(x,y,w);
48  return s;
49 }
50 
51 #undef VGL_HOMG_POINT_2D_INSTANTIATE
52 #define VGL_HOMG_POINT_2D_INSTANTIATE(T) \
53 template class vgl_homg_point_2d<T >; \
54 template double cross_ratio(vgl_homg_point_2d<T >const&, vgl_homg_point_2d<T >const&, \
55  vgl_homg_point_2d<T >const&, vgl_homg_point_2d<T >const&); \
56 template std::ostream& operator<<(std::ostream&, vgl_homg_point_2d<T >const&); \
57 template std::istream& operator>>(std::istream&, vgl_homg_point_2d<T >&)
58 
59 #endif // vgl_homg_point_2d_hxx_
T a() const
Parameter a of line a*x + b*y + c*w = 0.
Represents a homogeneous 2D line.
Definition: vgl_fwd.h:14
T b() const
Parameter b of line a*x + b*y + c*w = 0.
double cross_ratio(vgl_homg_point_1d< T >const &p1, vgl_homg_point_1d< T >const &p2, vgl_homg_point_1d< T >const &p3, vgl_homg_point_1d< T >const &p4)
cross ratio of four points.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
line in projective 2D space
point in projective 2D space
void set(T px, T py, T pw=(T) 1)
Set x,y,w.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
T c() const
Parameter c of line a*x + b*y + c*w = 0.
vgl_homg_point_2d()
Default constructor with (0,0,1).
Represents a homogeneous 2D point.
Definition: vgl_fwd.h:8