Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new constructors to PCLVisualizer #2004

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions visualization/include/pcl/visualization/pcl_visualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ namespace pcl
*/
PCLVisualizer (int &argc, char **argv, const std::string &name = "",
PCLVisualizerInteractorStyle* style = PCLVisualizerInteractorStyle::New (), const bool create_interactor = true);

/** \brief PCL Visualizer constructor.
* \param[in] custom vtk renderer
* \param[in] custom vtk render window
* \param[in] create_interactor if true (default), create an interactor, false otherwise
*/
PCLVisualizer (vtkSmartPointer<vtkRenderer> ren, vtkSmartPointer<vtkRenderWindow> wind, const std::string &name = "", const bool create_interactor = true);

/** \brief PCL Visualizer constructor.
* \param[in] argc
* \param[in] argv
* \param[in] custom vtk renderer
* \param[in] custom vtk render window
* \param[in] style interactor style (defaults to PCLVisualizerInteractorStyle)
* \param[in] create_interactor if true (default), create an interactor, false otherwise
*/
PCLVisualizer (int &argc, char **argv, vtkSmartPointer<vtkRenderer> ren, vtkSmartPointer<vtkRenderWindow> wind, const std::string &name = "",
PCLVisualizerInteractorStyle* style = PCLVisualizerInteractorStyle::New (),
const bool create_interactor = true);


/** \brief PCL Visualizer destructor. */
virtual ~PCLVisualizer ();
Expand Down Expand Up @@ -1939,6 +1959,35 @@ namespace pcl
vtkSmartPointer<vtkRenderWindowInteractor> interactor_;
#endif
private:
/** \brief Internal function for renderer setup
* \param[in] vtk renderer
*/
void setupRenderer (vtkSmartPointer<vtkRenderer> ren);

/** \brief Internal function for setting up FPS callback
* \param[in] vtk renderer
*/
void setupFPSCallback (const vtkSmartPointer<vtkRenderer>& ren);

/** \brief Internal function for setting up render window
* \param[in] name the window name
*/
void setupRenderWindow (const std::string& name);

/** \brief Internal function for setting up interactor style
*/
void setupStyle ();

/** \brief Internal function for setting the default render window size and position on screen
*/
void setDefaultWindowSizeAndPos ();

/** \brief Internal function for setting up camera parameters
* \param[in] argc
* \param[in] argv
*/
void setupCamera (int &argc, char **argv);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comment: I know this PR is only refactoring, but this signature is completely cryptic for me. Do you have any idea which format one passes to these camera parameters? Is there any way we can add some documentation to improve it this an the other signatures which pass these? From our current docs there's nothing on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, we pass the program arguments, which may contain -cam ".cam". It is good idea to add the documentation for it. I will do it, but in the another merge request. We are using my change in our project at the moment, and it will be nice to use pcl master repo instead of my fork.


