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

ENH: Allow for custom presets prefix and tooltips #1052

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
60 changes: 60 additions & 0 deletions Libs/MRML/Core/Testing/vtkMRMLSliceNodeTest1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ int GetSliceOrientationPresetTest();
int GetSliceOrientationPresetNameTest();
int SetOrientationTest();
int InitializeDefaultMatrixTest();
int SliceOrientationPresetPrefixTest();
int SliceOrientationPresetTooltipTest();

//----------------------------------------------------------------------------
int vtkMRMLSliceNodeTest1(int , char * [] )
Expand All @@ -48,6 +50,8 @@ int vtkMRMLSliceNodeTest1(int , char * [] )
CHECK_EXIT_SUCCESS(GetSliceOrientationPresetNameTest());
CHECK_EXIT_SUCCESS(SetOrientationTest());
CHECK_EXIT_SUCCESS(InitializeDefaultMatrixTest());
CHECK_EXIT_SUCCESS(SliceOrientationPresetPrefixTest());
CHECK_EXIT_SUCCESS(SliceOrientationPresetTooltipTest());

return EXIT_SUCCESS;
}
Expand Down Expand Up @@ -426,3 +430,59 @@ int InitializeDefaultMatrixTest()

return EXIT_SUCCESS;
}

//----------------------------------------------------------------------------
int SliceOrientationPresetPrefixTest()
{
vtkNew<vtkMRMLSliceNode> sliceNode;

vtkNew<vtkMatrix3x3> testMatrix;
sliceNode->AddSliceOrientationPreset("test_prefix", testMatrix.GetPointer());
CHECK_STD_STRING(sliceNode->GetSliceOrientationPresetPrefix("test_prefix"), "");
CHECK_STD_STRING(sliceNode->GetSliceOrientationPresetPrefix("Reformat"), "");

TESTING_OUTPUT_ASSERT_ERRORS_BEGIN();
CHECK_BOOL(sliceNode->RenameSliceOrientationPresetPrefix("", "t:"), false);
TESTING_OUTPUT_ASSERT_ERRORS_END();

TESTING_OUTPUT_ASSERT_ERRORS_BEGIN();
CHECK_BOOL(sliceNode->RenameSliceOrientationPresetPrefix("wrong name", "t:"), false);
TESTING_OUTPUT_ASSERT_ERRORS_END();

TESTING_OUTPUT_ASSERT_ERRORS_BEGIN();
CHECK_BOOL(sliceNode->RenameSliceOrientationPresetPrefix("Reformat", "t:"), false);
TESTING_OUTPUT_ASSERT_ERRORS_END();

CHECK_BOOL(sliceNode->RenameSliceOrientationPresetPrefix("test_prefix", "t:"), true);
CHECK_STD_STRING(sliceNode->GetSliceOrientationPresetPrefix("test_prefix"), "t:");

return EXIT_SUCCESS;
}

