8 #include "vtkStructuredGrid.h" 9 #include "vtkPointData.h" 10 #include "vtkFloatArray.h" 12 #include "vtkOutlineFilter.h" 13 #include "vtkStructuredGridGeometryFilter.h" 14 #include "vtkRenderWindow.h" 15 #include "vtkRenderer.h" 16 #include "vtkRenderWindowInteractor.h" 17 #include "vtkCommand.h" 23 inline double G(
double x,
double y,
double t)
26 return exp(-(x*x+y*y))*sin(t+x)*cos(y-t);
40 vtkObject *vtkNotUsed(caller),
41 unsigned long eventId,
42 void *vtkNotUsed(callData)
47 interactor->TerminateApp();
56 Example showing creation of custom scenes using 57 vtkfigFrame and vtkfigFigure base class. 62 cout.sync_with_stdio(
true);
67 std::vector<double> x(Nx);
68 std::vector<double> y(Ny);
69 std::vector<double> z(Nx*Ny);
71 const double x_low = -2.5;
72 const double x_upp = 1.5;
73 const double y_low = -2.5;
74 const double y_upp = 4;
75 const double dx = (x_upp-x_low)/(Nx-1);
76 const double dy = (y_upp-y_low)/(Ny-1);
84 for (
int i=0; i<Nx; i++)
87 for (
int i=0; i<Ny; i++)
94 auto t0=std::chrono::system_clock::now();
103 auto gridfunc=vtkSmartPointer<vtkStructuredGrid>::New();
104 auto points=vtkSmartPointer<vtkPoints>::New();
105 auto colors=vtkSmartPointer<vtkFloatArray>::New();
107 gridfunc->SetDimensions(Nx, Ny, 1);
109 points = vtkSmartPointer<vtkPoints>::New();
110 for (j = 0; j < Ny; j++)
112 for (i = 0; i < Nx; i++)
114 points->InsertNextPoint(x[i], y[j], 0);
117 gridfunc->SetPoints(points);
119 colors = vtkSmartPointer<vtkFloatArray>::New();
120 colors->SetNumberOfComponents(1);
121 colors->SetNumberOfTuples(Nx*Ny);
123 for (j = 0; j < Ny; j++)
124 for (i = 0; i < Nx; i++)
126 colors->InsertComponent(k, 0, 0);
132 {0.0, 0.0, 0.0, 1.0},
133 {0.5, 0.0, 1.0, 0.0},
139 gridfunc->GetPointData()->SetScalars(colors);
143 auto geometry = vtkSmartPointer<vtkStructuredGridGeometryFilter>::New();
144 geometry->SetInputDataObject(gridfunc);
148 auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
149 mapper->SetInputConnection(geometry->GetOutputPort());
153 auto surfplot = vtkSmartPointer<vtkActor>::New();
154 surfplot->SetMapper(mapper);
155 mapper->SetLookupTable(lut);
156 mapper->UseLookupTableScalarRangeOn();
160 auto outlinefilter = vtkSmartPointer<vtkOutlineFilter>::New();
161 outlinefilter->SetInputConnection(geometry->GetOutputPort());
163 auto outlineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
164 outlineMapper->SetInputConnection(outlinefilter->GetOutputPort());
165 auto outline = vtkSmartPointer<vtkActor>::New();
166 outline->SetMapper(outlineMapper);
167 outline->GetProperty()->SetColor(0, 0, 0);
170 auto window=vtkSmartPointer<vtkRenderWindow>::New();
171 auto renderer=vtkSmartPointer<vtkRenderer>::New();
172 window->AddRenderer(renderer);
173 auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
174 interactor->SetRenderWindow(window);
175 renderer->AddActor(surfplot);
176 renderer->AddActor(outline);
180 auto callback = vtkSmartPointer<TimerCallback>::New();
182 interactor->AddObserver(vtkCommand::TimerEvent,callback);
185 interactor->Initialize();
186 interactor->CreateRepeatingTimer(10);
195 for (
int i=0; i<Nx; i++)
196 for (
int j=0; j<Ny; j++)
197 z[j*Nx+i] =
G(x[i],y[j],t);
201 for (
int j = 0; j < Ny; j++)
203 for (
int i = 0; i < Nx; i++)
207 vmin=std::min(v,vmin);
208 vmax=std::max(v,vmax);
210 points->GetPoint(k,p);
212 points->SetPoint(k,p);
213 colors->InsertComponent(k, 0,v);
219 gridfunc->Modified();
221 lut->SetTableRange(vmin,vmax);
223 renderer->SetBackground(1,1,1);
224 interactor->Render();
228 auto t1=std::chrono::system_clock::now();
229 double dt=std::chrono::duration_cast<std::chrono::duration<double>>(t1-t0).count();
233 printf(
"Frame rate: %.2f fps\n",(
double)(i1-i0)/4.0);
234 t0=std::chrono::system_clock::now();
double G(double x, double y, double t)
vtkSmartPointer< vtkRenderWindowInteractor > interactor
vtkSmartPointer< vtkLookupTable > BuildLookupTable(RGBTable &xrgb, size_t size)
std::vector< RGBPoint > RGBTable
virtual void Execute(vtkObject *vtkNotUsed(caller), unsigned long eventId, void *vtkNotUsed(callData))
vtkSmartPointer< vtkScalarBarActor > BuildColorBar(vtkSmartPointer< vtkPolyDataMapper > mapper, int irank=0)
static TimerCallback * New()
Define Frame class providing a window for rendering.