Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
image_viewer.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_VISUALIZATION_IMAGE_VISUALIZER_H__
39 #define PCL_VISUALIZATION_IMAGE_VISUALIZER_H__
40 
41 #include <boost/shared_array.hpp>
42 #include <pcl/pcl_macros.h>
43 #include <pcl/console/print.h>
44 #include <boost/signals2.hpp>
46 #include <pcl/visualization/vtk.h>
49 
50 namespace pcl
51 {
52  namespace visualization
53  {
54  typedef Eigen::Array<unsigned char, 3, 1> Vector3ub;
55  static const Vector3ub green_color (0, 255, 0);
56  static const Vector3ub red_color (255, 0, 0);
57  static const Vector3ub blue_color (0, 0, 255);
58 
81  {
82  public:
86  ImageViewer (const std::string& window_title = "");
87 
89  virtual ~ImageViewer ();
90 
98  void
99  showMonoImage (const unsigned char* data, unsigned width, unsigned height,
100  const std::string &layer_id = "mono_image", double opacity = 1.0);
101 
109  void
110  addMonoImage (const unsigned char* data, unsigned width, unsigned height,
111  const std::string &layer_id = "mono_image", double opacity = 1.0);
112 
120  void
121  showRGBImage (const unsigned char* data, unsigned width, unsigned height,
122  const std::string &layer_id = "rgb_image", double opacity = 1.0);
123 
131  void
132  addRGBImage (const unsigned char* data, unsigned width, unsigned height,
133  const std::string &layer_id = "rgb_image", double opacity = 1.0);
134 
140  template <typename T> inline void
141  showRGBImage (const typename pcl::PointCloud<T>::ConstPtr &cloud,
142  const std::string &layer_id = "rgb_image", double opacity = 1.0)
143  {
144  return (showRGBImage<T> (*cloud, layer_id, opacity));
145  }
146 
152  template <typename T> inline void
153  addRGBImage (const typename pcl::PointCloud<T>::ConstPtr &cloud,
154  const std::string &layer_id = "rgb_image", double opacity = 1.0)
155  {
156  return (addRGBImage<T> (*cloud, layer_id, opacity));
157  }
158 
164  template <typename T> void
165  showRGBImage (const pcl::PointCloud<T> &cloud,
166  const std::string &layer_id = "rgb_image", double opacity = 1.0);
167 
173  template <typename T> void
174  addRGBImage (const pcl::PointCloud<T> &cloud,
175  const std::string &layer_id = "rgb_image", double opacity = 1.0);
176 
187  void
188  showFloatImage (const float* data, unsigned int width, unsigned int height,
189  float min_value = std::numeric_limits<float>::min (),
190  float max_value = std::numeric_limits<float>::max (), bool grayscale = false,
191  const std::string &layer_id = "float_image", double opacity = 1.0);
192 
203  void
204  addFloatImage (const float* data, unsigned int width, unsigned int height,
205  float min_value = std::numeric_limits<float>::min (),
206  float max_value = std::numeric_limits<float>::max (), bool grayscale = false,
207  const std::string &layer_id = "float_image", double opacity = 1.0);
208 
219  void
220  showShortImage (const unsigned short* short_image, unsigned int width, unsigned int height,
221  unsigned short min_value = std::numeric_limits<unsigned short>::min (),
222  unsigned short max_value = std::numeric_limits<unsigned short>::max (), bool grayscale = false,
223  const std::string &layer_id = "short_image", double opacity = 1.0);
224 
235  void
236  addShortImage (const unsigned short* short_image, unsigned int width, unsigned int height,
237  unsigned short min_value = std::numeric_limits<unsigned short>::min (),
238  unsigned short max_value = std::numeric_limits<unsigned short>::max (), bool grayscale = false,
239  const std::string &layer_id = "short_image", double opacity = 1.0);
240 
248  void
249  showAngleImage (const float* data, unsigned width, unsigned height,
250  const std::string &layer_id = "angle_image", double opacity = 1.0);
251 
259  void
260  addAngleImage (const float* data, unsigned width, unsigned height,
261  const std::string &layer_id = "angle_image", double opacity = 1.0);
262 
270  void
271  showHalfAngleImage (const float* data, unsigned width, unsigned height,
272  const std::string &layer_id = "half_angle_image", double opacity = 1.0);
273 
281  void
282  addHalfAngleImage (const float* data, unsigned width, unsigned height,
283  const std::string &layer_id = "half_angle_image", double opacity = 1.0);
284 
294  void
295  markPoint (size_t u, size_t v, Vector3ub fg_color, Vector3ub bg_color = red_color, double radius = 3.0,
296  const std::string &layer_id = "points", double opacity = 1.0);
297 
301  void
302  setWindowTitle (const std::string& name)
303  {
304  image_viewer_->GetRenderWindow ()->SetWindowName (name.c_str ());
305  }
306 
308  void
309  spin ();
310 
316  void
317  spinOnce (int time = 1, bool force_redraw = true);
318 
324  boost::signals2::connection
325  registerKeyboardCallback (void (*callback) (const pcl::visualization::KeyboardEvent&, void*),
326  void* cookie = NULL)
327  {
328  return (registerKeyboardCallback (boost::bind (callback, _1, cookie)));
329  }
330 
337  template<typename T> boost::signals2::connection
338  registerKeyboardCallback (void (T::*callback) (const pcl::visualization::KeyboardEvent&, void*),
339  T& instance, void* cookie = NULL)
340  {
341  return (registerKeyboardCallback (boost::bind (callback, boost::ref (instance), _1, cookie)));
342  }
343 
348  boost::signals2::connection
349  registerKeyboardCallback (boost::function<void (const pcl::visualization::KeyboardEvent&)> cb);
350 
356  boost::signals2::connection
357  registerMouseCallback (void (*callback) (const pcl::visualization::MouseEvent&, void*),
358  void* cookie = NULL)
359  {
360  return (registerMouseCallback (boost::bind (callback, _1, cookie)));
361  }
362 
369  template<typename T> boost::signals2::connection
370  registerMouseCallback (void (T::*callback) (const pcl::visualization::MouseEvent&, void*),
371  T& instance, void* cookie = NULL)
372  {
373  return (registerMouseCallback (boost::bind (callback, boost::ref (instance), _1, cookie)));
374  }
375 
380  boost::signals2::connection
381  registerMouseCallback (boost::function<void (const pcl::visualization::MouseEvent&)> cb);
382 
387  void
388  setPosition (int x, int y)
389  {
390  image_viewer_->SetPosition (x, y);
391  }
392 
397  void
398  setSize (int xw, int yw)
399  {
400  image_viewer_->SetSize (xw, yw);
401  }
402 
404  bool
405  wasStopped () const { if (image_viewer_) return (stopped_); else return (true); }
406 
414  bool
415  addCircle (unsigned int x, unsigned int y, double radius,
416  const std::string &layer_id = "circles", double opacity = 1.0);
417 
428  bool
429  addCircle (unsigned int x, unsigned int y, double radius,
430  double r, double g, double b,
431  const std::string &layer_id = "circles", double opacity = 1.0);
432 
439  bool
440  addRectangle (const pcl::PointXY &min_pt, const pcl::PointXY &max_pt,
441  const std::string &layer_id = "rectangles", double opacity = 1.0);
442 
452  bool
453  addRectangle (const pcl::PointXY &min_pt, const pcl::PointXY &max_pt,
454  double r, double g, double b,
455  const std::string &layer_id = "rectangles", double opacity = 1.0);
456 
465  bool
466  addRectangle (unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max,
467  const std::string &layer_id = "rectangles", double opacity = 1.0);
468 
480  bool
481  addRectangle (unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max,
482  double r, double g, double b,
483  const std::string &layer_id = "rectangles", double opacity = 1.0);
484 
492  template <typename T> bool
493  addRectangle (const typename pcl::PointCloud<T>::ConstPtr &image,
494  const T &min_pt, const T &max_pt,
495  const std::string &layer_id = "rectangles", double opacity = 1.0);
496 
507  template <typename T> bool
508  addRectangle (const typename pcl::PointCloud<T>::ConstPtr &image,
509  const T &min_pt, const T &max_pt,
510  double r, double g, double b,
511  const std::string &layer_id = "rectangles", double opacity = 1.0);
512 
522  template <typename T> bool
523  addRectangle (const typename pcl::PointCloud<T>::ConstPtr &image, const pcl::PointCloud<T> &mask,
524  double r, double g, double b,
525  const std::string &layer_id = "rectangles", double opacity = 1.0);
526 
533  template <typename T> bool
534  addRectangle (const typename pcl::PointCloud<T>::ConstPtr &image, const pcl::PointCloud<T> &mask,
535  const std::string &layer_id = "image_mask", double opacity = 1.0);
536 
545  bool
546  addFilledRectangle (unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max,
547  const std::string &layer_id = "boxes", double opacity = 0.5);
548 
560  bool
561  addFilledRectangle (unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max,
562  double r, double g, double b,
563  const std::string &layer_id = "boxes", double opacity = 0.5);
564 
576  bool
577  addLine (unsigned int x_min, unsigned int y_min, unsigned int x_max, unsigned int y_max,
578  double r, double g, double b,
579  const std::string &layer_id = "line", double opacity = 1.0);
580 
589  bool
590  addLine (unsigned int x_min, unsigned int y_min, unsigned int x_max, unsigned int y_max,
591  const std::string &layer_id = "line", double opacity = 1.0);
592 
593 
603  template <typename T> bool
604  addMask (const typename pcl::PointCloud<T>::ConstPtr &image, const pcl::PointCloud<T> &mask,
605  double r, double g, double b,
606  const std::string &layer_id = "image_mask", double opacity = 0.5);
607 
614  template <typename T> bool
615  addMask (const typename pcl::PointCloud<T>::ConstPtr &image, const pcl::PointCloud<T> &mask,
616  const std::string &layer_id = "image_mask", double opacity = 0.5);
617 
628  template <typename T> bool
629  addPlanarPolygon (const typename pcl::PointCloud<T>::ConstPtr &image, const pcl::PlanarPolygon<T> &polygon,
630  double r, double g, double b,
631  const std::string &layer_id = "planar_polygon", double opacity = 1.0);
632 
640  template <typename T> bool
641  addPlanarPolygon (const typename pcl::PointCloud<T>::ConstPtr &image, const pcl::PlanarPolygon<T> &polygon,
642  const std::string &layer_id = "planar_polygon", double opacity = 1.0);
643 
650  bool
651  addLayer (const std::string &layer_id, int width, int height, double opacity = 0.5);
652 
656  void
657  removeLayer (const std::string &layer_id);
658  protected:
660  void
661  resetStoppedFlag () { if (image_viewer_) stopped_ = false; }
662 
666  void
667  emitMouseEvent (unsigned long event_id);
668 
672  void
673  emitKeyboardEvent (unsigned long event_id);
674 
675  // Callbacks used to register for vtk command
676  static void
677  MouseCallback (vtkObject*, unsigned long eid, void* clientdata, void *calldata);
678  static void
679  KeyboardCallback (vtkObject*, unsigned long eid, void* clientdata, void *calldata);
680 
681  protected: // types
682  struct ExitMainLoopTimerCallback : public vtkCommand
683  {
684  ExitMainLoopTimerCallback () : right_timer_id (), window () {}
685 
686  static ExitMainLoopTimerCallback* New ()
687  {
688  return (new ExitMainLoopTimerCallback);
689  }
690  virtual void
691  Execute (vtkObject* vtkNotUsed (caller), unsigned long event_id, void* call_data)
692  {
693  if (event_id != vtkCommand::TimerEvent)
694  return;
695  int timer_id = *static_cast<int*> (call_data);
696  if (timer_id != right_timer_id)
697  return;
698  window->interactor_->TerminateApp ();
699  }
700  int right_timer_id;
701  ImageViewer* window;
702  };
703  struct ExitCallback : public vtkCommand
704  {
705  ExitCallback () : window () {}
706 
707  static ExitCallback* New ()
708  {
709  return (new ExitCallback);
710  }
711  virtual void
712  Execute (vtkObject*, unsigned long event_id, void*)
713  {
714  if (event_id != vtkCommand::ExitEvent)
715  return;
716  window->stopped_ = true;
717  window->interactor_->TerminateApp ();
718  }
719  ImageViewer* window;
720  };
721 
722  private:
723 
725  struct Layer
726  {
727  Layer () : canvas (), layer_name (), opacity () {}
728  vtkSmartPointer<PCLImageCanvasSource2D> canvas;
729  std::string layer_name;
730  double opacity;
731  };
732 
733  typedef std::vector<Layer> LayerMap;
734 
742  LayerMap::iterator
743  createLayer (const std::string &layer_id, int width, int height, double opacity = 0.5, bool fill_box = true);
744 
745  boost::signals2::signal<void (const pcl::visualization::MouseEvent&)> mouse_signal_;
746  boost::signals2::signal<void (const pcl::visualization::KeyboardEvent&)> keyboard_signal_;
747 
748  vtkSmartPointer<vtkRenderWindowInteractor> interactor_;
749  vtkSmartPointer<vtkCallbackCommand> mouse_command_;
750  vtkSmartPointer<vtkCallbackCommand> keyboard_command_;
751 
753  vtkSmartPointer<ExitMainLoopTimerCallback> exit_main_loop_timer_callback_;
754  vtkSmartPointer<ExitCallback> exit_callback_;
755 
757  vtkSmartPointer<vtkImageViewer> image_viewer_;
758 
760  boost::shared_array<unsigned char> data_;
761 
763  size_t data_size_;
764 
766  bool stopped_;
767 
769  int timer_id_;
770 
772  vtkSmartPointer<vtkImageBlend> blend_;
773 
775  LayerMap layer_map_;
776 
777  struct LayerComparator
778  {
779  LayerComparator (const std::string &str) : str_ (str) {}
780  const std::string &str_;
781 
782  bool
783  operator () (const Layer &layer)
784  {
785  return (layer.layer_name == str_);
786  }
787  };
788 
789  public:
790  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
791  };
792  }
793 }
794 
796 
797 #endif /* __IMAGE_VISUALIZER_H__ */
798