00001
00002
00003
00004
00005 #include "vsrl_point_picker.h"
00006 #include <vgui/vgui.h>
00007 #include <vgui/vgui_event.h>
00008 #include <vgui/vgui_find.h>
00009 #include <vgui/vgui_projection_inspector.h>
00010 #include <vil1/vil1_image.h>
00011 #include <vil1/vil1_pixel.h>
00012 #include <vxl_config.h>
00013
00014
00015 vsrl_point_picker::vsrl_point_picker( vgui_tableau_sptr child)
00016 : vgui_wrapper_tableau( child)
00017 , point_(0.f,0.f)
00018 {
00019 }
00020
00021 bool vsrl_point_picker::handle( vgui_event const &e)
00022 {
00023
00024
00025 vgui_modifier m = e.modifier;
00026
00027 if (e.type== vgui_BUTTON_DOWN)
00028 {
00029
00030 vgui_projection_inspector pi;
00031 float ix, iy;
00032 pi.window_to_image_coordinates( e.wx, e.wy, ix, iy);
00033 point_.set(ix,iy);
00034
00035 if ( (e.button== vgui_LEFT) && !m)
00036 {
00037 put_point(ix,iy);
00038 vgui::out << '(' << ix << ' ' << iy << ")\n";
00039 }
00040 else if (e.button== vgui_LEFT && (m & vgui_SHIFT))
00041 {
00042 put_H_line(ix,iy);
00043 vgui::out << '(' << ix << ' ' << iy << ")\n";
00044 }
00045 else if (e.button== vgui_LEFT && (m & vgui_ALT))
00046 {
00047 put_H_line(ix,iy);
00048 vgui::out << '(' << ix << ' ' << iy << ") ALT!\n";
00049 }
00050 else if (e.button== vgui_MIDDLE && !e.modifier)
00051 {
00052 int value = this->get_value(ix,iy);
00053 vgui::out << '(' << ix << ' ' << iy << ") I= " << value << "\n";
00054 }
00055 else
00056 {
00057 vgui::out << '(' << ix << ' ' << iy << ")\n";
00058 }
00059 this->post_redraw();
00060 }
00061
00062
00063 return child && child->handle( e);
00064 }
00065
00066 vgui_easy2D_tableau_sptr
00067 vsrl_point_picker::get_easy2D_pointer(vgui_tableau_sptr const& tab)
00068 {
00069 vgui_easy2D_tableau_sptr e2d_ptr;
00070 if (tab)
00071 e2d_ptr.vertical_cast(vgui_find_below_by_type_name(tab,
00072 vcl_string("vgui_easy2D_tableau")));
00073 return e2d_ptr;
00074 }
00075
00076 vgui_image_tableau_sptr
00077 vsrl_point_picker::get_image_tab_pointer(vgui_tableau_sptr const& tab)
00078 {
00079 vgui_image_tableau_sptr img_tab_ptr;
00080 if (tab)
00081 img_tab_ptr.vertical_cast(vgui_find_below_by_type_name(tab,
00082 vcl_string("vgui_image_tableau")));
00083 return img_tab_ptr;
00084 }
00085
00086
00087 vgl_point_2d<float> vsrl_point_picker::put_point(float x, float y)
00088 {
00089
00090 vgui_easy2D_tableau_sptr e2d = get_easy2D_pointer(this);
00091
00092
00093 e2d->add_point(x,y);
00094
00095 this->post_redraw();
00096
00097
00098 return point_;
00099 }
00100
00101 vgl_point_2d<float> vsrl_point_picker::put_H_line(float x, float y)
00102 {
00103
00104 vgui_easy2D_tableau_sptr e2d = get_easy2D_pointer(this);
00105 vgui_image_tableau_sptr img_tab = get_image_tab_pointer(this);
00106 vil1_image img = img_tab->get_image();
00107 int xmin = 0;
00108 int xmax = img.cols()-1;
00109
00110
00111 e2d->add_line(xmin,y,xmax,y);
00112
00113 point_.set(x,y);
00114
00115 this->post_redraw();
00116
00117
00118 return point_;
00119 }
00120
00121 int vsrl_point_picker::get_value(float x, float y)
00122 {
00123 vgui_image_tableau_sptr img_tab = get_image_tab_pointer(this);
00124 vil1_image img = img_tab->get_image();
00125 switch ( vil1_pixel_format(img) )
00126 {
00127 case VIL1_BYTE: { vxl_byte value=0;
00128 img.get_section(&value,int(x),int(y),1,1); return int(value); }
00129 case VIL1_UINT16: { vxl_uint_16 value=0;
00130 img.get_section(&value,int(x),int(y),1,1); return int(value); }
00131 case VIL1_UINT32: { vxl_uint_32 value=0;
00132 img.get_section(&value,int(x),int(y),1,1); return int(value); }
00133 default:
00134 vcl_cerr << "vsrl_point_picker::get_value() not implemented for this pixel type\n";
00135 return -1;
00136 }
00137 }