contrib/gel/vtol/vtol_vertex.cxx
Go to the documentation of this file.
00001 // This is gel/vtol/vtol_vertex.cxx
00002 #include "vtol_vertex.h"
00003 //:
00004 // \file
00005 
00006 #include <vtol/vtol_zero_chain.h>
00007 #include <vtol/vtol_edge.h>
00008 #include <vtol/vtol_face.h>
00009 #include <vtol/vtol_macros.h>
00010 #include <vtol/vtol_list_functions.h>
00011 #include <vcl_algorithm.h> // for vcl_find()
00012 #include <vcl_cassert.h>
00013 
00014 //***************************************************************************
00015 // Initialization
00016 //***************************************************************************
00017 
00018 //---------------------------------------------------------------------------
00019 // Destructor
00020 //---------------------------------------------------------------------------
00021 vtol_vertex::~vtol_vertex()
00022 {
00023   unlink_all_inferiors();
00024 }
00025 
00026 //******************************************************
00027 //*
00028 //*    Accessor Functions
00029 //*
00030 
00031 //: Returns a list of Vertices which only contains a pointer to itself.
00032 vcl_vector<vtol_vertex*> *vtol_vertex::compute_vertices()
00033 {
00034   LIST_SELF(vtol_vertex);
00035 }
00036 
00037 //: Returns a list of ZeroChains that contain the vertex. This is the vertex superiors list.
00038 vcl_vector<vtol_zero_chain*>* vtol_vertex::compute_zero_chains()
00039 {
00040   SEL_SUP(vtol_zero_chain,compute_zero_chains);
00041 }
00042 
00043 
00044 //: Returns a list of Edges which contain the vertex.
00045  vcl_vector<vtol_edge*>* vtol_vertex::compute_edges()
00046 {
00047   SEL_SUP(vtol_edge,compute_edges);
00048 }
00049 
00050 //: Returns a list of OneChains which contain the vertex.
00051 vcl_vector<vtol_one_chain*>* vtol_vertex::compute_one_chains()
00052 {
00053   SEL_SUP(vtol_one_chain,compute_one_chains);
00054 }
00055 
00056 //: Returns a list of Faces which contain the vertex.
00057  vcl_vector<vtol_face*>* vtol_vertex::compute_faces()
00058 {
00059   SEL_SUP(vtol_face,compute_faces);
00060 }
00061 
00062 //: Returns a list of TwoChains which contain the vertex.
00063   vcl_vector<vtol_two_chain*>* vtol_vertex::compute_two_chains()
00064 {
00065   SEL_SUP(vtol_two_chain,compute_two_chains);
00066 }
00067 
00068 //: Returns a list of Blocks which contain the vertex.
00069 vcl_vector<vtol_block*>* vtol_vertex::compute_blocks()
00070 {
00071   SEL_SUP(vtol_block,compute_blocks);
00072 }
00073 
00074 //******************************************************
00075 //*
00076 //*    Print Functions
00077 //*
00078 
00079 //: This method outputs a simple text representation of the vertex including its address in memory.
00080 void vtol_vertex::print(vcl_ostream &strm) const
00081 {
00082   strm<< "<vtol_vertex " << (void const *)this<<"> with id "
00083       << get_id() << vcl_endl;
00084 }
00085 
00086 
00087 //: This method outputs a detailed description of the vertex including the inferiors and superiors.
00088 void vtol_vertex::describe(vcl_ostream &strm,
00089                            int blanking) const
00090 {
00091   for (int i=0; i<blanking; ++i) strm << ' ';
00092   print(strm);
00093   describe_inferiors(strm, blanking);
00094   describe_superiors(strm, blanking);
00095 }
00096 
00097 //******************************************************
00098 //*
00099 //*    Implementor Functions
00100 //*
00101 
00102 //-----------------------------------------------------------------------------
00103 //: Is `this' connected with `v2' ?
00104 //    ie has a superior of `this' `v2' as inferior ?
00105 //-----------------------------------------------------------------------------
00106 bool vtol_vertex::is_connected(vtol_vertex_sptr const& v2) const
00107 {
00108   edge_list vertedges; edges(vertedges);
00109   for (edge_list::const_iterator i=vertedges.begin(); i!=vertedges.end(); ++i)
00110     if ((*i)->is_endpoint(v2))
00111       return true;
00112   return false;
00113 }
00114 
00115 
00116 //: This method returns true if edg is on the superior list of the vertex.
00117 bool vtol_vertex::is_endpoint(vtol_edge_sptr const& e) const
00118 {
00119   edge_list e_list; this->edges(e_list);
00120   return vcl_find(e_list.begin(),e_list.end(),e)!=e_list.end();
00121 }
00122 
00123 //---------------------------------------------------------------------------
00124 //: Assignment of `this' with `other' (copy the point not the links)
00125 //---------------------------------------------------------------------------
00126 vtol_vertex &vtol_vertex::operator=(const vtol_vertex &other)
00127 {
00128   if (this!=&other)
00129   {
00130     this->copy_geometry(other);
00131     // point_->set_x(other.point_->x());
00132     // point_->set_y(other.point_->y());
00133 
00134     touch();
00135   }
00136   return *this;
00137 }
00138 
00139 //: spatial object equality
00140 
00141 bool vtol_vertex::operator==(const vsol_spatial_object_2d& obj) const
00142 {
00143   return
00144    obj.cast_to_topology_object() &&
00145    obj.cast_to_topology_object()->cast_to_vertex() &&
00146    *this == *obj.cast_to_topology_object()->cast_to_vertex();
00147 }
00148 
00149 
00150 //---------------------------------------------------------------------------
00151 //: Is `this' has the same coordinates for its point than `other' ?
00152 //---------------------------------------------------------------------------
00153 bool vtol_vertex::operator== (const vtol_vertex &other) const
00154 {
00155   return this==&other || compare_geometry(other);
00156 }
00157 
00158 // ******************************************************
00159 //
00160 //    Functions
00161 //
00162 
00163 //
00164 //-----------------------------------------------------------------------------
00165 //:
00166 // Determine which other vertices share edges with this. Add any of these which
00167 // are not in the list to it, and recursively call explore_vertex on them. The
00168 // method is intended to recover all of the vertices in a single topological
00169 // structure which is composed of connected edges.
00170 //
00171 void vtol_vertex::explore_vertex(vertex_list &verts)
00172 {
00173   // Note that "this" is not first put on the list:
00174   // it will be put as the second element, during the first recursive call.
00175 
00176   edge_list e_list; this->edges(e_list);
00177   for (edge_list::iterator i=e_list.begin();i!=e_list.end();++i)
00178   {
00179     vtol_edge_sptr e=*i;
00180     vtol_vertex_sptr vv;
00181     if (e->v1()==this)
00182       vv=e->v2();
00183     else if (e->v2()==this)
00184       vv=e->v1();
00185     else
00186     {
00187       vcl_cerr << "Explore vtol_vertex: shouldn't get this\n";
00188       assert(false);
00189       continue;
00190     }
00191 
00192     if (vcl_find(verts.begin(),verts.end(),vv)==verts.end())
00193     {
00194       verts.push_back(vv);
00195       vv->explore_vertex(verts);
00196     }
00197   }
00198 }