Skip to content

Commit

Permalink
ENH: Many fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lassoan committed Jan 16, 2025
1 parent 9c72216 commit 51112eb
Show file tree
Hide file tree
Showing 25 changed files with 2,130 additions and 1,780 deletions.
45 changes: 45 additions & 0 deletions Libs/MRML/Core/vtkCodedEntry.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,48 @@ bool vtkCodedEntry::SetFromString(const std::string& content)
// CodeMeaning is optional
return success;
}

//----------------------------------------------------------------------------
bool vtkCodedEntry::AreEqual(vtkCodedEntry* entry1, vtkCodedEntry* entry2)
{
if (entry1 == entry2)
{
// they are the same object or both nullptr
return true;
}
if (!entry1 || !entry2)
{
// only one is nullptr
return false;
}
// Neither entry is nullptr
if ((entry1->GetCodeValue() == nullptr) != (entry2->GetCodeValue() == nullptr))
{
// Only one of them is nullptr
return false;
}
if (entry2->GetCodeValue() && entry1->GetCodeValue() && strcmp(entry2->GetCodeValue(), entry1->GetCodeValue()) != 0)
{
return false;
}
if ((entry1->GetCodingSchemeDesignator() == nullptr) != (entry2->GetCodingSchemeDesignator() == nullptr))
{
// Only one of them is nullptr
return false;
}
if (entry2->GetCodingSchemeDesignator() && entry1->GetCodingSchemeDesignator()
&& strcmp(entry2->GetCodingSchemeDesignator(), entry1->GetCodingSchemeDesignator()) != 0)
{
return false;
}
if ((entry1->GetCodeMeaning() == nullptr) != (entry2->GetCodeMeaning() == nullptr))
{
// Only one of them is nullptr
return false;
}
if (entry2->GetCodeMeaning() && entry1->GetCodeMeaning() && strcmp(entry2->GetCodeMeaning(), entry1->GetCodeMeaning()) != 0)
{
return false;
}
return true;
}
2 changes: 2 additions & 0 deletions Libs/MRML/Core/vtkCodedEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class VTK_MRML_EXPORT vtkCodedEntry : public vtkObject
/// \return true on success
bool SetFromString(const std::string& content);

static bool AreEqual(vtkCodedEntry* entry1, vtkCodedEntry* entry2);

protected:
vtkCodedEntry();
~vtkCodedEntry() override;
Expand Down
115 changes: 67 additions & 48 deletions Libs/MRML/Core/vtkMRMLColorNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ void vtkMRMLColorNode::PrintSelf(ostream& os, vtkIndent indent)
}
os << indent << indent << i << " ";
os << this->GetColorName(i);
os << " (" << vtkMRMLColorNode::GetNameSourceAsString(prop.NameSource) << ")";
double color[4];
this->GetColor(i, color);
os << " (" << color[0] << ", " << color[1] << ", " << color[2] << ", " << color[3] << ")";
Expand Down Expand Up @@ -278,7 +277,7 @@ bool vtkMRMLColorNode::SetNameFromColor(int index)
ss.setf(std::ios::fixed, std::ios::floatfield);
ss << "R=" << rgba[0] << " G=" << rgba[1] << " B=" << rgba[2] << " A=" << rgba[3];
vtkDebugMacro("SetNamesFromColors: " << index << " Name = " << ss.str().c_str());
if (this->SetColorName(index, ss.str().c_str(), NameAutoGeneratedFromColor) == 0)
if (this->SetColorName(index, ss.str().c_str()))
{
vtkErrorMacro("SetNamesFromColors: Error setting color name " << index << " Name = " << ss.str().c_str());
return false;
Expand Down Expand Up @@ -309,45 +308,45 @@ bool vtkMRMLColorNode::GetColorDefined(int index)
{
return false;
}
// For improved backward compatibility with older extensions,
// if the color name is NoName name then the we consider the color defined.
if (this->GetNoName() && this->GetColorName(index) &&
strcmp(this->GetNoName(), this->GetColorName(index)) == 0)
{
return false;
}
return true;
}

//---------------------------------------------------------------------------
bool vtkMRMLColorNode::SetColorDefined(int index, bool defined)
const char *vtkMRMLColorNode::GetColorName(int index)
{
// Do not use GetProperty because the content of the locally copied property's std::string does not survive the return
if (index < 0 || index >= (int)this->Properties.size())
{
return false;
return "";
}
if (this->Properties[index].Defined != defined)
if (!this->Properties[index].Defined)
{
this->Properties[index].Defined = defined;
this->StorableModifiedTime.Modified();
this->Modified();
return this->NoName;
}
return true;
return this->Properties[index].Name.c_str();
}

