1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-14 01:45:35 +00:00
LatAnalyze/lib/Plot.hpp

335 lines
8.4 KiB
C++
Raw Normal View History

/*
* Plot.hpp, part of LatAnalyze 3
*
2015-06-11 14:04:54 +01:00
* Copyright (C) 2013 - 2015 Antonin Portelli
*
* LatAnalyze 3 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LatAnalyze 3 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Latan_Plot_hpp_
#define Latan_Plot_hpp_
2014-03-13 18:51:01 +00:00
#include <LatAnalyze/Global.hpp>
#include <LatAnalyze/Function.hpp>
2014-03-13 18:51:01 +00:00
#include <LatAnalyze/Mat.hpp>
#include <LatAnalyze/MatSample.hpp>
#include <LatAnalyze/Histogram.hpp>
2016-04-04 19:09:18 +01:00
#include <LatAnalyze/XYStatData.hpp>
#include <sstream>
#include <vector>
// gnuplot default parameters
#ifndef GNUPLOT_BIN
#define GNUPLOT_BIN "gnuplot"
#endif
#ifndef GNUPLOT_ARGS
#define GNUPLOT_ARGS "-p"
#endif
BEGIN_LATAN_NAMESPACE
/******************************************************************************
2014-03-03 12:41:48 +00:00
* Plot objects *
******************************************************************************/
2014-03-03 12:41:48 +00:00
class PlotObject
{
public:
2014-02-20 22:54:11 +00:00
// destructor
2014-03-03 12:41:48 +00:00
virtual ~PlotObject(void) = default;
// access
2016-04-05 12:51:00 +01:00
std::string popTmpFile(void);
const std::string & getCommand(void) const;
const std::string & getHeadCommand(void) const;
2014-03-03 12:41:48 +00:00
// test
2016-04-05 12:51:00 +01:00
bool gotTmpFile(void) const;
protected:
2014-03-03 12:41:48 +00:00
// access
void pushTmpFile(const std::string &fileName);
void setCommand(const std::string &command);
2015-06-26 20:33:57 +01:00
void setHeadCommand(const std::string &command);
2014-03-03 18:33:53 +00:00
// dump a matrix to a temporary file
std::string dumpToTmpFile(const DMat &m);
2014-03-03 12:41:48 +00:00
private:
// plot command
std::string command_;
2015-06-26 20:33:57 +01:00
std::string headCommand_;
2014-03-03 12:41:48 +00:00
// stack of created temporary files
std::stack<std::string> tmpFileName_;
};
class PlotCommand: public PlotObject
{
public:
2014-03-03 18:33:53 +00:00
// constructor
2014-03-03 12:41:48 +00:00
explicit PlotCommand(const std::string &command);
// destructor
virtual ~PlotCommand(void) = default;
};
2015-06-26 20:33:57 +01:00
class PlotHeadCommand: public PlotObject
{
public:
// constructor
explicit PlotHeadCommand(const std::string &command);
// destructor
virtual ~PlotHeadCommand(void) = default;
};
2014-03-03 12:41:48 +00:00
class PlotData: public PlotObject
{
public:
2014-03-03 18:33:53 +00:00
// constructor
2015-07-07 18:50:36 +01:00
PlotData(const DMatSample &x, const DMatSample &y);
PlotData(const DVec &x, const DMatSample &y);
PlotData(const DMatSample &x, const DVec &y);
PlotData(const XYStatData &data, const Index i = 0, const Index j = 0);
2014-03-03 12:41:48 +00:00
// destructor
virtual ~PlotData(void) = default;
2014-03-03 18:33:53 +00:00
};
2015-02-13 15:26:00 +00:00
class PlotHLine: public PlotObject
{
public:
// constructor
PlotHLine(const double y);
// destructor
virtual ~PlotHLine(void) = default;
};
2014-03-03 18:33:53 +00:00
class PlotFunction: public PlotObject
{
public:
// constructor
PlotFunction(const DoubleFunction &function, const double xMin,
2014-04-17 12:19:13 +01:00
const double xMax, const unsigned int nPoint = 1000);
2014-03-03 18:33:53 +00:00
// destructor
virtual ~PlotFunction(void) = default;
2014-03-03 12:41:48 +00:00
};
2014-04-18 11:29:17 +01:00
class PlotPredBand: public PlotObject
{
public:
// constructor
PlotPredBand(const DoubleFunctionSample &function, const double xMin,
const double xMax, const unsigned int nPoint = 1000,
const double opacity = 0.15);
// destructor
virtual ~PlotPredBand(void) = default;
};
class PlotHistogram: public PlotObject
{
public:
// constructor
PlotHistogram(const Histogram &h);
// destructor
virtual ~PlotHistogram(void) = default;
};
2016-04-05 12:51:00 +01:00
class PlotMatrixNoRange: public PlotObject
{
public:
// constructor
PlotMatrixNoRange(const DMat &m);
// destructor
virtual ~PlotMatrixNoRange(void) = default;
};
#define PlotMatrix(m)\
PlotRange(Axis::x, -.5, (m).cols() - .5) <<\
PlotRange(Axis::y, -.5, (m).rows() - .5) <<\
PlotMatrixNoRange(m)
/******************************************************************************
2014-03-03 12:41:48 +00:00
* Plot modifiers *
******************************************************************************/
2014-03-03 12:41:48 +00:00
enum class Axis {x = 0, y = 1};
struct Range
{
double min, max;
};
struct PlotOptions
{
2014-04-03 09:00:53 +01:00
std::string terminal;
std::string output;
2014-06-02 16:04:20 +01:00
std::string caption;
2014-04-03 09:00:53 +01:00
std::string title;
unsigned int scaleMode[2];
Range scale[2];
std::string label[2];
std::string lineColor;
2014-03-03 12:41:48 +00:00
};
class PlotModifier
{
public:
// destructor
virtual ~PlotModifier(void) = default;
// modifier
virtual void operator()(PlotOptions &option) const = 0;
};
2014-06-02 16:04:20 +01:00
class Caption: public PlotModifier
{
public:
// constructor
explicit Caption(const std::string &title);
// destructor
virtual ~Caption(void) = default;
// modifier
virtual void operator()(PlotOptions &option) const;
private:
const std::string caption_;
};
2015-02-13 15:26:00 +00:00
class Label: public PlotModifier
{
public:
// constructor
explicit Label(const std::string &label, const Axis axis);
// destructor
virtual ~Label(void) = default;
// modifier
virtual void operator()(PlotOptions &option) const;
private:
const std::string label_;
const Axis axis_;
};
2014-03-03 12:41:48 +00:00
class Color: public PlotModifier
{
public:
// constructor
explicit Color(const std::string &color);
// destructor
virtual ~Color(void) = default;
// modifier
virtual void operator()(PlotOptions &option) const;
private:
const std::string color_;
};
class LogScale: public PlotModifier
{
public:
// constructor
explicit LogScale(const Axis axis);
// destructor
virtual ~LogScale(void) = default;
// modifier
virtual void operator()(PlotOptions &option) const;
private:
const Axis axis_;
};
class PlotRange: public PlotModifier
{
public:
2014-09-18 17:13:26 +01:00
// constructors
PlotRange(const Axis axis);
2014-03-03 14:21:37 +00:00
PlotRange(const Axis axis, const double min, const double max);
2014-03-03 12:41:48 +00:00
// destructor
virtual ~PlotRange(void) = default;
// modifier
virtual void operator()(PlotOptions &option) const;
private:
const Axis axis_;
2014-09-18 17:13:26 +01:00
const bool reset_;
2014-03-03 12:41:48 +00:00
const double min_, max_;
};
2014-04-18 11:29:17 +01:00
class Terminal: public PlotModifier
{
public:
// constructor
Terminal(const std::string &terminal, const std::string &options = "");
// destructor
virtual ~Terminal(void) = default;
// modifier
virtual void operator()(PlotOptions &option) const;
private:
const std::string terminalCmd_;
};
2014-03-17 14:57:46 +00:00
class Title: public PlotModifier
{
public:
// constructor
explicit Title(const std::string &title);
// destructor
virtual ~Title(void) = default;
// modifier
virtual void operator()(PlotOptions &option) const;
private:
const std::string title_;
};
2014-03-03 12:41:48 +00:00
/******************************************************************************
* Plot class *
******************************************************************************/
class Plot
{
public:
class Scale
{
public:
enum
{
reset = 0,
manual = 1 << 0,
log = 1 << 1
};
};
public:
// constructor/destructor
2014-04-03 09:00:53 +01:00
Plot(void);
virtual ~Plot(void) = default;
2014-03-03 12:41:48 +00:00
// plot operations
Plot & operator<<(PlotObject &&command);
Plot & operator<<(PlotModifier &&modifier);
// plot parsing and output
void display(void);
2014-09-18 17:13:26 +01:00
void save(std::string dirName);
friend std::ostream & operator<<(std::ostream &out, const Plot &plot);
2014-04-03 09:00:53 +01:00
// plot reset
void reset(void);
private:
// find gnuplot
void getProgramPath(void);
2014-04-03 09:00:53 +01:00
// default options
void initOptions(void);
private:
// gnuplot execution parameters
2014-03-03 12:41:48 +00:00
std::string gnuplotBin_ {GNUPLOT_BIN};
std::string gnuplotArgs_ {GNUPLOT_ARGS};
std::string gnuplotPath_ {""};
// string buffer for commands
std::ostringstream commandBuffer_;
// stack of created temporary files
2014-09-18 17:13:26 +01:00
std::vector<std::string> tmpFileName_;
// plot content
2014-03-03 12:41:48 +00:00
PlotOptions options_;
std::vector<std::string> headCommand_;
std::vector<std::string> plotCommand_;
};
std::ostream & operator<<(std::ostream &out, const Plot &plot);
END_LATAN_NAMESPACE
#endif // Latan_Plot_hpp_