diff --git a/pgfplotter b/pgfplotter index 19fa13a..b1a3f60 100644 --- a/pgfplotter +++ b/pgfplotter @@ -8,6 +8,11 @@ namespace pgfplotter { + template + using require_iterable = std::enable_if_t())), decltype(std::end(std::declval()))>::value>; + std::array, 3> mesh_grid(const std::function& f, double xMin, double xMax, double yMin, double yMax, std::size_t res); @@ -17,12 +22,8 @@ namespace pgfplotter class Plotter { - friend void plot_multiple(const std::string&, const Plotter&, const - Plotter&, bool); - friend void plot_multiple(const std::string&, const Plotter&, const - Plotter&, const Plotter&, bool); - friend void plot_multiple(const std::string&, const std::vector< - Plotter>&, bool); + friend void plot(const std::string&, const std::vector&); static constexpr std::array, 5> _colorCycle = {{ { 0, 57, 181}, @@ -32,12 +33,10 @@ namespace pgfplotter {255, 227, 0} }}; - const std::string name; - - std::string titleStr; - std::string xLabel; - std::string yLabel; - std::string zLabel; + std::string _title; + std::string _xLabel; + std::string _yLabel; + std::string _zLabel; std::vector, 3>> data; std::vector> surfaceX; @@ -142,10 +141,10 @@ namespace pgfplotter static constexpr unsigned int Northwest = 5; static constexpr unsigned int Southwest = 6; - void title(const std::string& title); - void x_label(const std::string& xLabel); - void y_label(const std::string& yLabel); - void z_label(const std::string& zLabel); + void setTitle(const std::string& title); + void setXLabel(const std::string& xLabel); + void setYLabel(const std::string& yLabel); + void setZLabel(const std::string& zLabel); void line(const std::vector& x, const std::vector& y, const std::vector& z, const std::string& name, const std:: @@ -451,7 +450,6 @@ namespace pgfplotter void axis_equal_image(); void resize(double width, double height); void resize(double size); - void plot(const std::string& path, bool deleteData = false) const; void x_precision(int n); void y_precision(int n); void z_precision(int n); @@ -475,12 +473,32 @@ namespace pgfplotter void bidirColormap(); }; - void plot_multiple(const std::string& path, const Plotter& p, const Plotter& - q, bool deleteData = false); - void plot_multiple(const std::string& path, const Plotter& p, const Plotter& - q, const Plotter& r, bool deleteData = false); - void plot_multiple(const std::string& path, const std::vector& p, - bool deleteData = false); + // Note: ".png" is automatically appended to plot path. + template + void plot(const std::string& path, const Plotter& p, Ts&&... q) + { + std::vector ptrs = {&p}; + plot(path, ptrs, q...); + } + template + void plot(const std::string& path, std::vector ptrs, const + Plotter& p, Ts&&... q) + { + ptrs.push_back(&p); + plot(path, ptrs, q...); + } + template* = nullptr> + void plot(const std::string& path, const T& p) + { + std::vector ptrs; + ptrs.reserve(p.size()); + for(auto& n : p) + { + ptrs.push_back(&n); + } + plot(path, ptrs); + } + void plot(const std::string& path, const std::vector& ptrs); } #endif diff --git a/plotter.cpp b/plotter.cpp index 771ed90..f6e9a06 100644 --- a/plotter.cpp +++ b/plotter.cpp @@ -423,24 +423,24 @@ static const std::string src4 = "\\end{groupplot}" + endl + static const std::string src5 = "\\end{document}"; static const std::string src7 = "" + endl; -void pgfplotter::Plotter::title(const std::string& title) +void pgfplotter::Plotter::setTitle(const std::string& title) { - titleStr = title; + _title = title; } -void pgfplotter::Plotter::x_label(const std::string& xLabel) +void pgfplotter::Plotter::setXLabel(const std::string& xLabel) { - this->xLabel = xLabel; + _xLabel = xLabel; } -void pgfplotter::Plotter::y_label(const std::string& yLabel) +void pgfplotter::Plotter::setYLabel(const std::string& yLabel) { - this->yLabel = yLabel; + _yLabel = yLabel; } -void pgfplotter::Plotter::z_label(const std::string& zLabel) +void pgfplotter::Plotter::setZLabel(const std::string& zLabel) { - this->zLabel = zLabel; + _zLabel = zLabel; } void pgfplotter::Plotter::line(const std::vector& x, const std::vector& y, @@ -877,9 +877,9 @@ std::string pgfplotter::Plotter::plot_src(const std::string& path, int subplot) src += ", /pgf/number format/sci"; } src += "}, label style = {font = \\" + FontSize + "}, ylabel near ticks"; - if(!zLabel.empty()) + if(!_zLabel.empty()) { - src += ", ylabel = {" + zLabel + "}"; + src += ", ylabel = {" + _zLabel + "}"; } src += "}"; @@ -1007,21 +1007,21 @@ std::string pgfplotter::Plotter::plot_src(const std::string& path, int subplot) zMax);//TEMP }*/ - if(!xLabel.empty()) + if(!_xLabel.empty()) { - src += ", xlabel = {" + xLabel + "}"; + src += ", xlabel = {" + _xLabel + "}"; } - if(!yLabel.empty()) + if(!_yLabel.empty()) { - src += ", ylabel = {" + yLabel + "}"; + src += ", ylabel = {" + _yLabel + "}"; } - if(!zLabel.empty()) + if(!_zLabel.empty()) { - src += ", zlabel = {" + zLabel + "}"; + src += ", zlabel = {" + _zLabel + "}"; } - if(!titleStr.empty()) + if(!_title.empty()) { - src += ", title = {\\" + TitleSize + " " + titleStr + "}"; + src += ", title = {\\" + TitleSize + " " + _title + "}"; } if(legendPos) { @@ -1316,37 +1316,8 @@ std::string pgfplotter::Plotter::plot_src(const std::string& path, int subplot) return src; } -void pgfplotter::Plotter::plot(const std::string& path, bool deleteData) const -{ - const std::string src = src0 + src2a + "1" + src2bNoSep + plot_src(path, 0) - + src4 + src5; - - compile(path, src, deleteData); -} - -void pgfplotter::plot_multiple(const std::string& path, const pgfplotter::Plotter& p, const pgfplotter::Plotter& q, - bool deleteData) -{ - const bool b = p._noSep || q._noSep; - const std::string src = src0 + src2a + "2" + (b ? src2bNoSep : src2b) + p. - plot_src(path, 0) + q.plot_src(path, 1) + src4 + src5; - - compile(path, src, deleteData); -} - -void pgfplotter::plot_multiple(const std::string& path, const pgfplotter::Plotter& p, const pgfplotter::Plotter& q, - const pgfplotter::Plotter& r, bool deleteData) -{ - const bool b = p._noSep || q._noSep || r._noSep; - const std::string src = src0 + src2a + "3" + (b ? src2bNoSep : src2b) + p. - plot_src(path, 0) + q.plot_src(path, 1) + r.plot_src(path, 2) + src4 + - src5; - - compile(path, src, deleteData); -} - -void pgfplotter::plot_multiple(const std::string& path, const std::vector& p, bool - deleteData) +void pgfplotter::plot(const std::string& path, const std::vector& p) { if(p.empty()) { @@ -1358,16 +1329,16 @@ void pgfplotter::plot_multiple(const std::string& path, const std::vector_noSep; } std::string src = src0 + src2a + std::to_string(p.size()) + (b ? src2bNoSep : src2b); for(std::size_t i = 0, n = p.size(); i < n; ++i) { - src += p[i].plot_src(path, i); + src += p[i]->plot_src(path, i); } src += src4 + src5; - compile(path, src, deleteData); + compile(path, src, false); } diff --git a/test/Makefile b/test/Makefile index 95a8718..2fd943f 100644 --- a/test/Makefile +++ b/test/Makefile @@ -66,4 +66,4 @@ build: mkdir -p $@ clean: - $(RM) test build/* output/* + $(RM) -r test build output diff --git a/test/test.cpp b/test/test.cpp index 02c9bd4..5e5d577 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -13,6 +13,8 @@ std::cout << "Passed " << test << std::endl; \ ++test; +namespace pgf = pgfplotter; + static const std::string PlotName = "plot"; static std::string get_dir(const std::string& path) @@ -38,11 +40,11 @@ int main(int, char** argv) } CATCH + pgf::Plotter p; try { const auto data = pgfplotter::mesh_grid([](double x, double y){ return std::sin(x)*std::sin(y); }, 0., 1., 0., 1., 50); - pgfplotter::Plotter p; p.surf(data[0], data[1], data[2]); p.x_min(0.); p.x_max(1.); @@ -51,10 +53,11 @@ int main(int, char** argv) p.z_min(0.); p.z_max(1.); p.view(45., 45.); - p.x_label("$x$ (\\si{\\lu})"); - p.y_label("$y$ (\\si{\\arcsec})"); - p.z_label("$\\dv{^2x}{y^2}$ (\\si{\\lu^2\\per\\arcsec^2})"); - p.plot(outputDir + "/" + PlotName); + p.setXLabel("$x$ (\\si{\\lu})"); + p.setYLabel("$y$ (\\si{\\arcsec})"); + p.setZLabel("$\\dv{^2x}{y^2}$ (\\si{\\lu^2\\per\\arcsec^2})"); + p.setTitle("Test Plot"); + pgf::plot(outputDir + "/" + PlotName, p); } CATCH @@ -102,4 +105,17 @@ int main(int, char** argv) } } CATCH + + try + { + pgf::plot(outputDir + "/" + PlotName + "-1", p, p); + } + CATCH + + try + { + std::vector v(2, p); + pgf::plot(outputDir + "/" + PlotName + "-2", v); + } + CATCH }