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