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 
26 #ifndef vtkChartSelectionHelper_h
27 #define vtkChartSelectionHelper_h
28 
29 #include "vtkNew.h"
30 #include "vtkSmartPointer.h"
31 #include "vtkAnnotationLink.h"
32 #include "vtkSelection.h"
33 #include "vtkSelectionNode.h"
34 #include "vtkIdTypeArray.h"
35 #include "vtkContextScene.h"
36 #include "vtkContextMouseEvent.h"
37 #include "vtkInformation.h"
38 #include "vtkPlot.h"
39 #include "vtkTable.h"
40 
41 #include <vector>
42 #include <algorithm>
43 
45 {
46 
51 static void MakeSelection(vtkAnnotationLink *link, vtkIdTypeArray *selectionIds,
52  vtkPlot *plot)
53 {
54  assert(link != nullptr && selectionIds != nullptr);
55 
56  if (plot)
57  {
58  // We are building up plot-based selections, using multiple nodes.
59  vtkSelection *selection = link->GetCurrentSelection();
61  for (unsigned int i = 0; i < selection->GetNumberOfNodes(); ++i)
62  {
63  vtkSelectionNode *tmp = selection->GetNode(i);
64  vtkPlot *selectionPlot =
66  if (selectionPlot == plot)
67  {
68  node = tmp;
69  break;
70  }
71  }
72  if (!node)
73  {
75  selection->AddNode(node.GetPointer());
78  node->GetProperties()->Set(vtkSelectionNode::PROP(), plot);
80  }
81  node->SetSelectionList(selectionIds);
82  }
83  else
84  {
85  // Use a simple single selection node layout, remove previous selections.
86  vtkNew<vtkSelection> selection;
88  selection->AddNode(node);
91  node->SetSelectionList(selectionIds);
92  link->SetCurrentSelection(selection);
93  }
94 }
95 
99 static void MinusSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
100 {
101  // We rely on the selection id arrays being sorted.
102  std::vector<vtkIdType> output;
103  vtkIdType *ptrSelection =
104  static_cast<vtkIdType *>(selection->GetVoidPointer(0));
105  vtkIdType *ptrOldSelection =
106  static_cast<vtkIdType *>(oldSelection->GetVoidPointer(0));
107  vtkIdType oldSize = oldSelection->GetNumberOfTuples();
108  vtkIdType size = selection->GetNumberOfTuples();
109  vtkIdType i = 0;
110  vtkIdType iOld = 0;
111 
112  while (i < size && iOld < oldSize)
113  {
114  if (ptrSelection[i] > ptrOldSelection[iOld]) // Skip the value.
115  {
116  output.push_back(ptrOldSelection[iOld++]);
117  }
118  else if (ptrSelection[i] == ptrOldSelection[iOld]) // Match - remove.
119  {
120  ++i;
121  ++iOld;
122  }
123  else if (ptrSelection[i] < ptrOldSelection[iOld]) // Add the new value.
124  {
125  ++i;
126  }
127  }
128  while (iOld < oldSize)
129  {
130  output.push_back(ptrOldSelection[iOld++]);
131  }
132  selection->SetNumberOfTuples(static_cast<vtkIdType>(output.size()));
133  ptrSelection = static_cast<vtkIdType *>(selection->GetVoidPointer(0));
134  for (std::vector<vtkIdType>::iterator it = output.begin();
135  it != output.end(); ++it, ++ptrSelection)
136  {
137  *ptrSelection = *it;
138  }
139 }
140 
144 static void AddSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
145 {
146  // Add all unique array indices to create a new combined array.
147  vtkIdType *ptrSelection =
148  static_cast<vtkIdType *>(selection->GetVoidPointer(0));
149  vtkIdType *ptrOldSelection =
150  static_cast<vtkIdType *>(oldSelection->GetVoidPointer(0));
151  std::vector<vtkIdType> output(selection->GetNumberOfTuples()
152  + oldSelection->GetNumberOfTuples());
153  std::vector<vtkIdType>::iterator it;
154  it = std::set_union(ptrSelection,
155  ptrSelection + selection->GetNumberOfTuples(),
156  ptrOldSelection,
157  ptrOldSelection + oldSelection->GetNumberOfTuples(),
158  output.begin());
159  int newSize = int(it - output.begin());
160  selection->SetNumberOfTuples(newSize);
161  ptrSelection = static_cast<vtkIdType *>(selection->GetVoidPointer(0));
162  for (std::vector<vtkIdType>::iterator i = output.begin(); i != it;
163  ++i, ++ptrSelection)
164  {
165  *ptrSelection = *i;
166  }
167 }
168 
172 static void ToggleSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
173 {
174  // We rely on the selection id arrays being sorted.
175  std::vector<vtkIdType> output;
176  vtkIdType *ptrSelection =
177  static_cast<vtkIdType *>(selection->GetVoidPointer(0));
178  vtkIdType *ptrOldSelection =
179  static_cast<vtkIdType *>(oldSelection->GetVoidPointer(0));
180  vtkIdType oldSize = oldSelection->GetNumberOfTuples();
181  vtkIdType size = selection->GetNumberOfTuples();
182  vtkIdType i = 0;
183  vtkIdType iOld = 0;
184  while (i < size && iOld < oldSize)
185  {
186  if (ptrSelection[i] > ptrOldSelection[iOld]) // Retain the value.
187  {
188  output.push_back(ptrOldSelection[iOld++]);
189  }
190  else if (ptrSelection[i] == ptrOldSelection[iOld]) // Match - toggle.
191  {
192  ++i;
193  ++iOld;
194  }
195  else if (ptrSelection[i] < ptrOldSelection[iOld]) // Add the new value.
196  {
197  output.push_back(ptrSelection[i++]);
198  }
199  }
200  while (i < size)
201  {
202  output.push_back(ptrSelection[i++]);
203  }
204  while (iOld < oldSize)
205  {
206  output.push_back(ptrOldSelection[iOld++]);
207  }
208  selection->SetNumberOfTuples(static_cast<vtkIdType>(output.size()));
209  ptrSelection = static_cast<vtkIdType *>(selection->GetVoidPointer(0));
210  for (std::vector<vtkIdType>::iterator it = output.begin();
211  it != output.end(); ++it, ++ptrSelection)
212  {
213  *ptrSelection = *it;
214  }
215 }
216 
222 static void BuildSelection(vtkAnnotationLink *link, int selectionMode,
223  vtkIdTypeArray *plotSelection, vtkIdTypeArray *oldSelection,
224  vtkPlot *plot)
225 {
226  if (!plotSelection || !oldSelection)
227  {
228  return;
229  }
230 
231  // Build a selection and set it on the annotation link if not null.
232  switch(selectionMode)
233  {
235  AddSelection(plotSelection, oldSelection);
236  break;
238  MinusSelection(plotSelection, oldSelection);
239  break;
241  ToggleSelection(plotSelection, oldSelection);
242  break;
244  default:
245  // Nothing necessary - overwrite the old selection.
246  break;
247  }
248 
249  if (link)
250  {
251  MakeSelection(link, plotSelection, plot);
252  }
253 }
254 
259 static int GetMouseSelectionMode(const vtkContextMouseEvent &mouse, int selectionMode)
260 {
261  // Mouse modifiers override the current selection mode.
264  {
266  }
268  {
270  }
272  {
274  }
275  return selectionMode;
276 }
277 
278 } // End vtkChartSelectionHelper namespace
279 
280 #endif // vtkChartSelectionHelper_h
281 // VTK-HeaderTest-Exclude: vtkChartSelectionHelper.h
A node in a selection tree.
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.
vtkIdType GetNumberOfTuples()
Get the number of complete tuples (a component group) in the array.
The selection data provided is point-data.
VTKCOMMONCORE_EXPORT void Set(vtkInformationRequestKey *key)
Get/Set a request-valued entry.
static void MinusSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
Subtract the supplied selection from the oldSelection.
data object that represents a "selection" in VTK.
Definition: vtkSelection.h:63
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:347
virtual vtkInformation * GetProperties()
Returns the property map.
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
void SetNumberOfTuples(vtkIdType number) override
Set the number of tuples (a component group) in the array.
virtual std::string AddNode(vtkSelectionNode *)
Adds a selection node.
static vtkPlot * SafeDownCast(vtkObjectBase *o)
helper functions for making selections in charts.
data structure to represent mouse events.
static vtkInformationObjectBaseKey * PROP()
Pointer to the prop the selection belongs to.
virtual void SetContentType(int type)
Get or set the content type of the selection.
Abstract class for 2D plots.
Definition: vtkPlot.h:52
unsigned int GetNumberOfNodes() const
Returns the number of nodes in this selection.
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 ...
Allocate and hold a VTK object.
Definition: vtkNew.h:58
T * GetPointer() const
Get the contained pointer.
Select entities by their offsets into the dataset.
static vtkInformationObjectBaseKey * SOURCE()
Pointer to the data or algorithm the selection belongs to.
virtual void SetSelectionList(vtkAbstractArray *)
Sets the selection list.
virtual vtkSelectionNode * GetNode(unsigned int idx) const
Returns a node given it&#39;s index.
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.
void * GetVoidPointer(vtkIdType valueIdx) override
Get the address of a particular data index.