VTKFIG  0.20.20181114
Easy VTK based in situ visualization
example-axis.cxx
Go to the documentation of this file.
1 
7 #include <chrono>
8 #include "vtkUnstructuredGrid.h"
9 #include "vtkPointData.h"
10 #include "vtkFloatArray.h"
11 #include "vtkActor.h"
12 #include "vtkOutlineFilter.h"
13 
14 #include "vtkCubeAxesActor.h"
15 #include "vtkTextProperty.h"
16 #include "vtkPolyLine.h"
17 #include "vtkDataSetMapper.h"
18 #include "vtkAxisActor.h"
19 #include "vtkContextScene.h"
20 
21 #include "vtkAxis.h"
22 #include "vtkPlotGrid.h"
23 #include "vtkDoubleArray.h"
24 #include "vtkTable.h"
25 #include "vtkPlotLine.h"
26 
27 #include "vtkfigFrame.h"
28 #include "vtkfigFigure.h"
29 #include "vtkfigTools.h"
30 #include "vtkLegendBoxActor.h"
31 
32 class CoordSystemActor: public vtkCubeAxesActor
33 {
34 protected:
35  CoordSystemActor(): vtkCubeAxesActor()
36  {
37  SetZAxisVisibility(0);
38  SetYAxisVisibility(1);
39  SetXAxisVisibility(1);
40  SetDrawXGridlines(1);
41  SetDrawXInnerGridlines(1);
42  SetDrawYGridlines(1);
43  GetLabelTextProperty(0)->SetFontSize(15);;
44  GetTitleTextProperty(0)->SetFontSize(15);;
45  GetLabelTextProperty(0)->SetColor(0,0,0);
46  GetTitleTextProperty(0)->SetColor(0,0,0);
47  GetLabelTextProperty(1)->SetFontSize(15);;
48  GetTitleTextProperty(1)->SetFontSize(15);;
49  GetLabelTextProperty(1)->SetColor(0,0,0);
50  GetTitleTextProperty(1)->SetColor(0,0,0);
51 
52  GetXAxesLinesProperty()->SetColor(0, 0, 0);
53  GetXAxesLinesProperty()->SetLineWidth(2);
54  GetYAxesLinesProperty()->SetColor(0, 0, 0);
55  GetYAxesLinesProperty()->SetLineWidth(2);
56 
57  SetAxisOrigin(0,0,0);
58  SetUseAxisOrigin(0);
59  GetXAxesGridlinesProperty()->SetLineWidth(0.5);
60  GetXAxesGridlinesProperty()->SetColor(0, 0, 0);
61  GetYAxesGridlinesProperty()->SetColor(0, 0, 0);
62  SetTickLocationToOutside();
63 
64  };
65 public:
66  static CoordSystemActor *New() {return new CoordSystemActor();}
67  void SetLogScaleX() { XAxes[0]->SetLog(true);}
68  void SetLogScaleY() { YAxes[0]->SetLog(true);}
69 };
70 
71 
72 class myplot0: public vtkfig::Figure
73 {
74 public:
75  myplot0(): vtkfig::Figure()
76  {
77  }
78  static std::shared_ptr<myplot0> New() { return std::make_shared<myplot0>();}
79  virtual std::string SubClassName() {return std::string("myplot0");}
80 
81  void RTBuildAllVTKPipelines(vtkSmartPointer<vtkRenderer> renderer)
82  {
83  auto coordsys=vtkSmartPointer<CoordSystemActor>::New();
84  coordsys->SetUse2DMode(1);
85  coordsys->SetBounds(0,1,0.001,100,0,0);
86 // coordsys->SetYAxisRange(0,1);
87 
88  coordsys->SetXTitle("x");
89  coordsys->SetXUnits("cm");
90  coordsys->SetYTitle("y");
91 // coordsys->SetLogScaleY();
92 
93  // coordsys->SetScreenSize(0.1);
94  // coordsys->SetLabelOffset(2);
95 
96  coordsys->SetCamera(renderer->GetActiveCamera());
97 
98 
99 
100  Figure::RTAddActor(coordsys);
101 
102 
103 
104  auto polyLinePoints = vtkSmartPointer<vtkPoints>::New();
105  auto polyLine = vtkSmartPointer<vtkPolyLine>::New();
106  int N=100;
107  polyLinePoints->SetNumberOfPoints(N);
108  polyLine->GetPointIds()->SetNumberOfIds(N);
109 
110 
111  double x=0.0;
112  double h=1.0/N;
113  for (int i=0;i<N;i++,x+=h)
114  {
115  polyLinePoints->InsertPoint(i, x, exp(sin(10.0*x)), 0);
116  polyLine->GetPointIds()->SetId(i,i);
117  }
118 
119  auto polyLineGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
120  polyLineGrid->Allocate(1, 1);
121  polyLineGrid->InsertNextCell(polyLine->GetCellType(), polyLine->GetPointIds());
122  polyLineGrid->SetPoints(polyLinePoints);
123 
124  auto polyLineMapper = vtkSmartPointer<vtkDataSetMapper>::New();
125  polyLineMapper->SetInputData(polyLineGrid);
126 
127  auto polyLineActor =vtkSmartPointer<vtkActor>::New();
128  polyLineActor->SetMapper(polyLineMapper);
129  polyLineActor->GetProperty()->SetColor(1,0,0);
130  polyLineActor->GetProperty()->SetLineWidth(2.5);
131  polyLineActor->GetProperty()->BackfaceCullingOn();
132 
133  Figure::RTAddActor(polyLineActor);
134 
135  auto lbox=vtkSmartPointer<vtkLegendBoxActor>::New();
136 
137  }
138 };
139 
140 
141 
142 
143 class myplot: public vtkfig::Figure
144 {
145 public:
146  myplot(): vtkfig::Figure()
147  {
148  }
149  static std::shared_ptr<myplot> New() { return std::make_shared<myplot>();}
150  virtual std::string SubClassName() {return std::string("myplot");}
151 
152  void RTBuildAllVTKPipelines(vtkSmartPointer<vtkRenderer> renderer)
153  {
154  auto ctxactor= vtkSmartPointer<vtkContextActor>::New();
155  auto XAxis=vtkSmartPointer<vtkAxis>::New();
156 
157  double P00x=0.1; double P00y=0.1;
158  double P01x=0.1; double P01y=0.9;
159  double P10x=0.9; double P10y=0.1;
160  double P11x=0.9; double P11y=0.9;
161  renderer->NormalizedDisplayToDisplay(P00x,P00y);
162  renderer->NormalizedDisplayToDisplay(P10x,P10y);
163  renderer->NormalizedDisplayToDisplay(P01x,P01y);
164  renderer->NormalizedDisplayToDisplay(P11x,P11y);
165 
166  XAxis->SetPoint1(P00x,P00y);
167  XAxis->SetPoint2(P10x,P10y);
168  cout << P10x << " " << P10y << endl;
169  XAxis->SetPosition(vtkAxis::BOTTOM);
170  XAxis->SetRange(0,10);
171  XAxis->SetAxisVisible(1);
172  XAxis->SetGridVisible(1);
173  XAxis->SetScene(ctxactor->GetScene());
174  XAxis->Update();
175 
176 
177  auto YAxis=vtkSmartPointer<vtkAxis>::New();
178  YAxis->SetPoint1(P00x,P00y);
179  YAxis->SetPoint2(P01x,P01y);
180  YAxis->SetPosition(vtkAxis::LEFT);
181  YAxis->SetRange(0.01,10);
182 // YAxis->LogScaleOn();
183 // YAxis->SetNumberOfTicks(2);
184  YAxis->SetAxisVisible(1);
185  YAxis->SetGridVisible(1);
186  YAxis->SetScene(ctxactor->GetScene());
187  YAxis->Update();
188 
189 // XAxis->GetProperty()->SetColor(0,0,0);
190 
191  auto Grid=vtkSmartPointer<vtkPlotGrid>::New();
192  Grid->SetXAxis(XAxis);
193  Grid->SetYAxis(YAxis);
194  Grid->Update();
195 
196  cout << ctxactor->GetScene()->GetViewWidth() << " "<< ctxactor->GetScene()->GetViewHeight() << endl;
197  cout << ctxactor->GetScene()->GetSceneWidth() << " "<< ctxactor->GetScene()->GetSceneHeight() << endl;
198 
199 
200 
201  vtkSmartPointer<vtkDoubleArray> X;
202  vtkSmartPointer<vtkDoubleArray> Y;
203  vtkSmartPointer<vtkTable> table;
204  vtkSmartPointer<vtkPlotLine> line;
205 
206  X=vtkSmartPointer<vtkDoubleArray>::New();
207  Y=vtkSmartPointer<vtkDoubleArray>::New();
208  X->SetName("X");
209  Y->SetName("Y");
210  table=vtkSmartPointer<vtkTable>::New();
211  table->AddColumn(X);
212  table->AddColumn(Y);
213 
214  int N=100;
215  table->SetNumberOfRows(N);
216  double x=0.0;
217  double h=1.0/N;
218  for (int i=0;i<N;i++,x+=h)
219  {
220  table->SetValue(i,0,10*x);
221  table->SetValue(i,1,exp(sin(10.0*x)));
222  }
223 
224 
225  line=vtkSmartPointer<vtkPlotLine>::New();
226  line->SetInputData(table, 0, 1);
227  line->SetXAxis(XAxis);
228  line->SetYAxis(YAxis);
229  line->SetPolyLine(true);
230  line->SetColor(1,0,0);
231  line->Update();
232 
233 
234 
235  ctxactor->GetScene()->AddItem(Grid);
236  ctxactor->GetScene()->AddItem(XAxis);
237  ctxactor->GetScene()->AddItem(YAxis);
238  ctxactor->GetScene()->AddItem(line);
239  Figure::RTAddContextActor(ctxactor);
240 
241 
242 
243 
244  }
245 };
246 
247 
248 int main(void)
249 {
250 
251 
252  auto frame=vtkfig::Frame::New();
253  auto plot=myplot0::New();
254 
255 
256  frame->AddFigure(plot);
257  frame->Interact();
258 
259 
260 
261 #if 0
262  while (ii <nspin)
263  {
264 
265  for (int i=0; i<Nx; i++)
266  for (int j=0; j<Ny; j++)
267  z[j*Nx+i] = G(x[i],y[j],t);
268 
269  float vmax=-10000;
270  float vmin=10000;
271  for (int j = 0; j < Ny; j++)
272  {
273  for (int i = 0; i < Nx; i++)
274  {
275  int k=j*Nx+i;
276  float v=z[k];
277  vmin=std::min(v,vmin);
278  vmax=std::max(v,vmax);
279  double p[3];
280  points->GetPoint(k,p);
281  p[2]=v;
282  points->SetPoint(k,p);
283  colors->InsertComponent(k, 0,v);
284  }
285  }
286 
287  points->Modified();
288  colors->Modified();
289  gridfunc->Modified();
290 
291  lut->SetTableRange(vmin,vmax);
292  lut->Modified();
293 
294  frame->Show();
295 
296 
297  t+=dt;
298  auto t1=std::chrono::system_clock::now();
299  double dt=std::chrono::duration_cast<std::chrono::duration<double>>(t1-t0).count();
300  double i1=ii;
301  if (dt>4.0)
302  {
303  printf("Frame rate: %.2f fps\n",(double)(i1-i0)/4.0);
304  t0=std::chrono::system_clock::now();
305  i0=ii;
306  fflush(stdout);
307  }
308  ii++;
309  }
310 #endif
311 }
static std::shared_ptr< Frame > New()
Create frame with multiple subframes.
Definition: vtkfigFrame.h:54
static CoordSystemActor * New()
double G(double x, double y, double t)
void RTBuildAllVTKPipelines(vtkSmartPointer< vtkRenderer > renderer)
Default implementation for 2D/3d datasets.
int main(void)
Base class for all figures.
Definition: vtkfigFigure.h:52
virtual std::string SubClassName()
Get subclass name (for s-c communication, should be replaced by tag.
static std::shared_ptr< myplot0 > New()
static std::shared_ptr< myplot > New()
void RTBuildAllVTKPipelines(vtkSmartPointer< vtkRenderer > renderer)
Default implementation for 2D/3d datasets.
virtual std::string SubClassName()
Get subclass name (for s-c communication, should be replaced by tag.
Define Frame class providing a window for rendering.