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