Commit 5582d505 authored by David Thompson's avatar David Thompson Committed by Code Review
Browse files

Merge topic '14189_set_annotations_deep_copy' into master

06ef3b9e Change `vtkScalarsToColors::SetAnnotations` behavior.
parents be5771a4 06ef3b9e
......@@ -10,6 +10,7 @@ vtk_add_test_cxx(NO_DATA NO_VALID NO_OUTPUT
TestArrayAPIDense.cxx
TestArrayAPISparse.cxx
TestArrayBool.cxx
TestScalarsToColors.cxx
# TestArrayCasting.cxx # Uses Boost in its own separate test.
TestArrayExtents.cxx
TestArrayInterpolationDense.cxx
......
#include "vtkNew.h"
#include "vtkScalarsToColors.h"
#include "vtkStringArray.h"
#include "vtkVariant.h"
#include "vtkVariantArray.h"
int TestScalarsToColors(int, char*[])
{
vtkNew<vtkScalarsToColors> lut;
vtkNew<vtkStringArray> ann;
vtkNew<vtkVariantArray> val;
lut->SetAnnotations(NULL, NULL);
vtkStringArray* ann2 = lut->GetAnnotations();
vtkAbstractArray* val2 = lut->GetAnnotatedValues();
if (ann2 || val2)
{
cerr << "Annotations set to NULL but didn't return NULL\n";
return EXIT_FAILURE;
}
ann->InsertNextValue("Foo");
val->InsertNextValue(vtkVariant(10.3));
lut->SetAnnotations(val.GetPointer(), ann.GetPointer());
ann2 = lut->GetAnnotations();
val2 = lut->GetAnnotatedValues();
if (!ann2 || !val2)
{
cerr << "Annotations set to non-NULL but returned NULL\n";
return EXIT_FAILURE;
}
int idx = lut->GetAnnotatedValueIndex(10.3);
if (idx != 0)
{
cerr << "Could not find annotated value 10.3.\n";
return EXIT_FAILURE;
}
idx = lut->GetAnnotatedValueIndex("Narf");
if (idx >= 0)
{
cerr << "Found unexpected annotated value \"Narf\".\n";
return EXIT_FAILURE;
}
ann->InsertNextValue("Not hardly!");
val->InsertNextValue("Narf");
ann->InsertNextValue("Fezzik");
val->InsertNextValue(vtkVariant(20));
lut->SetAnnotations(val.GetPointer(), ann.GetPointer());
idx = lut->GetAnnotatedValueIndex("Narf");
if (idx != 1)
{
cerr << "Couldn't find newly-annotated value (\"Narf\").\n";
return EXIT_FAILURE;
}
lut->SetAnnotations(NULL, NULL);
ann2 = lut->GetAnnotations();
val2 = lut->GetAnnotatedValues();
if (ann2 || val2)
{
cerr << "Annotations again set to NULL but didn't return NULL\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
......@@ -1596,8 +1596,7 @@ void vtkScalarsToColors::SetAnnotations(
{
if (
(values && !annotations) ||
(!values && annotations) ||
(values == this->AnnotatedValues && annotations == this->Annotations))
(!values && annotations))
return;
if (values && annotations &&
......@@ -1610,36 +1609,49 @@ void vtkScalarsToColors::SetAnnotations(
return;
}
bool sameVals = (values == this->AnnotatedValues);
bool sameText = (annotations == this->Annotations);
if (this->AnnotatedValues && !sameVals)
if (this->AnnotatedValues && !values)
{
this->AnnotatedValues->Delete();
this->AnnotatedValues = 0;
}
if (this->Annotations && !sameText)
{
this->Annotations->Delete();
this->Annotations = 0;
else if (values)
{ // Ensure arrays are of the same type before copying.
if (this->AnnotatedValues)
{
if (this->AnnotatedValues->GetDataType() != values->GetDataType())
{
this->AnnotatedValues->Delete();
this->AnnotatedValues = 0;
}
}
if (!this->AnnotatedValues)
{
this->AnnotatedValues =
vtkAbstractArray::CreateArray(
values->GetDataType());
}
}
if (!values)
bool sameVals = (values == this->AnnotatedValues);
if (!sameVals && values)
{
return;
this->AnnotatedValues->DeepCopy(values);
}
if (!sameVals)
if (this->Annotations && !annotations)
{
this->AnnotatedValues = values;
this->AnnotatedValues->Register(this);
this->Annotations->Delete();
this->Annotations = 0;
}
if (!sameText)
else if (!this->Annotations && annotations)
{
this->Annotations = annotations;
this->Annotations->Register(this);
this->Annotations = vtkStringArray::New();
}
if (this->AnnotatedValues)
bool sameText = (annotations == this->Annotations);
if (!sameText)
{
this->UpdateAnnotatedValueMap();
this->Annotations->DeepCopy(annotations);
}
this->UpdateAnnotatedValueMap();
this->Modified();
}
......@@ -1818,7 +1830,8 @@ void vtkScalarsToColors::UpdateAnnotatedValueMap()
{
this->AnnotatedValueMap->clear();
vtkIdType na = this->AnnotatedValues->GetMaxId() + 1;
vtkIdType na =
this->AnnotatedValues ? this->AnnotatedValues->GetMaxId() + 1 : 0;
for (vtkIdType i = 0; i < na; ++ i)
{
(*this->AnnotatedValueMap)[this->AnnotatedValues->GetVariantValue(i)] = i;
......
......@@ -233,6 +233,12 @@ public:
// as a set of annotations to add to a scalar array (when IndexedLookup is false).
// The two arrays must both either be NULL or of the same length or
// the call will be ignored.
//
// Note that these arrays are deep copied rather than being used directly
// in order to support the use case where edits are made. If the
// \a values and \a annotations arrays were held by this class then each
// call to map scalar values to colors would require us to check the MTime
// of the arrays.
virtual void SetAnnotations( vtkAbstractArray* values, vtkStringArray* annotations );
vtkGetObjectMacro(AnnotatedValues,vtkAbstractArray);
vtkGetObjectMacro(Annotations,vtkStringArray);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment