vgl_homg_operators_1d.hxx
Go to the documentation of this file.
1 // This is core/vgl/algo/vgl_homg_operators_1d.hxx
2 #ifndef vgl_homg_operators_1d_hxx_
3 #define vgl_homg_operators_1d_hxx_
4 
5 #include <cmath>
6 #include <iostream>
8 #include <vnl/vnl_vector_fixed.h>
9 #include <vnl/vnl_matrix_fixed.h>
10 #include <vgl/vgl_homg.h> // for infinity
11 #ifdef _MSC_VER
12 # include <vcl_msvc_warnings.h>
13 #endif
14 
15 template <class T>
17 {
18  return vnl_vector_fixed<T,2>(p.x(),p.w());
19 }
20 
21 template <class T>
23  const vgl_homg_point_1d<T>& b,
24  const vgl_homg_point_1d<T>& c,
25  const vgl_homg_point_1d<T>& d)
26 {
27  T x1 = a.x(), w1 = a.w();
28  T x2 = b.x(), w2 = b.w();
29  T x3 = c.x(), w3 = c.w();
30  T x4 = d.x(), w4 = d.w();
31  T n = (x1*w3-x3*w1)*(x2*w4-x4*w2);
32  T m = (x1*w4-x4*w1)*(x2*w3-x3*w2);
33  if (n == 0 && m == 0)
34  std::cerr << "cross_ratio not defined: three of the given points coincide\n";
35  return n/m;
36 }
37 
38 template <class T>
40 {
41  T x1 = a.x(), w1 = a.w();
42  T x2 = b.x(), w2 = b.w();
43  return x1*w2-w1*x2;
44 }
45 
46 template <class T>
48 {
49  T x1 = a.x(), w1 = a.w();
50  T x2 = b.x(), w2 = b.w();
51  return x1*x2 + w1*w2;
52 }
53 
54 template <class T>
56 {
57  double norm = std::sqrt (a.x()*a.x() + a.w()*a.w());
58  if (norm == 0.0) {
59  std::cerr << "vgl_homg_operators_1d<T>::unitize() -- Zero length vector\n";
60  return;
61  }
62  norm = 1.0/norm;
63  a.set(T(a.x()*norm), T(a.w()*norm));
64 }
65 
66 template <class T>
68  const vgl_homg_point_1d<T>& b)
69 {
70  T x1 = a.x(), w1 = a.w();
71  T x2 = b.x(), w2 = b.w();
72  if (w1 == 0 || w2 == 0) {
73  std::cerr << "vgl_homg_operators_1d<T>::distance() -- point at infinity";
74  return vgl_homg<T>::infinity;
75  }
76  x1 /= w1; x2 /= w2;
77  return (x1 > x2) ? x1-x2 : x2-x1;
78 }
79 
80 template <class T>
82  const vgl_homg_point_1d<T>& point2)
83 {
84  T d = distance(point1,point2);
85  return d*d;
86 }
87 
88 template <class T>
90  const vgl_homg_point_1d<T>& b)
91 {
92  T x1 = a.x(), w1 = a.w();
93  T x2 = b.x(), w2 = b.w();
94  return vgl_homg_point_1d<T>(x1*w2+x2*w1, 2*w1*w2);
95 }
96 
97 template <class T>
98 T vgl_homg_operators_1d<T>::conjugate(T x1, T x2, T x3, double cr)
99 // Default for cr is -1.
100 {
101  T a = x1 - x3; T b = x2 - x3; T c = T(a-cr*b);
102  if (c == 0) return (x2*a == cr*x1*b) ? 1 : vgl_homg<T>::infinity;
103  return T((x2*a-cr*x1*b)/c);
104 }
105 
106 template <class T>
108  const vgl_homg_point_1d<T>& b,
109  const vgl_homg_point_1d<T>& c,
110  double cr)
111 // Default for cr is -1.
112 {
113  T x1 = a.x(), w1 = a.w();
114  T x2 = b.x(), w2 = b.w();
115  T x3 = c.x(), w3 = c.w();
116  T k = x1*w3 - x3*w1, m = x2*w3 - x3*w2;
117  return vgl_homg_point_1d<T>(T(x2*k-cr*x1*m), T(k*w2-cr*m*w1));
118  // could be (0,0) !! not checked.
119 }
120 
121 template <class T>
122 vgl_homg_point_1d<T> operator*(vnl_matrix_fixed<T,2,2> const& m,
123  vgl_homg_point_1d<T> const& p)
124 {
125  return vgl_homg_point_1d<T>(m(0,0)*p.x()+m(0,1)*p.w(),
126  m(1,0)*p.x()+m(1,1)*p.w());
127 }
128 
129 #undef VGL_HOMG_OPERATORS_1D_INSTANTIATE
130 #define VGL_HOMG_OPERATORS_1D_INSTANTIATE(T) \
131 template class vgl_homg_operators_1d<T >; \
132 template vgl_homg_point_1d<T > operator*(vnl_matrix_fixed<T,2,2> const& m, vgl_homg_point_1d<T > const& p)
133 
134 #endif // vgl_homg_operators_1d_hxx_
static void unitize(vgl_homg_point_1d< T > &a)
Normalize vgl_homg_point_1d<T> to unit magnitude.
static T cross(const vgl_homg_point_1d< T > &a, const vgl_homg_point_1d< T > &b)
Cross product of two homogeneous points.
static T conjugate(T x1, T x2, T x3, double cr=-1)
Calculate the projective conjugate point of three given points.
static double cross_ratio(const vgl_homg_point_1d< T > &a, const vgl_homg_point_1d< T > &b, const vgl_homg_point_1d< T > &c, const vgl_homg_point_1d< T > &d)
cross ratio of four 1D points.
1D homogeneous functions
void set(T px, T pw)
Set x,w.
General purpose support class for vgl_homg_ classes.
static vnl_vector_fixed< T, 2 > get_vector(vgl_homg_point_1d< T > const &p)
get a vnl_vector_fixed representation of a homogeneous object.
static T dot(const vgl_homg_point_1d< T > &a, const vgl_homg_point_1d< T > &b)
Dot product of two homogeneous points.
static T distance_squared(const vgl_homg_point_1d< T > &point1, const vgl_homg_point_1d< T > &point2)
Get the square of the distance between the two points.
vgl_homg_point_1d< T > operator *(vnl_matrix_fixed< T, 2, 2 > const &m, vgl_homg_point_1d< T > const &p)
Transform a point through a 2x2 projective transformation matrix.
General purpose support class for vgl_homg_ classes.
Definition: vgl_fwd.h:6
Represents a homogeneous 1-D point, i.e., a homogeneous pair (x,w).
Definition: vgl_fwd.h:7
static T distance(const vgl_homg_point_1d< T > &point1, const vgl_homg_point_1d< T > &point2)
Get the distance between the two points.
static vgl_homg_point_1d< T > midpoint(const vgl_homg_point_1d< T > &p1, const vgl_homg_point_1d< T > &p2)
Return the midpoint of two homogeneous points.