Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

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

Generated on Thu Jan 10 14:52:11 2008 for contrib/brl/bbas/bgui by  doxygen 1.4.4