Mercurial > hg > kwantix
comparison src/vtkplot.cpp @ 43:8aea477c7cf8
Continue implementation of vtkplot class, only missing to move the tail of the pipeline to the Plot function.
author | Jordi Gutiérrez Hermoso <jordigh@gmail.com> |
---|---|
date | Mon, 15 Mar 2010 21:10:18 -0600 |
parents | 3f8311cbf602 |
children | 4134a0f2423d |
comparison
equal
deleted
inserted
replaced
42:3f8311cbf602 | 43:8aea477c7cf8 |
---|---|
42 | 42 |
43 #include <string> | 43 #include <string> |
44 #include <sstream> | 44 #include <sstream> |
45 #include <iomanip> | 45 #include <iomanip> |
46 | 46 |
47 #include <boost/function.hpp> | |
48 | |
47 namespace kwantix{ | 49 namespace kwantix{ |
48 | |
49 class CommandSubclass : public vtkCommand | |
50 { | |
51 public: | |
52 static CommandSubclass *New() | |
53 { | |
54 return new CommandSubclass; | |
55 } | |
56 | |
57 void Execute(vtkObject *caller, unsigned long eventId, | |
58 void *callData) | |
59 { | |
60 vtkRenderWindowInteractor *iren = | |
61 static_cast<vtkRenderWindowInteractor*>(caller); | |
62 | |
63 this->ProgrammableFilter->Modified(); | |
64 | |
65 iren->Render(); | |
66 | |
67 } | |
68 | |
69 vtkSmartPointer<vtkProgrammableFilter> ProgrammableFilter; | |
70 | |
71 }; | |
72 | |
73 template<typename RBF> | |
74 void vtkplot::AdjustPoints(void* arguments) | |
75 { | |
76 vtkProgrammableFilter* programmableFilter = | |
77 static_cast<vtkProgrammableFilter*>(arguments); | |
78 arguments = programmableFilter + 1; | |
79 | |
80 auto u = static_cast< const interpolator<RBF>& > (arguments); | |
81 | |
82 vtkPoints* inPts = programmableFilter->GetPolyDataInput()->GetPoints(); | |
83 vtkIdType numPts = inPts->GetNumberOfPoints(); | |
84 vtkSmartPointer<vtkPoints> newPts = | |
85 vtkSmartPointer<vtkPoints>::New(); | |
86 newPts->SetNumberOfPoints(numPts); | |
87 | |
88 vtkSmartPointer<vtkDoubleArray> newScalars | |
89 = vtkSmartPointer<vtkDoubleArray>::New(); | |
90 | |
91 static double t = 0; | |
92 | |
93 //auto& vals = u.at(); | |
94 | |
95 for(vtkIdType i = 0; i < numPts; i++) | |
96 { | |
97 double p[3]; | |
98 inPts -> GetPoint(i,p); | |
99 //p[2] = F(p[0],p[1],t); | |
100 newPts -> SetPoint(i,p); | |
101 newScalars -> InsertTuple(i, &p[2]); | |
102 } | |
103 | |
104 t += 0.05; | |
105 | |
106 programmableFilter-> | |
107 GetPolyDataOutput()->CopyStructure(programmableFilter->GetPolyDataInput()); | |
108 | |
109 programmableFilter->GetPolyDataOutput()->SetPoints(newPts); | |
110 programmableFilter | |
111 ->GetPolyDataOutput() | |
112 ->GetPointData() -> SetScalars(newScalars); | |
113 } | |
114 | 50 |
115 template<typename RBF> | 51 template<typename RBF> |
116 vtkplot::vtkplot(const interpolator<RBF>& u, | 52 vtkplot::vtkplot(const interpolator<RBF>& u, |
117 bool offscreen_in) | 53 bool offscreen_in) |
118 :hash(u.rbfs_hash), offscreen(offscreen) | 54 :hash(u.rbfs_hash), offscreen(offscreen) |
119 { | 55 { |
120 if(u.thebvp -> get_domain() -> get_dimension() != 2) | 56 if(u.thebvp -> get_domain() -> get_dimension() != 2) |
121 { | 57 { |
122 badArgument exc; | 58 badArgument exc; |
123 exc.reason = "This class only works on interpolators whose domain lies in R^2"; | 59 exc.reason = |
60 "Class vtkplot only works on interpolators whose " | |
61 "domain lies in R^2"; | |
124 exc.line = __LINE__; | 62 exc.line = __LINE__; |
125 exc.file = __FILE__; | 63 exc.file = __FILE__; |
126 throw exc; | 64 throw exc; |
127 } | 65 } |
66 | |
128 SetupPipeline(u, offscreen); | 67 SetupPipeline(u, offscreen); |
68 } | |
69 | |
70 void vtkplot::set_offscreen(bool offscreen_in) | |
71 { | |
72 offscreen = offscreen_in; | |
73 InitOffscreen(); | |
74 } | |
75 | |
76 template<typename RBF> | |
77 void vtkplot::update_values(const interp_values& vals_in) | |
78 { | |
79 vals = vals_in; | |
80 programmableFilter -> Modified(); | |
81 } | |
82 | |
83 void vtkplot::trampoline(void* this_ptr) | |
84 { | |
85 vtkplot* instance = static_cast<vtkplot*>(this_ptr); | |
86 instance -> AdjustPoints(); | |
129 } | 87 } |
130 | 88 |
131 template<typename RBF> | 89 template<typename RBF> |
132 void vtkplot::SetupPipeline(const interpolator<RBF>& u) | 90 void vtkplot::SetupPipeline(const interpolator<RBF>& u) |
133 { | 91 { |
161 delaunay->SetInput(polydata); | 119 delaunay->SetInput(polydata); |
162 delaunay->Update(); | 120 delaunay->Update(); |
163 | 121 |
164 InitOffscreen(); | 122 InitOffscreen(); |
165 | 123 |
166 vtkSmartPointer<vtkProgrammableFilter> programmableFilter = | 124 programmableFilter = vtkSmartPointer<vtkProgrammableFilter>::New(); |
167 vtkSmartPointer<vtkProgrammableFilter>::New(); | |
168 programmableFilter->SetInput(delaunay -> GetOutput()); | 125 programmableFilter->SetInput(delaunay -> GetOutput()); |
169 programmableFilter->SetExecuteMethod(AdjustPoints<RBF>, programmableFilter, u); | 126 programmableFilter->SetExecuteMethod(trampoline, this); |
170 | |
171 | 127 |
172 //Normals for Gourad shading | 128 //Normals for Gourad shading |
173 vtkSmartPointer<vtkPolyDataNormals> normals | 129 vtkSmartPointer<vtkPolyDataNormals> normals |
174 = vtkSmartPointer<vtkPolyDataNormals>::New(); | 130 = vtkSmartPointer<vtkPolyDataNormals>::New(); |
175 normals -> SetInputConnection(programmableFilter -> GetOutputPort() ); | 131 normals -> SetInputConnection(programmableFilter -> GetOutputPort() ); |
197 vtkSmartPointer<vtkActor> contActor | 153 vtkSmartPointer<vtkActor> contActor |
198 = vtkSmartPointer<vtkActor>::New(); | 154 = vtkSmartPointer<vtkActor>::New(); |
199 contActor->SetMapper(contMapper); | 155 contActor->SetMapper(contMapper); |
200 | 156 |
201 // a renderer and render window | 157 // a renderer and render window |
202 vtkSmartPointer<vtkRenderer> ren1 = | 158 ren1 = vtkSmartPointer<vtkRenderer>::New(); |
203 vtkSmartPointer<vtkRenderer>::New(); | 159 renWin = vtkSmartPointer<vtkRenderWindow>::New(); |
204 vtkSmartPointer<vtkRenderWindow> renWin | |
205 = vtkSmartPointer<vtkRenderWindow>::New(); | |
206 if(offscreen) | 160 if(offscreen) |
207 { | 161 { |
208 renWin->SetOffScreenRendering(1); | 162 renWin->SetOffScreenRendering(1); |
209 } | 163 } |
210 renWin->AddRenderer(ren1); | 164 renWin->AddRenderer(ren1); |
216 | 170 |
217 // Initialize must be called prior to creating timer events. | 171 // Initialize must be called prior to creating timer events. |
218 iren->Initialize(); | 172 iren->Initialize(); |
219 iren->CreateRepeatingTimer(10); | 173 iren->CreateRepeatingTimer(10); |
220 | 174 |
221 vtkSmartPointer<CommandSubclass> timerCallback = | |
222 vtkSmartPointer<CommandSubclass>::New(); | |
223 timerCallback->ProgrammableFilter = programmableFilter; | |
224 | |
225 iren->AddObserver ( vtkCommand::TimerEvent, timerCallback ); | |
226 | |
227 // add the actors to the scene | 175 // add the actors to the scene |
228 ren1->SetBackground(0.0,0.0,0.0); | 176 ren1->SetBackground(0.0,0.0,0.0); |
229 ren1->GetActiveCamera()->SetViewUp(0,0,1); | 177 ren1->GetActiveCamera()->SetViewUp(0,0,1); |
230 ren1->GetActiveCamera()->SetPosition(1, 0, 1); | 178 ren1->GetActiveCamera()->SetPosition(1, 1, 1); |
231 ren1->GetActiveCamera()->SetParallelProjection(1); | 179 ren1->GetActiveCamera()->SetParallelProjection(1); |
232 ren1->AddActor(contActor); | 180 ren1->AddActor(contActor); |
233 ren1->ResetCamera(); | 181 ren1->ResetCamera(); |
234 | 182 |
235 // render an image (lights and cameras are created automatically) | |
236 renWin->Render(); | |
237 | |
238 if(offscreen) | 183 if(offscreen) |
239 { | 184 { |
240 for(size_t i = 0; i < 1000; i++) | 185 for(size_t i = 0; i < 1000; i++) |
241 { | 186 { |
242 using namespace std; | 187 using namespace std; |
261 programmableFilter -> Modified(); | 206 programmableFilter -> Modified(); |
262 } | 207 } |
263 } | 208 } |
264 else | 209 else |
265 { | 210 { |
211 | |
212 // render an image (lights and cameras are created automatically) | |
213 renWin->Render(); | |
266 //The style | 214 //The style |
267 vtkSmartPointer<vtkInteractorStyleTerrain> terrain_style | 215 vtkSmartPointer<vtkInteractorStyleTerrain> terrain_style |
268 = vtkSmartPointer<vtkInteractorStyleTerrain>::New(); | 216 = vtkSmartPointer<vtkInteractorStyleTerrain>::New(); |
269 | 217 |
270 // Set the style | 218 // Set the style |
271 iren -> SetInteractorStyle(terrain_style); | 219 iren -> SetInteractorStyle(terrain_style); |
272 | 220 |
273 // begin mouse interaction | 221 // begin mouse interaction |
274 iren->Start(); | 222 iren->Start(); |
275 } | 223 } |
276 return delaunay; | |
277 } | 224 } |
278 | 225 |
279 void vtkplot::InitOffscreen() | 226 void vtkplot::InitOffscreen() |
280 { | 227 { |
281 // Graphics Factory | 228 // Graphics Factory |
288 vtkSmartPointer<vtkImagingFactory> imaging_factory | 235 vtkSmartPointer<vtkImagingFactory> imaging_factory |
289 = vtkSmartPointer<vtkImagingFactory>::New(); | 236 = vtkSmartPointer<vtkImagingFactory>::New(); |
290 imaging_factory->SetUseMesaClasses( offscreen ); | 237 imaging_factory->SetUseMesaClasses( offscreen ); |
291 } | 238 } |
292 | 239 |
240 void vtkplot::AdjustPoints() | |
241 { | |
242 | |
243 if(vals.get_hash() != hash) | |
244 { | |
245 badArgument exc; | |
246 exc.reason = | |
247 "Can't update vtkplot with data from a different " | |
248 "interpolation domain."; | |
249 exc.line = __LINE__; | |
250 exc.file = __FILE__; | |
251 throw exc; | |
252 } | |
253 | |
254 vtkPoints* inPts = programmableFilter->GetPolyDataInput()->GetPoints(); | |
255 vtkIdType numPts = inPts->GetNumberOfPoints(); | |
256 vtkSmartPointer<vtkPoints> newPts = | |
257 vtkSmartPointer<vtkPoints>::New(); | |
258 newPts->SetNumberOfPoints(numPts); | |
259 | |
260 vtkSmartPointer<vtkDoubleArray> newScalars | |
261 = vtkSmartPointer<vtkDoubleArray>::New(); | |
262 | |
263 for(vtkIdType i = 0; i < numPts; i++) | |
264 { | |
265 double p[3]; | |
266 inPts -> GetPoint(i,p); | |
267 p[2] = vals(i+1); | |
268 newPts -> SetPoint(i,p); | |
269 newScalars -> InsertTuple(i, &p[2]); | |
270 } | |
271 | |
272 programmableFilter-> | |
273 GetPolyDataOutput()->CopyStructure(programmableFilter->GetPolyDataInput()); | |
274 | |
275 programmableFilter->GetPolyDataOutput()->SetPoints(newPts); | |
276 programmableFilter | |
277 ->GetPolyDataOutput() | |
278 ->GetPointData() -> SetScalars(newScalars); | |
279 } | |
293 | 280 |
294 } //namespace kwantix | 281 } //namespace kwantix |
295 | 282 |