Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

vifa_imp_line.txx

Go to the documentation of this file.
00001 // This is gel/vifa/vifa_imp_line.txx
00002 #ifndef _VIFA_IMP_LINE_TXX_
00003 #define _VIFA_IMP_LINE_TXX_
00004 
00005 #include <vcl_cmath.h>  // for vcl_sqrt()
00006 #include <vcl_cassert.h>
00007 #include <vgl/vgl_line_2d.h>
00008 #include <vifa/vifa_imp_line.h>
00009 
00010 
00011 template <class Type> vifa_imp_line<Type>::
00012 vifa_imp_line(vgl_point_2d<Type> const&  p1,
00013               vgl_point_2d<Type> const&  p2)
00014   : vgl_line_segment_2d<Type>(p1, p2)
00015 {
00016   dx_ = p2.x() - p1.x();
00017   dy_ = p2.y() - p1.y();
00018 }
00019 
00020 template <class Type> vifa_imp_line<Type>::
00021 vifa_imp_line(vgl_vector_2d<Type>  d,
00022               vgl_point_2d<Type>  m)
00023 {
00024   dx_ = d.x();
00025   dy_ = d.y();
00026 
00027   vgl_point_2d<Type>  p1(m.x() - (dx_ / 2.0), m.y() - (dy_ / 2.0));
00028   vgl_point_2d<Type>  p2(m.x() + (dx_ / 2.0), m.y() + (dy_ / 2.0));
00029   this->set(p1, p2);
00030 }
00031 
00032 template <class Type> vifa_imp_line<Type>::
00033 vifa_imp_line(Type  a,
00034               Type  b,
00035               Type  c
00036        )
00037 {
00038   // The values of a and b should not both be zero
00039   assert (a || b);
00040 
00041   // Use implicit coefficients to compute two on-axis points
00042   vgl_point_2d<Type>  p1;
00043   vgl_point_2d<Type>  p2;
00044   vgl_line_2d<Type>  l(a, b, c);
00045   l.get_two_points(p1, p2);
00046 
00047   // Use the on-axis points for line segment endpoints
00048   set_points(p1, p2);
00049 }
00050 
00051 template <class Type> double vifa_imp_line<Type>::
00052 get_dir_x(void)
00053 {
00054   vgl_vector_2d<double>  v(dx_, dy_);
00055   double          s = v.length();
00056 
00057   s = (near_zero(s) ? 1.0 : 1.0 / s);
00058 
00059   double  dx = dx_ * s;
00060   return near_zero(dx) ? 0.0 : dx;
00061 }
00062 
00063 template <class Type> double vifa_imp_line<Type>::
00064 length(void)
00065 {
00066   vgl_vector_2d<double>  v = this->point2() - this->point1();
00067   double          s = v.length();
00068 
00069   return s;
00070 }
00071 
00072 template <class Type> double vifa_imp_line<Type>::
00073 get_dir_y(void)
00074 {
00075   vgl_vector_2d<double>  v(dx_, dy_);
00076   double          s = v.length();
00077 
00078   s = (near_zero(s) ? 1.0 : 1.0 / s);
00079   double  dy = dy_ * s;
00080   return near_zero(dy) ? 0.0 : dy;
00081 }
00082 
00083 template <class Type> void vifa_imp_line<Type>::
00084 set_points(vgl_point_2d<Type> const&  p1,
00085            vgl_point_2d<Type> const&  p2)
00086 {
00087   // Call base method to update the endpoints
00088   this->set(p1, p2);
00089 
00090   // Set axis projections of unit direction vector
00091   vgl_vector_2d<double>  d = this->direction();
00092   dx_ = d.x();
00093   dy_ = d.y();
00094 }
00095 
00096 template <class Type> void vifa_imp_line<Type>::
00097 project_2d_pt(const Type& p,
00098               const Type& q,
00099               Type&       x,
00100               Type&       y) const
00101 {
00102   double  a = this->a();
00103   double  b = this->b();
00104   double  c = this->c();
00105   double  a2 = a * a;
00106   double  b2 = b * b;
00107   double  m = vcl_sqrt(a2 + b2);
00108 
00109   a /= m;
00110   b /= m;
00111   c /= m;
00112 
00113   if (b != 0)
00114   {
00115     x = (Type)(- (a * c) + b2 * p + a * b * q);
00116     y = (Type)((- c + a2 * c - a * b2 * p + a2 * b * q) / b);
00117   }
00118   else
00119   {
00120     x = (Type)(- c / a);
00121     y = (Type)(q);
00122   }
00123 }
00124 
00125 template <class Type> vgl_point_2d<Type> vifa_imp_line<Type>::
00126 project_2d_pt(const vgl_point_2d<Type>&  t) const
00127 {
00128   Type  p = t.x();
00129   Type  q = t.y();
00130   Type  x;
00131   Type  y;
00132 
00133   project_2d_pt(p, q, x, y);
00134 
00135   vgl_point_2d<Type>  u(x, y);
00136   return u;
00137 }
00138 
00139 // Find parametric t-value for a given point relative to line segment.
00140 template <class Type> double vifa_imp_line<Type>::
00141 find_t(vgl_point_2d<Type> const&  p)
00142 {
00143   double  dirx = get_dir_x();
00144   double  diry = get_dir_y();
00145   double  s = dx_ * dirx + dy_ * diry;
00146 
00147   if (near_zero(s))
00148   {
00149     return 0.5;
00150   }
00151 
00152   double  x = p.x() - this->point1().x();
00153   double  y = p.y() - this->point1().y();
00154   double  t = (x * dirx + y * diry) / s;
00155   return t;
00156 }
00157 
00158 
00159 // Find point on line (defined by line segment) for a parametric t-value.
00160 template <class Type> vgl_point_2d<Type> vifa_imp_line<Type>::
00161 find_at_t(double  t)
00162 {
00163   Type  x = (Type)(this->point1().x() + dx_ * t);
00164   Type  y = (Type)(this->point1().y() + dy_ * t);
00165 
00166   vgl_point_2d<Type>  p(x, y);
00167   return p;
00168 }
00169 
00170 
00171 #endif  // _VIFA_IMP_LINE_TXX_

Generated on Thu Jan 10 14:47:30 2008 for contrib/gel/vifa by  doxygen 1.4.4