contrib/gel/vtol/vtol_vertex_2d.cxx
Go to the documentation of this file.
00001 // This is gel/vtol/vtol_vertex_2d.cxx
00002 #include "vtol_vertex_2d.h"
00003 //:
00004 // \file
00005 
00006 #include <vsol/vsol_point_2d.h>
00007 #include <vtol/vtol_edge_2d.h>
00008 #include <vtol/vtol_edge.h>
00009 #include <vcl_cassert.h>
00010 
00011 //***************************************************************************
00012 // Initialization
00013 //***************************************************************************
00014 
00015 //---------------------------------------------------------------------------
00016 // Default constructor
00017 //---------------------------------------------------------------------------
00018 vtol_vertex_2d::vtol_vertex_2d()
00019 {
00020   point_=new vsol_point_2d(0,0);
00021 }
00022 
00023 //---------------------------------------------------------------------------
00024 //: Constructor from a point (the point is copied)
00025 //---------------------------------------------------------------------------
00026 vtol_vertex_2d::vtol_vertex_2d(vsol_point_2d &new_point)
00027 {
00028   // Must allocate here, since this pointer will be unref()ed by destructor
00029   point_=new vsol_point_2d(new_point);
00030 }
00031 
00032 //---------------------------------------------------------------------------
00033 //: Constructor from a vector
00034 //---------------------------------------------------------------------------
00035 vtol_vertex_2d::vtol_vertex_2d(const vnl_double_2 &v)
00036 {
00037   point_=new vsol_point_2d(v[0],v[1]);
00038 }
00039 
00040 //---------------------------------------------------------------------------
00041 //: Constructor from abscissa `new_x' and ordinate `new_y' of the point
00042 //---------------------------------------------------------------------------
00043 vtol_vertex_2d::vtol_vertex_2d(const double new_x,
00044                                const double new_y)
00045 {
00046   point_=new vsol_point_2d(new_x,new_y);
00047 }
00048 
00049 //---------------------------------------------------------------------------
00050 //: Pseudo copy constructor.  Deep copy.
00051 //---------------------------------------------------------------------------
00052 vtol_vertex_2d::vtol_vertex_2d(vtol_vertex_2d_sptr const& other)
00053 : point_(new vsol_point_2d(*other->point_))
00054 {
00055 }
00056 
00057 //---------------------------------------------------------------------------
00058 //: Clone `this': creation of a new object and initialization
00059 // See Prototype pattern
00060 //---------------------------------------------------------------------------
00061 vsol_spatial_object_2d* vtol_vertex_2d::clone() const
00062 {
00063   return new vtol_vertex_2d(vtol_vertex_2d_sptr(const_cast<vtol_vertex_2d*>(this)));
00064 }
00065 
00066 //*****************************************************
00067 //
00068 //    Accessor Functions
00069 
00070 //---------------------------------------------------------------------------
00071 //: Return the point
00072 //---------------------------------------------------------------------------
00073 vsol_point_2d_sptr vtol_vertex_2d::point() const
00074 {
00075   return point_;
00076 }
00077 
00078 //---------------------------------------------------------------------------
00079 //: Set the point (the point is not copied)
00080 // Require: new_point!=0
00081 //---------------------------------------------------------------------------
00082 void vtol_vertex_2d::set_point(vsol_point_2d_sptr const& new_point)
00083 {
00084   point_=new_point;
00085 }
00086 
00087 //---------------------------------------------------------------------------
00088 //: Return the abscissa of the point
00089 //---------------------------------------------------------------------------
00090 double vtol_vertex_2d::x() const
00091 {
00092   return point_->x();
00093 }
00094 
00095 //---------------------------------------------------------------------------
00096 //: Return the ordinate of the point
00097 //---------------------------------------------------------------------------
00098 double vtol_vertex_2d::y() const
00099 {
00100   return point_->y();
00101 }
00102 
00103 //---------------------------------------------------------------------------
00104 //: Set the abscissa of the point with `new_x'
00105 //---------------------------------------------------------------------------
00106 void vtol_vertex_2d::set_x(const double new_x)
00107 {
00108   // Must allocate here, since this pointer will be unref()ed by destructor
00109 #if 0
00110   point_=new vsol_point_2d(new_x,point_->y());
00111 #endif
00112   point_->set_x(new_x);
00113   this->touch(); //Timestamp update
00114 }
00115 
00116 //---------------------------------------------------------------------------
00117 //: Set the ordinate of the point with `new_y'
00118 //---------------------------------------------------------------------------
00119 void vtol_vertex_2d::set_y(const double new_y)
00120 {
00121   // Must allocate here, since this pointer will be unref()ed by destructor
00122 #if 0
00123   point_=new vsol_point_2d(point_->x(),new_y);
00124 #endif
00125   point_->set_y(new_y);
00126   this->touch(); //Timestamp update
00127 }
00128 
00129 //*****************************************************
00130 //
00131 //    Print Functions
00132 
00133 //: This method outputs a simple text representation of the vertex including its address in memory.
00134 void vtol_vertex_2d::print(vcl_ostream &strm) const
00135 {
00136   strm<<"<vtol_vertex_2d "<<x()<<','<<y()<<','<<(void const *)this<<"> with id "
00137       <<get_id()<<vcl_endl;
00138 }
00139 
00140 
00141 //: This method outputs a detailed description of the vertex including the inferiors and superiors.
00142 void vtol_vertex_2d::describe(vcl_ostream &strm,
00143                               int blanking) const
00144 {
00145   for (int i=0; i<blanking; ++i)
00146     strm << ' ';
00147   print(strm);
00148   describe_inferiors(strm, blanking);
00149   describe_superiors(strm, blanking);
00150 }
00151 
00152 
00153 //*****************************************************
00154 //
00155 //    Implementor Functions
00156 
00157 //-----------------------------------------------------------------------------
00158 //: Create a line edge from `this' and `other' only if this edge does not exist.
00159 //  Otherwise it just returns the existing edge.
00160 // Require: *other!=*this
00161 //-----------------------------------------------------------------------------
00162 vtol_edge_sptr vtol_vertex_2d::new_edge(vtol_vertex_2d_sptr const& other)
00163 {
00164   // require
00165   assert(other);
00166 #if 0 //temporarily disabled
00167   assert(*other != *this);
00168 #endif // 0
00169 
00170   // awf: load vrml speed up by factor of 2 using this loop.
00171   vtol_edge_sptr result = 0;
00172 
00173   // Scan Zero Chains
00174   bool found = false;
00175   vtol_vertex_sptr v = other->cast_to_vertex();
00176   vcl_list<vtol_topology_object*>::const_iterator zp;
00177   for (zp=superiors_.begin();zp!=superiors_.end()&&!found;++zp)
00178   {
00179     // Scan superiors of ZChain (i.e. edges)
00180     const vcl_list<vtol_topology_object*> *sups=(*zp)->superiors_list();
00181     vcl_list<vtol_topology_object*>::const_iterator ep;
00182     for (ep=sups->begin();ep!=sups->end()&&!found;++ep)
00183     {
00184       vtol_edge_sptr e=(*ep)->cast_to_edge();
00185       if (e->v1()==v||e->v2()==v)
00186         { result=e; found = true; }
00187     }
00188   }
00189   if (!result)
00190     result= new vtol_edge_2d(this,other);
00191 
00192   return result;
00193 }
00194 
00195 vtol_edge_sptr vtol_vertex_2d::new_edge(vtol_vertex_sptr const& other)
00196 {
00197   return new_edge(vtol_vertex_2d_sptr(other->cast_to_vertex_2d()));
00198 }
00199 
00200 //: Returns the squared distance from the vertex and the vector location, v.
00201 double vtol_vertex_2d::distance_from(const vnl_double_2 &v)
00202 {
00203   vsol_point_2d point(v(0),v(1));
00204   return point_->distance(point);
00205 }
00206 
00207 //: This method returns the distance, not the squared distance, from this vertex and another vertex.
00208 double vtol_vertex_2d::euclidean_distance(vtol_vertex_2d& v)
00209 {
00210   return point_->distance(*v.point());
00211 }
00212 
00213 
00214 //---------------------------------------------------------------------------
00215 //: Assignment of `this' with `other' (copy the point not the links)
00216 //---------------------------------------------------------------------------
00217 vtol_vertex_2d &vtol_vertex_2d::operator=(const vtol_vertex_2d &other)
00218 {
00219   if (this!=&other)
00220   {
00221     this->touch(); //Timestamp update
00222     // Must allocate here, since this pointer will be unref()ed by destructor
00223     point_=new vsol_point_2d(*(other.point_));
00224   }
00225   return *this;
00226 }
00227 
00228 vtol_vertex_2d& vtol_vertex_2d::operator=(const vtol_vertex &other)
00229 {
00230   if (this!=&other)
00231   {
00232     this->touch(); //Timestamp update
00233     // Must allocate here, since this pointer will be unref()ed by destructor
00234     point_=new vsol_point_2d(*(other.cast_to_vertex_2d()->point_));
00235   }
00236   return *this;
00237 }
00238 
00239 //---------------------------------------------------------------------------
00240 //: spatial object equality
00241 //---------------------------------------------------------------------------
00242 
00243 bool vtol_vertex_2d::operator==(const vsol_spatial_object_2d& obj) const
00244 {
00245   return
00246    obj.cast_to_topology_object() &&
00247    obj.cast_to_topology_object()->cast_to_vertex() &&
00248    *this == *obj.cast_to_topology_object()->cast_to_vertex();
00249 }
00250 
00251 //---------------------------------------------------------------------------
00252 //: Does `this' have the same coordinates for its point than `other' ?
00253 //---------------------------------------------------------------------------
00254 bool vtol_vertex_2d::operator== (const vtol_vertex &other) const
00255 {
00256   return other.cast_to_vertex_2d() && other.cast_to_vertex_2d()->operator==(*this);
00257 }
00258 
00259 //---------------------------------------------------------------------------
00260 //: Does `this' have the same coordinates for its point than `other' ?
00261 //---------------------------------------------------------------------------
00262 bool vtol_vertex_2d::operator== (const vtol_vertex_2d &other) const
00263 {
00264   return this==&other || *point_==*(other.point_);
00265 }
00266 
00267 //*****************************************************
00268 //
00269 //    Functions
00270 
00271 //: copy the geometry
00272 
00273 void vtol_vertex_2d::copy_geometry(const vtol_vertex &other)
00274 {
00275   if (other.cast_to_vertex_2d()){
00276     point_ = new vsol_point_2d(*(other.cast_to_vertex_2d()->point()));
00277   }
00278 }
00279 
00280 //---------------------------------------------------------------------------
00281 //: compare the geometry
00282 //---------------------------------------------------------------------------
00283 
00284 bool vtol_vertex_2d::compare_geometry(const vtol_vertex &other) const
00285 {
00286   return other.cast_to_vertex_2d() && (*point_)==(*(other.cast_to_vertex_2d()->point()));
00287 }
00288 
00289 void vtol_vertex_2d::compute_bounding_box() const
00290 {
00291   set_bounding_box(this->x(), this->y());
00292 }