//---------------------------------------------------------------------------
const char *vtkMRMLColorNode::GetColorName(int index)
bool vtkMRMLColorNode::GetColorNameDefined(int index)
{
// Do not use GetProperty because the content of the locally copied property's std::string does not survive the return
if (index < 0 || index >= (int)this->Properties.size())
{
return "";
vtkWarningMacro("GetColorNameDefined failed: index " << index << " out of range (0-" << (this->Properties.size() - 1));
return false;
}
if (!this->Properties[index].Defined)
const char* name = this->GetColorName(index);
if (name == nullptr)
{
return this->NoName;
return false;
}
return this->Properties[index].Name.c_str();
// Return false if name equals NoName
// for improved backward compatibility with older extensions.
if (this->GetNoName() && strcmp(this->GetNoName(), name) == 0)
{
return false;
}
// return true if name is not empty
return strlen(name) > 0;
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -574,10 +573,15 @@ bool vtkMRMLColorNode::SetTerminologyFromString(int ind, std::string terminology
}
}

this->Properties[ind] = prop;
if (this->Properties[ind] != prop)
{
this->Properties[ind] = prop;
this->StorableModifiedTime.Modified();
this->Modified();
}

// Set attribute indicating that the color table contains terminology
this->SetAttribute(this->GetContainTerminologyAttributeName(), "true");
this->SetContainsTerminology(true);

return true;
}
Expand Down Expand Up @@ -643,42 +647,62 @@ std::string vtkMRMLColorNode::GetColorNameAsFileName(int colorIndex, const char
}

//---------------------------------------------------------------------------
int vtkMRMLColorNode::SetColorName(int ind, const char *name, NameSourceType source/*=NameSetManually*/)
int vtkMRMLColorNode::SetColorName(int ind, const char *name)
{
if (ind >= static_cast<int>(this->Properties.size()) || ind < 0)
{
vtkErrorMacro("SetColorName: Index was out of bounds: "<< ind << ", current size is "
<< this->Properties.size() << ", table name = " << (this->GetName() == nullptr ? "null" : this->GetName()));
return 0;
}
std::string newName(name);
std::string colorName;
bool colorDefined = true;
if (name)
{
// For improved backward compatibility with older extensions,
// if the color name is NoName name then the we consider the color undefined.
if (this->GetNoName() && strcmp(this->GetNoName(), name) == 0)
{
colorDefined = false;
}
else
{
colorName = name;
}
}
PropertyType& prop = this->Properties[ind];
if (prop.Name != newName || prop.NameSource != source || !prop.Defined)
if (prop.Name != colorName || prop.Defined != colorDefined)
{
prop.Name = newName;
prop.NameSource = source;
prop.Defined = true;
if (colorDefined)
{
prop.Name = colorName;
prop.Defined = colorDefined;
}
else
{
prop.Clear();
}
this->StorableModifiedTime.Modified();
this->Modified();
}
return 1;
}

//---------------------------------------------------------------------------
int vtkMRMLColorNode::SetColorNameWithSpaces(int ind, const char *name, const char *subst, NameSourceType source/*=NameSetManually*/)
int vtkMRMLColorNode::SetColorNameWithSpaces(int ind, const char *name, const char *subst)
{
std::string nameString = std::string(name);
std::string substString = std::string(subst);
// does the input name have the subst character in it?
if (strstr(name, substString.c_str()) != nullptr)
{
std::replace(nameString.begin(), nameString.end(), *subst, ' ');
return this->SetColorName(ind, nameString.c_str(), source);
return this->SetColorName(ind, nameString.c_str());
}
else
{
// no substitutions necessary
return this->SetColorName(ind, name, source);
return this->SetColorName(ind, name);
}
}

Expand Down Expand Up @@ -712,25 +736,20 @@ vtkLookupTable* vtkMRMLColorNode::CreateLookupTableCopy()
}

//---------------------------------------------------------------------------
/*static*/ std::string vtkMRMLColorNode::GetNameSourceAsString(NameSourceType nameSource)
bool vtkMRMLColorNode::GetContainsTerminology()
{
switch (nameSource)
{
case NameSourceUnknown: return "unknown";
case NameSetManually: return "setManually";
case NameAutoGeneratedFromColor: return "autoGeneratedFromColor";
case NameAutoGeneratedFromTerminology: return "autoGeneratedFromTerminology";
}
return "invalid";
return (this->GetAttribute(this->GetContainsTerminologyAttributeName()) != nullptr);
}

//---------------------------------------------------------------------------
int vtkMRMLColorNode::GetColorNameSource(int index)
void vtkMRMLColorNode::SetContainsTerminology(bool containsTerminology)
{
if (index < 0 || index >= (int)this->Properties.size())
if (containsTerminology)
{
vtkWarningMacro("GetColorNameSource failed: index " << index << " out og range (0-" << (this->Properties.size() - 1));
return NameSourceUnknown;
this->SetAttribute(this->GetContainsTerminologyAttributeName(), "true");
}
return this->Properties[index].NameSource;
}
else
{
this->RemoveAttribute(this->GetContainsTerminologyAttributeName());
}
}
Loading

0 comments on commit 51112eb

Please sign in to comment.