00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "Robots/LoBot/ui/LoLaserViz.H"
00048
00049 #include "Robots/LoBot/config/LoConfigHelpers.H"
00050
00051 #include "Robots/LoBot/misc/LoExcept.H"
00052 #include "Robots/LoBot/misc/LoTypes.H"
00053 #include "Robots/LoBot/misc/factory.hh"
00054 #include "Robots/LoBot/util/LoMath.H"
00055
00056
00057 #ifdef INVT_HAVE_LIBGL
00058 #include <GL/gl.h>
00059 #endif
00060
00061
00062
00063 namespace lobot {
00064
00065
00066
00067 LaserViz::LaserViz(const LaserRangeFinder* lrf)
00068 : Drawable("laser_viz", Params::geometry()),
00069 m_lrf(lrf),
00070 m_canvas(new GLCanvas()),
00071 m_markings(factory<LaserWindowMarkings>::create(Params::markings_type()))
00072 {
00073 if (! lrf)
00074 throw misc_error(LASER_RANGE_FINDER_MISSING) ;
00075
00076 m_markings->use_canvas(m_canvas.get()) ;
00077 m_markings->set_maximum(m_lrf->get_distance_range().max()) ;
00078 }
00079
00080 void LaserViz::gl_init()
00081 {
00082 const int M = m_lrf->get_distance_range().max() ;
00083 m_canvas->set_window(-M, M, -M, M) ;
00084 }
00085
00086
00087
00088 #ifdef INVT_HAVE_LIBGL
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 static void draw_rays(const LaserRangeFinder* lrf,
00100 const range<int>& angles, int step, const GLColor& color)
00101 {
00102 glPushAttrib(GL_COLOR_BUFFER_BIT) ;
00103 glBegin(GL_LINES) ;
00104 glColor3fv(color.rgb()) ;
00105
00106 for (float angle = angles.min(); angle <= angles.max(); angle += step)
00107 {
00108 int D = lrf->get_distance(static_cast<int>(angle)) ;
00109 if (D < 0)
00110 continue ;
00111 glVertex2i(0, 0) ;
00112 glVertex2f(D * cos(angle), D * sin(angle)) ;
00113 }
00114
00115
00116
00117 int D = lrf->get_distance(0) ;
00118 if (D > 0) {
00119 glVertex2i(0, 0) ;
00120 glVertex2i(0, D) ;
00121 }
00122 glEnd() ;
00123 glPopAttrib() ;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133 static void draw_hull(const LaserRangeFinder* lrf,
00134 const range<int>& angles, int step, const GLColor& color)
00135 {
00136 glPushAttrib(GL_COLOR_BUFFER_BIT) ;
00137 glBegin(GL_LINE_LOOP) ;
00138 glColor3fv(color.rgb()) ;
00139
00140 glVertex2i(0, 0) ;
00141 for (float angle = angles.min(); angle <= angles.max(); angle += step)
00142 {
00143 int D = lrf->get_distance(static_cast<int>(angle)) ;
00144 if (D < 0)
00145 continue ;
00146 glVertex2f(D * cos(angle), D * sin(angle)) ;
00147 }
00148 glEnd() ;
00149 glPopAttrib() ;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 static void draw_lrf(float R, const GLColor& color)
00159 {
00160 glPushAttrib(GL_COLOR_BUFFER_BIT) ;
00161 glBegin(GL_TRIANGLES) ;
00162 glColor3fv(color.rgb()) ;
00163
00164
00165 glVertex2f(R, 0) ;
00166 glVertex2f(0, R) ;
00167 glVertex2f(0, -R) ;
00168
00169
00170 glVertex2f( 0, R) ;
00171 glVertex2f(-R, R) ;
00172 glVertex2f( 0, -R) ;
00173 glVertex2f(-R, R) ;
00174 glVertex2f(-R, -R) ;
00175 glVertex2f( 0, -R) ;
00176 glEnd() ;
00177 glPopAttrib() ;
00178 }
00179
00180
00181
00182 void LaserViz::render_me()
00183 {
00184 glPushMatrix() ;
00185 glRotatef(Params::lrf_direction(), 0, 0, 1) ;
00186 m_markings->render() ;
00187 if (Params::draw_rays())
00188 draw_rays(m_lrf, Params::angles_range(), Params::angles_step(),
00189 Params::measurements_color()) ;
00190 else
00191 draw_hull(m_lrf, Params::angles_range(), Params::angles_step(),
00192 Params::measurements_color()) ;
00193 draw_lrf(Params::lrf_size(), Params::lrf_color()) ;
00194 glPopMatrix() ;
00195 }
00196
00197 #endif // INVT_HAVE_LIBGL
00198
00199
00200
00201 void LaserViz::zoom_by(float dz)
00202 {
00203 m_canvas->zoom_by(dz) ;
00204 }
00205
00206 void LaserViz::pan(int cx, int cy, int px, int py)
00207 {
00208 double curr_x, curr_y ;
00209 m_canvas->screen_to_world(cx, cy, &curr_x, &curr_y) ;
00210
00211 double prev_x, prev_y ;
00212 m_canvas->screen_to_world(px, py, &prev_x, &prev_y) ;
00213
00214 const float dx = static_cast<float>(curr_x - prev_x) ;
00215 const float dy = static_cast<float>(curr_y - prev_y) ;
00216 m_canvas->pan(-dx, -dy) ;
00217 }
00218
00219 void LaserViz::reset_zoom_pan()
00220 {
00221 m_canvas->reset_zoom_pan() ;
00222 }
00223
00224
00225
00226 void LaserViz::gl_cleanup()
00227 {
00228
00229
00230
00231
00232
00233 m_markings.reset(0) ;
00234
00235
00236 m_canvas.reset(0) ;
00237 }
00238
00239 LaserViz::~LaserViz(){}
00240
00241
00242
00243
00244 template<typename T>
00245 static inline T viz_conf(const std::string& key, const T& default_value)
00246 {
00247 return get_conf<T>("laser_viz", key, default_value) ;
00248 }
00249
00250
00251 template<typename T>
00252 static inline range<T>
00253 viz_conf(const std::string& key, const range<T>& default_value)
00254 {
00255 return get_conf<T>("laser_viz", key, default_value) ;
00256 }
00257
00258
00259 template<typename T>
00260 static inline triple<T, T, T>
00261 viz_conf(const std::string& key, const triple<T, T, T>& default_value)
00262 {
00263 return get_conf<T>("laser_viz", key, default_value) ;
00264 }
00265
00266
00267 template<typename T>
00268 static inline T markings_conf(const std::string& key, const T& default_value)
00269 {
00270 return get_conf<T>("markings", key, default_value) ;
00271 }
00272
00273
00274 LaserViz::Params::Params()
00275 : m_geometry(viz_conf<std::string>("geometry", "0 0 480 480")),
00276 m_measurements_style(downstring(
00277 viz_conf<std::string>("measurements_style", "rays"))),
00278 m_markings_type(downstring(markings_conf<std::string>("type", "rings"))),
00279 m_angles_range(viz_conf<int>("angles_range", make_range(-119, 135))),
00280 m_angles_step(clamp(viz_conf("angles_step", 5), 1, 30)),
00281 m_measurements_color(viz_conf<int>("measurements_color",
00282 make_triple(0, 128, 128))),
00283 m_lrf_size(clamp(viz_conf("lrf_size", 100.0f), 10.0f, 250.0f)),
00284 m_lrf_direction(clamp_angle(viz_conf("lrf_direction", 90.0f))),
00285 m_lrf_color(viz_conf<int>("lrf_color", make_triple(242, 13, 26)))
00286 {}
00287
00288
00289 LaserViz::Params::~Params(){}
00290
00291
00292
00293 }
00294
00295
00296
00297
00298