Main Lemma Repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matplot.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. #ifdef LEMMAUSEVTK
  2. #include "matplot.h"
  3. #include "vtkActor.h"
  4. #include "vtkAxesActor.h"
  5. #include "vtkCamera.h"
  6. #include "vtkCaptionActor2D.h"
  7. #include "vtkContourFilter.h"
  8. #include "vtkDataSetMapper.h"
  9. #include "vtkGlyph3D.h"
  10. #include "vtkGlyphSource2D.h"
  11. #include "vtkOutlineFilter.h"
  12. #include "vtkPolyData.h"
  13. #include "vtkPolyDataMapper.h"
  14. #include "vtkPNGWriter.h"
  15. #include "vtkProperty.h"
  16. #include "vtkProperty2D.h"
  17. #include "vtkRectilinearGridGeometryFilter.h"
  18. #include "vtkRenderWindow.h"
  19. #include "vtkRenderWindowInteractor.h"
  20. #include "vtkStructuredGridGeometryFilter.h"
  21. #include "vtkScalarBarActor.h"
  22. #include "vtkTextProperty.h"
  23. #include "vtkTubeFilter.h"
  24. #include "vtkWarpScalar.h"
  25. #include "vtkWindowToImageFilter.h"
  26. #include "vtkRenderLargeImage.h"
  27. #include "vtkTextActor.h"
  28. #include "vtkVectorText.h"
  29. #include "vtkTransformPolyDataFilter.h"
  30. #include "vtkTransform.h"
  31. #include "vtkFollower.h"
  32. #include "vtkAxisActor2D.h"
  33. //#include "vtkCubeAxesActor.h"
  34. #include "vtkCubeAxesActor2D.h"
  35. using namespace matplot;
  36. /// Default constructor
  37. Plot2D_VTK::Plot2D_VTK(std::string x_label, std::string y_label,
  38. int xpix, int ypix, bool semilogX)
  39. {
  40. semilogx = semilogX;
  41. xPix = xpix;
  42. yPix = ypix;
  43. plot_no = 0;
  44. // set up the renderer
  45. rend = vtkRenderer::New();
  46. rend->SetBackground(1., 1., 1.);
  47. // set up the xy plot
  48. xyplot = vtkXYPlotActor::New();
  49. xyplot->GetProperty()->SetColor(0.0, 0.0, 0.0);
  50. xyplot->SetBorder(10);
  51. xyplot->GetPositionCoordinate()->SetValue(0.0, 0.0, 0);
  52. xyplot->GetPosition2Coordinate()->SetValue(1.0, 1.0, 0);
  53. xyplot->GetProperty()->SetLineWidth(1);
  54. xyplot->GetProperty()->SetPointSize(5);
  55. xyplot->PlotPointsOff();
  56. xyplot->PlotLinesOff();
  57. xyplot->PlotCurvePointsOn();
  58. xyplot->PlotCurveLinesOn();
  59. xyplot->SetXValuesToArcLength();
  60. xyplot->SetLabelFormat("%2.1f");
  61. xyplot->SetTitle("");
  62. xyplot->SetXTitle(x_label.c_str());
  63. xyplot->SetYTitle(y_label.c_str());
  64. if (semilogx) {
  65. xyplot->LogxOn();
  66. }
  67. vtkTextProperty* text_prop = xyplot->GetTitleTextProperty();
  68. text_prop->SetColor(0.0, 0.0, 0.0);
  69. text_prop->SetFontFamilyToArial();
  70. xyplot->SetAxisTitleTextProperty(text_prop);
  71. xyplot->SetAxisLabelTextProperty(text_prop);
  72. xyplot->SetTitleTextProperty(text_prop);
  73. }
  74. /// Destructor
  75. Plot2D_VTK::~Plot2D_VTK()
  76. {
  77. xyplot->Delete();
  78. rend->Delete();
  79. }
  80. /// Render current figure to screen
  81. void Plot2D_VTK::show()
  82. {
  83. rend->AddActor(xyplot);
  84. render_interactive(rend,xPix,yPix);
  85. }
  86. /// Render current figure to file
  87. void Plot2D_VTK::draw_to_png(std::string filename)
  88. {
  89. rend->AddActor(xyplot);
  90. render_to_png(rend,xPix,yPix,filename);
  91. }
  92. //==============================================================================
  93. Surf_VTK::Surf_VTK(int px, int py)
  94. {
  95. has_data = false;
  96. Lxy = -1;
  97. Lz = -1;
  98. xPix = px;
  99. yPix = py;
  100. gridfunc = vtkStructuredGrid::New();
  101. rend = vtkRenderer::New();
  102. }
  103. /// Destructor
  104. Surf_VTK::~Surf_VTK()
  105. {
  106. gridfunc->Delete();
  107. rend->Delete();
  108. }
  109. /// Clear plot data before reuse
  110. void Surf_VTK::purge()
  111. {
  112. assert(has_data);
  113. gridfunc->Delete();
  114. rend->Delete();
  115. gridfunc = vtkStructuredGrid::New();
  116. rend = vtkRenderer::New();
  117. has_data = false;
  118. Lxy = -1;
  119. Lz = -1;
  120. }
  121. void Surf_VTK::renderer(bool draw_axes, bool draw_colorbar, bool draw_box,
  122. bool do_warp)
  123. {
  124. assert(has_data);
  125. // filter to geometry primitive
  126. vtkStructuredGridGeometryFilter *geometry =
  127. vtkStructuredGridGeometryFilter::New();
  128. geometry->SetInputData(gridfunc);
  129. // warp to fit in box
  130. vtkWarpScalar *warp = vtkWarpScalar::New();
  131. if (do_warp)
  132. {
  133. double scale = Lxy/Lz;
  134. warp->SetInputConnection(geometry->GetOutputPort());
  135. warp->XYPlaneOn();
  136. warp->SetScaleFactor(scale);
  137. }
  138. // map gridfunction
  139. vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
  140. if (do_warp)
  141. mapper->SetInputConnection(warp->GetOutputPort());
  142. else
  143. mapper->SetInputConnection(geometry->GetOutputPort());
  144. double tmp[2];
  145. gridfunc->GetScalarRange(tmp);
  146. mapper->SetScalarRange(tmp[0], tmp[1]);
  147. // create plot surface actor
  148. vtkActor *surfplot = vtkActor::New();
  149. surfplot->SetMapper(mapper);
  150. // create outline
  151. vtkOutlineFilter *outlinefilter = vtkOutlineFilter::New();
  152. if (do_warp)
  153. outlinefilter->SetInputConnection(warp->GetOutputPort());
  154. else
  155. outlinefilter->SetInputConnection(geometry->GetOutputPort());
  156. vtkPolyDataMapper *outlineMapper = vtkPolyDataMapper::New();
  157. outlineMapper->SetInputConnection(outlinefilter->GetOutputPort());
  158. vtkActor *outline = vtkActor::New();
  159. outline->SetMapper(outlineMapper);
  160. outline->GetProperty()->SetColor(0, 0, 0);
  161. // create axes
  162. vtkAxesActor* axes = vtkAxesActor::New();
  163. axes->SetShaftTypeToCylinder();
  164. axes->SetNormalizedShaftLength( 0.85, 0.85, 0.85);
  165. axes->SetNormalizedTipLength( 0.15, 0.15, 0.15);
  166. axes->SetCylinderRadius( 0.500 * axes->GetCylinderRadius() );
  167. axes->SetConeRadius( 1.025 * axes->GetConeRadius() );
  168. axes->SetSphereRadius( 1.500 * axes->GetSphereRadius() );
  169. vtkTextProperty* text_prop_ax = axes->GetXAxisCaptionActor2D()->
  170. GetCaptionTextProperty();
  171. text_prop_ax->SetColor(0.0, 0.0, 0.0);
  172. text_prop_ax->SetFontFamilyToArial();
  173. text_prop_ax->SetFontSize(8);
  174. axes->GetYAxisCaptionActor2D()->GetCaptionTextProperty()->
  175. ShallowCopy(text_prop_ax);
  176. axes->GetZAxisCaptionActor2D()->GetCaptionTextProperty()->
  177. ShallowCopy(text_prop_ax);
  178. // create colorbar
  179. vtkScalarBarActor *colorbar = vtkScalarBarActor::New();
  180. colorbar->SetLookupTable(mapper->GetLookupTable());
  181. colorbar->SetWidth(0.085);
  182. colorbar->SetHeight(0.9);
  183. colorbar->SetPosition(0.9, 0.1);
  184. vtkTextProperty* text_prop_cb = colorbar->GetLabelTextProperty();
  185. text_prop_cb->SetColor(1.0, 1.0, 1.0);
  186. colorbar->SetLabelTextProperty(text_prop_cb);
  187. // renderer
  188. rend->AddActor(surfplot);
  189. if (draw_box)
  190. rend->AddActor(outline);
  191. if (draw_axes)
  192. rend->AddActor(axes);
  193. if (draw_colorbar)
  194. rend->AddActor(colorbar);
  195. rend->SetBackground(0.25, 0.25, 0.25);
  196. // renderer is now set up!
  197. // clean up
  198. colorbar->Delete();
  199. warp->Delete();
  200. axes->Delete();
  201. outline->Delete();
  202. outlinefilter->Delete();
  203. outlineMapper->Delete();
  204. surfplot->Delete();
  205. mapper->Delete();
  206. geometry->Delete();
  207. }
  208. //==============================================================================
  209. /// Default constructor
  210. Contour_VTK::Contour_VTK(int px, int py)
  211. {
  212. XScale = LINEAR;
  213. YScale = LINEAR;
  214. has_data = false;
  215. xPix = px;
  216. yPix = py;
  217. gridfunc = vtkRectilinearGrid::New();
  218. rend = vtkRenderer::New();
  219. }
  220. /// Default constructor
  221. Contour_VTK::Contour_VTK(int px, int py, SCALE xsc, SCALE ysc)
  222. {
  223. XScale = xsc;
  224. YScale = ysc;
  225. xlabel = "x";
  226. ylabel = "y";
  227. has_data = false;
  228. xPix = px;
  229. yPix = py;
  230. gridfunc = vtkRectilinearGrid::New();
  231. rend = vtkRenderer::New();
  232. }
  233. /// Destructor
  234. Contour_VTK::~Contour_VTK()
  235. {
  236. gridfunc->Delete();
  237. rend->Delete();
  238. }
  239. /// Clear plot data before reuse
  240. void Contour_VTK::purge()
  241. {
  242. assert(has_data);
  243. gridfunc->Delete();
  244. rend->Delete();
  245. gridfunc = vtkRectilinearGrid::New();
  246. rend = vtkRenderer::New();
  247. has_data = false;
  248. }
  249. /// Set labels for contour plots
  250. void Contour_VTK::SetXLabel(const std::string &xlab) {
  251. xlabel = xlab;
  252. }
  253. void Contour_VTK::SetYLabel(const std::string &ylab ) {
  254. ylabel = ylab;
  255. }
  256. void Contour_VTK::renderer(bool draw_colorbar, bool draw_surf, int lines)
  257. {
  258. assert(has_data);
  259. // filter to geometry primitive
  260. vtkRectilinearGridGeometryFilter *geometry =
  261. vtkRectilinearGridGeometryFilter::New();
  262. geometry->SetInputData(gridfunc);
  263. // map gridfunction
  264. vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
  265. mapper->SetInputConnection(geometry->GetOutputPort());
  266. double tmp[2];
  267. gridfunc->GetScalarRange(tmp);
  268. mapper->SetScalarRange(tmp[0], tmp[1]);
  269. // create plot surface actor
  270. vtkActor *surfplot = vtkActor::New();
  271. surfplot->SetMapper(mapper);
  272. // create contour lines (10 lines)
  273. vtkContourFilter *contlines = vtkContourFilter::New();
  274. contlines->SetInputConnection(geometry->GetOutputPort());
  275. double tempdiff = (tmp[1]-tmp[0])/(10*lines);
  276. contlines->GenerateValues(lines, tmp[0]+tempdiff, tmp[1]-tempdiff);
  277. vtkPolyDataMapper *contourMapper = vtkPolyDataMapper::New();
  278. contourMapper->SetInputConnection(contlines->GetOutputPort());
  279. if (draw_surf)
  280. contourMapper->ScalarVisibilityOff();
  281. else
  282. contourMapper->SetScalarRange(tmp[0], tmp[1]);
  283. vtkActor *contours = vtkActor::New();
  284. contours->SetMapper(contourMapper);
  285. contours->GetProperty()->SetOpacity(.25);
  286. contours->GetProperty()->SetColor(0.5,0.5,0.5);
  287. // create outline
  288. vtkOutlineFilter *outlinefilter = vtkOutlineFilter::New();
  289. outlinefilter->SetInputConnection(geometry->GetOutputPort());
  290. vtkPolyDataMapper *outlineMapper = vtkPolyDataMapper::New();
  291. outlineMapper->SetInputConnection(outlinefilter->GetOutputPort());
  292. vtkActor *outline = vtkActor::New();
  293. outline->SetMapper(outlineMapper);
  294. outline->GetProperty()->SetColor(0, 0, 0);
  295. // create colorbar
  296. vtkScalarBarActor *colorbar = vtkScalarBarActor::New();
  297. if (draw_surf)
  298. colorbar->SetLookupTable(mapper->GetLookupTable());
  299. else
  300. colorbar->SetLookupTable(contourMapper->GetLookupTable());
  301. //
  302. colorbar->SetWidth(0.085);
  303. colorbar->SetHeight(0.9);
  304. colorbar->SetPosition(0.9, 0.1);
  305. vtkTextProperty* text_prop_cb = colorbar->GetLabelTextProperty();
  306. text_prop_cb->SetColor(1.0, 1.0, 1.0);
  307. colorbar->SetLabelTextProperty(text_prop_cb);
  308. double xrange[2];
  309. double yrange[2];
  310. gridfunc->GetXCoordinates()->GetRange(xrange);
  311. gridfunc->GetYCoordinates()->GetRange(yrange);
  312. vtkCubeAxesActor2D *caxis = vtkCubeAxesActor2D::New();
  313. caxis->ZAxisVisibilityOff();
  314. caxis->SetXLabel(xlabel.c_str());
  315. caxis->SetYLabel(ylabel.c_str());
  316. vtkCamera* mycam = rend->MakeCamera(); // vtkCamera::New();
  317. //rend->SetActiveCamera(mycam);
  318. caxis->SetBounds( xrange[0], xrange[1], yrange[0], yrange[1], 0, 0);
  319. caxis->SetRanges( xrange[0], xrange[1], ymin, ymax, 0, 0);
  320. caxis->SetUseRanges(1);
  321. caxis->SetCamera( mycam );
  322. caxis->SetNumberOfLabels(6);
  323. // doesn't work, rotate y label 90 degrees
  324. caxis->GetYAxisActor2D()->GetTitleTextProperty()->SetOrientation(90.);
  325. rend->AddActor(caxis);
  326. mycam->Delete();
  327. caxis->Delete();
  328. // handdrwan label
  329. // // X label
  330. // vtkVectorText *xlabel = vtkVectorText::New();
  331. // vtkTransformPolyDataFilter *transxlabel = vtkTransformPolyDataFilter::New();
  332. // vtkTransform *trans = vtkTransform::New();
  333. // vtkPolyDataMapper *xlabmap = vtkPolyDataMapper::New();
  334. // vtkFollower *xlabact = vtkFollower::New();
  335. // locate and rotate as needed
  336. //double yranget = std::abs(yrange[1]-yrange[0]);
  337. //double xranget = std::abs(xrange[1]-xrange[0]);
  338. // trans->Identity();
  339. // trans->Scale(5,5,5); // TODO, is 5 always OK???
  340. // transxlabel->SetTransform(trans);
  341. // transxlabel->SetInputConnection(xlabel->GetOutputPort());
  342. // // TODO input name
  343. // xlabel->SetText("Frequency [Hz]");
  344. // transxlabel->SetInputConnection(xlabel->GetOutputPort());
  345. // xlabmap->SetInputConnection(transxlabel->GetOutputPort());
  346. // xlabact->SetMapper(xlabmap);
  347. // // centre between axis
  348. // xlabact->SetPosition( (xrange[0]+xrange[1])/2 - xlabact->GetCenter()[0],
  349. // yrange[0]-.2*yranget, 0 );
  350. // //rend->AddActor(xlabact);
  351. // // Y label
  352. // vtkVectorText *ylabel = vtkVectorText::New();
  353. // vtkTransformPolyDataFilter *transylabel = vtkTransformPolyDataFilter::New();
  354. // vtkTransform *ytrans = vtkTransform::New();
  355. // vtkPolyDataMapper *ylabmap = vtkPolyDataMapper::New();
  356. // vtkFollower *ylabact = vtkFollower::New();
  357. // // locate and rotate as needed
  358. // ytrans->Identity();
  359. // ytrans->Scale(5,5,5); // TODO don't hard code, calc from window size maybe??
  360. // ytrans->RotateZ(90);
  361. // transylabel->SetTransform(ytrans);
  362. // transylabel->SetInputConnection(ylabel->GetOutputPort());
  363. //
  364. // ylabel->SetText("Pulse moment [A sec]");
  365. // transylabel->SetInputConnection(ylabel->GetOutputPort());
  366. // ylabmap->SetInputConnection(transylabel->GetOutputPort());
  367. // ylabact->SetMapper(ylabmap);
  368. // ylabact->SetPosition( xrange[0]-.2*xranget,
  369. // (yrange[0]+yrange[1])/2 - ylabact->GetCenter()[1],
  370. // 0 );
  371. // //rend->AddActor(ylabact);
  372. if (draw_surf)
  373. rend->AddActor(surfplot);
  374. rend->AddActor(contours);
  375. rend->AddActor(outline);
  376. if (draw_colorbar) {
  377. rend->AddActor(colorbar);
  378. }
  379. rend->SetBackground(0.25, 0.25, 0.25);
  380. // double xrange[2];
  381. // double yrange[2];
  382. // gridfunc->GetXCoordinates()->GetRange(xrange);
  383. // gridfunc->GetYCoordinates()->GetRange(yrange);
  384. // vtkCubeAxesActor *caxis = vtkCubeAxesActor::New();
  385. // caxis->ZAxisVisibilityOff();
  386. // caxis->SetCamera(rend->GetActiveCamera());
  387. // caxis->SetBounds( xrange[0], xrange[1], yrange[0], yrange[1], 0, 1 );
  388. // rend->AddActor(caxis);
  389. //rend->AddActor(yaxis);
  390. // // clean up
  391. // xlabel->Delete();
  392. // transxlabel->Delete();
  393. // trans->Delete();
  394. // xlabmap->Delete();
  395. // xlabact->Delete();
  396. //
  397. // ylabel->Delete();
  398. // transylabel->Delete();
  399. // ytrans->Delete();
  400. // ylabmap->Delete();
  401. // ylabact->Delete();
  402. // renderer is now set up!
  403. //caxis->Delete();
  404. //yaxis->Delete();
  405. contours->Delete();
  406. contlines->Delete();
  407. contourMapper->Delete();
  408. outline->Delete();
  409. outlinefilter->Delete();
  410. outlineMapper->Delete();
  411. colorbar->Delete();
  412. surfplot->Delete();
  413. mapper->Delete();
  414. geometry->Delete();
  415. }
  416. //==============================================================================
  417. /// Default constructor
  418. Quiver_VTK::Quiver_VTK(int px, int py)
  419. {
  420. has_data = false;
  421. xPix = px;
  422. yPix = py;
  423. gridfunc = vtkRectilinearGrid::New();
  424. rend = vtkRenderer::New();
  425. }
  426. /// Destructor
  427. Quiver_VTK::~Quiver_VTK()
  428. {
  429. gridfunc->Delete();
  430. rend->Delete();
  431. }
  432. /// Clear plot data before reuse
  433. void Quiver_VTK::purge()
  434. {
  435. assert(has_data);
  436. gridfunc->Delete();
  437. rend->Delete();
  438. gridfunc = vtkRectilinearGrid::New();
  439. rend = vtkRenderer::New();
  440. has_data = false;
  441. }
  442. void Quiver_VTK::renderer(double s)
  443. {
  444. assert(has_data);
  445. // filter to geometry primitive
  446. vtkRectilinearGridGeometryFilter *geometry =
  447. vtkRectilinearGridGeometryFilter::New();
  448. geometry->SetInputData(gridfunc);
  449. // make a vector glyph
  450. vtkGlyphSource2D* vec = vtkGlyphSource2D::New();
  451. vec->SetGlyphTypeToArrow();
  452. vec->SetScale(s);
  453. vec->FilledOff();
  454. vtkGlyph3D* glyph = vtkGlyph3D::New();
  455. glyph->SetInputConnection(geometry->GetOutputPort());
  456. glyph->SetSourceConnection(vec->GetOutputPort());
  457. glyph->SetColorModeToColorByScalar();
  458. // map gridfunction
  459. vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
  460. mapper->SetInputConnection(glyph->GetOutputPort());
  461. double tmp[2];
  462. gridfunc->GetScalarRange(tmp);
  463. mapper->SetScalarRange(tmp[0], tmp[1]);
  464. // create plot quiver actor
  465. vtkActor *quiver_actor = vtkActor::New();
  466. quiver_actor->SetMapper(mapper);
  467. // create colorbar
  468. vtkScalarBarActor *colorbar = vtkScalarBarActor::New();
  469. colorbar->SetLookupTable(mapper->GetLookupTable());
  470. colorbar->SetWidth(0.085);
  471. colorbar->SetHeight(0.9);
  472. colorbar->SetPosition(0.9, 0.1);
  473. vtkTextProperty* text_prop_cb = colorbar->GetLabelTextProperty();
  474. text_prop_cb->SetColor(1.0, 1.0, 1.0);
  475. colorbar->SetLabelTextProperty(text_prop_cb);
  476. // create outline
  477. vtkOutlineFilter *outlinefilter = vtkOutlineFilter::New();
  478. outlinefilter->SetInputConnection(geometry->GetOutputPort());
  479. vtkPolyDataMapper *outlineMapper = vtkPolyDataMapper::New();
  480. outlineMapper->SetInputConnection(outlinefilter->GetOutputPort());
  481. vtkActor *outline = vtkActor::New();
  482. outline->SetMapper(outlineMapper);
  483. outline->GetProperty()->SetColor(0, 0, 0);
  484. // add actors to renderer
  485. rend->AddActor(quiver_actor);
  486. rend->AddActor(colorbar);
  487. rend->AddActor(outline);
  488. rend->SetBackground(0.25, 0.25, 0.25);
  489. // renderer is now set up!
  490. // clean up
  491. outline->Delete();
  492. outlinefilter->Delete();
  493. outlineMapper->Delete();
  494. vec->Delete();
  495. glyph->Delete();
  496. colorbar->Delete();
  497. quiver_actor->Delete();
  498. mapper->Delete();
  499. }
  500. //==============================================================================
  501. /** Start interactive rendereing (default camera).
  502. * Sets resolution to (xPix,yPix)
  503. */
  504. void matplot::render_interactive(vtkRenderer *rend, int xPix, int yPix)
  505. {
  506. vtkRenderWindow *renWin = vtkRenderWindow::New();
  507. vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  508. renWin->AddRenderer(rend);
  509. iren->SetRenderWindow(renWin);
  510. renWin->SetSize(xPix, yPix);
  511. // makes first frame look reasonable interactive
  512. renWin->Render();
  513. renWin->Start();
  514. // Start interactive rendering
  515. iren->Initialize();
  516. iren->Start();
  517. iren->Disable();
  518. iren->Delete();
  519. renWin->Delete();
  520. }
  521. /** Start interactive rendereing (manual camera placement).
  522. * Sets resolution to (xPix,yPix)
  523. */
  524. void matplot::render_interactive_cam(vtkRenderer *rend, int xPix, int yPix,
  525. double cam[3], double focal[3])
  526. {
  527. rend->GetActiveCamera()->SetViewUp(0, 0, 1);
  528. rend->GetActiveCamera()->SetPosition(cam);
  529. rend->GetActiveCamera()->SetFocalPoint(focal);
  530. vtkRenderWindow *renWin = vtkRenderWindow::New();
  531. vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  532. renWin->AddRenderer(rend);
  533. iren->SetRenderWindow(renWin);
  534. renWin->SetSize(xPix, yPix);
  535. // Start interactive rendering
  536. iren->Initialize();
  537. iren->Start();
  538. iren->Delete();
  539. renWin->Delete();
  540. }
  541. /// Renders scene to PNG (default camera)
  542. void matplot::render_to_png(vtkRenderer *rend, int xPix, int yPix,
  543. std::string fname)
  544. {
  545. vtkRenderWindow *renWin = vtkRenderWindow::New();
  546. renWin->AddRenderer(rend);
  547. renWin->SetSize(xPix, yPix);
  548. renWin->Render();
  549. rend->Render();
  550. vtkRenderLargeImage* renderLarge = vtkRenderLargeImage::New();
  551. renderLarge->SetInput(rend);
  552. renderLarge->SetMagnification(1);
  553. vtkPNGWriter *pngfile = vtkPNGWriter::New();
  554. pngfile->SetFileName(fname.c_str());
  555. pngfile->SetInputConnection(renderLarge->GetOutputPort());
  556. pngfile->Update();
  557. pngfile->Update();
  558. pngfile->Update();
  559. pngfile->Write();
  560. renderLarge->Delete();
  561. pngfile->Delete();
  562. renWin->Delete();
  563. }
  564. /// Renders scene to PNG (manual camera placement)
  565. void matplot::render_to_png_cam(vtkRenderer *rend, int xPix, int yPix,
  566. std::string fname, double cam[3],
  567. double focal[3])
  568. {
  569. rend->GetActiveCamera()->SetViewUp(0, 0, 1);
  570. rend->GetActiveCamera()->SetPosition(cam);
  571. rend->GetActiveCamera()->SetFocalPoint(focal);
  572. vtkRenderWindow *renWin = vtkRenderWindow::New();
  573. renWin->AddRenderer(rend);
  574. renWin->SetSize(xPix, yPix);
  575. vtkRenderLargeImage* renderLarge = vtkRenderLargeImage::New();
  576. renderLarge->SetInput(rend);
  577. renderLarge->SetMagnification(1);
  578. //vtkWindowToImageFilter *w2i = vtkWindowToImageFilter::New();
  579. vtkPNGWriter *pngfile = vtkPNGWriter::New();
  580. //pngfile->SetInputConnection(w2i->GetOutputPort());
  581. pngfile->SetInputConnection(renderLarge->GetOutputPort());
  582. pngfile->SetFileName(fname.c_str());
  583. //w2i->SetInput(renWin);
  584. //w2i->Update();
  585. renWin->Render();
  586. pngfile->Write();
  587. //w2i->Delete();
  588. renderLarge->Delete();
  589. pngfile->Delete();
  590. renWin->Delete();
  591. }
  592. #endif