From d031b0eac683038bb952bc9a0dca46d8a5d1760d Mon Sep 17 00:00:00 2001 From: Marcus Hanwell Date: Fri, 27 Nov 2009 13:17:56 -0500 Subject: [PATCH] ENH: Initial import of the work 2D API, 2D scene and charts code. This is an initial import of the surviving parts of the 2D drawing API I have been working on, along with a prototype for a 2D scene and some chart elements. This work is experimental, and parts of the API will be moved into Rendering. The API is subject to change and/or removal. --- CMakeLists.txt | 9 + Charts/CMakeLists.txt | 54 ++++ Charts/vtkAxis.cxx | 137 ++++++++++ Charts/vtkAxis.h | 107 ++++++++ Charts/vtkBlockItem.cxx | 172 ++++++++++++ Charts/vtkBlockItem.h | 112 ++++++++ Charts/vtkBrush.cxx | 128 +++++++++ Charts/vtkBrush.h | 105 ++++++++ Charts/vtkChart.cxx | 126 +++++++++ Charts/vtkChart.h | 117 ++++++++ Charts/vtkChartXY.cxx | 374 ++++++++++++++++++++++++++ Charts/vtkChartXY.h | 92 +++++++ Charts/vtkContext2D.cxx | 396 ++++++++++++++++++++++++++++ Charts/vtkContext2D.h | 188 +++++++++++++ Charts/vtkContextActor.cxx | 106 ++++++++ Charts/vtkContextActor.h | 75 ++++++ Charts/vtkContextDevice2D.cxx | 40 +++ Charts/vtkContextDevice2D.h | 140 ++++++++++ Charts/vtkContextItem.cxx | 98 +++++++ Charts/vtkContextItem.h | 102 +++++++ Charts/vtkContextMapper2D.cxx | 73 +++++ Charts/vtkContextMapper2D.h | 74 ++++++ Charts/vtkContextScene.cxx | 315 ++++++++++++++++++++++ Charts/vtkContextScene.h | 152 +++++++++++ Charts/vtkContextView.cxx | 111 ++++++++ Charts/vtkContextView.h | 75 ++++++ Charts/vtkImageItem.cxx | 191 ++++++++++++++ Charts/vtkImageItem.h | 121 +++++++++ Charts/vtkOpenGLContextDevice2D.cxx | 384 +++++++++++++++++++++++++++ Charts/vtkOpenGLContextDevice2D.h | 157 +++++++++++ Charts/vtkPen.cxx | 128 +++++++++ Charts/vtkPen.h | 114 ++++++++ Charts/vtkPlot.cxx | 110 ++++++++ Charts/vtkPlot.h | 111 ++++++++ Charts/vtkPlotGrid.cxx | 91 +++++++ Charts/vtkPlotGrid.h | 77 ++++++ Charts/vtkPlotLine.cxx | 205 ++++++++++++++ Charts/vtkPlotLine.h | 74 ++++++ Charts/vtkPlotPoints.cxx | 205 ++++++++++++++ Charts/vtkPlotPoints.h | 74 ++++++ Common/vtkWin32Header.h | 7 + vtkIncludeDirectories.cmake | 2 + 42 files changed, 5529 insertions(+) create mode 100644 Charts/CMakeLists.txt create mode 100644 Charts/vtkAxis.cxx create mode 100644 Charts/vtkAxis.h create mode 100644 Charts/vtkBlockItem.cxx create mode 100644 Charts/vtkBlockItem.h create mode 100644 Charts/vtkBrush.cxx create mode 100644 Charts/vtkBrush.h create mode 100644 Charts/vtkChart.cxx create mode 100644 Charts/vtkChart.h create mode 100644 Charts/vtkChartXY.cxx create mode 100644 Charts/vtkChartXY.h create mode 100644 Charts/vtkContext2D.cxx create mode 100644 Charts/vtkContext2D.h create mode 100644 Charts/vtkContextActor.cxx create mode 100644 Charts/vtkContextActor.h create mode 100644 Charts/vtkContextDevice2D.cxx create mode 100644 Charts/vtkContextDevice2D.h create mode 100644 Charts/vtkContextItem.cxx create mode 100644 Charts/vtkContextItem.h create mode 100644 Charts/vtkContextMapper2D.cxx create mode 100644 Charts/vtkContextMapper2D.h create mode 100644 Charts/vtkContextScene.cxx create mode 100644 Charts/vtkContextScene.h create mode 100644 Charts/vtkContextView.cxx create mode 100644 Charts/vtkContextView.h create mode 100644 Charts/vtkImageItem.cxx create mode 100644 Charts/vtkImageItem.h create mode 100644 Charts/vtkOpenGLContextDevice2D.cxx create mode 100644 Charts/vtkOpenGLContextDevice2D.h create mode 100644 Charts/vtkPen.cxx create mode 100644 Charts/vtkPen.h create mode 100644 Charts/vtkPlot.cxx create mode 100644 Charts/vtkPlot.h create mode 100644 Charts/vtkPlotGrid.cxx create mode 100644 Charts/vtkPlotGrid.h create mode 100644 Charts/vtkPlotLine.cxx create mode 100644 Charts/vtkPlotLine.h create mode 100644 Charts/vtkPlotPoints.cxx create mode 100644 Charts/vtkPlotPoints.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e6db623cd9..1305676414 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,6 +269,9 @@ VTK_DEPENDENT_OPTION(VTK_USE_CG_SHADERS "Build pixel and vertex shader support f VTK_DEPENDENT_OPTION(VTK_USE_GLSL_SHADERS "Build pixel and vertex shader support for GLSL." ON "VTK_USE_RENDERING" OFF) +VTK_DEPENDENT_OPTION(VTK_USE_CHARTS "Build VTK chart support (OpenGL based)" ON + "VTK_USE_RENDERING;VTK_USE_VIEWS" OFF) + SET(VTK_DEFAULT_SHADERS_DIR "${VTK_BINARY_DIR}/Utilities/MaterialLibrary/Repository" CACHE INTERNAL @@ -366,6 +369,9 @@ IF(VTK_USE_MFC) SET(VTK_KITS ${VTK_KITS} MFC) ENDIF(VTK_USE_MFC) +IF(VTK_USE_CHARTS) + SET(VTK_KITS ${VTK_KITS} CHARTS) +ENDIF(VTK_USE_CHARTS) #----------------------------------------------------------------------------- # Determine GUI. @@ -1150,6 +1156,9 @@ ENDIF(VTK_USE_VIEWS) IF(VTK_USE_GUISUPPORT) ADD_SUBDIRECTORY(GUISupport) ENDIF(VTK_USE_GUISUPPORT) +IF(VTK_USE_CHARTS) + ADD_SUBDIRECTORY(Charts) +ENDIF(VTK_USE_CHARTS) # Wrapping. IF(VTK_WRAP_TCL) diff --git a/Charts/CMakeLists.txt b/Charts/CMakeLists.txt new file mode 100644 index 0000000000..15451d636d --- /dev/null +++ b/Charts/CMakeLists.txt @@ -0,0 +1,54 @@ +SET(KIT Charts) +SET(UKIT CHARTS) + +SET(KIT_TCL_LIBS vtkHybridTCL vtkViewsTCL ${VTK_TK_LIBRARIES}) +SET(KIT_PYTHON_LIBS vtkHybridPythonD vtkViewsPythonD) +SET(KIT_JAVA_LIBS vtkHybridJava vtkViewsJava) + +IF (JAVA_AWT_LIBRARY) + SET(KIT_JAVA_LIBS ${KIT_JAVA_LIBS} ${JAVA_AWT_LIBRARY}) +ENDIF (JAVA_AWT_LIBRARY) + +SET(KIT_INTERFACE_LIBRARIES vtkHybrid vtkViews) +SET(KIT_LIBS vtkIO vtkftgl + ${VTK_FREETYPE_LIBRARIES} +) + +SET(Kit_SRCS + vtkAxis.cxx + vtkBlockItem.cxx + vtkBrush.cxx + vtkChart.cxx + vtkChartXY.cxx + vtkContext2D.cxx + vtkContextActor.cxx + vtkContextDevice2D.cxx + vtkContextItem.cxx + vtkContextMapper2D.cxx + vtkContextScene.cxx + vtkContextView.cxx + vtkImageItem.cxx + vtkOpenGLContextDevice2D.cxx + vtkPen.cxx + vtkPlot.cxx + vtkPlotGrid.cxx + vtkPlotLine.cxx + vtkPlotPoints.cxx + ) + +SET_SOURCE_FILES_PROPERTIES( + vtkChart + vtkContextDevice2D + vtkContextItem + vtkContextMapper2D + vtkPlot + ABSTRACT + ) + +#----------------------------------------------------------------------------- +# Include CMake code common to all kits. +INCLUDE(${VTK_CMAKE_DIR}/KitCommonBlock.cmake) +#----------------------------------------------------------------------------- + +#TARGET_LINK_LIBRARIES(vtkCharts ${QT_LIBRARIES}) + diff --git a/Charts/vtkAxis.cxx b/Charts/vtkAxis.cxx new file mode 100644 index 0000000000..9bb165a9c5 --- /dev/null +++ b/Charts/vtkAxis.cxx @@ -0,0 +1,137 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkAxis.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 "vtkAxis.h" + +#include "vtkContext2D.h" +#include "vtkPen.h" +#include "vtkTextProperty.h" + +#include "vtkObjectFactory.h" + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkAxis, "1.1"); + +//----------------------------------------------------------------------------- +vtkStandardNewMacro(vtkAxis); + +//----------------------------------------------------------------------------- +vtkAxis::vtkAxis() +{ + this->Point1[0] = 10.0; + this->Point1[1] = 10.0; + this->Point2[0] = 10.0; + this->Point2[1] = 10.0; + this->NumberOfTicks = 6; + this->Minimum = 0.0; + this->Maximum = 6.66; + this->Label = NULL; +} + +//----------------------------------------------------------------------------- +vtkAxis::~vtkAxis() +{ + this->SetLabel(NULL); +} + +//----------------------------------------------------------------------------- +bool vtkAxis::Paint(vtkContext2D *painter) +{ + // This is where everything should be drawn, or dispatched to other methods. + vtkDebugMacro(<< "Paint event called in vtkAxis."); + + painter->GetPen()->SetWidth(1.0); + // Draw this axis + painter->DrawLine(this->Point1[0], this->Point1[1], + this->Point2[0], this->Point2[1]); + + // Draw the axis label + if (this->Point1[0] == this->Point2[0]) // x1 == x2, therefore vertical + { + // Draw the axis label + int x = this->Point1[0] - 45; + int y = (this->Point1[1] + this->Point2[1]) / 2; + vtkTextProperty *prop = painter->GetTextProp(); + prop->SetFontSize(15); + prop->SetFontFamilyAsString("Arial"); + prop->SetColor(0.0, 0.0, 0.0); + prop->SetOrientation(90.0); + prop->SetJustificationToCentered(); + prop->SetVerticalJustificationToBottom(); + painter->DrawText(x, y, this->Label); + + // Now draw the tick marks + float spacing = (this->Point2[1] - this->Point1[1]) / + float(this->NumberOfTicks - 1); + prop->SetOrientation(0.0); + prop->SetJustificationToRight(); + prop->SetVerticalJustificationToCentered(); + float labelSpacing = (this->Maximum - this->Minimum) / + float(this->NumberOfTicks -1); + for (int i = 0; i < this->NumberOfTicks; ++i) + { + int yTick = this->Point1[1] + float(i)*spacing; + painter->DrawLine(this->Point1[0] - 5, yTick, this->Point1[0], yTick); + + // Draw the tick label + char string[20]; + sprintf(string, "%-#6.3g", this->Minimum + i*labelSpacing); + painter->DrawText(this->Point1[0] - 5, yTick, string); + } + } + else // Default to horizontal orientation + { + int x = (this->Point1[0] + this->Point2[0]) / 2; + int y = this->Point1[1] - 30; + vtkTextProperty *prop = painter->GetTextProp(); + prop->SetFontSize(15); + prop->SetFontFamilyAsString("Arial"); + prop->SetColor(0.0, 0.0, 0.0); + prop->SetJustificationToCentered(); + prop->SetVerticalJustificationToTop(); + painter->DrawText(x, y, "X Axis"); + + // Now draw the tick marks + float spacing = (this->Point2[0] - this->Point1[0]) / + float(this->NumberOfTicks - 1); + prop->SetJustificationToCentered(); + prop->SetVerticalJustificationToTop(); + float labelSpacing = (this->Maximum - this->Minimum) / + float(this->NumberOfTicks -1); + for (int i = 0; i < this->NumberOfTicks; ++i) + { + int xTick = this->Point1[1] + float(i)*spacing; + painter->DrawLine(xTick, this->Point1[1] - 5, xTick, this->Point1[1]); + + char string[20]; + sprintf(string, "%-#6.3g", this->Minimum + i*labelSpacing); + painter->DrawText(xTick, this->Point1[1] - 5, string); + } + } +} + +//----------------------------------------------------------------------------- +void vtkAxis::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + os << indent << "Axis label: \"" << *this->Label << "\"" << endl; + os << indent << "Minimum point: " << this->Point1[0] << ", " + << this->Point1[1] << endl; + os << indent << "Maximum point: " << this->Point2[0] << ", " + << this->Point2[1] << endl; + os << indent << "Range: " << this->Minimum << " - " << this->Maximum << endl; + os << indent << "Number of tick marks: " << this->NumberOfTicks << endl; + +} diff --git a/Charts/vtkAxis.h b/Charts/vtkAxis.h new file mode 100644 index 0000000000..439ae7eb74 --- /dev/null +++ b/Charts/vtkAxis.h @@ -0,0 +1,107 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkAxis.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 vtkAxis - takes care of drawing 2D axes +// +// .SECTION Description +// The vtkAxis is drawn in screen coordinates. It is usually one of the last +// elements of a chart to be drawn. It renders the axis label, tick marks and +// tick labels. + +#ifndef __vtkAxis_h +#define __vtkAxis_h + +#include "vtkContextItem.h" + +class vtkContext2D; + +class VTK_CHARTS_EXPORT vtkAxis : public vtkContextItem +{ +public: + vtkTypeRevisionMacro(vtkAxis, vtkContextItem); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Creates a 2D Chart object. + static vtkAxis *New(); + + // Description: + // Set point 1 of the axis (in pixels), this is usually the origin. + vtkSetVector2Macro(Point1, float); + + // Description: + // Get point 1 of the axis (in pixels), this is usually the origin. + vtkGetVector2Macro(Point1, float); + + // Description: + // Set point 2 of the axis (in pixels), this is usually the terminus. + vtkSetVector2Macro(Point2, float); + + // Description: + // Get point 2 of the axis (in pixels), this is usually the terminus. + vtkGetVector2Macro(Point2, float); + + // Description: + // Set the number of tick marks for this axis. + vtkSetMacro(NumberOfTicks, int); + + // Description: + // Get the number of tick marks for this axis. + vtkGetMacro(NumberOfTicks, int); + + // Description: + // Set the logical minimum value of the axis, in plot coordinates. + vtkSetMacro(Minimum, float); + + // Description: + // Get the logical minimum value of the axis, in plot coordinates. + vtkGetMacro(Minimum, float); + + // Description: + // Set the logical maximum value of the axis, in plot coordinates. + vtkSetMacro(Maximum, float); + vtkGetMacro(Maximum, float); + + // Description: + // Get/set the label text for the axis, in plot coordinates. + vtkSetStringMacro(Label); + + // Description: + // Get/set the label text for the axis, in plot coordinates. + vtkGetStringMacro(Label); + + // Description: + // Paint event for the axis, called whenever the axis needs to be drawn. + virtual bool Paint(vtkContext2D *painter); + +//BTX +protected: + vtkAxis(); + ~vtkAxis(); + + float Point1[2]; // The position of point 1 (usually the origin) + float Point2[2]; // The position of point 2 (usually the terminus) + int NumberOfTicks; // The number of tick marks to draw + float Minimum; // Minimum value of the axis + float Maximum; // Maximum values of the axis + char *Label; // The text label drawn on the axis + +private: + vtkAxis(const vtkAxis &); // Not implemented. + void operator=(const vtkAxis &); // Not implemented. +//ETX +}; + +#endif //__vtkAxis_h diff --git a/Charts/vtkBlockItem.cxx b/Charts/vtkBlockItem.cxx new file mode 100644 index 0000000000..d1eeb25150 --- /dev/null +++ b/Charts/vtkBlockItem.cxx @@ -0,0 +1,172 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkBlockItem.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 "vtkBlockItem.h" + +// Get my new commands +#include "vtkCommand.h" + +#include "vtkContext2D.h" +#include "vtkContextScene.h" +#include "vtkPen.h" +#include "vtkBrush.h" +#include "vtkTextProperty.h" + +#include "vtkObjectFactory.h" + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkBlockItem, "1.1"); +vtkStandardNewMacro(vtkBlockItem); + +//----------------------------------------------------------------------------- +vtkBlockItem::vtkBlockItem() +{ + this->Label = NULL; + this->MouseOver = false; + this->MouseButtonPressed = -1; + this->scalarFunction = NULL; +} + +//----------------------------------------------------------------------------- +vtkBlockItem::~vtkBlockItem() +{ + this->SetLabel(NULL); +} + +//----------------------------------------------------------------------------- +bool vtkBlockItem::Paint(vtkContext2D *painter) +{ + painter->GetTextProp()->SetVerticalJustificationToCentered(); + painter->GetTextProp()->SetJustificationToCentered(); + painter->GetTextProp()->SetColor(0.0, 0.0, 0.0); + painter->GetTextProp()->SetFontSize(24); + painter->GetPen()->SetColor(0, 0, 0); + + if (this->MouseOver) + { + painter->GetBrush()->SetColor(255, 0, 0); + } + else + { + painter->GetBrush()->SetColor(0, 255, 0); + } + painter->DrawRect(this->Dimensions[0], this->Dimensions[1], + this->Dimensions[2], this->Dimensions[3]); + + int x = this->Dimensions[0] + 0.5 * this->Dimensions[2]; + int y = this->Dimensions[1] + 0.5 * this->Dimensions[3]; + painter->DrawText(x, y, this->Label); + + if (this->scalarFunction) + { + // We have a function pointer - do something... + ; + } +} + +//----------------------------------------------------------------------------- +bool vtkBlockItem::Hit(const vtkContextMouseEvent &mouse) +{ + if (mouse.Pos[0] > this->Dimensions[0] && + mouse.Pos[0] < this->Dimensions[0]+this->Dimensions[2] && + mouse.Pos[1] > this->Dimensions[1] && + mouse.Pos[1] < this->Dimensions[1]+this->Dimensions[3]) + { + return true; + } + else + { + return false; + } + return false; +} + +//----------------------------------------------------------------------------- +bool vtkBlockItem::MouseEnterEvent(const vtkContextMouseEvent &mouse) +{ + this->MouseOver = true; + return true; +} + +//----------------------------------------------------------------------------- +bool vtkBlockItem::MouseMoveEvent(const vtkContextMouseEvent &mouse) +{ + int deltaX = mouse.Pos[0] - this->LastPosition[0]; + int deltaY = mouse.Pos[1] - this->LastPosition[1]; + this->LastPosition[0] = mouse.Pos[0]; + this->LastPosition[1] = mouse.Pos[1]; + + if (this->MouseButtonPressed == 0) + { + // Move the block by this amount + this->Dimensions[0] += deltaX; + this->Dimensions[1] += deltaY; + + return true; + } + else if (this->MouseButtonPressed == 1) + { + // Resize the block by this amount + this->Dimensions[0] += deltaX; + this->Dimensions[1] += deltaY; + this->Dimensions[2] -= deltaX; + this->Dimensions[3] -= deltaY; + + return true; + } + else if (this->MouseButtonPressed == 2) + { + // Resize the block by this amount + this->Dimensions[2] += deltaX; + this->Dimensions[3] += deltaY; + + return true; + } + return false; +} + +//----------------------------------------------------------------------------- +bool vtkBlockItem::MouseLeaveEvent(const vtkContextMouseEvent &mouse) +{ + this->MouseOver = false; + return true; +} + +//----------------------------------------------------------------------------- +bool vtkBlockItem::MouseButtonPressEvent(const vtkContextMouseEvent &mouse) +{ + this->MouseButtonPressed = mouse.Button; + this->LastPosition[0] = mouse.Pos[0]; + this->LastPosition[1] = mouse.Pos[1]; + return true; +} + +//----------------------------------------------------------------------------- +bool vtkBlockItem::MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse) +{ + this->MouseButtonPressed = -1; + return true; +} + +void vtkBlockItem::SetScalarFunctor(double (*scalarFunction)(double, double)) +{ + this->scalarFunction = scalarFunction; +} + +//----------------------------------------------------------------------------- +void vtkBlockItem::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); +} diff --git a/Charts/vtkBlockItem.h b/Charts/vtkBlockItem.h new file mode 100644 index 0000000000..9eee64b697 --- /dev/null +++ b/Charts/vtkBlockItem.h @@ -0,0 +1,112 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkBlockItem.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 vtkBlockItem - a vtkContextItem that draws a block (optional label). +// +// .SECTION Description +// This is a vtkContextItem that can be placed into a vtkContextScene. It draws +// a block of the given dimensions, and reacts to mouse events. + +#ifndef __vtkBlockItem_h +#define __vtkBlockItem_h + +#include "vtkContextItem.h" + +class vtkContext2D; + +class VTK_CHARTS_EXPORT vtkBlockItem : public vtkContextItem +{ +public: + vtkTypeRevisionMacro(vtkBlockItem, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + static vtkBlockItem *New(); + + // Description: + // Paint event for the item. + virtual bool Paint(vtkContext2D *painter); + +//BTX + // Description: + // Returns true if the supplied x, y coordinate is inside the item. + virtual bool Hit(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse enter event. + virtual bool MouseEnterEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse move event. + virtual bool MouseMoveEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse leave event. + virtual bool MouseLeaveEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse button down event. + virtual bool MouseButtonPressEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse button release event. + virtual bool MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse); +//ETX + + // Description: + // Set the block label. + vtkSetStringMacro(Label); + + // Description: + // Get the block label. + vtkGetStringMacro(Label); + + // Description: + // Set the dimensions of the block, elements 0 and 1 are the x and y coordinate + // of the bottom corner. Elements 2 and 3 are the width and height. + vtkSetVector4Macro(Dimensions, int); + + // Description: + // Get the dimensions of the block, elements 0 and 1 are the x and y coordinate + // of the bottom corner. Elements 2 and 3 are the width and height. + vtkGetVector4Macro(Dimensions, int); + +//BTX + void SetScalarFunctor(double (*scalarFunction)(double, double)); +//ETX + +//BTX +protected: + vtkBlockItem(); + ~vtkBlockItem(); + + int Dimensions[4]; + + int LastPosition[2]; + + char *Label; + + bool MouseOver; + int MouseButtonPressed; + + // Some function pointers to optionally do funky things... + double (*scalarFunction)(double, double); + +private: + vtkBlockItem(const vtkBlockItem &); // Not implemented. + void operator=(const vtkBlockItem &); // Not implemented. +//ETX +}; + +#endif //__vtkBlockItem_h diff --git a/Charts/vtkBrush.cxx b/Charts/vtkBrush.cxx new file mode 100644 index 0000000000..f1991859db --- /dev/null +++ b/Charts/vtkBrush.cxx @@ -0,0 +1,128 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkBrush.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 "vtkBrush.h" + +#include "vtkObjectFactory.h" + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkBrush, "1.1"); + +//----------------------------------------------------------------------------- +vtkStandardNewMacro(vtkBrush); + +//----------------------------------------------------------------------------- +vtkBrush::vtkBrush() +{ + this->Color[0] = 0; + this->Color[1] = 0; + this->Color[2] = 0; + this->Color[3] = 255; +} + +//----------------------------------------------------------------------------- +vtkBrush::~vtkBrush() +{ +} + +//----------------------------------------------------------------------------- +void vtkBrush::SetColorF(double color[3]) +{ + this->Color[0] = color[0] * 255.0; + this->Color[1] = color[1] * 255.0; + this->Color[2] = color[2] * 255.0; +} + +//----------------------------------------------------------------------------- +void vtkBrush::SetColorF(double r, double g, double b) +{ + this->Color[0] = r * 255.0; + this->Color[1] = g * 255.0; + this->Color[2] = b * 255.0; +} + +//----------------------------------------------------------------------------- +void vtkBrush::SetColorF(double r, double g, double b, double a) +{ + this->Color[0] = r * 255.0; + this->Color[1] = g * 255.0; + this->Color[2] = b * 255.0; + this->Color[3] = a * 255.0; +} + +//----------------------------------------------------------------------------- +void vtkBrush::SetOpacityF(double a) +{ + this->Color[3] = a * 255.0; +} + +//----------------------------------------------------------------------------- +void vtkBrush::SetColor(unsigned char color[3]) +{ + this->Color[0] = color[0]; + this->Color[1] = color[1]; + this->Color[2] = color[2]; +} + +//----------------------------------------------------------------------------- +void vtkBrush::SetColor(unsigned char r, unsigned char g, unsigned char b) +{ + this->Color[0] = r; + this->Color[1] = g; + this->Color[2] = b; +} + +//----------------------------------------------------------------------------- +void vtkBrush::SetColor(unsigned char r, unsigned char g, unsigned char b, + unsigned char a) +{ + this->Color[0] = r; + this->Color[1] = g; + this->Color[2] = b; + this->Color[3] = a; +} + +//----------------------------------------------------------------------------- +void vtkBrush::SetOpacity(unsigned char a) +{ + this->Color[3] = a; +} + +//----------------------------------------------------------------------------- +void vtkBrush::GetColorF(double color[4]) +{ + for (int i = 0; i < 4; ++i) + { + color[i] = this->Color[i] / 255.0; + } +} + +//----------------------------------------------------------------------------- +void vtkBrush::GetColor(unsigned char color[4]) +{ + for (int i = 0; i < 4; ++i) + { + color[i] = this->Color[i]; + } +} + +//----------------------------------------------------------------------------- +void vtkBrush::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + os << indent << "Color: " << this->Color[0] << ", " << this->Color[1] + << ", " << this->Color[2] << ", " << this->Color[3] << endl; + +} diff --git a/Charts/vtkBrush.h b/Charts/vtkBrush.h new file mode 100644 index 0000000000..ade1cad9d3 --- /dev/null +++ b/Charts/vtkBrush.h @@ -0,0 +1,105 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkBrush.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 vtkBrush - provides a brush that fills shapes drawn by vtkContext2D. +// +// .SECTION Description +// The vtkBrush defines the fill (or pattern) of shapes that are drawn by +// vtkContext2D. The color is stored as four unsigned chars (RGBA), where the +// opacity defaults to 255, but can be modified separately to the other +// components. Ideally we would use a lightweight color class to store and pass +// around colors. + +#ifndef __vtkBrush_h +#define __vtkBrush_h + +#include "vtkObject.h" + +class VTK_CHARTS_EXPORT vtkBrush : public vtkObject +{ +public: + vtkTypeRevisionMacro(vtkBrush, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + static vtkBrush *New(); + + // Description: + // Set the color of the brush with three component doubles (RGB), ranging from + // 0.0 to 1.0. + void SetColorF(double color[3]); + + // Description: + // Set the color of the brush with three component doubles (RGB), ranging from + // 0.0 to 1.0. + void SetColorF(double r, double g, double b); + + // Description: + // Set the color of the brush with four component doubles (RGBA), ranging from + // 0.0 to 1.0. + void SetColorF(double r, double g, double b, double a); + + // Description: + // Set the opacity with a double, ranging from 0.0 (transparent) to 1.0 + // (opaque). + void SetOpacityF(double a); + + // Description: + // Set the color of the brush with three component unsigned chars (RGB), + // ranging from 0 to 255. + void SetColor(unsigned char color[3]); + + // Description: + // Set the color of the brush with three component unsigned chars (RGB), + // ranging from 0 to 255. + void SetColor(unsigned char r, unsigned char g, unsigned char b); + + // Description: + // Set the color of the brush with four component unsigned chars (RGBA), + // ranging from 0 to 255. + void SetColor(unsigned char r, unsigned char g, unsigned char b, + unsigned char a); + + // Description: + // Set the opacity with an unsigned char, ranging from 0 (transparent) to 255 + // (opaque). + void SetOpacity(unsigned char a); + + // Description: + // Get the color of the brush - expects a double of length 4 to copy into. + void GetColorF(double color[4]); + + // Description: + // Get the color of the brush - expects an unsigned char of length 4. + void GetColor(unsigned char color[4]); + + // Description: + // Get the color of the brush - gives a pointer to the underlying data. + unsigned char * GetColor() { return &this->Color[0]; } + +//BTX +protected: + vtkBrush(); + ~vtkBrush(); + + // Storage of the color in RGBA format (0-255 per channel). + unsigned char Color[4]; + +private: + vtkBrush(const vtkBrush &); // Not implemented. + void operator=(const vtkBrush &); // Not implemented. +//ETX +}; + +#endif //__vtkBrush_h diff --git a/Charts/vtkChart.cxx b/Charts/vtkChart.cxx new file mode 100644 index 0000000000..0e6720bd51 --- /dev/null +++ b/Charts/vtkChart.cxx @@ -0,0 +1,126 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkChart.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 "vtkChart.h" + +// Get my new commands +#include "vtkCommand.h" + +#include "vtkAnnotationLink.h" +#include "vtkContextScene.h" +#include "vtkInteractorStyle.h" +#include "vtkInteractorStyleRubberBand2D.h" +#include "vtkObjectFactory.h" + +//----------------------------------------------------------------------------- +// Minimal command class to handle callbacks. +class vtkChart::Command : public vtkCommand +{ +public: + static Command* New() { return new Command(); } + virtual void Execute(vtkObject *caller, unsigned long eventId, + void *callData) + { + if (this->Target) + { + switch (eventId) + { + case vtkCommand::SelectionChangedEvent : + this->Target->ProcessSelectionEvent(caller, callData); + break; + default: + this->Target->ProcessEvents(caller, eventId, callData); + } + } + } + + void SetTarget(vtkChart* t) { this->Target = t; } + +private: + Command() { this->Target = 0; } + vtkChart *Target; +}; + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkChart, "1.1"); +vtkCxxSetObjectMacro(vtkChart, AnnotationLink, vtkAnnotationLink); + +//----------------------------------------------------------------------------- +vtkChart::vtkChart() +{ + this->Observer = vtkChart::Command::New(); + this->Observer->SetTarget(this); + this->AnnotationLink = NULL; +} + +//----------------------------------------------------------------------------- +vtkChart::~vtkChart() +{ + this->Observer->Delete(); +} + +//----------------------------------------------------------------------------- +vtkPlot * vtkChart::AddPlot(Type type) +{ + return NULL; +} + +//----------------------------------------------------------------------------- +vtkIdType vtkChart::GetNumberPlots() +{ + return 0; +} + +//----------------------------------------------------------------------------- +void vtkChart::AddInteractorStyle(vtkInteractorStyle *interactor) +{ + interactor->AddObserver(vtkCommand::SelectionChangedEvent, this->Observer); +} + +//----------------------------------------------------------------------------- +void vtkChart::ProcessEvents(vtkObject* caller, unsigned long eventId, + void* callData) +{ + cout << "ProcessEvents called! " << caller->GetClassName() << "\t" + << vtkCommand::GetStringFromEventId(eventId) + << "\n\t" << vtkInteractorStyleRubberBand2D::SafeDownCast(caller)->GetInteraction() << endl; + return; +} + +//----------------------------------------------------------------------------- +void vtkChart::ProcessSelectionEvent(vtkObject* caller, void* callData) +{ + cout << "ProcessSelectionEvent called! " << caller << "\t" << callData << endl; + unsigned int *rect = reinterpret_cast(callData); + cout << "Rect:"; + for (int i = 0; i < 5; ++i) + { + cout << "\t" << rect[i]; + } + cout << endl; +} + +//----------------------------------------------------------------------------- +void vtkChart::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + // Print out the chart's geometry if it has been set + os << indent << "Origin: " << this->Geometry[0] << "\t" << this->Geometry[1] + << endl; + os << indent << "Width: " << this->Geometry[2] << endl + << indent << "Height: " << this->Geometry[3] << endl; + os << indent << "Right border: " << this->Geometry[4] << endl + << indent << "Top border: " << this->Geometry[5] << endl; +} diff --git a/Charts/vtkChart.h b/Charts/vtkChart.h new file mode 100644 index 0000000000..50d6f81f45 --- /dev/null +++ b/Charts/vtkChart.h @@ -0,0 +1,117 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkChart.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 vtkChart - Factory class for drawing 2D charts +// +// .SECTION Description +// This defines the interface for a chart. + +#ifndef __vtkChart_h +#define __vtkChart_h + +#include "vtkContextItem.h" + +class vtkContext2D; +class vtkContextScene; +class vtkPlot; + +class vtkInteractorStyle; +class vtkAnnotationLink; +class vtkTable; + +class VTK_CHARTS_EXPORT vtkChart : public vtkContextItem +{ +public: + vtkTypeRevisionMacro(vtkChart, vtkContextItem); + virtual void PrintSelf(ostream &os, vtkIndent indent); + +//BTX + // Description: + // Enum of the available chart types + enum Type { + LINE, + POINTS, + BAR, + STACKED}; +//ETX + + // Description: + // Paint event for the chart, called whenever the chart needs to be drawn + virtual bool Paint(vtkContext2D *painter) = 0; + + // Description: + // Add a plot to the chart, defaults to using the name of the y column + virtual vtkPlot * AddPlot(Type type); + + // Description: + // Get the number of plots the chart contains. + virtual vtkIdType GetNumberPlots(); + + // Description: + // Set the vtkAnnotationLink for the chart. + virtual void SetAnnotationLink(vtkAnnotationLink *link); + + // Description: + // Get the vtkAnnotationLink for the chart. + vtkGetObjectMacro(AnnotationLink, vtkAnnotationLink); + + // Description: + // This function allows you to set the overall dimensions of the chart. + // An int pointer of length 6 is expected with the dimensions in the order of + // width, height, left border, bottom border, right border, top border in + // pixels of the device. + vtkSetVector6Macro(Geometry, int); + + // Description: + // Add the chart as an observer on the supplied interaction style. + void AddInteractorStyle(vtkInteractorStyle *interactor); + +//BTX +protected: + vtkChart(); + ~vtkChart(); + + // Description: + // Called to process events. + virtual void ProcessEvents(vtkObject* caller, unsigned long eventId, + void* callData); + + // Description: + // Process a rubber band selection event. + virtual void ProcessSelectionEvent(vtkObject* caller, void* callData); + + // Description: + // Our annotation link, used for sharing selections etc. + vtkAnnotationLink *AnnotationLink; + + // Store the chart dimensions packed into a vtkPoints2D + // [0] = width, height of chart in screen coordinates + // [1] = left border, bottom border (roughly - origin of the chart + // [2] = right border, top border (offset from top right most point) + int Geometry[6]; + + // Description: + // The command object for the charts. + class Command; + friend class Command; + Command *Observer; + +private: + vtkChart(const vtkChart &); // Not implemented. + void operator=(const vtkChart &); // Not implemented. +//ETX +}; + +#endif //__vtkChart_h diff --git a/Charts/vtkChartXY.cxx b/Charts/vtkChartXY.cxx new file mode 100644 index 0000000000..f16237a0e7 --- /dev/null +++ b/Charts/vtkChartXY.cxx @@ -0,0 +1,374 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkChartXY.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 "vtkChartXY.h" + +#include "vtkContext2D.h" +#include "vtkPen.h" +#include "vtkBrush.h" +#include "vtkContextDevice2D.h" +#include "vtkTransform2D.h" +#include "vtkContextScene.h" +#include "vtkPoints2D.h" + +#include "vtkPlot.h" +#include "vtkPlotLine.h" +#include "vtkPlotPoints.h" + +#include "vtkAxis.h" +#include "vtkPlotGrid.h" + +#include "vtkTable.h" +#include "vtkAbstractArray.h" +#include "vtkFloatArray.h" +#include "vtkIntArray.h" +#include "vtkIdTypeArray.h" + +#include "vtkAnnotationLink.h" +#include "vtkSelection.h" +#include "vtkSelectionNode.h" + +#include "vtkObjectFactory.h" + +#include "vtkStdString.h" +#include "vtkTextProperty.h" + +// My STL containers +#include + +//----------------------------------------------------------------------------- +class vtkChartXYPrivate +{ + public: + vtkstd::vector plots; // Charts can contain multiple plots of data +}; + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkChartXY, "1.1"); + +//----------------------------------------------------------------------------- +vtkStandardNewMacro(vtkChartXY); + +//----------------------------------------------------------------------------- +vtkChartXY::vtkChartXY() +{ + this->XAxis = vtkAxis::New(); + this->YAxis = vtkAxis::New(); + this->Grid = vtkPlotGrid::New(); + this->Grid->SetXAxis(this->XAxis); + this->Grid->SetYAxis(this->YAxis); + this->ChartPrivate = new vtkChartXYPrivate; + + // Set up the x and y axes - should be congigured based on data + this->XAxis->SetMinimum(0.0); + this->XAxis->SetMaximum(7.0); + this->XAxis->SetNumberOfTicks(8); + this->XAxis->SetLabel("X Axis"); + this->YAxis->SetMinimum(1.0); + this->YAxis->SetMaximum(-1.0); + this->YAxis->SetNumberOfTicks(5); + this->YAxis->SetLabel("Y Axis"); +} + +//----------------------------------------------------------------------------- +vtkChartXY::~vtkChartXY() +{ + for (int i = 0; i < this->ChartPrivate->plots.size(); ++i) + { + this->ChartPrivate->plots[i]->Delete(); + } + delete this->ChartPrivate; + this->ChartPrivate = 0; + + this->XAxis->Delete(); + this->XAxis = 0; + this->YAxis->Delete(); + this->YAxis = 0; + this->Grid->Delete(); + this->Grid = 0; +} + +//----------------------------------------------------------------------------- +void vtkChartXY::SetGeometry(int *p) +{ + vtkChart::SetGeometry(p); + + // This is where we set the axes up too + float p1[] = { p[2], p[3], p[0]-p[4], p[3] }; + float p2[] = { p[2], p[3], p[2], p[1]-p[5] }; + this->XAxis->SetPoint1(p[2] , p[3] ); + this->XAxis->SetPoint2(p[0]-p[4], p[3] ); + this->YAxis->SetPoint1(p[2] , p[3] ); + this->YAxis->SetPoint2(p[2] , p[1]-p[5]); +} + +//----------------------------------------------------------------------------- +bool vtkChartXY::Paint(vtkContext2D *painter) +{ + // This is where everything should be drawn, or dispatched to other methods. + vtkDebugMacro(<< "Paint event called."); + + vtkIdTypeArray *idArray = 0; + if (this->AnnotationLink) + { + this->AnnotationLink->Update(); + vtkSelection *selection = vtkSelection::SafeDownCast(this->AnnotationLink->GetOutputDataObject(2)); + if (selection->GetNumberOfNodes()) + { + vtkSelectionNode *node = selection->GetNode(0); + idArray = vtkIdTypeArray::SafeDownCast(node->GetSelectionList()); + } + } + else + { + vtkDebugMacro("No annotation link set."); + } + + // This method could be optimized if only certain regions needed painting. + + // Draw a hard wired grid right now - this should be configurable + painter->GetPen()->SetColorF(0.8, 0.8, 0.8); + painter->GetPen()->SetWidth(1.0); + this->Grid->Paint(painter); + + // The origin of the plot area + float xOrigin = this->Geometry[2]; + float yOrigin = this->Geometry[3]; + + // Get the scale for the plot area from the x and y axes + float *min = this->XAxis->GetPoint1(); + float *max = this->XAxis->GetPoint2(); + float xScale = (this->XAxis->GetMaximum() - this->XAxis->GetMinimum()) / + (max[0] - min[0]); + min = this->YAxis->GetPoint1(); + max = this->YAxis->GetPoint2(); + float yScale = (this->YAxis->GetMaximum() - this->YAxis->GetMinimum()) / + (max[1] - min[1]); + + // Set up the scaling for the plot area + float plot[4]; + plot[0] = this->XAxis->GetMinimum() - xOrigin * xScale; + plot[2] = this->XAxis->GetMaximum() + this->Geometry[4] * xScale; + plot[1] = this->YAxis->GetMinimum() - yOrigin * yScale; + plot[3] = this->YAxis->GetMaximum() + this->Geometry[5] * yScale; + painter->GetDevice()->PushMatrix(); + painter->GetDevice()->SetViewExtents(&plot[0]); + + // Clip drawing while plotting + int clip[4]; + clip[0] = xOrigin; + clip[1] = yOrigin; + clip[2] = this->Geometry[0] - this->Geometry[4] - this->Geometry[2]; + clip[3] = this->Geometry[1] - this->Geometry[5] - this->Geometry[3]; + painter->GetDevice()->SetClipping(&clip[0]); + + // Now iterate through the plots + int n = this->ChartPrivate->plots.size(); + for (int i = 0; i < n; ++i) + { + this->ChartPrivate->plots[i]->SetSelection(idArray); + this->ChartPrivate->plots[i]->Paint(painter); + } + + // Stop clipping and reset back to screen coordinates + painter->GetDevice()->DisableClipping(); + painter->GetDevice()->PopMatrix(); + + // Set the color and width, draw the axes, color and width push to axis props + painter->GetPen()->SetColorF(0.0, 0.0, 0.0, 1.0); + painter->GetPen()->SetWidth(1.0); + this->XAxis->Paint(painter); + this->YAxis->Paint(painter); +} + +//----------------------------------------------------------------------------- +void vtkChartXY::RenderPlots(vtkContext2D *painter) +{ + // This function ensures that the correct view transforms are in place for + // the plot functions before calling paint on each one. + float x[] = { -0.1, -0.1, 10.0, 2.1 }; + painter->GetDevice()->SetViewExtents(&x[0]); +} + +//----------------------------------------------------------------------------- +vtkPlot * vtkChartXY::AddPlot(vtkChart::Type type) +{ + switch (type) + { + case LINE: + { + vtkPlotLine *line = vtkPlotLine::New(); + this->ChartPrivate->plots.push_back(line); + + // Set up the scaling for the plot area + line->Translate(0.0, 0.0); + line->GetTransform()->Translate(this->Geometry[2], + this->Geometry[3]); + line->GetTransform()->Print(cout); + // Get the scale for the plot area from the x and y axes + float *min = this->XAxis->GetPoint1(); + float *max = this->XAxis->GetPoint2(); + float xScale = (this->XAxis->GetMaximum() - this->XAxis->GetMinimum()) / + (max[0] - min[0]); + min = this->YAxis->GetPoint1(); + max = this->YAxis->GetPoint2(); + float yScale = (this->YAxis->GetMaximum() - this->YAxis->GetMinimum()) / + (max[1] - min[1]); + line->GetTransform()->Scale(1.0 / xScale, 1.0 / yScale); + line->GetTransform()->Translate(- this->XAxis->GetMinimum(), + - this->YAxis->GetMinimum()); + + line->GetTransform()->Print(cout); + +// this->Scene->AddItem(line); + return line; + break; + } + case POINTS: + { + vtkPlotPoints *points = vtkPlotPoints::New(); + this->ChartPrivate->plots.push_back(points); + + // Set up the scaling for the plot area + points->Translate(0.0, 0.0); + points->GetTransform()->Translate(this->Geometry[2], + this->Geometry[3]); + // Get the scale for the plot area from the x and y axes + float *min = this->XAxis->GetPoint1(); + float *max = this->XAxis->GetPoint2(); + float xScale = (this->XAxis->GetMaximum() - this->XAxis->GetMinimum()) / + (max[0] - min[0]); + min = this->YAxis->GetPoint1(); + max = this->YAxis->GetPoint2(); + float yScale = (this->YAxis->GetMaximum() - this->YAxis->GetMinimum()) / + (max[1] - min[1]); + points->GetTransform()->Scale(1.0 / xScale, 1.0 / yScale); + points->GetTransform()->Translate(- this->XAxis->GetMinimum(), + - this->YAxis->GetMinimum()); + +// this->Scene->AddItem(points); + return points; + break; + } + } +} + +//----------------------------------------------------------------------------- +vtkIdType vtkChartXY::GetNumberPlots() +{ + return this->ChartPrivate->plots.size(); +} + +//----------------------------------------------------------------------------- +void vtkChartXY::ProcessSelectionEvent(vtkObject* caller, void* callData) +{ + cout << "ProcessSelectionEvent called in XY! " << caller << "\t" << callData << endl; + unsigned int *rect = reinterpret_cast(callData); + + // The origin of the plot area + float xOrigin = this->Geometry[2]; + float yOrigin = this->Geometry[3]; + + // Get the scale for the plot area from the x and y axes + float *min = this->XAxis->GetPoint1(); + float *max = this->XAxis->GetPoint2(); + double xScale = (this->XAxis->GetMaximum() - this->XAxis->GetMinimum()) / + (max[0] - min[0]); + min = this->YAxis->GetPoint1(); + max = this->YAxis->GetPoint2(); + double yScale = (this->YAxis->GetMaximum() - this->YAxis->GetMinimum()) / + (max[1] - min[1]); + + double matrix[3][3]; + matrix[0][0] = xScale; + matrix[0][1] = 0; + matrix[0][2] = -1.0 * xOrigin*xScale; + + matrix[1][0] = yScale; + matrix[1][1] = 0; + matrix[1][2] = -1.0 * yOrigin*yScale; + + matrix[2][0] = 0; + matrix[2][1] = 0; + matrix[2][2] = 1; + + double tRect[4]; + + tRect[0] = matrix[0][0]*rect[0] + matrix[0][2]; + tRect[1] = matrix[1][0]*rect[1] + matrix[1][2]; + + tRect[2] = matrix[0][0]*rect[2] + matrix[0][2]; + tRect[3] = matrix[1][0]*rect[3] + matrix[1][2]; + + // As an example - handle zooming using the rubber band... + if (tRect[0] > tRect[2]) + { + double tmp = tRect[0]; + tRect[0] = tRect[2]; + tRect[2] = tmp; + } + if (tRect[1] > tRect[3]) + { + double tmp = tRect[1]; + tRect[1] = tRect[3]; + tRect[3] = tmp; + } + // Now set the values of the axes + this->XAxis->SetMinimum(tRect[0]); + this->XAxis->SetMaximum(tRect[2]); + this->YAxis->SetMinimum(tRect[1]); + this->YAxis->SetMaximum(tRect[3]); + // Now re-render the chart + + vtkInteractorStyle *interactor = reinterpret_cast(caller); +} + + +//----------------------------------------------------------------------------- +void vtkChartXY::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + os << indent << "X Axis: "; + if (this->XAxis) + { + os << endl; + this->XAxis->PrintSelf(os, indent.GetNextIndent()); + } + else + { + os << "(none)" << endl; + } + os << indent << "Y Axis: "; + if (this->YAxis) + { + os << endl; + this->YAxis->PrintSelf(os, indent.GetNextIndent()); + } + else + { + os << "(none)" << endl; + } + if (this->ChartPrivate) + { + os << indent << "Number of plots: " << this->ChartPrivate->plots.size() + << endl; + for (int i = 0; i < this->ChartPrivate->plots.size(); ++i) + { + os << indent << "Plot " << i << ":" << endl; + this->ChartPrivate->plots[i]->PrintSelf(os, indent.GetNextIndent()); + } + } + +} diff --git a/Charts/vtkChartXY.h b/Charts/vtkChartXY.h new file mode 100644 index 0000000000..21a754bbff --- /dev/null +++ b/Charts/vtkChartXY.h @@ -0,0 +1,92 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkChartXY.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 vtkChartXY - Factory class for drawing XY charts +// +// .SECTION Description +// This class implements an XY chart. + +#ifndef __vtkChartXY_h +#define __vtkChartXY_h + +#include "vtkChart.h" + +class vtkPlot; +class vtkAxis; +class vtkPlotGrid; +class vtkTable; +class vtkChartXYPrivate; // Private class to keep my STL vector in... + +class VTK_CHARTS_EXPORT vtkChartXY : public vtkChart +{ +public: + vtkTypeRevisionMacro(vtkChartXY, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + +//BTX + // Description: + // Enum of the available chart types + enum Type { + LINE, + STACKED}; +//ETX + + // Description: + // Creates a 2D Chart object. + static vtkChartXY *New(); + + // Description: + // Paint event for the chart, called whenever the chart needs to be drawn + virtual bool Paint(vtkContext2D *painter); + + // Add a plot to the chart, defaults to using the name of the y column +//BTX + virtual vtkPlot * AddPlot(vtkChart::Type type); +//ETX + virtual vtkIdType GetNumberPlots(); + + // Description: + // This function allows you to set the overall dimensions of the chart. + // An int pointer of length 6 is expected with the dimensions in the order of + // width, height, left border, bottom border, right border, top border in + // pixels of the device. + virtual void SetGeometry(int *p); + +//BTX +protected: + vtkChartXY(); + ~vtkChartXY(); + + // Description: + // Process a rubber band selection event. + virtual void ProcessSelectionEvent(vtkObject* caller, void* callData); + + // The X and Y axes for the chart + vtkAxis *XAxis, *YAxis; + vtkPlotGrid *Grid; + +private: + vtkChartXY(const vtkChartXY &); // Not implemented. + void operator=(const vtkChartXY &); // Not implemented. + + vtkChartXYPrivate *ChartPrivate; // Private class where I hide my STL containers + + // Private functions to render different parts of the chart + void RenderPlots(vtkContext2D *painter); + +//ETX +}; + +#endif //__vtkChartXY_h diff --git a/Charts/vtkContext2D.cxx b/Charts/vtkContext2D.cxx new file mode 100644 index 0000000000..b6f3df16f8 --- /dev/null +++ b/Charts/vtkContext2D.cxx @@ -0,0 +1,396 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContext2D.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 "vtkContext2D.h" + +#include "vtkPoints2D.h" +#include "vtkTransform2D.h" +#include "vtkContextDevice2D.h" +#include "vtkPen.h" +#include "vtkBrush.h" +#include "vtkTextProperty.h" + +#include "vtkFloatArray.h" + +#include "vtkObjectFactory.h" +#include "vtkRenderer.h" +#include "vtkRenderWindow.h" + +vtkCxxRevisionMacro(vtkContext2D, "1.1"); +vtkCxxSetObjectMacro(vtkContext2D, Pen, vtkPen); +vtkCxxSetObjectMacro(vtkContext2D, Brush, vtkBrush); +vtkCxxSetObjectMacro(vtkContext2D, TextProp, vtkTextProperty); +vtkCxxSetObjectMacro(vtkContext2D, Transform, vtkTransform2D); + +//----------------------------------------------------------------------------- +vtkStandardNewMacro(vtkContext2D); + +//----------------------------------------------------------------------------- +bool vtkContext2D::Begin(vtkContextDevice2D *device) +{ + this->Device = device; + this->Modified(); +} + +//----------------------------------------------------------------------------- +bool vtkContext2D::End() +{ + this->Device->End(); + this->Device = NULL; + this->Modified(); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawLine(float x1, float y1, float x2, float y2) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + float x[] = { x1, y1, x2, y2 }; + + this->ApplyPen(); + this->Device->DrawPoly(&x[0], 2); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawLine(float p[4]) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + + this->ApplyPen(); + this->Device->DrawPoly(&p[0], 2); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawLine(vtkPoints2D *points) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + if (points->GetNumberOfPoints() < 2) + { + vtkErrorMacro(<< "Attempted to paint a line with <2 points."); + return; + } + float *f = vtkFloatArray::SafeDownCast(points->GetData())->GetPointer(0); + + this->ApplyPen(); + this->Device->DrawPoly(f, 2); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawPoly(float *x, float *y, int n) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + float p[2*n]; + for (int i = 0; i < n; ++i) + { + p[2*i] = x[i]; + p[2*i+1] = y[i]; + } + + this->ApplyPen(); + this->Device->DrawPoly(&p[0], n); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawPoly(vtkPoints2D *points) +{ + // Construct an array with the correct coordinate packing for OpenGL. + int n = points->GetNumberOfPoints(); + // If the points are of type float then call OpenGL directly + float *f = vtkFloatArray::SafeDownCast(points->GetData())->GetPointer(0); + this->DrawPoly(f, n); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawPoly(float *points, int n) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + if (n < 2) + { + vtkErrorMacro(<< "Attempted to paint a line with <2 points."); + return; + } + + this->ApplyPen(); + + if (this->Transform) + { + float p[2*n]; + this->Transform->TransformPoints(points, &p[0], n); + this->Device->DrawPoly(&p[0], n); + } + else + { + this->Device->DrawPoly(points, n); + } +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawPoint(float x, float y) +{ + float p[] = { x, y }; + this->DrawPoints(&p[0], 1); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawPoints(float *x, float *y, int n) +{ + // Copy the points into an array and draw it. + float p[2*n]; + for (int i = 0; i < n; ++i) + { + p[2*i] = x[i]; + p[2*i+1] = y[i]; + } + this->DrawPoints(&p[0], n); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawPoints(vtkPoints2D *points) +{ + // Construct an array with the correct coordinate packing for OpenGL. + int n = points->GetNumberOfPoints(); + // If the points are of type float then call OpenGL directly + float *f = vtkFloatArray::SafeDownCast(points->GetData())->GetPointer(0); + this->DrawPoints(f, n); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawPoints(float *points, int n) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + + this->ApplyPen(); + if (this->Transform) + { + float p[2*n]; + this->Transform->TransformPoints(points, &p[0], n); + this->Device->DrawPoints(&p[0], n); + } + else + { + this->Device->DrawPoints(points, n); + } +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawRect(float x1, float y1, float x2, float y2) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + float p[] = { x1, y1, + x1+x2, y1, + x1+x2, y1+y2, + x1, y1+y2 }; + if (this->Transform) + { + this->Transform->TransformPoints(&p[0], &p[0], 4); + } + + // Draw the filled area of the rectangle. + this->ApplyBrush(); + this->Device->DrawQuad(&p[0], 4); + + // Draw the outline now. + this->ApplyPen(); + this->Device->DrawPoly(p, 4); + float closeLine[] = { p[0], p[1], p[6], p[7] }; + this->Device->DrawPoly(&closeLine[0], 2); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawQuad(float x1, float y1, float x2, float y2, + float x3, float y3, float x4, float y4) +{ + float p[] = { x1, y1, x2, y2, x3, y3, x4, y4 }; + this->DrawQuad(&p[0]); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawQuad(float *p) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + + // Draw the filled area of the quad. + this->ApplyBrush(); + this->Device->DrawQuad(p, 4); + + // Draw the outline now. + this->ApplyPen(); + this->Device->DrawPoly(p, 4); + float closeLine[] = { p[0], p[1], p[6], p[7] }; + this->Device->DrawPoly(&closeLine[0], 2); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawEllipse(float x, float y, float rx, float ry) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + // Raterize an ellipse + int iterations = 100; + float p[2*(iterations+1)]; + float length = 2.0 * M_PI / iterations; + for (int i = 0; i <= iterations; ++i) + { + p[2*i ] = rx * cos(i * length) + x; + p[2*i+1] = ry * sin(i * length) + y; + } + this->ApplyPen(); + this->Device->DrawPoly(&p[0], iterations + 1); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawText(vtkPoints2D *point, const vtkStdString &string) +{ + float *f = vtkFloatArray::SafeDownCast(point->GetData())->GetPointer(0); + this->DrawText(f[0], f[1], string); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawText(int x, int y, const vtkStdString &string) +{ + if (!this->Device) + { + vtkErrorMacro(<< "Attempted to paint with no active vtkContextDevice2D."); + return; + } + float f[] = { x, y }; + if (this->Transform) + { + this->Transform->TransformPoints(&f[0], &f[0], 1); + } + this->Device->DrawText(&f[0], this->TextProp, string); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawText(vtkPoints2D *point, const char *string) +{ + float *f = vtkFloatArray::SafeDownCast(point->GetData())->GetPointer(0); + vtkStdString str = string; + this->DrawText(f[0], f[1], str); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawText(int x, int y, const char *string) +{ + vtkStdString str = string; + this->DrawText(x, y, str); +} + +//----------------------------------------------------------------------------- +void vtkContext2D::DrawImage(float x, float y, vtkImageData *image) +{ + float p[] = { x, y }; + if (this->Transform) + { + this->Transform->TransformPoints(&p[0], &p[0], 1); + } + this->Device->DrawImage(&p[0], 1, image); +} + +//----------------------------------------------------------------------------- +unsigned int vtkContext2D::AddPointSprite(vtkImageData *image) +{ + this->Device->AddPointSprite(image); +} + +//----------------------------------------------------------------------------- +inline void vtkContext2D::ApplyPen() +{ + this->Device->SetColor4(this->Pen->GetColor()); + this->Device->SetLineWidth(this->Pen->GetWidth()); + this->Device->SetPointSize(this->Pen->GetWidth()); +} + +//----------------------------------------------------------------------------- +inline void vtkContext2D::ApplyBrush() +{ + this->Device->SetColor4(this->Brush->GetColor()); +} + +//----------------------------------------------------------------------------- +vtkContext2D::vtkContext2D() +{ + this->Device = NULL; + this->Pen = vtkPen::New(); + this->Brush = vtkBrush::New(); + this->TextProp = vtkTextProperty::New(); + this->Transform = NULL; +} + +//----------------------------------------------------------------------------- +vtkContext2D::~vtkContext2D() +{ + this->Pen->Delete(); + this->Pen = 0; + this->Brush->Delete(); + this->Brush = 0; + this->TextProp->Delete(); + this->TextProp = 0; +} + +//----------------------------------------------------------------------------- +void vtkContext2D::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + os << indent << "Context Device: "; + if (this->Device) + { + os << endl; + this->Device->PrintSelf(os, indent.GetNextIndent()); + } + else + { + os << "(none)" << endl; + } + os << indent << "Pen: "; + this->Pen->PrintSelf(os, indent.GetNextIndent()); + os << indent << "Brush: "; + this->Brush->PrintSelf(os, indent.GetNextIndent()); +} + diff --git a/Charts/vtkContext2D.h b/Charts/vtkContext2D.h new file mode 100644 index 0000000000..75894b548b --- /dev/null +++ b/Charts/vtkContext2D.h @@ -0,0 +1,188 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContext2D.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 vtkContext2D - Class for drawing 2D primitives to a graphical context. +// +// .SECTION Description +// This defines the interface for drawing onto a 2D context. The context must +// be set up with a vtkContextDevice2D derived class that provides the functions +// to facilitate the low level calls to the context. Currently only an OpenGL +// based device is provided, but this could be extended in the future. + +#ifndef __vtkContext2D_h +#define __vtkContext2D_h + +#include "vtkObject.h" + +class vtkWindow; + +class vtkStdString; +class vtkTextProperty; + +class vtkPoints2D; +class vtkContextDevice2D; +class vtkPen; +class vtkBrush; +class vtkImageData; +class vtkTransform2D; + +class VTK_CHARTS_EXPORT vtkContext2D : public vtkObject +{ +public: + vtkTypeRevisionMacro(vtkContext2D, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Creates a 2D Painter object. + static vtkContext2D *New(); + + // Description: + // Begin painting on a vtkContextDevice2D, no painting can occur before this call + // has been made. Only one painter is allowed at a time on any given paint + // device. Returns true if successful, otherwise false. + bool Begin(vtkContextDevice2D *device); + + vtkGetObjectMacro(Device, vtkContextDevice2D); + + // Description: + // Ends painting on the device, you would not usually need to call this as it + // should be called by the destructor. Returns true if the painter is no + // longer active, otherwise false. + bool End(); + + // Description: + // Draw a line between the specified points. + void DrawLine(float x1, float y1, float x2, float y2); + + // Description: + // Draw a line between the specified points. + void DrawLine(float p[4]); + + // Description: + // Draw a line between the specified points. + // Note: Fastest path - points packed in x and y. + void DrawLine(vtkPoints2D *points); + + // Description: + // Draw a poly line between the specified points. + void DrawPoly(float *x, float *y, int n); + + // Description: + // Draw a poly line between the specified points - fastest code path due to + // memory layout of the coordinates. + void DrawPoly(vtkPoints2D *points); + + // Description: + // Draw a poly line between the specified points, where the float array is of + // size 2*n and the points are packed x1, y1, x2, y2 etc. + // Note: Fastest code path - points packed in x and y. + void DrawPoly(float *points, int n); + + // Description: + // Draw a point at the supplied x and y coordinate + void DrawPoint(float x, float y); + + // Description: + // Draw the specified number of points using the x and y arrays supplied + void DrawPoints(float *x, float *y, int n); + + // Description: + // Draw a poly line between the specified points - fastest code path due to + // memory layout of the coordinates. + void DrawPoints(vtkPoints2D *points); + + // Description: + // Draw a poly line between the specified points, where the float array is of + // size 2*n and the points are packed x1, y1, x2, y2 etc. + // Note: Fastest code path - points packed in x and y. + void DrawPoints(float *points, int n); + + // Description: + // Draw a rectangle with origin at x, y and width w, height h + void DrawRect(float x, float y, float w, float h); + + // Description: + // Draw a quadrilateral at the specified points (4 points, 8 floats in x, y). + void DrawQuad(float x1, float y1, float x2, float y2, + float x3, float y3, float x4, float y4); + void DrawQuad(float *p); + + // Description: + // Draw an ellipse with center at x, y and radii rx, ry. + void DrawEllipse(float x, float y, float rx, float ry); + + // Description: + // Draw the supplied image at the given x, y location (bottom corner). + void DrawImage(float x, float y, vtkImageData *image); + +//BTX + // Description: + // Draw some text to the screen. + void DrawText(vtkPoints2D *point, const vtkStdString &string); + void DrawText(int x, int y, const vtkStdString &string); +//ETX + void DrawText(vtkPoints2D *point, const char *string); + void DrawText(int x, int y, const char *string); + + // Description: + // Get/Set the pen which controls the outlines of shapes as well as lines, + // points and related primitives. + void SetPen(vtkPen *pen); + vtkGetObjectMacro(Pen, vtkPen); + + // Description: + // Get/Set the pen which controls the outlines of shapes as well as lines, + // points and related primitives. + void SetBrush(vtkBrush *brush); + vtkGetObjectMacro(Brush, vtkBrush); + + // Description: + // Get/set the text properties. + void SetTextProp(vtkTextProperty *prop); + vtkGetObjectMacro(TextProp, vtkTextProperty); + + // Description: + // Experimentation with point sprites + unsigned int AddPointSprite(vtkImageData *image); + + // Description: + // Set the transform for the context. + void SetTransform(vtkTransform2D *transform); + vtkGetObjectMacro(Transform, vtkTransform2D); + +//BTX +protected: + vtkContext2D(); + ~vtkContext2D(); + + vtkContextDevice2D *Device; // The underlying device + vtkPen *Pen; // Outlining + vtkBrush *Brush; // Fills + vtkTextProperty *TextProp; // Text property + vtkTransform2D *Transform; // The painter transform + +private: + vtkContext2D(const vtkContext2D &); // Not implemented. + void operator=(const vtkContext2D &); // Not implemented. + + // Apply the pen settings to the context + void ApplyPen(); + // Apply the brush settings to the context + void ApplyBrush(); + +//ETX +}; + +#endif //__vtkContext2D_h diff --git a/Charts/vtkContextActor.cxx b/Charts/vtkContextActor.cxx new file mode 100644 index 0000000000..ba4fe61420 --- /dev/null +++ b/Charts/vtkContextActor.cxx @@ -0,0 +1,106 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextActor.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 "vtkContextActor.h" + +#include "vtkContext2D.h" +#include "vtkOpenGLContextDevice2D.h" +#include "vtkContextScene.h" + +#include "vtkViewport.h" + +#include "vtkObjectFactory.h" + +vtkCxxRevisionMacro(vtkContextActor, "1.1"); +vtkStandardNewMacro(vtkContextActor); + +vtkCxxSetObjectMacro(vtkContextActor, Context, vtkContext2D); +vtkCxxSetObjectMacro(vtkContextActor, Scene, vtkContextScene); + +//---------------------------------------------------------------------------- +// Creates an actor2D with the following defaults: +// position -1, -1 (view coordinates) +// orientation 0, scale (1,1), layer 0, visibility on +vtkContextActor::vtkContextActor() +{ + this->Context = vtkContext2D::New(); + vtkOpenGLContextDevice2D *pd = vtkOpenGLContextDevice2D::New(); + this->Context->Begin(pd); + + this->Scene = vtkContextScene::New(); +} + +//---------------------------------------------------------------------------- +// Destroy an actor2D. +vtkContextActor::~vtkContextActor() +{ + if (this->Context) + { + this->Context->GetDevice()->Delete(); + this->Context->Delete(); + this->Context = NULL; + } + + if (this->Scene) + { + this->Scene->Delete(); + this->Scene = NULL; + } +} + +//---------------------------------------------------------------------------- +void vtkContextActor::ReleaseGraphicsResources(vtkWindow *win) +{ +} + +//---------------------------------------------------------------------------- +// Renders an actor2D's property and then it's mapper. +int vtkContextActor::RenderOverlay(vtkViewport* viewport) +{ + vtkDebugMacro(<< "vtkContextActor::RenderOverlay"); + + if (!this->Context) + { + vtkErrorMacro(<< "vtkContextActor::Render - No painter set"); + return 0; + } + + // This is the entry point for all 2D rendering. + // First initialize the drawing device. + this->Context->GetDevice()->Begin(viewport); + + int size[2]; + size[0] = viewport->GetSize()[0]; + size[1] = viewport->GetSize()[1]; + + this->Scene->SetGeometry(&size[0]); + + this->Scene->Paint(this->Context); + + this->Context->GetDevice()->End(); + + return 1; +} + +//---------------------------------------------------------------------------- +void vtkContextActor::PrintSelf(ostream& os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os,indent); + + os << indent << "Context: " << this->Context << "\n"; + if (this->Context) + { + this->Context->PrintSelf(os, indent.GetNextIndent()); + } +} diff --git a/Charts/vtkContextActor.h b/Charts/vtkContextActor.h new file mode 100644 index 0000000000..3d91ceb42f --- /dev/null +++ b/Charts/vtkContextActor.h @@ -0,0 +1,75 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextActor.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 vtkContextActor - provides a vtkProp derived object. +// .SECTION Description +// This object provides the entry point for the vtkContextScene to be rendered +// in a vtkRenderer. Uses the RenderOverlay pass to render the 2D +// vtkContextScene. + +#ifndef __vtkContextActor_h +#define __vtkContextActor_h + +#include "vtkProp.h" + +class vtkContext2D; +class vtkContextScene; + +class VTK_CHARTS_EXPORT vtkContextActor : public vtkProp +{ +public: + void PrintSelf(ostream& os, vtkIndent indent); + vtkTypeRevisionMacro(vtkContextActor,vtkProp); + + static vtkContextActor* New(); + + // Description: + // We only render in the overlay for the context scene. + virtual int RenderOverlay(vtkViewport *viewport); + + // Description: + // Set the vtkContext2D for the actor. + virtual void SetContext(vtkContext2D *context); + + // Description: + // Set/Get the vtk2DPainter. + vtkGetObjectMacro(Context, vtkContext2D); + + // Description: + // Get the chart object for the Actor. + vtkGetObjectMacro(Scene, vtkContextScene); + + // Description: + // Set the chart object for the Actor. + virtual void SetScene(vtkContextScene *scene); + + // Description: + // Release any graphics resources that are being consumed by this actor. + // The parameter window could be used to determine which graphic + // resources to release. + virtual void ReleaseGraphicsResources(vtkWindow *); + +protected: + vtkContextActor(); + ~vtkContextActor(); + + vtkContextScene *Scene; + vtkContext2D *Context; + +private: + vtkContextActor(const vtkContextActor&); // Not implemented. + void operator=(const vtkContextActor&); // Not implemented. +}; + +#endif diff --git a/Charts/vtkContextDevice2D.cxx b/Charts/vtkContextDevice2D.cxx new file mode 100644 index 0000000000..8e072d194c --- /dev/null +++ b/Charts/vtkContextDevice2D.cxx @@ -0,0 +1,40 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextDevice2D.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 "vtkContextDevice2D.h" + +#include "vtkObjectFactory.h" + +vtkCxxRevisionMacro(vtkContextDevice2D, "1.1"); +//----------------------------------------------------------------------------- +//vtkStandardNewMacro(vtkContextDevice2D); + +vtkContextDevice2D::vtkContextDevice2D() +{ + this->Geometry[0] = 0; + this->Geometry[1] = 0; +} + +//----------------------------------------------------------------------------- +vtkContextDevice2D::~vtkContextDevice2D() +{ +} + +//----------------------------------------------------------------------------- +void vtkContextDevice2D::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); +} diff --git a/Charts/vtkContextDevice2D.h b/Charts/vtkContextDevice2D.h new file mode 100644 index 0000000000..7150cad5a9 --- /dev/null +++ b/Charts/vtkContextDevice2D.h @@ -0,0 +1,140 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextDevice2D.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 vtkContextDevice2D - Abstract class for drawing 2D primitives. +// +// .SECTION Description +// This defines the interface for a vtkContextDevice2D. In this sense a +// ContextDevice is a class used to paint 2D primitives onto a device, such as +// an OpenGL context or a QGraphicsView. + +#ifndef __vtkContextDevice2D_h +#define __vtkContextDevice2D_h + +#include "vtkObject.h" + +class vtkWindow; +class vtkViewport; +class vtkStdString; +class vtkTextProperty; +class vtkPoints2D; +class vtkImageData; + +class VTK_CHARTS_EXPORT vtkContextDevice2D : public vtkObject +{ +public: + vtkTypeRevisionMacro(vtkContextDevice2D, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Draw a poly line using the vtkPoints2D - fastest code path due to memory + // layout of the coordinates. + virtual void DrawPoly(float *points, int n) = 0; + + // Description: + // Draw a poly line using the vtkPoints2D - fastest code path due to memory + // layout of the coordinates. + virtual void DrawPoints(float *points, int n) = 0; + + // Description: + // Draw a quad using the specified number of points. + virtual void DrawQuad(float *points, int n) { ; } + +//BTX + // Description: + // Draw some text to the screen. + virtual void DrawText(float *point, vtkTextProperty *tprop, + const vtkStdString &string) = 0; +//ETX + + // Description: + // Draw the supplied image at the given x, y (p[0], p[1]) location(s) (bottom corner). + virtual void DrawImage(float *p, int n, vtkImageData *image) {;} + + // Description: + // Experimentation with point sprites + virtual unsigned int AddPointSprite(vtkImageData *image) {;} + + // Description: + // Set the color for the device using unsigned char of length 4, RGBA. + virtual void SetColor4(unsigned char *color) = 0; + + // Description: + // Set the color for the device using unsigned char of length 3, RGB. + virtual void SetColor(unsigned char *color) = 0; + + // Description: + // Set the point size for glyphs/sprites. + virtual void SetPointSize(float size) = 0; + + // Description: + // Set the line width for glyphs/sprites. + virtual void SetLineWidth(float width) = 0; + + // Description: + // Get the width of the device in pixels. + virtual int GetWidth() { return this->Geometry[0]; } + + // Description: + // Get the width of the device in pixels. + virtual int GetHeight() { return this->Geometry[1]; } + + // Description: + // Supply a float array of length 4 with x1, y1, x2, y2 specifying the extents. + // of the display + virtual void SetViewExtents(float *x) = 0; + + // Description: + // Push the current matrix onto the stack. + virtual void PushMatrix() = 0; + + // Description: + // Pop the current matrix off of the stack. + virtual void PopMatrix() = 0; + + // Description: + // Supply a float array of length 4 with x1, y1, width, height specifying + // clipping region for the device in pixels. + virtual void SetClipping(int *x) = 0; + + // Description: + // Disable clipping of the display. + virtual void DisableClipping() = 0; + + // Description: + // Begin drawing, pass in the viewport to set up the view. + virtual void Begin(vtkViewport* viewport) { } + + // Description: + // End drawing, clean up the view. + virtual void End() { } + +//BTX +protected: + vtkContextDevice2D(); + virtual ~vtkContextDevice2D(); + + // Description: + // Store the width and height of the device in pixels. + int Geometry[2]; + +private: + vtkContextDevice2D(const vtkContextDevice2D &); // Not implemented. + void operator=(const vtkContextDevice2D &); // Not implemented. + +//ETX +}; + +#endif //__vtkContextDevice2D_h diff --git a/Charts/vtkContextItem.cxx b/Charts/vtkContextItem.cxx new file mode 100644 index 0000000000..6ff8944f2f --- /dev/null +++ b/Charts/vtkContextItem.cxx @@ -0,0 +1,98 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextItem.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 "vtkContextItem.h" + +// Get my new commands +#include "vtkCommand.h" + +#include "vtkAnnotationLink.h" +#include "vtkInteractorStyle.h" +#include "vtkInteractorStyleRubberBand2D.h" +#include "vtkTransform2D.h" +#include "vtkObjectFactory.h" + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkContextItem, "1.1"); +vtkCxxSetObjectMacro(vtkContextItem, Transform, vtkTransform2D) + +//----------------------------------------------------------------------------- +vtkContextItem::vtkContextItem() +{ + this->Transform = NULL;//vtkTransform2D::New(); + this->Opacity = 1.0; +} + +//----------------------------------------------------------------------------- +vtkContextItem::~vtkContextItem() +{ + if (this->Transform) + { + this->Transform->Delete(); + this->Transform = NULL; + } +} + +//----------------------------------------------------------------------------- +bool vtkContextItem::Hit(const vtkContextMouseEvent &mouse) +{ + return false; +} + +//----------------------------------------------------------------------------- +bool vtkContextItem::MouseEnterEvent(const vtkContextMouseEvent &mouse) +{ + return false; +} + +//----------------------------------------------------------------------------- +bool vtkContextItem::MouseMoveEvent(const vtkContextMouseEvent &mouse) +{ + return false; +} + +//----------------------------------------------------------------------------- +bool vtkContextItem::MouseLeaveEvent(const vtkContextMouseEvent &mouse) +{ + return false; +} + +//----------------------------------------------------------------------------- +bool vtkContextItem::MouseButtonPressEvent(const vtkContextMouseEvent &mouse) +{ + return false; +} + +//----------------------------------------------------------------------------- +bool vtkContextItem::MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse) +{ + return false; +} + +//----------------------------------------------------------------------------- +void vtkContextItem::Translate(float dx, float dy) +{ + if (!this->Transform) + { + this->Transform = vtkTransform2D::New(); + } + this->Transform->Translate(dx, dy); +} + +//----------------------------------------------------------------------------- +void vtkContextItem::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); +} diff --git a/Charts/vtkContextItem.h b/Charts/vtkContextItem.h new file mode 100644 index 0000000000..0ffc4140d9 --- /dev/null +++ b/Charts/vtkContextItem.h @@ -0,0 +1,102 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextItem.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 vtkContextItem - base class for items that are part of a vtkContextScene. +// +// .SECTION Description +// Derive from this class to create custom items that can be added to a +// vtkContextScene. + +#ifndef __vtkContextItem_h +#define __vtkContextItem_h + +#include "vtkObject.h" + +class vtkContext2D; +class vtkTransform2D; +struct vtkContextMouseEvent; + +class VTK_CHARTS_EXPORT vtkContextItem : public vtkObject +{ +public: + vtkTypeRevisionMacro(vtkContextItem, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Paint event for the item, called whenever the item needs to be drawn, + virtual bool Paint(vtkContext2D *painter) = 0; + +//BTX + // Description: + // Return true if the supplied x, y coordinate is inside the item. + virtual bool Hit(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse enter event. + virtual bool MouseEnterEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse move event. + virtual bool MouseMoveEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse leave event. + virtual bool MouseLeaveEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse button down event + virtual bool MouseButtonPressEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse button release event. + virtual bool MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse); +//ETX + + // Description: + // Set the transform for the item. + virtual void SetTransform(vtkTransform2D *transform); + + // Description: + // Set the transform for the item. + vtkGetObjectMacro(Transform, vtkTransform2D); + + // Description: + // Get the opacity of the item. + vtkGetMacro(Opacity, double); + + // Description: + // Set the opacity of the item. + vtkSetMacro(Opacity, double); + + // Description: + // Translate the item by the given dx, dy. + void Translate(float dx, float dy); + +//BTX +protected: + vtkContextItem(); + ~vtkContextItem(); + + vtkTransform2D *Transform; + + double Opacity; + +private: + vtkContextItem(const vtkContextItem &); // Not implemented. + void operator=(const vtkContextItem &); // Not implemented. +//ETX +}; + +#endif //__vtkContextItem_h diff --git a/Charts/vtkContextMapper2D.cxx b/Charts/vtkContextMapper2D.cxx new file mode 100644 index 0000000000..edb018db6f --- /dev/null +++ b/Charts/vtkContextMapper2D.cxx @@ -0,0 +1,73 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextMapper2D.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 "vtkContextMapper2D.h" + +#include "vtkTable.h" +#include "vtkInformation.h" +#include "vtkExecutive.h" + +#include "vtkObjectFactory.h" + +vtkCxxRevisionMacro(vtkContextMapper2D, "1.1"); +vtkStandardNewMacro(vtkContextMapper2D); +//----------------------------------------------------------------------------- +vtkContextMapper2D::vtkContextMapper2D() +{ + // We take 1 input and no outputs + this->SetNumberOfInputPorts(1); + this->SetNumberOfOutputPorts(0); +} + +//----------------------------------------------------------------------------- +vtkContextMapper2D::~vtkContextMapper2D() +{ +} + +//---------------------------------------------------------------------------- +void vtkContextMapper2D::SetInput(vtkTable *input) +{ + if(input) + { + vtkDebugMacro(<< "Input table set."); + this->SetInputConnection(0, input->GetProducerPort()); + } + else + { + // Setting a NULL input removes the connection. + vtkDebugMacro(<< "Null input table set."); + this->SetInputConnection(0, 0); + } +} + +//---------------------------------------------------------------------------- +vtkTable * vtkContextMapper2D::GetInput() +{ + return vtkTable::SafeDownCast(this->GetExecutive()->GetInputData(0, 0)); +} + +//----------------------------------------------------------------------------- +int vtkContextMapper2D::FillInputPortInformation(int, vtkInformation *info) +{ + info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable"); + return 1; +} + + +//----------------------------------------------------------------------------- +void vtkContextMapper2D::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); +} diff --git a/Charts/vtkContextMapper2D.h b/Charts/vtkContextMapper2D.h new file mode 100644 index 0000000000..cf6f3bda4a --- /dev/null +++ b/Charts/vtkContextMapper2D.h @@ -0,0 +1,74 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextMapper2D.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 vtkContextMapper2D - Abstract class for 2D context mappers. +// +// .SECTION Description +// +// This class provides an abstract base for 2D context mappers. They currently +// only accept vtkTable objects as input. + +#ifndef __vtkContextMapper2D_h +#define __vtkContextMapper2D_h + +#include "vtkAlgorithm.h" + +class vtkContext2D; +class vtkTable; +class vtkDataArray; +class vtkAbstractArray; + +class VTK_CHARTS_EXPORT vtkContextMapper2D : public vtkAlgorithm +{ +public: + vtkTypeRevisionMacro(vtkContextMapper2D, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + static vtkContextMapper2D *New(); + + // Description: + // Set/Get the input for this object - only accepts vtkTable as input. + virtual void SetInput(vtkTable *input); + virtual vtkTable * GetInput(); + + // Description: + // Make the arrays accessible to the plot objects. + vtkDataArray *GetInputArrayToProcess(int idx, + vtkDataObject* input) + { + return this->vtkAlgorithm::GetInputArrayToProcess(idx, input); + } + + vtkAbstractArray *GetInputAbstractArrayToProcess(int idx, + vtkDataObject* input) + { + return this->vtkAlgorithm::GetInputAbstractArrayToProcess(idx, input); + } + +//BTX +protected: + vtkContextMapper2D(); + ~vtkContextMapper2D(); + + // Description: + // Specify the types of input we can handle. + virtual int FillInputPortInformation(int port, vtkInformation *info); + +private: + vtkContextMapper2D(const vtkContextMapper2D &); // Not implemented. + void operator=(const vtkContextMapper2D &); // Not implemented. +//ETX +}; + +#endif //__vtkContextMapper2D_h diff --git a/Charts/vtkContextScene.cxx b/Charts/vtkContextScene.cxx new file mode 100644 index 0000000000..db8e07fbe6 --- /dev/null +++ b/Charts/vtkContextScene.cxx @@ -0,0 +1,315 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextScene.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 "vtkContextScene.h" + +#include "vtkContextItem.h" +#include "vtkContext2D.h" +#include "vtkTransform2D.h" +#include "vtkMatrix3x3.h" + +// Get my new commands +#include "vtkCommand.h" + +#include "vtkAnnotationLink.h" +#include "vtkInteractorStyle.h" +#include "vtkRenderWindowInteractor.h" +#include "vtkRenderWindow.h" +#include "vtkInteractorStyleRubberBand2D.h" +#include "vtkObjectFactory.h" + +// My STL containers +#include + +//----------------------------------------------------------------------------- +// Minimal command class to handle callbacks. +class vtkContextScene::Command : public vtkCommand +{ +public: + Command(vtkContextScene *scene) { this->Target = scene; } + + virtual void Execute(vtkObject *caller, unsigned long eventId, void *callData) + { + if (this->Target) + { + vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(caller); + vtkRenderWindowInteractor *interactor = + vtkRenderWindowInteractor::SafeDownCast(style->GetInteractor()); + + int x = interactor->GetEventPosition()[0]; + int y = interactor->GetEventPosition()[1]; + + switch (eventId) + { + case vtkCommand::MouseMoveEvent : + this->Target->MouseMoveEvent(x, y); + break; + case vtkCommand::LeftButtonPressEvent : + this->Target->ButtonPressEvent(0, x, y); + break; + case vtkCommand::MiddleButtonPressEvent : + this->Target->ButtonPressEvent(1, x, y); + break; + case vtkCommand::RightButtonPressEvent : + this->Target->ButtonPressEvent(2, x, y); + break; + case vtkCommand::LeftButtonReleaseEvent : + this->Target->ButtonReleaseEvent(0, x, y); + break; + case vtkCommand::MiddleButtonReleaseEvent : + this->Target->ButtonReleaseEvent(1, x, y); + break; + case vtkCommand::RightButtonReleaseEvent : + this->Target->ButtonReleaseEvent(2, x, y); + break; + case vtkCommand::SelectionChangedEvent : + this->Target->ProcessSelectionEvent(caller, callData); + break; + default: + this->Target->ProcessEvents(caller, eventId, callData); + } + } + } + + void SetTarget(vtkContextScene* t) { this->Target = t; } + + vtkContextScene *Target; +}; + +//----------------------------------------------------------------------------- +// Minimal storage class for STL containers etc. +class vtkContextScene::Private +{ +public: + vtkstd::vector items; + vtkstd::vector itemState; + int itemMousePressCurrent; // Index of the item with a current mouse down + vtkContextMouseEvent Event; // Mouse event structure +}; + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkContextScene, "1.1"); +vtkStandardNewMacro(vtkContextScene); +vtkCxxSetObjectMacro(vtkContextScene, AnnotationLink, vtkAnnotationLink); +vtkCxxSetObjectMacro(vtkContextScene, Window, vtkRenderWindow); + +//----------------------------------------------------------------------------- +vtkContextScene::vtkContextScene() +{ + this->Observer = new vtkContextScene::Command(this); + this->Storage = new Private; + this->Storage->itemMousePressCurrent = -1; + this->Storage->Event.Button = -1; + this->AnnotationLink = NULL; + this->Window = NULL; +} + +//----------------------------------------------------------------------------- +vtkContextScene::~vtkContextScene() +{ + delete this->Observer; + this->Observer = 0; + delete this->Storage; + this->Storage = 0; +} + +//----------------------------------------------------------------------------- +bool vtkContextScene::Paint(vtkContext2D *painter) +{ + vtkDebugMacro("Paint event called."); + unsigned int size = this->Storage->items.size(); + for (unsigned int i = 0; i < size; ++i) + { + painter->SetTransform(this->Storage->items[i]->GetTransform()); + this->Storage->items[i]->Paint(painter); + } +} + +//----------------------------------------------------------------------------- +void vtkContextScene::AddItem(vtkContextItem *item) +{ + this->Storage->items.push_back(item); + this->Storage->itemState.push_back(false); +} + +//----------------------------------------------------------------------------- +int vtkContextScene::NumberOfItems() +{ + return this->Storage->items.size(); +} + +//----------------------------------------------------------------------------- +vtkContextItem * vtkContextScene::GetItem(int index) +{ + if (index < this->NumberOfItems()) + { + return this->Storage->items[index]; + } +} + +//----------------------------------------------------------------------------- +void vtkContextScene::SetInteractorStyle(vtkInteractorStyle *interactor) +{ + cout << "Interactor style " << interactor << " " << interactor->GetClassName() << endl; + interactor->AddObserver(vtkCommand::SelectionChangedEvent, this->Observer); + interactor->AddObserver(vtkCommand::AnyEvent, this->Observer); +} + +//----------------------------------------------------------------------------- +void vtkContextScene::ProcessEvents(vtkObject* caller, unsigned long eventId, + void* callData) +{ + vtkDebugMacro("ProcessEvents called! " << caller->GetClassName() << "\t" + << vtkCommand::GetStringFromEventId(eventId) + << "\n\t" << vtkInteractorStyleRubberBand2D::SafeDownCast(caller)->GetInteraction()); +} + +//----------------------------------------------------------------------------- +void vtkContextScene::ProcessSelectionEvent(vtkObject* caller, void* callData) +{ + cout << "ProcessSelectionEvent called! " << caller << "\t" << callData << endl; + unsigned int *rect = reinterpret_cast(callData); + cout << "Rect:"; + for (int i = 0; i < 5; ++i) + { + cout << "\t" << rect[i]; + } + cout << endl; +} + +//----------------------------------------------------------------------------- +void vtkContextScene::MouseMoveEvent(int x, int y) +{ + unsigned int size = this->Storage->items.size(); + vtkContextMouseEvent &event = this->Storage->Event; + event.ScreenPos[0] = x; + event.ScreenPos[1] = y; + event.ScenePos[0] = x; + event.ScenePos[1] = y; + + // Check if there is a selected item that needs to receive a move event + if (this->Storage->itemMousePressCurrent >= 0) + { + this->PerformTransform( + this->Storage->items[this->Storage->itemMousePressCurrent]->GetTransform(), + event); + this->Storage->items[this->Storage->itemMousePressCurrent]->MouseMoveEvent(event); + } + for (int i = size-1; i >= 0; --i) + { + if (this->Storage->itemMousePressCurrent == i) + { + // Don't send the mouse move event twice... + continue; + } + this->PerformTransform(this->Storage->items[i]->GetTransform(), event); + if (this->Storage->items[i]->Hit(event)) + { + if (!this->Storage->itemState[i] && this->Storage->itemMousePressCurrent < 0) + { + this->Storage->itemState[i] = true; + this->Storage->items[i]->MouseEnterEvent(event); + } + } + else + { + if (this->Storage->itemState[i]) + { + this->Storage->itemState[i] = false; + this->Storage->items[i]->MouseLeaveEvent(event); + } + } + } + + // Update the last positions now + event.LastScreenPos[0] = event.ScreenPos[0]; + event.LastScreenPos[1] = event.ScreenPos[1]; + event.LastScenePos[0] = event.ScenePos[0]; + event.LastScenePos[1] = event.ScenePos[1]; + + if (this->Window) + { + this->Window->Render(); + } +} + +//----------------------------------------------------------------------------- +void vtkContextScene::ButtonPressEvent(int button, int x, int y) +{ + unsigned int size = this->Storage->items.size(); + vtkContextMouseEvent &event = this->Storage->Event; + event.ScreenPos[0] = event.LastScreenPos[0] = x; + event.ScreenPos[1] = event.LastScreenPos[1] = y; + event.ScenePos[0] = event.LastScenePos[0] = x; + event.ScenePos[1] = event.LastScenePos[1] = y; + event.Button = button; + for (int i = size-1; i >= 0; --i) + { + this->PerformTransform(this->Storage->items[i]->GetTransform(), event); + if (this->Storage->items[i]->Hit(event)) + { + if (this->Storage->items[i]->MouseButtonPressEvent(event)) + { + // The event was accepted - stop propagating + this->Storage->itemMousePressCurrent = i; + return; + } + } + } +} + +//----------------------------------------------------------------------------- +void vtkContextScene::ButtonReleaseEvent(int button, int x, int y) +{ + if (this->Storage->itemMousePressCurrent >= 0) + { + vtkContextMouseEvent &event = this->Storage->Event; + event.ScreenPos[0] = x; + event.ScreenPos[1] = y; + event.ScenePos[0] = x; + event.ScenePos[1] = y; + event.Button = button; + this->PerformTransform( + this->Storage->items[this->Storage->itemMousePressCurrent]->GetTransform(), + event); + this->Storage->items[this->Storage->itemMousePressCurrent]->MouseButtonReleaseEvent(event); + this->Storage->itemMousePressCurrent = -1; + event.Button = -1; + } +} + +//----------------------------------------------------------------------------- +inline void vtkContextScene::PerformTransform(vtkTransform2D *transform, + vtkContextMouseEvent &mouse) +{ + if (transform) + { + transform->InverseTransformPoints(&mouse.ScenePos[0], &mouse.Pos[0], 1); + } + else + { + mouse.Pos[0] = mouse.ScenePos[0]; + mouse.Pos[1] = mouse.ScenePos[1]; + } +} + +//----------------------------------------------------------------------------- +void vtkContextScene::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + // Print out the chart's geometry if it has been set + os << indent << "Widthxheight: " << this->Geometry[0] << "\t" << this->Geometry[1] + << endl; +} diff --git a/Charts/vtkContextScene.h b/Charts/vtkContextScene.h new file mode 100644 index 0000000000..9a54d1474c --- /dev/null +++ b/Charts/vtkContextScene.h @@ -0,0 +1,152 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextScene.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 vtkContextScene - Provides a 2D scene for vtkContextItem objects. +// +// .SECTION Description +// Provides a 2D scene that vtkContextItem objects can be added to. Manages the +// items, ensures that they are rendered at the right times and passes on mouse +// events. + +#ifndef __vtkContextScene_h +#define __vtkContextScene_h + +#include "vtkObject.h" + +class vtkContext2D; +class vtkContextItem; +class vtkTransform2D; +struct vtkContextMouseEvent; + +class vtkInteractorStyle; +class vtkAnnotationLink; + +class vtkRenderWindow; + +class VTK_CHARTS_EXPORT vtkContextScene : public vtkObject +{ +public: + vtkTypeRevisionMacro(vtkContextScene, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Creates a 2D Painter object. + static vtkContextScene * New(); + + // Description: + // Paint event for the chart, called whenever the chart needs to be drawn + virtual bool Paint(vtkContext2D *painter); + + // Description: + // Add an item to the scene. + void AddItem(vtkContextItem *item); + + // Description: + // Get the number of items in the scene. + int NumberOfItems(); + + // Get the item at the specified index. + vtkContextItem * GetItem(int index); + + // Description: + // Set the vtkAnnotationLink for the chart. + virtual void SetAnnotationLink(vtkAnnotationLink *link); + + // Description: + // Get the vtkAnnotationLink for the chart. + vtkGetObjectMacro(AnnotationLink, vtkAnnotationLink); + + // Description: + // Set the width and height of the scene in pixels. + vtkSetVector2Macro(Geometry, int); + + // Description: + // Add the scene as an observer on the supplied interactor style. + void SetInteractorStyle(vtkInteractorStyle *interactor); + + // Description: + // This should not be necessary as the context view should take care of + // rendering. + virtual void SetWindow(vtkRenderWindow *window); + +//BTX +protected: + vtkContextScene(); + ~vtkContextScene(); + + // Description: + // Called to process events - figure out what child(ren) to propagate events + // to. + virtual void ProcessEvents(vtkObject* caller, unsigned long eventId, + void* callData); + + // Description: + // Process a rubber band selection event. + virtual void ProcessSelectionEvent(vtkObject* caller, void* callData); + + // Description: + // Process a mouse move event. + virtual void MouseMoveEvent(int x, int y); + + // Description: + // Process a mouse button press event. + virtual void ButtonPressEvent(int button, int x, int y); + + // Description: + // Process a mouse button release event. + virtual void ButtonReleaseEvent(int button, int x, int y); + + vtkAnnotationLink *AnnotationLink; + + // Store the chart dimensions - width, height of scene in pixels + int Geometry[2]; + + // Description: + // The command object for the charts. + class Command; + friend class Command; + Command *Observer; + + // Description: + // Private storage object - where we hide all of our STL objects... + class Private; + Private *Storage; + + vtkRenderWindow *Window; + + // Description: + // Perform translation and fill in the vtkContextMouseEvent struct. + void PerformTransform(vtkTransform2D *transform, vtkContextMouseEvent &mouse); + +private: + vtkContextScene(const vtkContextScene &); // Not implemented. + void operator=(const vtkContextScene &); // Not implemented. +//ETX +}; + +// Description: +// Data structure to store context scene mouse events to be passed to items. +struct vtkContextMouseEvent +{ + float Pos[2]; // Item position + float ScenePos[2]; // Position in scene coordinates + int ScreenPos[2]; // Position in screen coordinates + float LastPos[2]; // Item position + float LastScenePos[2]; // Position in scene coordinates + int LastScreenPos[2]; // Position in screen coordinates + int Button; // Mouse button that was pressed (0-left, 1-middle, 2-right) +}; + +#endif //__vtkContextScene_h diff --git a/Charts/vtkContextView.cxx b/Charts/vtkContextView.cxx new file mode 100644 index 0000000000..44b1b9a5ac --- /dev/null +++ b/Charts/vtkContextView.cxx @@ -0,0 +1,111 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextView.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 "vtkContextView.h" + +#include "vtkContext2D.h" +#include "vtkOpenGLContextDevice2D.h" +#include "vtkContextScene.h" + +#include "vtkViewport.h" +#include "vtkRenderer.h" +#include "vtkRenderWindow.h" +#include "vtkRenderWindowInteractor.h" +#include "vtkInteractorStyle.h" +#include "vtkContextActor.h" + +#include "vtkObjectFactory.h" + +vtkCxxRevisionMacro(vtkContextView, "1.1"); +vtkStandardNewMacro(vtkContextView); + +vtkCxxSetObjectMacro(vtkContextView, Context, vtkContext2D); +vtkCxxSetObjectMacro(vtkContextView, Scene, vtkContextScene); + +//---------------------------------------------------------------------------- +vtkContextView::vtkContextView() +{ + this->Context = vtkContext2D::New(); + vtkOpenGLContextDevice2D *pd = vtkOpenGLContextDevice2D::New(); + this->Context->Begin(pd); + + vtkContextActor *actor = vtkContextActor::New(); + this->Renderer->AddActor(actor); + this->Scene = actor->GetScene(); + // Should not need to do this... + this->Scene->SetWindow(this->RenderWindow); + + // Set up our view to render on move, 2D interaction style + this->SetDisplayHoverText(false); + this->RenderOnMouseMoveOn(); + this->SetInteractionModeTo2D(); + + // Single color background + this->Renderer->SetBackground(1.0, 1.0, 1.0); + this->Renderer->SetBackground2(1.0, 1.0, 1.0); +} + +//---------------------------------------------------------------------------- +vtkContextView::~vtkContextView() +{ + if (this->Context) + { + this->Context->GetDevice()->Delete(); + this->Context->Delete(); + this->Context = NULL; + } + + if (this->Scene) + { + this->Scene->Delete(); + this->Scene = NULL; + } +} + +//---------------------------------------------------------------------------- +void vtkContextView::SetInteractionMode(int mode) +{ + this->vtkRenderView::SetInteractionMode(mode); + this->Scene->SetInteractorStyle( + vtkInteractorStyle::SafeDownCast(this->RenderWindow->GetInteractor()->GetInteractorStyle())); +} + +void vtkContextView::Render() +{ + this->Update(); + this->PrepareForRendering(); + this->Renderer->ResetCameraClippingRange(); + this->RenderWindow->Render(); + + // Render our scene +/* this->Context->GetDevice()->Begin(this->Renderer); + int size[2]; + size[0] = this->Renderer->GetSize()[0]; + size[1] = this->Renderer->GetSize()[1]; + this->Scene->SetGeometry(&size[0]); + this->Scene->Paint(this->Context); + this->Context->GetDevice()->End(); */ +} + +//---------------------------------------------------------------------------- +void vtkContextView::PrintSelf(ostream& os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os,indent); + + os << indent << "Context: " << this->Context << "\n"; + if (this->Context) + { + this->Context->PrintSelf(os, indent.GetNextIndent()); + } +} diff --git a/Charts/vtkContextView.h b/Charts/vtkContextView.h new file mode 100644 index 0000000000..26574ed9c2 --- /dev/null +++ b/Charts/vtkContextView.h @@ -0,0 +1,75 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkContextView.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 vtkContextView - provides a view of the vtkContextScene. +// +// .SECTION Description +// This class is derived from vtkRenderView and provides a view of a +// vtkContextScene, with a default interactor style, renderer etc. + +#ifndef __vtkContextView_h +#define __vtkContextView_h + +#include "vtkRenderView.h" + +class vtkContext2D; +class vtkContextScene; +class vtkRenderWindowInteractor; + +class VTK_CHARTS_EXPORT vtkContextView : public vtkRenderView +{ +public: + void PrintSelf(ostream& os, vtkIndent indent); + vtkTypeRevisionMacro(vtkContextView,vtkRenderView); + + static vtkContextView* New(); + + // Description: + // Set the vtkContext2D for the view. + virtual void SetContext(vtkContext2D *context); + + // Description: + // Get the vtkContext2D for the view. + vtkGetObjectMacro(Context, vtkContext2D); + + // Description: + // Set the interaction mode, defaults to 2D here. + virtual void SetInteractionMode(int mode); + + // Description: + // Get the scene of the view. + vtkGetObjectMacro(Scene, vtkContextScene); + + // Description: + // Set the scene object for the view. + virtual void SetScene(vtkContextScene *scene); + + // Description: + // Updates the representations, then calls Render() on the render window + // associated with this view. + virtual void Render(); + +protected: + vtkContextView(); + ~vtkContextView(); + + vtkContextScene *Scene; + vtkContext2D *Context; + +private: + vtkContextView(const vtkContextView&); // Not implemented. + void operator=(const vtkContextView&); // Not implemented. +}; + +#endif diff --git a/Charts/vtkImageItem.cxx b/Charts/vtkImageItem.cxx new file mode 100644 index 0000000000..a59277db7a --- /dev/null +++ b/Charts/vtkImageItem.cxx @@ -0,0 +1,191 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkImageItem.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 "vtkImageItem.h" + +// Get my new commands +#include "vtkCommand.h" + +#include "vtkContext2D.h" +#include "vtkContextScene.h" +#include "vtkTransform2D.h" +#include "vtkPen.h" +#include "vtkBrush.h" +#include "vtkTextProperty.h" +#include "vtkImageData.h" + +#include "vtkObjectFactory.h" + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkImageItem, "1.1"); +vtkStandardNewMacro(vtkImageItem); + +//----------------------------------------------------------------------------- +vtkCxxSetObjectMacro(vtkImageItem, Image, vtkImageData); + +//----------------------------------------------------------------------------- +vtkImageItem::vtkImageItem() +{ + this->Label = NULL; + this->Image = NULL; + this->MouseOver = false; + this->MouseButtonPressed = -1; + this->scalarFunction = NULL; +} + +//----------------------------------------------------------------------------- +vtkImageItem::~vtkImageItem() +{ + this->SetLabel(NULL); +} + +//----------------------------------------------------------------------------- +bool vtkImageItem::Paint(vtkContext2D *painter) +{ + // Drawing a hard wired diagram 800x600 as a demonstration of the 2D API + painter->GetTextProp()->SetVerticalJustificationToCentered(); + painter->GetTextProp()->SetJustificationToCentered(); + painter->GetTextProp()->SetColor(0.0, 0.0, 0.0); + painter->GetTextProp()->SetFontSize(24); + painter->GetPen()->SetColor(0, 0, 0); + + if (this->MouseOver) + { + painter->GetBrush()->SetColor(255, 0, 0); + } + else + { + painter->GetBrush()->SetColor(0, 255, 0); + } + painter->DrawRect(this->Dimensions[0], this->Dimensions[1], + this->Dimensions[2], this->Dimensions[3]); + + if (this->Image) + { + // Draw our image in the bottom left corner of the item + painter->DrawImage(this->Dimensions[0]+10, this->Dimensions[1]+10, this->Image); + } + + if (this->MouseOver && this->Label) + { + painter->GetBrush()->SetColor(255, 200, 0); + painter->DrawRect(this->Dimensions[0]+10, this->Dimensions[1]+50, + 100, 20); + painter->GetTextProp()->SetColor(0.0, 0.0, 0.0); + painter->GetTextProp()->SetFontSize(12); + painter->DrawText(this->Dimensions[0]+60, this->Dimensions[1]+60, + this->Label); + } + + float p[] = { 0.0, 0.0, + 20.0, 50.0, + 30.0, 5.0, + 100.0, 30.0 }; + painter->DrawPoly(p, 4); + + if (this->scalarFunction) + { + // We have a function pointer - do something... + ; + } +} + +//----------------------------------------------------------------------------- +bool vtkImageItem::Hit(const vtkContextMouseEvent &mouse) +{ + if (mouse.Pos[0] > this->Dimensions[0] && + mouse.Pos[0] < this->Dimensions[0]+this->Dimensions[2] && + mouse.Pos[1] > this->Dimensions[1] && + mouse.Pos[1] < this->Dimensions[1]+this->Dimensions[3]) + { + return true; + } + else + { + return false; + } + return false; +} + +//----------------------------------------------------------------------------- +bool vtkImageItem::MouseEnterEvent(const vtkContextMouseEvent &mouse) +{ + this->MouseOver = true; + return true; +} + +//----------------------------------------------------------------------------- +bool vtkImageItem::MouseMoveEvent(const vtkContextMouseEvent &mouse) +{ + // Work out our deltas... + int deltaX = mouse.ScenePos[0] - mouse.LastScenePos[0]; + int deltaY = mouse.ScenePos[1] - mouse.LastScenePos[1]; + + if (mouse.Button == 0) // Left mouse button - translate + { + // Move the block by this amount + this->Translate(deltaX, deltaY); + return true; + } + else if (mouse.Button == 1) + { + // Resize the block by this amount + this->Dimensions[0] += deltaX; + this->Dimensions[1] += deltaY; + this->Dimensions[2] -= deltaX; + this->Dimensions[3] -= deltaY; + + return true; + } + else if (mouse.Button == 2) + { + // Resize the block by this amount + this->Dimensions[2] += deltaX; + this->Dimensions[3] += deltaY; + + return true; + } + return false; +} + +//----------------------------------------------------------------------------- +bool vtkImageItem::MouseLeaveEvent(const vtkContextMouseEvent &mouse) +{ + this->MouseOver = false; + return true; +} + +//----------------------------------------------------------------------------- +bool vtkImageItem::MouseButtonPressEvent(const vtkContextMouseEvent &mouse) +{ + return true; +} + +//----------------------------------------------------------------------------- +bool vtkImageItem::MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse) +{ + return true; +} + +void vtkImageItem::SetScalarFunctor(double (*scalarFunction)(double, double)) +{ + this->scalarFunction = scalarFunction; +} + +//----------------------------------------------------------------------------- +void vtkImageItem::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); +} diff --git a/Charts/vtkImageItem.h b/Charts/vtkImageItem.h new file mode 100644 index 0000000000..0b16cf7b11 --- /dev/null +++ b/Charts/vtkImageItem.h @@ -0,0 +1,121 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkImageItem.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 vtkImageItem - a vtkContextItem that draws a supplied image in the +// scene. +// +// .SECTION Description +// This vtkContextItem draws the supplied image in the scene. Optionally showing +// the label as a tooltip on mouse over. + +#ifndef __vtkImageItem_h +#define __vtkImageItem_h + +#include "vtkContextItem.h" + +class vtkContext2D; +class vtkImageData; + +class VTK_CHARTS_EXPORT vtkImageItem : public vtkContextItem +{ +public: + vtkTypeRevisionMacro(vtkImageItem, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + static vtkImageItem *New(); + + // Description: + // Paint event for the item. + virtual bool Paint(vtkContext2D *painter); + +//BTX + // Description: + // Return true if the supplied x, y coordinate is inside the item. + virtual bool Hit(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse enter event. + virtual bool MouseEnterEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse move event. + virtual bool MouseMoveEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse leave event. + virtual bool MouseLeaveEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse button down event. + virtual bool MouseButtonPressEvent(const vtkContextMouseEvent &mouse); + + // Description: + // Mouse button release event. + virtual bool MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse); +//ETX + + // Description: + // Set the mouse over label for the item. + vtkSetStringMacro(Label); + + // Description: + // Get the mouse over label for the item. + vtkGetStringMacro(Label); + + // Description: + // Set the image of the item. + void SetImage(vtkImageData *image); + + // Description: + // Get the image of the item. + vtkGetObjectMacro(Image, vtkImageData); + + // Description: + // Set the dimensions of the item, bottom corner, width, height. + vtkSetVector4Macro(Dimensions, int); + + // Description: + // Get the dimensions of the item, bottom corner, width, height. + vtkGetVector4Macro(Dimensions, int); + +//BTX + void SetScalarFunctor(double (*scalarFunction)(double, double)); +//ETX + +//BTX +protected: + vtkImageItem(); + ~vtkImageItem(); + + int Dimensions[4]; + + int LastPosition[2]; + + char *Label; + vtkImageData *Image; + + bool MouseOver; + int MouseButtonPressed; + + // Some function pointers to optionally do funky things... + double (*scalarFunction)(double, double); + +private: + vtkImageItem(const vtkImageItem &); // Not implemented. + void operator=(const vtkImageItem &); // Not implemented. +//ETX +}; + +#endif //__vtkImageItem_h diff --git a/Charts/vtkOpenGLContextDevice2D.cxx b/Charts/vtkOpenGLContextDevice2D.cxx new file mode 100644 index 0000000000..e3a72d0261 --- /dev/null +++ b/Charts/vtkOpenGLContextDevice2D.cxx @@ -0,0 +1,384 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkOpenGLContextDevice2D.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 "vtkOpenGLContextDevice2D.h" + +#include "vtkPoints2D.h" + +#include "vtkFloatArray.h" +#include "vtkSmartPointer.h" + +#include "vtkActor2D.h" +#include "vtkMath.h" +#include "vtkObjectFactory.h" +#include "vtkPlane.h" +#include "vtkPlaneCollection.h" +#include "vtkPointData.h" +#include "vtkPolyData.h" +#include "vtkProperty2D.h" +#include "vtkScalarsToColors.h" +#include "vtkUnsignedCharArray.h" +#include "vtkViewport.h" +#include "vtkWindow.h" +#include "vtkgluPickMatrix.h" + +#include "vtkTexture.h" +#include "vtkImageData.h" + +#include "vtkRenderer.h" +#include "vtkOpenGLRenderer.h" +#include "vtkOpenGLRenderWindow.h" +#include "vtkOpenGLExtensionManager.h" +#include "vtkgl.h" + +#ifdef VTK_USE_QT + #include "vtkQtLabelRenderStrategy.h" +#else + #include "vtkFreeTypeLabelRenderStrategy.h" +#endif + +#include "vtkObjectFactory.h" + +//----------------------------------------------------------------------------- +class vtkOpenGLContextDevice2D::Private +{ +public: + Private() + { + this->texture = 0; + } + + vtkTexture *texture; +}; + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkOpenGLContextDevice2D, "1.1"); +vtkStandardNewMacro(vtkOpenGLContextDevice2D); + +//----------------------------------------------------------------------------- +vtkOpenGLContextDevice2D::vtkOpenGLContextDevice2D() +{ + this->Renderer = 0; + this->IsTextDrawn = false; +#ifdef VTK_USE_QT + this->TextRenderer = vtkQtLabelRenderStrategy::New(); +#else + this->TextRenderer = vtkFreeTypeLabelRenderStrategy::New(); +#endif + this->Storage = new vtkOpenGLContextDevice2D::Private; +} + +//----------------------------------------------------------------------------- +vtkOpenGLContextDevice2D::~vtkOpenGLContextDevice2D() +{ + this->TextRenderer->Delete(); + this->TextRenderer = 0; + delete this->Storage; + this->Storage = 0; +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::Begin(vtkViewport* viewport) +{ + int size[2]; + size[0] = viewport->GetSize()[0]; + size[1] = viewport->GetSize()[1]; + double *vport = viewport->GetViewport(); + + // push a 2D matrix on the stack + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho( 0.5, size[0]+0.5, + 0.5, size[1]+0.5, + -1, 0); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + glDisable(GL_LIGHTING); + + glDepthMask(GL_FALSE); + + this->Renderer = vtkRenderer::SafeDownCast(viewport); + this->TextRenderer->SetRenderer(this->Renderer); + this->IsTextDrawn = false; + + vtkOpenGLRenderer *gl = vtkOpenGLRenderer::SafeDownCast(viewport); + if (gl) + { + vtkOpenGLRenderWindow *glWin = vtkOpenGLRenderWindow::SafeDownCast(gl->GetRenderWindow()); + if (glWin) + { + this->LoadExtensions(glWin->GetExtensionManager()); + } + } + + this->Modified(); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::End() +{ + if (this->IsTextDrawn) + { + this->TextRenderer->EndFrame(); + } +// push a 2D matrix on the stack + glMatrixMode( GL_PROJECTION); + glPopMatrix(); + glMatrixMode( GL_MODELVIEW); + glPopMatrix(); + glEnable( GL_LIGHTING); + + // Turn it back on in case we've turned it off + glDepthMask( GL_TRUE ); + + this->Modified(); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::DrawPoly(float *f, int n) +{ +/* if (f && n == 4) + { + float p[] = { f[0], f[1], 0.0, + f[2], f[3], 0.0, + f[4], f[5], 0.0, + f[6], f[7], 0.0 }; + glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &p[0]); + glEnable(GL_MAP1_VERTEX_3); + glBegin(GL_LINE_STRIP); + for (int i = 0; i <= 30; ++i) + { + glEvalCoord1f(float(i / 30.0)); + } + glEnd(); + glDisable(GL_MAP1_VERTEX_3); + this->DrawPoints(f, 4); + } +*/ + if(f && n > 0) + { + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, &f[0]); + glDrawArrays(GL_LINE_STRIP, 0, n); + glDisableClientState(GL_VERTEX_ARRAY); + } + else + { + vtkWarningMacro(<< "Points supplied that were not of type float."); + } +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::DrawPoints(float *f, int n) +{ + if (f && n > 0) + { + if (this->Storage->texture) + { + this->Storage->texture->Render(this->Renderer); + glEnable(vtkgl::POINT_SPRITE); + glTexEnvi(vtkgl::POINT_SPRITE, vtkgl::COORD_REPLACE, GL_TRUE); + vtkgl::PointParameteri(vtkgl::POINT_SPRITE_COORD_ORIGIN, vtkgl::LOWER_LEFT); + } + + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, &f[0]); + glDrawArrays(GL_POINTS, 0, n); + glDisableClientState(GL_VERTEX_ARRAY); + + if (this->Storage->texture) + { + glTexEnvi(vtkgl::POINT_SPRITE, vtkgl::COORD_REPLACE, GL_FALSE); + glDisable(vtkgl::POINT_SPRITE); + this->Storage->texture->PostRender(this->Renderer); + glDisable(GL_TEXTURE_2D); + } + } + else + { + vtkWarningMacro(<< "Points supplied that were not of type float."); + } +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::DrawQuad(float *f, int n) +{ + if (f && n > 0) + { + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, f); + glDrawArrays(GL_QUADS, 0, n); + glDisableClientState(GL_VERTEX_ARRAY); + } + else + { + vtkWarningMacro(<< "Points supplied that were not of type float."); + } +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::DrawText(float *point, vtkTextProperty *prop, + const vtkStdString &string) +{ + if (!this->IsTextDrawn) + { + this->IsTextDrawn = true; + this->TextRenderer->StartFrame(); + } + + int p[] = { point[0], point[1] }; + this->TextRenderer->RenderLabel(&p[0], prop, string); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::DrawImage(float *p, int n, vtkImageData *image) +{ + vtkTexture *tex =vtkTexture::New(); + tex->SetInput(image); + tex->Render(this->Renderer); + int *extent = image->GetExtent(); + float points[] = { p[0] , p[1], + p[0]+extent[1], p[1], + p[0]+extent[1], p[1]+extent[3], + p[0] , p[1]+extent[3] }; + + float texCoord[] = { 0.0, 0.0, + 1.0, 0.0, + 1.0, 1.0, + 0.0, 1.0 }; + + glColor4ub(255, 255, 255, 255); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, &points[0]); + glTexCoordPointer(2, GL_FLOAT, 0, &texCoord[0]); + glDrawArrays(GL_QUADS, 0, 4); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + tex->PostRender(this->Renderer); + glDisable(GL_TEXTURE_2D); + tex->Delete(); +} + +//----------------------------------------------------------------------------- +unsigned int vtkOpenGLContextDevice2D::AddPointSprite(vtkImageData *image) +{ + this->Storage->texture = vtkTexture::New(); + this->Storage->texture->SetInput(image); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::SetColor4(unsigned char *color) +{ + glColor4ubv(color); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::SetColor(unsigned char *color) +{ + glColor3ubv(color); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::SetPointSize(float size) +{ + glPointSize(size); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::SetLineWidth(float width) +{ + glLineWidth(width); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::SetViewExtents(float *x) +{ + glLoadIdentity(); + glOrtho( x[0], x[2], + x[1], x[3], + -1, 0); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::PushMatrix() +{ + glMatrixMode( GL_MODELVIEW ); + glPushMatrix(); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::PopMatrix() +{ + glMatrixMode( GL_MODELVIEW ); + glPopMatrix(); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::SetClipping(int *x) +{ + // Test the glScissor function + vtkDebugMacro(<< "Clipping area: " << x[0] << "\t" << x[1] + << "\t" << x[2] << "\t" << x[3]); + glScissor(x[0], x[1], x[2], x[3]); + glEnable(GL_SCISSOR_TEST); +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::DisableClipping() +{ + glDisable(GL_SCISSOR_TEST); +} + +//----------------------------------------------------------------------------- +bool vtkOpenGLContextDevice2D::LoadExtensions(vtkOpenGLExtensionManager *m) +{ + bool supportsGL15=m->ExtensionSupported("GL_VERSION_1_5"); + if(supportsGL15) + { + m->LoadExtension("GL_VERSION_1_5"); + } +} + +//----------------------------------------------------------------------------- +void vtkOpenGLContextDevice2D::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + os << indent << "Renderer: "; + if (this->Renderer) + { + os << endl; + this->Renderer->PrintSelf(os, indent.GetNextIndent()); + } + else + { + os << "(none)" << endl; + } + os << indent << "Text Renderer: "; + if (this->Renderer) + { + os << endl; + this->TextRenderer->PrintSelf(os, indent.GetNextIndent()); + } + else + { + os << "(none)" << endl; + } +} diff --git a/Charts/vtkOpenGLContextDevice2D.h b/Charts/vtkOpenGLContextDevice2D.h new file mode 100644 index 0000000000..3c949fdf87 --- /dev/null +++ b/Charts/vtkOpenGLContextDevice2D.h @@ -0,0 +1,157 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkOpenGLContextDevice2D.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 vtkOpenGLContextDevice2D - Class for drawing 2D primitives using OpenGL. +// +// .SECTION Description +// This class takes care of drawing the 2D primitives for the vtkContext2D class. +// In general this class should not be used directly, but called by vtkContext2D +// which takes care of many of the higher level details. + +#ifndef __vtkOpenGLContextDevice2D_h +#define __vtkOpenGLContextDevice2D_h + +#include "vtkContextDevice2D.h" + +class vtkWindow; +class vtkViewport; +class vtkRenderer; +class vtkLabelRenderStrategy; +class vtkOpenGLRenderWindow; +class vtkOpenGLExtensionManager; + +class VTK_CHARTS_EXPORT vtkOpenGLContextDevice2D : public vtkContextDevice2D +{ +public: + vtkTypeRevisionMacro(vtkOpenGLContextDevice2D, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Creates a 2D Painter object. + static vtkOpenGLContextDevice2D *New(); + + // Description: + // Draw a poly line using the vtkPoints2D - fastest code path due to memory + // layout of the coordinates. + virtual void DrawPoly(float *points, int n); + + // Description: + // Draw a poly line using the vtkPoints2D - fastest code path due to memory + // layout of the coordinates. + virtual void DrawPoints(float *points, int n); + + // Description: + // Draws a rectangle + virtual void DrawQuad(float *points, int n); + +//BTX + // Description: + // Draw some text to the screen! + virtual void DrawText(float *point, vtkTextProperty *tprop, + const vtkStdString &string); +//ETX + + // Description: + // Draw the supplied image at the given x, y (p[0], p[1]) location(s) (bottom corner). + void DrawImage(float *p, int n, vtkImageData *image); + + // Description: + // Experimentation with point sprites + virtual unsigned int AddPointSprite(vtkImageData *image); + + // Description: + // Set the color for the device using unsigned char of length 4, RGBA. + virtual void SetColor4(unsigned char *color); + + // Description: + // Set the color for the device using unsigned char of length 3, RGB. + virtual void SetColor(unsigned char *color); + + // Description: + // Set the point size for glyphs/sprites. + virtual void SetPointSize(float size); + + // Description: + // Set the line width for glyphs/sprites. + virtual void SetLineWidth(float width); + + // Description: + // Supply a float array of length 4 with x1, y1, x2, y2 specifying the extents + // of the display + virtual void SetViewExtents(float *x); + + // Description: + // Push the current matrix onto the stack. + virtual void PushMatrix(); + + // Description: + // Pop the current matrix off of the stack. + virtual void PopMatrix(); + + // Description: + // Supply an int array of length 4 with x1, y1, x2, y2 specifying clipping + // for the display. + virtual void SetClipping(int *x); + + // Description: + // Disable clipping of the display. + virtual void DisableClipping(); + + // Description: + // Begin drawing, pass in the viewport to set up the view. + virtual void Begin(vtkViewport* viewport); + + // Description: + // End drawing, clean up the view. + virtual void End(); + +//BTX +protected: + vtkOpenGLContextDevice2D(); + virtual ~vtkOpenGLContextDevice2D(); + + // Description: + // Store the width and height of the display devicen (in pixels). + int Geometry[2]; + + // Description: + // We need to store a pointer to the renderer for the text rendering + vtkRenderer *Renderer; + + // Description: + // We also need a label render strategy + vtkLabelRenderStrategy *TextRenderer; + + // Description: + // Store whether any text has been drawn to control Start frame end frame + bool IsTextDrawn; + + // Description: + // Private data pointer of the class + class Private; + Private *Storage; + + // Description: + // Load the OpenGL extensions we need. + bool LoadExtensions(vtkOpenGLExtensionManager *m); + +private: + vtkOpenGLContextDevice2D(const vtkOpenGLContextDevice2D &); // Not implemented. + void operator=(const vtkOpenGLContextDevice2D &); // Not implemented. + +//ETX +}; + +#endif //__vtkOpenGLContextDevice2D_h diff --git a/Charts/vtkPen.cxx b/Charts/vtkPen.cxx new file mode 100644 index 0000000000..59cbe0e048 --- /dev/null +++ b/Charts/vtkPen.cxx @@ -0,0 +1,128 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPen.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 "vtkPen.h" + +#include "vtkObjectFactory.h" + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkPen, "1.1"); + +//----------------------------------------------------------------------------- +vtkStandardNewMacro(vtkPen); + +//----------------------------------------------------------------------------- +vtkPen::vtkPen() +{ + this->Color[0] = 0; + this->Color[1] = 0; + this->Color[2] = 0; + this->Color[3] = 255; +} + +//----------------------------------------------------------------------------- +vtkPen::~vtkPen() +{ +} + +//----------------------------------------------------------------------------- +void vtkPen::SetColorF(double color[3]) +{ + this->Color[0] = color[0] * 255.0; + this->Color[1] = color[1] * 255.0; + this->Color[2] = color[2] * 255.0; +} + +//----------------------------------------------------------------------------- +void vtkPen::SetColorF(double r, double g, double b) +{ + this->Color[0] = r * 255.0; + this->Color[1] = g * 255.0; + this->Color[2] = b * 255.0; +} + +//----------------------------------------------------------------------------- +void vtkPen::SetColorF(double r, double g, double b, double a) +{ + this->Color[0] = r * 255.0; + this->Color[1] = g * 255.0; + this->Color[2] = b * 255.0; + this->Color[3] = a * 255.0; +} + +//----------------------------------------------------------------------------- +void vtkPen::SetOpacityF(double a) +{ + this->Color[3] = a * 255.0; +} + +//----------------------------------------------------------------------------- +void vtkPen::SetColor(unsigned char color[3]) +{ + this->Color[0] = color[0]; + this->Color[1] = color[1]; + this->Color[2] = color[2]; +} + +//----------------------------------------------------------------------------- +void vtkPen::SetColor(unsigned char r, unsigned char g, unsigned char b) +{ + this->Color[0] = r; + this->Color[1] = g; + this->Color[2] = b; +} + +//----------------------------------------------------------------------------- +void vtkPen::SetColor(unsigned char r, unsigned char g, unsigned char b, + unsigned char a) +{ + this->Color[0] = r; + this->Color[1] = g; + this->Color[2] = b; + this->Color[3] = a; +} + +//----------------------------------------------------------------------------- +void vtkPen::SetOpacity(unsigned char a) +{ + this->Color[3] = a; +} + +//----------------------------------------------------------------------------- +void vtkPen::GetColorF(double color[4]) +{ + for (int i = 0; i < 4; ++i) + { + color[i] = this->Color[i] / 255.0; + } +} + +//----------------------------------------------------------------------------- +void vtkPen::GetColor(unsigned char color[4]) +{ + for (int i = 0; i < 4; ++i) + { + color[i] = this->Color[i]; + } +} + +//----------------------------------------------------------------------------- +void vtkPen::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + os << indent << "Color: " << this->Color[0] << ", " << this->Color[1] + << ", " << this->Color[2] << ", " << this->Color[3] << endl; + os << indent << "Width: " << this->Width << endl; +} diff --git a/Charts/vtkPen.h b/Charts/vtkPen.h new file mode 100644 index 0000000000..da527d3671 --- /dev/null +++ b/Charts/vtkPen.h @@ -0,0 +1,114 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPen.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 vtkPen - provides a pen that draws the outlines of shapes drawn +// by vtkContext2D. +// +// .SECTION Description +// The vtkPen defines the outline of shapes that are drawn by vtkContext2D. +// The color is stored as four unsigned chars (RGBA), where the +// opacity defaults to 255, but can be modified separately to the other +// components. Ideally we would use a lightweight color class to store and pass +// around colors. + +#ifndef __vtkPen_h +#define __vtkPen_h + +#include "vtkObject.h" + +class VTK_CHARTS_EXPORT vtkPen : public vtkObject +{ +public: + vtkTypeRevisionMacro(vtkPen, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + static vtkPen *New(); + + // Description: + // Set the color of the brush with three component doubles (RGB), ranging from + // 0.0 to 1.0. + void SetColorF(double color[3]); + + // Description: + // Set the color of the brush with three component doubles (RGB), ranging from + // 0.0 to 1.0. + void SetColorF(double r, double g, double b); + + // Description: + // Set the color of the brush with four component doubles (RGBA), ranging from + // 0.0 to 1.0. + void SetColorF(double r, double g, double b, double a); + + // Description: + // Set the opacity with a double, ranging from 0.0 (transparent) to 1.0 + // (opaque). + void SetOpacityF(double a); + + // Description: + // Set the color of the brush with three component unsigned chars (RGB), + // ranging from 0 to 255. + void SetColor(unsigned char color[3]); + + // Description: + // Set the color of the brush with three component unsigned chars (RGB), + // ranging from 0 to 255. + void SetColor(unsigned char r, unsigned char g, unsigned char b); + + // Description: + // Set the color of the brush with four component unsigned chars (RGBA), + // ranging from 0 to 255. + void SetColor(unsigned char r, unsigned char g, unsigned char b, + unsigned char a); + + // Description: + // Set the opacity with an unsigned char, ranging from 0 (transparent) to 255 + // (opaque). + void SetOpacity(unsigned char a); + + // Description: + // Get the color of the brush - expects a double of length 4 to copy into. + void GetColorF(double color[4]); + + // Description: + // Get the color of the brush - expects an unsigned char of length 4. + void GetColor(unsigned char color[4]); + + // Description: + // Get the color of the brush - gives a pointer to the underlying data. + unsigned char * GetColor() { return &this->Color[0]; } + + // Description: + // Set/Get the width of the pen. + vtkSetMacro(Width, float); + vtkGetMacro(Width, float); + +//BTX +protected: + vtkPen(); + ~vtkPen(); + + // Storage of the color in RGBA format (0-255 per channel). + unsigned char Color[4]; + + // Store the width of the pen. + float Width; + +private: + vtkPen(const vtkPen &); // Not implemented. + void operator=(const vtkPen &); // Not implemented. +//ETX +}; + +#endif //__vtkPen_h diff --git a/Charts/vtkPlot.cxx b/Charts/vtkPlot.cxx new file mode 100644 index 0000000000..80fdc7b775 --- /dev/null +++ b/Charts/vtkPlot.cxx @@ -0,0 +1,110 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPlot.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 "vtkPlot.h" + +#include "vtkTable.h" +#include "vtkDataObject.h" +#include "vtkIdTypeArray.h" +#include "vtkContextMapper2D.h" +#include "vtkObjectFactory.h" + +#include "vtkStdString.h" + +vtkCxxRevisionMacro(vtkPlot, "1.1"); +vtkCxxSetObjectMacro(vtkPlot, Selection, vtkIdTypeArray); + +//----------------------------------------------------------------------------- +vtkPlot::vtkPlot() +{ + this->r = 0; + this->g = 0; + this->b = 0; + this->a = 0; + this->Width = 1.0; + this->Data = vtkContextMapper2D::New(); + this->Selection = NULL; +} + +//----------------------------------------------------------------------------- +vtkPlot::~vtkPlot() +{ + if (this->Data) + { + this->Data->Delete(); + this->Data = NULL; + } +} + +//----------------------------------------------------------------------------- +void vtkPlot::SetColor(unsigned char r, unsigned char g, unsigned char b, + unsigned char a) +{ + this->r = r; + this->g = g; + this->b = b; + this->a = a; +} + +//----------------------------------------------------------------------------- +void vtkPlot::SetInput(vtkTable *table) +{ + this->Data->SetInput(table); +} + +//----------------------------------------------------------------------------- +void vtkPlot::SetInput(vtkTable *table, const char *xColumn, + const char *yColumn) +{ + if (!xColumn || !yColumn) + { + vtkErrorMacro(<< "Called with null arguments for X or Y column.") + } + vtkDebugMacro(<< "Setting input, X column = \"" << vtkstd::string(xColumn) + << "\", " << "Y column = \"" << vtkstd::string(yColumn) << "\""); + + this->Data->SetInput(table); + this->Data->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_ROWS, + xColumn); + this->Data->SetInputArrayToProcess(1, 0, 0, vtkDataObject::FIELD_ASSOCIATION_ROWS, + yColumn); +} + +//----------------------------------------------------------------------------- +void vtkPlot::SetInput(vtkTable *table, vtkIdType xColumn, + vtkIdType yColumn) +{ + this->SetInput(table, + table->GetColumnName(xColumn), + table->GetColumnName(yColumn)); +} + +//----------------------------------------------------------------------------- +void vtkPlot::SetInputArray(int index, const char *name) +{ + this->Data->SetInputArrayToProcess(index, 0, 0, + vtkDataObject::FIELD_ASSOCIATION_ROWS, + name); +} + +//----------------------------------------------------------------------------- +void vtkPlot::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + // Print out our color and width + os << indent << "Color: " << this->r << ", " << this->g + << ", " << this->b << ", " << this->a << endl; + os << indent << "Width: " << this->Width << endl; +} diff --git a/Charts/vtkPlot.h b/Charts/vtkPlot.h new file mode 100644 index 0000000000..9d184e5d7c --- /dev/null +++ b/Charts/vtkPlot.h @@ -0,0 +1,111 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPlot.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 vtkPlot - Abstract class for 2D plots. +// +// .SECTION Description +// + +#ifndef __vtkPlot_h +#define __vtkPlot_h + +#include "vtkContextItem.h" + +class vtkVariant; +class vtkTable; +class vtkIdTypeArray; +class vtkContextMapper2D; + +class VTK_CHARTS_EXPORT vtkPlot : public vtkContextItem +{ +public: + vtkTypeRevisionMacro(vtkPlot, vtkContextItem); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Set the plot color + virtual void SetColor(unsigned char r, unsigned char g, unsigned char b, + unsigned char a); + + // Description: + // Set the width of the line. + vtkSetMacro(Width, float); + + // Description: + // Get the width of the line. + vtkGetMacro(Width, float); + + // Description: + // Set the plot label. + vtkSetStringMacro(Label); + + // Description: + // Get the plot label. + vtkGetStringMacro(Label); + + // Description: + // This is a convenience function to set the input table and the x, y column + // for the plot. + virtual void SetInput(vtkTable *table); + virtual void SetInput(vtkTable *table, const char *xColumn, + const char *yColumn); + void SetInput(vtkTable *table, vtkIdType xColumn, vtkIdType yColumn); + + // Description: + // Convenience function to set the input arrays. For most mappers index 0 + // is the x axis, and index 1 is the y axis. The name is the name of the + // column in the vtkTable. + virtual void SetInputArray(int index, const char *name); + + virtual void SetSelection(vtkIdTypeArray *id); + +//BTX + // Description: + // A General setter/getter that should be overridden. It can silently drop + // options, case is important + void SetProperty(const char *property, vtkVariant *var); + vtkVariant GetProperty(const char *property); +//ETX + +//BTX +protected: + vtkPlot(); + ~vtkPlot(); + + unsigned char r, g, b, a; + + float Width; + + // Description: + // Plot label, used by legend + char *Label; + + // Description: + // This data member contains the data that will be plotted, it inherits + // from vtkAlgorithm. + vtkContextMapper2D *Data; + + // Description: + // Selected indices for the table the plot is rendering + vtkIdTypeArray *Selection; + +private: + vtkPlot(const vtkPlot &); // Not implemented. + void operator=(const vtkPlot &); // Not implemented. + +//ETX +}; + +#endif //__vtkPlot_h diff --git a/Charts/vtkPlotGrid.cxx b/Charts/vtkPlotGrid.cxx new file mode 100644 index 0000000000..6d208146ed --- /dev/null +++ b/Charts/vtkPlotGrid.cxx @@ -0,0 +1,91 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPlotGrid.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 "vtkPlotGrid.h" + +#include "vtkContext2D.h" +#include "vtkPoints2D.h" +#include "vtkPen.h" +#include "vtkAxis.h" + +#include "vtkObjectFactory.h" + +//----------------------------------------------------------------------------- +vtkCxxRevisionMacro(vtkPlotGrid, "1.1"); +vtkCxxSetObjectMacro(vtkPlotGrid, XAxis, vtkAxis); +vtkCxxSetObjectMacro(vtkPlotGrid, YAxis, vtkAxis); +//----------------------------------------------------------------------------- +vtkStandardNewMacro(vtkPlotGrid); + +//----------------------------------------------------------------------------- +vtkPlotGrid::vtkPlotGrid() +{ + this->XAxis = NULL; + this->YAxis = NULL; + this->Point1[0] = 0.0; + this->Point1[1] = 0.0; + this->Point2[0] = 0.0; + this->Point2[1] = 0.0; +} + +//----------------------------------------------------------------------------- +vtkPlotGrid::~vtkPlotGrid() +{ + this->SetXAxis(NULL); + this->SetYAxis(NULL); +} + +//----------------------------------------------------------------------------- +bool vtkPlotGrid::Paint(vtkContext2D *painter) +{ + if (!this->XAxis || !this->YAxis) + { + // Need axes to define where our grid lines should be drawn + vtkDebugMacro(<<"No axes set and so grid lines cannot be drawn."); + return false; + } + float ignored; // Values I want to ignore when getting others + this->XAxis->GetPoint1(&this->Point1[0]); + this->XAxis->GetPoint2(this->Point2[0], ignored); + this->YAxis->GetPoint2(ignored, this->Point2[1]); + + // Now do some grid drawing... + painter->GetPen()->SetWidth(1.0); + + // in x + int xLines = this->XAxis->GetNumberOfTicks(); + float spacing = (this->Point2[0] - this->Point1[0]) / float(xLines-1); + for (int i = 0; i < xLines; ++i) + { + painter->DrawLine(int(this->Point1[0] + i*spacing), this->Point1[1], + int(this->Point1[0] + i*spacing), this->Point2[1]); + } + + // in y + int yLines = this->YAxis->GetNumberOfTicks(); + spacing = (this->Point2[1] - this->Point1[1]) / float(yLines-1); + for (int i = 0; i < yLines; ++i) + { + painter->DrawLine(this->Point1[0], int(this->Point1[1] + i*spacing), + this->Point2[0], int(this->Point1[1] + i*spacing)); + } +} + +//----------------------------------------------------------------------------- +void vtkPlotGrid::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + +} diff --git a/Charts/vtkPlotGrid.h b/Charts/vtkPlotGrid.h new file mode 100644 index 0000000000..b199689ac2 --- /dev/null +++ b/Charts/vtkPlotGrid.h @@ -0,0 +1,77 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPlotGrid.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 vtkPlotGrid - takes care of drawing the plot grid +// +// .SECTION Description +// The vtkPlotGrid is drawn in screen coordinates. It is usually one of the +// first elements of a chart to be drawn, and will generally be obscured +// by all other elements of the chart. It builds up its own plot locations +// from the parameters of the x and y axis of the plot. + +#ifndef __vtkPlotGrid_h +#define __vtkPlotGrid_h + +#include "vtkContextItem.h" + +class vtkStdString; +class vtkContext2D; +class vtkPoints2D; +class vtkAxis; + +class VTK_CHARTS_EXPORT vtkPlotGrid : public vtkContextItem +{ +public: + vtkTypeRevisionMacro(vtkPlotGrid, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Creates a 2D Chart object. + static vtkPlotGrid *New(); + + // Description: + // Set the X axis of the grid. + virtual void SetXAxis(vtkAxis *axis); + + // Description: + // Set the X axis of the grid. + virtual void SetYAxis(vtkAxis *axis); + + // Description: + // Paint event for the axis, called whenever the axis needs to be drawn + virtual bool Paint(vtkContext2D *painter); + +//BTX +protected: + vtkPlotGrid(); + ~vtkPlotGrid(); + + // Description: + // The vtkAxis objects are used to figure out where the grid lines should be + // drawn. + vtkAxis *XAxis; + vtkAxis *YAxis; + + // These variables are not publicly accessible - cached for convenience + float Point1[2]; // The position of the grid origin + float Point2[2]; // Maximum positions in x and y (top corner of the grid + +private: + vtkPlotGrid(const vtkPlotGrid &); // Not implemented. + void operator=(const vtkPlotGrid &); // Not implemented. +//ETX +}; + +#endif //__vtkPlotGrid_h diff --git a/Charts/vtkPlotLine.cxx b/Charts/vtkPlotLine.cxx new file mode 100644 index 0000000000..60547515e4 --- /dev/null +++ b/Charts/vtkPlotLine.cxx @@ -0,0 +1,205 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPlotLine.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 "vtkPlotLine.h" + +#include "vtkContext2D.h" +#include "vtkPen.h" +#include "vtkContextDevice2D.h" +#include "vtkContextMapper2D.h" +#include "vtkPoints2D.h" +#include "vtkTable.h" +#include "vtkFloatArray.h" +#include "vtkDoubleArray.h" +#include "vtkIdTypeArray.h" +#include "vtkExecutive.h" +#include "vtkTimeStamp.h" +#include "vtkInformation.h" + +#include "vtkObjectFactory.h" + +vtkCxxRevisionMacro(vtkPlotLine, "1.1"); + +//----------------------------------------------------------------------------- +vtkStandardNewMacro(vtkPlotLine); + +//----------------------------------------------------------------------------- +vtkPlotLine::vtkPlotLine() +{ + this->Points = 0; + this->Label = 0; +} + +//----------------------------------------------------------------------------- +vtkPlotLine::~vtkPlotLine() +{ + delete this->Label; +} + +//----------------------------------------------------------------------------- +bool vtkPlotLine::Paint(vtkContext2D *painter) +{ + // This is where everything should be drawn, or dispatched to other methods. + vtkDebugMacro(<< "Paint event called in vtkPlotLine."); + + // First check if we have an input + vtkTable *table = this->Data->GetInput(); + if (!table) + { + vtkDebugMacro(<< "Paint event called with no input table set."); + return false; + } + else if(this->GetMTime() > this->BuildTime || + table->GetMTime() > this->BuildTime) + { + vtkDebugMacro(<< "Paint event called with outdated table cache. Updating."); + this->UpdateTableCache(table); + } + + // Now add some decorations for our selected points... + if (this->Selection) + { + vtkDebugMacro(<<"Selection set " << this->Selection->GetNumberOfTuples()); + for (int i = 0; i < this->Selection->GetNumberOfTuples(); ++i) + { + painter->GetPen()->SetWidth(this->Width*15.0); + painter->GetPen()->SetColor(0, 0, 255, 255); + vtkIdType id = 0; + this->Selection->GetTupleValue(i, &id); + if (id < this->Points->GetNumberOfPoints()) + { + double *point = this->Points->GetPoint(id); + painter->DrawPoint(point[0], point[1]); + } + } + } + else + { + vtkDebugMacro("No selectionn set."); + } + + // Now to plot the points + if (this->Points) + { + painter->GetPen()->SetColor(this->r, this->g, this->b, this->a); + painter->GetPen()->SetWidth(this->Width); + painter->DrawPoly(this->Points); + } + + return true; +} + +//----------------------------------------------------------------------------- +void vtkPlotLine::GetBounds(double bounds[4]) +{ + // Get the x and y arrays (index 0 and 1 respectively) + vtkTable *table = this->Data->GetInput(); + vtkDataArray *x = this->Data->GetInputArrayToProcess(0, table); + vtkDataArray *y = this->Data->GetInputArrayToProcess(1, table); + + if (x && y) + { + x->GetRange(&bounds[0]); + y->GetRange(&bounds[2]); + } + vtkDebugMacro(<< "Bounds: " << bounds[0] << "\t" << bounds[1] << "\t" + << bounds[2] << "\t" << bounds[3]); +} + +//----------------------------------------------------------------------------- +template +void CopyToPoints(vtkPoints2D *points, A *a, B *b, int n) +{ + points->SetNumberOfPoints(n); + for (int i = 0; i < n; ++i) + { + points->SetPoint(i, a[i], b[i]); + } +} + +//----------------------------------------------------------------------------- +bool vtkPlotLine::UpdateTableCache(vtkTable *table) +{ + // Get the x and y arrays (index 0 and 1 respectively) + vtkAbstractArray *x = this->Data->GetInputAbstractArrayToProcess(0, table); + vtkAbstractArray *y = this->Data->GetInputAbstractArrayToProcess(1, table); + if (!x) + { + vtkErrorMacro(<< "No X column is set (index 0)."); + return false; + } + else if (!y) + { + vtkErrorMacro(<< "No Y column is set (index 1)."); + return false; + } + else if (x->GetSize() != y->GetSize()) + { + vtkErrorMacro("The x and y columns must have the same number of elements."); + return false; + } + + if (!this->Points) + { + this->Points = vtkPoints2D::New(); + } + + // Figure out the type and copy to our points + if (x->IsA("vtkFloatArray") && y->IsA("vtkFloatArray")) + { + CopyToPoints(this->Points, + vtkFloatArray::SafeDownCast(x)->GetPointer(0), + vtkFloatArray::SafeDownCast(y)->GetPointer(0), + x->GetSize()); + this->BuildTime.Modified(); + double bounds[4]; + this->GetBounds(&bounds[0]); + } + else if (x->IsA("vtkDoubleArray") && y->IsA("vtkDoubleArray")) + { + CopyToPoints(this->Points, + vtkDoubleArray::SafeDownCast(x)->GetPointer(0), + vtkDoubleArray::SafeDownCast(y)->GetPointer(0), + x->GetSize()); + this->BuildTime.Modified(); + double bounds[4]; + this->GetBounds(&bounds[0]); + } + else + { + vtkErrorMacro(<< "Error the x or y array was not a valid type." + << endl << x->GetClassName() + << "\t" << y->GetClassName()); + } +} + +//----------------------------------------------------------------------------- +void vtkPlotLine::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + // Print some details about this plot + os << indent << "Label: "; + if (this->Label) + { + os << "\"" << *this->Label << "\"" << endl; + } + else + { + os << "(none)" << endl; + } + os << indent << "Line Width: " << this->Width << endl; + os << indent << "Color: " << this->r << ", " << this->g + << ", " << this->b << ", " << this->a << endl; +} diff --git a/Charts/vtkPlotLine.h b/Charts/vtkPlotLine.h new file mode 100644 index 0000000000..d15f2ae764 --- /dev/null +++ b/Charts/vtkPlotLine.h @@ -0,0 +1,74 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPlotLine.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 vtkPlotLine - Class for drawing an XY plot given two columns from a +// vtkTable. +// +// .SECTION Description +// + +#ifndef __vtkPlotLine_h +#define __vtkPlotLine_h + +#include "vtkPlot.h" + +class vtkContext2D; +class vtkTable; +class vtkPoints2D; +class vtkStdString; + +class VTK_CHARTS_EXPORT vtkPlotLine : public vtkPlot +{ +public: + vtkTypeRevisionMacro(vtkPlotLine, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Creates a 2D Chart object. + static vtkPlotLine *New(); + + // Description: + // Paint event for the XY plot, called whenever the chart needs to be drawn + virtual bool Paint(vtkContext2D *painter); + + // Description: + // Get the bounds for this mapper as (Xmin,Xmax,Ymin,Ymax,Zmin,Zmax). + virtual void GetBounds(double bounds[4]); + +//BTX +protected: + vtkPlotLine(); + ~vtkPlotLine(); + + // Description: + // Update the table cache. + bool UpdateTableCache(vtkTable *table); + + // Description: + // Store a well packed set of XY coordinates for this data series. + vtkPoints2D *Points; + + // Description: + // The point cache is marked dirty until it has been initialized. + vtkTimeStamp BuildTime; + +private: + vtkPlotLine(const vtkPlotLine &); // Not implemented. + void operator=(const vtkPlotLine &); // Not implemented. + +//ETX +}; + +#endif //__vtkPlotLine_h diff --git a/Charts/vtkPlotPoints.cxx b/Charts/vtkPlotPoints.cxx new file mode 100644 index 0000000000..643db1fb48 --- /dev/null +++ b/Charts/vtkPlotPoints.cxx @@ -0,0 +1,205 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPlotPoints.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 "vtkPlotPoints.h" + +#include "vtkContext2D.h" +#include "vtkPen.h" +#include "vtkContextDevice2D.h" +#include "vtkContextMapper2D.h" +#include "vtkPoints2D.h" +#include "vtkTable.h" +#include "vtkFloatArray.h" +#include "vtkDoubleArray.h" +#include "vtkIdTypeArray.h" +#include "vtkExecutive.h" +#include "vtkTimeStamp.h" +#include "vtkInformation.h" + +#include "vtkObjectFactory.h" + +vtkCxxRevisionMacro(vtkPlotPoints, "1.1"); + +//----------------------------------------------------------------------------- +vtkStandardNewMacro(vtkPlotPoints); + +//----------------------------------------------------------------------------- +vtkPlotPoints::vtkPlotPoints() +{ + this->Points = 0; + this->Label = 0; +} + +//----------------------------------------------------------------------------- +vtkPlotPoints::~vtkPlotPoints() +{ + delete this->Label; +} + +//----------------------------------------------------------------------------- +bool vtkPlotPoints::Paint(vtkContext2D *painter) +{ + // This is where everything should be drawn, or dispatched to other methods. + vtkDebugMacro(<< "Paint event called in vtkPlotPoints."); + + // First check if we have an input + vtkTable *table = this->Data->GetInput(); + if (!table) + { + vtkDebugMacro(<< "Paint event called with no input table set."); + return false; + } + else if(this->GetMTime() > this->BuildTime || + table->GetMTime() > this->BuildTime) + { + vtkDebugMacro(<< "Paint event called with outdated table cache. Updating."); + this->UpdateTableCache(table); + } + + // Now add some decorations for our selected points... + if (this->Selection) + { + vtkDebugMacro(<<"Selection set " << this->Selection->GetNumberOfTuples()); + for (int i = 0; i < this->Selection->GetNumberOfTuples(); ++i) + { + painter->GetPen()->SetWidth(this->Width*15.0); + painter->GetPen()->SetColor(0, 0, 255, 255); + vtkIdType id = 0; + this->Selection->GetTupleValue(i, &id); + if (id < this->Points->GetNumberOfPoints()) + { + double *point = this->Points->GetPoint(id); + painter->DrawPoint(point[0], point[1]); + } + } + } + else + { + vtkDebugMacro("No selectionn set."); + } + + // Now to plot the points + if (this->Points) + { + painter->GetPen()->SetColor(this->r, this->g, this->b, this->a); + painter->GetPen()->SetWidth(this->Width); + painter->DrawPoints(this->Points); + } + + return true; +} + +//----------------------------------------------------------------------------- +void vtkPlotPoints::GetBounds(double bounds[4]) +{ + // Get the x and y arrays (index 0 and 1 respectively) + vtkTable *table = this->Data->GetInput(); + vtkDataArray *x = this->Data->GetInputArrayToProcess(0, table); + vtkDataArray *y = this->Data->GetInputArrayToProcess(1, table); + + if (x && y) + { + x->GetRange(&bounds[0]); + y->GetRange(&bounds[2]); + } + vtkDebugMacro(<< "Bounds: " << bounds[0] << "\t" << bounds[1] << "\t" + << bounds[2] << "\t" << bounds[3]); +} + +//----------------------------------------------------------------------------- +template +void CopyToPoints(vtkPoints2D *points, A *a, B *b, int n) +{ + points->SetNumberOfPoints(n); + for (int i = 0; i < n; ++i) + { + points->SetPoint(i, a[i], b[i]); + } +} + +//----------------------------------------------------------------------------- +bool vtkPlotPoints::UpdateTableCache(vtkTable *table) +{ + // Get the x and y arrays (index 0 and 1 respectively) + vtkAbstractArray *x = this->Data->GetInputAbstractArrayToProcess(0, table); + vtkAbstractArray *y = this->Data->GetInputAbstractArrayToProcess(1, table); + if (!x) + { + vtkErrorMacro(<< "No X column is set (index 0)."); + return false; + } + else if (!y) + { + vtkErrorMacro(<< "No Y column is set (index 1)."); + return false; + } + else if (x->GetSize() != y->GetSize()) + { + vtkErrorMacro("The x and y columns must have the same number of elements."); + return false; + } + + if (!this->Points) + { + this->Points = vtkPoints2D::New(); + } + + // Figure out the type and copy to our points + if (x->IsA("vtkFloatArray") && y->IsA("vtkFloatArray")) + { + CopyToPoints(this->Points, + vtkFloatArray::SafeDownCast(x)->GetPointer(0), + vtkFloatArray::SafeDownCast(y)->GetPointer(0), + x->GetSize()); + this->BuildTime.Modified(); + double bounds[4]; + this->GetBounds(&bounds[0]); + } + else if (x->IsA("vtkDoubleArray") && y->IsA("vtkDoubleArray")) + { + CopyToPoints(this->Points, + vtkDoubleArray::SafeDownCast(x)->GetPointer(0), + vtkDoubleArray::SafeDownCast(y)->GetPointer(0), + x->GetSize()); + this->BuildTime.Modified(); + double bounds[4]; + this->GetBounds(&bounds[0]); + } + else + { + vtkErrorMacro(<< "Error the x or y array was not a valid type." + << endl << x->GetClassName() + << "\t" << y->GetClassName()); + } +} + +//----------------------------------------------------------------------------- +void vtkPlotPoints::PrintSelf(ostream &os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); + // Print some details about this plot + os << indent << "Label: "; + if (this->Label) + { + os << "\"" << *this->Label << "\"" << endl; + } + else + { + os << "(none)" << endl; + } + os << indent << "Point size: " << this->Width << endl; + os << indent << "Color: " << this->r << ", " << this->g + << ", " << this->b << ", " << this->a << endl; +} diff --git a/Charts/vtkPlotPoints.h b/Charts/vtkPlotPoints.h new file mode 100644 index 0000000000..0a39df5c7e --- /dev/null +++ b/Charts/vtkPlotPoints.h @@ -0,0 +1,74 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkPlotPoints.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 vtkPlotPoints - Class for drawing an XY plot given two columns from a +// vtkTable. +// +// .SECTION Description +// + +#ifndef __vtkPlotPoints_h +#define __vtkPlotPoints_h + +#include "vtkPlot.h" + +class vtkContext2D; +class vtkTable; +class vtkPoints2D; +class vtkStdString; + +class VTK_CHARTS_EXPORT vtkPlotPoints : public vtkPlot +{ +public: + vtkTypeRevisionMacro(vtkPlotPoints, vtkObject); + virtual void PrintSelf(ostream &os, vtkIndent indent); + + // Description: + // Creates a 2D Chart object. + static vtkPlotPoints *New(); + + // Description: + // Paint event for the XY plot, called whenever the chart needs to be drawn + virtual bool Paint(vtkContext2D *painter); + + // Description: + // Get the bounds for this mapper as (Xmin,Xmax,Ymin,Ymax,Zmin,Zmax). + virtual void GetBounds(double bounds[4]); + +//BTX +protected: + vtkPlotPoints(); + ~vtkPlotPoints(); + + // Description: + // Update the table cache. + bool UpdateTableCache(vtkTable *table); + + // Description: + // Store a well packed set of XY coordinates for this data series. + vtkPoints2D *Points; + + // Description: + // The point cache is marked dirty until it has been initialized. + vtkTimeStamp BuildTime; + +private: + vtkPlotPoints(const vtkPlotPoints &); // Not implemented. + void operator=(const vtkPlotPoints &); // Not implemented. + +//ETX +}; + +#endif //__vtkPlotPoints_h diff --git a/Common/vtkWin32Header.h b/Common/vtkWin32Header.h index 48af77e972..6af2efb5ce 100644 --- a/Common/vtkWin32Header.h +++ b/Common/vtkWin32Header.h @@ -247,6 +247,12 @@ Do_not_include_vtkWin32Header_directly__vtkSystemIncludes_includes_it; #define VTK_VIEWS_EXPORT __declspec( dllimport ) #endif + #if defined(vtkCharts_EXPORTS) + #define VTK_CHARTS_EXPORT __declspec( dllexport ) + #else + #define VTK_CHARTS_EXPORT __declspec( dllimport ) + #endif + #else #define VTK_COMMON_EXPORT #define VTK_FILTERING_EXPORT @@ -264,6 +270,7 @@ Do_not_include_vtkWin32Header_directly__vtkSystemIncludes_includes_it; #define VTK_WIDGETS_EXPORT #define VTK_PARALLEL_EXPORT #define VTK_VIEWS_EXPORT + #define VTK_CHARTS_EXPORT #define VTK_EXPORT #endif diff --git a/vtkIncludeDirectories.cmake b/vtkIncludeDirectories.cmake index 28fd81f842..440e4c441d 100644 --- a/vtkIncludeDirectories.cmake +++ b/vtkIncludeDirectories.cmake @@ -130,6 +130,8 @@ IF(VTK_USE_RENDERING) SET(VTK_INCLUDE_DIRS_SOURCE_TREE ${VTK_INCLUDE_DIRS_SOURCE_TREE} ${VTK_SOURCE_DIR}/Widgets) SET(VTK_INCLUDE_DIRS_SOURCE_TREE ${VTK_INCLUDE_DIRS_SOURCE_TREE} ${VTK_SOURCE_DIR}/Rendering) SET(VTK_INCLUDE_DIRS_BUILD_TREE ${VTK_INCLUDE_DIRS_BUILD_TREE} ${VTK_BINARY_DIR}/Rendering) + SET(VTK_INCLUDE_DIRS_SOURCE_TREE ${VTK_INCLUDE_DIRS_SOURCE_TREE} ${VTK_SOURCE_DIR}/Charts) + SET(VTK_INCLUDE_DIRS_BUILD_TREE ${VTK_INCLUDE_DIRS_BUILD_TREE} ${VTK_BINARY_DIR}/Charts) # Access to vtkRegressionTestImage.h. SET(VTK_INCLUDE_DIRS_SOURCE_TREE ${VTK_INCLUDE_DIRS_SOURCE_TREE} ${VTK_SOURCE_DIR}/Rendering/Testing/Cxx -- GitLab