//----------------------------------------------------------------------------
int SliceOrientationPresetTooltipTest()
{
vtkNew<vtkMRMLSliceNode> sliceNode;

vtkNew<vtkMatrix3x3> testMatrix;
sliceNode->AddSliceOrientationPreset("test_tooltip", testMatrix.GetPointer());
CHECK_STD_STRING(sliceNode->GetSliceOrientationPresetTooltip("test_tooltip"), "Oblique");
CHECK_STD_STRING(sliceNode->GetSliceOrientationPresetTooltip("Reformat"), "Oblique");

TESTING_OUTPUT_ASSERT_ERRORS_BEGIN();
CHECK_BOOL(sliceNode->RenameSliceOrientationPresetTooltip("", "this is a tip"), false);
TESTING_OUTPUT_ASSERT_ERRORS_END();

TESTING_OUTPUT_ASSERT_ERRORS_BEGIN();
CHECK_BOOL(sliceNode->RenameSliceOrientationPresetTooltip("wrong name", "this is a tip"), false);
TESTING_OUTPUT_ASSERT_ERRORS_END();

TESTING_OUTPUT_ASSERT_ERRORS_BEGIN();
CHECK_BOOL(sliceNode->RenameSliceOrientationPresetTooltip("Reformat", "this is a tip"), false);
TESTING_OUTPUT_ASSERT_ERRORS_END();

CHECK_BOOL(sliceNode->RenameSliceOrientationPresetTooltip("test_tooltip", "this is a tip"), true);
CHECK_STD_STRING(sliceNode->GetSliceOrientationPresetTooltip("test_tooltip"), "this is a tip");

return EXIT_SUCCESS;
}
189 changes: 133 additions & 56 deletions Libs/MRML/Core/vtkMRMLSliceNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,8 @@ bool vtkMRMLSliceNode::MatrixAreEqual(const vtkMatrix4x4 *matrix,
//----------------------------------------------------------------------------
vtkMatrix3x3 *vtkMRMLSliceNode::GetSliceOrientationPreset(const std::string &name)
{
std::vector< OrientationPresetType >::iterator it;
for (it = this->OrientationMatrices.begin(); it != this->OrientationMatrices.end(); ++it)
{
if (it->first == name)
{
return it->second;
}
}

vtkErrorMacro("GetSliceOrientationPreset: invalid orientation preset name: " << name);
return NULL;
OrientationPresetType* preset = this->GetOrientationPreset(name);
return preset ? preset->Orientation : nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

nullptr -> NULL, since we will most likely create 4.10.1 ... let's continue support c++98

}

//----------------------------------------------------------------------------
Expand All @@ -366,11 +357,9 @@ std::string vtkMRMLSliceNode::GetSliceOrientationPresetName(vtkMatrix3x3* orient
it != this->OrientationMatrices.rend();
++it)
{
std::string presetName = it->first;
vtkMatrix3x3* presetOrientationMatrix = this->GetSliceOrientationPreset(presetName);
if (vtkAddonMathUtilities::MatrixAreEqual(orientationMatrix, presetOrientationMatrix))
if (vtkAddonMathUtilities::MatrixAreEqual(orientationMatrix, it->Orientation))
{
return presetName;
return it->Name;
}
}
return std::string();
Expand All @@ -391,6 +380,30 @@ std::string vtkMRMLSliceNode::GetOrientation(vtkMatrix4x4 *sliceToRAS)
return "Reformat";
}

//----------------------------------------------------------------------------
std::string vtkMRMLSliceNode::GetSliceOrientationPresetPrefix(const std::string& name)
{
if (name == "Reformat")
{
return "";
}

OrientationPresetType* preset = this->GetOrientationPreset(name);
return preset ? preset->Prefix : "";
}

//----------------------------------------------------------------------------
std::string vtkMRMLSliceNode::GetSliceOrientationPresetTooltip(const std::string& name)
{
if (name == "Reformat")
{
return "Oblique";
}

OrientationPresetType* preset = this->GetOrientationPreset(name);
return preset ? preset->Tooltip : "";
}

//----------------------------------------------------------------------------
void vtkMRMLSliceNode::GetSliceOrientationPresetNames(vtkStringArray *presetOrientationNames)
{
Expand All @@ -408,7 +421,7 @@ void vtkMRMLSliceNode::GetSliceOrientationPresetNames(vtkStringArray *presetOrie
it != this->OrientationMatrices.end();
++it)
{
presetOrientationNames->SetValue(id, it->first);
presetOrientationNames->SetValue(id, it->Name);
id++;
}
}
Expand All @@ -420,7 +433,9 @@ int vtkMRMLSliceNode::GetNumberOfSliceOrientationPresets() const
}

