Go to the documentation of this file.00001 
00002 #ifndef vgl_homg_point_1d_h_
00003 #define vgl_homg_point_1d_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #include <vcl_iosfwd.h>
00018 #include <vcl_cassert.h>
00019 #include <vcl_vector.h>
00020 
00021 
00022 template <class T>
00023 class vgl_homg_point_1d
00024 {
00025   T x_;
00026   T w_;
00027 
00028  public:
00029   
00030   inline vgl_homg_point_1d() : x_(0), w_(T(1)) {}
00031 
00032   
00033   inline vgl_homg_point_1d(T px, T pw = T(1)) : x_(px), w_(pw) {}
00034 
00035   
00036   inline vgl_homg_point_1d(const T v[2]) : x_(v[0]), w_(v[1]) {}
00037 
00038 #if 0 // the compiler defaults are better...
00039   
00040   inline vgl_homg_point_1d(const vgl_homg_point_1d<T>& that) : x_(p.x()), w_(p.w()) {}
00041 
00042   
00043   inline ~vgl_homg_point_1d() {}
00044 
00045   
00046   inline vgl_homg_point_1d<T>& operator=(const vgl_homg_point_1d<T>& p) {
00047     set(p.x(),p.w()); return *this; }
00048 #endif
00049 
00050   
00051   inline bool operator==(vgl_homg_point_1d<T> const& p) const {
00052     return this==&p || x()*p.w() == w()*p.x(); }
00053   inline bool operator!=(vgl_homg_point_1d<T> const& p)const { return !operator==(p); }
00054 
00055   
00056 
00057   inline T x() const { return x_; }
00058   inline T w() const { return w_; }
00059 
00060   
00061   
00062   inline void set(T px, T pw) { x_ = px, w_ = pw; }
00063   inline void set(T const p[2]) { x_ = p[0]; w_ = p[1]; }
00064 
00065   
00066   
00067   inline bool ideal(T tol = T(0)) const {
00068 #define vgl_Abs(x) (x<0?-x:x) // avoid #include of vcl_cmath.h AND vcl_cstdlib.h
00069     return vgl_Abs(w()) <= tol * vgl_Abs(x());
00070 #undef vgl_Abs
00071   }
00072 };
00073 
00074 
00075 
00076 
00077 
00078 template <class T>
00079 vcl_ostream& operator<<(vcl_ostream& s, vgl_homg_point_1d<T> const& p);
00080 
00081 
00082 
00083 template <class T>
00084 vcl_istream& operator>>(vcl_istream& s, vgl_homg_point_1d<T>& p);
00085 
00086 
00087 
00088 
00089 
00090 
00091 template <class T> inline
00092 bool is_ideal(vgl_homg_point_1d<T> const& p, T tol = T(0)) { return p.ideal(tol); }
00093 
00094 
00095 
00096 
00097 template <class T> inline
00098 T operator-(vgl_homg_point_1d<T> const& p1,
00099             vgl_homg_point_1d<T> const& p2) {
00100   assert(p1.w() && p2.w());
00101   return p1.x()/p1.w()-p2.x()/p2.w();
00102 }
00103 
00104 
00105 
00106 
00107 
00108 template <class T> inline
00109 vgl_homg_point_1d<T> operator+(vgl_homg_point_1d<T> const& p, T v)
00110 { return vgl_homg_point_1d<T>(p.x()+v*p.w(), p.w()); }
00111 
00112 
00113 
00114 
00115 template <class T> inline
00116 vgl_homg_point_1d<T>& operator+=(vgl_homg_point_1d<T>& p, T v)
00117 { p.set(p.x()+v*p.w(), p.w()); return p; }
00118 
00119 
00120 
00121 template <class T> inline
00122 vgl_homg_point_1d<T> operator-(vgl_homg_point_1d<T> const& p, T v)
00123 { return p + (-v); }
00124 
00125 
00126 
00127 template <class T> inline
00128 vgl_homg_point_1d<T>& operator-=(vgl_homg_point_1d<T>& p, T v)
00129 { return p += (-v); }
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 template <class T> inline
00149 double cross_ratio(vgl_homg_point_1d<T>const& p1, vgl_homg_point_1d<T>const& p2,
00150                    vgl_homg_point_1d<T>const& p3, vgl_homg_point_1d<T>const& p4)
00151 { return (p1.x()*p3.w()-p3.x()*p1.w())*(p2.x()*p4.w()-p4.x()*p2.w())
00152        /((p1.x()*p4.w()-p4.x()*p1.w())*(p2.x()*p3.w()-p3.x()*p2.w())); }
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 
00162 template <class T> inline
00163 double ratio(vgl_homg_point_1d<T> const& p1,
00164              vgl_homg_point_1d<T> const& p2,
00165              vgl_homg_point_1d<T> const& p3)
00166 { return (p3-p1)/(p2-p1); }
00167 
00168 
00169 
00170 template <class T> inline
00171 bool collinear(vgl_homg_point_1d<T> const&,
00172                vgl_homg_point_1d<T> const&,
00173                vgl_homg_point_1d<T> const&)
00174 { return true; }
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 template <class T> inline
00183 vgl_homg_point_1d<T> midpoint(vgl_homg_point_1d<T> const& p1,
00184                               vgl_homg_point_1d<T> const& p2,
00185                               T f = 0.5)
00186 { return p1 + f*(p2-p1); }
00187 
00188 
00189 
00190 
00191 
00192 template <class T> inline
00193 vgl_homg_point_1d<T> centre(vgl_homg_point_1d<T> const& p1,
00194                             vgl_homg_point_1d<T> const& p2)
00195 {
00196   if (p1 == p2) return p1;
00197   return vgl_homg_point_1d<T> (p1.x()*p2.w() + p2.x()*p1.w(), p1.w()*p2.w()*2);
00198 }
00199 
00200 
00201 
00202 
00203 template <class T> inline
00204 vgl_homg_point_1d<T> centre(vcl_vector<vgl_homg_point_1d<T> > const& v)
00205 {
00206   int n=v.size();
00207   assert(n>0); 
00208   T x = 0;
00209   for (int i=0; i<n; ++i) x+=v[i].x()/v[i].w();
00210   return vgl_homg_point_1d<T>(x,T(n));
00211 }
00212 
00213 #define VGL_HOMG_POINT_1D_INSTANTIATE(T) extern "please include vgl/vgl_homg_point_1d.txx first"
00214 
00215 #endif // vgl_homg_point_1d_h_