VTKFIG  0.25.0
Easy VTK based in situ visualization
vtkfigSurf2D.h
Go to the documentation of this file.
1 
6 #ifndef VTKFIG_SURF2D_H
7 #define VTKFIG_SURF2D_H
8 
9 #include <vtkRectilinearGrid.h>
10 #include <vtkStructuredGrid.h>
11 #include <vtkPointData.h>
12 #include <vtkDoubleArray.h>
13 #include <vtkWarpScalar.h>
14 
15 
16 #include "vtkfigFigure.h"
17 #include "vtkfigTools.h"
18 
19 #include "internals/vtkfigCommunicator.h"
20 
21 namespace vtkfig
22 {
23 
25  class Surf2D: public Figure
26  {
27  public:
28 
29  static std::shared_ptr<Surf2D> New();
30 
31  virtual std::string SubClassName() override final {return std::string("Surf2D");}
32 
33 
34  template<typename V>
35  void SetGrid(const V &xcoord,
36  const V &ycoord);
37 
38  template<typename V>
39  void UpdateValues(const V &values);
40 
41  void SetRGBTable(RGBTable & tab, int tabsize)
42  {
43  state.rgbtab_size=tabsize;
44  state.rgbtab_modified=true;
45  rgbtab=tab;
46  lut=internal::BuildLookupTable(tab,tabsize);
47  }
48  void ShowColorbar(bool b) {show_colorbar=b;}
49 
50  void ServerMPSend(vtkSmartPointer<internals::Communicator> communicator) override
51  {
52  communicator->SendCharBuffer((char*)&state,sizeof(state));
53  if (state.rgbtab_modified)
54  {
55  communicator->SendRGBTable(rgbtab);
56  }
57  communicator->Send(gridfunc,1,1);
58  state.rgbtab_modified=false;
59  };
60 
61  void ClientMPReceive(vtkSmartPointer<internals::Communicator> communicator) override
62  {
63  communicator->ReceiveCharBuffer((char*)&state,sizeof(state));
64  if (state.rgbtab_modified)
65  {
66  RGBTable new_rgbtab;
67  communicator->ReceiveRGBTable(new_rgbtab);
68  SetRGBTable(new_rgbtab,state.rgbtab_size);
69  }
70  communicator->Receive(gridfunc,1,1);
71  update_warp_and_lut();
72  gridfunc->Modified();
73  };
74 
75  protected:
76  Surf2D();
77  ~Surf2D(){};
78 
79  private:
80 
81  void RTBuildVTKPipeline() override final;
82 
83 
84  vtkSmartPointer<vtkStructuredGrid> gridfunc;
85  vtkSmartPointer<vtkPoints> points;
86  vtkSmartPointer<vtkDoubleArray> colors;
87  vtkSmartPointer<vtkWarpScalar> warp;
88 
89 
90  int Nx;
91  int Ny;
92 
93  RGBTable rgbtab{{0,0,1},{1,0,0}};
94  // all float in order to be endianness-aware
95  struct
96  {
97  float Lxy;
98  float Lz;
99  float vmax=0;
100  float vmin=0;
101  int rgbtab_size=255;
102  bool rgbtab_modified=false;
103  } state;
104 
105  vtkSmartPointer<vtkLookupTable> lut;
106  bool show_colorbar=true;
107 
108  void update_warp_and_lut()
109  {
110  state.Lz = state.vmax-state.vmin;
111  lut->SetTableRange(state.vmin,state.vmax);
112  lut->Modified();
113 
114  double scale = state.Lxy/state.Lz;
115  warp->XYPlaneOn();
116  warp->SetScaleFactor(scale);
117  warp->Modified();
118  }
119  };
120 
121 
122  template<typename V>
123  inline
124  void Surf2D::SetGrid(const V &x, const V &y)
125  {
126  Nx = x.size();
127  Ny = y.size();
128  int i,j,k;
129 
130  if (x[Nx-1]-x[0] > y[Ny-1]-y[0])
131  state.Lxy = x[Nx-1]-x[0];
132  else
133  state.Lxy = y[Ny-1]-y[0];
134 
135 
136 
137 
138  gridfunc->SetDimensions(Nx, Ny, 1);
139 
140  points = vtkSmartPointer<vtkPoints>::New();
141  for (j = 0; j < Ny; j++)
142  {
143  for (i = 0; i < Nx; i++)
144  {
145  points->InsertNextPoint(x[i], y[j], 0);
146  }
147  }
148  gridfunc->SetPoints(points);
149 
150  colors = vtkSmartPointer<vtkDoubleArray>::New();
151  colors->SetNumberOfComponents(1);
152  colors->SetNumberOfTuples(Nx*Ny);
153  k = 0;
154  for (j = 0; j < Ny; j++)
155  for (i = 0; i < Nx; i++)
156  {
157  colors->InsertComponent(k, 0, 0);
158  k++;
159  }
160 
161  gridfunc->GetPointData()->SetScalars(colors);
162 
163  }
164 
165  template<typename V>
166  inline
167  void Surf2D::UpdateValues(const V &z)
168  {
169  for (int j = 0; j < Ny; j++)
170  {
171  for (int i = 0; i < Nx; i++)
172  {
173  int k=j*Nx+i;
174  float v=z[k];
175  state.vmin=std::min(v,state.vmin);
176  state.vmax=std::max(v,state.vmax);
177  double p[3];
178  points->GetPoint(k,p);
179  p[2]=v;
180  points->SetPoint(k,p);
181  colors->InsertComponent(k, 0,v);
182  }
183  }
184 
185  points->Modified();
186  colors->Modified();
187  gridfunc->Modified();
188 
189  update_warp_and_lut();
190 
191  }
192 }
193 #endif
vtkfig::Surf2D::ClientMPReceive
void ClientMPReceive(vtkSmartPointer< internals::Communicator > communicator) override
Receive subclass data from server.
Definition: vtkfigSurf2D.h:61
vtkfig::Surf2D::SubClassName
virtual std::string SubClassName() override final
Get subclass name (for s-c communication, should be replaced by tag.
Definition: vtkfigSurf2D.h:31
vtkfigFigure.h
vtkfig::Figure
Base class for all figures.
Definition: vtkfigFigure.h:61
vtkfig::Surf2D::ServerMPSend
void ServerMPSend(vtkSmartPointer< internals::Communicator > communicator) override
Send subclass data to client.
Definition: vtkfigSurf2D.h:50
vtkfig::RGBTable
std::vector< RGBPoint > RGBTable
Color table.
Definition: vtkfigTools.h:43
vtkfig::Surf2D
Experimental class for 2D elevation plot - don't use.
Definition: vtkfigSurf2D.h:25
vtkfigTools.h