00001
00002 #include "vtol_edge_2d.h"
00003
00004
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
00014
00015
00016
00017
00018
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
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
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
00088
00089
00090
00091
00092
00093
00094
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
00102 curve_=new vsol_line_2d(v1_->cast_to_vertex_2d()->point(),
00103 v2_->cast_to_vertex_2d()->point());
00104 else
00105
00106
00107
00108 curve_=0;
00109 touch();
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119 vtol_edge_2d::vtol_edge_2d(zero_chain_list const& newchains)
00120 {
00121
00122
00123 for (zero_chain_list::const_iterator i=newchains.begin(); i!=newchains.end(); ++i)
00124 link_inferior(*i);
00125
00126
00127
00128 set_vertices_from_zero_chains();
00129 curve_=0;
00130 }
00131
00132
00133
00134
00135
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
00154
00155
00156
00157
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
00168
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
00177
00178 void vtol_edge_2d::set_curve(vsol_curve_2d &new_curve)
00179 {
00180 curve_=&new_curve;
00181 touch();
00182 }
00183
00184
00185
00186
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
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
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
00228
00229
00230
00231
00232
00233
00234
00235
00236
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
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
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
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
00288 vtol_topology_object::compute_bounding_box();
00289 }