1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkLabeledDataMapper.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 // .NAME vtkLabeledDataMapper - draw text labels at dataset points
16 // .SECTION Description
17 // vtkLabeledDataMapper is a mapper that renders text at dataset
18 // points. Various items can be labeled including point ids, scalars,
19 // vectors, normals, texture coordinates, tensors, and field data components.
20 //
21 // The format with which the label is drawn is specified using a
22 // printf style format string. The font attributes of the text can
23 // be set through the vtkTextProperty associated to this mapper.
24 //
25 // By default, all the components of multi-component data such as
26 // vectors, normals, texture coordinates, tensors, and multi-component
27 // scalars are labeled. However, you can specify a single component if
28 // you prefer. (Note: the label format specifies the format to use for
29 // a single component. The label is creating by looping over all components
30 // and using the label format to render each component.)
31 
32 // .SECTION Caveats
33 // Use this filter in combination with vtkSelectVisiblePoints if you want
34 // to label only points that are visible. If you want to label cells rather
35 // than points, use the filter vtkCellCenters to generate points at the
36 // center of the cells. Also, you can use the class vtkIdFilter to
37 // generate ids as scalars or field data, which can then be labeled.
38 
39 // .SECTION See Also
40 // vtkMapper2D vtkActor2D vtkTextMapper vtkTextProperty vtkSelectVisiblePoints
41 // vtkIdFilter vtkCellCenters
42 
43 #ifndef vtkLabeledDataMapper_h
44 #define vtkLabeledDataMapper_h
45 
46 #include "vtkRenderingLabelModule.h" // For export macro
47 #include "vtkMapper2D.h"
48 
49 #include <cassert> // For assert macro
50 
51 class vtkDataObject;
52 class vtkDataSet;
53 class vtkTextMapper;
54 class vtkTextProperty;
55 class vtkTransform;
56 
57 #define VTK_LABEL_IDS        0
58 #define VTK_LABEL_SCALARS    1
59 #define VTK_LABEL_VECTORS    2
60 #define VTK_LABEL_NORMALS    3
61 #define VTK_LABEL_TCOORDS    4
62 #define VTK_LABEL_TENSORS    5
63 #define VTK_LABEL_FIELD_DATA 6
64 
65 class VTKRENDERINGLABEL_EXPORT vtkLabeledDataMapper : public vtkMapper2D
66 {
67 public:
68   // Description:
69   // Instantiate object with %%-#6.3g label format. By default, point ids
70   // are labeled.
71   static vtkLabeledDataMapper *New();
72 
73   vtkTypeMacro(vtkLabeledDataMapper,vtkMapper2D);
74   void PrintSelf(ostream& os, vtkIndent indent);
75 
76   // Description:
77   // Set/Get the format with which to print the labels.  This should
78   // be a printf-style format string.
79   //
80   // By default, the mapper will try to print each component of the
81   // tuple using a sane format: %d for integers, %f for floats, %g for
82   // doubles, %ld for longs, et cetera.  If you need a different
83   // format, set it here.  You can do things like limit the number of
84   // significant digits, add prefixes/suffixes, basically anything
85   // that printf can do.  If you only want to print one component of a
86   // vector, see the ivar LabeledComponent.
87   vtkSetStringMacro(LabelFormat);
88   vtkGetStringMacro(LabelFormat);
89 
90   // Description:
91   // Set/Get the component number to label if the data to print has
92   // more than one component. For example, all the components of
93   // scalars, vectors, normals, etc. are labeled by default
94   // (LabeledComponent=(-1)). However, if this ivar is nonnegative,
95   // then only the one component specified is labeled.
96   vtkSetMacro(LabeledComponent,int);
97   vtkGetMacro(LabeledComponent,int);
98 
99   // Description:
100   // Set/Get the field data array to label. This instance variable is
101   // only applicable if field data is labeled.  This will clear
102   // FieldDataName when set.
103   void SetFieldDataArray(int arrayIndex);
104   vtkGetMacro(FieldDataArray,int);
105 
106   // Description:
107   // Set/Get the name of the field data array to label.  This instance
108   // variable is only applicable if field data is labeled.  This will
109   // override FieldDataArray when set.
110   void SetFieldDataName(const char *arrayName);
111   vtkGetStringMacro(FieldDataName);
112 
113   // Description:
114   // Set the input dataset to the mapper. This mapper handles any type of data.
115   virtual void SetInputData(vtkDataObject*);
116 
117   // Description:
118   // Use GetInputDataObject() to get the input data object for composite
119   // datasets.
120   vtkDataSet *GetInput();
121 
122   // Description:
123   // Specify which data to plot: IDs, scalars, vectors, normals, texture coords,
124   // tensors, or field data. If the data has more than one component, use
125   // the method SetLabeledComponent to control which components to plot.
126   // The default is VTK_LABEL_IDS.
127   vtkSetMacro(LabelMode, int);
128   vtkGetMacro(LabelMode, int);
SetLabelModeToLabelIds()129   void SetLabelModeToLabelIds() {this->SetLabelMode(VTK_LABEL_IDS);};
SetLabelModeToLabelScalars()130   void SetLabelModeToLabelScalars() {this->SetLabelMode(VTK_LABEL_SCALARS);};
SetLabelModeToLabelVectors()131   void SetLabelModeToLabelVectors() {this->SetLabelMode(VTK_LABEL_VECTORS);};
SetLabelModeToLabelNormals()132   void SetLabelModeToLabelNormals() {this->SetLabelMode(VTK_LABEL_NORMALS);};
SetLabelModeToLabelTCoords()133   void SetLabelModeToLabelTCoords() {this->SetLabelMode(VTK_LABEL_TCOORDS);};
SetLabelModeToLabelTensors()134   void SetLabelModeToLabelTensors() {this->SetLabelMode(VTK_LABEL_TENSORS);};
SetLabelModeToLabelFieldData()135   void SetLabelModeToLabelFieldData()
136             {this->SetLabelMode(VTK_LABEL_FIELD_DATA);};
137 
138   // Description:
139   // Set/Get the text property.
140   // If an integer argument is provided, you may provide different text
141   // properties for different label types. The type is determined by an
142   // optional type input array.
SetLabelTextProperty(vtkTextProperty * p)143   virtual void SetLabelTextProperty(vtkTextProperty *p)
144     { this->SetLabelTextProperty(p, 0); }
GetLabelTextProperty()145   virtual vtkTextProperty* GetLabelTextProperty()
146     { return this->GetLabelTextProperty(0); }
147   virtual void SetLabelTextProperty(vtkTextProperty *p, int type);
148   virtual vtkTextProperty* GetLabelTextProperty(int type);
149 
150   // Description:
151   // Draw the text to the screen at each input point.
152   void RenderOpaqueGeometry(vtkViewport* viewport, vtkActor2D* actor);
153   void RenderOverlay(vtkViewport* viewport, vtkActor2D* actor);
154 
155   // Description:
156   // Release any graphics resources that are being consumed by this actor.
157   virtual void ReleaseGraphicsResources(vtkWindow *);
158 
159   // Description:
160   // The transform to apply to the labels before mapping to 2D.
161   vtkGetObjectMacro(Transform, vtkTransform);
162   void SetTransform(vtkTransform* t);
163 
164   //BTX
165   /// Coordinate systems that output dataset may use.
166   enum Coordinates
167     {
168     WORLD=0,           //!< Output 3-D world-space coordinates for each label anchor.
169     DISPLAY=1          //!< Output 2-D display coordinates for each label anchor (3 components but only 2 are significant).
170     };
171   //ETX
172 
173   // Description:
174   // Set/get the coordinate system used for output labels.
175   // The output datasets may have point coordinates reported in the world space or display space.
176   vtkGetMacro(CoordinateSystem,int);
177   vtkSetClampMacro(CoordinateSystem,int,WORLD,DISPLAY);
CoordinateSystemWorld()178   void CoordinateSystemWorld() { this->SetCoordinateSystem( vtkLabeledDataMapper::WORLD ); }
CoordinateSystemDisplay()179   void CoordinateSystemDisplay() { this->SetCoordinateSystem( vtkLabeledDataMapper::DISPLAY ); }
180 
181   // Description:
182   // Return the modified time for this object.
183   virtual unsigned long GetMTime();
184 
185   // Description:
186   // Return the number of labels rendered by the mapper.
vtkGetMacro(NumberOfLabels,int)187   vtkGetMacro(NumberOfLabels, int)
188 
189   // Description:
190   // Return the position of the requested label.
191   void GetLabelPosition(int label, double pos[3])
192   {
193     assert("label index range" && label >= 0 && label < this->NumberOfLabels);
194     pos[0] = this->LabelPositions[3 * label];
195     pos[1] = this->LabelPositions[3 * label + 1];
196     pos[2] = this->LabelPositions[3 * label + 2];
197   }
198 
199   // Description:
200   // Return the text for the requested label.
201   const char *GetLabelText(int label);
202 
203 protected:
204   vtkLabeledDataMapper();
205   ~vtkLabeledDataMapper();
206 
207   vtkDataSet *Input;
208 
209   char  *LabelFormat;
210   int   LabelMode;
211   int   LabeledComponent;
212   int   FieldDataArray;
213   char  *FieldDataName;
214   int CoordinateSystem;
215 
216   vtkTimeStamp BuildTime;
217 
218   int NumberOfLabels;
219   int NumberOfLabelsAllocated;
220   vtkTextMapper **TextMappers;
221   double* LabelPositions;
222   vtkTransform *Transform;
223 
224   virtual int FillInputPortInformation(int, vtkInformation*);
225 
226   void AllocateLabels(int numLabels);
227   void BuildLabels();
228   void BuildLabelsInternal(vtkDataSet*);
229 
230   //BTX
231   class Internals;
232   Internals* Implementation;
233   //ETX
234 
235 private:
236   vtkLabeledDataMapper(const vtkLabeledDataMapper&);  // Not implemented.
237   void operator=(const vtkLabeledDataMapper&);  // Not implemented.
238 };
239 
240 #endif
241 
242