Commit 52ed14c9 authored by Jeff Baumes's avatar Jeff Baumes
Browse files

ENH: Adding vtkApplyIcons and support for attribute types.

vtkApplyIcons, much like vtkApplyColors, defines the
icon indices for elements of a dataset through a lookup table,
default icon, annotations, and/or the current selection.

There are a number of places that specify attribute types
(point, cell, vertex, edge, row, field) and create an enumeration
for this (vtkDataObjectToTable, vtkSelectionNode, ...). A new
method GetAttributes(int) in vtkDataObject uses an enum for the
possible attribute types and subclasses retrieve the appropriate
attributes. GetAttributesAsFieldData(int) performs the same function
but returns a vtkFieldData instance instead of vtkDataSetAttributes.
GetAttributeTypeForArray is another convenience method that returns
the attribute type that an array is contained in.
parent 08764697
......@@ -31,7 +31,7 @@
#include "vtkSelection.h"
#include "vtkSmartPointer.h"
vtkCxxRevisionMacro(vtkAnnotation, "1.6");
vtkCxxRevisionMacro(vtkAnnotation, "1.7");
vtkStandardNewMacro(vtkAnnotation);
vtkCxxSetObjectMacro(vtkAnnotation, Selection, vtkSelection);
......@@ -39,6 +39,7 @@ vtkCxxSetObjectMacro(vtkAnnotation, Selection, vtkSelection);
vtkInformationKeyMacro(vtkAnnotation, LABEL, String);
vtkInformationKeyRestrictedMacro(vtkAnnotation, COLOR, DoubleVector, 3);
vtkInformationKeyMacro(vtkAnnotation, OPACITY, Double);
vtkInformationKeyMacro(vtkAnnotation, ICON_INDEX, Integer);
vtkInformationKeyMacro(vtkAnnotation, ENABLE, Integer);
vtkInformationKeyMacro(vtkAnnotation, HIDE, Integer);
vtkInformationKeyMacro(vtkAnnotation, DATA, DataObject);
......
......@@ -72,6 +72,10 @@ public:
// This is stored as a value between 0 and 1.
static vtkInformationDoubleKey* OPACITY();
// Description:
// An icon index for this annotation.
static vtkInformationIntegerKey* ICON_INDEX();
// Description:
// Whether or not this annotation is enabled.
// A value of 1 means enabled, 0 disabled.
......
......@@ -15,6 +15,7 @@ PURPOSE. See the above copyright notice for more information.
#include "vtkDataObject.h"
#include "vtkAlgorithmOutput.h"
#include "vtkDataSetAttributes.h"
#include "vtkExtentTranslator.h"
#include "vtkFieldData.h"
#include "vtkGarbageCollector.h"
......@@ -36,7 +37,7 @@ PURPOSE. See the above copyright notice for more information.
#include "vtkInformationVector.h"
#include "vtkDataSetAttributes.h"
vtkCxxRevisionMacro(vtkDataObject, "1.45");
vtkCxxRevisionMacro(vtkDataObject, "1.46");
vtkStandardNewMacro(vtkDataObject);
vtkCxxSetObjectMacro(vtkDataObject,Information,vtkInformation);
......@@ -1472,3 +1473,53 @@ const char* vtkDataObject::GetAssociationTypeAsString(int associationType)
}
return vtkDataObject::AssociationNames[associationType];
}
//----------------------------------------------------------------------------
vtkDataSetAttributes* vtkDataObject::GetAttributes(int type)
{
return vtkDataSetAttributes::SafeDownCast(this->GetAttributesAsFieldData(type));
}
//----------------------------------------------------------------------------
vtkFieldData* vtkDataObject::GetAttributesAsFieldData(int type)
{
switch (type)
{
case FIELD:
return this->FieldData;
break;
}
return 0;
}
//----------------------------------------------------------------------------
int vtkDataObject::GetAttributeTypeForArray(vtkAbstractArray* arr)
{
for (int i = 0; i < NUMBER_OF_ATTRIBUTE_TYPES; ++i)
{
vtkFieldData* data = this->GetAttributesAsFieldData(i);
if (data)
{
for (int j = 0; j < data->GetNumberOfArrays(); ++j)
{
if (data->GetAbstractArray(j) == arr)
{
return i;
}
}
}
}
return -1;
}
//----------------------------------------------------------------------------
vtkIdType vtkDataObject::GetNumberOfElements(int type)
{
switch (type)
{
case FIELD:
return this->FieldData->GetNumberOfTuples();
break;
}
return 0;
}
......@@ -34,7 +34,9 @@
#include "vtkObject.h"
class vtkAbstractArray;
class vtkAlgorithmOutput;
class vtkDataSetAttributes;
class vtkExecutive;
class vtkFieldData;
class vtkInformation;
......@@ -431,6 +433,54 @@ public:
};
//ETX
//BTX
// Description:
// Possible attribute types.
enum AttributeTypes
{
POINT,
CELL,
FIELD,
VERTEX,
EDGE,
ROW,
NUMBER_OF_ATTRIBUTE_TYPES
};
//ETX
// Description:
// Returns the attributes of the data object of the specified
// attribute type. The type may be:
// <ul>
// <li>POINT - Defined in vtkDataSet subclasses.
// <li>CELL - Defined in vtkDataSet subclasses.
// <li>VERTEX - Defined in vtkGraph subclasses.
// <li>EDGE - Defined in vtkGraph subclasses.
// <li>ROW - Defined in vtkTable.
// </ul>
// The other attribute type, FIELD, will return NULL since
// field data is stored as a vtkFieldData instance, not a
// vtkDataSetAttributes instance. To retrieve field data, use
// GetAttributesAsFieldData.
virtual vtkDataSetAttributes* GetAttributes(int type);
// Description:
// Returns the attributes of the data object as a vtkFieldData.
// This returns non-null values in all the same cases as GetAttributes,
// in addition to the case of FIELD, which will return the field data
// for any vtkDataObject subclass.
virtual vtkFieldData* GetAttributesAsFieldData(int type);
// Description:
// Retrieves the attribute type that an array came from.
// This is useful for obtaining which attribute type a input array
// to an algorithm came from (retrieved from GetInputAbstractArrayToProcesss).
virtual int GetAttributeTypeForArray(vtkAbstractArray* arr);
// Description:
// Get the number of elements for a specific attribute type (POINT, CELL, etc.).
virtual vtkIdType GetNumberOfElements(int type);
//BTX
// Description:
// Possible values for the FIELD_OPERATION information entry.
......
......@@ -28,7 +28,7 @@
#include <math.h>
vtkCxxRevisionMacro(vtkDataSet, "1.14");
vtkCxxRevisionMacro(vtkDataSet, "1.15");
//----------------------------------------------------------------------------
// Constructor with default bounds (0,1, 0,1, 0,1).
......@@ -652,6 +652,36 @@ vtkDataSet* vtkDataSet::GetData(vtkInformationVector* v, int i)
return vtkDataSet::GetData(v->GetInformationObject(i));
}
//----------------------------------------------------------------------------
vtkFieldData* vtkDataSet::GetAttributesAsFieldData(int type)
{
switch(type)
{
case POINT:
return this->GetPointData();
break;
case CELL:
return this->GetCellData();
break;
}
return this->Superclass::GetAttributesAsFieldData(type);
}
//----------------------------------------------------------------------------
vtkIdType vtkDataSet::GetNumberOfElements(int type)
{
switch (type)
{
case POINT:
return this->GetNumberOfPoints();
break;
case CELL:
return this->GetNumberOfCells();
break;
}
return this->Superclass::GetNumberOfElements(type);;
}
//----------------------------------------------------------------------------
void vtkDataSet::PrintSelf(ostream& os, vtkIndent indent)
{
......
......@@ -330,6 +330,17 @@ public:
static vtkDataSet* GetData(vtkInformationVector* v, int i=0);
//ETX
// Description:
// Returns the attributes of the data object as a vtkFieldData.
// This returns non-null values in all the same cases as GetAttributes,
// in addition to the case of FIELD, which will return the field data
// for any vtkDataObject subclass.
virtual vtkFieldData* GetAttributesAsFieldData(int type);
// Description:
// Get the number of elements for a specific attribute type (POINT, CELL, etc.).
virtual vtkIdType GetNumberOfElements(int type);
protected:
// Constructor with default bounds (0,1, 0,1, 0,1).
vtkDataSet();
......
......@@ -196,6 +196,23 @@ public:
// Get the collection of attributes associated with this dataset.
vtkGetObjectMacro(Attributes, vtkGenericAttributeCollection);
// Description:
// Returns the attributes of the data object of the specified
// attribute type. The type may be:
// <ul>
// <li>POINT - Defined in vtkDataSet subclasses.
// <li>CELL - Defined in vtkDataSet subclasses.
// <li>VERTEX - Defined in vtkGraph subclasses.
// <li>EDGE - Defined in vtkGraph subclasses.
// <li>ROW - Defined in vtkTable.
// </ul>
// The other attribute type, FIELD, will return NULL since
// field data is stored as a vtkFieldData instance, not a
// vtkDataSetAttributes instance. To retrieve field data, use
// GetAttributesAsFieldData.
virtual vtkDataSetAttributes* GetAttributes(int type)
{ return this->Superclass::GetAttributes(type); }
// Description:
// Set/Get a cell tessellator if cells must be tessellated during
// processing.
......
......@@ -64,7 +64,7 @@ private:
void operator=(const vtkGraphEdgePoints&); // Not implemented.
};
vtkStandardNewMacro(vtkGraphEdgePoints);
vtkCxxRevisionMacro(vtkGraphEdgePoints, "1.37");
vtkCxxRevisionMacro(vtkGraphEdgePoints, "1.38");
//----------------------------------------------------------------------------
// class vtkGraph
......@@ -73,7 +73,7 @@ vtkCxxSetObjectMacro(vtkGraph, Points, vtkPoints);
vtkCxxSetObjectMacro(vtkGraph, Internals, vtkGraphInternals);
vtkCxxSetObjectMacro(vtkGraph, EdgePoints, vtkGraphEdgePoints);
vtkCxxSetObjectMacro(vtkGraph, EdgeList, vtkIdTypeArray);
vtkCxxRevisionMacro(vtkGraph, "1.37");
vtkCxxRevisionMacro(vtkGraph, "1.38");
//----------------------------------------------------------------------------
vtkGraph::vtkGraph()
{
......@@ -1519,6 +1519,36 @@ void vtkGraph::ForceOwnership()
}
}
//----------------------------------------------------------------------------
vtkFieldData* vtkGraph::GetAttributesAsFieldData(int type)
{
switch(type)
{
case VERTEX:
return this->GetVertexData();
break;
case EDGE:
return this->GetEdgeData();
break;
}
return this->Superclass::GetAttributesAsFieldData(type);
}
//----------------------------------------------------------------------------
vtkIdType vtkGraph::GetNumberOfElements(int type)
{
switch (type)
{
case VERTEX:
return this->GetNumberOfVertices();
break;
case EDGE:
return this->GetNumberOfEdges();
break;
}
return this->Superclass::GetNumberOfElements(type);;
}
//----------------------------------------------------------------------------
void vtkGraph::PrintSelf(ostream& os, vtkIndent indent)
{
......
......@@ -547,6 +547,17 @@ public:
// subgraph formed by the vertices in the vertex list.
void GetInducedEdges(vtkIdTypeArray* verts, vtkIdTypeArray* edges);
// Description:
// Returns the attributes of the data object as a vtkFieldData.
// This returns non-null values in all the same cases as GetAttributes,
// in addition to the case of FIELD, which will return the field data
// for any vtkDataObject subclass.
virtual vtkFieldData* GetAttributesAsFieldData(int type);
// Description:
// Get the number of elements for a specific attribute type (VERTEX, EDGE, etc.).
virtual vtkIdType GetNumberOfElements(int type);
protected:
//BTX
vtkGraph();
......
......@@ -35,7 +35,7 @@
// Standard functions
//
vtkCxxRevisionMacro(vtkTable, "1.26");
vtkCxxRevisionMacro(vtkTable, "1.27");
vtkStandardNewMacro(vtkTable);
vtkCxxSetObjectMacro(vtkTable, RowData, vtkDataSetAttributes);
......@@ -627,3 +627,27 @@ void vtkTable::DeepCopy(vtkDataObject* src)
Superclass::DeepCopy(src);
}
//----------------------------------------------------------------------------
vtkFieldData* vtkTable::GetAttributesAsFieldData(int type)
{
switch(type)
{
case ROW:
return this->GetRowData();
break;
}
return this->Superclass::GetAttributesAsFieldData(type);
}
//----------------------------------------------------------------------------
vtkIdType vtkTable::GetNumberOfElements(int type)
{
switch (type)
{
case ROW:
return this->GetNumberOfRows();
break;
}
return this->Superclass::GetNumberOfElements(type);;
}
......@@ -187,6 +187,17 @@ public:
virtual void ShallowCopy(vtkDataObject* src);
virtual void DeepCopy(vtkDataObject* src);
// Description:
// Returns the attributes of the data object as a vtkFieldData.
// This returns non-null values in all the same cases as GetAttributes,
// in addition to the case of FIELD, which will return the field data
// for any vtkDataObject subclass.
virtual vtkFieldData* GetAttributesAsFieldData(int type);
// Description:
// Get the number of elements for a specific attribute type (ROW, etc.).
virtual vtkIdType GetNumberOfElements(int type);
protected:
vtkTable();
~vtkTable();
......
......@@ -20,6 +20,7 @@ SET( Kit_SRCS
vtkAddMembershipArray.cxx
vtkAppendPoints.cxx
vtkApplyColors.cxx
vtkApplyIcons.cxx
vtkArcParallelEdgeStrategy.cxx
vtkAreaLayout.cxx
vtkAreaLayoutStrategy.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkApplyIcons.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
#include "vtkApplyIcons.h"
#include "vtkAnnotation.h"
#include "vtkAnnotationLayers.h"
#include "vtkCellData.h"
#include "vtkConvertSelection.h"
#include "vtkDataSet.h"
#include "vtkGraph.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSelectionNode.h"
#include "vtkSmartPointer.h"
#include "vtkTable.h"
#include <vtksys/stl/map>
vtkCxxRevisionMacro(vtkApplyIcons, "1.1");
vtkStandardNewMacro(vtkApplyIcons);
class vtkApplyIcons::Internals {
public:
vtksys_stl::map<vtkVariant, int> LookupTable;
};
vtkApplyIcons::vtkApplyIcons()
{
this->Implementation = new Internals();
this->DefaultIcon = 0;
this->SelectedIcon = 0;
this->SetNumberOfInputPorts(2);
this->SetInputArrayToProcess(0, 0, 0,
vtkDataObject::FIELD_ASSOCIATION_VERTICES,
vtkDataSetAttributes::SCALARS);
this->UseLookupTable = false;
this->IconOutputArrayName = 0;
this->SetIconOutputArrayName("vtkApplyIcons icon");
this->SelectionMode = IGNORE_SELECTION;
this->AttributeType = vtkDataObject::VERTEX;
}
vtkApplyIcons::~vtkApplyIcons()
{
delete Implementation;
this->SetIconOutputArrayName(0);
}
void vtkApplyIcons::SetIconType(vtkVariant v, int icon)
{
this->Implementation->LookupTable[v] = icon;
}
void vtkApplyIcons::ClearAllIconTypes()
{
this->Implementation->LookupTable.clear();
}
int vtkApplyIcons::FillInputPortInformation(int port, vtkInformation* info)
{
if (port == 0)
{
info->Remove(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE());
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkAnnotationLayers");
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
}
return 1;
}
int vtkApplyIcons::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// Get the info objects.
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* layersInfo = inputVector[1]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
if (!this->IconOutputArrayName)
{
vtkErrorMacro("Output array name must be valid");
return 0;
}
// Get the input and output.
vtkDataObject* input = inInfo->Get(vtkDataObject::DATA_OBJECT());
vtkAnnotationLayers* layers = 0;
if (layersInfo)
{
layers = vtkAnnotationLayers::SafeDownCast(
layersInfo->Get(vtkDataObject::DATA_OBJECT()));
}
vtkDataObject* output = outInfo->Get(vtkDataObject::DATA_OBJECT());
output->ShallowCopy(input);
// Initialize icon array.
vtkAbstractArray* arr = this->GetInputAbstractArrayToProcess(0, inputVector);
vtkSmartPointer<vtkIntArray> iconArr =
vtkSmartPointer<vtkIntArray>::New();
iconArr->SetName(this->IconOutputArrayName);
// If we have an input array, use its attribute type, otherwise use the
// AttributeType ivar.
int attribType = this->AttributeType;
if (arr)
{
attribType = output->GetAttributeTypeForArray(arr);
}
// Error if the attribute type is not defined on the data.
if (!output->GetAttributes(attribType))
{
vtkErrorMacro("The input array is not found, and the AttributeType parameter is not valid for this data object.");
return 1;
}
// Size the array and add it to the correct attributes.
vtkIdType numTuples = input->GetNumberOfElements(attribType);
iconArr->SetNumberOfTuples(numTuples);
output->GetAttributes(attribType)->AddArray(iconArr);
// Process the icon array.
if (this->UseLookupTable && arr)
{
// Map the data values through the lookup table.
vtksys_stl::map<vtkVariant, int>::iterator itEnd = this->Implementation->LookupTable.end();
for (vtkIdType i = 0; i < iconArr->GetNumberOfTuples(); ++i)
{
vtkVariant val = arr->GetVariantValue(i);
int mappedIcon = this->DefaultIcon;
if (this->Implementation->LookupTable.find(val) != itEnd)
{
mappedIcon = this->Implementation->LookupTable[val];
}
iconArr->SetValue(i, mappedIcon);
}
}
else if (arr)
{
// If no lookup table, pass the input array values.
for (vtkIdType i = 0; i < iconArr->GetNumberOfTuples(); ++i)
{
iconArr->SetValue(i, arr->GetVariantValue(i).ToInt());
}
}
else
{
// If no lookup table or array, use default icon.
for (vtkIdType i = 0; i < iconArr->GetNumberOfTuples(); ++i)
{
iconArr->SetValue(i, this->DefaultIcon);
}
}
// Convert to a selection attribute type.
int attribTypeSel = -1;
switch (attribType)
{
case vtkDataObject::POINT:
attribTypeSel = vtkSelectionNode::POINT;
break;
case vtkDataObject::CELL:
attribTypeSel = vtkSelectionNode::CELL;
break;
case vtkDataObject::VERTEX:
attribTypeSel = vtkSelectionNode::VERTEX;
break;
case vtkDataObject::EDGE:
attribTypeSel = vtkSelectionNode::EDGE;
break;
case vtkDataObject::ROW:
attribTypeSel = vtkSelectionNode::ROW;
break;
case vtkDataObject::FIELD:
attribTypeSel = vtkSelectionNode::FIELD;
break;
}
if (layers)
{
// Set annotated icons.
vtkSmartPointer<vtkIdTypeArray> list1 =
vtkSmartPointer<vtkIdTypeArray>::New();
unsigned int numAnnotations = layers->GetNumberOfAnnotations();
for (unsigned int a = 0; a < numAnnotations; ++a)
{
vtkAnnotation* ann = layers->GetAnnotation(a);
if (ann->GetInformation()->Has(vtkAnnotation::ENABLE()) &&
ann->GetInformation()->Get(vtkAnnotation::ENABLE())==0)
{
continue;
}
list1->Initialize();
vtkSelection* sel = ann->GetSelection();
int curIcon = -1;
if (ann->GetInformation()->Has(vtkAnnotation::ICON_INDEX()))
{
curIcon = ann->GetInformation()->Get(vtkAnnotation::ICON_INDEX());
}
else