VTK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 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 =
66  vtkPlot::SafeDownCast(tmp->GetProperties()->Get(vtkSelectionNode::PROP()));
67  if (selectionPlot == plot)
68  {
69  node = tmp;
70  break;
71  }
72  }
73  if (!node)
74  {
76  selection->AddNode(node.GetPointer());
77  node->SetContentType(vtkSelectionNode::INDICES);
78  node->SetFieldType(vtkSelectionNode::POINT);
79  node->GetProperties()->Set(vtkSelectionNode::PROP(), plot);
80  node->GetProperties()->Set(vtkSelectionNode::SOURCE(), plot->GetInput());
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());
90  node->SetContentType(vtkSelectionNode::INDICES);
91  node->SetFieldType(vtkSelectionNode::POINT);
92  node->SetSelectionList(selectionIds);
93  link->SetCurrentSelection(selection.GetPointer());
94  }
95 }
96 
98 
99 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 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 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 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  {
235  case vtkContextScene::SELECTION_ADDITION:
236  AddSelection(plotSelection, oldSelection);
237  break;
238  case vtkContextScene::SELECTION_SUBTRACTION:
239  MinusSelection(plotSelection, oldSelection);
240  break;
241  case vtkContextScene::SELECTION_TOGGLE:
242  ToggleSelection(plotSelection, oldSelection);
243  break;
244  case vtkContextScene::SELECTION_DEFAULT:
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 int GetMouseSelectionMode(const vtkContextMouseEvent &mouse, int selectionMode)
260 {
261  // Mouse modifiers override the current selection mode.
262  if (mouse.GetModifiers() & vtkContextMouseEvent::SHIFT_MODIFIER &&
263  mouse.GetModifiers() & vtkContextMouseEvent::CONTROL_MODIFIER)
264  {
265  return vtkContextScene::SELECTION_TOGGLE;
266  }
267  else if (mouse.GetModifiers() & vtkContextMouseEvent::SHIFT_MODIFIER)
268  {
269  return vtkContextScene::SELECTION_ADDITION;
270  }
271  else if (mouse.GetModifiers() & vtkContextMouseEvent::CONTROL_MODIFIER)
272  {
273  return vtkContextScene::SELECTION_SUBTRACTION;
274  }
275  return selectionMode;
276 }
278 
279 } // End vtkChartSelectionHelper namespace
280 
281 #endif // __vtkChartSelectionHelper_h
282 // VTK-HeaderTest-Exclude: vtkChartSelectionHelper.h
vtkIdType GetNumberOfTuples()
void MakeSelection(vtkAnnotationLink *link, vtkIdTypeArray *selectionIds, vtkPlot *plot)
void MinusSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
virtual void SetNumberOfTuples(vtkIdType number)=0
dynamic, self-adjusting array of vtkIdType
Hold a reference to a vtkObjectBase instance.
int vtkIdType
Definition: vtkType.h:268
static vtkSmartPointer< T > New()
static vtkPlot * SafeDownCast(vtkObjectBase *o)
void BuildSelection(vtkAnnotationLink *link, int selectionMode, vtkIdTypeArray *plotSelection, vtkIdTypeArray *oldSelection, vtkPlot *plot)
Abstract class for 2D plots.
Definition: vtkPlot.h:52
virtual void * GetVoidPointer(vtkIdType id)=0
void ToggleSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
virtual vtkTable * GetInput()
helper functions for making selections in charts.
void AddSelection(vtkIdTypeArray *selection, vtkIdTypeArray *oldSelection)
T * GetPointer() const
Definition: vtkNew.h:108
Allocate and hold a VTK object.
Definition: vtkNew.h:66
T * GetPointer() const
int GetMouseSelectionMode(const vtkContextMouseEvent &mouse, int selectionMode)