vgl_point_2d.hxx
Go to the documentation of this file.
1 // This is core/vgl/vgl_point_2d.hxx
2 #ifndef vgl_point_2d_hxx_
3 #define vgl_point_2d_hxx_
4 //:
5 // \file
6 
7 #include <iostream>
8 #include <iomanip>
9 #include <string>
10 #include "vgl_point_2d.h"
11 #include <vgl/vgl_homg_point_2d.h>
12 #include <vgl/vgl_line_2d.h>
13 #include <vgl/vgl_homg_line_2d.h>
14 
15 #ifdef _MSC_VER
16 # include <vcl_msvc_warnings.h>
17 #endif
18 
19 //: Construct from homogeneous point
20 template <class Type>
22  : x_(p.x()/p.w()), y_(p.y()/p.w()) // could be infinite!
23 {
24 }
25 
26 //: Construct from 2 lines (intersection).
27 template <class Type>
29  vgl_line_2d<Type> const& l2)
30 {
31  vgl_homg_line_2d<Type> h1(l1.a(), l1.b(), l1.c());
32  vgl_homg_line_2d<Type> h2(l2.a(), l2.b(), l2.c());
33  vgl_homg_point_2d<Type> p(h1, h2); // do homogeneous intersection
34  set(p.x()/p.w(), p.y()/p.w()); // could be infinite!
35 }
36 
37 template <class T>
38 double cross_ratio(vgl_point_2d<T>const& p1, vgl_point_2d<T>const& p2,
39  vgl_point_2d<T>const& p3, vgl_point_2d<T>const& p4)
40 {
41  // least squares solution: (Num_x-CR*Den_x)^2 + (Num_y-CR*Den_y)^2 minimal.
42  double Num_x = (p1.x()-p3.x())*(p2.x()-p4.x());
43  double Num_y = (p1.y()-p3.y())*(p2.y()-p4.y());
44  double Den_x = (p1.x()-p4.x())*(p2.x()-p3.x());
45  double Den_y = (p1.y()-p4.y())*(p2.y()-p3.y());
46  if (Den_x == Den_y) return 0.5*(Num_x+Num_y)/Den_x;
47  else return (Den_x*Num_x+Den_y*Num_y)/(Den_x*Den_x+Den_y*Den_y);
48 }
49 
50 //: Write "<vgl_point_2d x,y> " to stream
51 template <class Type>
52 std::ostream& operator<<(std::ostream& s, vgl_point_2d<Type> const& p)
53 {
54  return s << "<vgl_point_2d "<< p.x() << ',' << p.y() << " > ";
55 }
56 
57 //: Read from stream, possibly with formatting
58 // Either just reads two blank-separated numbers,
59 // or reads two comma-separated numbers,
60 // or reads two numbers in parenthesized form "(123, 321)"
61 // or reads form written by <<, "<vgl_point_2d 123, 321 >"
62 template <class Type>
63 std::istream& vgl_point_2d<Type>::read(std::istream& is)
64 {
65  if (! is.good()) return is; // (TODO: should throw an exception)
66  bool paren = false;
67  bool angle = false;
68  Type tx, ty;
69  is >> std::ws; // jump over any leading whitespace
70  char c;
71  c = is.peek();
72  if(c == '<')
73  {
74  // read the <vgl_point_2d string
75  std::string temp;
76  is >> temp;
77  angle = true;
78  }
79 
80  if (is.eof())
81  return is; // nothing to be set because of EOF (TODO: should throw an exception)
82 
83  if (is.peek() == '(')
84  {
85  is.ignore();
86  paren=true;
87  }
88 
89  is >> std::ws >> tx >> std::ws;
90  if (is.eof())
91  return is;
92 
93  if (is.peek() == ',')
94  is.ignore();
95 
96  is >> std::ws >> ty;
97  if (paren)
98  {
99  is >> std::ws;
100  if (is.eof())
101  return is;
102  if (is.peek() == ')')
103  is.ignore();
104  else
105  return is; // closing parenthesis is missing (TODO: throw an exception)
106  }
107 
108  if (angle)
109  {
110  is >> std::ws;
111  if (is.eof())
112  return is;
113  if (is.peek() == '>')
114  is.ignore();
115  else
116  return is; // closing parenthesis is missing (TODO: throw an exception)
117  }
118 
119  set(tx,ty);
120  return is;
121 }
122 
123 //: Read x y from stream
124 template <class Type>
125 std::istream& operator>>(std::istream& is, vgl_point_2d<Type>& p)
126 {
127  return p.read(is);
128 }
129 
130 #undef VGL_POINT_2D_INSTANTIATE
131 #define VGL_POINT_2D_INSTANTIATE(T) \
132 template class vgl_point_2d<T >; \
133 template double cross_ratio(vgl_point_2d<T >const&, vgl_point_2d<T >const&, \
134  vgl_point_2d<T >const&, vgl_point_2d<T >const&); \
135 template std::ostream& operator<<(std::ostream&, const vgl_point_2d<T >&); \
136 template std::istream& operator>>(std::istream&, vgl_point_2d<T >&)
137 
138 #endif // vgl_point_2d_hxx_
a point in 2D nonhomogeneous space
Represents a homogeneous 2D line.
Definition: vgl_fwd.h:14
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.
vgl_point_2d()=default
Default constructor.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
line in projective 2D space
Represents a Euclidean 2D line.
Definition: vgl_fwd.h:16
Type & y()
Definition: vgl_point_2d.h:72
Type a() const
Parameter a of line a*x + b*y + c = 0.
Definition: vgl_line_2d.h:97
point in projective 2D space
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
Type b() const
Parameter b of line a*x + b*y + c = 0.
Definition: vgl_line_2d.h:99
double angle(v const &a, v const &b)
smallest angle between two vectors (in radians, between 0 and Pi).
Type c() const
Parameter c of line a*x + b*y + c = 0.
Definition: vgl_line_2d.h:101
std::istream & read(std::istream &is)
Read from stream, possibly with formatting.
Type & x()
Definition: vgl_point_2d.h:71