VTKFIG  0.20.20181114
Easy VTK based in situ visualization
example-context2d.cxx
Go to the documentation of this file.
1 
7 #include <chrono>
8 #include <vtkContext2D.h>
9 #include <vtkContext3D.h>
10 #include <vtkContextItem.h>
11 #include <vtkContextActor.h>
12 #include <vtkContextScene.h>
13 #include <vtkBrush.h>
14 
15 #include "vtkfigFrame.h"
16 #include "vtkfigFigure.h"
17 #include "vtkfigTools.h"
18 
19 
20 
21 class MyDrawing : public vtkContextItem
22 {
23 public:
24  static MyDrawing *New();
25  vtkTypeMacro(MyDrawing, vtkContextItem);
26  double delta=0.0;
27  virtual bool Paint(vtkContext2D* painter)
28  {
29  painter->GetBrush()->SetColor(255, 0, 0);
30  painter->DrawRect(20+delta,20+delta,40,40);
31  auto p3d=painter->GetContext3D();
32  p3d->DrawLine(vtkVector3f{0,0,0},vtkVector3f{delta,delta,delta} );
33  return true;
34  }
35 };
36 
38 int main(void)
39 {
40 
41  cout <<
42 R"(
43 Example showing creation of custom scenes using
44 vtkfigFrame and vtkfigFigure base class.
45 )";
46 
47  size_t nspin=vtkfig::NSpin();
48 
49  cout.sync_with_stdio(true);
50 
51 
52  auto frame=vtkfig::Frame::New();
53  auto figure=vtkfig::Figure::New();
54  figure->SetViewVolume(0,1,0,1,0,1);
55 
56  auto cactor=vtkContextActor::New();
57  auto drawing=MyDrawing::New();
58  cactor->GetScene()->AddItem(drawing);
59  figure->RTAddContextActor(cactor);
60  frame->AddFigure(figure);
61 
62 
63  auto t0=std::chrono::system_clock::now();
64 
65 
66 
67  int ii=0;
68  int i0=0;
69  while (ii <nspin)
70  {
71  auto t1=std::chrono::system_clock::now();
72  double dt=std::chrono::duration_cast<std::chrono::duration<double>>(t1-t0).count();
73  int i1=ii;
74  drawing->delta+=0.5;
75  frame->Show();
76  if (dt>4.0)
77  {
78  printf("Frame rate: %.2f fps\n",(double)(i1-i0)/4.0);
79  t0=std::chrono::system_clock::now();
80  i0=ii;
81  fflush(stdout);
82  }
83  ii++;
84  }
85 
86 }
size_t NSpin()
Definition: vtkfigTools.cxx:13
vtkStandardNewMacro(MyDrawing)
static std::shared_ptr< Frame > New()
Create frame with multiple subframes.
Definition: vtkfigFrame.h:54
int main(void)
static std::shared_ptr< Figure > New()
Construct smartpointer to empty figure.
Definition: vtkfigFigure.h:61
virtual bool Paint(vtkContext2D *painter)
vtkTypeMacro(MyDrawing, vtkContextItem)
Define Frame class providing a window for rendering.
static MyDrawing * New()