struct PCL_EXPORTS ExitMainLoopTimerCallback : public vtkCommand
{
static ExitMainLoopTimerCallback* New ()
Expand Down
278 changes: 171 additions & 107 deletions visualization/src/pcl_visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,62 +128,23 @@ pcl::visualization::PCLVisualizer::PCLVisualizer (const std::string &name, const
, exit_main_loop_timer_callback_ ()
, exit_callback_ ()
, rens_ (vtkSmartPointer<vtkRendererCollection>::New ())
, win_ ()
, win_ (vtkSmartPointer<vtkRenderWindow>::New ())
, style_ (vtkSmartPointer<pcl::visualization::PCLVisualizerInteractorStyle>::New ())
, cloud_actor_map_ (new CloudActorMap)
, shape_actor_map_ (new ShapeActorMap)
, coordinate_actor_map_ (new CoordinateActorMap)
, camera_set_ ()
, camera_file_loaded_ (false)
{
// Create a Renderer
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New ();
ren->AddObserver (vtkCommand::EndEvent, update_fps_);
// Add it to the list of renderers
rens_->AddItem (ren);

// FPS callback
vtkSmartPointer<vtkTextActor> txt = vtkSmartPointer<vtkTextActor>::New ();
update_fps_->actor = txt;
update_fps_->pcl_visualizer = this;
update_fps_->decimated = false;
ren->AddActor (txt);
txt->SetInput("0 FPS");

// Create a RendererWindow
win_ = vtkSmartPointer<vtkRenderWindow>::New ();
win_->SetWindowName (name.c_str ());

// Get screen size
int scr_size_x = win_->GetScreenSize ()[0];
int scr_size_y = win_->GetScreenSize ()[1];
// Set the window size as 1/2 of the screen size
win_->SetSize (scr_size_x / 2, scr_size_y / 2);

// By default, don't use vertex buffer objects
use_vbos_ = false;

// Add all renderers to the window
rens_->InitTraversal ();
vtkRenderer* renderer = NULL;
while ((renderer = rens_->GetNextItem ()) != NULL)
win_->AddRenderer (renderer);

// Set renderer window in case no interactor is created
style_->setRenderWindow (win_);

// Create the interactor style
style_->Initialize ();
style_->setRendererCollection (rens_);
style_->setCloudActorMap (cloud_actor_map_);
style_->setShapeActorMap (shape_actor_map_);
style_->UseTimersOn ();
style_->setUseVbos(use_vbos_);
setupRenderer (ren);
setupFPSCallback (ren);
setupRenderWindow (name);
setDefaultWindowSizeAndPos ();
setupStyle ();

if (create_interactor)
createInteractor ();

win_->SetWindowName (name.c_str ());
}

/////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -197,86 +158,92 @@ pcl::visualization::PCLVisualizer::PCLVisualizer (int &argc, char **argv, const
, exit_main_loop_timer_callback_ ()
, exit_callback_ ()
, rens_ (vtkSmartPointer<vtkRendererCollection>::New ())
, win_ ()
, win_ (vtkSmartPointer<vtkRenderWindow>::New ())
, style_ (style)
, cloud_actor_map_ (new CloudActorMap)
, shape_actor_map_ (new ShapeActorMap)
, coordinate_actor_map_ (new CoordinateActorMap)
, camera_set_ ()
, camera_file_loaded_ (false)
{
// Create a Renderer
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New ();
ren->AddObserver (vtkCommand::EndEvent, update_fps_);
// Add it to the list of renderers
rens_->AddItem (ren);
setupRenderer (ren);
setupFPSCallback (ren);
setupRenderWindow (name);
setupStyle ();
setupCamera (argc, argv);

// FPS callback
vtkSmartPointer<vtkTextActor> txt = vtkSmartPointer<vtkTextActor>::New ();
update_fps_->actor = txt;
update_fps_->pcl_visualizer = this;
update_fps_->decimated = false;
ren->AddActor (txt);
txt->SetInput("0 FPS");
if(!camera_set_ && !camera_file_loaded_)
setDefaultWindowSizeAndPos ();

// Create a RendererWindow
win_ = vtkSmartPointer<vtkRenderWindow>::New ();
win_->SetWindowName (name.c_str ());

// By default, don't use vertex buffer objects
use_vbos_ = false;

// Add all renderers to the window
rens_->InitTraversal ();
vtkRenderer* renderer = NULL;
while ((renderer = rens_->GetNextItem ()) != NULL)
win_->AddRenderer (renderer);

// Set renderer window in case no interactor is created
style_->setRenderWindow (win_);
if (create_interactor)
createInteractor ();

// Create the interactor style
style_->Initialize ();
style_->setRendererCollection (rens_);
style_->setCloudActorMap (cloud_actor_map_);
style_->setShapeActorMap (shape_actor_map_);
style_->UseTimersOn ();
//window name should be reset due to its reset somewhere in camera initialization
win_->SetWindowName (name.c_str ());
}

// Get screen size
int scr_size_x = win_->GetScreenSize ()[0];
int scr_size_y = win_->GetScreenSize ()[1];
/////////////////////////////////////////////////////////////////////////////////////////////
pcl::visualization::PCLVisualizer::PCLVisualizer (vtkSmartPointer<vtkRenderer> ren, vtkSmartPointer<vtkRenderWindow> wind,
const std::string &name, const bool create_interactor)
: interactor_ ()
, update_fps_ (vtkSmartPointer<FPSCallback>::New ())
#if !((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION <= 4))
, stopped_ ()
, timer_id_ ()
#endif
, exit_main_loop_timer_callback_ ()
, exit_callback_ ()
, rens_ (vtkSmartPointer<vtkRendererCollection>::New ())
, win_ (wind)
, style_ (vtkSmartPointer<pcl::visualization::PCLVisualizerInteractorStyle>::New ())
, cloud_actor_map_ (new CloudActorMap)
, shape_actor_map_ (new ShapeActorMap)
, coordinate_actor_map_ (new CoordinateActorMap)
, camera_set_ ()
, camera_file_loaded_ (false)
{
setupRenderer (ren);
setupFPSCallback (ren);
setupRenderWindow (name);
setDefaultWindowSizeAndPos ();
setupStyle ();

// Set default camera parameters
initCameraParameters ();
if (create_interactor)
createInteractor ();
}

// Parse the camera settings and update the internal camera
camera_set_ = getCameraParameters (argc, argv);
// Calculate unique camera filename for camera parameter saving/restoring
if (!camera_set_)
{
std::string camera_file = getUniqueCameraFile (argc, argv);
if (!camera_file.empty ())
{
if (boost::filesystem::exists (camera_file) && style_->loadCameraParameters (camera_file))
{
camera_file_loaded_ = true;
}
else
{
style_->setCameraFile (camera_file);
}
}
}
// Set the window size as 1/2 of the screen size or the user given parameter
/////////////////////////////////////////////////////////////////////////////////////////////
pcl::visualization::PCLVisualizer::PCLVisualizer (int &argc, char **argv, vtkSmartPointer<vtkRenderer> ren, vtkSmartPointer<vtkRenderWindow> wind,
const std::string &name, PCLVisualizerInteractorStyle* style, const bool create_interactor)
: interactor_ ()
, update_fps_ (vtkSmartPointer<FPSCallback>::New ())
#if !((VTK_MAJOR_VERSION == 5) && (VTK_MINOR_VERSION <= 4))
, stopped_ ()
, timer_id_ ()
#endif
, exit_main_loop_timer_callback_ ()
, exit_callback_ ()
, rens_ (vtkSmartPointer<vtkRendererCollection>::New ())
, win_ (wind)
, style_ (style)
, cloud_actor_map_ (new CloudActorMap)
, shape_actor_map_ (new ShapeActorMap)
, coordinate_actor_map_ (new CoordinateActorMap)
, camera_set_ ()
, camera_file_loaded_ (false)
{
setupRenderer (ren);
setupFPSCallback (ren);
setupRenderWindow (name);
setupStyle ();
setupCamera (argc, argv);
if (!camera_set_ && !camera_file_loaded_)
{
win_->SetSize (scr_size_x/2, scr_size_y/2);
win_->SetPosition (0, 0);
}

setDefaultWindowSizeAndPos ();
if (create_interactor)
createInteractor ();

//window name should be reset due to its reset somewhere in camera initialization
win_->SetWindowName (name.c_str ());
}

Expand Down Expand Up @@ -387,6 +354,103 @@ pcl::visualization::PCLVisualizer::setupInteractor (
// iren->SetPicker (pp);
}

/////////////////////////////////////////////////////////////////////////////////////////////
void pcl::visualization::PCLVisualizer::setupRenderer (vtkSmartPointer<vtkRenderer> ren)
{
if (!ren)
PCL_ERROR ("Passed pointer to renderer is null");

ren->AddObserver (vtkCommand::EndEvent, update_fps_);
// Add it to the list of renderers
rens_->AddItem (ren);
}

/////////////////////////////////////////////////////////////////////////////////////////////
void pcl::visualization::PCLVisualizer::setupFPSCallback (const vtkSmartPointer<vtkRenderer>& ren)
{
if (!ren)
PCL_ERROR ("Passed pointer to renderer is null");
// FPS callback
vtkSmartPointer<vtkTextActor> txt = vtkSmartPointer<vtkTextActor>::New ();
update_fps_->actor = txt;
update_fps_->pcl_visualizer = this;
update_fps_->decimated = false;
ren->AddActor (txt);
txt->SetInput ("0 FPS");
}

/////////////////////////////////////////////////////////////////////////////////////////////
void pcl::visualization::PCLVisualizer::setupRenderWindow (const std::string& name)
{
if (!win_)
PCL_ERROR ("Pointer to render window is null");

win_->SetWindowName (name.c_str ());

// By default, don't use vertex buffer objects
use_vbos_ = false;

// Add all renderers to the window
rens_->InitTraversal ();
vtkRenderer* renderer = NULL;
while ((renderer = rens_->GetNextItem ()) != NULL)
win_->AddRenderer (renderer);
}

/////////////////////////////////////////////////////////////////////////////////////////////
void pcl::visualization::PCLVisualizer::setupStyle ()
{
if (!style_)
PCL_ERROR ("Pointer to style is null");

// Set rend erer window in case no interactor is created
style_->setRenderWindow (win_);

// Create the interactor style
style_->Initialize ();
style_->setRendererCollection (rens_);
style_->setCloudActorMap (cloud_actor_map_);
style_->setShapeActorMap (shape_actor_map_);
style_->UseTimersOn ();
style_->setUseVbos (use_vbos_);
}

/////////////////////////////////////////////////////////////////////////////////////////////
void pcl::visualization::PCLVisualizer::setDefaultWindowSizeAndPos ()
{
if (!win_)
PCL_ERROR ("Pointer to render window is null");
int scr_size_x = win_->GetScreenSize ()[0];
int scr_size_y = win_->GetScreenSize ()[1];
win_->SetSize (scr_size_x / 2, scr_size_y / 2);
win_->SetPosition (0, 0);
}

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

// Parse the camera settings and update the internal camera
camera_set_ = getCameraParameters (argc, argv);
// Calculate unique camera filename for camera parameter saving/restoring
if (!camera_set_)
{
std::string camera_file = getUniqueCameraFile (argc, argv);
if (!camera_file.empty ())
{
if (boost::filesystem::exists (camera_file) && style_->loadCameraParameters (camera_file))
{
camera_file_loaded_ = true;
}
else
{
style_->setCameraFile (camera_file);
}
}
}
}

/////////////////////////////////////////////////////////////////////////////////////////////
pcl::visualization::PCLVisualizer::~PCLVisualizer ()
{
Expand Down