image_viewer.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2009-2012, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id: image_viewer.cpp 5433 2012-03-30 04:34:55Z rusu $
00037  */
00038 
00039 #include <pcl/visualization/image_viewer.h>
00040 #include <pcl/visualization/common/float_image_utils.h>
00041 #include <pcl/visualization/keyboard_event.h>
00042 #include <pcl/visualization/mouse_event.h>
00043 #include <pcl/common/time.h>
00044 
00046 pcl::visualization::ImageViewer::ImageViewer (const std::string& window_title)
00047   : interactor_ (vtkSmartPointer<vtkRenderWindowInteractor>::New ())
00048   , mouse_command_ (vtkSmartPointer<vtkCallbackCommand>::New ())
00049   , keyboard_command_ (vtkSmartPointer<vtkCallbackCommand>::New ())
00050   , image_viewer_ (vtkSmartPointer<vtkImageViewer>::New ())
00051   , data_ ()
00052   , data_size_ (0)
00053   , stopped_ ()
00054   , timer_id_ ()
00055   , blend_ (vtkSmartPointer<vtkImageBlend>::New ())
00056   , layer_map_ ()
00057 {
00058   blend_->SetBlendModeToNormal ();
00059   blend_->SetNumberOfThreads (1);
00060   image_viewer_->SetColorLevel (127.5);
00061   image_viewer_->SetColorWindow (255);
00062 
00063   // Set the mouse/keyboard callbacks
00064   mouse_command_->SetClientData (this);
00065   mouse_command_->SetCallback (ImageViewer::MouseCallback);
00066   
00067   keyboard_command_->SetClientData (this);
00068   keyboard_command_->SetCallback (ImageViewer::KeyboardCallback);
00069 
00070   // Create our own  interactor and set the window title
00071   image_viewer_->SetupInteractor (interactor_);
00072   image_viewer_->GetRenderWindow ()->SetWindowName (window_title.c_str ());
00073 
00074   // Initialize and create timer
00075   interactor_->Initialize ();
00076   timer_id_ = interactor_->CreateRepeatingTimer (0);
00077   
00078   // Set the exit callbacks
00079   exit_main_loop_timer_callback_ = vtkSmartPointer<ExitMainLoopTimerCallback>::New ();
00080   exit_main_loop_timer_callback_->window = this;
00081   exit_main_loop_timer_callback_->right_timer_id = -1;
00082   interactor_->AddObserver (vtkCommand::TimerEvent, exit_main_loop_timer_callback_);
00083 
00084   exit_callback_ = vtkSmartPointer<ExitCallback>::New ();
00085   exit_callback_->window = this;
00086   interactor_->AddObserver (vtkCommand::ExitEvent, exit_callback_);
00087 
00088   resetStoppedFlag ();
00089 }
00090 
00092 pcl::visualization::ImageViewer::~ImageViewer ()
00093 {
00094    interactor_->DestroyTimer (timer_id_);
00095 }
00096 
00098 void
00099 pcl::visualization::ImageViewer::addRGBImage (
00100     const unsigned char* rgb_data, unsigned width, unsigned height,
00101     const std::string &layer_id, double opacity)
00102 {
00103   if (unsigned (image_viewer_->GetRenderWindow ()->GetSize ()[0]) != width || 
00104       unsigned (image_viewer_->GetRenderWindow ()->GetSize ()[1]) != height)
00105     image_viewer_->SetSize (width, height);
00106 
00107   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00108   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00109   if (am_it == layer_map_.end ())
00110   {
00111     PCL_DEBUG ("[pcl::visualization::ImageViewer::addRGBImage] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00112     am_it = createLayer (layer_id, width, height, opacity, false);
00113   }
00114 
00115   vtkSmartPointer<vtkImageImport> importer = vtkSmartPointer<vtkImageImport>::New ();
00116   importer->SetNumberOfScalarComponents (3);
00117   importer->SetDataScalarTypeToUnsignedChar ();
00118   importer->SetWholeExtent (0, width - 1, 0, height - 1, 0, 0);
00119   importer->SetDataExtentToWholeExtent ();
00120 
00121   void* data = const_cast<void*> (reinterpret_cast<const void*> (rgb_data));
00122   importer->SetImportVoidPointer (data, 1);
00123   importer->Update ();
00124 
00125   vtkSmartPointer<vtkMatrix4x4> transform = vtkSmartPointer<vtkMatrix4x4>::New ();
00126   transform->Identity ();
00127   transform->SetElement (1, 1, -1.0);
00128   transform->SetElement (1, 3, height);
00129   vtkSmartPointer<vtkTransform> imageTransform = vtkSmartPointer<vtkTransform>::New ();
00130   imageTransform->SetMatrix (transform);
00131   // Now create filter and set previously created transformation
00132   vtkSmartPointer<vtkImageReslice> algo = vtkSmartPointer<vtkImageReslice>::New ();
00133   algo->SetInput (importer->GetOutput ());
00134   algo->SetInformationInput (importer->GetOutput ());
00135   algo->SetResliceTransform (imageTransform);
00136   algo->SetInterpolationModeToCubic ();
00137   algo->Update ();
00138 
00139   // If we already have other layers, then it makes sense to use a blender
00140 //  if (layer_map_.size () != 1)
00141 //  {
00142 #if ((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 6))
00143     image_viewer_->SetInput (algo->GetOutput ());
00144 #else
00145     am_it->canvas->SetNumberOfScalarComponents (3);
00146     am_it->canvas->DrawImage (algo->GetOutput ());
00147 
00148     blend_->ReplaceNthInputConnection (int (am_it - layer_map_.begin ()), am_it->canvas->GetOutputPort ());
00149     image_viewer_->SetInputConnection (blend_->GetOutputPort ());
00150 #endif
00151 //  }
00152 //  // If not, pass the data directly to the viewer
00153 //  else
00154 //    image_viewer_->SetInput (algo->GetOutput ());
00155 }
00156 
00158 void
00159 pcl::visualization::ImageViewer::showRGBImage (
00160     const unsigned char* rgb_data, unsigned width, unsigned height,
00161     const std::string &layer_id, double opacity)
00162 {
00163   addRGBImage (rgb_data, width, height, layer_id, opacity);
00164   image_viewer_->Render ();
00165 }
00166 
00168 void
00169 pcl::visualization::ImageViewer::addMonoImage (
00170     const unsigned char* rgb_data, unsigned width, unsigned height,
00171     const std::string &layer_id, double opacity)
00172 {
00173   if (unsigned (image_viewer_->GetRenderWindow ()->GetSize ()[0]) != width || 
00174       unsigned (image_viewer_->GetRenderWindow ()->GetSize ()[1]) != height)
00175     image_viewer_->SetSize (width, height);
00176 
00177   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00178   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00179   if (am_it == layer_map_.end ())
00180   {
00181     PCL_DEBUG ("[pcl::visualization::ImageViewer::showMonoImage] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00182     am_it = createLayer (layer_id, width, height, opacity, false);
00183   }
00184 
00185   vtkSmartPointer<vtkImageImport> importer = vtkSmartPointer<vtkImageImport>::New ();
00186   importer->SetNumberOfScalarComponents (1);
00187   importer->SetWholeExtent (0, width - 1, 0, height - 1, 0, 0);
00188   importer->SetDataScalarTypeToUnsignedChar ();
00189   importer->SetDataExtentToWholeExtent ();
00190 
00191   void* data = const_cast<void*> (reinterpret_cast<const void*> (rgb_data));
00192   importer->SetImportVoidPointer (data, 1);
00193   importer->Update ();
00194 
00195   vtkSmartPointer<vtkMatrix4x4> transform = vtkSmartPointer<vtkMatrix4x4>::New ();
00196   transform->Identity ();
00197   transform->SetElement (1, 1, -1.0);
00198   transform->SetElement (1, 3, height);
00199   vtkSmartPointer<vtkTransform> imageTransform = vtkSmartPointer<vtkTransform>::New ();
00200   imageTransform->SetMatrix (transform);
00201   // Now create filter and set previously created transformation
00202   vtkSmartPointer<vtkImageReslice> algo = vtkSmartPointer<vtkImageReslice>::New ();
00203   algo->SetInput (importer->GetOutput ());
00204   algo->SetInformationInput (importer->GetOutput ());
00205   algo->SetResliceTransform (imageTransform);
00206   algo->SetInterpolationModeToCubic ();
00207   algo->Update ();
00208 
00209   // If we already have other layers, then it makes sense to use a blender
00210 //  if (layer_map_.size () != 1)
00211 //  {
00212 #if ((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 6))
00213     image_viewer_->SetInput (algo->GetOutput ());
00214 #else
00215     am_it->canvas->SetNumberOfScalarComponents (1);
00216     am_it->canvas->DrawImage (algo->GetOutput ());
00217 
00218     blend_->ReplaceNthInputConnection (int (am_it - layer_map_.begin ()), am_it->canvas->GetOutputPort ());
00219     image_viewer_->SetInputConnection (blend_->GetOutputPort ());
00220 #endif
00221 //  }
00222 //  // If not, pass the data directly to the viewer
00223 //  else
00224 //    image_viewer_->SetInput (algo->GetOutput ());
00225 }
00226 
00228 void
00229 pcl::visualization::ImageViewer::showMonoImage (
00230     const unsigned char* rgb_data, unsigned width, unsigned height,
00231     const std::string &layer_id, double opacity)
00232 {
00233   addMonoImage (rgb_data, width, height, layer_id, opacity);
00234   image_viewer_->Render ();
00235 }
00236 
00238 void
00239 pcl::visualization::ImageViewer::addFloatImage (
00240     const float* float_image, unsigned int width, unsigned int height,
00241     float min_value, float max_value, bool grayscale,
00242     const std::string &layer_id, double opacity)
00243 {
00244   unsigned char* rgb_image = FloatImageUtils::getVisualImage (float_image, width, height,
00245                                                               min_value, max_value, grayscale);
00246   showRGBImage (rgb_image, width, height, layer_id, opacity);
00247   delete[] rgb_image;  
00248  }
00249 
00251 void
00252 pcl::visualization::ImageViewer::showFloatImage (
00253     const float* float_image, unsigned int width, unsigned int height,
00254     float min_value, float max_value, bool grayscale,
00255     const std::string &layer_id, double opacity)
00256 {
00257   addFloatImage (float_image, width, height, min_value, max_value, grayscale, layer_id, opacity);
00258   image_viewer_->Render ();
00259  }
00260  
00262 void 
00263 pcl::visualization::ImageViewer::addAngleImage (
00264     const float* angle_image, unsigned int width, unsigned int height,
00265     const std::string &layer_id, double opacity)
00266 {
00267   unsigned char* rgb_image = FloatImageUtils::getVisualAngleImage (angle_image, width, height);
00268   showRGBImage (rgb_image, width, height, layer_id, opacity);
00269   delete[] rgb_image;
00270 }
00271 
00273 void 
00274 pcl::visualization::ImageViewer::showAngleImage (
00275     const float* angle_image, unsigned int width, unsigned int height,
00276     const std::string &layer_id, double opacity)
00277 {
00278   addAngleImage (angle_image, width, height, layer_id, opacity);
00279   image_viewer_->Render ();
00280 }
00281 
00283 void 
00284 pcl::visualization::ImageViewer::addHalfAngleImage (
00285     const float* angle_image, unsigned int width, unsigned int height,
00286     const std::string &layer_id, double opacity)
00287 {
00288   unsigned char* rgb_image = FloatImageUtils::getVisualHalfAngleImage (angle_image, width, height);
00289   showRGBImage (rgb_image, width, height, layer_id, opacity);
00290   delete[] rgb_image;
00291 }
00292 
00294 void 
00295 pcl::visualization::ImageViewer::showHalfAngleImage (
00296     const float* angle_image, unsigned int width, unsigned int height,
00297     const std::string &layer_id, double opacity)
00298 {
00299   addHalfAngleImage (angle_image, width, height, layer_id, opacity);
00300   image_viewer_->Render ();
00301 }
00302 
00304 void 
00305 pcl::visualization::ImageViewer::addShortImage (
00306     const unsigned short* short_image, unsigned int width, unsigned int height, 
00307     unsigned short min_value, unsigned short max_value, bool grayscale,
00308     const std::string &layer_id, double opacity)
00309 {
00310   unsigned char* rgb_image = FloatImageUtils::getVisualImage (short_image, width, height,
00311                                                               min_value, max_value, grayscale);
00312   showRGBImage (rgb_image, width, height, layer_id, opacity);
00313   delete[] rgb_image;
00314 }
00315 
00317 void 
00318 pcl::visualization::ImageViewer::showShortImage (
00319     const unsigned short* short_image, unsigned int width, unsigned int height, 
00320     unsigned short min_value, unsigned short max_value, bool grayscale,
00321     const std::string &layer_id, double opacity)
00322 {
00323   addShortImage (short_image, width, height, min_value, max_value, grayscale, layer_id, opacity);
00324   image_viewer_->Render ();
00325 }
00326 
00328 void 
00329 pcl::visualization::ImageViewer::spin ()
00330 {
00331   image_viewer_->Render ();
00332   resetStoppedFlag ();
00333   // Render the window before we start the interactor
00334   interactor_->Render ();
00335   interactor_->Start ();
00336 }
00337 
00339 void
00340 pcl::visualization::ImageViewer::spinOnce (int time, bool force_redraw)
00341 {
00342   if (force_redraw)
00343   {
00344     image_viewer_->Render ();
00345     interactor_->Render ();
00346   }
00347 
00348   if (time <= 0)
00349     time = 1;
00350   
00351   DO_EVERY (1.0 / interactor_->GetDesiredUpdateRate (),
00352     exit_main_loop_timer_callback_->right_timer_id = interactor_->CreateRepeatingTimer (time);
00353     interactor_->Start ();
00354     interactor_->DestroyTimer (exit_main_loop_timer_callback_->right_timer_id);
00355   );
00356 }
00357 
00359 boost::signals2::connection 
00360 pcl::visualization::ImageViewer::registerMouseCallback (
00361     boost::function<void (const pcl::visualization::MouseEvent&)> callback)
00362 {
00363   // just add observer at first time when a callback is registered
00364   if (mouse_signal_.empty ())
00365   {
00366     interactor_->AddObserver (vtkCommand::MouseMoveEvent, mouse_command_);
00367     interactor_->AddObserver (vtkCommand::MiddleButtonPressEvent, mouse_command_);
00368     interactor_->AddObserver (vtkCommand::MiddleButtonReleaseEvent, mouse_command_);
00369     interactor_->AddObserver (vtkCommand::MouseWheelBackwardEvent, mouse_command_);
00370     interactor_->AddObserver (vtkCommand::MouseWheelForwardEvent, mouse_command_);
00371     interactor_->AddObserver (vtkCommand::LeftButtonPressEvent, mouse_command_);
00372     interactor_->AddObserver (vtkCommand::LeftButtonReleaseEvent, mouse_command_);
00373     interactor_->AddObserver (vtkCommand::RightButtonPressEvent, mouse_command_);
00374     interactor_->AddObserver (vtkCommand::RightButtonReleaseEvent, mouse_command_);
00375   }
00376   return (mouse_signal_.connect (callback));
00377 }
00378 
00380 boost::signals2::connection 
00381 pcl::visualization::ImageViewer::registerKeyboardCallback (
00382     boost::function<void (const pcl::visualization::KeyboardEvent&)> callback)
00383 {
00384   // just add observer at first time when a callback is registered
00385   if (keyboard_signal_.empty ())
00386   {
00387     interactor_->AddObserver (vtkCommand::KeyPressEvent, keyboard_command_);
00388     interactor_->AddObserver (vtkCommand::KeyReleaseEvent, keyboard_command_);
00389   }
00390   
00391   return (keyboard_signal_.connect (callback));
00392 }
00393 
00395 void 
00396 pcl::visualization::ImageViewer::emitMouseEvent (unsigned long event_id)
00397 {
00398   //interactor_->GetMousePosition (&x, &y);
00399   int x = this->interactor_->GetEventPosition()[0];
00400   int y = this->interactor_->GetEventPosition()[1];
00401   MouseEvent event (MouseEvent::MouseMove, MouseEvent::NoButton, x, y, 
00402                     interactor_->GetAltKey (), interactor_->GetControlKey (), interactor_->GetShiftKey ());
00403   bool repeat = false;
00404   switch (event_id)
00405   {
00406     case vtkCommand::MouseMoveEvent :
00407       event.setType(MouseEvent::MouseMove);
00408       break;
00409     
00410     case vtkCommand::LeftButtonPressEvent :
00411       event.setButton(MouseEvent::LeftButton);
00412       if (interactor_->GetRepeatCount () == 0)
00413         event.setType(MouseEvent::MouseButtonPress);
00414       else
00415         event.setType(MouseEvent::MouseDblClick);
00416       break;
00417       
00418     case vtkCommand::LeftButtonReleaseEvent :
00419       event.setButton(MouseEvent::LeftButton);
00420       event.setType(MouseEvent::MouseButtonRelease);
00421       break;
00422       
00423     case vtkCommand::RightButtonPressEvent :
00424       event.setButton(MouseEvent::RightButton);
00425       if (interactor_->GetRepeatCount () == 0)
00426         event.setType(MouseEvent::MouseButtonPress);
00427       else
00428         event.setType(MouseEvent::MouseDblClick);
00429       break;
00430       
00431     case vtkCommand::RightButtonReleaseEvent :
00432       event.setButton(MouseEvent::RightButton);
00433       event.setType(MouseEvent::MouseButtonRelease);
00434       break;
00435       
00436     case vtkCommand::MiddleButtonPressEvent :
00437       event.setButton(MouseEvent::MiddleButton);
00438       if (interactor_->GetRepeatCount () == 0)
00439         event.setType(MouseEvent::MouseButtonPress);
00440       else
00441         event.setType(MouseEvent::MouseDblClick);
00442       break;
00443       
00444     case vtkCommand::MiddleButtonReleaseEvent :
00445       event.setButton(MouseEvent::MiddleButton);
00446       event.setType(MouseEvent::MouseButtonRelease);
00447       break;
00448       
00449       case vtkCommand::MouseWheelBackwardEvent :
00450       event.setButton(MouseEvent::VScroll);
00451       event.setType(MouseEvent::MouseScrollDown);
00452       if (interactor_->GetRepeatCount () != 0)
00453         repeat = true;
00454       break;
00455       
00456     case vtkCommand::MouseWheelForwardEvent :
00457       event.setButton(MouseEvent::VScroll);
00458       event.setType(MouseEvent::MouseScrollUp);
00459       if (interactor_->GetRepeatCount () != 0)
00460         repeat = true;
00461       break;
00462     default:
00463       return;
00464   }
00465   
00466   mouse_signal_ (event);
00467   if (repeat)
00468     mouse_signal_ (event);
00469 }
00470 
00472 void 
00473 pcl::visualization::ImageViewer::emitKeyboardEvent (unsigned long event_id)
00474 {
00475   KeyboardEvent event (bool(event_id == vtkCommand::KeyPressEvent), interactor_->GetKeySym (),  interactor_->GetKeyCode (), interactor_->GetAltKey (), interactor_->GetControlKey (), interactor_->GetShiftKey ());
00476   keyboard_signal_ (event);
00477 }
00478 
00480 void 
00481 pcl::visualization::ImageViewer::MouseCallback (vtkObject*, unsigned long eid, void* clientdata, void*)
00482 {
00483   ImageViewer* window = reinterpret_cast<ImageViewer*> (clientdata);
00484   window->emitMouseEvent (eid);
00485 }
00486 
00488 void 
00489 pcl::visualization::ImageViewer::KeyboardCallback (vtkObject*, unsigned long eid, void* clientdata, void*)
00490 {
00491   ImageViewer* window = reinterpret_cast<ImageViewer*> (clientdata);
00492   window->emitKeyboardEvent (eid);
00493 }
00494 
00496 pcl::visualization::ImageViewer::LayerMap::iterator
00497 pcl::visualization::ImageViewer::createLayer (
00498     const std::string &layer_id, int width, int height, double opacity, bool fill_box)
00499 {
00500   Layer l;
00501   l.layer_name = layer_id;
00502   l.opacity = opacity;
00503   // Create a new layer
00504   l.canvas = vtkSmartPointer<PCLImageCanvasSource2D>::New ();
00505   l.canvas->SetScalarTypeToUnsignedChar ();
00506   l.canvas->SetExtent (0, width, 0, height, 0, 0);
00507   l.canvas->SetNumberOfScalarComponents (4);
00508   if (fill_box)
00509   {
00510     l.canvas->SetDrawColor (0.0, 0.0, 0.0, 0.0);
00511     l.canvas->FillBox (0, width, 0, height);
00512     l.canvas->Update ();
00513     l.canvas->Modified ();
00514   }
00515   blend_->AddInputConnection (l.canvas->GetOutputPort ());
00516   blend_->SetOpacity (blend_->GetNumberOfInputs () - 1, opacity);
00517 
00518   // Add another element
00519   layer_map_.push_back (l);
00520 
00521   return (layer_map_.end () - 1);
00522 }
00523 
00525 bool
00526 pcl::visualization::ImageViewer::addLayer (
00527     const std::string &layer_id, int width, int height, double opacity)
00528 {
00529   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00530   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00531   if (am_it != layer_map_.end ())
00532   {
00533     PCL_DEBUG ("[pcl::visualization::ImageViewer::addLayer] Layer with ID'=%s' already exists!\n", layer_id.c_str ());
00534     return (false);
00535   }
00536 
00537   Layer l;
00538   l.layer_name = layer_id;
00539   l.opacity = opacity;
00540   // Create a new layer
00541   l.canvas = vtkSmartPointer<PCLImageCanvasSource2D>::New ();
00542   l.canvas->SetScalarTypeToUnsignedChar ();
00543   l.canvas->SetExtent (0, width, 0, height, 0, 0);
00544   l.canvas->SetNumberOfScalarComponents (4);
00545   blend_->AddInputConnection (l.canvas->GetOutputPort ());
00546   blend_->SetOpacity (blend_->GetNumberOfInputs () - 1, opacity);
00547 
00548   // Add another element
00549   layer_map_.push_back (l);
00550 
00551   return (true);
00552 }
00553 
00555 void
00556 pcl::visualization::ImageViewer::removeLayer (const std::string &layer_id)
00557 {
00558   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00559   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00560   if (am_it == layer_map_.end ())
00561   {
00562     PCL_DEBUG ("[pcl::visualization::ImageViewer::removeLayer] No layer with ID'=%s' found.\n", layer_id.c_str ());
00563     return;
00564   }
00565 
00566   // Remove the layers that we don't want anymore
00567   layer_map_.erase (layer_map_.begin () + int (am_it - layer_map_.begin ()));
00568 
00569   // Clear the blender
00570   blend_->RemoveAllInputs ();
00571   // Add the remaining layers back to the blender
00572   for (size_t i = 0; i < layer_map_.size (); ++i)
00573   {
00574     blend_->AddInputConnection (layer_map_[i].canvas->GetOutputPort ());
00575     blend_->SetOpacity (blend_->GetNumberOfInputs () - 1, layer_map_[i].opacity);
00576   }
00577   image_viewer_->SetInputConnection (blend_->GetOutputPort ());
00578 }
00579 
00581 bool
00582 pcl::visualization::ImageViewer::addCircle (
00583     unsigned int x, unsigned int y, double radius, double r, double g, double b, 
00584     const std::string &layer_id, double opacity)
00585 {
00586   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00587   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00588   if (am_it == layer_map_.end ())
00589   {
00590     PCL_DEBUG ("[pcl::visualization::ImageViewer::addCircle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00591     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00592   }
00593 
00594   am_it->canvas->SetDrawColor (r * 255.0, g * 255.0, b * 255.0, opacity * 255.0);
00595   am_it->canvas->DrawCircle (x, y, radius);
00596 //  blend_->ReplaceNthInputConnection (int (am_it - layer_map_.begin ()), am_it->canvas->GetOutputPort ());
00597 //  image_viewer_->SetInputConnection (blend_->GetOutputPort ());
00598 
00599   return (true);
00600 }
00601 
00603 bool
00604 pcl::visualization::ImageViewer::addCircle (unsigned int x, unsigned int y, double radius, 
00605                                             const std::string &layer_id, double opacity)
00606 {
00607   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00608   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00609   if (am_it == layer_map_.end ())
00610   {
00611     PCL_DEBUG ("[pcl::visualization::ImageViewer::addCircle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00612     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00613   }
00614 
00615   am_it->canvas->SetDrawColor (0.0, 255.0, 0.0, opacity * 255.0);
00616   am_it->canvas->DrawCircle (x, y, radius);
00617   return (true);
00618 }
00619 
00621 bool
00622 pcl::visualization::ImageViewer::addFilledRectangle (
00623     unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max, 
00624     double r, double g, double b, const std::string &layer_id, double opacity)
00625 {
00626   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00627   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00628   if (am_it == layer_map_.end ())
00629   {
00630     PCL_DEBUG ("[pcl::visualization::ImageViewer::addFilledRectangle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00631     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00632   }
00633 
00634   am_it->canvas->SetDrawColor (r * 255.0, g * 255.0, b * 255.0, opacity * 255.0);
00635   am_it->canvas->FillBox (x_min, x_max, y_min, y_max);
00636 //  blend_->ReplaceNthInputConnection (int (am_it - layer_map_.begin ()), am_it->canvas->GetOutputPort ());
00637 //  image_viewer_->SetInputConnection (blend_->GetOutputPort ());
00638 
00639   return (true);
00640 }
00641 
00643 bool
00644 pcl::visualization::ImageViewer::addFilledRectangle (
00645     unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max, 
00646     const std::string &layer_id, double opacity)
00647 {
00648   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00649   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00650   if (am_it == layer_map_.end ())
00651   {
00652     PCL_DEBUG ("[pcl::visualization::ImageViewer::addFilledRectangle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00653     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00654   }
00655 
00656   am_it->canvas->SetDrawColor (0.0, 255.0, 0.0, opacity * 255.0);
00657   am_it->canvas->FillBox (x_min, x_max, y_min, y_max);
00658   return (true);
00659 }
00660 
00662 bool
00663 pcl::visualization::ImageViewer::addRectangle (
00664     unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max, 
00665     double r, double g, double b, const std::string &layer_id, double opacity)
00666 {
00667   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00668   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00669   if (am_it == layer_map_.end ())
00670   {
00671     PCL_DEBUG ("[pcl::visualization::ImageViewer::addRectangle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00672     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00673   }
00674 
00675   am_it->canvas->SetDrawColor (r * 255.0, g * 255.0, b * 255.0, opacity * 255.0);
00676   am_it->canvas->DrawSegment (x_min, y_min, x_min, y_max);
00677   am_it->canvas->DrawSegment (x_min, y_max, x_max, y_max);
00678   am_it->canvas->DrawSegment (x_max, y_max, x_max, y_min);
00679   am_it->canvas->DrawSegment (x_max, y_min, x_min, y_min);
00680 
00681   return (true);
00682 }
00683 
00685 bool
00686 pcl::visualization::ImageViewer::addRectangle (
00687     unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max, 
00688     const std::string &layer_id, double opacity)
00689 {
00690   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00691   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00692   if (am_it == layer_map_.end ())
00693   {
00694     PCL_DEBUG ("[pcl::visualization::ImageViewer::addRectangle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00695     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00696   }
00697 
00698   am_it->canvas->SetDrawColor (0.0, 255.0, 0.0, opacity * 255.0);
00699   am_it->canvas->DrawSegment (x_min, y_min, x_min, y_max);
00700   am_it->canvas->DrawSegment (x_min, y_max, x_max, y_max);
00701   am_it->canvas->DrawSegment (x_max, y_max, x_max, y_min);
00702   am_it->canvas->DrawSegment (x_max, y_min, x_min, y_min);
00703 
00704   return (true);
00705 }
00706 
00708 bool
00709 pcl::visualization::ImageViewer::addRectangle (
00710     const pcl::PointXY &min_pt, const pcl::PointXY &max_pt,
00711     double r, double g, double b, const std::string &layer_id, double opacity)
00712 {
00713   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00714   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00715   if (am_it == layer_map_.end ())
00716   {
00717     PCL_DEBUG ("[pcl::visualization::ImageViewer::addRectangle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00718     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00719   }
00720 
00721   am_it->canvas->SetDrawColor (r * 255.0, g * 255.0, b * 255.0, opacity * 255.0);
00722   am_it->canvas->DrawSegment (int (min_pt.x), int (min_pt.y), int (min_pt.x), int (max_pt.y));
00723   am_it->canvas->DrawSegment (int (min_pt.x), int (max_pt.y), int (max_pt.x), int (max_pt.y));
00724   am_it->canvas->DrawSegment (int (max_pt.x), int (max_pt.y), int (max_pt.x), int (min_pt.y));
00725   am_it->canvas->DrawSegment (int (max_pt.x), int (min_pt.y), int (min_pt.x), int (min_pt.y));
00726 
00727   return (true);
00728 }
00729 
00731 bool
00732 pcl::visualization::ImageViewer::addRectangle (
00733     const pcl::PointXY &min_pt, const pcl::PointXY &max_pt,
00734     const std::string &layer_id, double opacity)
00735 {
00736   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00737   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00738   if (am_it == layer_map_.end ())
00739   {
00740     PCL_DEBUG ("[pcl::visualization::ImageViewer::addRectangle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00741     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00742   }
00743 
00744   am_it->canvas->SetDrawColor (0.0, 255.0, 0.0, opacity * 255.0);
00745   am_it->canvas->DrawSegment (int (min_pt.x), int (min_pt.y), int (min_pt.x), int (max_pt.y));
00746   am_it->canvas->DrawSegment (int (min_pt.x), int (max_pt.y), int (max_pt.x), int (max_pt.y));
00747   am_it->canvas->DrawSegment (int (max_pt.x), int (max_pt.y), int (max_pt.x), int (min_pt.y));
00748   am_it->canvas->DrawSegment (int (max_pt.x), int (min_pt.y), int (min_pt.x), int (min_pt.y));
00749 
00750   return (true);
00751 }
00752 
00754 bool
00755 pcl::visualization::ImageViewer::addLine (unsigned int x_min, unsigned int y_min, 
00756                                           unsigned int x_max, unsigned int y_max,
00757                                           const std::string &layer_id, double opacity)
00758 {
00759   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00760   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00761   if (am_it == layer_map_.end ())
00762   {
00763     PCL_DEBUG ("[pcl::visualization::ImageViewer::addLine] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00764     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00765   }
00766 
00767   am_it->canvas->SetDrawColor (0.0, 255.0, 0.0, opacity * 255.0);
00768   am_it->canvas->DrawSegment (x_min, y_min, x_max, y_max);
00769 
00770   return (true);
00771 }
00772 
00774 bool
00775 pcl::visualization::ImageViewer::addLine (unsigned int x_min, unsigned int y_min, 
00776                                           unsigned int x_max, unsigned int y_max,
00777                                           double r, double g, double b, 
00778                                           const std::string &layer_id, double opacity)
00779 {
00780   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00781   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00782   if (am_it == layer_map_.end ())
00783   {
00784     PCL_DEBUG ("[pcl::visualization::ImageViewer::addLine] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00785     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00786   }
00787 
00788   am_it->canvas->SetDrawColor (r * 255.0, g * 255.0, b * 255.0, opacity * 255.0);
00789   am_it->canvas->DrawSegment (x_min, y_min, x_max, y_max);
00790 
00791   return (true);
00792 }
00793 
00795 void
00796 pcl::visualization::ImageViewer::markPoint (
00797     size_t u, size_t v, Vector3ub fg_color, Vector3ub bg_color, double radius,
00798     const std::string &layer_id, double opacity)
00799 {
00800   // Check to see if this ID entry already exists (has it been already added to the visualizer?)
00801   LayerMap::iterator am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
00802   if (am_it == layer_map_.end ())
00803   {
00804     PCL_DEBUG ("[pcl::visualization::ImageViewer::markPoint] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
00805     am_it = createLayer (layer_id, image_viewer_->GetRenderWindow ()->GetSize ()[0] - 1, image_viewer_->GetRenderWindow ()->GetSize ()[1] - 1, opacity, true);
00806   }
00807 
00808   am_it->canvas->SetDrawColor (fg_color[0], fg_color[1], fg_color[2], opacity * 255.0);
00809   am_it->canvas->DrawPoint (int (u), int (v));
00810   am_it->canvas->SetDrawColor (bg_color[0], bg_color[1], bg_color[2], opacity * 255.0);
00811   am_it->canvas->DrawCircle (int (u), int (v), radius);
00812 }
00813 


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:15:25