1 // This is core/vgui/vgui_roi_tableau.cxx
2 //:
3 // \file
4 // \author Marko Bacic, RRG, University of Oxford
5 // \date   18 Jul 2000
6 // \brief  See vgui_roi_tableau.h for a description of this file.
7 
8 #include <string>
9 #include "vgui_roi_tableau.h"
10 #ifdef _MSC_VER
11 #  include "vcl_msvc_warnings.h"
12 #endif
13 
14 #include "vil1/vil1_load.h"
15 #include "vil1/vil1_crop.h"
16 
17 #include "vgui/vgui_event.h"
18 #include "vgui/vgui_matrix_state.h"
19 #include "vgui/vgui_gl.h"
20 #include "vgui/vgui_utils.h"
21 #include "vgui/vgui_glu.h"
22 
23 //------------------------------------------------------------------------------
24 
vgui_roi_tableau()25 vgui_roi_tableau::vgui_roi_tableau()
26   : vgui_tableau()
27 {
28   cropped_image_ = nullptr;
29 }
30 
vgui_roi_tableau(vil1_image const & I,char const * t_name,float x,float y,float w,float h)31 vgui_roi_tableau::vgui_roi_tableau(vil1_image const & I, char const * t_name, float x, float y, float w, float h)
32   : vgui_tableau()
33   , name_(t_name)
34 {
35   cropped_image_ = vil1_crop(I, int(x + 0.5), int(y + 0.5), int(w + 0.5), int(h + 0.5));
36   roi_.sx = x;
37   roi_.sy = y;
38   roi_.width = w;
39   roi_.height = h;
40 }
41 
~vgui_roi_tableau()42 vgui_roi_tableau::~vgui_roi_tableau() {}
43 
44 std::string
type_name() const45 vgui_roi_tableau::type_name() const
46 {
47   return "vgui_roi_tableau";
48 }
49 
50 
51 std::string
file_name() const52 vgui_roi_tableau::file_name() const
53 {
54   return name_;
55 }
56 
57 std::string
pretty_name() const58 vgui_roi_tableau::pretty_name() const
59 {
60   return type_name() + "[" + name_ + "]";
61 }
62 
63 //------------------------------------------------------------------------------
64 
65 vil1_image
get_image() const66 vgui_roi_tableau::get_image() const
67 {
68   return cropped_image_;
69 }
70 #if 0
71 // this removes the directory part of a filename :
72 static inline std::string __FILE__rem_dir(const char *s)
73 {
74   char const *slash = std::strrchr(s,'/');
75   return slash ? slash+1 : s;
76 }
77 #endif
78 
79 void
set_image(vil1_image const & I)80 vgui_roi_tableau::set_image(vil1_image const & I)
81 {
82   //  // use the name of the image as the name of the tableau :
83   //  name_ = __FILE__rem_dir(I.name().c_str());
84   cropped_image_ = vil1_crop(I, int(roi_.sx), int(roi_.sy), int(roi_.width), int(roi_.height));
85 }
86 
87 // derived :
88 void
set_image(char const * f)89 vgui_roi_tableau::set_image(char const * f)
90 {
91   set_image(vil1_load(f ? f : "az32_10.tif"));
92 }
93 
94 //------------------------------------------------------------------------------
95 
96 unsigned
width() const97 vgui_roi_tableau::width() const
98 {
99   return cropped_image_.width();
100 }
101 
102 unsigned
height() const103 vgui_roi_tableau::height() const
104 {
105   return cropped_image_.height();
106 }
107 
108 bool
get_bounding_box(float low[3],float high[3]) const109 vgui_roi_tableau::get_bounding_box(float low[3], float high[3]) const
110 {
111   low[0] = 0;
112   high[0] = width();
113   low[1] = 0;
114   high[1] = height();
115   low[2] = 0;
116   high[2] = 0; // why not ?
117   return true;
118 }
119 
120 //------------------------------------------------------------------------------
121 
122 bool
handle(vgui_event const & e)123 vgui_roi_tableau::handle(vgui_event const & e)
124 {
125   // if GL matrices are zero, set them to something sensible :
126   if (vgui_matrix_state::gl_matrices_are_cleared())
127   {
128     GLint vp[4];
129     vgui_utils::get_glViewport(vp);
130     int wdth = vp[2];
131     int hght = vp[3];
132 
133     glMatrixMode(GL_PROJECTION);
134     glLoadIdentity();
135     gluOrtho2D(0, wdth, hght, 0);
136 
137     glMatrixMode(GL_MODELVIEW);
138     glLoadIdentity();
139   }
140 
141   //
142   if (e.type == vgui_DRAW)
143   {
144     // ROI tableau will have only one child
145     get_child(0)->draw();
146     // Draw a region of interest
147     glBegin(GL_LINE_LOOP);
148     glVertex2f(roi_.sx, roi_.sy);
149     glVertex2f(roi_.sx + roi_.width, roi_.sy);
150     glVertex2f(roi_.sx + roi_.width, roi_.sy + roi_.height);
151     glVertex2f(roi_.sx, roi_.sy + roi_.height);
152     glEnd();
153 
154     return true;
155   }
156 
157   //
158   else
159     return get_child(0)->handle(e);
160 }
161 
162 //------------------------------------------------------------------------------
163