camera.cpp
Go to the documentation of this file.
00001 // C++
00002 #include <iostream>
00003 #include <string>
00004 
00005 // PCL - visualziation
00006 #include <pcl/visualization/common/common.h>
00007 
00008 // PCL - outofcore
00009 #include <pcl/outofcore/visualization/camera.h>
00010 #include <pcl/outofcore/visualization/object.h>
00011 
00012 // VTK
00013 #include <vtkActor.h>
00014 #include <vtkCamera.h>
00015 #include <vtkCameraActor.h>
00016 #include <vtkHull.h>
00017 #include <vtkPlanes.h>
00018 #include <vtkPolyData.h>
00019 #include <vtkPolyDataMapper.h>
00020 #include <vtkProperty.h>
00021 #include <vtkSmartPointer.h>
00022 
00023 // Operators
00024 // -----------------------------------------------------------------------------
00025 Camera::Camera (std::string name) :
00026     Object (name), display_ (false)
00027 {
00028   camera_ = vtkSmartPointer<vtkCamera>::New ();
00029   camera_->SetClippingRange(0.0001, 100000);
00030 
00031   camera_actor_ = vtkSmartPointer<vtkCameraActor>::New ();
00032   camera_actor_->SetCamera (camera_);
00033   camera_actor_->GetProperty ()->SetLighting (0);
00034   camera_actor_->GetProperty ()->SetLineStipplePattern (1010101010);
00035 
00036   for (int i = 0; i < 24; i++)
00037     frustum_[i] = 0;
00038 
00039   hull_actor_ = vtkSmartPointer<vtkActor>::New ();
00040   vtkSmartPointer<vtkPolyDataMapper> hull_mapper = vtkSmartPointer<vtkPolyDataMapper>::New ();
00041 
00042   hull_actor_->SetMapper (hull_mapper);
00043   hull_actor_->GetProperty ()->SetLighting (0);
00044   hull_actor_->GetProperty ()->SetColor (1.0, 0.0, 0.0);
00045   hull_actor_->GetProperty ()->SetOpacity (0.25);
00046 }
00047 
00048 Camera::Camera (std::string name, vtkSmartPointer<vtkCamera> camera) :
00049     Object (name), display_ (false)
00050 {
00051   camera_ = camera;
00052   camera_->SetClippingRange(0.0001, 100000);
00053 
00054   camera_actor_ = vtkSmartPointer<vtkCameraActor>::New ();
00055   camera_actor_->SetCamera (camera_);
00056   camera_actor_->GetProperty ()->SetLighting (0);
00057 
00058   for (int i = 0; i < 24; i++)
00059     frustum_[i] = 0;
00060 
00061   hull_actor_ = vtkSmartPointer<vtkActor>::New ();
00062   vtkSmartPointer<vtkPolyDataMapper> hull_mapper = vtkSmartPointer<vtkPolyDataMapper>::New ();
00063 
00064   hull_actor_->SetMapper (hull_mapper);
00065   hull_actor_->GetProperty ()->SetLighting (0);
00066   hull_actor_->GetProperty ()->SetColor (1.0, 0.0, 0.0);
00067   hull_actor_->GetProperty ()->SetOpacity (0.25);
00068 
00069   prevUp_[0] = prevUp_[1] = prevUp_[2] = 0;
00070   prevFocal_[0] = prevFocal_[1] = prevFocal_[2] = 0;
00071   prevPos_[0] = prevPos_[1] = prevPos_[2] = 0;
00072 }
00073 
00074 //std::ostream & operator<<(std::ostream &os, const Camera& camera)
00075 //{
00076 //    return os << camera.getName();
00077 //}
00078 
00079 // Methods
00080 // -----------------------------------------------------------------------------
00081 void
00082 //Camera::computeFrustum(double aspect)
00083 Camera::computeFrustum ()
00084 {
00085   // The planes array contains six plane equations of the form (Ax+By+Cz+D=0), the first four values are (A,B,C,D)
00086   // which repeats for each of the planes. The planes are given in the following order: -x,+x,-y,+y,-z,+z.
00087   //camera_->GetFrustumPlanes(aspect, frustum_);
00088 
00089   pcl::visualization::getViewFrustum (getViewProjectionMatrix (), frustum_);
00090 
00091 //  vtkSmartPointer<vtkHull> hull = vtkSmartPointer<vtkHull>::New ();
00092 //  vtkSmartPointer<vtkPlanes> planes = vtkSmartPointer<vtkPlanes>::New ();
00093 //  vtkSmartPointer<vtkPolyData> hullData = vtkSmartPointer<vtkPolyData>::New ();
00094 //
00095 //  planes->SetFrustumPlanes (frustum_);
00096 //  hull->SetPlanes (planes);
00097 //  hull->GenerateHull (hullData, -200, 200, -200, 200, -200, 200);
00098 //
00099 //  vtkSmartPointer<vtkPolyDataMapper> hull_mapper = static_cast<vtkPolyDataMapper*> (hull_actor_->GetMapper ());
00100 //
00101 //#if VTK_MAJOR_VERSION <= 5
00102 //  hull_mapper->SetInput (hullData);
00103 //#else
00104 //  hull_mapper->SetInputData(hullData);
00105 //#endif
00106 //
00107 //  hull_actor_->SetMapper (hull_mapper);
00108 }
00109 
00110 void
00111 Camera::printFrustum ()
00112 {
00113   for (int i = 0; i < 6; i++)
00114   {
00115     std::cout << frustum_[(i * 4)] << "x + " << frustum_[(i * 4) + 1] << "y + " << frustum_[(i * 4) + 2] << "z + "
00116         << frustum_[(i * 4) + 3] << std::endl;
00117   }
00118 }
00119 
00120 void
00121 Camera::render (vtkRenderer* renderer)
00122 {
00123   vtkSmartPointer<vtkCamera> active_camera = renderer->GetActiveCamera ();
00124 
00125 //  if (camera_.GetPointer() != active_camera.GetPointer())
00126 //  {
00127 //    if (display_)
00128 //    {
00129 //      renderer->AddActor (camera_actor_);
00130 //      renderer->AddActor (hull_actor_);
00131 //    }
00132 //    else
00133 //    {
00134 //      renderer->RemoveActor (camera_actor_);
00135 //      renderer->RemoveActor (hull_actor_);
00136 //    }
00137 //    return;
00138 //  }
00139 
00140   // Reset clipping range
00141   setClippingRange();
00142 
00143   double *up = active_camera->GetViewUp ();
00144   double *focal = active_camera->GetFocalPoint ();
00145   double *pos = active_camera->GetPosition ();
00146 
00147   bool viewpointChanged = false;
00148 
00149   // Check up vector
00150   if (up[0] != prevUp_[0] || up[1] != prevUp_[1] || up[2] != prevUp_[2])
00151     viewpointChanged = true;
00152 
00153   // Check focal point
00154   if (focal[0] != prevFocal_[0] || focal[1] != prevFocal_[1] || focal[2] != prevFocal_[2])
00155     viewpointChanged = true;
00156 
00157   // Check position
00158   if (pos[0] != prevPos_[0] || pos[1] != prevPos_[1] || pos[2] != prevPos_[2])
00159     viewpointChanged = true;
00160 
00161   // Break loop if the viewpoint hasn't changed
00162   if (viewpointChanged)
00163   {
00164     prevUp_[0] = up[0];
00165     prevUp_[1] = up[1];
00166     prevUp_[2] = up[2];
00167     prevFocal_[0] = focal[0];
00168     prevFocal_[1] = focal[1];
00169     prevFocal_[2] = focal[2];
00170     prevPos_[0] = pos[0];
00171     prevPos_[1] = pos[1];
00172     prevPos_[2] = pos[2];
00173 
00174 //        std::cout << "View Changed" << std::endl;
00175 //        std::cout << "Up: <" << up[0] << ", " << up[1] << ", " << up[2] << ">" << std::endl;
00176 //        std::cout << "Focal: <" << focal[0] << ", " << focal[1] << ", " << focal[2] << ">" << std::endl;
00177 //        std::cout << "Pos: <" << pos[0] << ", " << pos[1] << ", " << pos[2] << ">" << std::endl;
00178 
00179     {
00180       renderer->ComputeAspect ();
00181       double *aspect = renderer->GetAspect ();
00182 
00183       projection_matrix_ = pcl::visualization::vtkToEigen (active_camera->GetProjectionTransformMatrix (aspect[0] / aspect[1], 0.0, 1.0));
00184       model_view_matrix_ = pcl::visualization::vtkToEigen (active_camera->GetModelViewTransformMatrix ());
00185 
00186       //computeFrustum (renderer->GetTiledAspectRatio());
00187       computeFrustum ();
00188     }
00189   }
00190 
00191   Object::render(renderer);
00192 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:22:36