vtkChartXYZ.h 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkChartXYZ.h

  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.

=========================================================================*/

// .NAME vtkChartXYZ - Factory class for drawing 3D XYZ charts.
//
// .SECTION Description

20 21
#ifndef vtkChartXYZ_h
#define vtkChartXYZ_h
22

23
#include "vtkChartsCoreModule.h" // For export macro
24
#include "vtkContextItem.h"
25
#include "vtkColor.h"        // For vtkColor4ub
26 27 28
#include "vtkRect.h"         // For vtkRectf ivars
#include "vtkNew.h"          // For ivars
#include "vtkSmartPointer.h" // For ivars
29
#include <vector>            // For ivars
30 31 32

class vtkAnnotationLink;
class vtkAxis;
Zack Galbreath's avatar
Zack Galbreath committed
33 34 35
class vtkContext3D;
class vtkContextMouseEvent;
class vtkPen;
36
class vtkPlaneCollection;
Zack Galbreath's avatar
Zack Galbreath committed
37
class vtkPlot3D;
38 39
class vtkTable;
class vtkTransform;
Zack Galbreath's avatar
Zack Galbreath committed
40
class vtkUnsignedCharArray;
41

42
class VTKCHARTSCORE_EXPORT vtkChartXYZ : public vtkContextItem
43 44 45 46 47 48 49
{
public:
  vtkTypeMacro(vtkChartXYZ, vtkContextItem);
  virtual void PrintSelf(ostream &os, vtkIndent indent);

  static vtkChartXYZ * New();

50
  // Description:
Zack Galbreath's avatar
Zack Galbreath committed
51 52 53 54 55
  // Set the geometry in pixel coordinates (origin and width/height).
  // This method also sets up the end points of the axes of the chart.
  // For this reason, if you call SetAroundX(), you should call SetGeometry()
  // afterwards.
  void SetGeometry(const vtkRectf &bounds);
56 57

  // Description:
Zack Galbreath's avatar
Zack Galbreath committed
58 59
  // Set the rotation angle for the chart (AutoRotate mode only).
  void SetAngle(double angle);
60 61

  // Description:
Zack Galbreath's avatar
Zack Galbreath committed
62 63
  // Set whether or not we're rotating about the X axis.
  void SetAroundX(bool isX);
64 65 66 67 68

  // Description:
  // Set the vtkAnnotationLink for the chart.
  virtual void SetAnnotationLink(vtkAnnotationLink *link);

69 70 71 72
  // Description:
  // Get the x (0), y (1) or z (2) axis.
  vtkAxis * GetAxis(int axis);

73 74 75 76 77
  // Description:
  // Set the color for the axes.
  void SetAxisColor(const vtkColor4ub& color);
  vtkColor4ub GetAxisColor();

Zack Galbreath's avatar
Zack Galbreath committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  // Description:
  // Set whether or not we're using this chart to rotate on a timer.
  // Default value is false.
  void SetAutoRotate(bool b);

  // Description:
  // Set whether or not axes labels & tick marks should be drawn.
  // Default value is true.
  void SetDecorateAxes(bool b);

  // Description:
  // Set whether or not the chart should automatically resize itself to fill
  // the scene.  Default value is true.
  void SetFitToScene(bool b);

93 94 95 96
  // Description:
  // Perform any updates to the item that may be necessary before rendering.
  virtual void Update();

Zack Galbreath's avatar
Zack Galbreath committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  // Description:
  // Paint event for the chart, called whenever the chart needs to be drawn.
  virtual bool Paint(vtkContext2D *painter);

  // Description:
  // Adds a plot to the chart.
  virtual vtkIdType AddPlot(vtkPlot3D* plot);

  // Description:
  // Remove all the plots from this chart.
  void ClearPlots();

  // Description:
  // Determine the XYZ bounds of the plots within this chart.
  // This information is then used to set the range of the axes.
112 113
  void RecalculateBounds();

114
  // Description:
Zack Galbreath's avatar
Zack Galbreath committed
115 116 117 118
  // Use this chart's Geometry to set the endpoints of its axes.
  // This method also sets up a transformation that is used to
  // properly render the data within the chart.
  void RecalculateTransform();
119

Zack Galbreath's avatar
Zack Galbreath committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  // Description:
  // Returns true if the transform is interactive, false otherwise.
  virtual bool Hit(const vtkContextMouseEvent &mouse);

  // Description:
  // Mouse press event. Keep track of zoom anchor position.
  virtual bool MouseButtonPressEvent(const vtkContextMouseEvent &mouse);

  // Description:
  // Mouse move event. Perform pan or zoom as specified by the mouse bindings.
  virtual bool MouseMoveEvent(const vtkContextMouseEvent &mouse);

  // Description:
  // Mouse wheel event.  Zooms in or out.
  virtual bool MouseWheelEvent(const vtkContextMouseEvent &mouse, int delta);

  // Description:
  // Key press event.  This allows the user to snap the chart to one of three
  // different 2D views.  "x" changes the view so we're looking down the X axis.
  // Similar behavior occurs for "y" or "z".
  virtual bool KeyPressEvent(const vtkContextKeyEvent &key);
141 142 143 144 145

protected:
  vtkChartXYZ();
  ~vtkChartXYZ();

Zack Galbreath's avatar
Zack Galbreath committed
146 147 148 149 150 151
  // Description:
  // Calculate the transformation matrices used to draw data points and axes
  // in the scene.  This function also sets up clipping planes that determine
  // whether or not a data point is within range.
  virtual void CalculateTransforms();

152 153 154 155 156 157 158 159
  // Description:
  // Given the x, y and z vtkAxis, and a transform, calculate the transform that
  // the points in a chart would need to be drawn within the axes. This assumes
  // that the axes have the correct start and end positions, and that they are
  // perpendicular.
  bool CalculatePlotTransform(vtkAxis *x, vtkAxis *y, vtkAxis *z,
                              vtkTransform *transform);

Zack Galbreath's avatar
Zack Galbreath committed
160 161 162 163 164 165 166 167 168 169 170
  // Description:
  // Rotate the chart in response to a mouse movement.
  bool Rotate(const vtkContextMouseEvent &mouse);

  // Description:
  // Pan the data within the chart in response to a mouse movement.
  bool Pan(const vtkContextMouseEvent &mouse);

  // Description:
  // Zoom in or out on the data in response to a mouse movement.
  bool Zoom(const vtkContextMouseEvent &mouse);
171

Zack Galbreath's avatar
Zack Galbreath committed
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
  // Description:
  // Spin the chart in response to a mouse movement.
  bool Spin(const vtkContextMouseEvent &mouse);

  // Description:
  // Adjust the rotation of the chart so that we are looking down the X axis.
  void LookDownX();

  // Description:
  // Adjust the rotation of the chart so that we are looking down the Y axis.
  void LookDownY();

  // Description:
  // Adjust the rotation of the chart so that we are looking down the Z axis.
  void LookDownZ();

  // Description:
  // Adjust the rotation of the chart so that we are looking up the X axis.
  void LookUpX();

  // Description:
  // Adjust the rotation of the chart so that we are looking up the Y axis.
  void LookUpY();

  // Description:
  // Adjust the rotation of the chart so that we are looking up the Z axis.
  void LookUpZ();

  // Description:
  // Check to see if the scene changed size since the last render.
  bool CheckForSceneResize();

  // Description:
  // Scale the axes up or down in response to a scene resize.
  void RescaleAxes();

  // Description:
  // Scale up the axes when the scene gets larger.
  void ScaleUpAxes();

  // Description:
  // Scale down the axes when the scene gets smaller.
  void ScaleDownAxes();

  // Description:
  // Change the scaling of the axes by a specified amount.
  void ZoomAxes(int delta);

  // Description:
  // Initialize a list of "test points".  These are used to determine whether
  // or not the chart fits completely within the bounds of the current scene.
  void InitializeAxesBoundaryPoints();

  // Description:
  // Initialize the "future box" transform.  This transform is a duplicate of
  // the Box transform, which dictates how the chart's axes should be drawn.
  // In ScaleUpAxes() and ScaleDownAxes(), we incrementally change the scaling
  // of the FutureBox transform to determine how much we need to zoom in or
  // zoom out to fit the chart within the newly resized scene.  Using a
  // separate transform for this process allows us to resize the Box in a
  // single step.
  void InitializeFutureBox();

  // Description:
  // Compute a bounding box for the data that is rendered within the axes.
  void ComputeDataBounds();

  // Description:
  // Draw the cube axes of this chart.
  void DrawAxes(vtkContext3D *context);

  // Description:
  // For each of the XYZ dimensions, find the axis line that is furthest
  // from the rendered data.
  void DetermineWhichAxesToLabel();

  // Description:
  // Draw tick marks and tick mark labels along the axes.
  void DrawTickMarks(vtkContext2D *painter);

  // Description:
  // Label the axes.
  void DrawAxesLabels(vtkContext2D *painter);

  // Description:
  // Compute how some text should be offset from an axis.  The parameter
  // bounds contains the bounding box of the text to be rendered.  The
  // result is stored in the parameter offset.
  void GetOffsetForAxisLabel(int axis, float *bounds, float *offset);

  // Description:
  // Calculate the next "nicest" numbers above and below the current minimum.
  // \return the "nice" spacing of the numbers.
  // This function was mostly copied from vtkAxis.
  double CalculateNiceMinMax(double &min, double &max, int axis);

268 269 270 271
  // Description:
  // Get the equation for the ith face of our bounding cube.
  void GetClippingPlaneEquation(int i, double *planeEquation);

Zack Galbreath's avatar
Zack Galbreath committed
272 273
  // Description:
  // The size and position of this chart.
274 275
  vtkRectf Geometry;

Zack Galbreath's avatar
Zack Galbreath committed
276 277
  // Description:
  // The 3 axes of this chart.
278
  std::vector< vtkSmartPointer<vtkAxis> > Axes;
Zack Galbreath's avatar
Zack Galbreath committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

  // Description:
  // This boolean indicates whether or not we're using this chart to rotate
  // on a timer.
  bool AutoRotate;

  // Description:
  // When we're in AutoRotate mode, this boolean tells us if we should rotate
  // about the X axis or the Y axis.
  bool IsX;

  // Description:
  // When we're in AutoRotate mode, this value tells the chart how much it
  // should be rotated.
  double Angle;

  // Description:
  // This boolean indicates whether or not we should draw tick marks
  // and axes labels.
  bool DrawAxesDecoration;

  // Description:
  // This boolean indicates whether or not we should automatically resize the
  // chart so that it snugly fills up the scene.
  bool FitToScene;

  // Description:
  // This is the transform that is applied when rendering data from the plots.
  vtkNew<vtkTransform> ContextTransform;

  // Description:
  // This transform translates and scales the plots' data points so that they
  // appear within the axes of this chart.  It is one of the factors that
  // makes up the ContextTransform.
  vtkNew<vtkTransform> PlotTransform;

  // Description:
  // This is the transform that is applied when rendering data from the plots.
  vtkNew<vtkTransform> Box;

  // Description:
  // This transform keeps track of how the chart has been rotated.
  vtkNew<vtkTransform> Rotation;

  // Description:
  // This transform keeps track of how the data points have been panned within
  // the chart.
  vtkNew<vtkTransform> Translation;

  // Description:
  // This transform keeps track of how the data points have been scaled
  // (zoomed in or zoomed out) within the chart.
  vtkNew<vtkTransform> Scale;

  // Description:
  // This transform keeps track of how the axes have been scaled
  // (zoomed in or zoomed out).
  vtkNew<vtkTransform> BoxScale;

  // Description:
  // This transform is initialized as a copy of Box.  It is used within
  // ScaleUpAxes() and ScaleDownAxes() to figure out how much we need to
  // zoom in or zoom out to fit our chart within the newly resized scene.
  vtkNew<vtkTransform> FutureBox;

  // Description:
  // This transform keeps track of the Scale of the FutureBox transform.
  vtkNew<vtkTransform> FutureBoxScale;

  // Description:
  // This is the pen that is used to draw data from the plots.
350
  vtkNew<vtkPen> Pen;
Zack Galbreath's avatar
Zack Galbreath committed
351 352 353

  // Description:
  // This is the pen that is used to draw the axes.
354
  vtkNew<vtkPen> AxisPen;
Zack Galbreath's avatar
Zack Galbreath committed
355 356 357

  // Description:
  // This link is used to share selected points with other classes.
358
  vtkSmartPointer<vtkAnnotationLink> Link;
359

Zack Galbreath's avatar
Zack Galbreath committed
360 361
  // Description:
  // The plots that are drawn within this chart.
362
  std::vector<vtkPlot3D *> Plots;
363

Zack Galbreath's avatar
Zack Galbreath committed
364 365 366
  // Description:
  // The label for the X Axis.
  std::string XAxisLabel;
367

Zack Galbreath's avatar
Zack Galbreath committed
368 369 370 371 372 373 374 375 376 377
  // Description:
  // The label for the Y Axis.
  std::string YAxisLabel;

  // Description:
  // The label for the Z Axis.
  std::string ZAxisLabel;

  // Description:
  // The six planes that define the bounding cube of our 3D axes.
378
  vtkNew<vtkPlaneCollection> BoundingCube;
Zack Galbreath's avatar
Zack Galbreath committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

  // Description:
  // Points used to determine whether the axes will fit within the scene as
  // currently sized, regardless of rotation.
  float AxesBoundaryPoints[14][3];

  // Description:
  // This member variable stores the size of the tick labels for each axis.
  // It is used to determine the position of the axis labels.
  float TickLabelOffset[3][2];

  // Description:
  // The height of the scene, as of the most recent call to Paint().
  int SceneHeight;

  // Description:
  // The weight of the scene, as of the most recent call to Paint().
  int SceneWidth;

  // Description:
  // Which line to label.
  int XAxisToLabel[3];
  int YAxisToLabel[3];
  int ZAxisToLabel[3];

  // Description:
  // What direction the data is from each labeled axis line.
  int DirectionToData[3];

  // Description:
  // A bounding box surrounding the currently rendered data points.
  double DataBounds[4];
411

412
private:
413
  vtkChartXYZ(const vtkChartXYZ &) VTK_DELETE_FUNCTION;
414
  void operator=(const vtkChartXYZ &) VTK_DELETE_FUNCTION;
415 416 417
};

#endif