viewport.cpp
Go to the documentation of this file.
00001 // C++
00002 #include <iostream>
00003 #include <string>
00004 
00005 // PCL
00006 #include <pcl/outofcore/visualization/camera.h>
00007 #include <pcl/outofcore/visualization/geometry.h>
00008 #include <pcl/outofcore/visualization/scene.h>
00009 #include <pcl/outofcore/visualization/object.h>
00010 #include <pcl/outofcore/visualization/viewport.h>
00011 
00012 // VTK
00013 #include <vtkActor.h>
00014 #include <vtkCallbackCommand.h>
00015 #include <vtkObject.h>
00016 #include <vtkTextActor.h>
00017 #include <vtkTextProperty.h>
00018 #include <vtkRenderer.h>
00019 #include <vtkRenderWindow.h>
00020 #include <vtkRenderWindowInteractor.h>
00021 #include <vtkSmartPointer.h>
00022 
00023 //void CallbackFunction (vtkObject* caller, long unsigned int vtkNotUsed (eventId), void* clientData, void* vtkNotUsed (callData) )
00024 //{
00025 //  vtkRenderer* renderer = static_cast<vtkRenderer*> (caller);
00026 //
00027 //  double timeInSeconds = renderer->GetLastRenderTimeInSeconds ();
00028 //  double fps = 1.0/timeInSeconds;
00029 //  std::cout << "FPS: " << fps << std::endl;
00030 //
00031 //  std::cout << "Callback" << std::endl;
00032 //}
00033 
00034 // Operators
00035 // -----------------------------------------------------------------------------
00036 Viewport::Viewport (vtkSmartPointer<vtkRenderWindow> window, double xmin/*=0.0*/, double ymin/*=0.0*/,
00037                     double xmax/*=1.0*/, double ymax/*=1.0*/)
00038 {
00039   renderer_ = vtkSmartPointer<vtkRenderer>::New ();
00040   renderer_->SetViewport (xmin, ymin, xmax, ymax);
00041   renderer_->GradientBackgroundOn ();
00042   renderer_->SetBackground (.1, .1, .1);
00043   renderer_->SetBackground2 (.5, .6, .7);
00044 
00045   int *size = window->GetSize ();
00046   double viewport_xmin = size[0] * xmin;
00047   double viewport_xmax = size[0] * xmax;
00048 //  double viewport_ymin = size[1] * ymin;
00049   double viewport_ymax = size[1] * ymax;
00050 
00051   // HUD - Camera Name
00052   camera_hud_actor_ = vtkSmartPointer<vtkTextActor>::New ();
00053   camera_hud_actor_->GetTextProperty ()->SetFontSize (12);
00054   camera_hud_actor_->GetTextProperty ()->SetColor (0.8, 0.8, 0.8);
00055   camera_hud_actor_->GetTextProperty ()->SetJustificationToCentered ();
00056   camera_hud_actor_->SetPosition ((viewport_xmax - viewport_xmin) / 2, 10);
00057   renderer_->AddActor2D (camera_hud_actor_);
00058 
00059   // HUD - FPS
00060   fps_hud_actor_ = vtkSmartPointer<vtkTextActor>::New ();
00061   fps_hud_actor_->GetTextProperty ()->SetFontSize (12);
00062   fps_hud_actor_->GetTextProperty ()->SetColor (0.8, 0.8, 0.8);
00063   fps_hud_actor_->GetTextProperty ()->SetJustificationToRight ();
00064   fps_hud_actor_->SetInput ("fps");
00065   fps_hud_actor_->SetPosition ((viewport_xmax - viewport_xmin) - 10, 10);
00066   renderer_->AddActor2D (fps_hud_actor_);
00067 
00068   // HUD - Points Loaded
00069   points_hud_actor_ = vtkSmartPointer<vtkTextActor>::New ();
00070   points_hud_actor_->GetTextProperty ()->SetFontSize (12);
00071   points_hud_actor_->GetTextProperty ()->SetColor (0.8, 0.8, 0.8);
00072   points_hud_actor_->GetTextProperty ()->SetJustificationToRight ();
00073   points_hud_actor_->SetInput ("points/mb");
00074   points_hud_actor_->SetPosition ((viewport_xmax - viewport_xmin) - 10, viewport_ymax - 20);
00075   renderer_->AddActor2D (points_hud_actor_);
00076 
00077   // Callback - Viewport Modified - Window Resize
00078   viewport_modified_callback_ = vtkSmartPointer<vtkCallbackCommand>::New ();
00079   viewport_modified_callback_->SetCallback (Viewport::viewportModifiedCallback);
00080   viewport_modified_callback_->SetClientData (this);
00081 
00082   renderer_->AddObserver (vtkCommand::ModifiedEvent, viewport_modified_callback_);
00083 
00084   // Callback - Actor Updates
00085   viewport_actor_update_callback_ = vtkSmartPointer<vtkCallbackCommand>::New ();
00086   viewport_actor_update_callback_->SetCallback (Viewport::viewportActorUpdateCallback);
00087   viewport_actor_update_callback_->SetClientData (this);
00088 
00089   renderer_->AddObserver (vtkCommand::StartEvent, viewport_actor_update_callback_);
00090 
00091   // Callback - FPS
00092   viewport_hud_callback_ = vtkSmartPointer<vtkCallbackCommand>::New ();
00093   viewport_hud_callback_->SetCallback (Viewport::viewportHudUpdateCallback);
00094   viewport_hud_callback_->SetClientData (this);
00095 
00096   renderer_->AddObserver (vtkCommand::EndEvent, viewport_hud_callback_);
00097 
00098   window->AddRenderer (renderer_);
00099 
00100   Scene *scene = Scene::instance ();
00101   scene->addViewport (this);
00102 }
00103 
00104 // Callbacks
00105 // -----------------------------------------------------------------------------
00106 
00107 // Viewport Modified
00108 void
00109 Viewport::viewportModifiedCallback (vtkObject* vtkNotUsed (caller), unsigned long int vtkNotUsed (eventId),
00110                                     void* clientData, void* vtkNotUsed (callData))
00111 {
00112   Viewport *viewport = reinterpret_cast<Viewport*> (clientData);
00113   viewport->viewportModified ();
00114 }
00115 
00116 void
00117 Viewport::viewportModified ()
00118 {
00119   vtkRenderWindow *window = renderer_->GetRenderWindow ();
00120 
00121   int *size = window->GetSize ();
00122   double *viewport_size = renderer_->GetViewport ();
00123 
00124   double viewport_xmin = size[0] * viewport_size[0];
00125   double viewport_xmax = size[0] * viewport_size[2];
00126 
00127   camera_hud_actor_->SetPosition ((viewport_xmax - viewport_xmin) / 2, 10);
00128   fps_hud_actor_->SetPosition ((viewport_xmax - viewport_xmin) - 10, 10);
00129 }
00130 
00131 // Viewport Actor Update
00132 void
00133 Viewport::viewportActorUpdateCallback (vtkObject* /*caller*/, unsigned long int vtkNotUsed (eventId), void* clientData,
00134                                        void* vtkNotUsed (callData))
00135 {
00136   Viewport *viewport = reinterpret_cast<Viewport*> (clientData);
00137   viewport->viewportActorUpdate ();
00138 }
00139 
00140 void
00141 Viewport::viewportActorUpdate ()
00142 {
00143   Scene *scene = Scene::instance ();
00144 
00145   std::vector<Camera*> cameras = scene->getCameras ();
00146 
00147   for (int i = 0; i < cameras.size (); i++)
00148   {
00149     cameras[i]->render (renderer_);
00150 //    if (cameras[i]->getCamera () != renderer_->GetActiveCamera ())
00151 //    {
00152 //      renderer_->AddActor (cameras[i]->getCameraActor ());
00153 //      if (cameras[i]->getName () == "octree")
00154 //      {
00155 //        renderer_->AddActor (cameras[i]->getHullActor ());
00156 //      }
00157 //    }
00158   }
00159 
00160   std::vector<Object*> objects = scene->getObjects ();
00161   for (int i = 0; i < objects.size (); i++)
00162   {
00163     //std::cout << objects[i]->getName () << std::endl;
00164     objects[i]->render (renderer_);
00165   }
00166 }
00167 
00168 // FPS HUD Update
00169 void
00170 Viewport::viewportHudUpdateCallback (vtkObject* vtkNotUsed (caller), unsigned long int vtkNotUsed (eventId),
00171                                      void* clientData, void* vtkNotUsed (callData))
00172 {
00173   Viewport *viewport = reinterpret_cast<Viewport*> (clientData);
00174   viewport->viewportHudUpdate ();
00175 }
00176 
00177 void
00178 Viewport::viewportHudUpdate ()
00179 {
00180   // HUD - FPS
00181   double timeInSeconds = renderer_->GetLastRenderTimeInSeconds ();
00182   char fps_str[50];
00183   sprintf (fps_str, "%.2f fps", 1.0 / timeInSeconds);
00184   fps_hud_actor_->SetInput (fps_str);
00185 
00186   // HUD - Points Loaded
00187   Scene *scene = Scene::instance ();
00188   std::vector<Object*> objects = scene->getObjects ();
00189 
00190   uint64_t points_loaded = 0;
00191   uint64_t data_loaded = 0;
00192   for (int i = 0; i < objects.size (); i++)
00193   {
00194     //TYPE& dynamic_cast<TYPE&> (object);
00195     OutofcoreCloud* cloud = dynamic_cast<OutofcoreCloud*> (objects[i]);
00196     if (cloud != NULL)
00197     {
00198       points_loaded += cloud->getPointsLoaded ();
00199       data_loaded += cloud->getDataLoaded ();
00200     }
00201   }
00202 
00203   char points_loaded_str[50];
00204   sprintf (points_loaded_str, "%llu points/%llu mb", points_loaded, data_loaded/1024);
00205   points_hud_actor_->SetInput (points_loaded_str);
00206 }
00207 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:37:27