Go to the documentation of this file.00001
00002 #include "vsol_polyline_2d.h"
00003
00004
00005
00006 #include <vsol/vsol_point_2d.h>
00007 #include <vgl/vgl_vector_2d.h>
00008 #include <vsl/vsl_vector_io.h>
00009 #include <vcl_iostream.h>
00010 #include <vcl_cassert.h>
00011
00012
00013
00014
00015
00016
00017
00018
00019 vsol_polyline_2d::vsol_polyline_2d()
00020 : vsol_curve_2d()
00021 {
00022 storage_=new vcl_vector<vsol_point_2d_sptr>();
00023 p0_ = 0;
00024 p1_ = 0;
00025 }
00026
00027
00028
00029
00030
00031 vsol_polyline_2d::vsol_polyline_2d(vcl_vector<vsol_point_2d_sptr> const& new_vertices)
00032 : vsol_curve_2d()
00033 {
00034 storage_=new vcl_vector<vsol_point_2d_sptr>(new_vertices);
00035 int n = storage_->size();
00036 if (n<2)
00037 {
00038 p0_ = 0;
00039 p1_ = 0;
00040 return;
00041 }
00042 p0_ = (*storage_)[0];
00043 p1_ = (*storage_)[n-1];
00044 }
00045
00046
00047
00048 vsol_polyline_2d::vsol_polyline_2d(vsol_polyline_2d const& other)
00049 : vsol_curve_2d(other)
00050 {
00051 storage_=new vcl_vector<vsol_point_2d_sptr>(*other.storage_);
00052 for (unsigned int i=0;i<storage_->size();++i)
00053 (*storage_)[i]=new vsol_point_2d(*((*other.storage_)[i]));
00054 p0_ = other.p0_;
00055 p1_ = other.p1_;
00056 }
00057
00058
00059
00060
00061 vsol_polyline_2d::~vsol_polyline_2d()
00062 {
00063 for (unsigned i = 0; i < storage_->size(); i++)
00064 (*storage_)[i] = 0;
00065 p0_ = 0;
00066 p1_ = 0;
00067 delete storage_;
00068 }
00069
00070
00071
00072
00073
00074 vsol_spatial_object_2d* vsol_polyline_2d::clone() const
00075 {
00076 return new vsol_polyline_2d(*this);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 vsol_point_2d_sptr vsol_polyline_2d::vertex(const int i) const
00088 {
00089
00090 assert(valid_index(i));
00091
00092 return (*storage_)[i];
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102 bool vsol_polyline_2d::operator==(vsol_polyline_2d const& other) const
00103 {
00104 if (this==&other)
00105 return true;
00106
00107
00108 bool epts_eq = vsol_curve_2d::endpoints_equal(other);
00109 if (!epts_eq)
00110 return false;
00111
00112 if (storage_->size()!=other.storage_->size())
00113 return false;
00114
00115 int n = storage_->size();
00116 for (int i=0; i<n; i++)
00117 if (*((*storage_)[i])!=*((*other.storage_)[i]))
00118 return false;
00119 return true;
00120 }
00121
00122
00123
00124 bool vsol_polyline_2d::operator==(vsol_spatial_object_2d const& obj) const
00125 {
00126 return
00127 obj.cast_to_curve() && obj.cast_to_curve()->cast_to_polyline() &&
00128 *this == *obj.cast_to_curve()->cast_to_polyline();
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138 double vsol_polyline_2d::length() const
00139 {
00140 double l = 0.0;
00141 for (unsigned int i=0;i+1<storage_->size();++i)
00142 l += ::length(vgl_vector_2d<double>((*storage_)[i+1]->x(),(*storage_)[i+1]->y())
00143 -vgl_vector_2d<double>((*storage_)[i]->x(),(*storage_)[i]->y()));
00144 return l;
00145 }
00146
00147
00148
00149
00150 void vsol_polyline_2d::compute_bounding_box() const
00151 {
00152 set_bounding_box((*storage_)[0]->x(), (*storage_)[0]->y());
00153 for (unsigned int i=1;i<storage_->size();++i)
00154 add_to_bounding_box((*storage_)[i]->x(), (*storage_)[i]->y());
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 void vsol_polyline_2d::set_p0(vsol_point_2d_sptr const& new_p0)
00166 {
00167 p0_=new_p0;
00168 storage_->push_back(p0_);
00169 }
00170
00171
00172
00173
00174
00175 void vsol_polyline_2d::set_p1(vsol_point_2d_sptr const& new_p1)
00176 {
00177 p1_=new_p1;
00178 storage_->push_back(p0_);
00179 }
00180
00181
00182
00183
00184 void vsol_polyline_2d::add_vertex(vsol_point_2d_sptr const& new_p)
00185 {
00186 storage_->push_back(new_p);
00187
00188 p1_ = new_p;
00189 }
00190
00191
00192
00193
00194
00195
00196 void vsol_polyline_2d::b_write(vsl_b_ostream &os) const
00197 {
00198 if (!storage_)
00199 vsl_b_write(os, false);
00200 else
00201 {
00202 vsl_b_write(os, true);
00203 vsl_b_write(os, version());
00204 vsl_b_write(os, *storage_);
00205 }
00206 }
00207
00208 void vsol_polyline_2d::b_read(vsl_b_istream &is)
00209 {
00210 if (!is)
00211 return;
00212 delete storage_;
00213 storage_ = new vcl_vector<vsol_point_2d_sptr>();
00214 p0_=0;
00215 p1_=0;
00216 bool null_ptr;
00217 vsl_b_read(is, null_ptr);
00218 if (!null_ptr)
00219 return;
00220 short ver;
00221 vsl_b_read(is, ver);
00222 switch (ver)
00223 {
00224 case 1: {
00225 vsl_b_read(is, *storage_);
00226 int n = storage_->size();
00227 if (n<2)
00228 break;
00229 p0_=(*storage_)[0];
00230 p1_=(*storage_)[n-1];
00231 break;
00232 }
00233 default:
00234 vcl_cerr << "vsol_polyline_2d: unknown I/O version " << ver << '\n';
00235 }
00236 }
00237
00238 short vsol_polyline_2d::version() const
00239 {
00240 return 1;
00241 }
00242
00243
00244 void vsol_polyline_2d::print_summary(vcl_ostream &os) const
00245 {
00246 os << *this;
00247 }
00248
00249
00250 void
00251 vsl_b_write(vsl_b_ostream &os, const vsol_polyline_2d* p)
00252 {
00253 if (p==0) {
00254 vsl_b_write(os, false);
00255 }
00256 else {
00257 vsl_b_write(os,true);
00258 p->b_write(os);
00259 }
00260 }
00261
00262
00263
00264 void
00265 vsl_b_read(vsl_b_istream &is, vsol_polyline_2d* &p)
00266 {
00267 delete p;
00268 bool not_null_ptr;
00269 vsl_b_read(is, not_null_ptr);
00270 if (not_null_ptr) {
00271 p = new vsol_polyline_2d();
00272 p->b_read(is);
00273 }
00274 else
00275 p = 0;
00276 }
00277
00278 void vsol_polyline_2d::describe(vcl_ostream &strm, int blanking) const
00279 {
00280 if (blanking < 0) blanking = 0; while (blanking--) strm << ' ';
00281 strm << "[vsol_polyline_2d";
00282 for (unsigned int i=0; i<size(); ++i)
00283 strm << ' ' << *(vertex(i));
00284 strm << ']' << vcl_endl;
00285 }