123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634 |
- #ifndef __MATPLOT_VTK
- #define __MATPLOT_VTK
-
-
-
-
-
-
-
-
- #include <assert.h>
- #include <string>
- #include <cmath>
-
-
- #include "vtkFloatArray.h"
- #include "vtkPointData.h"
- #include "vtkPoints.h"
- #include "vtkRectilinearGrid.h"
- #include "vtkRenderer.h"
- #include "vtkStructuredGrid.h"
- #include "vtkXYPlotActor.h"
-
- namespace matplot {
-
- enum SCALE {LINEAR, LOG10};
-
- void render_interactive(vtkRenderer *rend, int xPix, int yPix);
-
- void render_interactive_camera(vtkRenderer *rend, int xPix, int yPix,
- vtkCamera* mycam);
-
- void render_interactive_cam(vtkRenderer *rend, int xPix, int yPix,
- double cam[3], double focal[3]);
-
- void render_to_png(vtkRenderer *rend, int xPix, int yPix,std::string fname);
-
- void render_to_png_cam(vtkRenderer *rend, int xPix, int yPix,
- std::string fname, double cam[3], double focal[3]);
-
-
-
- class Plot2D_VTK {
-
- public:
-
- Plot2D_VTK(std::string x_label="x", std::string y_label="y",
- int xpix = 800, int ypix = 600, bool semilogx=false );
- ~Plot2D_VTK();
-
-
-
- template<typename Vec_t>
- void plot(const Vec_t &x, const Vec_t &y) {
- double color[3] = { 0, 0, 1.0 };
- plot(x, y, color, "-");
- }
-
-
-
- template<typename Vec_t>
- void plot(const Vec_t &x, const Vec_t &y, const double col[3],
- const std::string linespec) {
-
- int i, N = x.size(), plot_points = 0, plot_lines = 0;
-
- vtkRectilinearGrid *curve;
- vtkFloatArray *yVal;
- vtkFloatArray *xVal;
-
-
- if (linespec == "-")
- plot_lines = 1;
- else if (linespec == ".")
- plot_points = 1;
- else if (linespec == ".-" || linespec == "-.") {
- plot_points = 1;
- plot_lines = 1;
- }
-
-
- xVal = vtkFloatArray::New();
- yVal = vtkFloatArray::New();
-
- for (i=0; i<N; i++) {
- xVal->InsertNextTuple1(x[i]);
- yVal->InsertNextTuple1(y[i]);
- }
-
-
- curve = vtkRectilinearGrid::New();
- curve->SetDimensions(N, 1, 1);
- curve->SetXCoordinates(xVal);
- curve->GetPointData()->SetScalars(yVal);
-
-
- xyplot->AddDataSetInput(curve);
-
- if (semilogx) {
- xyplot->LogxOn();
- }
-
-
-
-
- xyplot->SetXValuesToValue();
-
-
- xyplot->SetPlotColor(plot_no, col[0], col[1], col[2]);
- xyplot->SetPlotLines(plot_no, plot_lines);
- xyplot->SetPlotPoints(plot_no, plot_points);
-
- plot_no++;
-
- xVal->Delete();
- yVal->Delete();
- curve->Delete();
- }
-
- void show();
- void draw_to_png(std::string filename);
-
- private:
-
- int xPix;
- int yPix;
- bool semilogx;
- int plot_no;
-
-
- vtkRenderer* rend;
- vtkXYPlotActor* xyplot;
- };
-
-
-
- class Surf_VTK
- {
- public:
-
- Surf_VTK(int px = 800, int py = 600);
- ~Surf_VTK();
-
-
-
- template<typename Vec_t, typename Mat_t>
- void surf(const Vec_t &x, const Vec_t &y, const Mat_t &z)
- {
- geometry(x, y, z);
- renderer(false, true, true, false);
- render_interactive(rend, xPix, yPix);
- }
-
-
-
- template<typename Vec_t, typename Mat_t>
- void surf(const Vec_t &x, const Vec_t &y, const Mat_t &z, bool do_warp)
- {
- geometry(x, y, z);
- renderer(false, true, true, do_warp);
- render_interactive(rend, xPix, yPix);
- }
-
-
-
- template<typename Vec_t, typename Mat_t>
- void surf(const Vec_t &x, const Vec_t &y, const Mat_t &z, bool do_warp,
- double observer[3], double focal[3])
- {
- geometry(x, y, z);
- renderer(false, true, true, do_warp);
- render_interactive_cam(rend, xPix, yPix, observer, focal);
- }
-
-
-
- template<typename Vec_t, typename Mat_t>
- void surf_to_file(const Vec_t &x, const Vec_t &y, const Mat_t &z,
- bool do_warp, std::string fname, double observer[3],
- double focal[3])
- {
- geometry(x, y, z);
- renderer(false, true, true, do_warp);
- render_to_png_cam(rend, xPix, yPix, fname, observer, focal);
- }
-
- void purge();
-
- private:
- vtkStructuredGrid *gridfunc;
- vtkRenderer *rend;
-
- double Lxy, Lz;
- bool has_data;
-
- int xPix, yPix;
-
- template<typename Vec_t, typename Mat_t>
- void geometry(const Vec_t &x, const Vec_t &y, const Mat_t &z)
- {
- const unsigned int Nx = x.size();
- const unsigned int Ny = y.size();
- unsigned int i, j, k;
-
-
- assert(Nx == z.rows() );
- assert(Ny == z.cols() );
- assert(!has_data);
-
-
- if (x(Nx-1)-x(0) > y(Ny-1)-y(0))
- Lxy = x(Nx-1)-x(0);
- else
- Lxy = y(Ny-1)-y(0);
- double z_low = 10000, z_upp = -10000;
-
-
- gridfunc->SetDimensions(Nx, Ny, 1);
-
- vtkPoints *points = vtkPoints::New();
- for (j = 0; j < Ny; j++)
- {
- for (i = 0; i < Nx; i++)
- {
- points->InsertNextPoint(x(i), y(j), z(i, j));
-
- if (z(i, j)< z_low)
- z_low = z(i, j);
- if (z(i, j)> z_upp)
- z_upp = z(i, j);
- }
- }
- gridfunc->SetPoints(points);
-
-
- vtkFloatArray *colors = vtkFloatArray::New();
- colors->SetNumberOfComponents(1);
- colors->SetNumberOfTuples(Nx*Ny);
- k = 0;
- for (j = 0; j < Ny; j++)
- for (i = 0; i < Nx; i++)
- {
- colors->InsertComponent(k, 0, z(i, j));
- k++;
- }
-
- gridfunc->GetPointData()->SetScalars(colors);
-
- points->Delete();
- colors->Delete();
-
- has_data = true;
- Lz = z_upp-z_low;
- }
-
- void renderer(bool, bool, bool, bool);
-
- };
-
-
-
-
- class Contour_VTK {
-
- public:
-
- Contour_VTK(int px = 800, int py = 600);
-
-
- Contour_VTK(int px = 800, int py = 600, SCALE xscale=LINEAR, SCALE
- yscale=LINEAR);
-
- ~Contour_VTK();
-
-
-
- template<typename Vec_t, typename Mat_t>
- void contour(const Vec_t &x, const Vec_t &y, const Mat_t &z) {
- geometry(x, y, z);
- renderer(true, false, 10);
- render_interactive(rend, xPix, yPix);
- }
-
-
-
- template<typename Vec_t, typename Mat_t>
- void contour(const Vec_t &x, const Vec_t &y, const Mat_t &z,
- bool draw_surf, int num_lines) {
- geometry(x, y, z);
- renderer(true, draw_surf, num_lines);
- render_interactive(rend, xPix, yPix);
- }
-
-
-
-
- template<typename Vec_t, typename Mat_t>
- void contour_to_file(const Vec_t &x, const Vec_t &y, const Mat_t &z,
- bool draw_surf, int num_lines, std::string fname)
- {
- geometry(x, y, z);
- renderer(true, draw_surf, num_lines);
- render_to_png(rend, xPix, yPix, fname);
- }
-
- void purge();
-
- void SetXLabel(const std::string &xlab);
- void SetYLabel(const std::string &ylab);
-
- private:
-
- SCALE XScale;
- SCALE YScale;
-
- vtkRectilinearGrid *gridfunc;
- vtkRenderer *rend;
-
- bool has_data;
-
- int xPix, yPix;
- double axscale;
- double ymin;
- double ymax;
- std::string xlabel;
- std::string ylabel;
-
- template<typename Vec_t, typename Mat_t>
-
- void geometry(const Vec_t &x, const Vec_t &y, const Mat_t &z) {
-
- const unsigned int Nx = x.size();
- const unsigned int Ny = y.size();
- unsigned int i, j, k;
-
-
- assert(Nx == z.rows() );
- assert(Ny == z.cols() );
- assert(!has_data);
-
-
- vtkFloatArray *xcoord = vtkFloatArray::New();
- xcoord->SetNumberOfComponents(1);
- xcoord->SetNumberOfTuples(Nx);
- vtkFloatArray *ycoord = vtkFloatArray::New();
- ycoord->SetNumberOfComponents(1);
- ycoord->SetNumberOfTuples(Ny);
-
-
-
- axscale = 1.;
- ymin = y.minCoeff();
- ymax = y.maxCoeff();
-
- if (YScale == LINEAR && XScale == LINEAR)
- axscale = (x.maxCoeff() - x.minCoeff()) /
- (y.maxCoeff() - y.minCoeff()) ;
-
- if (YScale == LOG10 && XScale == LINEAR)
- axscale = ( x.maxCoeff() - x.minCoeff() ) /
- (std::log10(y.maxCoeff()) - std::log10(y.minCoeff())) ;
-
- if (YScale == LOG10 && XScale == LOG10)
- axscale = ( (std::log10(x.maxCoeff()) - std::log10(x.minCoeff())) /
- (std::log10(y.maxCoeff()) - std::log10(y.minCoeff())) );
-
- if (YScale == LINEAR && XScale == LOG10)
- axscale = (std::log10(x.maxCoeff()) - std::log10(x.minCoeff())) /
- ( y.maxCoeff() - y.minCoeff() ) ;
-
- if (XScale == LINEAR) {
- for (i=0; i<Nx; i++)
- xcoord->InsertComponent(i, 0, x(i));
- } else {
- for (i=0; i<Nx; i++)
- xcoord->InsertComponent(i, 0, std::log10(x(i)));
- }
-
- if (YScale == LINEAR) {
- for (i=0; i<Ny; i++)
- ycoord->InsertComponent(i, 0, axscale*y(i));
- } else {
- for (i=0; i<Ny; i++)
- ycoord->InsertComponent(i, 0, axscale*std::log10(y(i)));
- }
-
-
-
- gridfunc->SetDimensions(Nx, Ny, 1);
- gridfunc->SetXCoordinates(xcoord);
- gridfunc->SetYCoordinates(ycoord);
-
-
- vtkFloatArray *colors = vtkFloatArray::New();
- colors->SetNumberOfComponents(1);
- colors->SetNumberOfTuples(Nx*Ny);
- k = 0;
- for (j = 0; j < Ny; j++)
- for (i = 0; i < Nx; i++) {
- colors->InsertComponent(k, 0, z(i, j));
- k++;
- }
-
- gridfunc->GetPointData()->SetScalars(colors);
-
- colors->Delete();
- xcoord->Delete();
- ycoord->Delete();
-
- has_data = true;
- }
-
- void renderer(bool, bool, int);
- };
-
-
-
- class Quiver_VTK
- {
- public:
-
- Quiver_VTK(int px = 800, int py = 600);
- ~Quiver_VTK();
-
-
-
- template<typename Vec_t, typename Mat_t>
- void quiver(const Vec_t &x, const Vec_t &y, const Mat_t &u,
- const Mat_t &v)
- {
- geometry(x, y, u, v);
- renderer(1.0);
- render_interactive(rend, xPix, yPix);
- }
-
-
-
- template<typename Vec_t, typename Mat_t>
- void quiver(const Vec_t &x, const Vec_t &y, const Mat_t &u,
- const Mat_t &v, double s)
- {
- geometry(x, y, u, v);
- renderer(s);
- render_interactive(rend, xPix, yPix);
- }
-
-
-
- template<typename Vec_t, typename Mat_t>
- void quiver_to_file(const Vec_t &x, const Vec_t &y, const Mat_t &u,
- const Mat_t &v, double s, std::string filename)
- {
- geometry(x, y, u, v);
- renderer(s);
- render_to_png(rend, xPix, yPix, filename);
- }
-
- void purge();
-
- private:
- vtkRectilinearGrid *gridfunc;
- vtkRenderer *rend;
-
- bool has_data;
-
- int xPix, yPix;
-
- template<typename Vec_t, typename Mat_t>
- void geometry(const Vec_t& x, const Vec_t& y, const Mat_t& u,
- const Mat_t& v)
- {
- const unsigned int Nx = x.size();
- const unsigned int Ny = y.size();
- unsigned int i, j, k;
-
-
- assert(Nx == u.rows());
- assert(Ny == u.cols());
- assert(Nx == v.rows());
- assert(Ny == v.cols());
- assert(!has_data);
-
-
- vtkFloatArray *xcoord = vtkFloatArray::New();
- xcoord->SetNumberOfComponents(1);
- xcoord->SetNumberOfTuples(Nx);
- vtkFloatArray *ycoord = vtkFloatArray::New();
- ycoord->SetNumberOfComponents(1);
- ycoord->SetNumberOfTuples(Ny);
-
- for (i=0; i<Nx; i++)
- xcoord->InsertComponent(i, 0, x(i));
- for (i=0; i<Ny; i++)
- ycoord->InsertComponent(i, 0, y(i));
-
-
- gridfunc->SetDimensions(Nx, Ny, 1);
- gridfunc->SetXCoordinates(xcoord);
- gridfunc->SetYCoordinates(ycoord);
-
-
- vtkFloatArray *colors = vtkFloatArray::New();
- colors->SetNumberOfComponents(1);
- colors->SetNumberOfTuples(Nx*Ny);
-
-
- vtkFloatArray *vectors = vtkFloatArray::New();
- vectors->SetNumberOfComponents(3);
- vectors->SetNumberOfTuples(Nx*Ny);
-
- k = 0;
- for (j = 0; j < Ny; j++)
- for (i = 0; i < Nx; i++)
- {
- colors->InsertTuple1(k,sqrt(u(i,j)*u(i,j)+v(i,j)*v(i,j)));
- vectors->InsertTuple3(k, u(i, j), v(i, j), 0.0);
- k++;
- }
-
- gridfunc->GetPointData()->SetScalars(colors);
- gridfunc->GetPointData()->SetVectors(vectors);
-
- vectors->Delete();
- colors->Delete();
- xcoord->Delete();
- ycoord->Delete();
-
- has_data = true;
- }
-
- void renderer(double);
-
- };
-
- }
-
- #endif
|