forked from Kitware/VTK
/
vtkInteractiveArea.cxx
273 lines (227 loc) · 8.66 KB
/
vtkInteractiveArea.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*=========================================================================
Program: Visualization Toolkit
Module: vtkInteractiveArea.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include <algorithm>
#include "vtkCommand.h"
#include "vtkContextClip.h"
#include "vtkContextMouseEvent.h"
#include "vtkContextScene.h"
#include "vtkContextTransform.h"
#include "vtkInteractiveArea.h"
#include "vtkObjectFactory.h"
#include "vtkPlotGrid.h"
#include "vtkTransform2D.h"
#include "vtkVectorOperators.h"
//@{
/**
* Hold mouse action key-mappings and other action related resources.
*/
class vtkInteractiveArea::MouseActions
{
public:
enum
{
MaxAction = 1
};
MouseActions()
{
this->Pan() = vtkContextMouseEvent::LEFT_BUTTON;
// this->Zoom() = vtkContextMouseEvent::MIDDLE_BUTTON;
}
short& Pan() { return Data[0]; }
// short& Zoom() { return Data[1]; }
/**
* The box created as the mouse is dragged around the screen.
*/
vtkRectf MouseBox;
private:
MouseActions(MouseActions const&) = delete;
void operator=(MouseActions const*) = delete;
short Data[MaxAction];
};
//@}
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkInteractiveArea);
//------------------------------------------------------------------------------
vtkInteractiveArea::vtkInteractiveArea()
: Actions(new MouseActions)
{
Superclass::Interactive = true;
}
//------------------------------------------------------------------------------
vtkInteractiveArea::~vtkInteractiveArea()
{
delete Actions;
}
//------------------------------------------------------------------------------
void vtkInteractiveArea::PrintSelf(ostream& os, vtkIndent indent)
{
Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------------
void vtkInteractiveArea::SetAxisRange(vtkRectd const& data)
{
/// TODO This might be a hack. The intention is to only reset the axis range in
// Superclass::LayoutAxes at initialization and not during interaction.
if (!this->Scene->GetDirty())
{
Superclass::SetAxisRange(data);
}
}
//------------------------------------------------------------------------------
bool vtkInteractiveArea::Paint(vtkContext2D* painter)
{
return Superclass::Paint(painter);
}
//------------------------------------------------------------------------------
bool vtkInteractiveArea::Hit(const vtkContextMouseEvent& mouse)
{
if (!this->Interactive)
{
return false;
}
vtkVector2i const pos(mouse.GetScreenPos());
vtkVector2i const bottomLeft = this->DrawAreaGeometry.GetBottomLeft();
vtkVector2i const topRight = this->DrawAreaGeometry.GetTopRight();
if (pos[0] > bottomLeft[0] && pos[0] < topRight[0] && pos[1] > bottomLeft[1] &&
pos[1] < topRight[1])
{
return true;
}
return false;
}
//------------------------------------------------------------------------------
bool vtkInteractiveArea::MouseWheelEvent(const vtkContextMouseEvent& vtkNotUsed(mouse), int delta)
{
// Adjust the grid (delta stands for the number of wheel clicks)
this->RecalculateTickSpacing(this->TopAxis, delta);
this->RecalculateTickSpacing(this->BottomAxis, delta);
this->RecalculateTickSpacing(this->LeftAxis, delta);
this->RecalculateTickSpacing(this->RightAxis, delta);
// Mark the scene as dirty
this->Scene->SetDirty(true);
// ComputeViewTransform is called through Superclass::Paint
this->InvokeEvent(vtkCommand::InteractionEvent);
return true;
}
//------------------------------------------------------------------------------
bool vtkInteractiveArea::MouseMoveEvent(const vtkContextMouseEvent& mouse)
{
if (mouse.GetButton() == this->Actions->Pan())
{
// Figure out how much the mouse has moved by in plot coordinates - pan
vtkVector2d screenPos(mouse.GetScreenPos().Cast<double>().GetData());
vtkVector2d lastScreenPos(mouse.GetLastScreenPos().Cast<double>().GetData());
vtkVector2d pos(0.0, 0.0);
vtkVector2d last(0.0, 0.0);
// Go from screen to scene coordinates to work out the delta
vtkAxis* xAxis = this->BottomAxis;
vtkAxis* yAxis = this->LeftAxis;
vtkTransform2D* transform = this->Transform->GetTransform();
transform->InverseTransformPoints(screenPos.GetData(), pos.GetData(), 1);
transform->InverseTransformPoints(lastScreenPos.GetData(), last.GetData(), 1);
vtkVector2d delta = last - pos;
delta[0] /= xAxis->GetScalingFactor();
delta[1] /= yAxis->GetScalingFactor();
// Now move the axis and recalculate the transform
delta[0] = delta[0] > 0 ? std::min(delta[0], xAxis->GetMaximumLimit() - xAxis->GetMaximum())
: std::max(delta[0], xAxis->GetMinimumLimit() - xAxis->GetMinimum());
delta[1] = delta[1] > 0 ? std::min(delta[1], yAxis->GetMaximumLimit() - yAxis->GetMaximum())
: std::max(delta[1], yAxis->GetMinimumLimit() - yAxis->GetMinimum());
xAxis->SetMinimum(xAxis->GetMinimum() + delta[0]);
xAxis->SetMaximum(xAxis->GetMaximum() + delta[0]);
yAxis->SetMinimum(yAxis->GetMinimum() + delta[1]);
yAxis->SetMaximum(yAxis->GetMaximum() + delta[1]);
// Mark the scene as dirty
this->Scene->SetDirty(true);
// ComputeViewTransform is called through Superclass::Paint
this->InvokeEvent(vtkCommand::InteractionEvent);
return true;
}
return false;
}
//------------------------------------------------------------------------------
bool vtkInteractiveArea::MouseButtonPressEvent(const vtkContextMouseEvent& mouse)
{
if (mouse.GetButton() == this->Actions->Pan())
{
this->Actions->MouseBox.Set(mouse.GetPos().GetX(), mouse.GetPos().GetY(), 0.0, 0.0);
return true;
}
return false;
}
//------------------------------------------------------------------------------
void vtkInteractiveArea::RecalculateTickSpacing(vtkAxis* axis, int const numClicks)
{
double min = axis->GetMinimum();
double max = axis->GetMaximum();
double const increment = (max - min) * 0.1;
if (increment > 0.0)
{
min += numClicks * increment;
max -= numClicks * increment;
}
else
{
min -= numClicks * increment;
max += numClicks * increment;
}
axis->SetMinimum(min);
axis->SetMaximum(max);
axis->RecalculateTickSpacing();
}
//------------------------------------------------------------------------------
void vtkInteractiveArea::ComputeViewTransform()
{
double const minX = this->BottomAxis->GetMinimum();
double const minY = this->LeftAxis->GetMinimum();
vtkVector2d origin(minX, minY);
vtkVector2d scale(this->BottomAxis->GetMaximum() - minX, this->LeftAxis->GetMaximum() - minY);
vtkVector2d shift(0.0, 0.0);
vtkVector2d factor(1.0, 1.0);
/// TODO Cache and only compute if zoom changed
this->ComputeZoom(origin, scale, shift, factor);
this->BottomAxis->SetScalingFactor(factor[0]);
this->BottomAxis->SetShift(shift[0]);
this->LeftAxis->SetScalingFactor(factor[1]);
this->LeftAxis->SetShift(shift[1]);
// Update transform
this->Transform->Identity();
vtkRecti& boundsPixel = this->DrawAreaGeometry;
float const xOrigin = static_cast<float>(boundsPixel.GetLeft());
float const yOrigin = static_cast<float>(boundsPixel.GetBottom());
this->Transform->Translate(xOrigin, yOrigin);
float const xScalePixels = this->DrawAreaGeometry.GetWidth() / scale[0];
float const yScalePixels = this->DrawAreaGeometry.GetHeight() / scale[1];
this->Transform->Scale(xScalePixels, yScalePixels);
float const xTrans = -(this->BottomAxis->GetMinimum() + shift[0]) * factor[0];
float const yTrans = -(this->LeftAxis->GetMinimum() + shift[1]) * factor[1];
this->Transform->Translate(xTrans, yTrans);
}
//------------------------------------------------------------------------------
void vtkInteractiveArea::ComputeZoom(
vtkVector2d const& origin, vtkVector2d& scale, vtkVector2d& shift, vtkVector2d& factor)
{
for (int i = 0; i < 2; ++i)
{
if (log10(fabs(origin[i]) / scale[i]) > 2)
{
shift[i] = floor(log10(fabs(origin[i]) / scale[i]) / 3.0) * 3.0;
shift[i] = -origin[i];
}
if (fabs(log10(scale[i])) > 10)
{
// We need to scale the transform to show all data, do this in blocks.
factor[i] = pow(10.0, floor(log10(scale[i]) / 10.0) * -10.0);
scale[i] = scale[i] * factor[i];
}
}
}