Skip to content

Commit

Permalink
Merge pull request #2901 from taketwo/minor-camera-refactoring
Browse files Browse the repository at this point in the history
Minor refactoring of `pcl::visualization::Camera` and related functions
  • Loading branch information
SergioRAgostinho authored Mar 12, 2019
2 parents cd9cfa7 + 336993e commit 5237f12
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 89 deletions.
15 changes: 15 additions & 0 deletions visualization/include/pcl/visualization/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
#include <vtkSmartPointer.h>
#include <vtkLookupTable.h>

class vtkCamera;
class vtkRenderWindow;

namespace pcl
{
struct RGB;
Expand Down Expand Up @@ -148,6 +151,18 @@ namespace pcl
class PCL_EXPORTS Camera
{
public:
/** Construct a camera with meaningful default values.
* The camera is positioned at origin, looks along z-axis and has up-vector along y-axis. Window position and
* size are initialized with (0, 0) and (1, 1) respectively. */
Camera ();

/** Construct a camera by copying parameters from a VTK camera.
* Window position and size are initialized with (0, 0) and (1, 1) respectively.*/
Camera (vtkCamera& camera);

/** Construct a camera by copying parameters from a VTK camera and a VTK render window. */
Camera (vtkCamera& camera, vtkRenderWindow& window);

/** \brief Focal point or lookAt.
* \note The view direction can be obtained by (focal-pos).normalized ()
*/
Expand Down
6 changes: 2 additions & 4 deletions visualization/include/pcl/visualization/interactor_style.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,9 @@ namespace pcl
bool
saveCameraParameters (const std::string &file);

/** \brief Get camera parameters and save them to a \ref pcl::visualization::Camera.
* \param[out] camera the name of the \ref pcl::visualization::Camera
*/
/** \brief Get camera parameters of a given viewport (0 means default viewport). */
void
getCameraParameters (Camera &camera);
getCameraParameters (Camera &camera, int viewport = 0) const;

/** \brief Load camera parameters from a camera parameter file.
* \param[in] file the name of the camera parameter file
Expand Down
16 changes: 8 additions & 8 deletions visualization/include/pcl/visualization/pcl_visualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1882,11 +1882,9 @@ namespace pcl
void
saveCameraParameters (const std::string &file);

/** \brief Get camera parameters and save them to a pcl::visualization::Camera.
* \param[out] camera the name of the pcl::visualization::Camera
*/
/** \brief Get camera parameters of a given viewport (0 means default viewport). */
void
getCameraParameters (Camera &camera);
getCameraParameters (Camera &camera, int viewport = 0) const;

/** \brief Return a pointer to the underlying VTK Render Window used. */
vtkSmartPointer<vtkRenderWindow>
Expand Down Expand Up @@ -2001,11 +1999,13 @@ namespace pcl
*/
void setDefaultWindowSizeAndPos ();

/** \brief Internal function for setting up camera parameters
* \param[in] argc
* \param[in] argv
/** \brief Set up camera parameters.
*
* Parses command line arguments to find camera parameters (either explicit numbers or a path to a .cam file).
* If not found, will generate a unique .cam file path (based on the rest of command line arguments) and try
* to load that. If it is also not found, just set the defaults.
*/
void setupCamera (int &argc, char **argv);
void setupCamera (int argc, char **argv);

struct PCL_EXPORTS ExitMainLoopTimerCallback : public vtkCommand
{
Expand Down
53 changes: 53 additions & 0 deletions visualization/src/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
*
*/

#include <vtkCamera.h>
#include <vtkRenderWindow.h>

#include <pcl/point_types.h>
#include <pcl/visualization/common/common.h>
#include <pcl/console/print.h>
Expand Down Expand Up @@ -460,6 +463,56 @@ pcl::visualization::getColormapLUT (LookUpTableRepresentationProperties colormap
return true;
}

pcl::visualization::Camera::Camera ()
{
// Set default camera clipping range to something meaningful
clip[0] = 0.01;
clip[1] = 1000.01;

// Look straight along the z-axis
focal[0] = 0.0;
focal[1] = 0.0;
focal[2] = 1.0;

// Position the camera at the origin
pos[0] = 0.0;
pos[1] = 0.0;
pos[2] = 0.0;

// Set the up-vector of the camera to be the y-axis
view[0] = 0.0;
view[1] = 1.0;
view[2] = 0.0;

// Set the camera field of view to about
fovy = 0.8575;

window_pos[0] = 0;
window_pos[1] = 0;
window_size[0] = 1;
window_size[1] = 1;
}

pcl::visualization::Camera::Camera (vtkCamera& camera)
{
camera.GetFocalPoint (focal);
camera.GetPosition (pos);
camera.GetViewUp (view);
camera.GetClippingRange (clip);
fovy = camera.GetViewAngle () / 180.0 * M_PI;
}

pcl::visualization::Camera::Camera (vtkCamera& camera, vtkRenderWindow& window)
: Camera (camera)
{
int *win_pos = window.GetPosition ();
int *win_size = window.GetSize ();
window_pos[0] = win_pos[0];
window_pos[1] = win_pos[1];
window_size[0] = win_size[0];
window_size[1] = win_size[1];
}

/////////////////////////////////////////////////////////////////////////////////////////////
void
pcl::visualization::Camera::computeViewMatrix (Eigen::Matrix4d &view_mat) const
Expand Down
54 changes: 24 additions & 30 deletions visualization/src/interactor_style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/

#include <list>
#include <pcl/common/angles.h>
#include <pcl/visualization/common/io.h>
#include <pcl/visualization/interactor_style.h>
#include <vtkVersion.h>
Expand Down Expand Up @@ -161,22 +162,21 @@ pcl::visualization::PCLVisualizerInteractorStyle::saveCameraParameters (const st

//////////////////////////////////////////////////////////////////////////////////////////////
void
pcl::visualization::PCLVisualizerInteractorStyle::getCameraParameters (pcl::visualization::Camera &camera)
pcl::visualization::PCLVisualizerInteractorStyle::getCameraParameters (pcl::visualization::Camera &camera, int viewport) const
{
FindPokedRenderer (Interactor->GetEventPosition ()[0], Interactor->GetEventPosition ()[1]);

vtkSmartPointer<vtkCamera> cam = Interactor->GetRenderWindow ()->GetRenderers ()->GetFirstRenderer ()->GetActiveCamera ();
cam->GetClippingRange (camera.clip);
cam->GetFocalPoint (camera.focal);
cam->GetPosition (camera.pos);
cam->GetViewUp (camera.view);
camera.fovy = cam->GetViewAngle () / 180.0 * M_PI;
int *win_pos = Interactor->GetRenderWindow ()->GetPosition ();
int *win_size = Interactor->GetRenderWindow ()->GetSize ();
camera.window_pos[0] = win_pos[0];
camera.window_pos[1] = win_pos[1];
camera.window_size[0] = win_size[0];
camera.window_size[1] = win_size[1];
rens_->InitTraversal ();
vtkRenderer* renderer = NULL;
int i = 0;
while ((renderer = rens_->GetNextItem ()) != NULL)
{
if (viewport++ == i)
{
auto window = Interactor->GetRenderWindow ();
auto cam = renderer->GetActiveCamera ();
camera = Camera (*cam, *window);
break;
}
}
}

//////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -867,21 +867,15 @@ pcl::visualization::PCLVisualizerInteractorStyle::OnKeyDown ()
// display current camera settings/parameters
case 'c': case 'C':
{
vtkSmartPointer<vtkCamera> cam = Interactor->GetRenderWindow ()->GetRenderers ()->GetFirstRenderer ()->GetActiveCamera ();
double clip[2], focal[3], pos[3], view[3];
cam->GetClippingRange (clip);
cam->GetFocalPoint (focal);
cam->GetPosition (pos);
cam->GetViewUp (view);
int *win_pos = Interactor->GetRenderWindow ()->GetPosition ();
int *win_size = Interactor->GetRenderWindow ()->GetSize ();
std::cerr << "Clipping plane [near,far] " << clip[0] << ", " << clip[1] << endl <<
"Focal point [x,y,z] " << focal[0] << ", " << focal[1] << ", " << focal[2] << endl <<
"Position [x,y,z] " << pos[0] << ", " << pos[1] << ", " << pos[2] << endl <<
"View up [x,y,z] " << view[0] << ", " << view[1] << ", " << view[2] << endl <<
"Camera view angle [degrees] " << cam->GetViewAngle () << endl <<
"Window size [x,y] " << win_size[0] << ", " << win_size[1] << endl <<
"Window position [x,y] " << win_pos[0] << ", " << win_pos[1] << endl;
Camera cam;
getCameraParameters (cam);
std::cerr << "Clipping plane [near,far] " << cam.clip[0] << ", " << cam.clip[1] << endl <<
"Focal point [x,y,z] " << cam.focal[0] << ", " << cam.focal[1] << ", " << cam.focal[2] << endl <<
"Position [x,y,z] " << cam.pos[0] << ", " << cam.pos[1] << ", " << cam.pos[2] << endl <<
"View up [x,y,z] " << cam.view[0] << ", " << cam.view[1] << ", " << cam.view[2] << endl <<
"Camera view angle [degrees] " << rad2deg (cam.fovy) << endl <<
"Window size [x,y] " << cam.window_size[0] << ", " << cam.window_size[1] << endl <<
"Window position [x,y] " << cam.window_pos[0] << ", " << cam.window_pos[1] << endl;
break;
}
case '=':
Expand Down
53 changes: 6 additions & 47 deletions visualization/src/pcl_visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ void pcl::visualization::PCLVisualizer::setDefaultWindowSizeAndPos ()
}

/////////////////////////////////////////////////////////////////////////////////////////////
void pcl::visualization::PCLVisualizer::setupCamera (int &argc, char **argv)
void pcl::visualization::PCLVisualizer::setupCamera (int argc, char **argv)
{
initCameraParameters ();

Expand Down Expand Up @@ -464,9 +464,9 @@ pcl::visualization::PCLVisualizer::saveCameraParameters (const std::string &file

/////////////////////////////////////////////////////////////////////////////////////////////
void
pcl::visualization::PCLVisualizer::getCameraParameters (pcl::visualization::Camera &camera)
pcl::visualization::PCLVisualizer::getCameraParameters (pcl::visualization::Camera &camera, int viewport) const
{
style_->getCameraParameters (camera);
style_->getCameraParameters (camera, viewport);
}

/////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1904,32 +1904,9 @@ void
pcl::visualization::PCLVisualizer::initCameraParameters ()
{
Camera camera_temp;
// Set default camera parameters to something meaningful
camera_temp.clip[0] = 0.01;
camera_temp.clip[1] = 1000.01;

// Look straight along the z-axis
camera_temp.focal[0] = 0.;
camera_temp.focal[1] = 0.;
camera_temp.focal[2] = 1.;

// Position the camera at the origin
camera_temp.pos[0] = 0.;
camera_temp.pos[1] = 0.;
camera_temp.pos[2] = 0.;

// Set the up-vector of the camera to be the y-axis
camera_temp.view[0] = 0.;
camera_temp.view[1] = 1.;
camera_temp.view[2] = 0.;

// Set the camera field of view to about
camera_temp.fovy = 0.8575;

int scr_size_x = win_->GetScreenSize ()[0];
int scr_size_y = win_->GetScreenSize ()[1];
camera_temp.window_size[0] = scr_size_x / 2;
camera_temp.window_size[1] = scr_size_y / 2;
camera_temp.window_size[0] = win_->GetScreenSize ()[0] / 2;
camera_temp.window_size[1] = win_->GetScreenSize ()[1] / 2;

setCameraParameters (camera_temp);
}
Expand Down Expand Up @@ -2044,25 +2021,7 @@ pcl::visualization::PCLVisualizer::getCameras (std::vector<pcl::visualization::C
rens_->InitTraversal ();
vtkRenderer* renderer = NULL;
while ((renderer = rens_->GetNextItem ()) != NULL)
{
cameras.emplace_back();
cameras.back ().pos[0] = renderer->GetActiveCamera ()->GetPosition ()[0];
cameras.back ().pos[1] = renderer->GetActiveCamera ()->GetPosition ()[1];
cameras.back ().pos[2] = renderer->GetActiveCamera ()->GetPosition ()[2];
cameras.back ().focal[0] = renderer->GetActiveCamera ()->GetFocalPoint ()[0];
cameras.back ().focal[1] = renderer->GetActiveCamera ()->GetFocalPoint ()[1];
cameras.back ().focal[2] = renderer->GetActiveCamera ()->GetFocalPoint ()[2];
cameras.back ().clip[0] = renderer->GetActiveCamera ()->GetClippingRange ()[0];
cameras.back ().clip[1] = renderer->GetActiveCamera ()->GetClippingRange ()[1];
cameras.back ().view[0] = renderer->GetActiveCamera ()->GetViewUp ()[0];
cameras.back ().view[1] = renderer->GetActiveCamera ()->GetViewUp ()[1];
cameras.back ().view[2] = renderer->GetActiveCamera ()->GetViewUp ()[2];
cameras.back ().fovy = renderer->GetActiveCamera ()->GetViewAngle () / 180.0 * M_PI;
cameras.back ().window_size[0] = renderer->GetRenderWindow ()->GetSize ()[0];
cameras.back ().window_size[1] = renderer->GetRenderWindow ()->GetSize ()[1];
cameras.back ().window_pos[0] = 0;
cameras.back ().window_pos[1] = 0;
}
cameras.emplace_back (*renderer->GetActiveCamera (), *renderer->GetRenderWindow ());
}

/////////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 5237f12

Please sign in to comment.