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