[vtkusers] PATCH: Updates vtkLabeledDataMapper to display strings
Eric F. Sorton
eric at cctcorp.com
Wed Sep 18 14:15:59 EDT 2002
Greetings,
Attached is a small patch, that, when applied to vtk 4.0, adds two
capabilities to vtkLabeledDataMapper:
o) The ability to control the justification, horizontal and vertical,
of the text.
o) The ability to treat a character array stored as field data as
strings which provides a simple way to label cells with text.
The ability to control the justification and vertical justification of
the text is a straightforward feature. The two properties were added to
vtkLabeledDataMapper. Each time a new vtkTextMapper is created, the same
properties are set on it.
The ability to treat character array data as strings is a simple modification,
but requires a bit of an explanation. VTK does not provide a string data
type, but does provide a character data type vtkCharArray. I was able to use
vtkCharArray to emulate an array of fixed length strings. The component
indicates the length of each string in the array while the tuples indicate the
number string in the array. A snippet of C code could yield the following:
char labels[5][6] = { "a", "bc", "def", "ghij", "klmno" };
vtkCharArray *char_array = vtkCharArray::New();
char_array->SetNumberOfComponents(6);
char_array->SetNumberOfTuples(5);
char_array->SetArray(labels, 5*6, 1);
To use the above character array as labels, attach it to your data set as
field data, and then setup the vtkLabeledDataMapper as follows:
vtkLabeledDataMapper *label_mapper = vtkLabeledDataMapper::New();
label_mapper->SetLabelFormat("%s");
label_mapper->SetLabelModeToLabelFieldData();
label_mapper->TreatCharDataAsStringOn();
label_mapper->SetFieldDataArray(0);
Assuming that the character array is the 0th field data array.
Also attached is a working example that uses the data of the US states to
label each state with its appropriate two letter code. To run the example,
rebuild vtk with the above patch applied. Take appropriate steps to allow the
application to find the "usa.vtk" data file include in the vtk data (either
copy the file to the local directory or update the source to reference the
appropriate location). Build the test application and execute it.
I hope someone finds this code useful ...
Eric
--
+--=--=- Eric F. Sorton <eric at cctcorp.com> =--=--=--=--=--=--=--=--=--=-+
| Command and Control Technologies Corporation (http://www.cctcorp.com) |
| Phone: (321) 264-1193 Fax: (321) 383-5096 |
+--=--=--=--=--=--=--=--=--=--=--=-+-=--=--=--=--=--=--=--=--=--=--=--=-+
-------------- next part --------------
diff -ur vtk-4.0/Rendering/vtkLabeledDataMapper.cxx vtk-4.0-patched/Rendering/vtkLabeledDataMapper.cxx
--- vtk-4.0/Rendering/vtkLabeledDataMapper.cxx 2002-09-18 12:48:54.000000000 -0400
+++ vtk-4.0-patched/Rendering/vtkLabeledDataMapper.cxx 2002-09-18 12:57:56.000000000 -0400
@@ -77,6 +77,9 @@
strcpy(this->LabelFormat,"%g");
this->LabeledComponent = (-1);
this->FieldDataArray = 0;
+ this->TreatCharDataAsString = 0;
+ this->Justification = VTK_TEXT_LEFT;
+ this->VerticalJustification = VTK_TEXT_BOTTOM;
this->NumberOfLabels = 0;
this->NumberOfLabelsAllocated = 50;
@@ -253,7 +256,7 @@
this->TextMappers[i] = vtkTextMapper::New();
}
}//if we have to allocate new text mappers
-
+
for (i=0; i < this->NumberOfLabels; i++)
{
if ( pointIdLabels )
@@ -261,7 +264,11 @@
val = (float)i;
sprintf(string, this->LabelFormat, val);
}
- else
+ else if (data->GetDataType() == VTK_CHAR && this->TreatCharDataAsString)
+ {
+ sprintf(string, this->LabelFormat, (char *)data->GetVoidPointer(0) + data->GetNumberOfComponents()*i);
+ }
+ else
{
data->GetTuple(i, tuple);
if ( numComp == 1)
@@ -288,6 +295,8 @@
this->TextMappers[i]->SetItalic(this->Italic);
this->TextMappers[i]->SetShadow(this->Shadow);
this->TextMappers[i]->SetFontFamily(this->FontFamily);
+ this->TextMappers[i]->SetJustification(this->Justification);
+ this->TextMappers[i]->SetVerticalJustification(this->VerticalJustification);
}
if ( data )
@@ -369,6 +378,8 @@
os << indent << "Italic: " << (this->Italic ? "On\n" : "Off\n");
os << indent << "Shadow: " << (this->Shadow ? "On\n" : "Off\n");
os << indent << "Label Format: " << this->LabelFormat << "\n";
+ os << indent << "Justification: " << this->Justification << "\n";
+ os << indent << "Vertical Justification: " << this->VerticalJustification << "\n";
os << indent << "Labeled Component: ";
if ( this->LabeledComponent < 0 )
diff -ur vtk-4.0/Rendering/vtkLabeledDataMapper.h vtk-4.0-patched/Rendering/vtkLabeledDataMapper.h
--- vtk-4.0/Rendering/vtkLabeledDataMapper.h 2002-09-18 12:49:01.000000000 -0400
+++ vtk-4.0-patched/Rendering/vtkLabeledDataMapper.h 2002-09-18 12:50:44.000000000 -0400
@@ -153,6 +153,12 @@
vtkBooleanMacro(Shadow, int);
// Description:
+ // Enable/Disable treating character data as strings.
+ vtkSetMacro(TreatCharDataAsString,int);
+ vtkGetMacro(TreatCharDataAsString,int);
+ vtkBooleanMacro(TreatCharDataAsString,int);
+
+ // Description:
// Set/Get the font family for the labels. Three font types
// are available: Arial (VTK_ARIAL), Courier (VTK_COURIER), and
// Times (VTK_TIMES).
@@ -187,6 +193,27 @@
vtkSetClampMacro(FieldDataArray,int,0,VTK_LARGE_INTEGER);
vtkGetMacro(FieldDataArray,int);
+ // Description:
+ // Set/Get the horizontal justification to left (default), centered,
+ // or right.
+ vtkSetClampMacro(Justification,int,VTK_TEXT_LEFT,VTK_TEXT_RIGHT);
+ vtkGetMacro(Justification,int);
+ void SetJustificationToLeft() {this->SetJustification(VTK_TEXT_LEFT);};
+ void SetJustificationToCentered() {this->SetJustification(VTK_TEXT_CENTERED);};
+ void SetJustificationToRight() {this->SetJustification(VTK_TEXT_RIGHT);};
+
+ // Description:
+ // Set/Get the vertical justification to bottom (default), middle,
+ // or top.
+ vtkSetClampMacro(VerticalJustification,int,VTK_TEXT_BOTTOM,VTK_TEXT_TOP);
+ vtkGetMacro(VerticalJustification,int);
+ void SetVerticalJustificationToBottom()
+ {this->SetVerticalJustification(VTK_TEXT_BOTTOM);};
+ void SetVerticalJustificationToCentered()
+ {this->SetVerticalJustification(VTK_TEXT_CENTERED);};
+ void SetVerticalJustificationToTop()
+ {this->SetVerticalJustification(VTK_TEXT_TOP);};
+
protected:
vtkLabeledDataMapper();
~vtkLabeledDataMapper();
@@ -199,6 +226,10 @@
int Italic;
int Shadow;
int FontFamily;
+ int Justification;
+ int VerticalJustification;
+
+ int TreatCharDataAsString;
char *LabelFormat;
int LabeledComponent;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: LabeledStates.cxx
Type: text/x-c++src
Size: 2636 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20020918/37e462f9/attachment.cxx>
More information about the vtkusers
mailing list