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 an 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   else
00148     curve_=curve->clone()->cast_to_curve();
00149 
00150   link_inferior(new vtol_zero_chain(v1_,v2_));
00151 }
00152 
00153 //: Constructor for an vtol_edge_2d from a vsol_curve_2d.
00154 // If edgecurve is of vsol_line_2d
00155 // type, vertex locations for endpoints, v1_ and v2_, are computed from
00156 // the vsol_line_2d parameters.  If edgecurve is of any other type, v1_
00157 // and v2_ are retrieved from the end points of the curve.
00158 
00159 vtol_edge_2d::vtol_edge_2d(vsol_curve_2d &edgecurve)
00160 {
00161   v1_ = new vtol_vertex_2d(*(edgecurve.p0()));
00162   v2_ = new vtol_vertex_2d(*(edgecurve.p1()));
00163   link_inferior(new vtol_zero_chain(v1_,v2_));
00164 }
00165 
00166 //---------------------------------------------------------------------------
00167 //: Clone `this': creation of a new object and initialization
00168 // See Prototype pattern
00169 //---------------------------------------------------------------------------
00170 vsol_spatial_object_2d* vtol_edge_2d::clone() const
00171 {
00172   return new vtol_edge_2d(vtol_edge_2d_sptr(const_cast<vtol_edge_2d*>(this)));
00173 }
00174 
00175 //---------------------------------------------------------------------------
00176 //: Set the curve with `new_curve'
00177 //---------------------------------------------------------------------------
00178 void vtol_edge_2d::set_curve(vsol_curve_2d &new_curve)
00179 {
00180   curve_=&new_curve;
00181   touch(); //Update timestamp
00182 }
00183 
00184 // ******************************************************
00185 //
00186 //    Operators
00187 //
00188 
00189 bool vtol_edge_2d::operator==(const vtol_edge_2d &other) const
00190 {
00191   if (this==&other) return true;
00192 
00193   if ( (curve() && !other.curve()) ||
00194        (!curve() && other.curve()) )
00195     return false;
00196 
00197   if (curve() && (*curve())!=(*other.curve()))
00198     return false;
00199 
00200   if (!(*v1_==*(other.v1_)) || !(*v2_==*(other.v2_)))
00201     return false;
00202 
00203   vtol_zero_chain_sptr zc1=zero_chain();
00204   vtol_zero_chain_sptr zc2=other.zero_chain();
00205   if (!zc1||!zc2)
00206     return false;
00207   return *zc1==*zc2;
00208 }
00209 
00210 //: edge equality
00211 bool vtol_edge_2d::operator==(const vtol_edge &other) const
00212 {
00213   return other.cast_to_edge_2d() && *this == *other.cast_to_edge_2d();
00214 }
00215 
00216 //: spatial object equality
00217 bool vtol_edge_2d::operator==(const vsol_spatial_object_2d& obj) const
00218 {
00219   return
00220    obj.cast_to_topology_object() &&
00221    obj.cast_to_topology_object()->cast_to_edge() &&
00222    *this == *obj.cast_to_topology_object()->cast_to_edge();
00223 }
00224 
00225 // ******************************************************
00226 //
00227 //    Inferior/Superior Accessor Functions
00228 //
00229 // ******************************************************
00230 //
00231 //    I/O methods
00232 //
00233 
00234 //:
00235 // This method outputs all edge information to the vcl_ostream, strm.  It
00236 // indents various levels of output by the number given in blanking.
00237 void vtol_edge_2d::describe(vcl_ostream &strm,
00238                             int blanking) const
00239 {
00240   for (int i1=0; i1<blanking; ++i1) strm << ' ';
00241   print(strm);
00242   for (int i2=0; i2<blanking; ++i2) strm << ' ';
00243   if (v1_) {
00244     v1_->print(strm);
00245   } else {
00246     strm << "Null vertex 1\n";
00247   }
00248   for (int i3=0; i3<blanking; ++i3) strm << ' ';
00249   if (v2_) {
00250     v2_->print(strm);
00251   } else {
00252     strm << "Null vertex 2\n";
00253   }
00254 }
00255 
00256 //:
00257 // This method outputs a brief vtol_edge_2d info with vtol_edge_2d object address.
00258 void vtol_edge_2d::print(vcl_ostream &strm) const
00259 {
00260    strm<<"<vtol_edge_2d "<<(void const *)this <<"> with id "<<get_id()<<'\n';
00261 }
00262 
00263 //: copy the geometry
00264 
00265 void vtol_edge_2d::copy_geometry(const vtol_edge &other)
00266 {
00267   if (other.cast_to_edge_2d())
00268     curve_ = other.cast_to_edge_2d()->curve();
00269 }
00270 
00271 bool vtol_edge_2d::compare_geometry(const vtol_edge &other) const
00272 {
00273   // we want to compare geometry
00274 
00275   if (other.cast_to_edge_2d())
00276     return (*curve()) == *(other.cast_to_edge_2d()->curve());
00277   else
00278     return false;
00279 }
00280 
00281 void vtol_edge_2d::compute_bounding_box() const
00282 {
00283   this->empty_bounding_box();
00284   vsol_curve_2d_sptr c = this->curve();
00285   if (c && c->cast_to_vdgl_digital_curve())
00286     this->set_bounding_box(c->cast_to_vdgl_digital_curve()->get_bounding_box());
00287   else // the geometry is either a line segment or unknown
00288     vtol_topology_object::compute_bounding_box();
00289 }

Generated on Tue Dec 2 05:15:29 2008 for contrib/gel/vtol by  doxygen 1.5.1