VTKFIG  0.20.20181114
Easy VTK based in situ visualization
example-simplexcontour2d.cxx
Go to the documentation of this file.
1 
7 #include <chrono>
8 
9 #include "vtkfigFrame.h"
10 #include "vtkfigTools.h"
11 #include "vtkfigDataSet.h"
12 #include "vtkfigScalarView.h"
13 #include "vtkfigTools.h"
14 
15 #include <vtkMath.h>
16 double scalefac=1.0;
17 
18 inline double G(double x,double y, double t)
19 {
20  x/=scalefac;
21  y/=scalefac;
22  return exp(-(x*x+y*y))*sin(t+3.*x)*cos(4.*y-t);
23 }
24 
25 
26 int main(void)
27 {
28  size_t nspin=vtkfig::NSpin();
29 
30  std::vector<double> inpoints;
31  for(double x = -2; x < 2; x+=0.03)
32  {
33  for(double y = -2; y < 2; y+=0.03)
34  {
35  inpoints.push_back(x + vtkMath::Random(-.1, .1));
36  inpoints.push_back(y + vtkMath::Random(-.1, .1));
37  }
38  }
39 
40 
41 
42 
43  std::vector<double>points;
44  std::vector<int>cells;
45 
46  vtkfig::Delaunay2D(inpoints,points,cells);
47 
48 
49  int npoints=points.size()/2;
50  std::vector<double>values(npoints);
51 
52 
53  auto colors=vtkfig::RGBTable
54  {
55  {0.0, 0.0, 0.0, 1.0},
56  {0.5, 0.0, 1.0, 0.0},
57  {1.0, 1.0, 0.0, 0.0}
58  };
59 
60  double t=0;
61  double dt=0.1;
62  size_t ii=0;
63  auto t0=std::chrono::system_clock::now();
64  double i0=ii;
65 
66  for(auto &x: points) x*=scalefac;
67  auto griddata=vtkfig::DataSet::New();
68  griddata->SetSimplexGrid(2,points,cells);
69 
70  auto frame=vtkfig::Frame::New();
71  auto contour=vtkfig::ScalarView::New();
72  contour->SetData(griddata,"v");
73  contour->SetSurfaceRGBTable(colors,255);
74  contour->SetValueRange(-1,1);
75  auto isolines=std::vector<double>{-0.5,0,0.5};
76  contour->SetIsoLevels(isolines);
77  frame->AddFigure(contour);
78 
79 
80 
81  while (ii<nspin)
82  {
83  for (size_t ipoint=0, ival=0;ipoint<points.size(); ipoint+=2,ival++)
84  values[ival]=G(points[ipoint+0],points[ipoint+1],t);
85 
86  griddata->SetPointScalar(values,"v");
87 
88  frame->Show();
89  if (ii==3)
90  frame->WritePNG("example-simplexcontour2d.png");
91 
92  t+=dt;
93  auto t1=std::chrono::system_clock::now();
94  double xdt=std::chrono::duration_cast<std::chrono::duration<double>>(t1-t0).count();
95  double i1=ii;
96  if (xdt>4.0)
97  {
98  printf("Frame rate: %.2f fps\n",(double)(i1-i0)/4.0);
99  t0=std::chrono::system_clock::now();
100  i0=ii;
101  }
102  ii++;
103 
104  }
105 }
size_t NSpin()
Definition: vtkfigTools.cxx:13
static std::shared_ptr< Frame > New()
Create frame with multiple subframes.
Definition: vtkfigFrame.h:54
double scalefac
static std::shared_ptr< ScalarView > New()
int main(void)
static std::shared_ptr< DataSet > New()
Static constructor of an empty instance.
Definition: vtkfigDataSet.h:40
double G(double x, double y, double t)
std::vector< RGBPoint > RGBTable
Definition: vtkfigTools.h:26
void Delaunay2D(const V &points_in, V &points, IV &cells)
Definition: vtkfigTools.h:49
Define Frame class providing a window for rendering.