contrib/brl/bbas/bgui/bgui_vsol_camera_tableau.cxx
Go to the documentation of this file.
00001 #include "bgui_vsol_camera_tableau.h"
00002 //:
00003 // \file
00004 
00005 #include <bgui/bgui_vsol_soview2D.h>
00006 #include <vgui/vgui_style.h>
00007 #include <vsol/vsol_point_3d.h>
00008 #include <vsol/vsol_point_2d.h>
00009 #include <vsol/vsol_line_3d.h>
00010 #include <vsol/vsol_line_2d.h>
00011 #include <vsol/vsol_polygon_3d.h>
00012 #include <vsol/vsol_polygon_2d.h>
00013 #include <vsol/vsol_box_3d.h>
00014 #include <vgl/vgl_homg_point_2d.h>
00015 #include <vgl/vgl_homg_point_3d.h>
00016 
00017 bgui_vsol_camera_tableau::bgui_vsol_camera_tableau(const char* n) : vgui_easy2D_tableau(n)
00018 { this->init(); }
00019 
00020 bgui_vsol_camera_tableau::bgui_vsol_camera_tableau(vgui_image_tableau_sptr const& it,
00021                                                    const char* n) : vgui_easy2D_tableau(it, n)
00022 { this->init(); }
00023 
00024 bgui_vsol_camera_tableau::bgui_vsol_camera_tableau(vgui_tableau_sptr const& t,
00025                                                    const char* n) : vgui_easy2D_tableau(t, n)
00026 { this->init(); }
00027 
00028 bgui_vsol_camera_tableau::~bgui_vsol_camera_tableau()
00029 {
00030 }
00031 
00032 vgl_point_2d<double>
00033 bgui_vsol_camera_tableau::project(vsol_point_3d_sptr const& p3d)
00034 {
00035   vgl_homg_point_3d<double> hp3d = p3d->homg_point();
00036   vgl_homg_point_2d<double> hp2d = camera_(hp3d);
00037   vgl_point_2d<double> p2d(hp2d);
00038   return p2d;
00039 }
00040 
00041 void bgui_vsol_camera_tableau::init()
00042 {
00043   old_id_ = 0;
00044   highlight_ = true;
00045   //set highlight display style parameters
00046   highlight_style_ = vgui_style::new_style(0.0f, 0.0f, 1.0f, 5.0f, 5.0f);
00047 
00048   //define default soview styles
00049   //these can be overridden by later set_*_syle commands prior to drawing.
00050   //
00051    this->set_vsol_point_3d_style(0.0f, 1.0f, 0.0f, 5.0f);
00052    this->set_vsol_line_3d_style(0.8f, 0.2f, 0.9f, 3.0f);
00053    this->set_vsol_polygon_3d_style(0.0f, 1.0f, 0.0f, 3.0f);
00054    this->set_vsol_box_3d_style(0.8f, 0.2f, 0.9f, 3.0f);
00055    camera_.set_identity();
00056 }
00057 
00058 //: Provide roaming highlighting for soviews in the tableau.
00059 // As the mouse moves the soview closest to the mouse is
00060 // changed to the highlighted style.
00061 // vgui_displaylist2D_tableau::motion(..) has a mechanism for
00062 // highlighting the nearest object but it doesn't work.
00063 bool bgui_vsol_camera_tableau::handle(vgui_event const &e)
00064 {
00065   if (e.type == vgui_MOTION&&highlight_)
00066   {
00067     // retrieve the previously highlighted soview and
00068     // restore it to its default style
00069     vgui_soview* old_so = vgui_soview::id_to_object(old_id_);
00070     if (old_so)
00071       old_so->set_style(style_map_[old_so->type_name()]);
00072     //get the soview that is closest to the mouse
00073     vgui_soview2D* high_so = (vgui_soview2D*)get_highlighted_soview();
00074     if (high_so&&high_so->get_style())
00075     {
00076       //replace the old soview with the currently closest view
00077       int id = high_so->get_id();
00078       old_id_ = id;
00079       //set soview style to the highlight color and weight
00080       high_so->set_style(highlight_style_);
00081       this->post_redraw();
00082     }
00083   }
00084   // We aren't interested in other events so pass them to the base class.
00085   return vgui_easy2D_tableau::handle(e);
00086 }
00087 
00088 bgui_vsol_soview2D_point*
00089 bgui_vsol_camera_tableau::add_vsol_point_3d(vsol_point_3d_sptr const& p)
00090 {
00091   vgl_homg_point_3d<double> hp3d = p->homg_point();
00092   vgl_homg_point_2d<double> hp2d = camera_(hp3d);
00093   vgl_point_2d<double> p2d(hp2d);//Get
00094   vsol_point_2d_sptr pt = new vsol_point_2d(p2d.x(), p2d.y());
00095 
00096   bgui_vsol_soview2D_point* obj = new bgui_vsol_soview2D_point(pt);
00097   add(obj);
00098   obj->set_style(style_map_[obj->type_name()]);
00099   return obj;
00100 }
00101 
00102 bgui_vsol_soview2D_line_seg*
00103 bgui_vsol_camera_tableau::add_vsol_line_3d(vsol_line_3d_sptr const& line)
00104 {
00105   vsol_point_3d_sptr p0 = line->p0();
00106   vsol_point_3d_sptr p1 = line->p1();
00107   vgl_point_2d<double> p0_2d = this->project(p0);
00108   vgl_point_2d<double> p1_2d = this->project(p1);
00109 
00110   vsol_line_2d_sptr l2d = new vsol_line_2d(p0_2d, p1_2d);
00111   bgui_vsol_soview2D_line_seg* obj = new bgui_vsol_soview2D_line_seg(l2d);
00112   add(obj);
00113   //set the default style
00114   obj->set_style(style_map_[obj->type_name()]);
00115   return obj;
00116 }
00117 
00118 bgui_vsol_soview2D_polygon*
00119 bgui_vsol_camera_tableau::add_vsol_polygon_3d(vsol_polygon_3d_sptr const& poly)
00120 {
00121   int n = poly->size();
00122   vcl_vector<vsol_point_2d_sptr> vertices(n);
00123   //project the polygon
00124   for (int i = 0; i<n; ++i)
00125   {
00126     vsol_point_3d_sptr v = poly->vertex(i);
00127     vgl_point_2d<double> p2d = this->project(v);
00128     vsol_point_2d_sptr p = new vsol_point_2d(p2d);
00129     vertices[i] = p;
00130   }
00131   vsol_polygon_2d_sptr poly_2d = new vsol_polygon_2d(vertices);
00132   bgui_vsol_soview2D_polygon* obj = new bgui_vsol_soview2D_polygon(poly_2d);
00133   add(obj);
00134   //set the default style
00135   obj->set_style(style_map_[obj->type_name()]);
00136   return obj;
00137 }
00138 
00139 bgui_vsol_soview2D_polygon*
00140 bgui_vsol_camera_tableau::add_vsol_box_3d(vsol_box_3d_sptr const& box)
00141 {
00142   // top face points
00143   vsol_point_3d_sptr pt0 =
00144     new vsol_point_3d(box->get_min_x(), box->get_min_y(), box->get_max_z());
00145   vgl_point_2d<double> vpt0_2d = this->project(pt0);
00146   vsol_point_2d_sptr pt0_2d = new vsol_point_2d(vpt0_2d);
00147 
00148   vsol_point_3d_sptr pt1 =
00149     new vsol_point_3d(box->get_max_x(), box->get_min_y(), box->get_max_z());
00150   vgl_point_2d<double> vpt1_2d = this->project(pt1);
00151   vsol_point_2d_sptr pt1_2d = new vsol_point_2d(vpt1_2d);
00152 
00153   vsol_point_3d_sptr pt2 =
00154     new vsol_point_3d(box->get_max_x(), box->get_max_y(), box->get_max_z());
00155   vgl_point_2d<double> vpt2_2d = this->project(pt2);
00156   vsol_point_2d_sptr pt2_2d = new vsol_point_2d(vpt2_2d);
00157 
00158   vsol_point_3d_sptr pt3 =
00159     new vsol_point_3d(box->get_min_x(), box->get_max_y(), box->get_max_z());
00160   vgl_point_2d<double> vpt3_2d = this->project(pt3);
00161   vsol_point_2d_sptr pt3_2d = new vsol_point_2d(vpt3_2d);
00162 
00163   // bottom face points
00164   vsol_point_3d_sptr pb0 =
00165     new vsol_point_3d(box->get_min_x(), box->get_min_y(), box->get_min_z());
00166   vgl_point_2d<double> vpb0_2d = this->project(pb0);
00167   vsol_point_2d_sptr pb0_2d = new vsol_point_2d(vpb0_2d);
00168 
00169   vsol_point_3d_sptr pb1 =
00170     new vsol_point_3d(box->get_max_x(), box->get_min_y(), box->get_min_z());
00171   vgl_point_2d<double> vpb1_2d = this->project(pb1);
00172   vsol_point_2d_sptr pb1_2d = new vsol_point_2d(vpb1_2d);
00173 
00174   vsol_point_3d_sptr pb2 =
00175     new vsol_point_3d(box->get_max_x(), box->get_max_y(), box->get_min_z());
00176   vgl_point_2d<double> vpb2_2d = this->project(pb2);
00177   vsol_point_2d_sptr pb2_2d = new vsol_point_2d(vpb2_2d);
00178 
00179   vsol_point_3d_sptr pb3 = new vsol_point_3d(box->get_min_x(), box->get_max_y(), box->get_min_z());
00180   vgl_point_2d<double> vpb3_2d = this->project(pb3);
00181   vsol_point_2d_sptr pb3_2d = new vsol_point_2d(vpb3_2d);
00182 
00183   //construct the 6 polygons that make up the box and glue them together
00184   vcl_vector<vsol_point_2d_sptr> verts;
00185   //Top face
00186   verts.push_back(pt0_2d); verts.push_back(pt1_2d); verts.push_back(pt2_2d); verts.push_back(pt3_2d);
00187   //Side face 0
00188   verts.push_back(pt0_2d); verts.push_back(pb0_2d); verts.push_back(pb1_2d); verts.push_back(pt1_2d);
00189   //Side face 1
00190   verts.push_back(pt2_2d); verts.push_back(pb2_2d); verts.push_back(pb1_2d);
00191   //Bottom face
00192   verts.push_back(pb2_2d); verts.push_back(pb3_2d); verts.push_back(pb0_2d);
00193   //Side face 2&3
00194   verts.push_back(pb3_2d); verts.push_back(pt3_2d);
00195 
00196   bgui_vsol_soview2D_polygon* obj = new bgui_vsol_soview2D_polygon(new vsol_polygon_2d(verts));
00197   add(obj);
00198   //set the default style
00199   obj->set_style(style_map_[obj->type_name()]);
00200   return obj;
00201 }
00202 
00203 //--------------------------------------------------------------
00204 // Add general spatial objects
00205 //
00206 void bgui_vsol_camera_tableau::
00207 add_spatial_objects_3d(vcl_vector<vsol_spatial_object_3d_sptr> const& sos)
00208 {
00209   for (vcl_vector<vsol_spatial_object_3d_sptr>::const_iterator sit = sos.begin();
00210        sit != sos.end(); sit++)
00211   {
00212     add_spatial_object_3d( (*sit) );
00213   }
00214 }
00215 
00216 void bgui_vsol_camera_tableau::
00217 add_spatial_object_3d(vsol_spatial_object_3d_sptr const& so)
00218 {
00219   if (so->cast_to_point())
00220   {
00221     vsol_point_3d_sptr p = so->cast_to_point();
00222     this->add_vsol_point_3d(p);
00223   }
00224   if (so->cast_to_curve()) {
00225     if (so->cast_to_curve()->cast_to_line())
00226     {
00227       vsol_line_3d_sptr line =
00228         so->cast_to_curve()->cast_to_line();
00229       this->add_vsol_line_3d(line);
00230     }
00231   }
00232 
00233   if (so->cast_to_surface()) {
00234     if (so->cast_to_surface()->cast_to_region() &&
00235         so->cast_to_surface()->cast_to_region()->cast_to_polygon())
00236     {
00237       vsol_polygon_3d_sptr poly =
00238         so->cast_to_surface()->cast_to_region()->cast_to_polygon();
00239       this->add_vsol_polygon_3d(poly);
00240     }
00241   }
00242 
00243   return;
00244 }
00245 
00246 void bgui_vsol_camera_tableau::
00247 add_points_3d(vcl_vector<vsol_point_3d_sptr> const& points)
00248 {
00249   for (vcl_vector<vsol_point_3d_sptr>::const_iterator pit = points.begin();
00250        pit != points.end(); pit++)
00251     add_vsol_point_3d(*pit);
00252 }
00253 
00254 void bgui_vsol_camera_tableau::
00255 add_lines_3d(vcl_vector<vsol_line_3d_sptr> const& lines)
00256 {
00257   for (vcl_vector<vsol_line_3d_sptr>::const_iterator lit = lines.begin();
00258        lit != lines.end(); lit++)
00259     add_vsol_line_3d(*lit);
00260 }
00261 
00262 void bgui_vsol_camera_tableau::
00263 add_polygons_3d(vcl_vector<vsol_polygon_3d_sptr> const& polys)
00264 {
00265   for (vcl_vector<vsol_polygon_3d_sptr>::const_iterator pit = polys.begin();
00266        pit != polys.end(); pit++)
00267     add_vsol_polygon_3d(*pit);
00268 }
00269 
00270 void bgui_vsol_camera_tableau::clear_all()
00271 {
00272   bool temp = highlight_;
00273   highlight_ = false;//in case of event interrupts during the clear
00274   obj_map_.clear();
00275   vgui_easy2D_tableau::clear();
00276   old_id_ = 0;
00277   highlight_ = temp;
00278   this->post_redraw();
00279 }
00280 
00281 void
00282 bgui_vsol_camera_tableau::set_vsol_point_3d_style(const float r,
00283                                                   const float g,
00284                                                   const float b,
00285                                                   const float point_radius)
00286 {
00287   style_map_[bgui_vsol_soview2D_point::type_name_()] = vgui_style::new_style(r, g, b, point_radius, 0.0f);
00288 }
00289 
00290 void
00291 bgui_vsol_camera_tableau::set_vsol_line_3d_style(const float r,
00292                                                  const float g,
00293                                                  const float b,
00294                                                  const float line_width)
00295 {
00296   style_map_[bgui_vsol_soview2D_line_seg::type_name_()] = vgui_style::new_style(r, g, b, 0.0f, line_width);
00297 }
00298 
00299 void
00300 bgui_vsol_camera_tableau::set_vsol_polygon_3d_style(const float r,
00301                                                     const float g,
00302                                                     const float b,
00303                                                     const float line_width)
00304 {
00305   style_map_[bgui_vsol_soview2D_polygon::type_name_()] = vgui_style::new_style(r, g, b, 0.0f, line_width);
00306 }
00307 
00308 void
00309 bgui_vsol_camera_tableau::set_vsol_box_3d_style(const float r,
00310                                                 const float g,
00311                                                 const float b,
00312                                                 const float line_width)
00313 {
00314   style_map_[bgui_vsol_soview2D_polygon::type_name_()] = vgui_style::new_style(r, g, b, 0.0f, line_width);
00315 }