1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: TestChartXYZ.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15
16 #include "vtkAxis.h"
17 #include "vtkChartXYZ.h"
18 #include "vtkContextView.h"
19 #include "vtkContextScene.h"
20 #include "vtkFloatArray.h"
21 #include "vtkPlotPoints3D.h"
22
23 #include "vtkRenderer.h"
24 #include "vtkRenderWindow.h"
25 #include "vtkRenderWindowInteractor.h"
26 #include "vtkNew.h"
27 #include "vtkTable.h"
28 #include "vtkCallbackCommand.h"
29
30 // Need a timer so that we can animate, and then take a snapshot!
31 namespace
32 {
33 static double angle = 0;
34
ProcessEvents(vtkObject * caller,unsigned long,void * clientData,void * callerData)35 void ProcessEvents(vtkObject *caller, unsigned long,
36 void *clientData, void *callerData)
37 {
38 vtkChartXYZ *chart = reinterpret_cast<vtkChartXYZ *>(clientData);
39 vtkRenderWindowInteractor *interactor =
40 reinterpret_cast<vtkRenderWindowInteractor *>(caller);
41 angle += 2;
42 chart->SetAngle(angle);
43 interactor->Render();
44 if (angle >= 90)
45 {
46 int timerId = *reinterpret_cast<int *>(callerData);
47 interactor->DestroyTimer(timerId);
48 }
49 }
50 } // End of anonymous namespace.
51
TestChartXYZ(int,char * [])52 int TestChartXYZ(int , char * [])
53 {
54 // Now the chart
55 vtkNew<vtkChartXYZ> chart;
56 chart->SetAutoRotate(true);
57 chart->SetFitToScene(false);
58 chart->SetDecorateAxes(false);
59 vtkNew<vtkContextView> view;
60 view->GetRenderWindow()->SetSize(400, 300);
61 view->GetScene()->AddItem(chart);
62 vtkNew<vtkChartXYZ> chart2;
63 chart2->SetAutoRotate(true);
64 chart2->SetFitToScene(false);
65 chart->SetDecorateAxes(false);
66 view->GetScene()->AddItem(chart2);
67
68 chart->SetGeometry(vtkRectf(75.0, 20.0, 250, 260));
69 chart2->SetGeometry(vtkRectf(75.0, 20.0, 250, 260));
70
71 // Create a table with some points in it...
72 vtkNew<vtkTable> table;
73 vtkNew<vtkFloatArray> arrX;
74 arrX->SetName("X Axis");
75 table->AddColumn(arrX);
76 vtkNew<vtkFloatArray> arrC;
77 arrC->SetName("Cosine");
78 table->AddColumn(arrC);
79 vtkNew<vtkFloatArray> arrS;
80 arrS->SetName("Sine");
81 table->AddColumn(arrS);
82 // Test charting with a few more points...
83 int numPoints = 69;
84 float inc = 7.5 / (numPoints-1);
85 table->SetNumberOfRows(numPoints);
86 for (int i = 0; i < numPoints; ++i)
87 {
88 table->SetValue(i, 0, i * inc);
89 table->SetValue(i, 1, cos(i * inc) + 0.0);
90 table->SetValue(i, 2, sin(i * inc) + 0.0);
91 }
92
93 //chart->SetAroundX(true);
94 // Add the three dimensions we are interested in visualizing.
95 vtkNew<vtkPlotPoints3D> plot;
96 plot->SetInputData(table, "X Axis", "Sine", "Cosine");
97 chart->AddPlot(plot);
98 const vtkColor4ub axisColor(20, 200, 30);
99 chart->SetAxisColor(axisColor);
100 chart->GetAxis(0)->SetUnscaledRange(-0.1,7.6);
101 chart->GetAxis(1)->SetUnscaledRange(-1.1,1.1);
102 chart->GetAxis(2)->SetUnscaledRange(-1.1,1.1);
103 chart->RecalculateTransform();
104
105 // We want a duplicate, that does not move.
106 vtkNew<vtkPlotPoints3D> plot2;
107 plot2->SetInputData(table, "X Axis", "Sine", "Cosine");
108 chart2->AddPlot(plot2);
109
110 view->GetRenderWindow()->SetMultiSamples(0);
111 view->GetInteractor()->Initialize();
112
113 // Set up the timer, and be sure to incrememt the angle.
114 vtkNew<vtkCallbackCommand> callback;
115 callback->SetClientData(chart);
116 callback->SetCallback(::ProcessEvents);
117 view->GetInteractor()->AddObserver(vtkCommand::TimerEvent, callback,
118 0);
119 view->GetInteractor()->CreateRepeatingTimer(1000 / 25);
120
121 view->GetInteractor()->Start();
122
123 return EXIT_SUCCESS;
124 }
125