VTK
vtkChartSelectionHelper.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkVector.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 
27 #ifndef vtkChartSelectionHelper_h
28 #define vtkChartSelectionHelper_h
29 
30 #include "vtkNew.h"
31 #include "vtkSmartPointer.h"
32 #include "vtkAnnotationLink.h"
33 #include "vtkSelection.h"
34 #include "vtkSelectionNode.h"
35 #include "vtkIdTypeArray.h"
36 #include "vtkContextScene.h"
37 #include "vtkContextMouseEvent.h"
38 #include "vtkInformation.h"
39 #include "vtkPlot.h"
40 #include "vtkTable.h"
41 
42 #include <vector>
43 #include <algorithm>
44 
46 {
47 
52 static void MakeSelection(vtkAnnotationLink *link, vtkIdTypeArray *selectionIds,
53  vtkPlot *plot)
54 {
55  assert(link != NULL && selectionIds != NULL);
56 
57  if (plot)
58  {
59  // We are building up plot-based selections, using multiple nodes.
60  vtkSelection *selection = link->GetCurrentSelection();
62  for (unsigned int i = 0; i < selection->GetNumberOfNodes(); ++i)
63  {
64  vtkSelectionNode *tmp = selection->GetNode(i);
65  vtkPlot *selectionPlot =
67  if (selectionPlot == plot)
68  {
69  node = tmp;
70  break;
71  }
72  }
73  if (!node)
74  {
76  selection->AddNode(node.GetPointer());
79  node->GetProperties()->Set(vtkSelectionNode::PROP(), plot);
81  }
82  node->SetSelectionList(selectionIds);
83  }
84  else
85  {
86  // Use a simple single selection node layout, remove previous selections.
87  vtkNew<vtkSelection> selection;
89  selection->AddNode(node.GetPointer());
92  node->SetSelectionList(selectionIds);
93  link->SetCurrentSelection(selection.GetPointer());
94  }
95 }
96 
98 
101 static void MinusSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
102 {
103  // We rely on the selection id arrays being sorted.
104  std::vector<vtkIdType> output;
105  vtkIdType *ptrSelection =
106  static_cast<vtkIdType *>(selection->GetVoidPointer(0));
107  vtkIdType *ptrOldSelection =
108  static_cast<vtkIdType *>(oldSelection->GetVoidPointer(0));
109  vtkIdType oldSize = oldSelection->GetNumberOfTuples();
110  vtkIdType size = selection->GetNumberOfTuples();
111  vtkIdType i = 0;
112  vtkIdType iOld = 0;
114 
115  while (i < size && iOld < oldSize)
116  {
117  if (ptrSelection[i] > ptrOldSelection[iOld]) // Skip the value.
118  {
119  output.push_back(ptrOldSelection[iOld++]);
120  }
121  else if (ptrSelection[i] == ptrOldSelection[iOld]) // Match - remove.
122  {
123  ++i;
124  ++iOld;
125  }
126  else if (ptrSelection[i] < ptrOldSelection[iOld]) // Add the new value.
127  {
128  ++i;
129  }
130  }
131  while (iOld < oldSize)
132  {
133  output.push_back(ptrOldSelection[iOld++]);
134  }
135  selection->SetNumberOfTuples(output.size());
136  ptrSelection = static_cast<vtkIdType *>(selection->GetVoidPointer(0));
137  for (std::vector<vtkIdType>::iterator it = output.begin();
138  it != output.end(); ++it, ++ptrSelection)
139  {
140  *ptrSelection = *it;
141  }
142 }
143 
145 
148 static void AddSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
149 {
150  // Add all unique array indices to create a new combined array.
151  vtkIdType *ptrSelection =
152  static_cast<vtkIdType *>(selection->GetVoidPointer(0));
153  vtkIdType *ptrOldSelection =
154  static_cast<vtkIdType *>(oldSelection->GetVoidPointer(0));
155  std::vector<vtkIdType> output(selection->GetNumberOfTuples()
156  + oldSelection->GetNumberOfTuples());
157  std::vector<vtkIdType>::iterator it;
158  it = std::set_union(ptrSelection,
159  ptrSelection + selection->GetNumberOfTuples(),
160  ptrOldSelection,
161  ptrOldSelection + oldSelection->GetNumberOfTuples(),
162  output.begin());
163  int newSize = int(it - output.begin());
164  selection->SetNumberOfTuples(newSize);
165  ptrSelection = static_cast<vtkIdType *>(selection->GetVoidPointer(0));
166  for (std::vector<vtkIdType>::iterator i = output.begin(); i != it;
167  ++i, ++ptrSelection)
168  {
169  *ptrSelection = *i;
170  }
171 }
173 
175 
178 static void ToggleSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
179 {
180  // We rely on the selection id arrays being sorted.
181  std::vector<vtkIdType> output;
182  vtkIdType *ptrSelection =
183  static_cast<vtkIdType *>(selection->GetVoidPointer(0));
184  vtkIdType *ptrOldSelection =
185  static_cast<vtkIdType *>(oldSelection->GetVoidPointer(0));
186  vtkIdType oldSize = oldSelection->GetNumberOfTuples();
187  vtkIdType size = selection->GetNumberOfTuples();
188  vtkIdType i = 0;
189  vtkIdType iOld = 0;
190  while (i < size && iOld < oldSize)
191  {
192  if (ptrSelection[i] > ptrOldSelection[iOld]) // Retain the value.
193  {
194  output.push_back(ptrOldSelection[iOld++]);
195  }
196  else if (ptrSelection[i] == ptrOldSelection[iOld]) // Match - toggle.
197  {
198  ++i;
199  ++iOld;
200  }
201  else if (ptrSelection[i] < ptrOldSelection[iOld]) // Add the new value.
202  {
203  output.push_back(ptrSelection[i++]);
204  }
205  }
206  while (i < size)
207  {
208  output.push_back(ptrSelection[i++]);
209  }
210  while (iOld < oldSize)
211  {
212  output.push_back(ptrOldSelection[iOld++]);
213  }
214  selection->SetNumberOfTuples(output.size());
215  ptrSelection = static_cast<vtkIdType *>(selection->GetVoidPointer(0));
216  for (std::vector<vtkIdType>::iterator it = output.begin();
217  it != output.end(); ++it, ++ptrSelection)
218  {
219  *ptrSelection = *it;
220  }
221 }
223 
229 static void BuildSelection(vtkAnnotationLink *link, int selectionMode,
230  vtkIdTypeArray *plotSelection, vtkIdTypeArray *oldSelection,
231  vtkPlot *plot)
232 {
233  if (!plotSelection || !oldSelection)
234  {
235  return;
236  }
237 
238  // Build a selection and set it on the annotation link if not null.
239  switch(selectionMode)
240  {
242  AddSelection(plotSelection, oldSelection);
243  break;
245  MinusSelection(plotSelection, oldSelection);
246  break;
248  ToggleSelection(plotSelection, oldSelection);
249  break;
251  default:
252  // Nothing necessary - overwrite the old selection.
253  break;
254  }
255 
256  if (link)
257  {
258  MakeSelection(link, plotSelection, plot);
259  }
260 }
261 
263 
267 static int GetMouseSelectionMode(const vtkContextMouseEvent &mouse, int selectionMode)
268 {
269  // Mouse modifiers override the current selection mode.
272  {
274  }
276  {
278  }
280  {
282  }
283  return selectionMode;
284 }
286 
287 } // End vtkChartSelectionHelper namespace
288 
289 #endif // vtkChartSelectionHelper_h
290 // VTK-HeaderTest-Exclude: vtkChartSelectionHelper.h
A node in a selection tree.
unsigned int GetNumberOfNodes()
Returns the number of nodes in this selection.
virtual void SetFieldType(int type)
Get or set the field type of the selection.
static void AddSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
Add the supplied selection from the oldSelection.
virtual void * GetVoidPointer(vtkIdType valueIdx)=0
Return a void pointer.
vtkIdType GetNumberOfTuples()
Get the number of complete tuples (a component group) in the array.
VTKCOMMONCORE_EXPORT void Set(vtkInformationRequestKey *key)
Get/Set a request-valued entry.
virtual void AddNode(vtkSelectionNode *)
Adds a selection node.
static void MinusSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
Subtract the supplied selection from the oldSelection.
A node in a selection tree.
Definition: vtkSelection.h:43
static void MakeSelection(vtkAnnotationLink *link, vtkIdTypeArray *selectionIds, vtkPlot *plot)
Populate the annotation link with the supplied selectionIds array, and set the appropriate node prope...
dynamic, self-adjusting array of vtkIdType
Hold a reference to a vtkObjectBase instance.
int vtkIdType
Definition: vtkType.h:287
virtual vtkInformation * GetProperties()
Returns the property map.
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
static vtkPlot * SafeDownCast(vtkObjectBase *o)
data structure to represent mouse events.
static vtkInformationObjectBaseKey * PROP()
Pointer to the prop the selection belongs to.
virtual vtkSelectionNode * GetNode(unsigned int idx)
Returns a node given it's index.
virtual void SetContentType(int type)
Get or set the content type of the selection.
Abstract class for 2D plots.
Definition: vtkPlot.h:52
virtual vtkTable * GetInput()
Get the input table used by the plot.
int GetModifiers() const
Return the modifier keys, if any, ORed together.
static void BuildSelection(vtkAnnotationLink *link, int selectionMode, vtkIdTypeArray *plotSelection, vtkIdTypeArray *oldSelection, vtkPlot *plot)
Build a selection based on the supplied selectionMode using the new plotSelection and combining it wi...
static int GetMouseSelectionMode(const vtkContextMouseEvent &mouse, int selectionMode)
Combine the SelectionMode with any mouse modifiers to get an effective selection mode for this click ...
T * GetPointer() const
Get a raw pointer to the contained object.
Definition: vtkNew.h:113
Allocate and hold a VTK object.
Definition: vtkNew.h:67
T * GetPointer() const
Get the contained pointer.
virtual void SetNumberOfTuples(vtkIdType numTuples)=0
Set the number of tuples (a component group) in the array.
static vtkInformationObjectBaseKey * SOURCE()
Pointer to the data or algorithm the selection belongs to.
virtual void SetSelectionList(vtkAbstractArray *)
Sets the selection list.
static void ToggleSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
Toggle the supplied selection from the oldSelection.
VTKCOMMONCORE_EXPORT int Get(vtkInformationIntegerKey *key)
Get/Set an integer-valued entry.