contrib/oxl/mvl/HomgPoint2D.cxx
Go to the documentation of this file.
00001 // This is oxl/mvl/HomgPoint2D.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 
00008 #include "HomgPoint2D.h"
00009 #include <vcl_iostream.h>
00010 #include <vnl/vnl_double_2.h>
00011 #include <vcl_cmath.h> // for sqrt()
00012 
00013 //--------------------------------------------------------------
00014 //
00015 //: Return the non-homogeneous coordinates of the point.
00016 // If the point is at infinity, return false.
00017 bool
00018 HomgPoint2D::get_nonhomogeneous(double& ex, double& ey) const
00019 {
00020   double hx = x();
00021   double hy = y();
00022   double hz = w();
00023 
00024   if (hz == 0)
00025   {
00026     ex = ey = Homg::infinity;
00027     return false;
00028   }
00029 
00030   ex = hx / hz;
00031   ey = hy / hz;
00032   return true;
00033 }
00034 
00035 //-----------------------------------------------------------------------------
00036 //
00037 //: returns a non-homogeneous vnl_vector of length 2.
00038 vnl_double_2 HomgPoint2D::get_double2() const
00039 {
00040   double scale = 1.0 / (*this)[2];
00041   return vnl_double_2((*this)[0] * scale, (*this)[1] * scale);
00042 }
00043 
00044 //-----------------------------------------------------------------------------
00045 //--------------------------------------------------------------
00046 //
00047 //: Rescale point (defaults to w = 1)
00048 // If the point is at infinity, return false
00049 bool
00050 HomgPoint2D::rescale_w(double new_w)
00051 {
00052   double hx = x();
00053   double hy = y();
00054   double hw = w();
00055 
00056   if (hw == 0)
00057     return false;
00058 
00059   hw = new_w/hw;
00060 
00061   set(hx*hw, hy*hw, new_w);
00062   //(*this)[0] = hx*hw;
00063   //(*this)[1] = hy*hw;
00064   //(*this)[2] = new_w;
00065 
00066   return true;
00067 }
00068 
00069 //
00070 //: returns a unit-norm scaled copy of this.
00071 HomgPoint2D HomgPoint2D::get_unitized() const
00072 {
00073   double norm = x()*x() + y()*y() + w()*w();
00074 
00075   if (norm == 0.0) {
00076     vcl_cerr << "HomgPoint2D::get_unitized() -- Zero length vector\n";
00077     return *this;
00078   }
00079 
00080   norm = 1.0/vcl_sqrt(norm);
00081   return HomgPoint2D(x()*norm, y()*norm, w()*norm);
00082 }
00083 
00084 //-----------------------------------------------------------------------------
00085 //
00086 //: Print to vcl_ostream as <HomgPoint2D x y z>
00087 vcl_ostream& operator<<(vcl_ostream& s, const HomgPoint2D& p)
00088 {
00089   return s << "<HomgPoint2D " << p.get_vector() << '>';
00090 }
00091 
00092 //-----------------------------------------------------------------------------
00093 //
00094 //: Read from ASCII vcl_istream.  Assumes points are stored in homogeneous form as 3 reals.
00095 vcl_istream& operator>>(vcl_istream& is, HomgPoint2D& p)
00096 {
00097   double x, y, z;
00098   is >> x >> y >> z;
00099   if (is.good())
00100     p.set(x,y,z);
00101   return is;
00102 }
00103 
00104 //-----------------------------------------------------------------------------
00105 //
00106 //: read from vcl_istream.  Default is to assume that points are nonhomogeneous
00107 // 2D, set is_homogeneous to true if points are already in homogeneous form.
00108 HomgPoint2D HomgPoint2D::read(vcl_istream& is, bool is_homogeneous)
00109 {
00110   if (is_homogeneous) {
00111     HomgPoint2D ret;
00112     is >> ret;
00113     return ret;
00114   }
00115   double x, y;
00116   is >> x >> y;
00117   return HomgPoint2D(x, y, 1.0);
00118 }