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

Allow saving/restoring camera state to/from file #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
78 changes: 78 additions & 0 deletions GUI/Qt/Components/ViewPanel3D.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ui_ViewPanel3D.h"
#include "GlobalUIModel.h"
#include "Generic3DModel.h"
#include "Generic3DRenderer.h"
#include "itkCommand.h"
#include "IRISException.h"
#include "IRISApplication.h"
Expand Down Expand Up @@ -50,6 +51,9 @@ ViewPanel3D::ViewPanel3D(QWidget *parent) :
m_DropMenu->addAction(ui->actionSave_Viewpoint);
m_DropMenu->addAction(ui->actionRestore_Viewpoint);
m_DropMenu->addSeparator();
m_DropMenu->addAction(ui->actionImport_Viewpoint);
m_DropMenu->addAction(ui->actionExport_Viewpoint);
m_DropMenu->addSeparator();
m_DropMenu->addAction(ui->actionContinuous_Update);
ui->actionContinuous_Update->setObjectName("actionContinuousUpdate");
m_DropMenu->addSeparator();
Expand Down Expand Up @@ -301,6 +305,80 @@ void ViewPanel3D::on_actionRestore_Viewpoint_triggered()
m_Model->RestoreCameraState();
}

void ViewPanel3D::LoadCameraViewpoint(QString file)
{
try
{
std::string utf = to_utf8(file);
CameraState cam = {};
std::ifstream input(utf);
input >> cam.position;
input >> cam.focal_point;
input >> cam.view_up;
input >> cam.view_angle;
input >> cam.parallel_projection;
input >> cam.parallel_scale;
input >> cam.clipping_range;
m_Model->GetRenderer()->SetCameraState(cam);
}
catch(std::exception &exc)
{
ReportNonLethalException(this, exc, "Camera Viewpoint IO Error",
QString("Failed to load camera viewpoint from file"));
}
}

void ViewPanel3D::SaveCameraViewpoint(QString file)
{
try
{
std::string utf = to_utf8(file);
CameraState cam = m_Model->GetRenderer()->GetCameraState();
std::ofstream output(utf);
output << cam.position << std::endl;
output << cam.focal_point << std::endl;
output << cam.view_up << std::endl;
output << cam.view_angle << std::endl;
output << cam.parallel_projection << std::endl;
output << cam.parallel_scale << std::endl;
output << cam.clipping_range << std::endl;
}
catch(std::exception &exc)
{
ReportNonLethalException(this, exc, "Camera Viewpoint IO Error",
QString("Failed to save camera viewpoint to file"));
}
}

void ViewPanel3D::on_actionImport_Viewpoint_triggered()
{
// Ask for a filename
QString selection = ShowSimpleOpenDialogWithHistory(
this, m_Model->GetParentUI(), "CameraViewpoint",
"Load Camera Viewpoint - ITK-SNAP",
"Camera Viewpoint File",
"Camera Files (*.vtkcam)");

// Open the labels from the selection
if(selection.length())
LoadCameraViewpoint(selection);
}

void ViewPanel3D::on_actionExport_Viewpoint_triggered()
{
// Ask for a filename
QString selection = ShowSimpleSaveDialogWithHistory(
this, m_Model->GetParentUI(), "CameraViewpoint",
"Save Camera Viewpoint - ITK-SNAP",
"Camera Viewpoint File",
"Camera Files (*.vtkcam)",
true);

// Open the labels from the selection
if(selection.length())
SaveCameraViewpoint(selection);
}

void ViewPanel3D::on_actionContinuous_Update_triggered()
{
ui->btnUpdateMesh->setVisible(!ui->actionContinuous_Update->isChecked());
Expand Down
10 changes: 10 additions & 0 deletions GUI/Qt/Components/ViewPanel3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ private slots:

void on_actionRestore_Viewpoint_triggered();

void on_actionImport_Viewpoint_triggered();

void on_actionExport_Viewpoint_triggered();

void on_actionContinuous_Update_triggered();

void on_btnMenu_pressed();
Expand Down Expand Up @@ -105,6 +109,12 @@ private slots:

void UpdateActionButtons();

// Load camera viewpoint
void LoadCameraViewpoint(QString file);

// Save camera viewpoint
void SaveCameraViewpoint(QString file);

// Apply color bar visibility based on the active mesh layer type
void ApplyDefaultColorBarVisibility();

Expand Down
16 changes: 16 additions & 0 deletions GUI/Qt/Components/ViewPanel3D.ui
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,22 @@ font-size: 11px;
<string>Ctrl+K, R</string>
</property>
</action>
<action name="actionImport_Viewpoint">
<property name="text">
<string>Import Viewpoint</string>
</property>
<property name="shortcut">
<string>Ctrl+K, I</string>
</property>
</action>
<action name="actionExport_Viewpoint">
<property name="text">
<string>Export Viewpoint</string>
</property>
<property name="shortcut">
<string>Ctrl+K, E</string>
</property>
</action>
<action name="actionContinuous_Update">
<property name="checkable">
<bool>true</bool>
Expand Down