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 
48 
51 static void MakeSelection(vtkAnnotationLink *link, vtkIdTypeArray *selectionIds,
52  vtkPlot *plot)
53 {
54  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 
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;
112 
113  while (i < size && iOld < oldSize)
114  {
115  if (ptrSelection[i] > ptrOldSelection[iOld]) // Skip the value.
116  {
117  output.push_back(ptrOldSelection[iOld++]);
118  }
119  else if (ptrSelection[i] == ptrOldSelection[iOld]) // Match - remove.
120  {
121  ++i;
122  ++iOld;
123  }
124  else if (ptrSelection[i] < ptrOldSelection[iOld]) // Add the new value.
125  {
126  ++i;
127  }
128  }
129  while (iOld < oldSize)
130  {
131  output.push_back(ptrOldSelection[iOld++]);
132  }
133  selection->SetNumberOfTuples(output.size());
134  ptrSelection = static_cast<vtkIdType *>(selection->GetVoidPointer(0));
135  for (std::vector<vtkIdType>::iterator it = output.begin();
136  it != output.end(); ++it, ++ptrSelection)
137  {
138  *ptrSelection = *it;
139  }
140 }
141 
143 
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 }
169 
171 
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(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 }
217 
219 
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  }
231 
232  // Build a selection and set it on the annotation link if not null.
233  switch(selectionMode)
234  {
236  AddSelection(plotSelection, oldSelection);
237  break;
239  MinusSelection(plotSelection, oldSelection);
240  break;
242  ToggleSelection(plotSelection, oldSelection);
243  break;
245  default:
246  // Nothing necessary - overwrite the old selection.
247  break;
248  }
249 
250  if (link)
251  {
252  MakeSelection(link, plotSelection, plot);
253  }
254 }
255 
257 
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 }
278 
279 } // End vtkChartSelectionHelper namespace
280 
281 #endif // vtkChartSelectionHelper_h
282 // VTK-HeaderTest-Exclude: vtkChartSelectionHelper.h
A node in a selection tree. Used to store selection results.
unsigned int GetNumberOfNodes()
virtual void SetFieldType(int type)
static void AddSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
vtkIdType GetNumberOfTuples()
VTKCOMMONCORE_EXPORT void Set(vtkInformationRequestKey *key)
virtual void AddNode(vtkSelectionNode *)
static void MinusSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
A node in a selection tree. Used to store selection results.
Definition: vtkSelection.h:44
virtual void SetNumberOfTuples(vtkIdType number)=0
static void MakeSelection(vtkAnnotationLink *link, vtkIdTypeArray *selectionIds, vtkPlot *plot)
dynamic, self-adjusting array of vtkIdType
Hold a reference to a vtkObjectBase instance.
int vtkIdType
Definition: vtkType.h:247
virtual vtkInformation * GetProperties()
static vtkSmartPointer< T > New()
static vtkPlot * SafeDownCast(vtkObjectBase *o)
data structure to represent mouse events.
static vtkInformationObjectBaseKey * PROP()
virtual vtkSelectionNode * GetNode(unsigned int idx)
virtual void SetContentType(int type)
Abstract class for 2D plots.
Definition: vtkPlot.h:51
virtual void * GetVoidPointer(vtkIdType id)=0
virtual vtkTable * GetInput()
int GetModifiers() const
static void BuildSelection(vtkAnnotationLink *link, int selectionMode, vtkIdTypeArray *plotSelection, vtkIdTypeArray *oldSelection, vtkPlot *plot)
static int GetMouseSelectionMode(const vtkContextMouseEvent &mouse, int selectionMode)
T * GetPointer() const
Definition: vtkNew.h:108
Allocate and hold a VTK object.
Definition: vtkNew.h:66
T * GetPointer() const
static vtkInformationObjectBaseKey * SOURCE()
virtual void SetSelectionList(vtkAbstractArray *)
static void ToggleSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
VTKCOMMONCORE_EXPORT int Get(vtkInformationIntegerKey *key)