contrib/gel/vsol/vsol_point_2d.cxx
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_point_2d.cxx
00002 #include "vsol_point_2d.h"
00003 //:
00004 // \file
00005 #include <vgl/vgl_distance.h>
00006 
00007 //***************************************************************************
00008 // Initialization
00009 //***************************************************************************
00010 
00011 //---------------------------------------------------------------------------
00012 // Destructor
00013 //---------------------------------------------------------------------------
00014 vsol_point_2d::~vsol_point_2d()
00015 {
00016 }
00017 
00018 //---------------------------------------------------------------------------
00019 //: Clone `this': creation of a new object and initialization
00020 // See Prototype pattern
00021 //---------------------------------------------------------------------------
00022 vsol_spatial_object_2d* vsol_point_2d::clone(void) const
00023 {
00024   return new vsol_point_2d(*this);
00025 }
00026 
00027 //***************************************************************************
00028 // Comparison
00029 //***************************************************************************
00030 
00031 //---------------------------------------------------------------------------
00032 //: Has `this' the same coordinates than `other' ?
00033 //---------------------------------------------------------------------------
00034 bool vsol_point_2d::operator==(const vsol_point_2d &other) const
00035 {
00036   return this==&other || p_==other.p_;
00037 }
00038 
00039 //: spatial object equality
00040 
00041 bool vsol_point_2d::operator==(const vsol_spatial_object_2d& obj) const
00042 {
00043   return obj.cast_to_point() && *this == *obj.cast_to_point();
00044 }
00045 
00046 //***************************************************************************
00047 // Status report
00048 //***************************************************************************
00049 
00050 //---------------------------------------------------------------------------
00051 //: Return the real type of a point. It is a POINT
00052 //---------------------------------------------------------------------------
00053 vsol_spatial_object_2d::vsol_spatial_object_2d_type
00054 vsol_point_2d::spatial_type(void) const
00055 {
00056   return POINT;
00057 }
00058 
00059 //---------------------------------------------------------------------------
00060 //: Compute the bounding box of `this'
00061 //---------------------------------------------------------------------------
00062 void vsol_point_2d::compute_bounding_box(void) const
00063 {
00064   set_bounding_box(p_.x(),p_.y());
00065 }
00066 
00067 //***************************************************************************
00068 // Status setting
00069 //***************************************************************************
00070 
00071 //---------------------------------------------------------------------------
00072 //: Set the abscissa
00073 //---------------------------------------------------------------------------
00074 void vsol_point_2d::set_x(const double new_x)
00075 {
00076   p_.set(new_x, p_.y());
00077 }
00078 
00079 //---------------------------------------------------------------------------
00080 //: Set the ordinate
00081 //---------------------------------------------------------------------------
00082 void vsol_point_2d::set_y(const double new_y)
00083 {
00084   p_.set(p_.x(), new_y);
00085 }
00086 
00087 //***************************************************************************
00088 // Basic operations
00089 //***************************************************************************
00090 
00091 //---------------------------------------------------------------------------
00092 //: Return the distance (N2) between `this' and `other'
00093 //---------------------------------------------------------------------------
00094 double vsol_point_2d::distance(const vsol_point_2d &other) const
00095 {
00096   return vgl_distance(p_,other.p_);
00097 }
00098 
00099 double vsol_point_2d::distance(vsol_point_2d_sptr other) const
00100 {
00101   return vgl_distance(p_,other->p_);
00102 }
00103 
00104 //---------------------------------------------------------------------------
00105 //: Return the middle point between `this' and `other'
00106 //---------------------------------------------------------------------------
00107 vsol_point_2d_sptr vsol_point_2d::middle(const vsol_point_2d &other) const
00108 {
00109   return new vsol_point_2d(midpoint(p_,other.p_));
00110 }
00111 
00112 //---------------------------------------------------------------------------
00113 //: Add `v' to `this'
00114 //---------------------------------------------------------------------------
00115 void vsol_point_2d::add_vector(vgl_vector_2d<double> const& v)
00116 {
00117   p_ += v;
00118 }
00119 
00120 //---------------------------------------------------------------------------
00121 //: Add `v' and `this'
00122 //---------------------------------------------------------------------------
00123 vsol_point_2d_sptr
00124 vsol_point_2d::plus_vector(vgl_vector_2d<double> const& v) const
00125 {
00126   return new vsol_point_2d(p_ + v);
00127 }
00128 
00129 //---------------------------------------------------------------------------
00130 //: Return the vector `this',`other'.
00131 //---------------------------------------------------------------------------
00132 vgl_vector_2d<double>
00133 vsol_point_2d::to_vector(const vsol_point_2d &other) const
00134 {
00135   return vgl_vector_2d<double>(other.x() - x(), other.y() - y());
00136 }
00137 
00138 //----------------------------------------------------------------
00139 // ================   Binary I/O Methods ========================
00140 //----------------------------------------------------------------
00141 
00142 //: Binary save self to stream.
00143 void vsol_point_2d::b_write(vsl_b_ostream &os) const
00144 {
00145   vsl_b_write(os, version());
00146   vsol_spatial_object_2d::b_write(os);
00147   vsl_b_write(os, p_.x());
00148   vsl_b_write(os, p_.y());
00149 }
00150 
00151 //: Binary load self from stream (not typically used)
00152 void vsol_point_2d::b_read(vsl_b_istream &is)
00153 {
00154   if (!is)
00155     return;
00156   short ver;
00157   vsl_b_read(is, ver);
00158   switch (ver)
00159   {
00160    case 1:
00161     vsol_spatial_object_2d::b_read(is);
00162     { double x=0, y=0;
00163       vsl_b_read(is, x);
00164       vsl_b_read(is, y);
00165       this->p_.set(x, y);
00166     }
00167     break;
00168 
00169    default:
00170     vcl_cerr << "I/O ERROR: vsol_point_2d::b_read(vsl_b_istream&)\n"
00171              << "           Unknown version number "<< ver << '\n';
00172     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00173     return;
00174   }
00175 }
00176 
00177 //: Return IO version number;
00178 short vsol_point_2d::version() const
00179 {
00180   return 1;
00181 }
00182 
00183 //: Print an ascii summary to the stream
00184 void vsol_point_2d::print_summary(vcl_ostream &os) const
00185 {
00186   os << *this;
00187 }
00188 
00189 //: Binary save vsol_point_2d to stream.
00190 void
00191 vsl_b_write(vsl_b_ostream &os, const vsol_point_2d* p)
00192 {
00193   if (p==0) {
00194     vsl_b_write(os, false); // Indicate null pointer stored
00195   }
00196   else{
00197     vsl_b_write(os,true); // Indicate non-null pointer stored
00198     p->b_write(os);
00199   }
00200 }
00201 
00202 
00203 //: Binary load vsol_point_2d from stream.
00204 void
00205 vsl_b_read(vsl_b_istream &is, vsol_point_2d* &p)
00206 {
00207   delete p;
00208   bool not_null_ptr;
00209   vsl_b_read(is, not_null_ptr);
00210   if (not_null_ptr) {
00211     p = new vsol_point_2d();
00212     p->b_read(is);
00213   }
00214   else
00215     p = 0;
00216 }