//----------------------------------------------------------------------------
bool vtkMRMLSliceNode::AddSliceOrientationPreset(const std::string &name, vtkMatrix3x3 *orientationMatrix)
bool vtkMRMLSliceNode::AddSliceOrientationPreset(
const std::string &name, vtkMatrix3x3 *orientationMatrix,
const std::string &prefix, const std::string &tooltip)
{
if (name == "Reformat")
{
Expand All @@ -429,17 +444,19 @@ bool vtkMRMLSliceNode::AddSliceOrientationPreset(const std::string &name, vtkMat
return false;
}

std::vector< OrientationPresetType >::iterator it;
for (it = this->OrientationMatrices.begin(); it != this->OrientationMatrices.end(); ++it)
if (this->GetOrientationPreset(name, false))
{
if (it->first == name)
{
vtkDebugMacro("AddSliceOrientationPreset: the orientation preset " << name << " is already stored.");
return false;
}
vtkDebugMacro("AddSliceOrientationPreset: the orientation preset " << name << " is already stored.");
return false;
}

this->OrientationMatrices.push_back(OrientationPresetType(name, orientationMatrix));
OrientationPresetType newOrientation;
newOrientation.Name = name;
newOrientation.Orientation = orientationMatrix;
newOrientation.Prefix = prefix;
newOrientation.Tooltip = tooltip;

this->OrientationMatrices.push_back(newOrientation);
return true;
}

Expand All @@ -449,7 +466,7 @@ bool vtkMRMLSliceNode::RemoveSliceOrientationPreset(const std::string &name)
std::vector< OrientationPresetType >::iterator it;
for (it = this->OrientationMatrices.begin(); it != this->OrientationMatrices.end(); ++it)
{
if (it->first == name)
if (it->Name == name)
{
this->OrientationMatrices.erase(it);
return true;
Expand All @@ -460,6 +477,26 @@ bool vtkMRMLSliceNode::RemoveSliceOrientationPreset(const std::string &name)
return false;
}

//----------------------------------------------------------------------------
OrientationPresetType* vtkMRMLSliceNode::GetOrientationPreset(const std::string &name, bool error)
Copy link
Member

Choose a reason for hiding this comment

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

bool error -> bool displayError

Copy link
Member

Choose a reason for hiding this comment

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

Instead, what do you think of displaying the error on the user side ?

Copy link
Author

Choose a reason for hiding this comment

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

This is a protected method. It only has this option so you can query the preset existence in HasSliceOrientationPreset without an error.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would not complicate the API with with this "bool error" flag. Since the caller can detect if a preset is not found (by getting NULL as result), it is not essential to log an error..

{
std::vector< OrientationPresetType >::iterator it;
for (it = this->OrientationMatrices.begin(); it != this->OrientationMatrices.end(); ++it)
{
if (it->Name == name)
{
return &(*it);
}
}

if (error)
{
vtkErrorMacro("GetOrientationPreset: The orientation preset "
"'" << name << "' does NOT exist.");
}
return nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

-> NULL

}

//----------------------------------------------------------------------------
bool vtkMRMLSliceNode::RenameSliceOrientationPreset(const std::string &name, const std::string &updatedName)
{
Expand All @@ -479,43 +516,63 @@ bool vtkMRMLSliceNode::RenameSliceOrientationPreset(const std::string &name, con
this->SetOrientationReference(updatedName.c_str());
}

std::vector< OrientationPresetType >::iterator it;
for (it = this->OrientationMatrices.begin(); it != this->OrientationMatrices.end(); ++it)
OrientationPresetType* preset = this->GetOrientationPreset(name);
if (preset)
{
if (it->first == name)
{
it->first = updatedName;
return true;
}
preset->Name = updatedName;
return true;
}

vtkErrorMacro("RenameSliceOrientationPreset: The orientation preset "
"'" << name << "' does NOT exist.");
return false;
}

//----------------------------------------------------------------------------
bool vtkMRMLSliceNode::HasSliceOrientationPreset(const std::string &name)
bool vtkMRMLSliceNode::RenameSliceOrientationPresetPrefix(const std::string &name, const std::string &prefix)
Copy link
Member

Choose a reason for hiding this comment

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

What about RenameSliceOrientationPresetPrefix -> SetSliceOrientationPresetPrefix ?

{
if (name == "Reformat")
{
vtkWarningMacro("HasSliceOrientationPreset: 'Reformat' refers to any "
"arbitrary orientation. It can NOT be used as a preset name.");
vtkErrorMacro("RenameSliceOrientationPresetPrefix: 'Reformat' refers to any "
"arbitrary orientation. It can NOT be used as a preset name.");
return false;
}
OrientationPresetType* preset = this->GetOrientationPreset(name);
if (preset && preset->Prefix.compare(prefix) != 0)
{
preset->Prefix = prefix;
return true;
}
return false;
}

std::vector< OrientationPresetType >::iterator it;
for (it = this->OrientationMatrices.begin(); it != this->OrientationMatrices.end(); ++it)
//----------------------------------------------------------------------------
bool vtkMRMLSliceNode::RenameSliceOrientationPresetTooltip(const std::string &name, const std::string &tip)
Copy link
Member

Choose a reason for hiding this comment

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

RenameSliceOrientationPresetTooltip -> SetSliceOrientationPresetTooltip

{
if (name == "Reformat")
{
if (it->first == name)
{
return true;
}
vtkErrorMacro("RenameSliceOrientationPresetTooltip: 'Reformat' refers to any "
"arbitrary orientation. It can NOT be used as a preset name.");
return false;
}
OrientationPresetType* preset = this->GetOrientationPreset(name);
if (preset && preset->Tooltip.compare(tip) != 0)
{
preset->Tooltip = tip;
return true;
}

return false;
}

//----------------------------------------------------------------------------
bool vtkMRMLSliceNode::HasSliceOrientationPreset(const std::string &name)
{
if (name == "Reformat")
{
vtkWarningMacro("HasSliceOrientationPreset: 'Reformat' refers to any "
"arbitrary orientation. It can NOT be used as a preset name.");
return false;
}
return this->GetOrientationPreset(name, false) != nullptr;
}

//----------------------------------------------------------------------------
void vtkMRMLSliceNode::InitializeAxialMatrix(vtkMatrix3x3* orientationMatrix)
{
Expand Down Expand Up @@ -596,9 +653,12 @@ void vtkMRMLSliceNode::AddDefaultSliceOrientationPresets(vtkMRMLScene* scene)
scene->AddDefaultNode(defaultNode);
}
vtkMRMLSliceNode * defaultSliceNode = vtkMRMLSliceNode::SafeDownCast(defaultNode);
defaultSliceNode->AddSliceOrientationPreset("Axial", axialSliceToRAS.GetPointer());
defaultSliceNode->AddSliceOrientationPreset("Sagittal", sagittalSliceToRAS.GetPointer());
defaultSliceNode->AddSliceOrientationPreset("Coronal", coronalSliceToRAS.GetPointer());
defaultSliceNode->AddSliceOrientationPreset(
"Axial", axialSliceToRAS.GetPointer(), "S: ", "I <-----> S");
defaultSliceNode->AddSliceOrientationPreset(
"Sagittal", sagittalSliceToRAS.GetPointer(), "R: ", "L <-----> R");
defaultSliceNode->AddSliceOrientationPreset(
"Coronal", coronalSliceToRAS.GetPointer(), "A: ", "P <-----> A");
}

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -878,14 +938,17 @@ void vtkMRMLSliceNode::WriteXML(ostream& of, int nIndent)
{
for (j=0; j<3; j++)
{
ss << it->second->GetElement(i,j);
ss << it->Orientation->GetElement(i,j);
if ( !( i==2 && j==2) )
{
ss << " ";
}
}
}
of << " orientationMatrix"<< this->URLEncodeString(it->first.c_str()) <<"=\"" << ss.str().c_str() << "\"";
of << " orientationMatrix"<< this->URLEncodeString(it->Name.c_str()) <<"=\"" << ss.str().c_str()
<< " " << this->URLEncodeString(it->Prefix.c_str())
<< " " << this->URLEncodeString(it->Tooltip.c_str())
<< "\"";
}

