contrib/gel/vtol/vtol_edge_2d.cxx
Go to the documentation of this file.
00001 // This is gel/vtol/vtol_edge_2d.cxx
00002 #include "vtol_edge_2d.h"
00003 //:
00004 // \file
00005 
00006 #include <vtol/vtol_zero_chain.h>
00007 #include <vsol/vsol_curve_2d.h>
00008 #include <vsol/vsol_line_2d.h>
00009 #include <vdgl/vdgl_digital_curve.h>
00010 #include <vcl_cassert.h>
00011 
00012 //***************************************************************************
00013 // Initialization
00014 //***************************************************************************
00015 
00016 //---------------------------------------------------------------------------
00017 //: Constructor from the two endpoints `new_v1', `new_v2' and from a curve `new_curve'.
00018 //  If `new_curve' is 0, a line is created from `new_v1' and `new_v2'.
00019 //---------------------------------------------------------------------------
00020 vtol_edge_2d::vtol_edge_2d(vtol_vertex_2d_sptr const& new_v1,
00021                            vtol_vertex_2d_sptr const& new_v2,
00022                            vsol_curve_2d_sptr const& new_curve)
00023 {
00024   assert(new_v1!=0); v1_=new_v1->cast_to_vertex();
00025   assert(new_v2!=0); v2_=new_v2->cast_to_vertex();
00026   if (!new_curve)
00027     curve_=new vsol_line_2d(new_v1->point(),new_v2->point());
00028   else
00029     curve_=new_curve;
00030 
00031   link_inferior(new vtol_zero_chain(v1_,v2_));
00032 }
00033 
00034 vtol_edge_2d::vtol_edge_2d(vtol_vertex_sptr const& new_v1,
00035                            vtol_vertex_sptr const& new_v2,
00036                            vsol_curve_2d_sptr const& new_curve)
00037 {
00038   assert(new_v1->cast_to_vertex_2d()); v1_=new_v1;
00039   assert(new_v2->cast_to_vertex_2d()); v2_=new_v2;
00040   if (!new_curve)
00041     curve_=new vsol_line_2d(v1_->cast_to_vertex_2d()->point(),
00042                             v2_->cast_to_vertex_2d()->point());
00043   else
00044     curve_=new_curve;
00045 
00046   link_inferior(new vtol_zero_chain(v1_,v2_));
00047 }
00048 
00049 //---------------------------------------------------------------------------
00050 //: Pseudo copy constructor. Deep copy.
00051 //---------------------------------------------------------------------------
00052 vtol_edge_2d::vtol_edge_2d(vtol_edge_2d_sptr const& other)
00053   : curve_(0)
00054 {
00055   topology_list::const_iterator i;
00056   for (i=other->inferiors()->begin();i!=other->inferiors()->end();++i)
00057   {
00058     link_inferior((*i)->clone()->cast_to_topology_object()->cast_to_zero_chain());
00059   }
00060   set_vertices_from_zero_chains();
00061 
00062   if (other->curve())
00063   {
00064     curve_ = other->curve()->clone()->cast_to_curve();
00065     // make sure the geometry and Topology are in sync
00066     if (v1_)
00067     {
00068       if (v1_->cast_to_vertex_2d())
00069       {
00070         curve_->set_p0(v1_->cast_to_vertex_2d()->point());
00071         curve_->touch();
00072       }
00073     }
00074     if (v2_)
00075     {
00076       if (v1_->cast_to_vertex_2d())
00077       {
00078         curve_->set_p1(v2_->cast_to_vertex_2d()->point());
00079         curve_->touch();
00080       }
00081     }
00082   }
00083   touch();
00084 }
00085 
00086 //---------------------------------------------------------------------------
00087 //: Constructor from a zero-chain.
00088 //---------------------------------------------------------------------------
00089 //
00090 // Constructor for a vtol_edge_2d. If the vtol_zero_chain has two vertices , then the
00091 // first and last vertices of the vtol_zero_chain are used for endpoints and
00092 // an ImplicitLine is assumed to be the curve.  Otherwise, the all data
00093 // (v1_, v2_, curve_) are set to NULL.  The vtol_zero_chain, newchain, becomes
00094 // the Inferior of the vtol_edge_2d.
00095 
00096 vtol_edge_2d::vtol_edge_2d(vtol_zero_chain_sptr const& new_zero_chain)
00097 {
00098   link_inferior(new_zero_chain);
00099   set_vertices_from_zero_chains();
00100   if (new_zero_chain->numinf()==2 && v1_->cast_to_vertex_2d() && v2_->cast_to_vertex_2d())
00101     // Safe to assume that it is a vsol_line_2d.
00102     curve_=new vsol_line_2d(v1_->cast_to_vertex_2d()->point(),
00103                             v2_->cast_to_vertex_2d()->point());
00104   else
00105     // User must set the type of curve needed.
00106     // Since guessing could get confusing.
00107     // So NULL indicates an edge of unknown type.
00108     curve_=0;
00109   touch();
00110 }
00111 
00112 //: Constructor for a vtol_edge_2d from a list of zero-chains.
00113 // The list of zero-chains, newchains, is
00114 // assumed to be ordered along an edge. This method assigns the first
00115 // vertex in the chain list to v1_, and assigns the last vertex in the
00116 // chain list to v2_. No assumptions are made as to the curve type. The
00117 // data member, curve_ is left to be NULL.
00118 
00119 vtol_edge_2d::vtol_edge_2d(zero_chain_list const& newchains)
00120 {
00121   // 1) Link the inferiors.
00122 
00123   for (zero_chain_list::const_iterator i=newchains.begin(); i!=newchains.end(); ++i)
00124     link_inferior(*i);
00125 
00126   // 2) Set v1_ and v2_;
00127 
00128   set_vertices_from_zero_chains();
00129   curve_=0;
00130 }
00131 
00132 //: Constructor for a linear vtol_edge_2d.
00133 // The coordinates, (x1, y1, z1), determine vtol_vertex_2d, v1_.
00134 // The coordinates, (x2, y2, z2), determine v2_.
00135 // If curve is NULL, a vsol_line_2d is generated for the vtol_edge_2d.
00136 
00137 vtol_edge_2d::vtol_edge_2d(double x1, double y1,
00138                            double x2, double y2,
00139                            vsol_curve_2d_sptr curve)
00140 {
00141   v1_=new vtol_vertex_2d(x1,y1);
00142   v2_=new vtol_vertex_2d(x2,y2);
00143   if (!curve) {
00144     if (v1_->cast_to_vertex_2d() && v2_->cast_to_vertex_2d())
00145       curve_=new vsol_line_2d(v1_->cast_to_vertex_2d()->point(),
00146                               v2_->cast_to_vertex_2d()->point());
00147   }
00148   else
00149     curve_=curve->clone()->cast_to_curve();
00150 
00151   link_inferior(new vtol_zero_chain(v1_,v2_));
00152 }
00153 
00154 //: Constructor for a vtol_edge_2d from a vsol_curve_2d.
00155 // If edgecurve is of vsol_line_2d
00156 // type, vertex locations for endpoints, v1_ and v2_, are computed from
00157 // the vsol_line_2d parameters.  If edgecurve is of any other type, v1_
00158 // and v2_ are retrieved from the end points of the curve.
00159 
00160 vtol_edge_2d::vtol_edge_2d(vsol_curve_2d &edgecurve)
00161 {
00162   v1_ = new vtol_vertex_2d(*(edgecurve.p0()));
00163   v2_ = new vtol_vertex_2d(*(edgecurve.p1()));
00164   link_inferior(new vtol_zero_chain(v1_,v2_));
00165 }
00166 
00167 //---------------------------------------------------------------------------
00168 //: Clone `this': creation of a new object and initialization
00169 // See Prototype pattern
00170 //---------------------------------------------------------------------------
00171 vsol_spatial_object_2d* vtol_edge_2d::clone() const
00172 {
00173   return new vtol_edge_2d(vtol_edge_2d_sptr(const_cast<vtol_edge_2d*>(this)));
00174 }
00175 
00176 //---------------------------------------------------------------------------
00177 //: Set the curve with `new_curve'
00178 //---------------------------------------------------------------------------
00179 void vtol_edge_2d::set_curve(vsol_curve_2d &new_curve)
00180 {
00181   curve_=&new_curve;
00182   touch(); //Update timestamp
00183 }
00184 
00185 // ******************************************************
00186 //
00187 //    Operators
00188 //
00189 
00190 bool vtol_edge_2d::operator==(const vtol_edge_2d &other) const
00191 {
00192   if (this==&other) return true;
00193 
00194   if ( (curve() && !other.curve()) ||
00195        (!curve() && other.curve()) )
00196     return false;
00197 
00198   if (curve() && (*curve())!=(*other.curve()))
00199     return false;
00200 
00201   if (!(*v1_==*(other.v1_)) || !(*v2_==*(other.v2_)))
00202     return false;
00203 
00204   vtol_zero_chain_sptr zc1=zero_chain();
00205   vtol_zero_chain_sptr zc2=other.zero_chain();
00206   if (!zc1||!zc2)
00207     return false;
00208   return *zc1==*zc2;
00209 }
00210 
00211 //: edge equality
00212 bool vtol_edge_2d::operator==(const vtol_edge &other) const
00213 {
00214   return other.cast_to_edge_2d() && *this == *other.cast_to_edge_2d();
00215 }
00216 
00217 //: spatial object equality
00218 bool vtol_edge_2d::operator==(const vsol_spatial_object_2d& obj) const
00219 {
00220   return
00221    obj.cast_to_topology_object() &&
00222    obj.cast_to_topology_object()->cast_to_edge() &&
00223    *this == *obj.cast_to_topology_object()->cast_to_edge();
00224 }
00225 
00226 // ******************************************************
00227 //
00228 //    Inferior/Superior Accessor Functions
00229 //
00230 // ******************************************************
00231 //
00232 //    I/O methods
00233 //
00234 
00235 //:
00236 // This method outputs all edge information to the vcl_ostream, strm.  It
00237 // indents various levels of output by the number given in blanking.
00238 void vtol_edge_2d::describe(vcl_ostream &strm,
00239                             int blanking) const
00240 {
00241   for (int i1=0; i1<blanking; ++i1) strm << ' ';
00242   print(strm);
00243   for (int i2=0; i2<blanking; ++i2) strm << ' ';
00244   if (v1_) {
00245     v1_->print(strm);
00246   }
00247   else {
00248     strm << "Null vertex 1\n";
00249   }
00250   for (int i3=0; i3<blanking; ++i3) strm << ' ';
00251   if (v2_) {
00252     v2_->print(strm);
00253   }
00254   else {
00255     strm << "Null vertex 2\n";
00256   }
00257 }
00258 
00259 //:
00260 // This method outputs a brief vtol_edge_2d info with vtol_edge_2d object address.
00261 void vtol_edge_2d::print(vcl_ostream &strm) const
00262 {
00263    strm<<"<vtol_edge_2d "<<(void const *)this <<"> with id "<<get_id()<<'\n';
00264 }
00265 
00266 //: copy the geometry
00267 
00268 void vtol_edge_2d::copy_geometry(const vtol_edge &other)
00269 {
00270   if (other.cast_to_edge_2d())
00271     curve_ = other.cast_to_edge_2d()->curve();
00272 }
00273 
00274 bool vtol_edge_2d::compare_geometry(const vtol_edge &other) const
00275 {
00276   // we want to compare geometry
00277 
00278   if (other.cast_to_edge_2d())
00279     return (*curve()) == *(other.cast_to_edge_2d()->curve());
00280   else
00281     return false;
00282 }
00283 
00284 void vtol_edge_2d::compute_bounding_box() const
00285 {
00286   this->empty_bounding_box();
00287   vsol_curve_2d_sptr c = this->curve();
00288   if (c && c->cast_to_vdgl_digital_curve())
00289     this->set_bounding_box(c->cast_to_vdgl_digital_curve()->get_bounding_box());
00290   else // the geometry is either a line segment or unknown
00291     vtol_topology_object::compute_bounding_box();
00292 }