rock_widget_collection  0.1
Plot2d.cc
Go to the documentation of this file.
1 
2 #include "Plot2d.h"
3 #include <stdexcept>
4 #include <sstream>
5 #include <QtCore/QtPlugin>
6 #include <QMouseEvent>
7 
8 #include <vtkSphereSource.h>
9 #include <vtkPolyDataMapper.h>
10 #include <vtkActor.h>
11 #include <vtkImageViewer.h>
12 #include <vtkInteractorStyleImage.h>
13 #include <vtkJPEGReader.h>
14 
15 #include <vtkMath.h>
16 #include <vtkPolyData.h>
17 
18 #include <vtkRenderWindowInteractor.h>
19 #include <vtkXYPlotWidget.h>
20 
21 const double c_norm = 1.0/255;
22 
23 Plot2d::Plot2d(QWidget *parent):
24  QVTKWidget(parent),auto_scroll(true)
25 {
26  vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
27  SetRenderWindow(renderWindow);
28 
29  text = vtkSmartPointer<vtkTextActor>::New();
30  text->SetInput("0/0");
31  text->SetDisplayPosition(5,1);
32 
33  plot = vtkSmartPointer<vtkXYPlotActor>::New();
34  plot->ExchangeAxesOff();
35  plot->SetXValuesToValue();
36  plot->SetWidth(0.9);
37  plot->SetHeight(1.0);
38  plot->SetPosition(0.04,0);
39  plot->LegendOn();
40  plot->PlotPointsOn();
41  plot->PlotLinesOn();
42  addPlot(0);
43 
44  renderer = vtkSmartPointer<vtkRenderer>::New();
45  renderer->AddActor(plot);
46  renderer->AddActor(text);
47  renderer->ResetCamera();
48  renderer->SetBackground(0.1,0.1,0.1);
49  renderWindow->AddRenderer(renderer);
50 
51  vtkSmartPointer<vtkXYPlotWidget> widget = vtkSmartPointer<vtkXYPlotWidget>::New();
52  widget->SetXYPlotActor(plot);
53  GetRenderWindow()->GetInteractor()->SetInteractorStyle(widget);
54 }
55 
56 Plot2d::~Plot2d(){}
57 void Plot2d::setTitle(QString title){plot->SetTitle(title.toStdString().c_str());}
58 void Plot2d::setXTitle(QString title){plot->SetXTitle(title.toStdString().c_str());}
59 void Plot2d::setYTitle(QString title){plot->SetYTitle(title.toStdString().c_str());}
60 void Plot2d::setStyle(int id, QString style){}
61 void Plot2d::setColor(int id, int r, int g, int b){addPlot(id);plot->SetPlotColor(id,c_norm*r,c_norm*g,c_norm*b);}
62 void Plot2d::setXRange(double min, double max){plot->SetXRange(min,max);}
63 void Plot2d::setYRange(double min, double max){plot->SetYRange(min,max);}
64 void Plot2d::setPlotLabel(int id, QString label){addPlot(id);plot->SetPlotLabel(id,label.toStdString().c_str());}
65 void Plot2d::showLegend(bool on){if(on){plot->LegendOn();}else{plot->LegendOff();}}
66 void Plot2d::showPlotPoints(bool on){if(on){plot->PlotPointsOn();}else{plot->PlotPointsOff();}}
67 void Plot2d::showPlotLines(bool on){if(on){plot->PlotLinesOn();}else{plot->PlotLinesOff();}}
68 void Plot2d::setNumberOfXLabels(int i){plot->SetNumberOfXLabels(i);}
69 void Plot2d::setNumberOfYLabels(int i){plot->SetNumberOfYLabels(i);}
70 void Plot2d::autoScroll(bool on){auto_scroll = on;}
71 
72 //not supported by vtk 5.2
73 //void Plot2d::showReferenceXLine(bool on){if(on){plot->ShowReferenceXLineOn();}else{plot->ShowReferenceXLineOff();}}
74 //void Plot2d::showReferenceYLine(bool on){if(on){plot->ShowReferenceYLineOn();}else{plot->ShowReferenceYLineOff();}}
75 //void Plot2d::setReferenceXValue(double val){plot->SetReferenceXValue(val);}
76 //void Plot2d::setReferenceYValue(double val){plot->SetReferenceYValue(val);}
77 
78 void Plot2d::clear(int id)
79 {
80  if(!isIdValid(id))
81  return;
82  getField(id)->Reset();
83 }
84 
85 void Plot2d::scrollTo(double x, double y)
86 {
87  double* xrange = plot->GetXRange();
88  double width = (xrange[1]-xrange[0]);
89  double* yrange = plot->GetYRange();
90  double height = (yrange[1]-yrange[0]);
91  setXRange(x-width*0.8,x+width*0.2);
92  setYRange(y-height*0.8,y+height*0.2);
93 }
94 
95 void Plot2d::addPoint(int id, double x,double y)
96 {
97  field_t field = getField(id);
98  array_t array = (vtkDoubleArray*)field->GetArray(0);
99  array_t array2 = (vtkDoubleArray*)field->GetArray(1);
100  array->InsertNextValue(x);
101  array2->InsertNextValue(y);
102  array->Modified();
103  array2->Modified();
104  if(auto_scroll)
105  scrollTo(x,y);
106 }
107 
108 void Plot2d::addPoints(int id, const QList<double> &x, const QList<double> &y)
109 {
110  if(x.size() != y.size())
111  std::runtime_error("addValues: Size mismatch. Both lists must have the same size.");
112 
113  if(x.size() == 0)
114  return;
115 
116  field_t field = getField(id);
117  array_t array = (vtkDoubleArray*)field->GetArray(0);
118  array_t array2 = (vtkDoubleArray*)field->GetArray(1);
119 
120  QList<double>::const_iterator iter_x = x.constBegin();
121  QList<double>::const_iterator iter_y = y.constBegin();
122  for(;iter_x != x.constEnd();++iter_x,++iter_y)
123  {
124  array->InsertNextValue(*iter_x);
125  array2->InsertNextValue(*iter_y);
126  }
127  array->Modified();
128  array2->Modified();
129  if(auto_scroll)
130  scrollTo(*(--iter_x),*(--iter_y));
131 }
132 
133 void Plot2d::setXValues(int id)
134 {
135  if(isIdValid(id))
136  plot->SetXValues(id);
137  else
138  plot->SetXValuesToIndex();
139 }
140 
141 bool Plot2d::isIdValid(int id)
142 {
143  if (id >=0 && (unsigned int)id < field_data.size())
144  return true;
145  return false;
146 }
147 
149 {
150 // data2->Update();
151 // cachedImageDirty();
152 // GetRenderWindow()->GetInteractor()->Render();
153 // update();
154 }
155 
157 //protected methods
159 
160 void Plot2d::mouseMoveEvent(QMouseEvent *event)
161 {
162  QVTKWidget::mouseMoveEvent(event);
163 
164  int x,y;
165  double x2,y2;
166  std::stringstream ss;
167 
168  GetRenderWindow()->GetInteractor()->GetLastEventPosition(x,y);
169  plot->SetViewportCoordinate(x,y);
170  plot->ViewportToPlotCoordinate(renderer);
171  plot->GetViewportCoordinate(x2,y2);
172 
173  ss << x2 << " / " << y2 << "\n";
174  text->SetInput(ss.str().c_str());
175  text->Modified();
176  update();
177 }
178 
179 void Plot2d::addPlot(int id)
180 {
181  if(id < 0)
182  throw std::runtime_error("addPlot: Bad id. Id must be positive.");
183 
184  while((unsigned int)id >= field_data.size())
185  {
186  array_t array = vtkSmartPointer<vtkDoubleArray>::New();
187  array_t array2 = vtkSmartPointer<vtkDoubleArray>::New();
188  field_t field = vtkSmartPointer<vtkFieldData>::New();
189  vtkSmartPointer<vtkDataObject> data = vtkSmartPointer<vtkDataObject>::New();
190 
191  field->AddArray(array);
192  field->AddArray(array2);
193  data->SetFieldData(field);
194  plot->AddDataObjectInput(data);
195  plot->SetDataObjectXComponent(id,0);
196  plot->SetDataObjectYComponent(id,1);
197  field_data.push_back(field);
198  }
199 }
200 
202 {
203  addPlot(id);
204  return field_data[id];
205 }
206 
void addPoint(int id, double x, double y)
Definition: Plot2d.cc:95
void setYRange(double min, double max)
Definition: Plot2d.cc:63
void setXTitle(QString label)
Definition: Plot2d.cc:58
vtkSmartPointer< vtkFieldData > field_t
Definition: Plot2d.h:22
void setStyle(int id, QString style)
Definition: Plot2d.cc:60
void setPlotLabel(int id, QString label)
Definition: Plot2d.cc:64
void setTitle(QString title)
Definition: Plot2d.cc:57
void addPoints(int id, const QList< double > &x, const QList< double > &y)
Definition: Plot2d.cc:108
void clear(int id)
Definition: Plot2d.cc:78
void setXValues(int id)
Definition: Plot2d.cc:133
void addPlot(int id)
Definition: Plot2d.cc:179
void setNumberOfYLabels(int i)
Definition: Plot2d.cc:69
void scrollTo(double x, double y)
Definition: Plot2d.cc:85
bool isIdValid(int id)
Definition: Plot2d.cc:141
void showLegend(bool on)
Definition: Plot2d.cc:65
void setNumberOfXLabels(int i)
Definition: Plot2d.cc:68
const double c_norm
Definition: Plot2d.cc:21
void autoScroll(bool on)
Definition: Plot2d.cc:70
void render()
Definition: Plot2d.cc:148
field_t getField(int id)
Definition: Plot2d.cc:201
void setYTitle(QString label)
Definition: Plot2d.cc:59
Plot2d(QWidget *parent=NULL)
Definition: Plot2d.cc:9
virtual void mouseMoveEvent(QMouseEvent *event)
Definition: Plot2d.cc:160
vtkSmartPointer< vtkDoubleArray > array_t
Definition: Plot2d.h:21
void showPlotPoints(bool on)
Definition: Plot2d.cc:66
virtual ~Plot2d()
Definition: Plot2d.cc:14
void setXRange(double min, double max)
Definition: Plot2d.cc:62
void showPlotLines(bool on)
Definition: Plot2d.cc:67
void setColor(int id, int r, int g, int b)
Definition: Plot2d.cc:61