of << " orientation=\"" << this->GetOrientation() << "\"";
Expand Down Expand Up @@ -1173,8 +1236,17 @@ void vtkMRMLSliceNode::ReadXMLAttributes(const char** atts)
orientationMatrix->SetElement(i,j,val);
}
}
std::string prefix;
ss >> prefix;

std::string tooltip;
ss >> tooltip;

name.erase(0,17);
this->AddSliceOrientationPreset(name, orientationMatrix.GetPointer());
this->AddSliceOrientationPreset(
name, orientationMatrix.GetPointer(),
this->URLDecodeString(prefix.c_str()),
this->URLDecodeString(tooltip.c_str()));
}
else if (!strcmp(attName, "prescribedSliceSpacing"))
{
Expand Down Expand Up @@ -1286,8 +1358,11 @@ void vtkMRMLSliceNode::Copy(vtkMRMLNode *anode)

for (int i = 0; i < namedOrientations->GetNumberOfValues(); i++)
{
this->AddSliceOrientationPreset(namedOrientations->GetValue(i),
node->GetSliceOrientationPreset(namedOrientations->GetValue(i)));
std::string name = namedOrientations->GetValue(i);
this->AddSliceOrientationPreset(name,
node->GetSliceOrientationPreset(name),
node->GetSliceOrientationPresetPrefix(name),
node->GetSliceOrientationPresetTooltip(name));
}

std::string orientation = node->GetOrientation();
Expand Down Expand Up @@ -1427,8 +1502,10 @@ void vtkMRMLSliceNode::PrintSelf(ostream& os, vtkIndent indent)
std::vector< OrientationPresetType >::iterator it;
for (it = this->OrientationMatrices.begin(); it != this->OrientationMatrices.end(); ++it)
{
os << indent << "OrientationMatrix"<< this->URLEncodeString(it->first.c_str()) <<": \n";
it->second->PrintSelf(os, indent.GetNextIndent());
os << indent << "OrientationMatrix"<< this->URLEncodeString(it->Name.c_str()) <<": \n";
it->Orientation->PrintSelf(os, indent.GetNextIndent());
os << indent.GetNextIndent() << "Prefix: " << this->URLEncodeString(it->Prefix.c_str()) << "\n";
os << indent.GetNextIndent() << "Tooltip: " << this->URLEncodeString(it->Tooltip.c_str()) << "\n";
}

os << indent << "XYToRAS: \n";
Expand Down
Loading