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