00001
00002
00003 #include "bgui_vsol_soview2D.h"
00004
00005 #include <vcl_iostream.h>
00006 #include <vnl/vnl_math.h>
00007
00008 #include <vgui/vgui_gl.h>
00009 #include <vgui/vgui_soview2D.h>
00010
00011 #include <vgl/vgl_distance.h>
00012 #include <vdgl/vdgl_digital_curve.h>
00013 #include <vdgl/vdgl_interpolator.h>
00014 #include <vdgl/vdgl_edgel_chain.h>
00015 #include <vdgl/vdgl_edgel.h>
00016
00017 #include <vsol/vsol_spatial_object_2d.h>
00018 #include <vsol/vsol_line_2d.h>
00019 #include <vsol/vsol_point_2d.h>
00020 #include <vsol/vsol_conic_2d.h>
00021 #include <vsol/vsol_polyline_2d.h>
00022 #include <vsol/vsol_polygon_2d.h>
00023 #include <vsol/vsol_digital_curve_2d.h>
00024
00025
00026
00027
00028 bgui_vsol_soview2D::bgui_vsol_soview2D( vsol_spatial_object_2d_sptr const & pt)
00029 : sptr_(pt)
00030 {
00031 }
00032
00033 vcl_ostream& bgui_vsol_soview2D::print(vcl_ostream& s) const
00034 {
00035 this->sptr_->describe(s);
00036 return vgui_soview2D::print(s);
00037 }
00038
00039
00040
00041
00042
00043
00044 bgui_vsol_soview2D_point::bgui_vsol_soview2D_point( vsol_point_2d_sptr const & pt)
00045 : bgui_vsol_soview2D(pt.ptr())
00046 {
00047 }
00048
00049 vsol_point_2d_sptr bgui_vsol_soview2D_point::sptr() const
00050 {
00051 return sptr_->cast_to_point();
00052 }
00053
00054 void bgui_vsol_soview2D_point::draw() const
00055 {
00056 glBegin(GL_POINTS);
00057 glVertex2f(sptr()->x(),sptr()->y());
00058 glEnd();
00059 }
00060
00061 float bgui_vsol_soview2D_point::distance_squared(float x, float y) const
00062 {
00063 float dx = sptr()->x() - x;
00064 float dy = sptr()->y() - y;
00065 return dx*dx + dy*dy;
00066 }
00067
00068 void bgui_vsol_soview2D_point::get_centroid(float* x, float* y) const
00069 {
00070 *x = sptr()->x();
00071 *y = sptr()->y();
00072 }
00073
00074 void bgui_vsol_soview2D_point::translate(float tx, float ty)
00075 {
00076 sptr()->set_x( sptr()->x() + tx );
00077 sptr()->set_y( sptr()->y() + ty );
00078 }
00079
00080
00081
00082
00083
00084 bgui_vsol_soview2D_line_seg::bgui_vsol_soview2D_line_seg( vsol_line_2d_sptr const & line)
00085 : bgui_vsol_soview2D(line.ptr())
00086 {
00087 }
00088
00089 vsol_line_2d_sptr bgui_vsol_soview2D_line_seg::sptr() const
00090 {
00091 return sptr_->cast_to_curve()->cast_to_line();
00092 }
00093
00094 void bgui_vsol_soview2D_line_seg::draw() const
00095 {
00096 glBegin(GL_LINES);
00097 glVertex2f(sptr()->p0()->x(),sptr()->p0()->y());
00098 glVertex2f(sptr()->p1()->x(),sptr()->p1()->y());
00099 glEnd();
00100 }
00101
00102 float bgui_vsol_soview2D_line_seg::distance_squared(float x, float y) const
00103 {
00104 return vgl_distance2_to_linesegment(sptr()->p0()->x(), sptr()->p0()->y(),
00105 sptr()->p1()->x(), sptr()->p1()->y(),
00106 double(x), double(y));
00107 }
00108
00109 void bgui_vsol_soview2D_line_seg::get_centroid(float* x, float* y) const
00110 {
00111 *x = (sptr()->p0()->x() + sptr()->p1()->x()) / 2;
00112 *y = (sptr()->p0()->y() + sptr()->p1()->y()) / 2;
00113 }
00114
00115 void bgui_vsol_soview2D_line_seg::translate(float tx, float ty)
00116 {
00117 sptr()->p0()->set_x( sptr()->p0()->x() + tx );
00118 sptr()->p0()->set_y( sptr()->p0()->y() + ty );
00119 sptr()->p1()->set_x( sptr()->p1()->x() + tx );
00120 sptr()->p1()->set_y( sptr()->p1()->y() + ty );
00121 }
00122
00123
00124
00125
00126
00127 bgui_vsol_soview2D_conic_seg::bgui_vsol_soview2D_conic_seg( vsol_conic_2d_sptr const & conic)
00128 : bgui_vsol_soview2D(conic.ptr())
00129 {
00130 if (!conic||!conic->is_real_ellipse())
00131 {
00132 xc_ = 0; yc_ =0;
00133 major_axis_ = 0; minor_axis_ = 0;
00134 angle_ = 0;
00135 start_angle_ = 0;
00136 end_angle_ = 0;
00137 return;
00138 }
00139 conic->ellipse_parameters(xc_, yc_, angle_, major_axis_, minor_axis_);
00140
00141
00142 vsol_point_2d_sptr p0 = conic->p0();
00143 start_angle_ = conic->ellipse_angular_position(p0);
00144
00145
00146 vsol_point_2d_sptr p1 = conic->p1();
00147 end_angle_ = conic->ellipse_angular_position(p1);
00148 if (end_angle_<=start_angle_)
00149 end_angle_ = 2.0*vnl_math::pi + end_angle_;
00150 }
00151
00152 vsol_conic_2d_sptr bgui_vsol_soview2D_conic_seg::sptr() const
00153 {
00154 return sptr_->cast_to_curve()->cast_to_conic();
00155 }
00156
00157
00158 void bgui_vsol_soview2D_conic_seg::draw() const
00159 {
00160 if (start_angle_==end_angle_)
00161 return;
00162
00163
00164 double one_degree = vnl_math::pi/180;
00165
00166 double px, py;
00167 glBegin(GL_LINE_STRIP);
00168 for (double phi = start_angle_; phi<=end_angle_; phi+=one_degree)
00169 {
00170 px = major_axis_*vcl_cos(angle_)*vcl_cos(phi)
00171 - minor_axis_*vcl_sin(angle_)*vcl_sin(phi);
00172
00173 py = minor_axis_*vcl_cos(angle_)*vcl_sin(phi)
00174 + major_axis_*vcl_sin(angle_)*vcl_cos(phi);
00175
00176 glVertex2d(xc_+px, yc_+py);
00177 }
00178 glEnd();
00179 }
00180
00181 float bgui_vsol_soview2D_conic_seg::distance_squared(float x, float y) const
00182 {
00183 vsol_point_2d_sptr p = new vsol_point_2d(x, y);
00184 double d = sptr()->distance(p);
00185 return static_cast<float>(d*d);
00186 }
00187
00188 void bgui_vsol_soview2D_conic_seg::get_centroid(float* x, float* y) const
00189 {
00190 *x = xc_;
00191 *y = yc_;
00192 }
00193
00194 void bgui_vsol_soview2D_conic_seg::translate(float tx, float ty)
00195 {
00196 double txd = static_cast<double>(tx), tyd = static_cast<double>(ty);
00197
00198 vsol_point_2d_sptr p0 = sptr()->p0();
00199 p0->set_x(p0->x()+tx); p0->set_y(p0->y()+ty);
00200 vsol_point_2d_sptr p1 = sptr()->p1();
00201 p1->set_x(p1->x()+tx); p1->set_y(p1->y()+ty);
00202
00203
00204 double a = sptr()->a(), b = sptr()->b(), c = sptr()->c(),
00205 d = sptr()->d(), e = sptr()->e(), f = sptr()->f();
00206 double dp = ( d + 2.0*a*txd + b*ty );
00207 double ep = ( e + 2.0*c*tyd + b*tx );
00208 double fp = ( f + a*txd*txd + b*txd*tyd + c*tyd*tyd + d*tx + e*ty);
00209 sptr()->set(a, b, c, dp, ep, fp);
00210 }
00211
00212
00213
00214
00215
00216 bgui_vsol_soview2D_polyline::bgui_vsol_soview2D_polyline(vsol_polyline_2d_sptr const& pline)
00217 : bgui_vsol_soview2D(pline.ptr())
00218 {
00219 }
00220
00221 vsol_polyline_2d_sptr bgui_vsol_soview2D_polyline::sptr() const
00222 {
00223 return sptr_->cast_to_curve()->cast_to_polyline();
00224 }
00225
00226 void bgui_vsol_soview2D_polyline::draw() const
00227 {
00228 unsigned int n = sptr()->size();
00229
00230 glBegin( GL_LINE_STRIP );
00231 for (unsigned int i=0; i<n;i++)
00232 {
00233 glVertex2f( sptr()->vertex(i)->x() , sptr()->vertex(i)->y() );
00234 }
00235 glEnd();
00236 }
00237
00238 float bgui_vsol_soview2D_polyline::distance_squared(float x, float y) const
00239 {
00240 unsigned int n = sptr()->size();
00241
00242 float* xptr = new float[n];
00243 float* yptr = new float[n];
00244 for (unsigned int i=0; i<n;i++)
00245 {
00246 xptr[i] = sptr()->vertex(i)->x();
00247 yptr[i] = sptr()->vertex(i)->y();
00248 }
00249
00250 double tmp = vgl_distance_to_non_closed_polygon( xptr , yptr , n , x , y );
00251
00252 delete [] xptr;
00253 delete [] yptr;
00254
00255 return tmp * tmp;
00256 }
00257
00258 void bgui_vsol_soview2D_polyline::get_centroid(float* x, float* y) const
00259 {
00260 unsigned int n = sptr()->size();
00261
00262 *x = 0;
00263 *y = 0;
00264
00265 for (unsigned int i=0; i<n;i++)
00266 {
00267 *x += sptr()->vertex(i)->x();
00268 *y += sptr()->vertex(i)->y();
00269 }
00270 float s = 1.0f / float( n );
00271 *x *= s;
00272 *y *= s;
00273 }
00274
00275 void bgui_vsol_soview2D_polyline::translate(float tx, float ty)
00276 {
00277 unsigned int n = sptr()->size();
00278
00279 for (unsigned int i=0; i<n;i++)
00280 {
00281 sptr()->vertex(i)->set_x( sptr()->vertex(i)->x() + tx );
00282 sptr()->vertex(i)->set_y( sptr()->vertex(i)->y() + ty );
00283 }
00284 }
00285
00286
00287
00288
00289
00290 bgui_vsol_soview2D_digital_curve::bgui_vsol_soview2D_digital_curve(vsol_digital_curve_2d_sptr const& dc,
00291 bool dotted)
00292 : bgui_vsol_soview2D(dc.ptr()), draw_dotted_(dotted)
00293 {
00294 }
00295
00296 vsol_digital_curve_2d_sptr bgui_vsol_soview2D_digital_curve::sptr() const
00297 {
00298 return sptr_->cast_to_curve()->cast_to_digital_curve();
00299 }
00300
00301 void bgui_vsol_soview2D_digital_curve::draw() const
00302 {
00303 unsigned int n = sptr()->size();
00304
00305 glBegin( GL_LINE_STRIP );
00306 for (unsigned int i=0; i<n;i++)
00307 {
00308 glVertex2f( sptr()->point(i)->x() , sptr()->point(i)->y() );
00309 }
00310 glEnd();
00311
00312 if (draw_dotted_)
00313 {
00314 glBegin(GL_POINTS);
00315 for (unsigned int i=0; i<n;i++)
00316 {
00317 glVertex2f( sptr()->point(i)->x() , sptr()->point(i)->y() );
00318 }
00319 glEnd();
00320 }
00321 }
00322
00323 float bgui_vsol_soview2D_digital_curve::distance_squared(float x, float y) const
00324 {
00325 unsigned int n = sptr()->size();
00326
00327 float* xptr = new float[n];
00328 float* yptr = new float[n];
00329 for (unsigned int i=0; i<n;i++)
00330 {
00331 xptr[i] = sptr()->point(i)->x();
00332 yptr[i] = sptr()->point(i)->y();
00333 }
00334
00335 double tmp = vgl_distance_to_non_closed_polygon( xptr , yptr , n , x , y );
00336
00337 delete [] xptr;
00338 delete [] yptr;
00339
00340 return tmp * tmp;
00341 }
00342
00343 void bgui_vsol_soview2D_digital_curve::get_centroid(float* x, float* y) const
00344 {
00345 unsigned int n = sptr()->size();
00346
00347 *x = 0;
00348 *y = 0;
00349
00350 for (unsigned int i=0; i<n;i++)
00351 {
00352 *x += sptr()->point(i)->x();
00353 *y += sptr()->point(i)->y();
00354 }
00355 float s = 1.0f / float( n );
00356 *x *= s;
00357 *y *= s;
00358 }
00359
00360 void bgui_vsol_soview2D_digital_curve::translate(float tx, float ty)
00361 {
00362 unsigned int n = sptr()->size();
00363
00364 for (unsigned int i=0; i<n;i++)
00365 {
00366 vsol_point_2d_sptr pt = sptr()->point(i);
00367 pt->set_x( pt->x() + tx );
00368 pt->set_y( pt->y() + ty );
00369 }
00370 }
00371
00372
00373
00374
00375
00376 bgui_vsol_soview2D_edgel_curve::bgui_vsol_soview2D_edgel_curve(vdgl_digital_curve_sptr const& e,
00377 bool dotted)
00378 : bgui_vsol_soview2D(e.ptr()), draw_dotted_(dotted)
00379 {
00380 }
00381
00382 vdgl_digital_curve_sptr bgui_vsol_soview2D_edgel_curve::sptr() const
00383 {
00384 return ( vdgl_digital_curve*)(sptr_.ptr());
00385 }
00386
00387 void bgui_vsol_soview2D_edgel_curve::draw() const
00388 {
00389
00390 vdgl_interpolator_sptr itrp = this->sptr()->get_interpolator();
00391 vdgl_edgel_chain_sptr ech = itrp->get_edgel_chain();
00392
00393 unsigned int n = ech->size();
00394
00395 glBegin( GL_LINE_STRIP );
00396 for (unsigned int i=0; i<n;i++)
00397 {
00398 vdgl_edgel ed = (*ech)[i];
00399 glVertex2f( ed.get_x() , ed.get_y() );
00400 }
00401 glEnd();
00402
00403 if (draw_dotted_)
00404 {
00405 glBegin(GL_POINTS);
00406 for (unsigned int i=0; i<n;i++)
00407 {
00408 vdgl_edgel ed = (*ech)[i];
00409 glVertex2f( ed.get_x() , ed.get_y() );
00410 }
00411 glEnd();
00412 }
00413 }
00414
00415 float bgui_vsol_soview2D_edgel_curve::distance_squared( float x , float y ) const
00416 {
00417
00418 vdgl_interpolator_sptr itrp = this->sptr()->get_interpolator();
00419 vdgl_edgel_chain_sptr ech = itrp->get_edgel_chain();
00420
00421 unsigned int n = ech->size();
00422
00423 float* xptr = new float[n];
00424 float* yptr = new float[n];
00425 for (unsigned int i=0; i<n;i++)
00426 {
00427 vdgl_edgel ed = (*ech)[i];
00428 xptr[i]=ed.get_x();
00429 yptr[i]=ed.get_y();
00430 }
00431
00432 double tmp = vgl_distance_to_non_closed_polygon( xptr , yptr , n , x , y );
00433
00434 delete [] xptr;
00435 delete [] yptr;
00436
00437 return tmp * tmp;
00438 }
00439
00440 void bgui_vsol_soview2D_edgel_curve::get_centroid( float* x, float* y ) const
00441 {
00442
00443 vdgl_interpolator_sptr itrp = this->sptr()->get_interpolator();
00444 vdgl_edgel_chain_sptr ech = itrp->get_edgel_chain();
00445
00446 unsigned int n = ech->size();
00447
00448 *x = 0;
00449 *y = 0;
00450
00451 for (unsigned int i=0; i<n;i++)
00452 {
00453 vdgl_edgel ed = (*ech)[i];
00454 *x += ed.get_x();
00455 *y += ed.get_y();
00456 }
00457 float s = 1.0f / float( n );
00458 *x *= s;
00459 *y *= s;
00460 }
00461
00462 void bgui_vsol_soview2D_edgel_curve::translate( float x , float y )
00463 {
00464
00465 vdgl_interpolator_sptr itrp = this->sptr()->get_interpolator();
00466 vdgl_edgel_chain_sptr ech = itrp->get_edgel_chain();
00467
00468 unsigned int n = ech->size();
00469
00470 for (unsigned int i=0; i<n;i++)
00471 {
00472 vdgl_edgel ed = (*ech)[i];
00473 ed.set_x( ed.get_x() + x );
00474 ed.set_y( ed.get_y() + y );
00475 }
00476 }
00477
00478
00479
00480
00481
00482 bgui_vsol_soview2D_polygon::bgui_vsol_soview2D_polygon(vsol_polygon_2d_sptr const& e)
00483 : bgui_vsol_soview2D(e.ptr())
00484 {
00485 }
00486
00487 vsol_polygon_2d_sptr bgui_vsol_soview2D_polygon::sptr() const
00488 {
00489 return sptr_->cast_to_region()->cast_to_polygon();
00490 }
00491
00492 void bgui_vsol_soview2D_polygon::draw() const
00493 {
00494 unsigned int n = sptr()->size();
00495
00496 glBegin( GL_LINE_LOOP );
00497 for (unsigned int i=0; i<n;i++)
00498 {
00499 glVertex2f( sptr()->vertex(i)->x() , sptr()->vertex(i)->y() );
00500 }
00501 glEnd();
00502 }
00503
00504 float bgui_vsol_soview2D_polygon::distance_squared( float x , float y ) const
00505 {
00506 unsigned int n = sptr()->size();
00507
00508 float* xptr = new float[n];
00509 float* yptr = new float[n];
00510 for (unsigned int i=0; i<n;i++)
00511 {
00512 xptr[i]=sptr()->vertex(i)->x();
00513 yptr[i]=sptr()->vertex(i)->y();
00514 }
00515
00516 double tmp = vgl_distance_to_closed_polygon( xptr , yptr , n , x , y );
00517
00518 delete [] xptr;
00519 delete [] yptr;
00520
00521 return tmp * tmp;
00522 }
00523
00524 void bgui_vsol_soview2D_polygon::get_centroid( float* x, float* y ) const
00525 {
00526 unsigned int n = sptr()->size();
00527
00528 *x = 0;
00529 *y = 0;
00530
00531 for (unsigned int i=0; i<n;i++)
00532 {
00533 *x += sptr()->vertex(i)->x();
00534 *y += sptr()->vertex(i)->y();
00535 }
00536 float s = 1.0f / float( n );
00537 *x *= s;
00538 *y *= s;
00539 }
00540
00541 void bgui_vsol_soview2D_polygon::translate( float x , float y )
00542 {
00543 unsigned int n = sptr()->size();
00544
00545 for (unsigned int i=0; i<n;i++)
00546 {
00547 sptr()->vertex(i)->set_x( sptr()->vertex(i)->x() + x );
00548 sptr()->vertex(i)->set_y( sptr()->vertex(i)->y() + y );
00549 }
00550 }