ååï¼d:id:hecomi:20100708ï¼ï¼åã åï¼d:id:hecomi:20100707ï¼ã«å¼ãç¶ãï¼gnuplotãããããããã¯ã©ã¹ã®ã話ã§ãï¼
è¿½å æ©è½ / 夿´ç®æ
大ãããã®ã¯è¿½å ãã¦ã¾ãããï¼è¿½å æ©è½ã¯ä»¥ä¸ã®éãã§ãï¼
- ç¾å¨ãããããã¦ãããã¼ã¿ããã¡ã¤ã«ã«ä¿å
- ç¾å¨ãããããã¦ãããã¼ã¿ãEPSã«åºå
- ã³ãã³ããæ¸ãã¦ããå¤é¨ãã¡ã¤ã«ãèªã¿è¾¼ã¿ã»å®è¡
å¾ã¯ã¡ããã¡ããã¨ä¸çµ±ä¸ã ã£ã夿°ã®è¨æ³ãªã©ãä¿®æ£ããã¾ããï¼
å ãã¦ï¼Doxygenã«å¯¾å¿ããã³ã¡ã³ããä»å ãã¾ããï¼
Doxygenã¯æ¬å½ã«ä¾¿å©ãªã®ã§ä½¿ã£ããã¨ãªãæ¹ã¯æ¯éã¤ã³ã¹ãã¼ã«ããããã¨ããããããã¾ãï¼http://www.doxygen.jp/ï¼ï¼
ããã§åãæ¢ããï¼èªåã«ã¨ã£ã¦ã¯ä¸è¶³ããªãç¨åº¦ã«ä¸éãæ©è½ãæãã¾ããã®ã§ï¼ãããã§éçºãçµäºãããã¨æãã¾ãï¼
ã³ã¼ãã¯èªç±ã«ä½¿ã£ã¦ããã ãã¦æ§ãã¾ããï¼
ï¼ãã ãï¼ä½ãèµ·ãã¦ã責任ã¯åãããã¾ãï¼
ã½ã¼ã¹ã³ã¼ã
gnuplot.h
#pragma once /*! @file @brief gnuplotã¯ã©ã¹ @author hecomi @date July 9, 2010. */ #include <vector> #include <fstream> #include <string> /*! @brief gnuplotãæ±ãã©ã¤ãã©ãª gnuplotãæ±ãã¯ã©ã¹ã«é¢ããã©ã¤ãã©ãªã§ãï¼ pgnuplotã¸ã¨ãã¤ããç¹ãï¼é¢æ°ããã¼ã¿ãgnuplotãç¨ãã¦ããããããæå©ãããã¾ãï¼ pgnuplot.exeãç°å¢å¤æ°ã¸ã¨ç»é²ããã¦ããå¿ è¦ãããã¾ãï¼ */ class CGnuplot { private: //! ããããããè¦ç´ ã®éå§ä½ç½®ã¨çµäºä½ç½® FILE* Fp; //! ããããããè¦ç´ ã®éå§ä½ç½®ã¨çµäºä½ç½® template <class Container> struct PlotInfo { Container begin; //!< ããããéå§ä½ç½® Container end; //!< ããããçµäºä½ç½® }; /*! @brief ãããã¡ãã©ãã·ã¥ */ void Flush(); /*! @brief 1次å è¦ç´ ããããããã @param[in] x ããããæ å ±ãæ ¼ç´ããxãã¼ã¿ */ template <class T> void PlotX(PlotInfo<T> x) { T it = x.begin; std::ofstream fout(TempFileName); if (fout.fail()) { std::cout << "Error! (@PlotX)" << std::endl; return; } while (it != x.end) { fout << *it << std::endl; it++; } Command("plot '%s' w lines", TempFileName); } /*! @brief 2次å è¦ç´ ããããããã @param[in] x ããããæ å ±ãæ ¼ç´ããxãã¼ã¿ @param[in] y ããããæ å ±ãæ ¼ç´ããyãã¼ã¿ */ template <class T> void PlotXY(PlotInfo<T> x, PlotInfo<T> y) { T itX = x.begin, itY = y.begin; std::ofstream fout(TempFileName); if (fout.fail()) { std::cout << "Error! (@PlotXY)" << std::endl; return; } while (itX != x.end && itY != y.end) { fout << *itX << " " << *itY << std::endl; itX++; itY++; } Command("plot '%s' w lines", TempFileName); } /*! @brief 3次å è¦ç´ ããããããã @param[in] x ããããæ å ±ãæ ¼ç´ããxãã¼ã¿ @param[in] y ããããæ å ±ãæ ¼ç´ããyãã¼ã¿ @param[in] z ããããæ å ±ãæ ¼ç´ããzãã¼ã¿ */ template <class T> void PlotXYZ(PlotInfo<T> x, PlotInfo<T> y, PlotInfo<T> z) { T itX = x.begin, itY = y.begin, itZ = z.begin; std::ofstream fout(TempFileName); if (fout.fail()) { std::cout << "Error! (@PlotXYZ)" << std::endl; return; } while (itX != x.end && itY != y.end && itZ != z.end) { fout << *itX << " " << *itY << " " << *itZ << std::endl; itX++; itY++; itZ++; } Command("splot '%s' w lines", TempFileName); } public: /*! @brief ã³ã³ã¹ãã©ã¯ã¿ */ CGnuplot(); CGnuplot(const char* fileName); /*! @brief ãã¹ãã©ã¯ã¿ */ ~CGnuplot(); /*! @brief 䏿ãã¡ã¤ã«å */ static const char* TempFileName; /*! @brief æ£å¸¸ã«æ©è½ãã¦ãããã©ãã */ bool Check(); /*! @brief printfã©ã¤ã¯ã«gnuplotã®ã³ãã³ããå®è¡ @param[in] format printfã«ç¨ãããã©ã¼ããã @param[in] ... å¯å¤é·å¼æ° */ void Command(const char* format, ...); /*! @brief 颿°ããããã Ex. DrawFunc("sin (x)") @param[in] format ããããå¯¾ç§°é¢æ°æåå */ void DrawFunc(const char* format); /*! @brief 1次å è¦ç´ ããããããã @param[in] cont ãããã対称ã³ã³ãã */ template <class T, template <class A, class Allocator = std::allocator<A> > class Container> void Plot(Container<T> cont) { PlotInfo<Container<T>::iterator> pi = { cont.begin(), cont.end() }; PlotX(pi); } /*! @brief 1次å è¦ç´ ãããããããï¼é åï¼ @param[in] cont ãããã対称é å */ template <class T, int N> void Plot(T (&cont)[N]) { PlotInfo<T*> pi = { &cont[0], &cont[N-1] }; PlotX(pi); } /*! @brief 2次å è¦ç´ ããããããã @param[in] contX ãããã対称ã³ã³ããï¼xï¼ @param[in] contY ãããã対称ã³ã³ããï¼yï¼ */ template <class T, template <class A, class Allocator = std::allocator<A> > class Container> void Plot(Container<T> contX, Container<T> contY) { PlotInfo<Container<T>::iterator> piX = { contX.begin(), contX.end() }, piY = { contY.begin(), contY.end() }; PlotXY(piX, piY); } /*! @brief 2次å è¦ç´ ãããããããï¼é åï¼ @param[in] contX ãããã対称é åï¼xï¼ @param[in] contY ãããã対称é åï¼yï¼ */ template <class T, int N, int M> void Plot(T (&contX)[N], T (&contY)[M]) { PlotInfo<T*> piX = { &contX[0], &contX[N] }, piY = { &contY[0], &contY[M] }; PlotXY(piX, piY); } /*! @brief 3次å è¦ç´ ããããããã @param[in] contX ãããã対称ã³ã³ããï¼xï¼ @param[in] contY ãããã対称ã³ã³ããï¼yï¼ @param[in] contZ ãããã対称ã³ã³ããï¼zï¼ */ template <class T, template <class A, class Allocator = std::allocator<A> > class Container> void Plot(Container<T> contX, Container<T> contY, Container<T> contZ) { PlotInfo<Container<T>::iterator> piX = { contX.begin(), contX.end() }, piY = { contY.begin(), contY.end() }, piZ = { contZ.begin(), contZ.end() }; PlotXYZ(piX, piY, piZ); } /*! @brief 3次å è¦ç´ ãããããããï¼é åï¼ @param[in] contX ãããã対称é åï¼xï¼ @param[in] contY ãããã対称é åï¼yï¼ @param[in] contZ ãããã対称é åï¼zï¼ */ template <class T, int N, int M, int L> void Plot(T (&contX)[N], T (&contY)[M], T (&contZ)[L]) { PlotInfo<T*> piX = { &contX[0], &contX[N] }, piY = { &contY[0], &contY[M] }, piZ = { &contZ[0], &contZ[L] }; PlotXYZ(piX, piY, piZ); } /*! @brief ã©ãã«ãã»ãã @param[in] formatX Xã©ãã«å @param[in] formatY Yã©ãã«å */ void SetLabel(const char* formatX, const char* formatY); /*! @brief ããããç¯å²ãæå® @param[in] min x軸ããããç¯å²æå°å¤ @param[in] min y軸ããããç¯å²æå°å¤ */ void SetXRange(const double min, const double max); /*! @brief ããããç¯å²ãæå® @param[in] min y軸ããããç¯å²æå°å¤ @param[in] min y軸ããããç¯å²æå°å¤ */ void SetYRange(const double min, const double max); /*! @brief ãªãããã */ void Replot(); /*! @brief ç¾å¨ãããããã¦ãããã¼ã¿ããã¡ã¤ã«ã«æ¸ãåºã ä¸èº«ã¯TempFileNameã«ä¿åããã¦ãããã¼ã¿ãã³ãã¼ãã¦ããã ã @param[in] fileName æ¸ãåºãå ãã¡ã¤ã«å */ void DumpToFile(const char* fileName); /*! @brief ç¾å¨ãããããã¦ãããã¼ã¿ãEPSã«æ¸ãåºã @param[in] fileName æ¸ãåºãå ãã¡ã¤ã«å */ void DumpToEps(const char* fileName); /*! @brief å¤é¨ãã¡ã¤ã«ããã³ãã³ããèªã¿è¾¼ã¿å®è¡ @param[in] fileName èªã¿è¾¼ã¿å ãã¡ã¤ã«å */ void CommandFromFile(const char* fileName); };
gnuplot.cpp
#pragma warning(disable: 4996) #include <stdio.h> #include <stdarg.h> #include <iostream> #include "gnuplot.h" const char* CGnuplot::TempFileName = "temp.dat"; CGnuplot::CGnuplot() { Fp = _popen("pgnuplot", "w"); if (Fp == NULL) { printf("pipe error\n"); exit(EXIT_FAILURE); } } CGnuplot::CGnuplot(const char* fileName) { Fp = fopen(fileName, "w"); if (Fp == NULL) { printf("pipe error\n"); exit(EXIT_FAILURE); } } CGnuplot::~CGnuplot() { _pclose(Fp); } bool CGnuplot::Check() { if (Fp == NULL) { return false; } return true; } void CGnuplot::Command(const char* format, ...) { char buf[1024]; va_list ap; va_start(ap, format); vsprintf(buf, format, ap); va_end(ap); fprintf(Fp, "%s\n", buf); Flush(); } void CGnuplot::DrawFunc(const char* format) { Command("plot %s", format); } void CGnuplot::SetLabel(const char* formatX, const char* formatY) { Command("set xlabel '%s'", formatX); Command("set ylabel '%s'", formatY); } void CGnuplot::SetXRange(const double min, const double max) { Command("set xrange [%f:%f]", min, max); } void CGnuplot::SetYRange(const double min, const double max) { Command("set yrange [%f:%f]", min, max); } void CGnuplot::Flush() { fflush(Fp); } void CGnuplot::Replot() { Command("replot"); } void CGnuplot::DumpToFile(const char* fileName) { std::ofstream fout(fileName, std::ios_base::binary); std::ifstream fin(TempFileName, std::ios_base::binary); if (fin.fail() || fout.fail()) { std::cout << "Error! (@DumpToFile)" << std::endl; return; } while (!fin.eof()) { const int BUF_SIZE = 4096; char buf[BUF_SIZE]; fin.read(buf, BUF_SIZE); fout.write(buf, BUF_SIZE); } } void CGnuplot::DumpToEps(const char* fileName) { Command("set term postscript eps enhanced"); Command("set output '%s'", fileName); Command("replot"); Command("set output"); Command("set terminal window"); } void CGnuplot::CommandFromFile(const char* fileName) { std::ifstream fin(fileName); if (fin.fail()) { std::cout << "Error! (@CommandFromFile)" << std::endl; return; } std::string str; while (!fin.eof()) { std::getline(fin, str); Command(str.c_str()); } }