contrib/brl/bseg/segv/segv_misc_manager.cxx
Go to the documentation of this file.
00001 // This is brl/bseg/segv/segv_misc_manager.cxx
00002 #include "segv_misc_manager.h"
00003 //:
00004 // \file
00005 // \author J.L. Mundy
00006 
00007 #if 1 //JLM
00008 #include <vpgl/file_formats/vpgl_nitf_rational_camera.h>
00009 #endif
00010 // include for project points menu option
00011 #include <vpgl/vpgl_rational_camera.h>
00012 #include <vcl_iostream.h>
00013 #include <vcl_cmath.h> //sin, cos exp
00014 #include <vul/vul_file.h>
00015 #include <vnl/vnl_math.h>
00016 #include <vil/vil_image_view.h>
00017 #include <vil/vil_blocked_image_resource.h>
00018 #include <vil/vil_pyramid_image_resource.h>
00019 #include <vil/vil_load.h>
00020 #include <vil/vil_save.h>
00021 #include <vil/vil_new.h>
00022 #include <vil/vil_property.h>
00023 #include <sdet/sdet_detector_params.h>
00024 #include <sdet/sdet_detector.h>
00025 #include <sdet/sdet_harris_detector_params.h>
00026 #include <sdet/sdet_harris_detector.h>
00027 #include <sdet/sdet_nonmax_suppression_params.h>
00028 #include <sdet/sdet_nonmax_suppression.h>
00029 #include <sdet/sdet_fit_lines_params.h>
00030 #include <sdet/sdet_fit_lines.h>
00031 #include <sdet/sdet_fit_conics.h>
00032 #include <sdet/sdet_grid_finder_params.h>
00033 #include <sdet/sdet_grid_finder.h>
00034 #include <vgui/vgui.h>
00035 #include <vgui/vgui_find.h>
00036 #include <vgui/vgui_tableau.h>
00037 #include <vgui/vgui_dialog.h>
00038 #include <vgui/vgui_style_sptr.h>
00039 #include <vgui/vgui_style.h>
00040 #include <vgui/vgui_viewer2D_tableau.h>
00041 #include <vgui/vgui_shell_tableau.h>
00042 #include <vgui/vgui_grid_tableau.h>
00043 #include <vgui/vgui_range_map_params.h>
00044 #include <bgui/bgui_image_tableau.h>
00045 #include <bgui/bgui_vtol2D_tableau.h>
00046 #include <bgui/bgui_picker_tableau.h>
00047 #include <bgui/bgui_range_adjuster_tableau.h>
00048 #include <bgui/bgui_image_utils.h>
00049 #include <bvgl/bvgl_articulated_poly.h>
00050 #include <vsol/vsol_point_2d.h>
00051 #include <vsol/vsol_point_2d_sptr.h>
00052 #include <vsol/vsol_conic_2d.h>
00053 #include <vsol/vsol_polyline_2d.h>
00054 #include <vtol/vtol_vertex_2d.h>
00055 #include <vtol/vtol_vertex.h>
00056 #include <vtol/vtol_edge_2d.h>
00057 #include <vtol/vtol_intensity_face.h>
00058 #include <brip/brip_vil1_float_ops.h>
00059 #include <brip/brip_vil_float_ops.h>
00060 #include <brip/brip_para_cvrg_params.h>
00061 #include <brip/brip_para_cvrg.h>
00062 #include <brip/brip_watershed_params.h>
00063 #include <sdet/sdet_watershed_region_proc_params.h>
00064 #include <sdet/sdet_watershed_region_proc.h>
00065 #include <sdet/sdet_region_proc_params.h>
00066 #include <sdet/sdet_region_proc.h>
00067 #include <strk/strk_region_info_params.h>
00068 #include <strk/strk_region_info.h>
00069 #include <strk/strk_io.h>
00070 
00071 ;
00072 segv_misc_manager *segv_misc_manager::instance_ = 0;
00073 
00074 segv_misc_manager *segv_misc_manager::instance()
00075 {
00076   if (!instance_)
00077   {
00078     instance_ = new segv_misc_manager();
00079     instance_->init();
00080   }
00081   return segv_misc_manager::instance_;
00082 }
00083 
00084 //-----------------------------------------------------------
00085 // constructors/destructor
00086 //
00087 segv_misc_manager::segv_misc_manager():vgui_wrapper_tableau()
00088 {
00089   first_ = true;
00090 }
00091 
00092 segv_misc_manager::~segv_misc_manager()
00093 {
00094 }
00095 
00096 //: Set up the tableaux
00097 void segv_misc_manager::init()
00098 {
00099   bgui_image_tableau_sptr itab = bgui_image_tableau_new();
00100   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00101   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00102   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00103   grid_ = vgui_grid_tableau_new(1,1);
00104   grid_->set_grid_size_changeable(true);
00105   grid_->add_at(v2D, 0, 0);
00106   vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(grid_);
00107   this->add_child(shell);
00108   first_ = true;
00109 }
00110 
00111 //: Calculate the range parameters for the input image
00112 vgui_range_map_params_sptr segv_misc_manager::
00113 range_params(vil_image_resource_sptr const& image)
00114 {
00115   float gamma = 1.0;
00116   bool invert = false;
00117   bool gl_map = false;
00118   bool cache = true;
00119 
00120   //Check if the image is blocked
00121   vil_blocked_image_resource_sptr bir = blocked_image_resource(image);
00122   if (bir)
00123   { gl_map = true; cache = false; }
00124 
00125   //Check if the image is a pyramid
00126   bool pyr = image->get_property(vil_property_pyramid, 0);
00127   if (pyr)
00128   { gl_map = true; cache = false; }
00129   //Get max min parameters
00130 
00131   double min=0, max=0;
00132   unsigned n_components = image->nplanes();
00133   vgui_range_map_params_sptr rmps;
00134   if (n_components == 1)
00135   {
00136     bgui_image_utils iu(image);
00137     iu.range(min, max);
00138     rmps= new vgui_range_map_params(min, max, gamma, invert,
00139                                     gl_map, cache);
00140   }
00141   else if (n_components == 3)
00142   {
00143     min = 0; max = 255;//for now - ultimately need to compute color histogram
00144     rmps = new vgui_range_map_params(min, max, min, max, min, max,
00145                                      gamma, gamma, gamma, invert,
00146                                      gl_map, cache);
00147   }
00148   return rmps;
00149 }
00150 
00151 //: set the image at the currently selected grid cell
00152 void segv_misc_manager::
00153 set_selected_grid_image(vil_image_resource_sptr const& image,
00154                         vgui_range_map_params_sptr const& rmps)
00155 {
00156   bgui_image_tableau_sptr itab = this->selected_image_tab();
00157   if (!itab)
00158     this->add_image(image, rmps);
00159   else
00160   {
00161     itab->set_image_resource(image);
00162     itab->set_mapping(rmps);
00163   }
00164   itab->post_redraw();
00165 }
00166 
00167 //: Add an image at the specified grid cell
00168 void segv_misc_manager::
00169 add_image_at(vil_image_resource_sptr const& image,
00170              const unsigned col, const unsigned row,
00171              vgui_range_map_params_sptr const& rmps)
00172 {
00173   vgui_range_map_params_sptr rmap = rmps;
00174   if (!rmps)
00175     rmap = range_params(image);
00176   bgui_image_tableau_sptr itab = bgui_image_tableau_new(image);
00177   itab->set_mapping(rmap);
00178   bgui_vtol2D_tableau_sptr t2D = bgui_vtol2D_tableau_new(itab);
00179   bgui_picker_tableau_sptr picktab = bgui_picker_tableau_new(t2D);
00180   vgui_viewer2D_tableau_sptr v2D = vgui_viewer2D_tableau_new(picktab);
00181   grid_->add_at(v2D, col, row);
00182   itab->post_redraw();
00183 }
00184 
00185 //: Add an image to the currently selected grid cell
00186 void segv_misc_manager::
00187 add_image(vil_image_resource_sptr const& image,
00188           vgui_range_map_params_sptr const& rmps)
00189 {
00190   unsigned row=0, col=0;
00191   grid_->get_last_selected_position(&col, &row);
00192   this->add_image_at(image, col, row, rmps);
00193 }
00194 
00195 #if 0
00196 //-----------------------------------------------------------------------------
00197 //: remove the selected image
00198 //-----------------------------------------------------------------------------
00199 void segv_misc_manager::remove_image()
00200 {
00201   unsigned row=0, col=0;
00202   grid_->get_last_selected_position(&col, &row);
00203   grid_->remove_at(col, row);
00204 }
00205 
00206 void segv_misc_manager::convert_to_grey()
00207 {
00208   vil_image_resource_sptr img = this->selected_image();
00209   if (!img)
00210     return;
00211   vil_image_view<unsigned char> grey =
00212     brip_vil_float_ops::convert_to_byte(img);
00213   vil_image_resource_sptr gimg = vil_new_image_resource_of_view(grey);
00214   this->add_image(gimg);
00215 }
00216 #endif
00217 
00218 //: Get the image tableau for the currently selected grid cell
00219 bgui_image_tableau_sptr segv_misc_manager::selected_image_tab()
00220 {
00221   unsigned row=0, col=0;
00222   grid_->get_last_selected_position(&col, &row);
00223   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00224   if (top_tab)
00225   {
00226     bgui_image_tableau_sptr itab;
00227     itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00228                                                     vcl_string("vgui_image_tableau")));
00229     if (itab)
00230       return itab;
00231   }
00232   vcl_cout << "Unable to get bgui_image_tableau at (" << col
00233            << ", " << row << ")\n";
00234   return bgui_image_tableau_sptr();
00235 }
00236 
00237 //: Get the vtol2D tableau at the specified grid cell
00238 bgui_vtol2D_tableau_sptr
00239 segv_misc_manager::vtol2D_tab_at(const unsigned col,
00240                                  const unsigned row)
00241 {
00242   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00243   if (top_tab)
00244   {
00245     bgui_vtol2D_tableau_sptr v2D;
00246     v2D.vertical_cast(vgui_find_below_by_type_name(top_tab,
00247                                                    vcl_string("bgui_vtol2D_tableau")));
00248     if (v2D)
00249       return v2D;
00250   }
00251   vcl_cout << "Unable to get bgui_vtol2D_tableau at (" << col
00252            << ", " << row << ")\n";
00253   return bgui_vtol2D_tableau_sptr();
00254 }
00255 
00256 //: Get the vtol2D tableau for the currently selected grid cell
00257 bgui_vtol2D_tableau_sptr segv_misc_manager::selected_vtol2D_tab()
00258 {
00259   unsigned row=0, col=0;
00260   grid_->get_last_selected_position(&col, &row);
00261   return this->vtol2D_tab_at(col, row);
00262 }
00263 
00264 //: Get the picker tableau for the currently selected grid cell
00265 bgui_picker_tableau_sptr segv_misc_manager::selected_picker_tab()
00266 {
00267   unsigned row=0, col=0;
00268   grid_->get_last_selected_position(&col, &row);
00269   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00270   if (top_tab)
00271   {
00272     bgui_picker_tableau_sptr pick;
00273     pick.vertical_cast(vgui_find_below_by_type_name(top_tab,
00274                                                     vcl_string("bgui_picker_tableau")));
00275     if (pick)
00276       return pick;
00277   }
00278   vcl_cout << "Unable to get bgui_picker_tableau at (" << col
00279            << ", " << row << ")\n";
00280   return bgui_picker_tableau_sptr();
00281 }
00282 
00283 
00284 vil_image_resource_sptr segv_misc_manager::selected_image()
00285 {
00286   bgui_image_tableau_sptr itab = this->selected_image_tab();
00287   if (!itab)
00288     return 0;
00289   return itab->get_image_resource();
00290 }
00291 
00292 vil_image_resource_sptr segv_misc_manager::image_at(const unsigned col,
00293                                                     const unsigned row)
00294 {
00295   vgui_tableau_sptr top_tab = grid_->get_tableau_at(col, row);
00296   if (!top_tab)
00297     return 0;
00298 
00299   bgui_image_tableau_sptr itab;
00300   itab.vertical_cast(vgui_find_below_by_type_name(top_tab,
00301                                                   vcl_string("vgui_image_tableau")));
00302   if (!itab)
00303   {
00304     vcl_cout << "Unable to get bgui_image_tableau at (" << col
00305              << ", " << row << ")\n";
00306     return 0;
00307   }
00308   return itab->get_image_resource();
00309 }
00310 
00311 //-----------------------------------------------------------------------------
00312 //: Clear spatial objects from the selected display
00313 //-----------------------------------------------------------------------------
00314 void segv_misc_manager::clear_display()
00315 {
00316   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00317   if (!t2D)
00318     return;
00319   t2D->clear_all();
00320 }
00321 
00322 //-----------------------------------------------------------------------------
00323 //: Clear spatial objects from all spatial panes
00324 //-----------------------------------------------------------------------------
00325 void segv_misc_manager::clear_all()
00326 {
00327   unsigned ncols = grid_->cols(), nrows = grid_->rows();
00328   for (unsigned r=0; r<nrows; ++r)
00329     for (unsigned c=0; c<ncols; ++c)
00330     {
00331       bgui_vtol2D_tableau_sptr t = this->vtol2D_tab_at(c, r);
00332       if (t)
00333         t->clear_all();
00334     }
00335 }
00336 
00337 //-----------------------------------------------------------------------------
00338 //: Draw edges onto the tableau
00339 //-----------------------------------------------------------------------------
00340 void
00341 segv_misc_manager::draw_edges(vcl_vector<vtol_edge_2d_sptr>& edges,
00342                               bool verts)
00343 {
00344   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00345   if (!t2D)
00346     return;
00347   this->clear_display();
00348 #if 0
00349   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00350   if (!itab)
00351   {
00352     vcl_cout << "In segv_misc_manager::draw_edges - null image tab\n";
00353     return;
00354   }
00355 #endif
00356   for (vcl_vector<vtol_edge_2d_sptr>::iterator eit = edges.begin();
00357        eit != edges.end(); eit++)
00358   {
00359     t2D->add_edge(*eit);
00360     //optionally display the edge vertices
00361     if (verts)
00362     {
00363       if ((*eit)->v1())
00364       {
00365         vtol_vertex_2d_sptr v1 = (*eit)->v1()->cast_to_vertex_2d();
00366         t2D->add_vertex(v1);
00367       }
00368       if ((*eit)->v2())
00369       {
00370         vtol_vertex_2d_sptr v2 = (*eit)->v2()->cast_to_vertex_2d();
00371         t2D->add_vertex(v2);
00372       }
00373     }
00374   }
00375   t2D->post_redraw();
00376 }
00377 
00378 
00379 //-----------------------------------------------------------------------------
00380 //: Draw polylines on the tableau
00381 //-----------------------------------------------------------------------------
00382 void segv_misc_manager::
00383 draw_polylines(vcl_vector<vsol_polyline_2d_sptr > const& polys,
00384                vgui_style_sptr style)
00385 {
00386   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00387   if (!t2D)
00388     return;
00389   //this->clear_display();
00390 #if 0
00391   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00392   if (!itab)
00393   {
00394     vcl_cout << "In segv_misc_manager::draw_polylines - null image tab\n";
00395     return;
00396   }
00397 #endif
00398   for (vcl_vector<vsol_polyline_2d_sptr>::const_iterator pit = polys.begin();
00399        pit != polys.end(); pit++)
00400   {
00401     t2D->add_vsol_polyline_2d(*pit, style);
00402   }
00403 
00404   t2D->post_redraw();
00405 }
00406 
00407 //-----------------------------------------------------------------------------
00408 //: Draw line segments on the tableau
00409 //-----------------------------------------------------------------------------
00410 void segv_misc_manager::
00411 draw_lines(vcl_vector<vsol_line_2d_sptr > const& lines,
00412            const vgui_style_sptr& style)
00413 {
00414   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00415   if (!t2D)
00416     return;
00417   //this->clear_display();
00418 #if 0
00419   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00420   if (!itab)
00421   {
00422     vcl_cout << "In segv_misc_manager::draw_edges - null image tab\n";
00423     return;
00424   }
00425 #endif
00426   for (vcl_vector<vsol_line_2d_sptr>::const_iterator lit = lines.begin();
00427        lit != lines.end(); lit++)
00428   {
00429     t2D->add_vsol_line_2d(*lit,style);
00430   }
00431 
00432   t2D->post_redraw();
00433 }
00434 
00435 //-----------------------------------------------------------------------------
00436 //: Draw conic segments on the tableau
00437 //-----------------------------------------------------------------------------
00438 void segv_misc_manager::
00439 draw_conics(vcl_vector<vsol_conic_2d_sptr > const& conics,
00440             const vgui_style_sptr& style)
00441 {
00442   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00443   if (!t2D)
00444     return;
00445   //this->clear_display();
00446 #if 0
00447   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00448   if (!itab)
00449   {
00450     vcl_cout << "In segv_misc_manager::draw_edges - null image tab\n";
00451     return;
00452   }
00453 #endif
00454   for (vcl_vector<vsol_conic_2d_sptr>::const_iterator lit = conics.begin();
00455        lit != conics.end(); lit++)
00456   {
00457     t2D->add_vsol_conic_2d(*lit,style);
00458   }
00459   t2D->post_redraw();
00460 }
00461 
00462 //-----------------------------------------------------------------------------
00463 //: Draw points on the tableau
00464 //-----------------------------------------------------------------------------
00465 void segv_misc_manager::
00466 draw_points(vcl_vector<vsol_point_2d_sptr> const& points, const vgui_style_sptr& style)
00467 {
00468   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00469   if (!t2D)
00470     return;
00471   //this->clear_display();
00472 #if 0
00473   vgui_image_tableau_sptr itab = t2D->get_image_tableau();
00474   if (!itab)
00475   {
00476     vcl_cout << "In segv_misc_manager::draw_edges - null image tab\n";
00477     return;
00478   }
00479 #endif
00480   for (vcl_vector<vsol_point_2d_sptr>::const_iterator pit = points.begin();
00481        pit != points.end(); pit++)
00482   {
00483     t2D->add_vsol_point_2d(*pit,style);
00484   }
00485 
00486   t2D->post_redraw();
00487 }
00488 
00489 void segv_misc_manager::draw_regions(vcl_vector<vtol_intensity_face_sptr>& regions,
00490                                      bool verts)
00491 {
00492   bgui_vtol2D_tableau_sptr t2D = this->selected_vtol2D_tab();
00493   if (!t2D)
00494     return;
00495   for (vcl_vector<vtol_intensity_face_sptr>::iterator rit = regions.begin();
00496        rit != regions.end(); rit++)
00497   {
00498     vtol_face_2d_sptr f = (*rit)->cast_to_face_2d();
00499     t2D->add_face(f);
00500     if (verts)
00501     {
00502       vcl_vector<vtol_vertex_sptr> vts;
00503       f->vertices(vts);
00504       for (vcl_vector<vtol_vertex_sptr>::iterator vit = vts.begin();
00505            vit != vts.end(); vit++)
00506       {
00507         vtol_vertex_2d_sptr v = (*vit)->cast_to_vertex_2d();
00508         t2D->add_vertex(v);
00509       }
00510     }
00511   }
00512   t2D->post_redraw();
00513 }
00514 
00515 void segv_misc_manager::quit()
00516 {
00517   this->clear_all();
00518   vgui::quit();
00519 }
00520 
00521 void segv_misc_manager::load_image()
00522 {
00523   static bool greyscale = false;
00524   static bool sblock = false;
00525   vgui_dialog load_image_dlg("Load image file");
00526   static vcl_string image_filename = "/home/dec/images/cal_image1.tif";
00527   static vcl_string ext = "*.*";
00528   load_image_dlg.file("Image Filename:", ext, image_filename);
00529   load_image_dlg.checkbox("greyscale ", greyscale);
00530   load_image_dlg.checkbox("blocked?:", sblock);
00531   if (!load_image_dlg.ask())
00532     return;
00533   //first check to see if the filename is a directory
00534   //if so, then assume a pyramid image
00535   bool pyrm = false;
00536   vil_image_resource_sptr image;
00537   if (vul_file::is_directory(image_filename.c_str()))
00538   {
00539     vil_pyramid_image_resource_sptr pyr =
00540       vil_load_pyramid_resource(image_filename.c_str());
00541     if (pyr)
00542     {
00543       image = pyr.ptr();
00544       pyrm = true;
00545     }
00546   }
00547   if (!image)
00548     image = vil_load_image_resource(image_filename.c_str());
00549 
00550   if (!image)
00551     return;
00552 
00553   if (greyscale&&!pyrm)
00554   {
00555     vil_image_view<unsigned char> grey_view =
00556       brip_vil_float_ops::convert_to_grey(*image);
00557     image = vil_new_image_resource_of_view(grey_view);
00558   }
00559 
00560   if (sblock&&!pyrm)
00561   {
00562     vil_blocked_image_resource_sptr bimage = vil_new_blocked_image_facade(image);
00563     image = (vil_image_resource*)(vil_new_cached_image_resource(bimage)).ptr();
00564   }
00565 
00566   vgui_range_map_params_sptr rmps = range_params(image);
00567 
00568   if (first_)
00569   {
00570     this->set_selected_grid_image(image, rmps);
00571     first_ = false;
00572   }
00573   else
00574     this->add_image(image, rmps);
00575 }
00576 
00577 void segv_misc_manager::save_image()
00578 {
00579   vgui_dialog file_dialog("Save Image");
00580   static vcl_string image_file;
00581   static vcl_string ext = "tif";
00582   static vcl_string type = "tiff";
00583   static unsigned size_block = 0;
00584   static bool byte = false;
00585   file_dialog.file("Image Filename:", ext, image_file);
00586   file_dialog.field("Image Format: ", type);
00587   file_dialog.field("BlockSize", size_block);
00588   file_dialog.checkbox("Convert to byte image", byte);
00589   if (!file_dialog.ask())
00590     return;
00591   vil_image_resource_sptr img = this->selected_image();
00592   if (!img)
00593   {
00594     vcl_cerr << "Null image in segv_misc_manager::save_image\n";
00595     return;
00596   }
00597   vil_image_resource_sptr save_image = img;
00598   if (byte)
00599   {
00600     vil_image_view<unsigned char> byte_view = brip_vil_float_ops::convert_to_byte(img);
00601     save_image = vil_new_image_resource_of_view(byte_view);
00602   }
00603   if (size_block>0)
00604   {
00605     vil_blocked_image_resource_sptr bim =
00606       vil_new_blocked_image_resource(image_file.c_str(),
00607                                      save_image->ni(), save_image->nj(),
00608                                      save_image->nplanes(),
00609                                      save_image->pixel_format(),
00610                                      size_block, size_block,
00611                                      "tiff");
00612     vil_image_view_base_sptr view = save_image->get_view();
00613     if (view)
00614       bim->vil_image_resource::put_view(*view);
00615     return;
00616   }
00617 
00618   if (!vil_save_image_resource(save_image, image_file.c_str(), type.c_str()))
00619     vcl_cerr << "segv_misc_manager::save_image operation failed\n";
00620 }
00621 
00622 #if 0
00623 void segv_misc_manager::set_range_params()
00624 {
00625   bgui_image_tableau_sptr itab = this->selected_image_tab();
00626   if (!itab)
00627     return;
00628   vgui_range_map_params_sptr rmps = itab->map_params();
00629   if (!rmps)
00630   {
00631     vil_image_resource_sptr img = itab->get_image_resource();
00632     if (!img)
00633       return;
00634     rmps = range_params(img);
00635     if (!rmps)
00636       return;
00637   }
00638   unsigned nc = rmps->n_components_;
00639   static double min = static_cast<double>(rmps->min_L_),
00640     max = static_cast<double>(rmps->max_L_);
00641   static float gamma = rmps->gamma_L_;
00642   static bool invert = rmps->invert_;
00643   static bool gl_map = rmps->use_glPixelMap_;
00644   static bool cache = rmps->cache_mapped_pix_;
00645   if (nc==3)
00646   {
00647     min = static_cast<double>(rmps->min_R_);
00648     max = static_cast<double>(rmps->max_R_);
00649     gamma = rmps->gamma_R_;
00650   }
00651   vgui_dialog range_dlg("Set Range Map Params");
00652   range_dlg.field("Range min:", min);
00653   range_dlg.field("Range max:", max);
00654   range_dlg.field("Gamma:", gamma);
00655   range_dlg.checkbox("Invert:", invert);
00656   range_dlg.checkbox("Use GL Mapping", gl_map);
00657   range_dlg.checkbox("Cache Pixels", cache);
00658   if (!range_dlg.ask())
00659     return;
00660   if (nc==1)
00661     rmps= new vgui_range_map_params(min, max, gamma, invert,
00662                                     gl_map, cache);
00663   else if (nc == 3)
00664     rmps = new vgui_range_map_params(min, max, min, max, min, max,
00665                                      gamma, gamma, gamma, invert,
00666                                      gl_map, cache);
00667   else
00668     rmps = 0;
00669   itab->set_mapping(rmps);
00670 }
00671 #endif
00672 
00673 //: Transform a polyline according to a rotation and scale in the plane.
00674 static vsol_polyline_2d_sptr
00675 trans_poly(const double alpha,
00676            const double theta,
00677            vsol_polyline_2d_sptr const & base_poly)
00678 {
00679   if (!base_poly)
00680     return 0;
00681   double e = vnl_math::e;
00682   double scale = vcl_pow(e, alpha);
00683   vcl_vector<vsol_point_2d_sptr> verts, trans_verts;
00684   for (unsigned i=0; i<base_poly->size(); ++i)
00685   {
00686     vsol_point_2d_sptr bv = base_poly->vertex(i);
00687     double x = bv->x(), y = bv->y();
00688     double sx = x*scale, sy = y*scale;
00689     double rsx = sx*vcl_cos(theta)-sy*vcl_sin(theta);
00690     double rsy = sx*vcl_sin(theta)+sy*vcl_cos(theta);
00691     vsol_point_2d_sptr tv = new vsol_point_2d(rsx, rsy);
00692     trans_verts.push_back(tv);
00693   }
00694   return new vsol_polyline_2d(trans_verts);
00695 }
00696 
00697 //:Generate a specific polyline as 3 sides of a square
00698 static vsol_polyline_2d_sptr base_poly()
00699 {
00700   vsol_point_2d_sptr p0 = new vsol_point_2d(-100,-100);
00701   vsol_point_2d_sptr p1 = new vsol_point_2d(-100,+100);
00702   vsol_point_2d_sptr p2 = new vsol_point_2d(+100,+100);
00703   vsol_point_2d_sptr p3 = new vsol_point_2d(+100,-100);
00704   vcl_vector<vsol_point_2d_sptr> verts;
00705   verts.push_back(p3);   verts.push_back(p2);
00706   verts.push_back(p1);   verts.push_back(p0);
00707   return new vsol_polyline_2d(verts);
00708 }
00709 
00710 //:optimum projection onto a one-dimensional sub-manifold of rotation and scale.
00711 // \a alpha_hat and \a theta_hat are the parameters of the
00712 // 1-d sub-manifold, \a alpha = t*alpha_hat, \a theta = t*theta_hat.
00713 static double topt(const double alpha_hat, const double theta_hat,
00714                    const double alpha, const double theta)
00715 {
00716   double t = alpha_hat*alpha + theta_hat*theta;
00717   t /= (alpha_hat*alpha_hat + theta_hat*theta_hat);
00718   return t;
00719 }
00720 
00721 // Experiment with projecting a shape onto a manifold
00722 void segv_misc_manager::project_on_subm()
00723 {
00724   this->clear_display();
00725   vgui_style_sptr mstyle = vgui_style::new_style(0.5f, 1.0f, 0.25f,
00726                                                1.0f, 5.0f);
00727   vgui_style_sptr pstyle = vgui_style::new_style(1.0f, 0.5f, 0.1f,
00728                                                1.0f, 5.0f);
00729     static double theta_hat = 0.25*vnl_math::pi;
00730   static double alpha_hat = 1.0;
00731   static double theta = 0.3*vnl_math::pi;
00732   static double alpha = 1.5;
00733   static double tinc = 0.1;
00734   static double tmax = 3.0;
00735   vgui_dialog project_dlg("Projection Parameters");
00736   project_dlg.field("Theta hat:", theta_hat);
00737   project_dlg.field("Alpha hat:", alpha_hat);
00738   project_dlg.field("tinc:", tinc);
00739   project_dlg.field("tmax:", tmax);
00740   project_dlg.field("Theta:", theta );
00741   project_dlg.field("Alpha:", alpha);
00742   if (!project_dlg.ask())
00743     return;
00744   vcl_vector<vsol_polyline_2d_sptr> tpolys;
00745   vsol_polyline_2d_sptr base = base_poly();
00746   vsol_polyline_2d_sptr tbase = trans_poly(alpha,theta,base);
00747   tpolys.push_back(tbase);
00748   this->draw_polylines(tpolys,mstyle);
00749   tpolys.clear();
00750   double to = topt(alpha_hat, theta_hat, alpha, theta);
00751   double alp = to*alpha_hat;
00752   double thet = to*theta_hat;
00753   vsol_polyline_2d_sptr popt = trans_poly(alp, thet, base);
00754   tpolys.push_back(popt);
00755   this->draw_polylines(tpolys,pstyle);
00756   tpolys.clear();
00757   for (double t = 0; t<=tmax; t+=tinc)
00758   {
00759     alp = t*alpha_hat;
00760     thet = t*theta_hat;
00761     vsol_polyline_2d_sptr p = trans_poly(alp, thet, base);
00762     tpolys.push_back(p);
00763   }
00764   this->draw_polylines(tpolys);
00765 }
00766 
00767 void segv_misc_manager::project_on_articulation()
00768 {
00769   this->clear_display();
00770   vgui_style_sptr mstyle = vgui_style::new_style(0.5f, 1.0f, 0.25f,
00771                                                1.0f, 5.0f);
00772   vgui_style_sptr pstyle = vgui_style::new_style(1.0f, 0.5f, 0.1f,
00773                                                1.0f, 5.0f);
00774   unsigned njoints = 4;
00775   static vcl_vector<double> joint_trans(njoints, 0);
00776   static double basis_angle = 0.25*vnl_math::pi;
00777   joint_trans[0] = 0.25*vnl_math::pi;
00778   joint_trans[1] = 0.25*vnl_math::pi;
00779   joint_trans[2] = -0.25*vnl_math::pi;
00780   vgui_dialog project_dlg("Articulation Projection Parameters");
00781   project_dlg.field("Ang 0:", joint_trans[0]);
00782   project_dlg.field("Ang 1:", joint_trans[1]);
00783   project_dlg.field("Ang 2:", joint_trans[2]);
00784   project_dlg.field("Basis Angle:", basis_angle);
00785   if (!project_dlg.ask())
00786     return;
00787   vcl_vector<double> links(njoints-1, 100.0);
00788   bvgl_articulated_poly_sptr ap = new bvgl_articulated_poly(njoints, links);
00789   ap->transform(joint_trans);
00790   ap->print();
00791 
00792   vcl_vector<vsol_polyline_2d_sptr> tpolys;
00793 
00794   vcl_vector<double> basis(njoints, basis_angle);
00795   for (double t = - 2.5; t<=2.5; t+=0.25)
00796   {
00797     bvgl_articulated_poly_sptr manifold =
00798       new bvgl_articulated_poly(njoints,links);
00799     manifold->sub_manifold_transform(t, basis);
00800     tpolys.push_back((vsol_polyline_2d*)(manifold.ptr()));
00801   }
00802   this->draw_polylines(tpolys);
00803   tpolys.clear();
00804   tpolys.push_back((vsol_polyline_2d*)(ap.ptr()));
00805   this->draw_polylines(tpolys, mstyle);
00806   //get the projection
00807   bvgl_articulated_poly_sptr proj =
00808     bvgl_articulated_poly::projection(ap, basis);
00809   tpolys.clear();
00810   tpolys.push_back((vsol_polyline_2d*)(proj.ptr()));
00811   this->draw_polylines(tpolys, pstyle);
00812 }